Skip to content

feat(hslm): [E] Ternary Attention — integer scoring + sparse attend #334

Description

@gHashTag

Parent: #329 — Fully Ternary Transformer

Level 1 (needs D for quantizer) | ~150 LOC | New: src/hslm/ternary_attention.zig

Description

Replace float Q·K^T attention with ternary scoring: +1 (match), -1 (mismatch), 0 (ignore). Sparse attend with top-k → ternary attention weights at 33% density.

Implementation

  • ternaryScore(q: []const i2, k: []const i2) → i32 — count matches vs mismatches
  • sparseAttend() — top-k scoring → ternary attention weights {-1, 0, +1}, 33% density
  • No softmax needed — just sign of top-k scores
  • Value aggregation: ternary weights × ternary values = integer accumulation

tri CLI

tri hslm ternary-attn

Tests

  • Score symmetry: score(q, k) == score(k, q)
  • Sparse density: exactly 33% non-zero attention weights
  • Attention output is valid ternary

Dependencies

  • Needs D (Ternary Activations) for quantizer

Blocks

  • 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