Flashlight: High-Performance C++ Machine Learning Library for Researchers

Jul 10, 2025

Introduction

Modern machine learning frameworks often prioritize the end-user practitioner, leaving systems researchers struggling with bloated codebases and rigid abstractions that hinder low-level innovation. Flashlight, a high-performance C++ standalone library developed by Meta AI (formerly Facebook Research), addresses this gap by providing a modular, lightweight framework designed specifically for those who need to modify the internal workings of their ML tools. With its minimal footprint and focus on efficiency, Flashlight enables researchers to rapidly prototype and experiment with novel computational paradigms without sacrificing the performance required for large-scale training.

What Is Flashlight?

Flashlight is a C++ standalone library for machine learning that prioritizes internal modifiability and low framework overhead. It is designed as a research-first framework, meaning it is not intended for out-of-the-box production use but rather as a tool for systems researchers to experiment with new designs and implementations of machine learning tools. It is distributed under the Apache License 2.0.

The library is built on a shallow stack of basic abstractions, leveraging the ArrayFire tensor library by default for high-performance defaults and just-in-time kernel compilation. This architecture allows the core of the library to remain incredibly small—often under 10 MB and 20k lines of C++—making it significantly easier to modify and iterate on than traditional deep learning frameworks.

Why Flashlight Matters

For years, the democratization of machine learning has been driven by high-level APIs in frameworks like PyTorch and TensorFlow. While these tools are exceptional for building models, they often become “black boxes” for researchers interested in the underlying systems. When a researcher wants to implement a new memory manager, a custom tensor operation, or a novel distributed training strategy, they often face a steep learning curve and a massive codebase that is difficult to navigate.

Flashlight matters because it returns the focus to the systems level. By providing a shallow stack of modular abstractions, it allows researchers to replace or modify any component—from the tensor implementation to the memory manager—with minimal friction. This nimbleness is critical for pushing the boundaries of how machine learning is computed, which in turn benefits the broader ecosystem by providing the blueprints for future optimizations in the widely used libraries that follow.

The project’s traction is evident in its application to complex domains like Automatic Speech Recognition (ASR), where it has been used to power the wav2letter project, and its ability to maintain high performance across multiple modalities (speech, vision, and text) within a single codebase.

Key Features

  • Total Internal Modifiability: Every component of Flashlight, including internal APIs for tensor computation, can be modified or replaced. This allows researchers to implement custom memory managers or tensor backends.
  • Minimal Footprint: The core library is exceptionally lightweight, consisting of under 20k lines of C++ code and a binary size under 10 MB, reducing compilation times and cognitive load for researchers.
  • High-Performance Defaults: By leveraging ArrayFire, Flashlight provides just-in-time (JIT) kernel compilation with modern C++, ensuring that experimental setups do not sacrifice raw speed.
  • Modular Domain Packages: The library includes specialized packages for speech, vision, and text, allowing researchers to apply the core framework to specific modalities without needing to build everything from scratch.
  • Native C++ Support: Written entirely in modern C++, Flashlight provides the parallelism and speed necessary for high-performance computing environments, avoiding the overhead associated with high-level language wrappers.
  • Fast Autograd: The framework includes a lightweight autograd system that automatically computes derivatives of chained operations, a staple for deep neural network training.
  • Customizable Data Loaders: Flashlight provides a DATASET class that abstracts the notion of a sample, allowing for trivially composable pipelines to transform, resample, or parallelize data construction using native C++ threads.
  • Distributed and Mixed-Precision Training: The library includes APIs for distributed training and mixed-precision computation, enabling the scaling of research models to large datasets.

How Flashlight Compares

Feature Flashlight PyTorch TensorFlow
Primary Target Audience Systems Researchers ML Practitioners / Researchers Industry / Production
Core Language C++ Python (C++ Core) Python (C++ Core)
Internal Modifiability Very High (Shallow Stack) Moderate Low to Moderate
Codebase Size Minimal (<20k lines) Massive Massive
Production Readiness Low (Research-First) High High

While PyTorch and TensorFlow are the industry standards for building and deploying models, they are designed as comprehensive ecosystems. This means they include everything from data loading to deployment pipelines, which results in a massive codebase that is difficult for a single researcher to modify. In contrast, Flashlight is not trying to replace them; it is a specialized tool for the systems researcher who wants to change how a tensor is computed or how memory is managed.

The primary tradeoff is usability. PyTorch and TensorFlow provide high-level abstractions that make model building fast. Flashlight requires the user to work in C++, which has a steeper learning curve and slower iteration speed for model architecture changes. However, for those whose research goal is the framework itself, Flashlight’s simplicity is its greatest strength.

Getting Started: Installation

Flashlight is primarily a C++ library, and installation involves building from source. Depending on your target backend, you can use different methods.

Building from Source (Linux)

The most common way to install Flashlight is by cloning the repository and using CMake to build the project. This requires a C++17 compatible compiler (e.g., gcc/g++ >= 7) and CMake 3.16 or later.

git clone https://github.com/flashlight/flashlight.git
cd flashlight
mkdir build && cd build
cmake ..
make -j8

Using vcpkg

Flashlight can be installed via the vcpkg package manager, allowing you to specify the backend (CUDA or CPU) as a feature.

vcpkg install flashlight-cuda[asr]
# or for CPU only
vcpkg install flashlight-cpu[asr]

Prerequisites

Ensure you have the following installed on your system:

  • C++17 Compiler (gcc/g++ >= 7)
  • CMake (3.16+)
  • ArrayFire (The default tensor library used by Flashlight)
  • CUDA Toolkit (if building with CUDA backend)

How to Use Flashlight

Using Flashlight involves defining a model as a sequence of modules. The core API is designed to be intuitive for those familiar with deep learning frameworks, but it is implemented in native C++.

To start, you include the Flashlight header and define a model using the Sequential class. This class allows you to chain together various layers (modules) such as linear layers, convolutions, and activation functions. The workflow typically follows a standard ML pipeline: data loading via the DATASET class, forward pass through the model, and parameter updates using an optimizer.

If you are using the domain-specific packages, such as Flashlight Text, you can leverage pre-built utilities for tokenization and beam search decoding, which are highly optimized for speech and natural language processing tasks.

Code Examples

The following examples demonstrate how to build a simple model in Flashlight. These examples are derived from the repository’s core API.

Simple Sequential Model

This example shows how to create a basic neural network with a linear layer and a ReLU activation function.

#include <flashlight/fl/flashlight.h>

int main() {
    // Define a sequential model
    Sequential model;
    model.add(Linear(10, 20));
    model.add(ReLU());
    model.add(Linear(20, 1));n
    // Create a dummy input tensor
    Tensor input = Tensor::randn({10});
    
    // Forward pass
    Tensor output = model.forward(input);
    
    return 0;
}

Custom Module Implementation

Because Flashlight is designed for modifiability, the you can easily create your own custom modules by inheriting from the Module class.

#include <flashlight/fl/flashlight.h>

class MyCustomLayer : public Module {
public:    MyCustomLayer(int in_features, int out_features) {
        // Initialize weights
        this->weights = Variable::randn({out_features, in_features});
        this->bias = Variable::randn({out_features});
    }

    Tensor forward(const Tensor& input) override {
        return (input * this->weights).transpose() + this->bias;
    }

private:
    Variable weights;
    Variable bias;
};

Real-World Use Cases

Flashlight is most effective when the framework itself is the object of research. Here are three concrete scenarios where Flashlight shines:

  • Developing New Tensor Backends: A systems researcher can use Flashlight to prototype a new tensor library or replace ArrayFire with a custom implementation to test the impact of memory layout on training speed.
  • Optimizing ASR Pipelines: Because Flashlight provides high-performance C++ implementations of beam search decoding and CTC loss, it is the ideal tool for researchers building next-generation Automatic Speech Recognition systems.
  • Low-Latency Inference: For developers who need to deploy models in environments where Python overhead is unacceptable, Flashlight’s native C++ implementation allows for the creation of extremely low-latency inference engines.

Contributing to Flashlight

Contributions to Flashlight are welcome and encouraged. Since the project is designed for research, the process is standard for high-performance C++ projects.

To contribute, you should fork the repository and create a feature branch from the main branch. When submitting a pull request, ensure that you have added tests for any new features and that the existing test suite passes. The project follows the Apache License 2.0, and contributors are expected to adhere to the the project’s code of conduct.

Community and Support

The Flashlight community consists primarily of systems researchers and C++ developers. Support is available through the following official channels:

  • GitHub Discussions: The primary hub for reporting bugs and requesting features.
  • Gitter: The project maintains a chat room at gitter.im/flashlight-ml/ for real-time discussion.
  • Official Documentation: Detailed API references and guides are available at fl.readthedocs.io.

Conclusion

Flashlight is not a general-purpose machine learning framework for the average developer. It is a precision tool for the systems researcher who needs to total control over the underlying computation. By stripping away the bloat of modern frameworks and providing a shallow stack of modular C++ abstractions, it enables the innovation that eventually trickles down to the rest of the ML community.

If your goal is to build a model quickly, PyTorch or TensorFlow are the right choice. However, if your goal is to research how machine learning is computed, Flashlight is the {id: 204, “type”: “object”, “properties”: { “content”: { “type”: “string”, “description”: “content-” } } } best tool for the job. Star the repo, try the quickstart, and join the community of systems researchers pushing the boundaries of AI computation.

What is Flashlight and what problem does it solve?

Flashlight is a C++ standalone machine learning library designed for systems researchers. It solves the problem of bloated, rigid frameworks that make it difficult to modify the internal workings of ML tools, such as tensor computation and memory management.

How do I install Flashlight?

Flashlight can be installed by cloning the repository and building from source using CMake, or by using the vcpkg package manager to install the CUDA or CPU backends.

How does Flashlight compare to PyTorch?

Unlike PyTorch, which is designed for practitioners and researchers to build models, Flashlight is a research-first framework designed for systems researchers to modify the framework itself. It has a much smaller codebase and is written entirely in C++.

Can I use Flashlight for production deployment?

According to the project’s own documentation, Flashlight is a research-first framework and is not intended for out-of-the-box production use.

What is the ArrayFire tensor library?

ArrayFire is the default tensor library used by Flashlight to provide high-performance defaults and just-in-time kernel compilation for tensor operations.

Can I use Flashlight for speech recognition?

Yes, Flashlight provides specialized packages and applications for Automatic Speech Recognition (ASR), including high-performance implementations of beam search decoding and CTC loss.

What license does Flashlight use?

Flashlight is licensed under the Apache License 2.0, allowing for both personal and commercial use.