tqai 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. tqai-0.1.0/.github/ISSUE_TEMPLATE/bug_report.yml +55 -0
  2. tqai-0.1.0/.github/ISSUE_TEMPLATE/feature_request.yml +23 -0
  3. tqai-0.1.0/.github/homebrew/tqai.rb +33 -0
  4. tqai-0.1.0/.github/pull_request_template.md +21 -0
  5. tqai-0.1.0/.github/workflows/ci.yml +45 -0
  6. tqai-0.1.0/.github/workflows/release.yml +78 -0
  7. tqai-0.1.0/.gitignore +40 -0
  8. tqai-0.1.0/CHANGELOG.md +22 -0
  9. tqai-0.1.0/CODE_OF_CONDUCT.md +39 -0
  10. tqai-0.1.0/CONTRIBUTING.md +89 -0
  11. tqai-0.1.0/LICENSE +21 -0
  12. tqai-0.1.0/PKG-INFO +210 -0
  13. tqai-0.1.0/README.md +165 -0
  14. tqai-0.1.0/SECURITY.md +25 -0
  15. tqai-0.1.0/examples/hf_example.py +34 -0
  16. tqai-0.1.0/examples/mlx_example.py +33 -0
  17. tqai-0.1.0/pyproject.toml +78 -0
  18. tqai-0.1.0/scripts/generate_codebooks.py +42 -0
  19. tqai-0.1.0/src/tqai/__init__.py +65 -0
  20. tqai-0.1.0/src/tqai/__main__.py +3 -0
  21. tqai-0.1.0/src/tqai/_patch.py +26 -0
  22. tqai-0.1.0/src/tqai/backend/__init__.py +40 -0
  23. tqai-0.1.0/src/tqai/backend/_base.py +26 -0
  24. tqai-0.1.0/src/tqai/backend/_mlx.py +68 -0
  25. tqai-0.1.0/src/tqai/backend/_torch.py +67 -0
  26. tqai-0.1.0/src/tqai/cache/__init__.py +0 -0
  27. tqai-0.1.0/src/tqai/cache/hf.py +133 -0
  28. tqai-0.1.0/src/tqai/cache/mlx.py +164 -0
  29. tqai-0.1.0/src/tqai/cli.py +382 -0
  30. tqai-0.1.0/src/tqai/codebook/__init__.py +10 -0
  31. tqai-0.1.0/src/tqai/codebook/data/d064_b2.npz +0 -0
  32. tqai-0.1.0/src/tqai/codebook/data/d064_b3.npz +0 -0
  33. tqai-0.1.0/src/tqai/codebook/data/d064_b4.npz +0 -0
  34. tqai-0.1.0/src/tqai/codebook/data/d096_b2.npz +0 -0
  35. tqai-0.1.0/src/tqai/codebook/data/d096_b3.npz +0 -0
  36. tqai-0.1.0/src/tqai/codebook/data/d096_b4.npz +0 -0
  37. tqai-0.1.0/src/tqai/codebook/data/d128_b2.npz +0 -0
  38. tqai-0.1.0/src/tqai/codebook/data/d128_b3.npz +0 -0
  39. tqai-0.1.0/src/tqai/codebook/data/d128_b4.npz +0 -0
  40. tqai-0.1.0/src/tqai/codebook/data/d256_b2.npz +0 -0
  41. tqai-0.1.0/src/tqai/codebook/data/d256_b3.npz +0 -0
  42. tqai-0.1.0/src/tqai/codebook/data/d256_b4.npz +0 -0
  43. tqai-0.1.0/src/tqai/codebook/lloyd_max.py +91 -0
  44. tqai-0.1.0/src/tqai/codebook/registry.py +65 -0
  45. tqai-0.1.0/src/tqai/config.py +16 -0
  46. tqai-0.1.0/src/tqai/convert.py +166 -0
  47. tqai-0.1.0/src/tqai/quantizer.py +116 -0
  48. tqai-0.1.0/tests/conftest.py +42 -0
  49. tqai-0.1.0/tests/test_accuracy.py +497 -0
  50. tqai-0.1.0/tests/test_backend.py +72 -0
  51. tqai-0.1.0/tests/test_cache_hf.py +99 -0
  52. tqai-0.1.0/tests/test_cache_mlx.py +94 -0
  53. tqai-0.1.0/tests/test_codebook.py +89 -0
  54. tqai-0.1.0/tests/test_convert.py +107 -0
  55. tqai-0.1.0/tests/test_e2e_large_models.py +120 -0
  56. tqai-0.1.0/tests/test_e2e_models.py +102 -0
  57. tqai-0.1.0/tests/test_patch.py +23 -0
  58. tqai-0.1.0/tests/test_quantizer.py +128 -0
@@ -0,0 +1,55 @@
1
+ name: Bug Report
2
+ description: Report a bug in tqai
3
+ labels: [bug]
4
+ body:
5
+ - type: textarea
6
+ id: description
7
+ attributes:
8
+ label: Description
9
+ description: A clear description of the bug
10
+ validations:
11
+ required: true
12
+
13
+ - type: textarea
14
+ id: reproduce
15
+ attributes:
16
+ label: Steps to Reproduce
17
+ description: Minimal code or commands to reproduce the issue
18
+ render: python
19
+
20
+ - type: textarea
21
+ id: expected
22
+ attributes:
23
+ label: Expected Behavior
24
+ description: What you expected to happen
25
+
26
+ - type: textarea
27
+ id: actual
28
+ attributes:
29
+ label: Actual Behavior
30
+ description: What actually happened (include error messages)
31
+
32
+ - type: dropdown
33
+ id: backend
34
+ attributes:
35
+ label: Backend
36
+ options:
37
+ - PyTorch (CPU)
38
+ - PyTorch (CUDA)
39
+ - MLX (Apple Silicon)
40
+ validations:
41
+ required: true
42
+
43
+ - type: input
44
+ id: model
45
+ attributes:
46
+ label: Model
47
+ description: Model ID you were using
48
+ placeholder: e.g., mlx-community/Llama-3.1-8B-Instruct-4bit
49
+
50
+ - type: textarea
51
+ id: environment
52
+ attributes:
53
+ label: Environment
54
+ description: Paste the output of `tqai info`
55
+ render: text
@@ -0,0 +1,23 @@
1
+ name: Feature Request
2
+ description: Suggest a feature for tqai
3
+ labels: [enhancement]
4
+ body:
5
+ - type: textarea
6
+ id: description
7
+ attributes:
8
+ label: Description
9
+ description: What feature would you like to see?
10
+ validations:
11
+ required: true
12
+
13
+ - type: textarea
14
+ id: use-case
15
+ attributes:
16
+ label: Use Case
17
+ description: How would you use this feature?
18
+
19
+ - type: textarea
20
+ id: alternatives
21
+ attributes:
22
+ label: Alternatives Considered
23
+ description: Any workarounds or alternatives you've tried
@@ -0,0 +1,33 @@
1
+ class Tqai < Formula
2
+ include Language::Python::Virtualenv
3
+
4
+ desc "TurboQuant KV cache compression for local LLM inference"
5
+ homepage "https://github.com/AlphaWaveSystems/tqai"
6
+ url "https://files.pythonhosted.org/packages/source/t/tqai/tqai-0.1.0.tar.gz"
7
+ sha256 "PLACEHOLDER_SHA256"
8
+ license "MIT"
9
+
10
+ depends_on "python@3.12"
11
+
12
+ resource "numpy" do
13
+ url "https://files.pythonhosted.org/packages/source/n/numpy/numpy-2.2.5.tar.gz"
14
+ sha256 "PLACEHOLDER_SHA256"
15
+ end
16
+
17
+ def install
18
+ virtualenv_install_with_resources
19
+ end
20
+
21
+ test do
22
+ assert_match "tqai v", shell_output("#{bin}/tqai info")
23
+ end
24
+ end
25
+
26
+ # NOTE: After publishing to PyPI, update the sha256 values:
27
+ # 1. Download the sdist: pip download tqai==0.1.0 --no-deps --no-binary :all:
28
+ # 2. Get sha256: shasum -a 256 tqai-0.1.0.tar.gz
29
+ # 3. Same for numpy
30
+ #
31
+ # To install this formula into the existing tap:
32
+ # cp .github/homebrew/tqai.rb /path/to/homebrew-tap/Formula/tqai.rb
33
+ # cd /path/to/homebrew-tap && git add Formula/tqai.rb && git commit -s -m "Add tqai formula"
@@ -0,0 +1,21 @@
1
+ ## Summary
2
+
3
+ <!-- What does this PR do? -->
4
+
5
+ ## Type of Change
6
+
7
+ - [ ] Bug fix
8
+ - [ ] New feature
9
+ - [ ] Performance improvement
10
+ - [ ] Documentation
11
+ - [ ] Refactoring
12
+
13
+ ## Testing
14
+
15
+ - [ ] `pytest tests/` passes
16
+ - [ ] `ruff check .` passes
17
+ - [ ] Added tests for new functionality (if applicable)
18
+
19
+ ## DCO
20
+
21
+ - [ ] All commits are signed off (`git commit -s`)
@@ -0,0 +1,45 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ lint:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: actions/setup-python@v5
15
+ with:
16
+ python-version: "3.12"
17
+ - run: pip install ruff
18
+ - run: ruff check .
19
+
20
+ test:
21
+ runs-on: ${{ matrix.os }}
22
+ strategy:
23
+ fail-fast: false
24
+ matrix:
25
+ os: [ubuntu-latest, macos-latest]
26
+ python-version: ["3.10", "3.11", "3.12"]
27
+ steps:
28
+ - uses: actions/checkout@v4
29
+ - uses: actions/setup-python@v5
30
+ with:
31
+ python-version: ${{ matrix.python-version }}
32
+
33
+ - name: Install dependencies
34
+ run: |
35
+ pip install -e ".[dev]"
36
+ if [[ "$RUNNER_OS" == "macOS" ]]; then
37
+ pip install mlx mlx-lm || true
38
+ fi
39
+
40
+ - name: Run tests
41
+ run: |
42
+ pytest tests/ \
43
+ --ignore=tests/test_e2e_models.py \
44
+ --ignore=tests/test_e2e_large_models.py \
45
+ -v --tb=short
@@ -0,0 +1,78 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags: ["v*"]
6
+
7
+ permissions:
8
+ contents: write
9
+ id-token: write
10
+
11
+ jobs:
12
+ build:
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-python@v5
17
+ with:
18
+ python-version: "3.12"
19
+
20
+ - name: Build package
21
+ run: |
22
+ pip install hatchling build
23
+ python -m build
24
+
25
+ - uses: actions/upload-artifact@v4
26
+ with:
27
+ name: dist
28
+ path: dist/
29
+
30
+ publish-pypi:
31
+ needs: build
32
+ runs-on: ubuntu-latest
33
+ environment: pypi
34
+ permissions:
35
+ id-token: write
36
+ steps:
37
+ - uses: actions/download-artifact@v4
38
+ with:
39
+ name: dist
40
+ path: dist/
41
+
42
+ - name: Publish to PyPI
43
+ uses: pypa/gh-action-pypi-publish@release/v1
44
+
45
+ github-release:
46
+ needs: build
47
+ runs-on: ubuntu-latest
48
+ permissions:
49
+ contents: write
50
+ steps:
51
+ - uses: actions/checkout@v4
52
+
53
+ - uses: actions/download-artifact@v4
54
+ with:
55
+ name: dist
56
+ path: dist/
57
+
58
+ - name: Create GitHub Release
59
+ env:
60
+ GH_TOKEN: ${{ github.token }}
61
+ TAG_NAME: ${{ github.ref_name }}
62
+ run: |
63
+ gh release create "$TAG_NAME" dist/* \
64
+ --title "$TAG_NAME" \
65
+ --notes-file CHANGELOG.md
66
+
67
+ notify-homebrew:
68
+ needs: publish-pypi
69
+ runs-on: ubuntu-latest
70
+ steps:
71
+ - name: Trigger Homebrew formula update
72
+ env:
73
+ GH_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
74
+ VERSION: ${{ github.ref_name }}
75
+ run: |
76
+ gh api repos/AlphaWaveSystems/homebrew-tap/dispatches \
77
+ -f event_type=tqai-release \
78
+ -f "client_payload[version]=$VERSION" || true
tqai-0.1.0/.gitignore ADDED
@@ -0,0 +1,40 @@
1
+ # Python
2
+ __pycache__/
3
+ *.pyc
4
+ *.pyo
5
+ *.egg-info/
6
+ dist/
7
+ build/
8
+ *.egg
9
+
10
+ # Testing
11
+ .pytest_cache/
12
+ htmlcov/
13
+ .coverage
14
+ coverage.xml
15
+
16
+ # Tools
17
+ .ruff_cache/
18
+ .mypy_cache/
19
+
20
+ # IDE
21
+ .vscode/
22
+ .idea/
23
+ *.swp
24
+ *.swo
25
+ *~
26
+
27
+ # OS
28
+ .DS_Store
29
+ Thumbs.db
30
+
31
+ # Virtual environments
32
+ .venv/
33
+ venv/
34
+ env/
35
+
36
+ # Converted model outputs (user-generated)
37
+ *-tqai/
38
+
39
+ # Keep shipped codebook data
40
+ !src/tqai/codebook/data/*.npz
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.0] - 2026-04-04
9
+
10
+ ### Added
11
+
12
+ - Core PolarQuantizer implementing TurboQuant Stage 1 (random rotation + Lloyd-Max scalar quantization)
13
+ - Multi-backend support: PyTorch (CPU/CUDA) and MLX (Apple Silicon)
14
+ - HuggingFace Transformers integration via `DynamicCache` subclass
15
+ - mlx-lm integration via `KVCache` replacement
16
+ - Precomputed Lloyd-Max codebooks for head dimensions 64, 96, 128, 256 at 2/3/4 bits
17
+ - Asymmetric K/V bit allocation (e.g., K4/V2 for 80% memory savings)
18
+ - Sink token support (keep first N tokens uncompressed)
19
+ - CLI tool with commands: `info`, `benchmark`, `run`, `compare`, `convert`
20
+ - Offline model conversion (`tqai convert`) for faster startup
21
+ - Comprehensive test suite (179 tests) covering accuracy, edge cases, and cross-backend consistency
22
+ - Verified quality-neutral on 8B+ parameter models (Llama 3.1 8B, Qwen 14B)
@@ -0,0 +1,39 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to a positive environment:
15
+
16
+ * Using welcoming and inclusive language
17
+ * Being respectful of differing viewpoints and experiences
18
+ * Gracefully accepting constructive criticism
19
+ * Focusing on what is best for the community
20
+ * Showing empathy towards other community members
21
+
22
+ Examples of unacceptable behavior:
23
+
24
+ * The use of sexualized language or imagery, and sexual attention or advances of any kind
25
+ * Trolling, insulting or derogatory comments, and personal or political attacks
26
+ * Public or private harassment
27
+ * Publishing others' private information without explicit permission
28
+ * Other conduct which could reasonably be considered inappropriate in a professional setting
29
+
30
+ ## Enforcement
31
+
32
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
33
+ reported to the project maintainers at **github@alphawavesystems.com**.
34
+
35
+ All complaints will be reviewed and investigated promptly and fairly.
36
+
37
+ ## Attribution
38
+
39
+ This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/), version 2.1.
@@ -0,0 +1,89 @@
1
+ # Contributing to tqai
2
+
3
+ Thank you for your interest in contributing to tqai! This guide explains how to get involved.
4
+
5
+ ## Reporting Bugs
6
+
7
+ Open an issue on [GitHub Issues](https://github.com/AlphaWaveSystems/tqai/issues) with:
8
+
9
+ - A clear, descriptive title
10
+ - Steps to reproduce the problem
11
+ - Expected vs actual behavior
12
+ - tqai version (`tqai info`), OS, Python version
13
+ - Model ID and backend (torch/mlx) you were using
14
+
15
+ ## Pull Request Flow
16
+
17
+ 1. **Fork** the repository on GitHub
18
+ 2. **Branch** from `main` — use a descriptive name (e.g., `fix/dtype-mismatch`, `feat/bit-packing`)
19
+ 3. **Develop** your changes following the code style guidelines below
20
+ 4. **Test** your changes (see Testing section)
21
+ 5. **Submit a PR** against `main` with a clear description of what and why
22
+
23
+ Keep PRs focused on a single concern. If you have multiple unrelated changes, submit separate PRs.
24
+
25
+ ## Code Style
26
+
27
+ - **Linting**: Run `ruff check .` before committing
28
+ - **Formatting**: Follow PEP 8, 100-char line length
29
+ - **Imports**: Sorted by ruff (isort compatible)
30
+ - **Types**: Use type hints for public API functions
31
+
32
+ ## DCO Sign-Off
33
+
34
+ All commits must include a Developer Certificate of Origin sign-off line:
35
+
36
+ ```
37
+ Signed-off-by: Your Name <your@email.com>
38
+ ```
39
+
40
+ Add this automatically with:
41
+
42
+ ```bash
43
+ git commit -s -m "Your commit message"
44
+ ```
45
+
46
+ This certifies that you wrote or have the right to submit the code under the project's license. See [developercertificate.org](https://developercertificate.org/) for the full text.
47
+
48
+ ## Testing
49
+
50
+ Before submitting a PR, run the test suite:
51
+
52
+ ```bash
53
+ # Install dev dependencies
54
+ pip install -e ".[dev]"
55
+
56
+ # Run unit + accuracy tests
57
+ pytest tests/ --ignore=tests/test_e2e_models.py --ignore=tests/test_e2e_large_models.py
58
+
59
+ # Run linter
60
+ ruff check .
61
+ ```
62
+
63
+ For changes to the quantizer or cache, also run the accuracy tests:
64
+
65
+ ```bash
66
+ pytest tests/test_accuracy.py -v
67
+ ```
68
+
69
+ ## Adding Support for New Head Dimensions
70
+
71
+ If you need codebooks for a head dimension not in `{64, 96, 128, 256}`:
72
+
73
+ ```bash
74
+ pip install tqai[codegen]
75
+ python scripts/generate_codebooks.py
76
+ ```
77
+
78
+ ## Architecture Overview
79
+
80
+ - `src/tqai/quantizer.py` — Core PolarQuantizer algorithm
81
+ - `src/tqai/backend/` — PyTorch + MLX abstraction (Protocol-based)
82
+ - `src/tqai/codebook/` — Lloyd-Max codebook generation and loading
83
+ - `src/tqai/cache/` — HuggingFace and mlx-lm cache wrappers
84
+ - `src/tqai/cli.py` — CLI tool
85
+ - `src/tqai/convert.py` — Offline model conversion
86
+
87
+ ## License
88
+
89
+ By contributing to tqai, you agree that your contributions will be licensed under the MIT License.
tqai-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AlphaWaveSystems
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
tqai-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,210 @@
1
+ Metadata-Version: 2.4
2
+ Name: tqai
3
+ Version: 0.1.0
4
+ Summary: TurboQuant KV cache compression for local LLM inference
5
+ Project-URL: Homepage, https://github.com/AlphaWaveSystems/tqai
6
+ Project-URL: Repository, https://github.com/AlphaWaveSystems/tqai
7
+ Project-URL: Issues, https://github.com/AlphaWaveSystems/tqai/issues
8
+ Project-URL: Changelog, https://github.com/AlphaWaveSystems/tqai/blob/main/CHANGELOG.md
9
+ Author: pbertsch
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: apple-silicon,compression,kv-cache,llm,mlx,quantization,turboquant
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Programming Language :: Python :: 3.14
23
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.10
26
+ Requires-Dist: numpy>=1.24
27
+ Provides-Extra: codegen
28
+ Requires-Dist: scipy>=1.10; extra == 'codegen'
29
+ Provides-Extra: cuda
30
+ Requires-Dist: torch>=2.1; extra == 'cuda'
31
+ Requires-Dist: triton>=2.0; extra == 'cuda'
32
+ Provides-Extra: dev
33
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
34
+ Requires-Dist: pytest>=7.0; extra == 'dev'
35
+ Requires-Dist: ruff>=0.4; extra == 'dev'
36
+ Requires-Dist: scipy>=1.10; extra == 'dev'
37
+ Requires-Dist: torch>=2.1; extra == 'dev'
38
+ Requires-Dist: transformers>=4.38; extra == 'dev'
39
+ Provides-Extra: mlx
40
+ Requires-Dist: mlx-lm>=0.12; extra == 'mlx'
41
+ Requires-Dist: mlx>=0.14; extra == 'mlx'
42
+ Provides-Extra: torch
43
+ Requires-Dist: torch>=2.1; extra == 'torch'
44
+ Description-Content-Type: text/markdown
45
+
46
+ # tqai
47
+
48
+ [![PyPI version](https://img.shields.io/pypi/v/tqai)](https://pypi.org/project/tqai/)
49
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
50
+ [![CI](https://github.com/AlphaWaveSystems/tqai/actions/workflows/ci.yml/badge.svg)](https://github.com/AlphaWaveSystems/tqai/actions/workflows/ci.yml)
51
+ [![Python 3.10+](https://img.shields.io/pypi/pyversions/tqai)](https://pypi.org/project/tqai/)
52
+
53
+ TurboQuant KV cache compression for local LLM inference.
54
+
55
+ Compresses the KV cache to ~3 bits per channel with **80%+ memory savings** and near-zero quality loss on 8B+ models. Supports both PyTorch (CPU/CUDA) and MLX (Apple Silicon).
56
+
57
+ Based on [TurboQuant](https://arxiv.org/abs/2504.19874) (Google Research, ICLR 2026).
58
+
59
+ ## Installation
60
+
61
+ ```bash
62
+ # Homebrew (macOS)
63
+ brew install alphawavesystems/tap/tqai
64
+
65
+ # PyPI
66
+ pip install tqai
67
+
68
+ # With PyTorch backend
69
+ pip install tqai[torch]
70
+
71
+ # With MLX backend (Apple Silicon)
72
+ pip install tqai[mlx]
73
+ ```
74
+
75
+ ## Quick Start
76
+
77
+ ### HuggingFace Transformers (PyTorch)
78
+
79
+ ```python
80
+ import tqai
81
+ from transformers import AutoModelForCausalLM, AutoTokenizer
82
+
83
+ model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-3B-Instruct", dtype="auto")
84
+ tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-3B-Instruct")
85
+
86
+ # One line to enable KV cache compression
87
+ cache = tqai.patch(model, bits_k=4, bits_v=2)
88
+
89
+ inputs = tokenizer("Explain quantum entanglement:", return_tensors="pt")
90
+ output = model.generate(**inputs, past_key_values=cache, max_new_tokens=200)
91
+ print(tokenizer.decode(output[0], skip_special_tokens=True))
92
+ ```
93
+
94
+ ### MLX (Apple Silicon)
95
+
96
+ ```python
97
+ import tqai
98
+ import mlx_lm
99
+
100
+ model, tokenizer = mlx_lm.load("mlx-community/Llama-3.1-8B-Instruct-4bit")
101
+
102
+ # One line to enable KV cache compression
103
+ tqai.patch(model, bits_k=4, bits_v=2, backend="mlx")
104
+
105
+ response = mlx_lm.generate(model, tokenizer, prompt="Explain quantum entanglement:", max_tokens=200)
106
+ print(response)
107
+
108
+ # Restore original behaviour when done
109
+ tqai.unpatch(model)
110
+ ```
111
+
112
+ ## Compression Configs
113
+
114
+ | Config | Avg Bits | Memory Saved | Recommended For |
115
+ |--------|----------|--------------|-----------------|
116
+ | `bits_k=4, bits_v=2` | 3.0 | **80%** | Production (best quality/compression balance) |
117
+ | `bits_k=3, bits_v=2` | 2.5 | **84%** | Extended context windows |
118
+ | `bits_k=4, bits_v=3` | 3.5 | **78%** | Quality-sensitive applications |
119
+
120
+ ## How It Works
121
+
122
+ tqai implements Stage 1 of TurboQuant (PolarQuant):
123
+
124
+ 1. **Random orthogonal rotation** — Rotates KV vectors by a fixed Haar-distributed matrix to spread information across all coordinates
125
+ 2. **Lloyd-Max scalar quantization** — Quantizes each coordinate independently using precomputed optimal codebooks
126
+ 3. **Norm preservation** — Stores vector norms separately in FP16
127
+
128
+ No training, calibration, or model-specific tuning required. The same codebooks work for any model.
129
+
130
+ ## Quality Results
131
+
132
+ Tested on Apple Silicon with various model sizes:
133
+
134
+ | Model | Baseline | + tqai K4/V2 | + tqai K3/V2 |
135
+ |-------|----------|--------------|--------------|
136
+ | Qwen 0.5B | Good | Degraded | Poor |
137
+ | Qwen 3B bf16 | Excellent | Good | Degraded |
138
+ | Llama 8B Q4 | Excellent | **Excellent** | **Excellent** |
139
+ | Qwen 14B Q4 | Excellent | **Excellent** | **Excellent** |
140
+
141
+ Quality is near-identical to baseline on 8B+ parameter models.
142
+
143
+ ## CLI
144
+
145
+ tqai includes a command-line tool for quick testing without writing code:
146
+
147
+ ```bash
148
+ # Show environment and library info
149
+ tqai info
150
+
151
+ # Run quantization accuracy benchmark
152
+ tqai benchmark
153
+ tqai benchmark --bits-k 3 --bits-v 2 --head-dim 128
154
+
155
+ # Generate text with TurboQuant compression
156
+ tqai run "Explain gravity" --model mlx-community/Llama-3.1-8B-Instruct-4bit
157
+ tqai run "Explain gravity" --model Qwen/Qwen2.5-3B-Instruct --backend torch
158
+
159
+ # Compare baseline vs compressed output side by side
160
+ tqai compare "Explain gravity" --model mlx-community/Llama-3.1-8B-Instruct-4bit
161
+
162
+ # Pre-convert a model for faster startup
163
+ tqai convert --model mlx-community/Llama-3.1-8B-Instruct-4bit --output ./llama-tqai/
164
+ tqai run "Explain gravity" --model mlx-community/Llama-3.1-8B-Instruct-4bit --tqai-config ./llama-tqai/
165
+
166
+ # Run without compression (baseline)
167
+ tqai run "Explain gravity" --model mlx-community/Llama-3.1-8B-Instruct-4bit --no-tqai
168
+ ```
169
+
170
+ ## Advanced Options
171
+
172
+ ```python
173
+ cache = tqai.patch(
174
+ model,
175
+ bits_k=4, # Bits per key coordinate (2, 3, or 4)
176
+ bits_v=2, # Bits per value coordinate (2, 3, or 4)
177
+ sink_tokens=4, # Keep first N tokens uncompressed (attention sinks)
178
+ backend="torch", # Force backend: "torch" or "mlx"
179
+ device="cuda", # PyTorch device (ignored for MLX)
180
+ )
181
+ ```
182
+
183
+ ## Running Tests
184
+
185
+ ```bash
186
+ # Install dev dependencies
187
+ pip install tqai[dev]
188
+
189
+ # Unit + accuracy tests (175 tests, <1s)
190
+ pytest tests/ --ignore=tests/test_e2e_models.py --ignore=tests/test_e2e_large_models.py
191
+
192
+ # End-to-end with real models (requires model downloads)
193
+ pytest tests/test_e2e_models.py -v -s
194
+ ```
195
+
196
+ ## Project Structure
197
+
198
+ ```
199
+ src/tqai/
200
+ ├── __init__.py # patch(), unpatch(), TurboQuantConfig
201
+ ├── config.py # Configuration dataclass
202
+ ├── quantizer.py # PolarQuantizer (core algorithm)
203
+ ├── backend/ # PyTorch + MLX abstraction
204
+ ├── codebook/ # Lloyd-Max codebooks (precomputed)
205
+ └── cache/ # HuggingFace + mlx-lm integrations
206
+ ```
207
+
208
+ ## License
209
+
210
+ MIT