pyturboquant-cpu 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 (26) hide show
  1. pyturboquant_cpu-0.1.0/.github/DISCUSSION_TEMPLATE/announcements.yml +10 -0
  2. pyturboquant_cpu-0.1.0/.github/FUNDING.yml +1 -0
  3. pyturboquant_cpu-0.1.0/.github/workflows/publish.yml +38 -0
  4. pyturboquant_cpu-0.1.0/.gitignore +54 -0
  5. pyturboquant_cpu-0.1.0/LICENSE +176 -0
  6. pyturboquant_cpu-0.1.0/PKG-INFO +170 -0
  7. pyturboquant_cpu-0.1.0/README.md +139 -0
  8. pyturboquant_cpu-0.1.0/pyproject.toml +57 -0
  9. pyturboquant_cpu-0.1.0/setup.cfg +4 -0
  10. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu/__init__.py +36 -0
  11. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu/codebook.py +182 -0
  12. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu/packing.py +258 -0
  13. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu/qjl.py +86 -0
  14. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu/quantizer.py +415 -0
  15. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu/rotation.py +84 -0
  16. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu.egg-info/PKG-INFO +170 -0
  17. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu.egg-info/SOURCES.txt +24 -0
  18. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu.egg-info/dependency_links.txt +1 -0
  19. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu.egg-info/requires.txt +6 -0
  20. pyturboquant_cpu-0.1.0/src/pyturboquant_cpu.egg-info/top_level.txt +1 -0
  21. pyturboquant_cpu-0.1.0/tests/test_codebook.py +101 -0
  22. pyturboquant_cpu-0.1.0/tests/test_packing.py +73 -0
  23. pyturboquant_cpu-0.1.0/tests/test_qjl.py +92 -0
  24. pyturboquant_cpu-0.1.0/tests/test_quantizer.py +170 -0
  25. pyturboquant_cpu-0.1.0/tests/test_rotation.py +75 -0
  26. pyturboquant_cpu-0.1.0/tests/test_roundtrip.py +109 -0
@@ -0,0 +1,10 @@
1
+ title: Announcements
2
+ body:
3
+ - type: textarea
4
+ id: welcome_text
5
+ attributes:
6
+ label: Welcome Text
7
+ description: Welcome text for this announcement.
8
+ placeholder: Write something here...
9
+ validations:
10
+ required: true
@@ -0,0 +1 @@
1
+ github: [ishandutta2007]
@@ -0,0 +1,38 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - 'v*'
7
+ workflow_dispatch:
8
+
9
+ jobs:
10
+ build-and-publish:
11
+ name: Build and publish Python distribution to PyPI
12
+ runs-on: ubuntu-latest
13
+ environment:
14
+ name: pypi
15
+ url: https://pypi.org/p/pyturboquant-cpu
16
+ permissions:
17
+ id-token: write # IMPORTANT: mandatory for trusted publishing
18
+ contents: read
19
+
20
+ steps:
21
+ - name: Checkout repository
22
+ uses: actions/checkout@v4
23
+
24
+ - name: Set up Python
25
+ uses: actions/setup-python@v5
26
+ with:
27
+ python-version: "3.x"
28
+
29
+ - name: Install build dependencies
30
+ run: python -m pip install --upgrade pip build
31
+
32
+ - name: Build a binary wheel and a source tarball
33
+ run: python -m build
34
+
35
+ - name: Publish package distribution to PyPI
36
+ uses: pypa/gh-action-pypi-publish@release/v1
37
+ with:
38
+ skip-existing: true
@@ -0,0 +1,54 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ *.egg-info/
24
+ .installed.cfg
25
+ *.egg
26
+
27
+ # Virtual environments
28
+ .env
29
+ .venv
30
+ env/
31
+ venv/
32
+ ENV/
33
+
34
+ # IDE
35
+ .vscode/
36
+ .idea/
37
+ *.swp
38
+ *.swo
39
+
40
+ # Testing
41
+ .pytest_cache/
42
+ .coverage
43
+ htmlcov/
44
+ .tox/
45
+ .nox/
46
+
47
+ # mypy
48
+ .mypy_cache/
49
+
50
+ # Misc
51
+ *.bk
52
+ *.bak
53
+ .DS_Store
54
+ Thumbs.db
@@ -0,0 +1,176 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work.
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by the Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding any notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyturboquant-cpu
3
+ Version: 0.1.0
4
+ Summary: CPU implementation of TurboQuant data-oblivious vector quantization (arXiv:2504.19874)
5
+ Author: pyturboquant contributors
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/pyturboquant/pyturboquant-cpu
8
+ Project-URL: Documentation, https://github.com/pyturboquant/pyturboquant-cpu#readme
9
+ Project-URL: Repository, https://github.com/pyturboquant/pyturboquant-cpu
10
+ Project-URL: Issues, https://github.com/pyturboquant/pyturboquant-cpu/issues
11
+ Keywords: vector-quantization,kv-cache,llm,compression,turboquant,quantization
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Intended Audience :: Science/Research
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: numpy>=1.24
26
+ Requires-Dist: scipy>=1.10
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest>=7.0; extra == "dev"
29
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
30
+ Dynamic: license-file
31
+
32
+ # pyturboquant-cpu
33
+
34
+ CPU implementation of **TurboQuant**, a data-oblivious vector quantization algorithm for compressing high-dimensional vectors with near-optimal distortion.
35
+
36
+ Based on the paper: [TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate](https://arxiv.org/abs/2504.19874) (Zandieh et al., ICLR 2026).
37
+
38
+ ## Installation
39
+
40
+ ```bash
41
+ pip install pyturboquant-cpu
42
+ ```
43
+
44
+ For development:
45
+
46
+ ```bash
47
+ git clone https://github.com/pyturboquant/pyturboquant-cpu.git
48
+ cd pyturboquant-cpu
49
+ pip install -e ".[dev]"
50
+ ```
51
+
52
+ ## Quick Start
53
+
54
+ ### MSE-Optimal Quantization
55
+
56
+ Minimizes mean-squared reconstruction error:
57
+
58
+ ```python
59
+ import numpy as np
60
+ from pyturboquant_cpu import quantize_mse, dequantize_mse
61
+
62
+ # Random vectors (e.g., KV cache embeddings)
63
+ vectors = np.random.randn(100, 128) # 100 vectors of dimension 128
64
+
65
+ # Quantize at 3 bits per coordinate
66
+ quantized = quantize_mse(vectors, bits=3, seed=42)
67
+
68
+ # Reconstruct
69
+ reconstructed = dequantize_mse(quantized)
70
+
71
+ # Check reconstruction quality
72
+ mse = np.mean(np.sum((vectors - reconstructed) ** 2, axis=1))
73
+ print(f"MSE: {mse:.4f}")
74
+ ```
75
+
76
+ ### Inner-Product-Optimal Quantization
77
+
78
+ Provides **unbiased** inner product estimates — essential for attention mechanisms and nearest-neighbor search:
79
+
80
+ ```python
81
+ from pyturboquant_cpu import quantize_prod, dequantize_prod
82
+
83
+ # Quantize at 4 bits total (3 bits MSE + 1 bit QJL correction)
84
+ quantized = quantize_prod(vectors, bits=4, seed=42)
85
+ reconstructed = dequantize_prod(quantized)
86
+
87
+ # Inner products are unbiased: E[⟨y, x̃⟩] = ⟨y, x⟩
88
+ query = np.random.randn(128)
89
+ true_ip = vectors @ query
90
+ approx_ip = reconstructed @ query
91
+ print(f"Mean IP error: {np.mean(np.abs(true_ip - approx_ip)):.4f}")
92
+ ```
93
+
94
+ ## How It Works
95
+
96
+ TurboQuant is a **data-oblivious** algorithm — it requires no training data or calibration:
97
+
98
+ 1. **Random Rotation**: Input vectors are multiplied by a random orthogonal matrix, transforming coordinates to follow a known Beta distribution
99
+ 2. **Lloyd-Max Scalar Quantization**: Each coordinate is independently quantized using a precomputed optimal codebook for the Beta distribution
100
+ 3. **QJL Residual Correction** (Prod mode only): A 1-bit Quantized Johnson-Lindenstrauss sketch of the residual removes inner-product bias
101
+
102
+ ### Theoretical Distortion Bounds
103
+
104
+ For unit vectors on S^{d-1}:
105
+
106
+ | Bits | MSE Distortion | Inner Product Distortion |
107
+ |------|---------------|-------------------------|
108
+ | 1 | ≈ 0.36 | ≈ 1.57/d |
109
+ | 2 | ≈ 0.117 | ≈ 0.56/d |
110
+ | 3 | ≈ 0.03 | ≈ 0.18/d |
111
+ | 4 | ≈ 0.009 | ≈ 0.047/d |
112
+
113
+ These are within a factor of ~2.7× of the information-theoretic lower bound.
114
+
115
+ ## API Reference
116
+
117
+ ### `quantize_mse(vectors, bits, dim=None, seed=None)`
118
+
119
+ Quantize vectors using TurboQuant_MSE (MSE-optimal).
120
+
121
+ - **vectors**: array of shape `(..., d)` — input vectors
122
+ - **bits**: int in `[1, 8]` — bits per coordinate
123
+ - **seed**: int or None — random seed for reproducibility
124
+ - **Returns**: `QuantizedMSE` dataclass
125
+
126
+ ### `dequantize_mse(quantized)`
127
+
128
+ Reconstruct vectors from MSE quantization result.
129
+
130
+ - **quantized**: `QuantizedMSE` — output of `quantize_mse`
131
+ - **Returns**: ndarray with same shape as original input
132
+
133
+ ### `quantize_prod(vectors, bits, dim=None, seed=None)`
134
+
135
+ Quantize vectors using TurboQuant_Prod (unbiased inner products).
136
+
137
+ - **vectors**: array of shape `(..., d)` — input vectors
138
+ - **bits**: int in `[2, 8]` — total bits per coordinate
139
+ - **seed**: int or None — random seed
140
+ - **Returns**: `QuantizedProd` dataclass
141
+
142
+ ### `dequantize_prod(quantized)`
143
+
144
+ Reconstruct vectors from Prod quantization result.
145
+
146
+ - **quantized**: `QuantizedProd` — output of `quantize_prod`
147
+ - **Returns**: ndarray with same shape as original input
148
+
149
+ ## GPU Version
150
+
151
+ For GPU-accelerated quantization using PyTorch, see the companion package:
152
+
153
+ ```bash
154
+ pip install pyturboquant-gpu
155
+ ```
156
+
157
+ ## Citation
158
+
159
+ ```bibtex
160
+ @article{zandieh2025turboquant,
161
+ title={TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate},
162
+ author={Zandieh, Amir and Daliri, Majid and Hadian, Majid and Mirrokni, Vahab},
163
+ journal={arXiv preprint arXiv:2504.19874},
164
+ year={2025}
165
+ }
166
+ ```
167
+
168
+ ## License
169
+
170
+ Apache 2.0
@@ -0,0 +1,139 @@
1
+ # pyturboquant-cpu
2
+
3
+ CPU implementation of **TurboQuant**, a data-oblivious vector quantization algorithm for compressing high-dimensional vectors with near-optimal distortion.
4
+
5
+ Based on the paper: [TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate](https://arxiv.org/abs/2504.19874) (Zandieh et al., ICLR 2026).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ pip install pyturboquant-cpu
11
+ ```
12
+
13
+ For development:
14
+
15
+ ```bash
16
+ git clone https://github.com/pyturboquant/pyturboquant-cpu.git
17
+ cd pyturboquant-cpu
18
+ pip install -e ".[dev]"
19
+ ```
20
+
21
+ ## Quick Start
22
+
23
+ ### MSE-Optimal Quantization
24
+
25
+ Minimizes mean-squared reconstruction error:
26
+
27
+ ```python
28
+ import numpy as np
29
+ from pyturboquant_cpu import quantize_mse, dequantize_mse
30
+
31
+ # Random vectors (e.g., KV cache embeddings)
32
+ vectors = np.random.randn(100, 128) # 100 vectors of dimension 128
33
+
34
+ # Quantize at 3 bits per coordinate
35
+ quantized = quantize_mse(vectors, bits=3, seed=42)
36
+
37
+ # Reconstruct
38
+ reconstructed = dequantize_mse(quantized)
39
+
40
+ # Check reconstruction quality
41
+ mse = np.mean(np.sum((vectors - reconstructed) ** 2, axis=1))
42
+ print(f"MSE: {mse:.4f}")
43
+ ```
44
+
45
+ ### Inner-Product-Optimal Quantization
46
+
47
+ Provides **unbiased** inner product estimates — essential for attention mechanisms and nearest-neighbor search:
48
+
49
+ ```python
50
+ from pyturboquant_cpu import quantize_prod, dequantize_prod
51
+
52
+ # Quantize at 4 bits total (3 bits MSE + 1 bit QJL correction)
53
+ quantized = quantize_prod(vectors, bits=4, seed=42)
54
+ reconstructed = dequantize_prod(quantized)
55
+
56
+ # Inner products are unbiased: E[⟨y, x̃⟩] = ⟨y, x⟩
57
+ query = np.random.randn(128)
58
+ true_ip = vectors @ query
59
+ approx_ip = reconstructed @ query
60
+ print(f"Mean IP error: {np.mean(np.abs(true_ip - approx_ip)):.4f}")
61
+ ```
62
+
63
+ ## How It Works
64
+
65
+ TurboQuant is a **data-oblivious** algorithm — it requires no training data or calibration:
66
+
67
+ 1. **Random Rotation**: Input vectors are multiplied by a random orthogonal matrix, transforming coordinates to follow a known Beta distribution
68
+ 2. **Lloyd-Max Scalar Quantization**: Each coordinate is independently quantized using a precomputed optimal codebook for the Beta distribution
69
+ 3. **QJL Residual Correction** (Prod mode only): A 1-bit Quantized Johnson-Lindenstrauss sketch of the residual removes inner-product bias
70
+
71
+ ### Theoretical Distortion Bounds
72
+
73
+ For unit vectors on S^{d-1}:
74
+
75
+ | Bits | MSE Distortion | Inner Product Distortion |
76
+ |------|---------------|-------------------------|
77
+ | 1 | ≈ 0.36 | ≈ 1.57/d |
78
+ | 2 | ≈ 0.117 | ≈ 0.56/d |
79
+ | 3 | ≈ 0.03 | ≈ 0.18/d |
80
+ | 4 | ≈ 0.009 | ≈ 0.047/d |
81
+
82
+ These are within a factor of ~2.7× of the information-theoretic lower bound.
83
+
84
+ ## API Reference
85
+
86
+ ### `quantize_mse(vectors, bits, dim=None, seed=None)`
87
+
88
+ Quantize vectors using TurboQuant_MSE (MSE-optimal).
89
+
90
+ - **vectors**: array of shape `(..., d)` — input vectors
91
+ - **bits**: int in `[1, 8]` — bits per coordinate
92
+ - **seed**: int or None — random seed for reproducibility
93
+ - **Returns**: `QuantizedMSE` dataclass
94
+
95
+ ### `dequantize_mse(quantized)`
96
+
97
+ Reconstruct vectors from MSE quantization result.
98
+
99
+ - **quantized**: `QuantizedMSE` — output of `quantize_mse`
100
+ - **Returns**: ndarray with same shape as original input
101
+
102
+ ### `quantize_prod(vectors, bits, dim=None, seed=None)`
103
+
104
+ Quantize vectors using TurboQuant_Prod (unbiased inner products).
105
+
106
+ - **vectors**: array of shape `(..., d)` — input vectors
107
+ - **bits**: int in `[2, 8]` — total bits per coordinate
108
+ - **seed**: int or None — random seed
109
+ - **Returns**: `QuantizedProd` dataclass
110
+
111
+ ### `dequantize_prod(quantized)`
112
+
113
+ Reconstruct vectors from Prod quantization result.
114
+
115
+ - **quantized**: `QuantizedProd` — output of `quantize_prod`
116
+ - **Returns**: ndarray with same shape as original input
117
+
118
+ ## GPU Version
119
+
120
+ For GPU-accelerated quantization using PyTorch, see the companion package:
121
+
122
+ ```bash
123
+ pip install pyturboquant-gpu
124
+ ```
125
+
126
+ ## Citation
127
+
128
+ ```bibtex
129
+ @article{zandieh2025turboquant,
130
+ title={TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate},
131
+ author={Zandieh, Amir and Daliri, Majid and Hadian, Majid and Mirrokni, Vahab},
132
+ journal={arXiv preprint arXiv:2504.19874},
133
+ year={2025}
134
+ }
135
+ ```
136
+
137
+ ## License
138
+
139
+ Apache 2.0
@@ -0,0 +1,57 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68.0", "setuptools-scm>=8.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "pyturboquant-cpu"
7
+ version = "0.1.0"
8
+ description = "CPU implementation of TurboQuant data-oblivious vector quantization (arXiv:2504.19874)"
9
+ readme = "README.md"
10
+ license = "Apache-2.0"
11
+ requires-python = ">=3.9"
12
+ authors = [
13
+ {name = "pyturboquant contributors"},
14
+ ]
15
+ keywords = [
16
+ "vector-quantization",
17
+ "kv-cache",
18
+ "llm",
19
+ "compression",
20
+ "turboquant",
21
+ "quantization",
22
+ ]
23
+ classifiers = [
24
+ "Development Status :: 3 - Alpha",
25
+ "Intended Audience :: Developers",
26
+ "Intended Audience :: Science/Research",
27
+ "Programming Language :: Python :: 3",
28
+ "Programming Language :: Python :: 3.9",
29
+ "Programming Language :: Python :: 3.10",
30
+ "Programming Language :: Python :: 3.11",
31
+ "Programming Language :: Python :: 3.12",
32
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
33
+ "Topic :: Scientific/Engineering :: Mathematics",
34
+ ]
35
+ dependencies = [
36
+ "numpy>=1.24",
37
+ "scipy>=1.10",
38
+ ]
39
+
40
+ [project.optional-dependencies]
41
+ dev = [
42
+ "pytest>=7.0",
43
+ "pytest-cov>=4.0",
44
+ ]
45
+
46
+ [project.urls]
47
+ Homepage = "https://github.com/pyturboquant/pyturboquant-cpu"
48
+ Documentation = "https://github.com/pyturboquant/pyturboquant-cpu#readme"
49
+ Repository = "https://github.com/pyturboquant/pyturboquant-cpu"
50
+ Issues = "https://github.com/pyturboquant/pyturboquant-cpu/issues"
51
+
52
+ [tool.setuptools.packages.find]
53
+ where = ["src"]
54
+
55
+ [tool.pytest.ini_options]
56
+ testpaths = ["tests"]
57
+ addopts = "-v"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,36 @@
1
+ """
2
+ pyturboquant-cpu: CPU implementation of TurboQuant vector quantization.
3
+
4
+ TurboQuant is a data-oblivious vector quantization algorithm from
5
+ "TurboQuant: Online Vector Quantization with Near-optimal Distortion Rate"
6
+ (Zandieh et al., arXiv:2504.19874).
7
+
8
+ Two quantization modes are provided:
9
+
10
+ - **MSE-optimal** (``quantize_mse`` / ``dequantize_mse``):
11
+ Minimises mean-squared reconstruction error.
12
+ - **Inner-product-optimal** (``quantize_prod`` / ``dequantize_prod``):
13
+ Gives an *unbiased* inner-product estimator by combining an MSE
14
+ quantizer with a 1-bit QJL residual correction.
15
+ """
16
+
17
+ from pyturboquant_cpu.quantizer import (
18
+ QuantizedMSE,
19
+ QuantizedProd,
20
+ quantize_mse,
21
+ dequantize_mse,
22
+ quantize_prod,
23
+ dequantize_prod,
24
+ )
25
+
26
+ __version__ = "0.1.0"
27
+
28
+ __all__ = [
29
+ "quantize_mse",
30
+ "dequantize_mse",
31
+ "quantize_prod",
32
+ "dequantize_prod",
33
+ "QuantizedMSE",
34
+ "QuantizedProd",
35
+ "__version__",
36
+ ]