Skip to content

feat(hslm): [D] Ternary Activations — STE quantizer + integer matmul #333

Description

@gHashTag

Parent: #329 — Fully Ternary Transformer

Level 1 (needs B for int embed) | ~200 LOC | New: src/hslm/ternary_activations.zig

Description

Quantize activations to ternary {-1, 0, +1} with STE (Straight-Through Estimator) for gradients. Pure integer ternary matmul: Vec32i8 = 32 ops/cycle with widening i8→i16 multiply.

Implementation

  • TernaryQuantizer: forward quantize (threshold-based), backward STE pass-through
  • integerTernaryMatmul() — pure integer version (i8 activations × i2 weights)
  • simdIntegerTernaryMatmul() — SIMD vectorized (Vec32i8 = 32 parallel multiply-accumulates)
  • Widening multiply: i8 × i8 → i16, accumulate in i32

tri CLI

tri hslm quantize-act

Tests

  • Quantizer roundtrip: quantize(dequantize(x)) preserves sign
  • Integer matmul == float matmul (exact for ternary)
  • SIMD result == scalar result
  • Benchmark: integer vs float matmul throughput

Dependencies

  • Needs B (Trit Encoding) for integer embedding input

Blocks

  • Issue E (Ternary Attention)
  • Issue J (Full Ternary Inference)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions