Skip to main content

FSA/FST algorithms, intended to (eventually) be interoperable with PyTorch and similar

Project description

k2

The vision of k2 is to be able to seamlessly integrate Finite State Automaton (FSA) and Finite State Transducer (FST) algorithms into autograd-based machine learning toolkits like PyTorch and TensorFlow. For speech recognition applications, this should make it easy to interpolate and combine various training objectives such as cross-entropy, CTC and MMI and to jointly optimize a speech recognition system with multiple decoding passes including lattice rescoring and confidence estimation. We hope k2 will have many other applications as well.

One of the key algorithms that we want to make efficient in the short term is pruned composition of a generic FSA with a "dense" FSA (i.e. one that corresponds to log-probs of symbols at the output of a neural network). This can be used as a fast implementation of decoding for ASR, and for CTC and LF-MMI training. This won't give a direct advantage in terms of Word Error Rate when compared with existing technology; but the point is to do this in a much more general and extensible framework to allow further development of ASR technology.

Implementation

A few key points on our implementation strategy.

Most of the code is in C++ and CUDA. We implement a templated class Ragged, which is quite like TensorFlow's RaggedTensor (actually we came up with the design independently, and were later told that TensorFlow was using the same ideas). Despite a close similarity at the level of data structures, the design is quite different from TensorFlow and PyTorch. Most of the time we don't use composition of simple operations, but rely on C++11 lambdas defined directly in the C++ implementations of algorithms. The code in these lambdas operate directly on data pointers and, if the backend is CUDA, they can run in parallel for each element of a tensor. (The C++ and CUDA code is mixed together and the CUDA kernels get instantiated via templates).

It is difficult to adequately describe what we are doing with these Ragged objects without going in detail through the code. The algorithms look very different from the way you would code them on CPU because of the need to avoid sequential processing. We are using coding patterns that make the most expensive parts of the computations "embarrassingly parallelizable"; the only somewhat nontrivial CUDA operations are generally reduction-type operations such as exclusive-prefix-sum, for which we use NVidia's cub library. Our design is not too specific to the NVidia hardware and the bulk of the code we write is fairly normal-looking C++; the nontrivial CUDA programming is mostly done via the cub library, parts of which we wrap with our own convenient interface.

The Finite State Automaton object is then implemented as a Ragged tensor templated on a specific data type (a struct representing an arc in the automaton).

Autograd

If you look at the code as it exists now, you won't find any references to autograd. The design is quite different to TensorFlow and PyTorch (which is why we didn't simply extend one of those toolkits). Instead of making autograd come from the bottom up (by making individual operations differentiable) we are implementing it from the top down, which is much more efficient in this case (and will tend to have better roundoff properties).

An example: suppose we are finding the best path of an FSA, and we need derivatives. We implement this by keeping track of, for each arc in the output best-path, which input arc it corresponds to. (For more complex algorithms an arc in the output might correspond to a sum of probabilities of a list of input arcs). We can make this compatible with PyTorch/TensorFlow autograd at the Python level, by, for example, defining a Function class in PyTorch that remembers this relationship between the arcs and does the appropriate (sparse) operations to propagate back the derivatives w.r.t. the weights.

Current state of the code

A lot of the code is still unfinished (Sep 11, 2020). We finished the CPU versions of many algorithms and this code is in k2/csrc/host/; however, after that we figured out how to implement things on the GPU and decided to change the interfaces so the CPU and GPU code had a more unified interface. Currently in k2/csrc/ we have more GPU-oriented implementations (although these algorithms will also work on CPU). We had almost finished the Python wrapping for the older code, in the k2/python/ subdirectory, but we decided not to release code with that wrapping because it would have had to be reworked to be compatible with our GPU algorithms. Instead we will use the interfaces drafted in k2/csrc/ e.g. the Context object (which encapsulates things like memory managers from external toolkits) and the Tensor object which can be used to wrap tensors from external toolkits; and wrap those in Python (using pybind11). The code in host/ will eventually be either deprecated, rewritten or wrapped with newer-style interfaces.

Plans for initial release

We hope to get the first version working in early October. The current short-term aim is to finish the GPU implementation of pruned composition of a normal FSA with a dense FSA, which is the same as decoder search in speech recognition and can be used to implement CTC training and lattice-free MMI (LF-MMI) training. The proof-of-concept that we will release initially is something that's like CTC but allowing more general supervisions (general FSAs rather than linear sequences). This will work on GPU. The same underlying code will support LF-MMI so that would be easy to implement soon after. We plan to put example code in a separate repository.

Plans after initial release

We will then gradually implement more algorithms in a way that's compatible with the interfaces in k2/csrc/. Some of them will be CPU-only to start with. The idea is to eventually have very rich capabilities for operating on collections of sequences, including methods to convert from a lattice to a collection of linear sequences and back again (for purposes of neural language model rescoring, neural confidence estimation and the like).

Quick start

Want to try it out without installing anything? We have setup a Google Colab.

Caution: k2 is not nearly ready for actual use! We are still coding the core algorithms, and hope to have an early version working by early October.

Project details


Release history Release notifications | RSS feed

This version

1.9

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

k2-1.9-py38-none-any.whl (77.7 MB view details)

Uploaded Python 3.8

k2-1.9-py37-none-any.whl (77.7 MB view details)

Uploaded Python 3.7

k2-1.9-py36-none-any.whl (77.7 MB view details)

Uploaded Python 3.6

k2-1.9-cp38-cp38-macosx_10_15_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

k2-1.9-cp37-cp37m-macosx_10_15_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

k2-1.9-cp36-cp36m-macosx_10_15_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.6m macOS 10.15+ x86-64

File details

Details for the file k2-1.9-py38-none-any.whl.

File metadata

  • Download URL: k2-1.9-py38-none-any.whl
  • Upload date:
  • Size: 77.7 MB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.11

File hashes

Hashes for k2-1.9-py38-none-any.whl
Algorithm Hash digest
SHA256 ab9e43fecd0ad889cd5f29598dcf3005cf84a51b5a18d1b9b50b48e63803e112
MD5 820859fd87d4920f3c3f78765e00c2bd
BLAKE2b-256 c059e8df7d4a1a79e1bc1b6c7cb734c6d1f16e70b9bf5e46c6bc33c268534ffa

See more details on using hashes here.

Provenance

File details

Details for the file k2-1.9-py37-none-any.whl.

File metadata

  • Download URL: k2-1.9-py37-none-any.whl
  • Upload date:
  • Size: 77.7 MB
  • Tags: Python 3.7
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.12

File hashes

Hashes for k2-1.9-py37-none-any.whl
Algorithm Hash digest
SHA256 9d2bebdef166be34a9c993446ce2eefdb519918d33603fb213bc1b947d9a5fb1
MD5 ce1272d3304a7befa1ff3390b5209b30
BLAKE2b-256 c6a65fb52c844e33f7588f574835bb90182b7b25ea3ea269f6307fbe17099620

See more details on using hashes here.

Provenance

File details

Details for the file k2-1.9-py36-none-any.whl.

File metadata

  • Download URL: k2-1.9-py36-none-any.whl
  • Upload date:
  • Size: 77.7 MB
  • Tags: Python 3.6
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.15

File hashes

Hashes for k2-1.9-py36-none-any.whl
Algorithm Hash digest
SHA256 d140a334ed5fda233230289604bef6b1c0b170d5987d4e4d7f8cadc5d90e7124
MD5 2cf2d1a4f95a1ab367eedc7322010225
BLAKE2b-256 eb3a3ee55932ecd04f083154f42febd6fdcc75af9f9067dcc641410f1cd0daf3

See more details on using hashes here.

Provenance

File details

Details for the file k2-1.9-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: k2-1.9-cp38-cp38-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.8, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.12

File hashes

Hashes for k2-1.9-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 60e0c87bdedc5d7c9a5780be5b8c939cb2946e3717219ec942d019c810a40595
MD5 4c7d1aacc0bafe7ec626e3b4cb853b5f
BLAKE2b-256 f13c5201712d634e8d2ce2dfe001a7e27cbd4ce363d807fe9aa550b3a9bfac38

See more details on using hashes here.

Provenance

File details

Details for the file k2-1.9-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: k2-1.9-cp37-cp37m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.7m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.7.12

File hashes

Hashes for k2-1.9-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 8250248a1221db8dcea2807fff5f4d0c79c778c344472fe91cf25641f43ee838
MD5 e2e489d91b2b1e43493b9370c5575a85
BLAKE2b-256 900a101e6fb972c00bdc13926266662fe1d240d8e446d356d4b3d2e0a27008b1

See more details on using hashes here.

Provenance

File details

Details for the file k2-1.9-cp36-cp36m-macosx_10_15_x86_64.whl.

File metadata

  • Download URL: k2-1.9-cp36-cp36m-macosx_10_15_x86_64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.6m, macOS 10.15+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.15

File hashes

Hashes for k2-1.9-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ade608ebe93f9cca01dbe596a97d88e88e1770b63347b6c10e97f016d33edcfa
MD5 c85b41a1d8752008f370a898118820f0
BLAKE2b-256 ea6f66da932114b2e9e7eb426d891fc9c262614751070395d75488a7ffddd441

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page