k2 FSA and FST Algorithms with PyTorch Integration
FSA/FST algorithms, differentiable, with PyTorch compatibility.
At a glance
- What is it?
- k2 provides C++ and CUDA implementations of finite state algorithms, including pruned composition, through a PyTorch-compatible Python interface. It targets speech recognition researchers who need differentiable decoding, CTC or LF-MMI training, lattice rescoring, and confidence estimation, but its native-code build introduces deployment constraints.
- Who is it for?
- Speech recognition researchers working on integrated decoding and training objectives should evaluate k2 when native C++ or GPU execution and compatibility with finite state algorithms justify a source-built dependency. Projects that need a simple Python-only package, predictable release support, or undocumented rollback procedures should check the installation documentation and version compatibility first.
- Can I use it commercially?
- Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 119 days ago.
- What is it written in?
- Mainly Cuda, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 7, 2026, and from our analysis. They are not legal advice.
Star history
- k2-fsa/k21,370 ★ · +2
| Repository | 2026-10-17 | 2026-11-07 |
|---|---|---|
| k2-fsa/k2 | 1.4k | 1.4k |
2 Stars in the last 21 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
k2's Role in Speech Recognition Toolchains
k2 addresses the boundary between finite state algorithms and machine learning frameworks. The project documentation describes FSA and FST operations as a way to connect structured decoding algorithms with autograd systems such as PyTorch and TensorFlow. Its stated speech recognition use cases include cross-entropy, CTC, and MMI objectives, as well as systems that combine multiple decoding passes for lattice rescoring and confidence estimation.
The intended audience is therefore narrower than a general machine learning audience. k2 is relevant to engineers building or modifying ASR systems, especially where decoding and training need to share finite state representations. It is also relevant to developers experimenting with pruned composition, where a generic FSA is combined with a dense FSA representing symbol log probabilities produced by a neural network. The README presents that composition as useful for fast ASR decoding and for CTC and LF-MMI training, while being explicit that the project is not claiming a direct Word Error Rate advantage over existing technology.
That distinction matters. k2 is positioned as an extensible framework for combining algorithms and objectives, not as a ready-made ASR model. The repository says that speech recognition recipes are maintained in the separate icefall repository. Users therefore need another project for concrete recipes and should not treat k2 itself as a complete speech recognition application.
Ragged Tensors and Sparse Automaton Storage
The implementation is built around a templated C++ class named `Ragged`, described as similar in purpose to TensorFlow's `RaggedTensor`, but with a different design. Finite state automata are represented as Ragged tensors whose element type is a structure describing an arc. This representation is a central architectural choice because automata contain variable-length state and arc relationships that do not naturally fit a regular rectangular tensor.
The README states that most algorithms operate through C++11 lambdas defined directly in the C++ implementations. Those lambdas work on data pointers, and CUDA implementations can process elements in parallel. Rather than expressing most work as a composition of small tensor operations, k2 uses coding patterns intended to expose expensive computations as parallel work. Reduction operations, including exclusive-prefix-sum operations, use NVIDIA's `cub` library. The same source also supports AMD GPUs through ROCm/HIP, where `hipCUB` and `rocThrust` provide the relevant primitives.
This design favors control over memory and parallel execution, but it also makes the project less portable at the Python level. A user interacts with native code rather than only with ordinary Python objects. The repository's mixture of C++, CUDA, and Python bindings means debugging, building, and matching library versions can matter more than for a typical pure Python package.
Top-Down Autograd Through Decoding Relationships
k2 does not expose conventional autograd references throughout its C++ implementation. Instead, the README describes a top-down design for handling derivatives. For a best-path operation, the implementation records which input arc corresponds to each output arc. More complex algorithms may represent an output arc as the result of a sum of probabilities from several input arcs.
At the Python boundary, that relationship can be used by a PyTorch `Function`, which performs sparse operations to propagate derivatives back to weights. This approach connects an automaton algorithm with PyTorch's autograd graph without requiring every finite state primitive to be expressed as a separate differentiable tensor operation. The project documentation describes this as a way to implement differentiation from the overall algorithm downward.
The practical implication is that k2 is designed for algorithms whose output has a known relationship to input weights. It is not simply a collection of ordinary tensor functions that can be combined freely in Python. Users adopting it must understand how the selected operation records arc provenance and how those records are handled by the Python integration. The README provides the conceptual explanation, but it does not present a complete API-level derivative example for a new integration.
Building k2 From Source
The repository's setup.py gives the direct installation and verification commands. It supports Python 3 and rejects Python versions below 3.6. The package requirements list `torch>=1.6.0`, plus `typing_extensions` for Python versions below 3.8. These are the documented dependencies for this repository layout; setup.py also contains platform and PyTorch version handling that can affect the build.
To install from the source tree, use the command documented in setup.py. After installation, the documented version check provides a concrete first verification step:
python3 setup.py install
python3 -m k2.versionThe first command builds and installs k2. The second should complete without an import or version-reporting error. If CUDA is not required, setup.py documents disabling it through the `K2_CMAKE_ARGS` environment variable:
export K2_CMAKE_ARGS="-DK2_WITH_CUDA=OFF"
python3 setup.py installThat option produces a build without CUDA support. It does not imply that every PyTorch, compiler, platform, and dependency combination is supported. The README points to the installation documentation for ROCm and source-build details, while the provided setup.py excerpt does not document a universal one-command install for every platform.
PyTorch and GPU Backend Compatibility
The README says that the C++ implementation is wrapped into Python with `pybind11` and that integration with PyTorch has been completed. The repository's primary language is listed as CUDA, but the implementation also includes ordinary C++ and supports AMD GPU primitives through ROCm/HIP. This makes hardware choice part of the adoption decision rather than a separate operational detail.
For NVIDIA systems, the documented build controls include the normal CUDA path and the `K2_CMAKE_ARGS` switch shown in setup.py. For AMD systems, the README directs users to separate installation documentation for the ROCm backend. The repository does not, in the provided files, state a complete matrix of supported CUDA versions, ROCm versions, PyTorch releases, compilers, or Python distributions.
PyTorch compatibility is also version-sensitive in practice because setup.py contains a helper named `fix_pytorch_1_12()` and accesses PyTorch's installed directory structure. That is evidence of build logic tied to PyTorch internals, although it does not establish compatibility with every current release. The repository requirements specify only `torch>=1.6.0`, which is a minimum dependency statement rather than proof that all later versions work together. A new user should inspect the full installation documentation and build configuration before selecting a production environment.
Release Cadence and Upgrade Boundaries
The repository is not archived, and its last push was on 2026-07-11. The available release history lists v1.24.4 on 2023-11-11, v1.24.1 on 2023-04-30, and v1.24.0 on 2023-04-27. This separates source activity from packaged release cadence: the repository can receive pushes while the most recent listed package release remains older.
That gap is relevant to k2 because users must build native C++ and CUDA components and coordinate them with PyTorch and the host toolchain. The provided files do not document a rollback procedure, a compatibility matrix, or a formal upgrade policy. setup.py documents uninstalling a package with `pip uninstall k2`, but that removes an installed package and does not explain how to return to a previous source revision or wheel.
The license is Apache-2.0. The repository includes a `LICENSE` file and identifies that license in its metadata. The license affects how the code may be used under its terms, but this review does not interpret those terms or provide legal advice. Upgrade planning should focus on the concrete build boundary: confirm the PyTorch version, CUDA or ROCm backend, Python version, and source revision together.
Editorial conclusion
Speech recognition researchers working on integrated decoding and training objectives should evaluate k2 when native C++ or GPU execution and compatibility with finite state algorithms justify a source-built dependency. Projects that need a simple Python-only package, predictable release support, or undocumented rollback procedures should check the installation documentation and version compatibility first. Adoption depends on confirming that k2 builds against the intended PyTorch and platform combination, because setup.py embeds environment-specific build choices.
Frequently asked questions
How do I install k2 and check that it works?
The documented source installation command is `python3 setup.py install`, followed by `python3 -m k2.version`. setup.py states that this installs k2 and provides a command for checking the installed version.
Can k2 be built without CUDA?
Yes, according to setup.py. Set `K2_CMAKE_ARGS="-DK2_WITH_CUDA=OFF"` before running `python3 setup.py install` to build without CUDA support.
What is k2 used for in speech recognition?
The README describes k2 as a framework for FSA and FST algorithms that can support ASR decoding, CTC and LF-MMI training, lattice rescoring, confidence estimation, and combinations of training objectives. It also says that concrete speech recognition recipes are hosted in the separate icefall repository.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/k2-fsa-k2)Star history chart for your README
[](https://hysenlabs.com/projects/k2-fsa-k2)Was this useful?