Flashlight
A C++ standalone library for machine learning
Install / Use
/learn @flashlight/FlashlightREADME
Flashlight is a fast, flexible machine learning library written entirely in C++ from the Facebook AI Research and the creators of Torch, TensorFlow, Eigen and Deep Speech. Its core features include:
- Total internal modifiability including internal APIs for tensor computation.
- A small footprint, with the core clocking in at under 10 MB and 20k lines of C++.
- High-performance defaults featuring just-in-time kernel compilation with modern C++ via the ArrayFire tensor library.
- An emphasis on efficiency and scale.
Native support in C++ and simple extensibility makes Flashlight a powerful research framework that enables fast iteration on new experimental setups and algorithms with little unopinionation and without sacrificing performance. In a single repository, Flashlight provides apps for research across multiple domains:
- Automatic speech recognition (formerly wav2letter project) — Documentation | Tutorial
- Image classification
- Object detection
- Language modeling
Project Layout
Flashlight is broken down into a few parts:
flashlight/libcontains kernels and standalone utilities for audio processing and more.flashlight/flis the core tensor interface and neural network library using the ArrayFire tensor library by default.flashlight/pkgare domain packages for speech, vision, and text built on the core.flashlight/appare applications of the core library to machine learning across domains.
Quickstart
First, build and install Flashlight and link it to your own project.
Sequential forms a sequence of Flashlight Modules for chaining computation.
#include <flashlight/fl/flashlight.h>
Sequential model;
model.add(View(fl::Shape({IM_DIM, IM_DIM, 1, -1})));
model.add(Conv2D(
1 /* input channels */,
32 /* output channels */,
5 /* kernel width */,
5 /* kernel height */,
1 /* stride x */,
1 /* stride y */,
PaddingMode::SAME; /* padding mode */,
PaddingMode::SAME; /* padding mode */));
model.add(ReLU());
model.add(Pool2D(
2 /* kernel width */,
2 /* kernel height */,
2 /* stride x */,
2 /* stride y */));
model.add(Conv2D(32, 64, 5, 5, 1, 1, PaddingMode::SAME, PaddingMode::SAME));
model.add(ReLU());
model.add(Pool2D(2, 2, 2, 2));
model.add(View(fl::Shape({7 * 7 * 64, -1})));
model.add(Linear(7 * 7 * 64, 1024));
model.add(ReLU());
model.add(Dropout(0.5));
model.add(Linear(1024, 10));
model.add(LogSoftmax());
Performing forward and backward computation is straightforwards:
auto output = model.forward(input);
auto loss = categoricalCrossEntropy(output, target);
loss.backward();
</details>
See the MNIST example for a full tutorial including a training loop and dataset abstractions.
Variable is a tape-based abstraction that wraps Flashlight tensors. Tape-based Automatic differentiation in Flashlight is simple and works as you'd expect.
auto A = Variable(fl::rand({1000, 1000}), true /* calcGrad */);
auto B = 2.0 * A;
auto C = 1.0 + B;
auto D = log(C);
D.backward(); // populates A.grad() along with gradients for B, C, and D.
</details>
Building and Installing
Install with vcpkg | With Docker | From Source | From Source with vcpkg | Build Your Project with Flashlight
Requirements
At minimum, compilation requires:
- A C++ compiler with good C++17 support (e.g. gcc/g++ >= 7)
- CMake — version 3.10 or later, and
make - A Linux-based operating system.
See the full dependency list for more details if building from source.
Instructions for building/installing Python bindings can be found here.
Flashlight Build Setups
Flashlight can be broken down into several components as described above. Each component can be incrementally built by specifying the correct build options.
There are two ways to work with Flashlight:
- As an installed library that you link to with your own project. This is best for building standalone applications dependent on Flashlight.
- With in-source development where the Flashlight project source is changed and rebuilt. This is best if customizing/hacking the core framework or the Flashlight-provided app binaries.
Flashlight can be built in one of two ways:
- With
vcpkg, a C++ package manager. - From source by installing dependencies as needed.
Installing Flashlight with vcpkg
Library Installation with vcpkg
Flashlight is most-easily built and installed with vcpkg. Both the CUDA and CPU backends are supported with vcpkg. For either backend, first install Intel MKL. For the CUDA backend, install CUDA >= 9.2, cuDNN, and NCCL. Then, after installing vcpkg, install the libraries and core with:
./vcpkg/vcpkg install flashlight-cuda # CUDA backend, OR
./vcpkg/vcpkg install flashlight-cpu # CPU backend
To install Flashlight apps, check the features available for installation by running ./vcpkg search flashlight-cuda or ./vcpkg search flashlight-cpu. Each app is a "feature": for example, ./vcpkg install flashlight-cuda[asr] installs the ASR app with the CUDA backend.
Below is the currently-supported list of features (for each of flashlight-cuda and flashlight-cpu):
flashlight-{cuda/cpu}[lib] # Flashlight libraries
flashlight-{cuda/cpu}[nn] # Flashlight neural net library
flashlight-{cuda/cpu}[asr] # Flashlight speech recognition app
flashlight-{cuda/cpu}[lm] # Flashlight language modeling app
flashlight-{cuda/cpu}[imgclass] # Flashlight image classification app
Flashlight app binaries are also built for the selected features and are installed into the vcpkg install tree's tools directory.
Integrating Flashlight into your own project with is simple using vcpkg's CMake toolchain integration.
From-Source Build with vcpkg
First, install the dependencies for your backend of choice using vcpkg (click to expand the below):
Related Skills
YC-Killer
2.7kA library of enterprise-grade AI agents designed to democratize artificial intelligence and provide free, open-source alternatives to overvalued Y Combinator startups. If you are excited about democratizing AI access & AI agents, please star ⭐️ this repository and use the link in the readme to join our open source AI research team.
openclaw-plugin-loom
Loom Learning Graph Skill This skill guides agents on how to use the Loom plugin to build and expand a learning graph over time. Purpose - Help users navigate learning paths (e.g., Nix, German)
best-practices-researcher
The most comprehensive Claude Code skills registry | Web Search: https://skills-registry-web.vercel.app
Leadership-Mirror
Product Overview Project Purpose Hack Atria is a leadership development and team management platform that provides AI-powered insights, feedback analysis, and learning resources to help leaders
