Skip to content

feat(hslm): TTQ — Trained Ternary Quantization with learned thresholds per layer#565

Merged
gHashTag merged 1 commit into
mainfrom
feat/320-ttq-trained-ternary-quantization
Apr 30, 2026
Merged

feat(hslm): TTQ — Trained Ternary Quantization with learned thresholds per layer#565
gHashTag merged 1 commit into
mainfrom
feat/320-ttq-trained-ternary-quantization

Conversation

@gHashTag
Copy link
Copy Markdown
Owner

Summary

Trained Ternary Quantization (TTQ) — learn per-layer thresholds instead of fixed quantization boundaries.

New file

  • src/b2t/ttq.zig — 196 LOC

Key insight

Standard ternary quantization uses a fixed threshold (e.g., |w| > 0.5 → ±1). TTQ learns the optimal threshold per layer via STE gradient, improving accuracy by adapting quantization boundaries to each layer's weight distribution.

Features

  • TTQLayer: per-layer learned threshold with STE gradient
  • quantize(): weight → {P, Z, N} based on learned threshold
  • quantizeScaled(): scale-dependent threshold for progressive quantization
  • computeGradient(): straight-through estimator for threshold update
  • sparsity(): fraction of zeros at current threshold
  • effectiveBits(): entropy-based effective bit width
  • TTQNetwork: multi-layer threshold management

Tests (6)

  • Basic quantization, threshold update, sparsity, scaled quantize, multi-layer, effective bits

Closes #320

- Add src/b2t/ttq.zig
- TTQLayer: per-layer learned threshold for ternary quantization
- Quantize: weight → {P, Z, N} based on threshold
- STE gradient approximation for threshold update
- Scaled quantize for layer-dependent thresholds
- Sparsity and effective bits computation
- TTQNetwork: multi-layer threshold management
- 6 tests: quantize, threshold update, sparsity, scaled,
  multi-layer network, effective bits

Closes #320
@gHashTag gHashTag merged commit 3552171 into main Apr 30, 2026
9 of 19 checks passed
@gHashTag gHashTag deleted the feat/320-ttq-trained-ternary-quantization branch April 30, 2026 00:51
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.

feat(hslm): TTQ — Trained Ternary Quantization with learned thresholds per layer

1 participant