|
| 1 | +# Ternary Attention Specification |
| 2 | +# Full ternary attention using TernaryKVCache |
| 3 | +# φ² + 1/φ² = 3 | KOSCHEI IS IMMORTAL |
| 4 | + |
| 5 | +name: ternary_attention |
| 6 | +version: "1.0.0" |
| 7 | +language: zig |
| 8 | +module: ternary_attention |
| 9 | + |
| 10 | +description: | |
| 11 | + Full ternary attention implementation using TernaryKVCache. |
| 12 | + Combines ternary weights, ternary KV cache, and optimized attention. |
| 13 | + No multiplications in attention score computation (only add/sub). |
| 14 | + 16x memory reduction + faster computation. |
| 15 | + |
| 16 | +types: |
| 17 | + TernaryAttentionConfig: |
| 18 | + description: "Configuration for ternary attention" |
| 19 | + fields: |
| 20 | + num_heads: Int |
| 21 | + num_kv_heads: Int |
| 22 | + head_dim: Int |
| 23 | + max_seq_len: Int |
| 24 | + |
| 25 | + TernaryAttentionState: |
| 26 | + description: "Pre-allocated buffers for attention" |
| 27 | + fields: |
| 28 | + scores: List<Float> |
| 29 | + output: List<Float> |
| 30 | + kv_cache: TernaryKVCache |
| 31 | + |
| 32 | +behaviors: |
| 33 | + - name: ternary_attention_scores |
| 34 | + given: f32 query and TernaryKVCache |
| 35 | + when: Computing attention scores Q @ K^T |
| 36 | + then: Use simdTernaryDot for each cached position |
| 37 | + |
| 38 | + - name: ternary_softmax |
| 39 | + given: Attention scores |
| 40 | + when: Normalizing scores |
| 41 | + then: Standard softmax (scores are f32) |
| 42 | + |
| 43 | + - name: ternary_weighted_sum |
| 44 | + given: Softmax weights and TernaryKVCache values |
| 45 | + when: Computing attention output |
| 46 | + then: Dequantize V on-the-fly, accumulate weighted sum |
| 47 | + |
| 48 | + - name: ternary_attention_head |
| 49 | + given: Single query head, TernaryKVCache, head index |
| 50 | + when: Computing attention for one head |
| 51 | + then: Scores → softmax → weighted sum |
| 52 | + |
| 53 | + - name: ternary_attention_gqa |
| 54 | + given: All query heads, TernaryKVCache, GQA config |
| 55 | + when: Computing attention for all heads |
| 56 | + then: Process each head with shared KV heads |
| 57 | + |
| 58 | + - name: online_ternary_attention |
| 59 | + given: Query, TernaryKVCache, tile size |
| 60 | + when: Computing with online softmax |
| 61 | + then: Tiled attention without full score materialization |
| 62 | + |
| 63 | +algorithm: |
| 64 | + ternary_attention: |
| 65 | + description: | |
| 66 | + For each query head h: |
| 67 | + kv_h = h / kv_group_size # GQA mapping |
| 68 | + |
| 69 | + # Compute scores using ternary dot product |
| 70 | + for t in 0..seq_len: |
| 71 | + scores[t] = cache.simdTernaryDot(q_head, t, kv_h) * scale |
| 72 | + |
| 73 | + # Softmax |
| 74 | + softmax(scores) |
| 75 | + |
| 76 | + # Weighted sum with on-the-fly dequantization |
| 77 | + output = zeros(head_dim) |
| 78 | + for t in 0..seq_len: |
| 79 | + v = cache.dequantizeV(t, kv_h) |
| 80 | + output += scores[t] * v |
| 81 | + |
| 82 | +optimizations: |
| 83 | + - name: no_k_dequantization |
| 84 | + description: "ternaryDot computes Q @ K without dequantizing K" |
| 85 | + |
| 86 | + - name: simd_ternary_dot |
| 87 | + description: "8 values per iteration using sign lookup" |
| 88 | + |
| 89 | + - name: lazy_v_dequantization |
| 90 | + description: "Dequantize V only when needed (weighted sum)" |
| 91 | + |
| 92 | + - name: fused_scale_add |
| 93 | + description: "Combine dequantization and accumulation" |
| 94 | + |
| 95 | +memory_analysis: |
| 96 | + f32_attention: |
| 97 | + kv_cache: "O(seq_len * num_kv_heads * head_dim * 4 bytes)" |
| 98 | + scores: "O(seq_len * 4 bytes)" |
| 99 | + |
| 100 | + ternary_attention: |
| 101 | + kv_cache: "O(seq_len * num_kv_heads * head_dim / 4 bytes)" |
| 102 | + scores: "O(seq_len * 4 bytes)" |
| 103 | + savings: "16x on KV cache" |
| 104 | + |
| 105 | +accuracy_considerations: |
| 106 | + - name: quantization_error |
| 107 | + description: "K,V quantized to {-1, 0, +1} with scale" |
| 108 | + |
| 109 | + - name: attention_approximation |
| 110 | + description: "Ternary dot product is approximate" |
| 111 | + |
| 112 | + - name: scale_preservation |
| 113 | + description: "Per-token scales preserve magnitude" |
| 114 | + |
| 115 | +benchmarks: |
| 116 | + - name: memory_reduction |
| 117 | + metric: "ratio" |
| 118 | + target: "16x on KV cache" |
| 119 | + |
| 120 | + - name: attention_speedup |
| 121 | + metric: "ratio" |
| 122 | + target: "1.5-2x (no K dequantization)" |
| 123 | + |
| 124 | + - name: accuracy |
| 125 | + metric: "cosine similarity" |
| 126 | + target: ">0.90" |
| 127 | + |
| 128 | +integration: |
| 129 | + - target: tri_inference.zig |
| 130 | + description: "Replace f32 attention with ternary" |
| 131 | + |
| 132 | + - target: flash_attention.zig |
| 133 | + description: "Add ternary variant of flash attention" |
0 commit comments