Skip to content

TurboQuant Lloyd Max Codebook - #8672

Merged
JojiiOfficial merged 3 commits into
devfrom
tq-lloyd-max
Apr 16, 2026
Merged

JojiiOfficial merged 3 commits into
devfrom
tq-lloyd-max

Conversation

@JojiiOfficial

Copy link
Copy Markdown
Member

Depends on #8657

Implements Lloyd max algorithm and unit-test to verify hard-coded optimal codebook cluster.

@JojiiOfficial JojiiOfficial added this to the TurboQuant milestone Apr 14, 2026
@JojiiOfficial JojiiOfficial changed the title Tq lloyd max TurboQuant Lloyd Max Codebook Apr 14, 2026
@JojiiOfficial JojiiOfficial mentioned this pull request Apr 14, 2026
63 of 69 tasks
@JojiiOfficial
JojiiOfficial force-pushed the tq-lloyd-max branch 2 times, most recently from a2558d9 to 9a982cb Compare April 16, 2026 07:43
Base automatically changed from tq-hadamard to dev April 16, 2026 09:46
@coderabbitai

coderabbitai Bot commented Apr 16, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A new Rust module lloyd_max.rs was added to the turboquant quantization library, containing hardcoded Lloyd-Max centroid tables for 1, 2, and 4-bit Gaussian quantization. The module exposes a public function get_centroids(bits: u8) that returns the appropriate centroid slice for supported bit-widths, panicking for unsupported values. The module includes a test suite that validates the hardcoded centroids by numerically approximating the standard normal PDF/CDF and solving the Lloyd-Max optimization problem iteratively, verifying results match within 1e-3 tolerance.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'TurboQuant Lloyd Max Codebook' directly describes the main change: adding Lloyd-Max algorithm implementation with hardcoded optimal codebook constants for TurboQuant quantization.
Description check ✅ Passed The description accurately describes the changeset: implementing the Lloyd-Max algorithm and adding unit tests to verify hardcoded optimal codebook clusters, which matches the file changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tq-lloyd-max

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
lib/quantization/src/turboquant/lloyd_max.rs (1)

3-3: Add doc comment for consistency.

CENTROIDS_2BIT and CENTROIDS_4BIT have doc comments describing their purpose and source distribution, but CENTROIDS_1BIT lacks one. Consider adding a consistent doc comment for clarity.

📝 Suggested doc comment
+/// 1-bit (2 centroids): Lloyd-Max for N(0,1)
 const CENTROIDS_1BIT: &[f32] = &[-0.797_884_6, 0.797_884_6];
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@lib/quantization/src/turboquant/lloyd_max.rs` at line 3, Add a doc comment
for CENTROIDS_1BIT similar to the existing comments for CENTROIDS_2BIT and
CENTROIDS_4BIT: explain that these are the precomputed centroids for 1-bit
Lloyd–Max quantization and state the source distribution and normalization
(e.g., derived for a standard normal N(0,1) or the same distribution used by the
other centroids). Place the comment immediately above the constant declaration
for CENTROIDS_1BIT to match style and tone of the other centroid constants.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@lib/quantization/src/turboquant/lloyd_max.rs`:
- Line 3: Add a doc comment for CENTROIDS_1BIT similar to the existing comments
for CENTROIDS_2BIT and CENTROIDS_4BIT: explain that these are the precomputed
centroids for 1-bit Lloyd–Max quantization and state the source distribution and
normalization (e.g., derived for a standard normal N(0,1) or the same
distribution used by the other centroids). Place the comment immediately above
the constant declaration for CENTROIDS_1BIT to match style and tone of the other
centroid constants.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3e19a9a2-95d8-4f7d-a4b0-c87e9c3adafc

📥 Commits

Reviewing files that changed from the base of the PR and between 8bd1600 and 480c75f.

📒 Files selected for processing (2)
  • lib/quantization/src/turboquant/lloyd_max.rs
  • lib/quantization/src/turboquant/mod.rs

@JojiiOfficial
JojiiOfficial merged commit 1346cc5 into dev Apr 16, 2026
15 checks passed
@JojiiOfficial
JojiiOfficial deleted the tq-lloyd-max branch April 16, 2026 10:17
This was referenced Apr 17, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Apr 27, 2026
timvisee pushed a commit that referenced this pull request May 8, 2026
* [AI] Add lloyd_max calculation test and constants

* [AI] use closed-form formula

* Review remarks
@coderabbitai coderabbitai Bot mentioned this pull request May 18, 2026
8 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants