k2py
Python bindings for k2
Features
- Efficient Streaming Decoding: OnlineDenseIntersecter for real-time forced alignment
- Memory-Bounded Streaming:
decode_and_flush()keeps memory at O(chunk) for arbitrarily long audio - Mixed Decode Modes: Combine
decode()anddecode_and_flush()for flush-every-N-chunks strategies - WebAssembly Support: Browser-side forced alignment via Emscripten (WASM32 + WASM64)
- Cross-Platform: Supports Linux, macOS, and Windows
- Python 3.10+: Compatible with modern Python versions
- Built with pybind11: Fast C++ bindings with minimal overhead
Installation
From PyPI
pip install k2py
Coexistence with k2
k2py can coexist with the k2 package. Both can be imported in any order:
import k2
import k2py # works fine
Usage
Basic Example
from k2py import OnlineDenseIntersecter, CreateFsaVecFromStr
import numpy as np
# Create FSA from string representation
fsa_str = "0 1 1 1.0\n1 2 2 1.0\n2"
result = CreateFsaVecFromStr(fsa_str, final_state=2)
# Initialize decoder
decoder = OnlineDenseIntersecter(
result["fsa"],
result["aux_labels"],
search_beam=20.0,
output_beam=8.0,
min_active_states=30,
max_active_states=10000
)
# Prepare acoustic scores (num_frames x vocab_size)
scores = np.random.randn(100, 50).astype(np.float32)
# Decode
lattice = decoder.DecodeWithArray(scores, return_lattice=True)
# Get final alignment result
alignment = decoder.Finish()
print(f"Token IDs: {alignment['token_ids']}")
print(f"Timestamps: {alignment['timestamps']}")
print(f"Durations: {alignment['durations']}")
Streaming with Memory-Bounded Flush
# For long audio: decode_and_flush() caps memory at O(chunk)
all_tokens = []
for chunk_scores in chunk_iterator(scores, chunk_size=1000):
tokens, labels = decoder.decode_and_flush(chunk_scores)
all_tokens.extend(tokens)
# Get any remaining tokens
final_tokens, final_labels = decoder.finish()
all_tokens.extend(final_tokens[0])
WebAssembly
k2py also compiles to WebAssembly for browser-side forced alignment. See wasm/README.md for build instructions, API reference, and benchmarks.
Metadata
Release files for k2py 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Reset | |||
|---|---|---|---|---|
| k2py-0.4.0-cp314-cp314-win_amd64.whl | CPython 3.14 | CPython 3.14 | Windows x86-64 | Details |
| k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl | CPython 3.14 | CPython 3.14 | Linux glibc 2.27+ x86-64, Linux glibc 2.28+ x86-64 | Details |
| k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl | CPython 3.14 | CPython 3.14 | macOS 15.0+ ARM64 | Details |
| k2py-0.4.0-cp313-cp313-win_amd64.whl | CPython 3.13 | CPython 3.13 | Windows x86-64 | Details |
| k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl | CPython 3.13 | CPython 3.13 | Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 | Details |
| k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl | CPython 3.13 | CPython 3.13 | macOS 15.0+ ARM64 | Details |
| k2py-0.4.0-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details |
| k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl | CPython 3.12 | CPython 3.12 | Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 | Details |
| k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl | CPython 3.12 | CPython 3.12 | macOS 15.0+ ARM64 | Details |
| k2py-0.4.0-cp311-cp311-win_amd64.whl | CPython 3.11 | CPython 3.11 | Windows x86-64 | Details |
| k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl | CPython 3.11 | CPython 3.11 | Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 | Details |
| k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl | CPython 3.11 | CPython 3.11 | macOS 15.0+ ARM64 | Details |
| k2py-0.4.0-cp310-cp310-win_amd64.whl | CPython 3.10 | CPython 3.10 | Windows x86-64 | Details |
| k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl | CPython 3.10 | CPython 3.10 | Linux glibc 2.28+ x86-64, Linux glibc 2.27+ x86-64 | Details |
| k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl | CPython 3.10 | CPython 3.10 | macOS 15.0+ ARM64 | Details |
Total release size: 5.6 MB
Release files / k2py-0.4.0-cp314-cp314-win_amd64.whl
| Download URL | k2py-0.4.0-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 320.4 kB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
fca13fed91b88b458bef2396f09dca161bb72489bda09c6dbb2a5f10306bc0da
|
|
BLAKE2b-256 checksum How to use checksums |
29f960c0a5597253ca6f3567ba51b7d537e11794195ebee98cf2d2a2ee63a027
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | k2py-0.4.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 484.8 kB |
| Tags | CPython 3.14 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
0bf51c68e796a14d7802136862ecf558cd4655cdcd163d8ffe8520ba7db367ad
|
|
BLAKE2b-256 checksum How to use checksums |
aec03bbae886adcfbbb041a230df062573c7db0045217f8583ed12a59fdd50b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl
| Download URL | k2py-0.4.0-cp314-cp314-macosx_15_0_arm64.whl |
|---|---|
| Size | 324.2 kB |
| Tags | CPython 3.14 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
920fb9d1673c446a7fb0dbfd04567f74ac761a1971e589c352c66bba8a61cd3c
|
|
BLAKE2b-256 checksum How to use checksums |
0878988528a0199e06f2d969ea48b1c745239722d8a90c584a1c1df5ac92a500
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp313-cp313-win_amd64.whl
| Download URL | k2py-0.4.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 311.0 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
16d62b5370525066d6b99a960b702eab26396c4751b21606846a6e6136eee3c2
|
|
BLAKE2b-256 checksum How to use checksums |
505dc7021eec2dd2262d59ac718787d765f3687646f5be11fe3ae19322324883
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | k2py-0.4.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 484.8 kB |
| Tags | CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
b039d6f4f708b05769cdfc62285db7c701e833e15825c5b1e09732545b844791
|
|
BLAKE2b-256 checksum How to use checksums |
18d7f0e52536b0296ad2b5978d681e49510ede0d1e92dbfcb1a1c9e5d0315d4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl
| Download URL | k2py-0.4.0-cp313-cp313-macosx_15_0_arm64.whl |
|---|---|
| Size | 323.8 kB |
| Tags | CPython 3.13 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
cb54cc36d81693fca9c7f51236250bc5dac590bb706ae56f871c01ad34e84ab2
|
|
BLAKE2b-256 checksum How to use checksums |
50a1434eb63356d04d4b2eb228438c350d45c37a3ee1578471ef161a1e358ba8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp312-cp312-win_amd64.whl
| Download URL | k2py-0.4.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 311.1 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
d1eb564baf7f393350520f0ea53681e408dbef5fd858222a3e9c96438c0c6a94
|
|
BLAKE2b-256 checksum How to use checksums |
08b5888b46442aa42803f9182f78dc946696012c0c641d45f9aa5cb42d68cfb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | k2py-0.4.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 484.8 kB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
8c372eeb505b98fa1be83a4ec009492d4b25fc6292179335d8b79ca784efa496
|
|
BLAKE2b-256 checksum How to use checksums |
bf7118444361bae27c00713ee8f1f77ce0749d2d28e8b17dd1868fd43e7bf163
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl
| Download URL | k2py-0.4.0-cp312-cp312-macosx_15_0_arm64.whl |
|---|---|
| Size | 323.8 kB |
| Tags | CPython 3.12 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
6b6b3bc72d189303793a61763de77d157fb29445d5ef0895cc900f3b2176dfe4
|
|
BLAKE2b-256 checksum How to use checksums |
1d460723218bfd228395d9302edb8effd161bfa907d71c0df2b84aeb45208ebd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp311-cp311-win_amd64.whl
| Download URL | k2py-0.4.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 309.4 kB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
7b162b3264a5902a35b61d7d99476cb76374109ea480dc52822058c95635ff4e
|
|
BLAKE2b-256 checksum How to use checksums |
b6d26f7d340a0641b92fad4887bb05be5718e8ea8016fba3b8de2829290a1d89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | k2py-0.4.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 481.6 kB |
| Tags | CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
470e907d26708407015bc891520795bd445f3c08e983d7a364014dddcb214de7
|
|
BLAKE2b-256 checksum How to use checksums |
899311808e3afbd5ddb80600605c3e2e52146fd10e80d2badac7d25e080aba5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl
| Download URL | k2py-0.4.0-cp311-cp311-macosx_15_0_arm64.whl |
|---|---|
| Size | 323.2 kB |
| Tags | CPython 3.11 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
1f47cebc56533d476c9d8c24897689bd33583be964b6d6826db692f088aae986
|
|
BLAKE2b-256 checksum How to use checksums |
e745a168b6b29b7122cee7d47b30b4135680119cbc0cda14a2f734da2d1f8cee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp310-cp310-win_amd64.whl
| Download URL | k2py-0.4.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 308.5 kB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5c951919060aa533199a46717085c4ada73cd74d98bc265bba5365e4e9287036
|
|
BLAKE2b-256 checksum How to use checksums |
6aa92a61af0cc1768353dc143f996f391c2d5f5b5e5d1e191a5d4b0309c2c2aa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | k2py-0.4.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 480.7 kB |
| Tags | CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
1ba7998871196efd6fdf280499116c37f73444b16c289580987d0140030bf827
|
|
BLAKE2b-256 checksum How to use checksums |
8702477b7151410cfd3a3450bf3144724ec5b890b6c0350303256dabd16b9a5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency logRelease files / k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl
| Download URL | k2py-0.4.0-cp310-cp310-macosx_15_0_arm64.whl |
|---|---|
| Size | 321.9 kB |
| Tags | CPython 3.10 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
34fdcb0389a8458465f10886719c1e3336650f927e0ae8e81a09b3ec1c81cf56
|
|
BLAKE2b-256 checksum How to use checksums |
99bbf64d96936648b998c6ee5fca68b2301e0a0a201f9976daf6f541f98cde9f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 24, 2026.
Transparency log