mktnhr commited on
Commit
2698a31
·
verified ·
1 Parent(s): 42db578
.gitattributes CHANGED
@@ -58,3 +58,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
58
  # Video files - compressed
59
  *.mp4 filter=lfs diff=lfs merge=lfs -text
60
  *.webm filter=lfs diff=lfs merge=lfs -text
 
 
 
58
  # Video files - compressed
59
  *.mp4 filter=lfs diff=lfs merge=lfs -text
60
  *.webm filter=lfs diff=lfs merge=lfs -text
61
+ Bitstate/BitState_LNS8_base74_8step_zone8_popcount_demo_EN_v0.9_RMSNorm.xlsx filter=lfs diff=lfs merge=lfs -text
62
+ Bitstate/BitState_LNS8_base74_8step_zone8_popcount_demo_JA_v0.9_RMSNorm.xlsx filter=lfs diff=lfs merge=lfs -text
Bitstate/BitState_GitHub_author_notes_JA_v0.5.md ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GitHub投稿を自分で書くための論点メモ(BitState / HSDR PoC v0.5)
2
+
3
+ 重要: llama.cpp系リポジトリには、AIにfeature request / issue / discussion本文を書かせることを禁止する方針があります。このファイルは投稿本文ではなく、自分の言葉で短く書くための技術的な論点メモです。コピー&ペースト用文章としては使わないでください。
4
+
5
+ ## 投稿先候補
6
+
7
+ 第一候補:
8
+ - PrismML-Eng/llama.cpp の Issue / feature discussion
9
+
10
+ 理由:
11
+ - Bonsai 2固有runtime / ternary kernel実験に近い。
12
+ - 新しいGGUF型を要求せず、既存PQ2_0/PTQ1_0の意味論でmicrobenchmarkできる。
13
+ - mainline採用を求める話ではなく、まずforkでCPU PoCを測る範囲に限定できる。
14
+
15
+ ## 自分の言葉で最初に伝える要点
16
+
17
+ - 「速くなる」と断定しない。性能仮説を測りたい。
18
+ - 主対象はbatch-1 ternary GEMV。
19
+ - 中心アイデアは、LNS productを `S1|E4|L3` として扱い、K個のproductを最終的に8本のphase aggregateへ縮約すること。
20
+ - correctness baselineは `A[L3] += sign × (1<<E4)` のdirect 8-phase integer accumulation。K=4096/5120でも最後は8本。
21
+ - phase maskはTile64/Tile32上の中間routing selectorで、Excel v0.8で128項の一致を確認済み。
22
+ - POPCOUNT histogramは別候補。signed populationは `P-N` または `Ntotal-2Nneg` でexactに取れる。
23
+ - どちらが速いかは未測定で、K→8 routing/reduction costをbenchmarkする。
24
+ - direct 8-phase dyadic pathをreferenceとして残し、full histogram→shift/addは等価な別実装として比較する。
25
+ - v0.5では、histogram候補についてPOPCOUNT後のstateをfractional-LNSへfoldし、balanced LNS add/sub treeで最後までlinear化しない候補も試す。
26
+ - 最終8 phaseなら `8->4->2->1` の3 dependent stages。
27
+ - shared scaleも可能ならlog offset ADDで適用する。
28
+ - negative resultでも、state-set build / POPCOUNT / LUT / dependency depthのどこが支配したか分かれば有用。
29
+
30
+ ## 実装者に聞きたいこと
31
+
32
+ 必要なら2〜3個に絞る:
33
+
34
+ - Tile64/Tile32で `packed product ADD -> phase routing -> eight integer accumulators` をfuseするなら、現在のggml scheduling/layout上どこに置くのが自然か。
35
+ - standalone CPU microbenchmarkならPQ2_0 / predecoded ternary masksから始めるのが妥当か。
36
+ - row costの比較で、total cyclesだけでなくdependent critical-path stagesを測る方法として何が自然か。
37
+ - pure fractional-LNS add/sub LUTを実験するなら、どのSIMD width / table layoutが扱いやすいか。
38
+
39
+ ## 最初の投稿では削った方がよい話
40
+
41
+ - 特許の長い検討
42
+ - 研究新規性の主張
43
+ - FPGA/ASICまで含む将来構想
44
+ - prefill全般を置き換える話
45
+ - Tensor Coreより速いという主張
46
+ - Q9.6またはpure-LNSが絶対に正しいという主張
47
+ - 1000行級仕様の本文貼り付け
48
+
49
+ ## 添付/リンクするなら優先度
50
+
51
+ 1. `BitState_PoC_technical_brief_EN_v0.5.md`
52
+ 2. `BitState_PoC_experiment_matrix_EN_v0.5.md`
53
+ 3. `bitstate_poc_test_vectors_v2.json`
54
+ 4. `BitState_HSDR_v0.5_WHT-RMSNorm-premise.md`
55
+ 5. `LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx`
56
+
57
+ ## タイトルを自分で考える際に含めたい概念
58
+
59
+ - experimental / PoC
60
+ - ternary GEMV
61
+ - state-domain reduction
62
+ - POPCOUNT
63
+ - dependency breaking
64
+ - batch-1 decode
65
+ - Bonsai 2
66
+
67
+ 「proposal to replace GEMM」や「new LNS architecture」など大きすぎる表現は避ける。
68
+
69
+ ## 投稿後の返答で守る点
70
+
71
+ - 実測がない部分は仮説と明示する。
72
+ - pairwise LNS add自体は既知技法として扱い、独自性を主張しない。
73
+ - BitStateでまず検証したいのは `packed 8-bit product ADD -> K→8 phase reduction -> 8-state finalization` のcost structure。POPCOUNT state-set pathはその一候補として比較する。
74
+ - dyadic referenceの方が速い可能性も普通に認める。
75
+ - activation quantization品質、state-count correctness、pure-LNS rounding、kernel速度を別問題として扱う。
76
+ - AIに返信本文そのものを書かせず、技術内容の確認だけに使う。
Bitstate/BitState_HSDR_v0.5_en_WHT-RMSNorm-premise.md ADDED
@@ -0,0 +1,1482 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BitState / HSDR
2
+
3
+ ## Finite-State Population Reduction and Dependency-Breaking Log-Domain Reduction for Low-Bit LLM Inference
4
+
5
+ **Implementation Proposal v0.5**
6
+ **Primary target:** low-bit LLM GEMV / small-batch decode
7
+ **Reference/validation numeric path:** LNS8 → finite-state population reduction → wide integer/fixed-point → optional Q9.6 → LNS8
8
+ **Optimized candidate path:** LNS8 → state-domain population reduction → relative fractional-LNS → balanced LNS add/sub tree → LNS8
9
+
10
+ ---
11
+
12
+ ## 1. Purpose
13
+
14
+ BitState is a compute model for low-bit inference that moves the reduction problem into a finite **state domain** before performing unavoidable numerical reduction.
15
+
16
+ The core idea is:
17
+
18
+ > **Move the reduction into the state domain to break the long numerical reduction dependency.** Form wide state sets, count equivalent states in parallel, and let only the compressed state population enter the remaining arithmetic tree.
19
+
20
+ Delaying numerical materialization remains important, but it is now a consequence of the stronger objective: replace a long dependent arithmetic chain with parallel set formation / POPCOUNT and a much smaller final reduction.
21
+
22
+ The main pipeline is below. v0.5 explicitly adds **direct reduction from final product states into eight phase integer accumulators** as the simplest correctness and implementation baseline. A full POPCOUNT histogram remains a separate candidate for stronger dependency breaking.
23
+
24
+ ```text
25
+ packed low-bit values
26
+ ↓
27
+ packed LNS product-state ADD / sign XOR
28
+ ↓
29
+ final product state = S1 | E4 | L3
30
+ ↓
31
+ State Tile (CPU Tile64 / GPU Tile32, etc.)
32
+ ↓
33
+ phase routing / state-set construction
34
+ ├───────────────────────────────────────────────┐
35
+ │ │
36
+ │ Direct 8-phase dyadic path │ Population / Pure-LNS path
37
+ ↓ ↓
38
+ A[L3] += sign × (1 << E4) signed state populations
39
+ ↓ ↓
40
+ 8 integer accumulators dyadic histogram or fractional-LNS
41
+ ↓ ↓
42
+ 8 phase coefficients + short final sum balanced κ± add/sub tree
43
+ ↓ ↓
44
+ optional Q9.6 / LNS8 boundary LNS8 boundary
45
+ ```
46
+
47
+ The optimized path is allowed to remain in logarithmic coordinates from product generation through final reduction. No FP/linear materialization is required inside the GEMV reduction itself.
48
+
49
+ The primary optimization target is not peak FLOPS. It is the whole decode pipeline:
50
+
51
+ ```text
52
+ memory load + low-bit decode + product generation + reduction + re-encoding
53
+ ```
54
+
55
+ ---
56
+
57
+ ### 1.1 Design premise — rotation-aware quantization makes narrower numeric domains practical
58
+
59
+ One reason BitState can seriously consider relatively narrow internal formats such as LNS8 and Q9.6 is the increasing use of **rotation-aware quantization** in low-bit LLMs. Work such as QuaRot and SpinQuant illustrates the broader direction: WHT / Hadamard or learned rotations redistribute outliers and concentrated energy across dimensions before low-bit representation, reducing the need for every GEMM stage to preserve rare full-scale values with a conventional BF16/FP16-style dynamic range.
60
+
61
+ This is not a claim that full-scale dynamic range is unnecessary for every model. The important engineering change is that more models can be designed around the **measured post-rotation activation and weight distributions**, rather than requiring the inference kernel to reproduce the entire theoretical dynamic range of BF16/FP16 unconditionally.
62
+
63
+ Bonsai2 is a concrete example. Its public model documentation states that language-model weights are stored in a blockwise Hadamard-rotated basis and that the runtime applies the matching transform to activations. Low-bit coverage includes embeddings, attention projections, MLP projections, and the LM head. The MLX package also documents that ordinary loaders produce incorrect output if they omit the activation transform and the inverse embedding lookup.
64
+
65
+ For Bonsai2, rotation is therefore not merely an offline quantizer preprocessing step. It is a **numeric-basis contract between the checkpoint and the runtime**. The token embedding / token path participates in that contract, with the corresponding inverse transform required at the embedding-lookup boundary.
66
+
67
+ The multimodal path requires a distinction. Public packaging information shows configurations where the vision tower itself remains unrotated and unquantized in FP16. However, once projected visual tokens enter the language model, they must follow the language model's rotation-aware activation path. Thus the **multimodal interface is not independent of the language-side basis contract**, even though the vision tower itself is not Hadamard-rotated.
68
+
69
+ The public Bonsai2 materials do not fully disclose the complete quantization-aware training recipe, so this document does not claim a specific training procedure that is not documented. What is clear is that the distributed checkpoint is rotation-native and requires matching activation transforms and inverse embedding handling at runtime. This is sufficient to treat **rotation-aware low-bit model/runtime design** as an important premise when evaluating whether LNS8 / Q9.6 can use a narrower measured dynamic range.
70
+
71
+ A BitState implementation MUST measure, per target model:
72
+
73
+ - activation absolute maxima before and after WHT / rotation;
74
+ - activation absolute maxima before and after RMSNorm, and before and after learned gain `gamma`;
75
+ - RMS and percentile distributions;
76
+ - required dynamic range by layer;
77
+ - LNS8 saturation / underflow rates;
78
+ - Q9.6 saturation rates; and
79
+ - end-to-end quality with the rotation path enabled.
80
+
81
+ In other words, BitState does **not** discard full-scale support merely because a rotation exists. It uses the measured post-rotation distribution to preserve only the range that the model actually requires.
82
+
83
+ References:
84
+
85
+ - https://arxiv.org/abs/2404.00456 (QuaRot)
86
+ - https://arxiv.org/abs/2405.16406 (SpinQuant)
87
+ - https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-gguf
88
+ - https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-mlx-2bit
89
+ - https://github.com/PrismML-Eng/Bonsai-demo
90
+
91
+ ### 1.2 Design premise — widespread RMSNorm makes pre-GEMM absolute activation range easier to bound
92
+
93
+ Separate from rotation-aware quantization, **RMSNorm** is now widely used in modern decoder-only LLM families. Qwen3.5, Gemma 3, Llama 4, and Mistral-family implementations all use RMS normalization layers. BitState should therefore treat not only post-WHT / post-rotation activations, but also the **post-RMSNorm activation domain** as an important integration point for LNS8.
94
+
95
+ RMSNorm does not clip individual outliers. It rescales the entire hidden vector for each token by a common root-mean-square factor. Before the learned gain is applied, let the normalized vector be `u`:
96
+
97
+ ```text
98
+ u_i = x_i / sqrt(mean_j(x_j^2) + eps)
99
+ ```
100
+
101
+ For hidden dimension `d`:
102
+
103
+ ```text
104
+ sum_i(u_i^2) <= d
105
+ ```
106
+
107
+ and therefore every coordinate has the theoretical bound:
108
+
109
+ ```text
110
+ |u_i| <= sqrt(d)
111
+ ```
112
+
113
+ This does **not** mean that channel-wise outliers disappear. Energy can still concentrate strongly in one coordinate. What changes is the absolute numeric scale: values that may have reached tens or hundreds of thousands before normalization are forced back into a finite domain determined by the hidden dimension. A subsequent WHT / Hadamard rotation can then redistribute the remaining channel-wise concentration across dimensions. From the BitState perspective, RMSNorm and WHT are therefore complementary rather than competing mechanisms.
114
+
115
+ The current reference LNS8 format, `S1E4L3 / base74`, has maximum magnitude:
116
+
117
+ ```text
118
+ x_max = 2^((127 - 74)/8) ~= 98.7015
119
+ ```
120
+
121
+ If LNS8 encoding is placed directly after RMS normalization and before learned `gamma`, the theoretical one-axis worst case fits without saturation for approximately:
122
+
123
+ ```text
124
+ d <= x_max^2 ~= 9741.98
125
+ ```
126
+
127
+ Representative hidden sizes:
128
+
129
+ ```text
130
+ hidden 5120 -> sqrt(d) ~= 71.55
131
+ hidden 8192 -> sqrt(d) ~= 90.51
132
+ hidden 9216 -> sqrt(d) = 96.00
133
+ hidden 12288 -> sqrt(d) ~= 110.85
134
+ ```
135
+
136
+ Thus, for hidden sizes in the 5120-9216 range, the current base74 / S1E4L3 range of about +/-98.7 can **cover the RMSNorm theoretical coordinate bound itself**. At hidden size 12288, only the mathematical worst-case coordinate exceeds the range, by roughly 12%. The implementation can then choose, based on measurement, among:
137
+
138
+ 1. saturating / clamping only the rare overflow to the LNS8 maximum;
139
+ 2. shifting a tensor/model-level system exponent offset slightly upward (for example, `code 72 = 1.0` gives a maximum of about 117.38); or
140
+ 3. using a future wide-range profile / escape path only for tensors that empirically require it.
141
+
142
+ Real RMSNorm layers normally apply a learned gain `gamma_i` after normalization, so their final output is not strictly bounded by `sqrt(d)`. BitState must therefore choose per model/backend whether to **encode after RMS normalization but before `gamma`, fold `gamma` into downstream linear weights where valid, or size the LNS8 range from the measured post-`gamma` distribution**.
143
+
144
+ The purpose of this premise is not to claim that RMSNorm eliminates the need for full-scale support in every model. The engineering hypothesis is narrower: **for activation paths whose absolute range is already controlled by RMSNorm and/or whose channel concentration is reduced by WHT / rotation, preserving the finer 8-states-per-octave S1E4L3 grid may be preferable to sacrificing precision across all elements merely to preserve rare theoretical extremes**.
145
+
146
+ Additional references:
147
+
148
+ - https://arxiv.org/abs/1910.07467 (RMSNorm)
149
+ - https://huggingface.co/docs/transformers/main/en/model_doc/qwen3_5
150
+ - https://huggingface.co/docs/transformers/model_doc/gemma3
151
+ - https://huggingface.co/docs/transformers/model_doc/llama4
152
+ - https://huggingface.co/docs/transformers/model_doc/mistral
153
+
154
+ ---
155
+
156
+ ## 2. BitState and HSDR
157
+
158
+ ### BitState
159
+
160
+ BitState treats a low-bit numerical representation as a small finite state space that can be manipulated using Boolean and integer operations.
161
+
162
+ Typical primitives:
163
+
164
+ - packed integer ADD
165
+ - XOR
166
+ - AND / OR / NOT
167
+ - SHIFT
168
+ - bit transpose / ballot / mask generation
169
+ - POPCOUNT
170
+ - small integer reduction
171
+ - small LUT
172
+
173
+ ### HSDR — Hierarchical State-Domain Reduction
174
+
175
+ HSDR reduces many element-wise numerical operations through progressively smaller state domains **and uses that domain change to cut the serial reduction dependency**.
176
+
177
+ Typical hierarchy:
178
+
179
+ ```text
180
+ individual elements
181
+ ↓
182
+ finite-state IDs
183
+ ↓
184
+ wide state sets / masks
185
+ ↓
186
+ parallel population counts
187
+ ↓
188
+ small set of state-domain aggregates
189
+ ├─ reference: integer/fixed-point realization
190
+ └─ candidate: fractional-LNS reduction tree
191
+ ```
192
+
193
+ The essential idea is:
194
+
195
+ > **State-domain reduction is a dependency-breaking transform.** Count equivalent states in parallel first; only the compressed state set is allowed to enter a dependent arithmetic reduction.
196
+
197
+ The objective is therefore not merely fewer values. It is a shallower dependency graph that maps naturally to scalar bit operations, SIMD masks, GPU ballots, and hardware population counters.
198
+
199
+ ---
200
+
201
+ ## 3. LNS8 reference format
202
+
203
+ The current LNS8 magnitude grid is:
204
+
205
+ ```text
206
+ |x| = 2^((k - 74) / 8)
207
+ ```
208
+
209
+ where:
210
+
211
+ - `k = 1..127` : non-zero magnitude code
212
+ - `k = 0` : exact zero
213
+ - sign is stored separately
214
+ - 8 codes = exactly ×2
215
+ - code 74 = 1.0
216
+
217
+ For a non-zero 7-bit magnitude code:
218
+
219
+ ```text
220
+ phase = k & 0x07 // lower 3 bits, intra-octave index 0..7
221
+ octave = k >> 3 // upper 4 bits, coarse exponent / octave index 0..15
222
+ ```
223
+
224
+ Terminology used in this document:
225
+
226
+ - **phase** = lower 3 bits = intra-octave position
227
+ - **octave** = upper 4 bits = coarse exponent
228
+ - **state** = the complete finite-state code, optionally including sign
229
+
230
+ Because 74 = 9×8 + 2:
231
+
232
+ ```text
233
+ 2^((k - 74)/8)
234
+ = 2^(octave - 9) × 2^((phase - 2)/8)
235
+ ```
236
+
237
+ This factorization is useful only after population counting. It does not constrain how the histogram itself must be generated.
238
+
239
+ ### 3.1 Logical S1E4L3 decomposition — exponent × fractional-log field
240
+
241
+ The same 8-bit representation can be read not only as a flat 7-bit magnitude code, but also as an FP-like field structure. For non-zero states, the logical decomposition is:
242
+
243
+ ```text
244
+ bit 7 bits 6..3 bits 2..0
245
+ +------+-------------------+-------------------+
246
+ | S1 | E4 | L3 |
247
+ +------+-------------------+-------------------+
248
+ | sign | octave / exponent | fractional-log |
249
+ | | | phase |
250
+ +------+-------------------+-------------------+
251
+ ```
252
+
253
+ Let `E = octave`, `L = phase`, and `s ∈ {-1,+1}`. A non-zero value is then
254
+
255
+ ```text
256
+ x = s × 2^(E - 9) × 2^((L - 2)/8)
257
+ = s × 2^(E - 9 + (L - 2)/8)
258
+ ```
259
+
260
+ The fields therefore have the following roles:
261
+
262
+ - **S1** — sign. In population reduction it maps naturally to positive / negative populations.
263
+ - **E4** — a 4-bit coarse binary exponent / octave. `E + 1` is exactly `×2`, so this axis maps to SHIFT / dyadic scaling during materialization.
264
+ - **L3** — a 3-bit fractional logarithmic field. It divides one octave into eight states representing `2^((L-2)/8)`.
265
+
266
+ For intuition, L3 may be described as a **logarithmic mantissa**, but it is not an IEEE-style linear mantissa / significand. Mathematically it is a **fractional exponent**. To avoid ambiguity, this document uses **fractional-log field** or the existing term **phase** as the formal name.
267
+
268
+ Compared with an ordinary E4M3-like structure:
269
+
270
+ ```text
271
+ ordinary FP-like E4M3:
272
+ x ~ sign × 2^E × (linear significand from M3)
273
+
274
+ BitState LNS8 logical S1E4L3:
275
+ x = sign × 2^E × 2^(fractional-log field)
276
+ (with the base74 bias terms shown above)
277
+ ```
278
+
279
+ Thus the visible layout is `S1 | E4 | L3`, but the lower field remains part of the logarithmic coordinate. LNS multiplication therefore requires no linear mantissa multiplier. In full-code form it remains
280
+
281
+ ```text
282
+ k_product = k_a + k_b - 74
283
+ s_product = s_a XOR s_b
284
+ ```
285
+
286
+ In field form, carry / borrow from the `L3` sum flows directly into `E4`. For non-zero inputs:
287
+
288
+ ```text
289
+ t = L_a + L_b - 2
290
+ L_p = t mod 8
291
+ E_p = E_a + E_b - 9 + floor(t / 8)
292
+ ```
293
+
294
+ which is exactly equivalent to `k_product = k_a + k_b - 74`. Fast implementations must keep a wider signed intermediate rather than saturating prematurely to the final storage range.
295
+
296
+ ### 3.2 2-D state-space view
297
+
298
+ From the HSDR perspective, the non-zero magnitude states can be treated not only as a flat set of 127 values, but conceptually as the product of three small axes:
299
+
300
+ ```text
301
+ vertical axis : E4 = 16 coarse exponent / power-of-two scale positions
302
+ horizontal axis : L3 = 8 intra-octave logarithmic positions
303
+ sign axis : S1 = positive / negative
304
+ ```
305
+
306
+ For intuition, the **horizontal direction can be viewed as an `S1 + L3` 4-bit signed intra-octave LNS slice, while the vertical direction is an E4M0-like pure power-of-two scale axis**. This is an algorithmic / visualization decomposition, not a claim that the format is IEEE E4M0 or an independent 4-bit LNS storage type. `k=0` remains the separately reserved exact-zero state.
307
+
308
+ This view maps directly onto the later HSDR operations:
309
+
310
+ ```text
311
+ S1 -> sign mask / signed population
312
+ L3 -> 8 phase buckets / 8 fixed fractional-log coefficients
313
+ E4 -> exact power-of-two position / integer SHIFT axis
314
+ ```
315
+
316
+ Accordingly, `H[phase][octave]` is not merely a convenient 2-D histogram. It is the direct state-domain expansion of the logical S1E4L3 fields, with sign folded into signed counts. BitState exploits this product structure by mapping sign, fractional-log phase, and power-of-two scale onto separate cheap primitives before final reduction.
317
+
318
+ ---
319
+
320
+ ## 4. Stage A — Product-state generation
321
+
322
+ LNS multiplication becomes exponent addition.
323
+
324
+ Conceptually:
325
+
326
+ ```text
327
+ product_exp = weight_exp + activation_exp + bias_adjust
328
+ product_sign = weight_sign XOR activation_sign
329
+ ```
330
+
331
+ The fast path should use packed integer arithmetic wherever the dynamic range permits it.
332
+
333
+ Candidate implementations:
334
+
335
+ - packed INT8 ADD
336
+ - packed INT16 reference path
337
+ - SWAR lane arithmetic
338
+ - SIMD byte arithmetic
339
+ - SIMT lane arithmetic
340
+
341
+ ### Product-state completion rule
342
+
343
+ Before decomposing into `L3` / `E4`, building masks, or bit-slicing, apply **all log-domain offsets that belong to the final product state for that variant**. Weight exponent, preconverted scale offset, and bias adjustment must be included first if they change the routed product code.
344
+
345
+ ```text
346
+ activation LNS code
347
+ + weight / scale / bias log offsets
348
+ ↓
349
+ final product magnitude code
350
+ ↓
351
+ L3 / E4 decomposition, bit transpose, phase routing
352
+ ```
353
+
354
+ A mathematically factorable shared group scale may instead be kept outside as `d × Σ(...)`, but that is a **separate factorized variant**. Do not mix “scale included in product-state routing” and “scale factored after reduction” in the same implementation description.
355
+
356
+ ### Range rule
357
+
358
+ The first correctness implementation MUST use a wider intermediate type than the final LNS code.
359
+
360
+ Measure:
361
+
362
+ - minimum product exponent
363
+ - maximum product exponent
364
+ - overflow frequency
365
+ - underflow frequency
366
+
367
+ Only after the observed range is understood should packed INT8 saturation/wrapping rules be selected.
368
+
369
+ ---
370
+
371
+ ## 5. Stage B — State Tile
372
+
373
+ Process multiple product states as a tile.
374
+
375
+ Recommended initial tile sizes:
376
+
377
+ ```text
378
+ Tile32
379
+ Tile64
380
+ Tile128
381
+ ```
382
+
383
+ CPU reference target:
384
+
385
+ - Tile64, because a state-membership mask maps naturally to one 64-bit integer.
386
+
387
+ Bonsai2 reference target:
388
+
389
+ - Tile128, because one ternary weight scale group already contains 128 weights.
390
+
391
+ A State Tile is a logical grouping. The backend is free to keep the values in registers, vector lanes, warp lanes, or bitsets.
392
+
393
+ ---
394
+
395
+ ## 6. Stage C — S1E4L3 decomposition and direct 8-phase reduction
396
+
397
+ For each final product state:
398
+
399
+ ```text
400
+ phase = state & 0x07 // L3, 0..7
401
+ octave = state >> 3 // E4, 0..15
402
+ sign = product sign // S1
403
+ ```
404
+
405
+ The v0.5 correctness baseline does not require a persistent full histogram. Each product can be reduced directly into **eight signed integer phase accumulators**:
406
+
407
+ ```text
408
+ payload = sign × (1 << octave)
409
+ A[phase] += payload
410
+ ```
411
+
412
+ Equivalently:
413
+
414
+ ```text
415
+ A[p] = Σ_{i: phase_i=p} sign_i × 2^(octave_i)
416
+ ```
417
+
418
+ Final materialization is:
419
+
420
+ ```text
421
+ F[p] = 2^((p - 2)/8)
422
+ S = 2^-9 × Σ_{p=0..7} A[p] × F[p]
423
+ ```
424
+
425
+ For quantized LNS8 states this decomposition is algebraically exact. It is equivalent to first building `H[phase][octave]` and then computing `Σ H[p][o] << o`.
426
+
427
+ ### C1. GEMM / GEMV interpretation
428
+
429
+ For one output dot with `K=4096` or `K=5120`, all K product states ultimately feed only **A[0..7]**. Once those eight integers exist, only eight fixed phase coefficients and a short final sum remain. The dominant implementation question is therefore **K products → eight weighted phase accumulators**, not the final materialization.
430
+
431
+ ### C2. Excel correctness reference
432
+
433
+ This path uses the procedure demonstrated in `LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx` (`Inputs_128` / `BitSlice128_一致確認`) as a correctness reference.
434
+
435
+ The workbook treats 128 inputs as `Tile64 × 2`, interprets each product as:
436
+
437
+ ```text
438
+ S1 | E4 | L3
439
+ ```
440
+
441
+ forms `±(1 << E4)`, routes by `L3`, and verifies that the eight integer accumulators reconstruct the same result as direct decoding.
442
+
443
+ ### C3. Histogram / POPCOUNT remains a valid exact family
444
+
445
+ A full-state histogram remains a separate exact implementation family:
446
+
447
+ ```text
448
+ H[phase][octave]
449
+ A[phase] = Σ_o H[phase][o] × 2^o
450
+ ```
451
+
452
+ The direct 8-phase path fuses bucket creation with dyadic shift/add. The histogram path preserves state populations explicitly and is still useful for POPCOUNT-based dependency breaking and pure-LNS folding.
453
+
454
+ ## 7. Stage D — Bit transpose / backend-equivalent routing-mask construction
455
+
456
+ For a CPU Tile64, fields of the final product states can be represented as 64-bit bitplanes:
457
+
458
+ ```text
459
+ L0 L1 L2 // three L3 bitplanes
460
+ E0 E1 E2 E3 // four E4 bitplanes
461
+ BSIGN // sign plane, 1 = negative
462
+ ```
463
+
464
+ The same bit position refers to the same original lane across all planes.
465
+
466
+ ### D1. Phase masks are intermediate selectors
467
+
468
+ Boolean decode of `L2,L1,L0` produces a 64-bit mask `M[p]` whose set bits are exactly the lanes belonging to phase `p`.
469
+
470
+ Example:
471
+
472
+ ```text
473
+ M[101] = L2 & ~L1 & L0
474
+ ```
475
+
476
+ This is an **intermediate routing/selector mask**, not a final population or numerical result. It may be generated on demand and discarded after accumulation. All eight masks need not be stored persistently.
477
+
478
+ The 3-to-8 decoder should share intermediate 2-bit groups instead of performing eight fully independent 3-bit comparisons.
479
+
480
+ ### D2. Sign handling in bit form
481
+
482
+ A bitset cannot directly represent `-1` population. For exact signed counts:
483
+
484
+ ```text
485
+ Hpos = POPCOUNT(M & ~BSIGN)
486
+ Hneg = POPCOUNT(M & BSIGN)
487
+ Hsigned = Hpos - Hneg
488
+ ```
489
+
490
+ or equivalently:
491
+
492
+ ```text
493
+ N = POPCOUNT(M)
494
+ Nneg = POPCOUNT(M & BSIGN)
495
+ Hsigned = N - 2*Nneg
496
+ ```
497
+
498
+ This is exactly the same population as the Excel `(+ POPCOUNT) - (- POPCOUNT)` construction. Algebraic XOR identities also exist, but they temporarily count negative lanes outside the local bucket; the table-local formulas above are preferred for the reference implementation.
499
+
500
+ ### D3. Phase POPCOUNT alone is insufficient for direct 8-accumulator reduction
501
+
502
+ `POPCOUNT(M[p])` loses E4 weighting. Within one phase, `E=3` and `E=11` contribute different magnitudes, so the direct path must still perform the weighted reduction:
503
+
504
+ ```text
505
+ A[p] += ±(1 << E)
506
+ ```
507
+
508
+ Bit transpose and phase masks are intermediate wide-routing representations. POPCOUNT is optional, not mandatory, for the direct path.
509
+
510
+ Backend equivalents:
511
+
512
+ - scalar 64-bit CPU: `uint64_t` bitplanes and masks
513
+ - AVX2 / AVX-512: vector mask extraction, mask logic, optional vector/scalar popcount
514
+ - ARM NEON/SVE: predicates / bit operations / count
515
+ - CUDA: 32-lane ballot / lane predicates / subgroup reduction
516
+ - FPGA/ASIC: 3-to-8 decoder + sign + shifted-payload routing
517
+
518
+ The first theoretical consistency targets are CPU Tile64 and GPU Tile32. After exact equivalence is established, larger bit arrays and wider SIMD can be explored.
519
+
520
+ ## 8. Stage E — Two exact reduction families
521
+
522
+ v0.5 separates **direct 8-phase routed accumulation** from the **state POPCOUNT histogram**. Both are exact LNS8 reduction families.
523
+
524
+ ### E1. Direct 8-phase routed accumulation — preferred correctness baseline
525
+
526
+ For each product state:
527
+
528
+ ```text
529
+ p = L3
530
+ e = E4
531
+ v = (sign ? -1 : +1) × (1 << e)
532
+ A[p] += v
533
+ ```
534
+
535
+ After K products, the numerical state has been reduced to eight signed integers `A[0..7]`, independent of whether K is 128, 4096, or 5120.
536
+
537
+ POPCOUNT is not required by this path. CPU/GPU PoCs should compare phase masks, masked/grouped integer reduction, SWAR, SIMD, ballot, and related methods without falling back to K scalar dependent adds.
538
+
539
+ ### E2. State POPCOUNT histogram — dependency-breaking candidate
540
+
541
+ For state `s`:
542
+
543
+ ```text
544
+ M[s] = (state == s)
545
+ H[s] = POPCOUNT(M[s])
546
+ ```
547
+
548
+ For signed values:
549
+
550
+ ```text
551
+ Hpos[s] = POPCOUNT(M[s] & ~negative_mask)
552
+ Hneg[s] = POPCOUNT(M[s] & negative_mask)
553
+ Hsigned[s] = Hpos[s] - Hneg[s]
554
+ ```
555
+
556
+ or:
557
+
558
+ ```text
559
+ N = POPCOUNT(M[s])
560
+ Nneg = POPCOUNT(M[s] & negative_mask)
561
+ Hsigned[s] = N - 2*Nneg
562
+ ```
563
+
564
+ The histogram path can collapse identical states before dependent arithmetic, and remains the input for the pure fractional-LNS candidate.
565
+
566
+ ### E3. Exact equivalence
567
+
568
+ With the 2-D histogram:
569
+
570
+ ```text
571
+ H[p][o] = signed population of phase p, octave o
572
+ A[p] = Σ_o H[p][o] × 2^o
573
+ ```
574
+
575
+ The result must match E1 bit-exactly. The PoC treats this equivalence as a correctness invariant.
576
+
577
+ ## 9. Intermediate-state organization
578
+
579
+ If a histogram is kept explicitly, use either:
580
+
581
+ ### Full-state form
582
+
583
+ ```text
584
+ H[k] // k = 1..127, plus optional zero/sign handling
585
+ ```
586
+
587
+ ### 2-D LNS form
588
+
589
+ ```text
590
+ H[phase][octave] // 8 × 16 signed counts
591
+ ```
592
+
593
+ The direct 8-phase path does not persist this histogram. It folds immediately into:
594
+
595
+ ```text
596
+ A[phase] = Σ sign × 2^octave
597
+ ```
598
+
599
+ so only eight integer states remain.
600
+
601
+ Therefore v0.5 distinguishes:
602
+
603
+ ```text
604
+ full-state / 2-D histogram = optional intermediate state representation
605
+ A[0..7] = direct dyadic reduction state
606
+ ```
607
+
608
+ The histogram is useful when state populations themselves matter, especially for pure-LNS population folding. `A[0..7]` is the shortest path to the dyadic materializer.
609
+
610
+ ## 10. Stage F — Eight-phase finalization and pure-LNS alternative
611
+
612
+ In the direct path, `A[p]` already exists at the end of Stage E. In the histogram path, the same `A[p]` is reconstructed as follows.
613
+
614
+ For each phase `p`:
615
+
616
+ ```text
617
+ R[p] = Σ_o H[p][o] × 2^o
618
+ ```
619
+
620
+ This is integer shift/add only:
621
+
622
+ ```text
623
+ R[p] = H[p][0]
624
+ + (H[p][1] << 1)
625
+ + (H[p][2] << 2)
626
+ + ...
627
+ + (H[p][15] << 15)
628
+ ```
629
+
630
+ The 8 phase reductions are independent.
631
+
632
+ Alternative reduction styles to benchmark:
633
+
634
+ - direct shift/add
635
+ - balanced add tree
636
+ - Horner form
637
+ - SIMD: one lane per phase
638
+
639
+ ### Phase coefficients
640
+
641
+ Define:
642
+
643
+ ```text
644
+ F[p] = 2^((p - 2)/8)
645
+ ```
646
+
647
+ Then the unscaled numerical sum is:
648
+
649
+ ```text
650
+ S = 2^-9 × Σ_p R[p] × F[p]
651
+ ```
652
+
653
+ Only 8 non-power-of-two phase coefficients exist.
654
+
655
+ This eight-phase finalization is the preferred dyadic reference materializer. The direct path may reach `A[0..7]` without a POPCOUNT histogram; the histogram path reaches the same eight values via POPCOUNT + integer shift/add. In either case, only at most eight non-power-of-two phase coefficients remain at the end.
656
+
657
+ ---
658
+
659
+ ### F1. Dependency-breaking role of POPCOUNT
660
+
661
+ For a 128-element tile, a conventional accumulator exposes a long producer/consumer chain: the next arithmetic add depends on the previous partial sum. BitState first transforms the problem into independent state predicates and population counts.
662
+
663
+ ```text
664
+ many product lanes
665
+ ↓
666
+ independent state membership masks
667
+ ↓
668
+ independent POPCOUNTs / signed populations
669
+ ↓
670
+ small state set
671
+ ↓
672
+ short balanced reduction tree
673
+ ```
674
+
675
+ The important performance hypothesis is therefore **not** simply that POPCOUNT is fast. The hypothesis is that state-domain population reduction removes most of the serial arithmetic dependency before the final reduction begins.
676
+
677
+ The reference shift/add materializer above remains useful because it is algebraically simple and gives a strong correctness baseline. However, it is no longer the only post-POPCOUNT path.
678
+
679
+ ### F2. Relative fractional-LNS path — no linear materialization
680
+
681
+ For a non-zero LNS8 magnitude state `k`, define an internal logarithmic coordinate in **LNS8 code units**:
682
+
683
+ ```text
684
+ lambda = k + fractional_code_offset
685
+ 1.0 code unit = 1/8 octave
686
+ ```
687
+
688
+ The stored LNS8 value has an integer `k`, but the internal reduction is allowed to retain fractional code bits, e.g. a signed fixed-point representation of the logarithmic coordinate. This is an internal accumulator format, not a new storage format.
689
+
690
+ After signed POPCOUNT, let `C[k]` be the signed population of identical state `k`. If `C[k] != 0`, all equal-magnitude contributions in that bucket can be collapsed before any pairwise numerical reduction:
691
+
692
+ ```text
693
+ bucket_sign = sign(C[k])
694
+ bucket_lambda = k + 8*log2(abs(C[k]))
695
+ ```
696
+
697
+ `abs(C[k])` is bounded by the tile size, so for Tile128 the population term requires only a tiny `1..128 -> 8*log2(n)` LUT (or an equivalent integer implementation). This operation does **not** materialize a linear value.
698
+
699
+ For the base74 / 8-code grid, the eight phase tables `000..111` may be normalized around the eight top-octave anchors `120..127`; code 122 is the exact power-of-two anchor 64. Because an octave step is exactly +8 code units, moving a state between octave positions is an integer code offset. A convenient identity is:
700
+
701
+ ```text
702
+ k = 8*q + phase
703
+ = (120 + phase) + 8*(q - 15)
704
+ ```
705
+
706
+ Thus phase-specific entries can be brought to a common relative fractional-LNS coordinate using only known integer octave offsets plus a small fractional correction.
707
+
708
+ ### F3. LNS add/sub primitive
709
+
710
+ For two positive-magnitude fractional-LNS states with coordinates `a >= b`, let `d = a-b` in LNS8 code units.
711
+
712
+ Same sign:
713
+
714
+ ```text
715
+ out = a + kappa_plus(d)
716
+ kappa_plus(d) = 8*log2(1 + 2^(-d/8))
717
+ ```
718
+
719
+ Opposite sign, `d > 0`:
720
+
721
+ ```text
722
+ out = a + kappa_minus(d)
723
+ kappa_minus(d) = 8*log2(1 - 2^(-d/8))
724
+ ```
725
+
726
+ The output sign is the sign of the larger magnitude. Equal-magnitude opposite-sign inputs cancel to exact zero.
727
+
728
+ Runtime does not require general `log2()` or `pow()`. `kappa_plus` and `kappa_minus` are small correction LUTs indexed by a quantized difference in the chosen fractional-code format. The existing integer-code threshold adder is the coarse rounded special case; the pure-LNS path keeps additional fractional bits so that repeated additions do not round to 8-bit LNS after every step.
729
+
730
+ ### F4. Balanced reduction instead of a serial accumulator
731
+
732
+ The population buckets should be reduced as a balanced tree, not a left-to-right accumulator. For the final eight phase aggregates:
733
+
734
+ ```text
735
+ 8 states → 4 LNS add/sub operations in parallel
736
+ → 2 LNS add/sub operations in parallel
737
+ → 1 LNS add/sub operation
738
+ ```
739
+
740
+ The final eight-state dependency depth is therefore three add/sub stages. If each phase has multiple non-zero octave buckets, the same balanced primitive is used inside the phase. Sparse-state masks reduce the number of leaves further.
741
+
742
+ Finite internal LUT precision makes reduction order an error source, so PoC correctness MUST compare multiple internal fractional precisions and tree orders against an FP64 reference.
743
+
744
+ ### F5. Shared scale without leaving the log domain
745
+
746
+ For a positive shared group scale `d_scale`, applying the scale after state reduction can remain logarithmic:
747
+
748
+ ```text
749
+ lambda_scaled = lambda_sum + 8*log2(d_scale)
750
+ ```
751
+
752
+ The scale offset may be precomputed by the converter or cached once per quantization group. This replaces the final linear multiply in the pure-LNS path with another logarithmic offset ADD.
753
+
754
+ ### F6. Keep the two paths conceptually separate
755
+
756
+ There are now two post-POPCOUNT implementations to benchmark:
757
+
758
+ 1. **Dyadic reference path:** signed populations → integer shift/add → fixed-point/Q9.6 → optional LNS8 re-encode.
759
+ 2. **Pure-LNS candidate path:** signed populations → population-log LUT → fractional-LNS add/sub tree → log-scale offset → final LNS8 rounding.
760
+
761
+ The first is an important correctness and performance baseline. The second tests the stronger hypothesis that the GEMV reduction can remain in state/log coordinates without ever constructing a linear numerical accumulator.
762
+
763
+ ## 11. Fixed-point materialization and Q9.6 — reference / escape path
764
+
765
+ Do not round individual LNS states directly to Q9.6 before reduction.
766
+
767
+ Small states may individually be below one Q9.6 LSB but become significant when many occurrences are accumulated.
768
+
769
+ Use an internal fixed-point precision wider than Q9.6 for the phase-coefficient stage.
770
+
771
+ Recommended reference implementation:
772
+
773
+ ```text
774
+ R[p] : signed INT32/INT64
775
+ F_fixed[p] : high-precision fixed coefficient, e.g. Q1.15 or Q2.14
776
+ phase_product[p] : widened integer multiply
777
+ SUM_internal : INT64
778
+ ```
779
+
780
+ Then round once into Q9.6:
781
+
782
+ ```text
783
+ Q9.6_raw = rounded_shift(SUM_internal)
784
+ ```
785
+
786
+ The exact internal coefficient format is a tuning parameter and must be selected from error measurements, not guessed.
787
+
788
+ ---
789
+
790
+ ## 12. Stage G — Q9.6 → LNS8
791
+
792
+ The default re-encoding path is:
793
+
794
+ ```text
795
+ Q9.6
796
+ ↓
797
+ zero test
798
+ ↓
799
+ sign extraction
800
+ ↓
801
+ absolute value
802
+ ↓
803
+ MSB / leading-one detection
804
+ ↓
805
+ normalization
806
+ ↓
807
+ MSB + next 5 bits = 1.xxxxx (6 significant binary bits)
808
+ ↓
809
+ 32-entry LUT
810
+ ↓
811
+ LNS8 phase + possible octave carry
812
+ ↓
813
+ compose LNS8 code
814
+ ```
815
+
816
+ No general `log2()` is required.
817
+
818
+ The LUT MUST be generated by exhaustive nearest-LNS8 mapping over the Q9.6 input domain used by the implementation.
819
+
820
+ Also test:
821
+
822
+ - 5 significant bits
823
+ - 6 significant bits — default
824
+ - 7 significant bits
825
+
826
+ Measure disagreement against exact nearest-LNS8 conversion.
827
+
828
+ ---
829
+
830
+ ## 13. Q4_0 weight path
831
+
832
+ Q4_0 weight storage remains 4-bit packed.
833
+
834
+ Do not expand the model to 8-bit LNS8 weights in memory unless a benchmark shows a benefit.
835
+
836
+ Q4_0 coefficient values are:
837
+
838
+ ```text
839
+ -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7
840
+ ```
841
+
842
+ A tiny LUT can map the absolute Q4 coefficient to an LNS8 exponent offset:
843
+
844
+ ```text
845
+ |q| : 1 2 3 4 5 6 7 8
846
+ Δ : 0 8 13 16 19 21 22 24 // nearest 1/8-octave approximation
847
+ ```
848
+
849
+ Reference path:
850
+
851
+ ```text
852
+ Q4 nibble
853
+ ↓
854
+ small LUT
855
+ ↓
856
+ weight LNS offset + sign
857
+ ↓
858
+ packed exponent ADD with activation
859
+ ↓
860
+ product state
861
+ ```
862
+
863
+ Q4 block scale handling variants:
864
+
865
+ 1. FP reference: reconstruct `q × d`, then encode to LNS8.
866
+ 2. Preconverted log-scale: encode `d` once and add it as an exponent offset.
867
+ 3. Converter-generated custom scale metadata.
868
+
869
+ Measure the additional quantization error of 2 and 3 against 1.
870
+
871
+ ---
872
+
873
+ ## 14. Ternary / Bonsai2 fast path
874
+
875
+ Ternary weight groups are especially natural for BitState.
876
+
877
+ ### Rotation-aware integration point
878
+
879
+ The Bonsai2 fast path must treat the **activation domain after the matching Hadamard / sign transform** as part of its input contract. Primal-basis activations MUST NOT be fed directly into the ternary POPCOUNT path.
880
+
881
+ The token embedding lookup is also a boundary of the rotation-aware runtime contract and must preserve the required inverse transform. For multimodal input, the vision tower itself may remain unrotated, but once projected visual tokens enter the language model they must follow the same language-side basis contract.
882
+
883
+ The preferred BitState integration point is therefore one of:
884
+
885
+ 1. encode activations into LNS8 / finite-state form after the WHT / sign transform; or
886
+ 2. fuse the WHT / sign transform with state encoding to reduce intermediate floating-point materialization.
887
+
888
+ Option 2 is a performance optimization. The first correctness implementation should use option 1.
889
+
890
+ For a 128-weight group:
891
+
892
+ ```text
893
+ w_i = d × t_i
894
+ t_i ∈ {-1, 0, +1}
895
+ ```
896
+
897
+ The shared scale `d` can remain outside the 128-element population reduction:
898
+
899
+ ```text
900
+ Σ_i w_i a_i
901
+ = d × Σ_i t_i a_i
902
+ ```
903
+
904
+ Weight magnitude processing therefore collapses to Boolean selection:
905
+
906
+ ```text
907
+ W_nonzero
908
+ W_negative
909
+ ```
910
+
911
+ No per-weight magnitude multiplication is required.
912
+
913
+ ### Signed population count
914
+
915
+ For activation magnitude state `s`:
916
+
917
+ ```text
918
+ A_s = mask of activation lanes whose magnitude state == s
919
+ M = A_s & W_nonzero
920
+ PNEG = activation_sign XOR W_negative
921
+
922
+ N = POPCOUNT(M)
923
+ Nneg = POPCOUNT(M & PNEG)
924
+ Cs = N - 2*Nneg
925
+ ```
926
+
927
+ `Cs` is the signed population of activation state `s` after ternary weight selection.
928
+
929
+ The group scale is not applied until the 128-element state reduction is complete.
930
+
931
+ Two preferred flows must be tested.
932
+
933
+ Reference / validation flow:
934
+
935
+ ```text
936
+ 128 activations + ternary masks
937
+ ↓
938
+ state-set intersection
939
+ ↓
940
+ POPCOUNT signed histogram
941
+ ↓
942
+ dyadic integer shift/add
943
+ ↓
944
+ wide fixed-point / optional Q9.6
945
+ ↓
946
+ apply shared group scale d
947
+ ```
948
+
949
+ Pure-LNS flow:
950
+
951
+ ```text
952
+ 128 activations + ternary masks
953
+ ↓
954
+ state-set intersection
955
+ ↓
956
+ POPCOUNT signed histogram ← dependency-breaking reduction
957
+ ↓
958
+ non-zero population buckets
959
+ ↓
960
+ population count -> fractional-LNS offset
961
+ ↓
962
+ balanced signed LNS add/sub tree
963
+ ↓
964
+ add shared scale as log-code offset
965
+ ↓
966
+ final LNS8 rounding only at the boundary
967
+ ```
968
+
969
+ The pure-LNS path remains a stronger v0.5 hypothesis: the inner GEMV reduction does not require FP, Q9.6, or another linear accumulator. The reference path remains mandatory for differential testing.
970
+
971
+ ---
972
+
973
+ ## 15. CPU implementation plan
974
+
975
+ ### Reference 0 — scalar correctness
976
+
977
+ - explicitly decode each final product state
978
+ - execute `A[L3] += sign × (1 << E4)`
979
+ - compare against exact FP64 direct decode
980
+ - no performance goal
981
+
982
+ ### Reference 1 — scalar histogram equivalence
983
+
984
+ - build `H[phase][octave]`
985
+ - compute `A[p] = Σ_o H[p][o] << o`
986
+ - require bit-exact equality with Reference 0 `A[0..7]`
987
+
988
+ ### CPU PoC A — Tile64 bitslice direct 8-phase path
989
+
990
+ - 64 final product states
991
+ - extract / construct `L2,L1,L0,E3..E0,BSIGN` planes or equivalent masks
992
+ - 3-bit phase decoder to intermediate selector masks
993
+ - reduce `±(1<<E)` weighted contributions into eight phase accumulators
994
+ - 64 lanes → eight partial accumulators
995
+ - reproduce the Excel v0.8 128-input validation with two Tile64 blocks
996
+
997
+ ### CPU PoC B — Tile64 signed-histogram path
998
+
999
+ - full-state or phase-local membership masks
1000
+ - `Hpos/Hneg` or `N-2*Nneg`
1001
+ - POPCNT64
1002
+ - histogram → `A[0..7]`
1003
+
1004
+ ### CPU PoC B2 — pure fractional-LNS reduction
1005
+
1006
+ - use signed POPCOUNT histogram as input
1007
+ - fold non-zero populations with a tiny count-to-log LUT
1008
+ - keep configurable fractional log coordinates
1009
+ - balanced `kappa+ / kappa-` add/sub tree
1010
+ - compare total cycles and dependency depth against the direct 8-phase dyadic path
1011
+
1012
+ ### CPU PoC C — AVX2 / ordinary 64-bit CPU
1013
+
1014
+ Start with standard integer and bit primitives rather than assuming a large custom bit-array layout:
1015
+
1016
+ - packed byte ADD for LNS product generation
1017
+ - 64-bit scalar bitplanes / masks
1018
+ - AND / OR / XOR / NOT / SHIFT
1019
+ - POPCNT64 where useful
1020
+ - `pshufb` / vector mask extraction
1021
+ - eight independent integer accumulators
1022
+
1023
+ ### CPU PoC D — AVX-512 / wider bitsets
1024
+
1025
+ Evaluate only after Tile64 correctness is fixed:
1026
+
1027
+ - mask registers
1028
+ - VPOPCNT
1029
+ - wider state-set construction
1030
+ - multiple output dots in flight
1031
+ - direct 8-phase masked/grouped reduction
1032
+
1033
+ ## 16. GPU implementation plan
1034
+
1035
+ Preserve the algorithmic abstraction, not the CPU instruction sequence. The first consistency target is:
1036
+
1037
+ ```text
1038
+ Tile32 = one 32-bit warp ballot mask
1039
+ ```
1040
+
1041
+ Generate the same field masks:
1042
+
1043
+ ```text
1044
+ L0 L1 L2
1045
+ E0 E1 E2 E3
1046
+ SIGN
1047
+ ```
1048
+
1049
+ as 32-bit warp-local bitsets and use the same phase-routing / signed-count identities as the CPU Tile64 reference.
1050
+
1051
+ GPU objective:
1052
+
1053
+ ```text
1054
+ packed LNS product ADD / sign XOR
1055
+ ↓
1056
+ 32-lane final product states
1057
+ ↓
1058
+ ballot / lane predicates
1059
+ ↓
1060
+ 8-way phase routing
1061
+ ├─ direct ±(1<<E) integer reduction to A[0..7]
1062
+ └─ optional signed POPCOUNT histogram
1063
+ ↓
1064
+ 8-state finalization or pure-LNS tree
1065
+ ```
1066
+
1067
+ Candidates:
1068
+
1069
+ - warp ballot
1070
+ - `__popc` / `__popcll`
1071
+ - `__match_any_sync`
1072
+ - warp-level grouping / segmented reduction
1073
+ - shared-memory eight-accumulator layout
1074
+ - register-resident partial accumulators
1075
+
1076
+ Benchmark at least:
1077
+
1078
+ 1. scalar/lane reference `A[L3] += ±(1<<E4)`
1079
+ 2. ballot-generated phase masks + direct eight-accumulator reduction
1080
+ 3. phase-local signed histogram
1081
+ 4. full-state histogram / match-any grouping
1082
+
1083
+ Do not mechanically port CPU bit transpose. First prove Tile32 and Tile64 produce the same `A[0..7]`; only then explore larger bit arrays, wider subgroups, or custom layouts.
1084
+
1085
+ ## 17. FPGA / custom logic model
1086
+
1087
+ A streaming implementation is straightforward:
1088
+
1089
+ ```text
1090
+ packed state generation
1091
+ ↓
1092
+ state-set decoder / bit-vector formation
1093
+ ↓
1094
+ population counters
1095
+ ↓
1096
+ compressed signed state populations
1097
+ ├─ reference: shift/add → fixed-point
1098
+ └─ pure-LNS: count-log LUT → balanced kappa± tree
1099
+ ↓
1100
+ log-scale offset
1101
+ ↓
1102
+ LNS8
1103
+ ```
1104
+
1105
+ For ternary weights:
1106
+
1107
+ ```text
1108
+ activation state masks
1109
+ AND ternary non-zero/sign masks
1110
+ POPCOUNT
1111
+ ```
1112
+
1113
+ can replace most conventional multiplier logic.
1114
+
1115
+ ---
1116
+
1117
+ ## 18. Correctness test plan
1118
+
1119
+ ### Exhaustive micro-tests
1120
+
1121
+ 1. all LNS8 non-zero code pairs
1122
+ 2. all sign combinations
1123
+ 3. zero × anything
1124
+ 4. saturation boundaries
1125
+ 5. Q9.6 → LNS8 full input range
1126
+ 6. 5-bit / 6-bit / 7-bit re-log LUT comparison
1127
+ 7. exact same-sign fractional-LNS pair add against FP64
1128
+ 8. opposite-sign add/sub including exact cancellation
1129
+ 9. population-count folding: `n` identical states vs `k + 8*log2(n)`, `n=1..128`
1130
+ 10. balanced-tree pure-LNS reduction vs direct FP64 sum at multiple internal fractional precisions
1131
+
1132
+ ### Dot-product tests
1133
+
1134
+ Run random and adversarial vectors for:
1135
+
1136
+ ```text
1137
+ K = 32
1138
+ K = 64
1139
+ K = 128
1140
+ K = 256
1141
+ K = 4096
1142
+ K = 8192
1143
+ ```
1144
+
1145
+ References:
1146
+
1147
+ - FP32 original
1148
+ - ordinary Q4_0 dequantized dot
1149
+ - exact LNS8→FP64 dot
1150
+ - BitState histogram path
1151
+
1152
+ Separate error into:
1153
+
1154
+ 1. source quantization error
1155
+ 2. Q4→LNS mapping error
1156
+ 3. histogram arithmetic error
1157
+ 4. fixed-point materialization error
1158
+ 5. Q9.6 rounding error
1159
+ 6. Q9.6→LNS8 re-encoding error
1160
+
1161
+ The histogram itself should be exact for the chosen finite-state representation.
1162
+
1163
+ ---
1164
+
1165
+ ## 19. Performance test plan
1166
+
1167
+ Measure each stage separately and fused.
1168
+
1169
+ ### Micro-kernels
1170
+
1171
+ A. Q4 packed decode / LUT
1172
+
1173
+ B. packed product-state ADD
1174
+
1175
+ C. state decomposition
1176
+
1177
+ D. state-tile packing / bit transpose
1178
+
1179
+ E. state mask generation
1180
+
1181
+ F. POPCOUNT histogram
1182
+
1183
+ G. histogram shift/add reduction
1184
+
1185
+ H. eight phase coefficients / dyadic reference finalization
1186
+
1187
+ I. population-count → fractional-LNS offset LUT
1188
+
1189
+ J. `kappa+ / kappa-` fractional-LNS add/sub primitive
1190
+
1191
+ K. balanced 8→4→2→1 LNS reduction
1192
+
1193
+ L. Q9.6 → LNS8 reference boundary
1194
+
1195
+ M. fully fused dot/GEMV kernel
1196
+
1197
+ ### Metrics
1198
+
1199
+ CPU:
1200
+
1201
+ - cycles / output
1202
+ - cycles / weight
1203
+ - instructions / weight
1204
+ - uops
1205
+ - IPC
1206
+ - POPCNT throughput
1207
+ - dependent reduction depth / critical-path stages
1208
+ - fractional-LNS LUT accesses and add/sub stages
1209
+ - L1/L2 misses
1210
+ - effective GB/s
1211
+ - actual memory GB/s
1212
+
1213
+ GPU:
1214
+
1215
+ - kernel latency
1216
+ - weights/s
1217
+ - effective GB/s
1218
+ - memory throughput
1219
+ - registers/thread
1220
+ - occupancy
1221
+ - warp stall reason
1222
+ - ballot/match/popcount instruction cost
1223
+
1224
+ The main comparison is end-to-end latency, not nominal FLOPS.
1225
+
1226
+ ---
1227
+
1228
+ ## 20. Primary baselines
1229
+
1230
+ CPU:
1231
+
1232
+ - llama.cpp Q4_0 GEMV / dot path
1233
+ - scalar Q4_0 dequant + dot
1234
+ - optimized INT8 dot where available
1235
+ - T-MAC-like LUT path where practical
1236
+
1237
+ GPU:
1238
+
1239
+ - existing Q4 dequant + CUDA GEMV/GEMM
1240
+ - Tensor Core path
1241
+ - CUDA-core low-bit path
1242
+
1243
+ Primary workload:
1244
+
1245
+ ```text
1246
+ LLM decode
1247
+ batch = 1 first
1248
+ small batch second
1249
+ prefill later
1250
+ ```
1251
+
1252
+ ---
1253
+
1254
+ ## 21. Optimization priorities
1255
+
1256
+ Priority order:
1257
+
1258
+ 1. memory traffic
1259
+ 2. weight unpack / ternary mask load
1260
+ 3. reusable state-set construction
1261
+ 4. bit transpose / ballot / equivalent grouping
1262
+ 5. POPCOUNT throughput and state-domain dependency breaking
1263
+ 6. sparse histogram / non-zero bucket layout
1264
+ 7. fractional-LNS count-folding and `kappa±` LUT cost
1265
+ 8. balanced reduction critical path
1266
+ 9. dyadic shift/add reference materializer
1267
+ 10. Q9.6 / final boundary conversion
1268
+
1269
+ Do not over-optimize the final conversion before the state-domain path is competitive. The central measurement is whether POPCOUNT-based state reduction removes enough dependent arithmetic work to compensate for state-set and LUT overhead.
1270
+
1271
+ ---
1272
+
1273
+ ## 22. Patent claim review — implementation context, not a design constraint
1274
+
1275
+ The technical design above should be optimized on its own merits.
1276
+
1277
+ A review of the currently issued U.S. NVIDIA patents in the relevant family does **not** support treating generic software LNS bucket accumulation, generic phase/octave decomposition, or BitState population counting as broadly claimed subject matter.
1278
+
1279
+ ### US11886980B2
1280
+
1281
+ Independent claims 1, 16, and 19 include a combination centered on:
1282
+
1283
+ - image or sound data
1284
+ - a tensor core
1285
+ - at least one neural-network layer
1286
+ - logarithmic values expressed as sign / quotient / remainder
1287
+ - generating partial sums by sorting quotient components according to remainder components
1288
+ - a specified neural-network result
1289
+
1290
+ Dependent claims add separate accumulators, shifted binary-one quotient contributions, scaling, and re-log conversion.
1291
+
1292
+ ### US12118454B2
1293
+
1294
+ The continuation retains a similarly specific independent-claim structure:
1295
+
1296
+ - image or sound data
1297
+ - tensor core / neural-network layer
1298
+ - quotient/remainder sorting into partial sums
1299
+ - specified downstream application use
1300
+
1301
+ Dependent claims add separate accumulators and shifted-one contributions.
1302
+
1303
+ ### US12033060B2
1304
+
1305
+ The independent claims are directed much more specifically to an **asynchronous accumulator** architecture, including:
1306
+
1307
+ - a bit-selection input specifying bit position `j`
1308
+ - adding `2^j` to the asynchronous accumulator output
1309
+ - paired positive/negative asynchronous accumulators in claim 1
1310
+ - toggle flip-flop accumulator-cell implementations in other independent claims
1311
+ - quotient-derived one-hot bit selection in claim 7
1312
+ - an array of asynchronous accumulators in system claim 16
1313
+
1314
+ BitState's ordinary CPU/GPU Boolean masks + POPCOUNT histogram are not this accumulator architecture.
1315
+
1316
+ ### US12141225B2
1317
+
1318
+ Independent claim 1 requires, among other things:
1319
+
1320
+ - an array of asynchronous accumulators
1321
+ - one accumulator corresponding to each remainder value
1322
+ - incrementing or decrementing a single accumulator bit selected by the quotient
1323
+ - paired toggle flip-flops used to compute partial-sum bits
1324
+
1325
+ Again, a software state-set / POPCOUNT histogram does not use this structure.
1326
+
1327
+ ### Engineering conclusion
1328
+
1329
+ The BitState design should therefore **not** be artificially weakened merely to avoid generic operations such as:
1330
+
1331
+ - phase/octave extraction
1332
+ - state grouping
1333
+ - shift/add reconstruction
1334
+ - fixed coefficients
1335
+ - leading-one re-encoding
1336
+
1337
+ Those are useful numerical techniques and are not, by themselves, the issued independent claims summarized above.
1338
+
1339
+ The issued NVIDIA family also contains quotient/remainder bucket accumulation concepts that are mathematically close to an 8-phase decomposition. That similarity should be acknowledged when comparing implementations. The v0.5 software hypothesis, however, specifically emphasizes **wide reusable state sets + POPCOUNT as a dependency-breaking reduction**, followed optionally by a **pure fractional-LNS reduction tree instead of linear partial-sum materialization**. This is an engineering distinction, not a legal conclusion.
1340
+
1341
+ BitState's implementation focus is now:
1342
+
1343
+ ```text
1344
+ finite states
1345
+ ↓
1346
+ wide reusable state-set representation
1347
+ ↓
1348
+ parallel population counting
1349
+ ↓
1350
+ dependency-broken compressed state domain
1351
+ ├─ dyadic reference materialization
1352
+ └─ pure fractional-LNS reduction tree
1353
+ ```
1354
+
1355
+ This section is an engineering claim review, not a legal non-infringement opinion.
1356
+
1357
+ ### Pending applications
1358
+
1359
+ A pending published U.S. application should not be treated as though it were already an issued patent with a final enforceable claim set.
1360
+
1361
+ It can still be monitored for future claim changes. U.S. law also provides limited provisional-rights conditions for published applications if, among other requirements, a later-issued claim is substantially identical and the accused party had actual notice.
1362
+
1363
+ Therefore:
1364
+
1365
+ - do not present a pending application as a current issued-patent boundary;
1366
+ - do not ignore it completely if a formal FTO review is required;
1367
+ - keep pending applications in a separate monitoring section, not in the core implementation restrictions.
1368
+
1369
+ ---
1370
+
1371
+ ## 23. Coding-agent workflow
1372
+
1373
+ The coding agent should proceed experimentally rather than assuming one layout is optimal.
1374
+
1375
+ ### Phase 1 — correctness
1376
+
1377
+ Implement:
1378
+
1379
+ ```text
1380
+ exact LNS8 / FP64 reference
1381
+ scalar signed histogram
1382
+ shift/add reference materializer
1383
+ fractional-LNS pair add/sub reference
1384
+ population-count -> log-offset LUT
1385
+ balanced pure-LNS reduction
1386
+ optional Q9.6 boundary
1387
+ ```
1388
+
1389
+ Require bit-exact histogram agreement with the reference state counts. Separately measure pure-LNS internal-rounding error; do not conflate it with state-counting correctness.
1390
+
1391
+ ### Phase 2 — CPU Tile64 routing / state counting
1392
+
1393
+ Implement and compare:
1394
+
1395
+ - Tile64 direct 8-phase routed accumulator
1396
+ - Tile32 word-width-equivalent reference
1397
+ - phase-mask generation cost
1398
+ - phase-local signed histogram
1399
+ - direct full-state masks
1400
+ - SWAR transpose / mask extraction
1401
+ - AVX2 LUT/unpack
1402
+
1403
+ Report stage-by-stage cycles, separating `product ADD`, `state-set/phase-mask build`, `K→8 weighted reduction`, and `final eight-state materialization`.
1404
+
1405
+ ### Phase 3 — ternary 128-group path
1406
+
1407
+ Implement:
1408
+
1409
+ ```text
1410
+ W_nonzero
1411
+ W_negative
1412
+ activation state masks
1413
+ POPCOUNT signed histogram
1414
+ one scale application per 128-weight group
1415
+ ```
1416
+
1417
+ Compare directly against the existing Bonsai2 kernel path.
1418
+
1419
+ ### Phase 3B — dependency-breaking pure-LNS CPU path
1420
+
1421
+ Compare the existing dyadic materializer against a path that never creates a linear accumulator:
1422
+
1423
+ - signed population buckets;
1424
+ - count-to-log folding;
1425
+ - relative fractional-LNS normalization;
1426
+ - balanced `kappa±` reduction;
1427
+ - shared scale as a log offset;
1428
+ - final LNS8 rounding only at the output boundary.
1429
+
1430
+ Report both total cycles and critical-path/dependency depth.
1431
+
1432
+ ### Phase 4 — GPU
1433
+
1434
+ Implement independent CUDA approaches:
1435
+
1436
+ - Tile32 ballot phase masks
1437
+ - direct eight-way integer accumulation
1438
+ - match-any
1439
+ - shared / register histogram
1440
+
1441
+ Do not mechanically port CPU bit transpose. First verify Tile32 produces the same `A[0..7]` as the CPU Tile64 reference.
1442
+
1443
+ ### Phase 5 — fused optimization
1444
+
1445
+ Only after profiling, fuse the stages that reduce actual traffic or instruction overhead.
1446
+
1447
+ The agent must preserve a slow, clear reference implementation for differential testing.
1448
+
1449
+ ---
1450
+
1451
+ ## 24. Success criteria
1452
+
1453
+ BitState is successful if at least one practical backend shows a useful end-to-end advantage while keeping model quality acceptable.
1454
+
1455
+ The strongest initial success case would be:
1456
+
1457
+ ```text
1458
+ low-bit / ternary model
1459
+ + batch-1 decode
1460
+ + bandwidth-sensitive hardware
1461
+ + state counting cheaper than dequantize + MAC
1462
+ ```
1463
+
1464
+ A useful result does not require beating Tensor Core peak throughput in dense prefill.
1465
+
1466
+ A CPU-only win, ternary-only win, or batch-1 decode win is already technically meaningful.
1467
+
1468
+ ---
1469
+
1470
+ ## 25. One-sentence definition
1471
+
1472
+ > **BitState/HSDR moves reduction into a finite state domain to break serial arithmetic dependencies: wide state sets and POPCOUNT compress many low-bit contributions in parallel, after which only a small state set is reduced—optionally entirely in fractional-LNS form without linear materialization.**
1473
+
1474
+ ---
1475
+
1476
+ ## References used for the patent-claim review
1477
+
1478
+ - https://patents.google.com/patent/US11886980B2/en
1479
+ - https://patents.google.com/patent/US12033060B2/en
1480
+ - https://patents.google.com/patent/US12141225B2/en
1481
+ - https://patents.google.com/patent/US12118454B2/en
1482
+ - https://www.law.cornell.edu/uscode/text/35/154
Bitstate/BitState_HSDR_v0.5_ja_WHT-RMSNorm-premise.md ADDED
@@ -0,0 +1,1488 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BitState / HSDR
2
+
3
+ ## 低ビットLLM推論のための有限状態度数縮約と依存鎖切断型log-domain reduction
4
+
5
+ **実装提案 v0.5 日本語版**
6
+ **主対象:** 低ビットLLMのGEMV / 小バッチdecode
7
+ **Reference / 検証経路:** LNS8 → finite-state population reduction → wide integer/fixed-point → optional Q9.6 → LNS8
8
+ **最適化候補経路:** LNS8 → state-domain population reduction → relative fractional-LNS → balanced LNS add/sub tree → LNS8
9
+
10
+ ---
11
+
12
+ ## 1. 目的
13
+
14
+ BitStateは、低ビット推論のreduction問題を、不可避な数値加算より先に有限な **state domain** へ移す計算モデルである。
15
+
16
+ 中心となる考え方は次の一文に集約する。
17
+
18
+ > **state-domainへ移すことでreduction dependencyを切断する。** wideなstate setを形成し、等価状態を並列に数え、圧縮後に残った小さなstate集合だけを最終reductionへ渡す。
19
+
20
+ 数値実体化を後段へ遅延することは引き続き重要だが、v0.5ではそれをより強い目的――長い逐次数値加算鎖を、並列な集合演算+短い最終reductionへ置換すること――の結果として位置付ける。
21
+
22
+ 主要パイプラインは次のとおり。v0.5では、**最終product stateから8本のphase integer accumulatorへ直接縮約する経路**を、最も単純なcorrectness / implementation baselineとして明示する。full-state POPCOUNT histogramは、dependency breakingをより強く狙う別経路として残す。
23
+
24
+ ```text
25
+ packed low-bit values
26
+ ↓
27
+ packed LNS product-state ADD / sign XOR
28
+ ↓
29
+ final product state = S1 | E4 | L3
30
+ ↓
31
+ State Tile (CPU: Tile64 / GPU: Tile32など)
32
+ ↓
33
+ phase routing / state-set construction
34
+ ├───────────────────────────────────────────────┐
35
+ │ │
36
+ │ Direct 8-phase dyadic path │ Population / Pure-LNS path
37
+ ↓ ↓
38
+ A[L3] += sign × (1 << E4) signed state populations
39
+ ↓ ↓
40
+ 8 integer accumulators dyadic histogram or fractional-LNS
41
+ ↓ ↓
42
+ 8 phase coefficients + short final sum balanced κ± add/sub tree
43
+ ↓ ↓
44
+ optional Q9.6 / LNS8 boundary LNS8 boundary
45
+ ```
46
+
47
+ 最適化経路では、product生成から最終reductionまでlogarithmic coordinateのまま保持してよく、GEMV reduction内部でFP/linear materializationを必須としない。
48
+
49
+ 主な最適化対象はピークFLOPSではない。対象はdecodeパイプライン全体である。
50
+
51
+ ```text
52
+ memory load + low-bit decode + product generation + reduction + re-encoding
53
+ ```
54
+
55
+ ---
56
+
57
+ ### 1.1 前提 — 回転量子化によって狭い数値域が現実的になってきた
58
+
59
+ BitStateがLNS8やQ9.6のような比較的狭い数値表現を検討できる背景には、近年の低ビットLLMで **回転量子化(rotation-aware quantization)** が実用化され、QuaRotやSpinQuantに代表されるように、WHT / Hadamard rotationなどで外れ値やエネルギー集中を複数次元へ再分配した状態で低ビット化する設計が広がっていることがある。
60
+
61
+ この前提は、「すべてのモデルでfull-scale dynamic rangeが不要」という意味ではない。重要なのは、推論kernelがBF16/FP16の理論上の全dynamic rangeを無条件に再現するのではなく、**回転後に実際に現れるactivation / weight distributionを測定し、その必要範囲に合わせて内部表現を設計できるモデルが増えている**という点である。
62
+
63
+ Bonsai2は具体例である。公開されているBonsai2のモデル資料では、language model側のweightはblockwise Hadamard-rotated basisで保存され、runtimeがmatching transformをactivationへ適用することが明記されている。低ビット化の対象にはembedding、attention projection、MLP projection、LM headが含まれる。またMLX版では、通常loaderがactivation transformとinverse embedding lookupを行わない場合に誤った出力になることも明記されている。
64
+
65
+ したがってBonsai2では、回転は単なるquantizer前処理ではなく、**checkpointとruntimeの間で維持される数値basisの契約**になっている。token embedding / token pathもこの契約に含まれ、embedding lookup境界では対応するinverse transformが必要になる。
66
+
67
+ multimodalについては区別が必要である。公開資料上、vision tower自体は非回転・非量子化のFP16で保持される構成がある。一方、projected visual tokensがlanguage modelへ入った後は、language model側のrotation-aware activation pathを通る必要がある。��たがって **multimodal interfaceもlanguage-model側のbasis contractから独立ではない** が、vision towerそのものがHadamard回転されているという意味ではない。
68
+
69
+ Bonsai2の公開資料だけからquantization-aware trainingの全recipeを確定することはできない。ただし、配布checkpoint自体がrotated basisを前提とし、activation transformとinverse embedding lookupを必須とすることから、少なくとも **rotation-awareな低ビット最適化を前提にしたモデル / runtime設計** であることは明確である。本書ではこの傾向を、LNS8 / Q9.6の必要dynamic rangeを実測ベースで狭められる可能性を持つ重要な前提とする。
70
+
71
+ BitState実装では、各モデルについて次を必ず測定する。
72
+
73
+ - WHT / rotation前後のactivation absolute maximum
74
+ - RMSNorm前後、およびlearned gain `gamma` 適用前後のactivation absolute maximum
75
+ - RMS / percentile distribution
76
+ - layer別の必要dynamic range
77
+ - LNS8 saturation / underflow率
78
+ - Q9.6 saturation率
79
+ - rotationを含むend-to-end quality差
80
+
81
+ つまり、**回転によってfull-scale保持を機械的に捨てるのではなく、回転後の実測分布を根拠に必要範囲だけを保持する**。
82
+
83
+ 参考:
84
+
85
+ - https://arxiv.org/abs/2404.00456 (QuaRot)
86
+ - https://arxiv.org/abs/2405.16406 (SpinQuant)
87
+ - https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-gguf
88
+ - https://huggingface.co/prism-ml/Ternary-Bonsai-2-27B-mlx-2bit
89
+ - https://github.com/PrismML-Eng/Bonsai-demo
90
+
91
+ ### 1.2 前提 — RMSNormの普及により、GEMM直前activationの絶対レンジも管理しやすい
92
+
93
+ 回転量子化とは別に、近年のdecoder-only LLMでは **RMSNorm** が広く採用されている。Qwen3.5、Gemma 3、Llama 4、Mistral系など、主要な新しいモデルfamilyでRMS normalization layerが使われている。したがってBitStateでは、WHT / rotationだけでなく、**RMSNorm後のactivation domainをLNS8へ入れるintegration point**も重要な対象とする。
94
+
95
+ RMSNormはoutlierを個別にclipする処理ではない。tokenごとのhidden vector `x` 全体を、そのroot-mean-squareで共通にrescaleする。learned gainを掛ける前の正規化値を `u` とすると、概念的には:
96
+
97
+ ```text
98
+ u_i = x_i / sqrt(mean_j(x_j^2) + eps)
99
+ ```
100
+
101
+ であり、hidden dimensionを `d` とすれば:
102
+
103
+ ```text
104
+ sum_i(u_i^2) <= d
105
+ ```
106
+
107
+ したがって各要素には次の理論上限がある:
108
+
109
+ ```text
110
+ |u_i| <= sqrt(d)
111
+ ```
112
+
113
+ これは「channel-wise outlierが消える」という意味ではない。1つのchannelへエネルギーが極端に集中することは残り得るが、**RMSNorm前に存在した数万~数十万という絶対値そのものは、正規化後にはhidden dimension由来の有限範囲へ押し戻される**。WHT / Hadamard rotationはその次に、残ったchannel-wise concentrationを複数次元へ再分配できる。このためRMSNormとWHTは競合する処理ではなく、BitStateから見ると互いに補完的である。
114
+
115
+ 現在のLNS8基準format `S1E4L3 / base74` は、最大magnitudeが:
116
+
117
+ ```text
118
+ x_max = 2^((127 - 74)/8) ~= 98.7015
119
+ ```
120
+
121
+ である。したがって `gamma` 適用前のRMS-normalized activationを直接LNS8へencodeする場合、理論上はおおよそ:
122
+
123
+ ```text
124
+ d <= x_max^2 ~= 9741.98
125
+ ```
126
+
127
+ まで、RMSNorm由来の最悪1-axis concentrationをsaturationなしで覆える。代表例:
128
+
129
+ ```text
130
+ hidden 5120 -> sqrt(d) ~= 71.55
131
+ hidden 8192 -> sqrt(d) ~= 90.51
132
+ hidden 9216 -> sqrt(d) = 96.00
133
+ hidden 12288 -> sqrt(d) ~= 110.85
134
+ ```
135
+
136
+ したがって5120~9216級では、current base74 / S1E4L3の約±98.7 rangeが **RMSNorm直後の理論上限そのものを包含できる**。12288級では理論最悪値だけが上限を約12%超えるため、次のいずれかを実測で選ぶ:
137
+
138
+ 1. 稀なoverflowだけをLNS8最大値へsaturate / clampする;
139
+ 2. tensor / model metadataのsystem exponent offsetをわずかに上側へずらす(例: `code 72 = 1.0` なら最大値は約117.38);
140
+ 3. 実測上wide rangeが本当に必要なtensorだけ、将来のwide-range profile / escape pathを使う。
141
+
142
+ ただし実際のRMSNormは通常、正規化後にlearned gain `gamma_i` を掛けるため、最終出力は厳密には `sqrt(d)` を超え得る。したがってBitStateは、**RMS normalization後・`gamma` 適用前をencode点にする、`gamma` を後続linear weightへfoldできる箇所ではfoldする、または`gamma`適用後の実測分布でrangeを決める**、のいずれかをmodel/backendごとに選択する。
143
+
144
+ この前提の目的は、「RMSNormがあるからfull-scale supportは不要」と断定することではない。**WHT / rotation済み、またはRMSNormによって絶対rangeが管理されたactivation pathでは、稀な理論外れ値のために全要素のLNS8 precisionを犠牲にするより、S1E4L3の細かい8-state-per-octave gridを維持できる可能性が高い**、という検証仮説を明示することである。
145
+
146
+ 追加参考:
147
+
148
+ - https://arxiv.org/abs/1910.07467 (RMSNorm)
149
+ - https://huggingface.co/docs/transformers/main/en/model_doc/qwen3_5
150
+ - https://huggingface.co/docs/transformers/model_doc/gemma3
151
+ - https://huggingface.co/docs/transformers/model_doc/llama4
152
+ - https://huggingface.co/docs/transformers/model_doc/mistral
153
+
154
+ ---
155
+
156
+ ## 2. BitState と HSDR
157
+
158
+ ### BitState
159
+
160
+ BitStateは、低ビット数値表現を、小さな有限状態空間として扱い、Boolean演算と整数演算で操作する。
161
+
162
+ 代表的なprimitive:
163
+
164
+ - packed integer ADD
165
+ - XOR
166
+ - AND / OR / NOT
167
+ - SHIFT
168
+ - bit transpose / ballot / mask generation
169
+ - POPCOUNT
170
+ - small integer reduction
171
+ - small LUT
172
+
173
+ ### HSDR — Hierarchical State-Domain Reduction
174
+
175
+ HSDRは、多数の要素単位演算を段階的に小さなstate domainへ縮約し、**domainを移すこと自体を使ってserial reduction dependencyを切断する**。
176
+
177
+ 典型的な階層:
178
+
179
+ ```text
180
+ individual elements
181
+ ↓
182
+ finite-state IDs
183
+ ↓
184
+ wide state sets / masks
185
+ ↓
186
+ parallel population counts
187
+ ↓
188
+ small state-domain aggregates
189
+ ├─ reference: integer/fixed-point realization
190
+ └─ candidate: fractional-LNS reduction tree
191
+ ```
192
+
193
+ 本質は次のとおり。
194
+
195
+ > **state-domain reductionはdependency-breaking transformである。** 等価状態を先に並列集計し、圧縮後の小さなstate集合だけを依存関係を持つ算術reductionへ入れる。
196
+
197
+ 目的は単に「値の個数を減らす」ことではない。scalar bit operation、SIMD mask、GPU ballot、hardware population counterへ自然に写像できる、浅いdependency graphを作ることである。
198
+
199
+ ---
200
+
201
+ ## 3. LNS8 基準フォーマット
202
+
203
+ 現在のLNS8 magnitude gridは次式で定義する。
204
+
205
+ ```text
206
+ |x| = 2^((k - 74) / 8)
207
+ ```
208
+
209
+ ここで:
210
+
211
+ - `k = 1..127` : 非zero magnitude code
212
+ - `k = 0` : exact zero
213
+ - signは別管理
214
+ - 8 code進むと正確に×2
215
+ - code 74 = 1.0
216
+
217
+ 非zeroの7bit magnitude codeについて:
218
+
219
+ ```text
220
+ phase = k & 0x07 // 下位3bit、intra-octave index 0..7
221
+ octave = k >> 3 // 上位4bit、coarse exponent / octave index 0..15
222
+ ```
223
+
224
+ 本書の用語:
225
+
226
+ - **phase** = 下位3bit = octave内位置
227
+ - **octave** = 上位4bit = coarse exponent
228
+ - **state** = 完全な有限状態code。必要に応じてsignを含む
229
+
230
+ 74 = 9×8 + 2 なので:
231
+
232
+ ```text
233
+ 2^((k - 74)/8)
234
+ = 2^(octave - 9) × 2^((phase - 2)/8)
235
+ ```
236
+
237
+ この因数分解はpopulation counting後の数値復元で有効である。ただしhistogram生成方法を制約するものではない。
238
+
239
+ ### 3.1 S1E4L3としての論理分解 — exponent × fractional-log field
240
+
241
+ 同じ8bit表現を、flatな7bit magnitude codeとしてだけでなく、FPに似たfield構造として読むこともできる。非zero stateについての論理分解は次のとおり。
242
+
243
+ ```text
244
+ bit 7 bits 6..3 bits 2..0
245
+ +------+-------------------+-------------------+
246
+ | S1 | E4 | L3 |
247
+ +------+-------------------+-------------------+
248
+ | sign | octave / exponent | fractional-log |
249
+ | | | phase |
250
+ +------+-------------------+-------------------+
251
+ ```
252
+
253
+ ここで `E = octave`、`L = phase`、符号を `s ∈ {-1,+1}` とすると、非zero値は
254
+
255
+ ```text
256
+ x = s × 2^(E - 9) × 2^((L - 2)/8)
257
+ = s × 2^(E - 9 + (L - 2)/8)
258
+ ```
259
+
260
+ と書ける。したがって各fieldの役割は次のように整理できる。
261
+
262
+ - **S1** — sign。population reductionではpositive / negative populationへ写像できる。
263
+ - **E4** — 4bitのcoarse binary exponent / octave。`E + 1` は厳密に `×2` であり、materializationではSHIFT / dyadic scale軸として扱える。
264
+ - **L3** — 3bitのfractional logarithmic field。1 octave内を8分割し、`2^((L-2)/8)` の8状態を表す。
265
+
266
+ 直感的にはL3を **logarithmic mantissa** と呼ぶことができるが、IEEE floating-pointのlinear mantissa / significandではない。数学的には **fractional exponent** である。本書では誤解を避けるため、正式名称を **fractional-log field** または既存用語の **phase** とする。
267
+
268
+ 通常のE4M3風の表現と比較すると構造差は次のようになる。
269
+
270
+ ```text
271
+ ordinary FP-like E4M3:
272
+ x ~ sign × 2^E × (linear significand from M3)
273
+
274
+ BitState LNS8 logical S1E4L3:
275
+ x = sign × 2^E × 2^(fractional-log field)
276
+ (with the base74 bias terms shown above)
277
+ ```
278
+
279
+ つまり見た目は `S1 | E4 | L3` だが、下位fieldもlogarithmic coordinateの一部である。このためLNS乗算ではlinear mantissa multiplierを必要とせず、full codeでは従来どおり
280
+
281
+ ```text
282
+ k_product = k_a + k_b - 74
283
+ s_product = s_a XOR s_b
284
+ ```
285
+
286
+ で処理できる。field分解で見ると、`L3` の加算結果から生じるcarry / borrowがそのまま `E4` へ移る。非zero inputについて、
287
+
288
+ ```text
289
+ t = L_a + L_b - 2
290
+ L_p = t mod 8
291
+ E_p = E_a + E_b - 9 + floor(t / 8)
292
+ ```
293
+
294
+ であり、これは `k_product = k_a + k_b - 74` と同じ関係である。高速実装では最終storage rangeへ先にsaturateせず、より広いsigned intermediateを使う。
295
+
296
+ ### 3.2 2-D state-spaceとしての見方
297
+
298
+ HSDRの観点では、非zero magnitude stateをflatな127状態としてだけでなく、概念的に次の直積として扱える。
299
+
300
+ ```text
301
+ vertical axis : E4 = 16 coarse exponent / power-of-two scale positions
302
+ horizontal axis : L3 = 8 intra-octave logarithmic positions
303
+ sign axis : S1 = positive / negative
304
+ ```
305
+
306
+ 直感上は、**横方向を `S1 + L3` の4bit signed intra-octave LNS slice、縦方向をE4M0-likeなpure power-of-two scale axis** と見ることができる。ただしこれはalgorithmic / visualization上の分解であり、IEEE E4M0や独立した4bit LNS storage formatを意味しない。`k=0` はexact zeroとして別扱いである。
307
+
308
+ この見方では、後段のHSDR操作との対応も明確になる。
309
+
310
+ ```text
311
+ S1 -> sign mask / signed population
312
+ L3 -> 8 phase buckets / 8 fixed fractional-log coefficients
313
+ E4 -> exact power-of-two position / integer SHIFT axis
314
+ ```
315
+
316
+ したがって `H[phase][octave]` は単なる便宜的な2-D histogramではなく、S1E4L3論理fieldをそのままstate-domainへ展開した表現として理解できる。BitStateはこの直積構造を利用し、sign、fractional-log phase、power-of-two scaleを別々のcheap primitiveへ写像してから縮約する。
317
+
318
+ ---
319
+
320
+ ## 4. Stage A — Product-state generation
321
+
322
+ LNSの乗算は指数加算になる。
323
+
324
+ 概念的には:
325
+
326
+ ```text
327
+ product_exp = weight_exp + activation_exp + bias_adjust
328
+ product_sign = weight_sign XOR activation_sign
329
+ ```
330
+
331
+ 高速pathでは、dynamic rangeが許す限りpacked integer arithmeticを使う。
332
+
333
+ 候補:
334
+
335
+ - packed INT8 ADD
336
+ - packed INT16 reference path
337
+ - SWAR lane arithmetic
338
+ - SIMD byte arithmetic
339
+ - SIMT lane arithmetic
340
+
341
+ ### Product-state completion rule
342
+
343
+ `L3` / `E4`へ分解してphase routingまたはbitsliceを開始する前に、**その経路で最終product stateに含める全log-domain offsetを加算し終える**。weight exponent、preconverted scale offset、bias adjustmentなどがproduct codeを変えるなら、それらを先に反映する。
344
+
345
+ ```text
346
+ activation LNS code
347
+ + weight / scale / bias log offsets
348
+ ↓
349
+ final product magnitude code
350
+ ↓
351
+ L3 / E4 decomposition, bit transpose, phase routing
352
+ ```
353
+
354
+ shared group scaleを数式上 `d × Σ(...)` として外側にfactorすること自体は可能だが、それは**別のfactorized variant**として扱う。scaleをproduct codeへ含める経路と、reduction後へfactorする経路を同じstate routing説明の中で混同しない。
355
+
356
+ ### Range rule
357
+
358
+ 最初のcorrectness実装では、最終LNS codeより広い中間型を必ず使用する。
359
+
360
+ 測定項目:
361
+
362
+ - minimum product exponent
363
+ - maximum product exponent
364
+ - overflow frequency
365
+ - underflow frequency
366
+
367
+ 観測範囲を理解してから、packed INT8のsaturation / wrapping ruleを決める。
368
+
369
+ ---
370
+
371
+ ## 5. Stage B — State Tile
372
+
373
+ 複数のproduct stateを1つのtileとして処理する。
374
+
375
+ 初期候補:
376
+
377
+ ```text
378
+ Tile32
379
+ Tile64
380
+ Tile128
381
+ ```
382
+
383
+ CPU reference target:
384
+
385
+ - Tile64。state-membership maskを64bit integer 1本へ自然に対応させられるため。
386
+
387
+ Bonsai2 reference target:
388
+
389
+ - Tile128。ternary weight scale groupが128 weight単位だからである。
390
+
391
+ State Tileは論理的なグルーピングである。backendはregister、vector lane、warp lane、bitsetなど、任意の物理表現を選んでよい。
392
+
393
+ ---
394
+
395
+ ## 6. Stage C — S1E4L3 decomposition and direct 8-phase reduction
396
+
397
+ final product stateについて:
398
+
399
+ ```text
400
+ phase = state & 0x07 // L3, 0..7
401
+ octave = state >> 3 // E4, 0..15
402
+ sign = product sign // S1
403
+ ```
404
+
405
+ v0.5のcorrectness baselineでは、full histogramを必須とせず、各productを次の**8本のsigned integer accumulator**へ直接縮約できる。
406
+
407
+ ```text
408
+ payload = sign × (1 << octave)
409
+ A[phase] += payload
410
+ ```
411
+
412
+ すなわち:
413
+
414
+ ```text
415
+ A[p] = Σ_{i: phase_i=p} sign_i × 2^(octave_i)
416
+ ```
417
+
418
+ 最終値は:
419
+
420
+ ```text
421
+ F[p] = 2^((p - 2)/8)
422
+ S = 2^-9 × Σ_{p=0..7} A[p] × F[p]
423
+ ```
424
+
425
+ である。この変形は量子化済みLNS8 stateに対して代数的に厳密であり、`H[phase][octave]`を先に作ってから `Σ H[p][o] << o` する方式と同じ結果になる。
426
+
427
+ ### C1. GEMM / GEMVでの意味
428
+
429
+ 1つのoutput dotで `K=4096` や `K=5120` のproduct stateがある場合、全K項を最終的に **8本のA[0..7]** へ入れる。8本まで縮約できれば、その後に実数係数 `F[p]` を適用して8項だけを加算すればよく、支配的な課題は最終materializationではなく **K項 → 8本へのrouting / weighted reduction** である。
430
+
431
+ ### C2. Excel correctness reference
432
+
433
+ 本書のこの経路は、`LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx` の `Inputs_128` / `BitSlice128_一致確���` 相当の手順をcorrectness referenceとする。
434
+
435
+ 同Excelでは128項を `Tile64 × 2` として扱い、各項の:
436
+
437
+ ```text
438
+ S1 | E4 | L3
439
+ ```
440
+
441
+ から `±(1 << E4)` を作り、`L3`で8本のaccumulatorへrouteする。8本の整数accumulatorから復元した値がdirect decodeと一致することを確認する。
442
+
443
+ ### C3. Histogram / POPCOUNT path remains valid
444
+
445
+ full-state histogramは不要ではなく、別の正確な実装familyとして残す。
446
+
447
+ ```text
448
+ H[phase][octave] // signed counts
449
+ A[phase] = Σ_o H[phase][o] × 2^o
450
+ ```
451
+
452
+ direct 8-phase pathはbucket作成とshift/addを融合した形、histogram pathはstate populationを明示的に保持する形である。前者は単純なreference / routed-reduction候補、後者はPOPCOUNTによるdependency breakingやpure-LNS foldingの候補として比較する。
453
+
454
+ ## 7. Stage D — Bit transpose / backend-equivalent routing-mask construction
455
+
456
+ Tile64のCPU bitsliceでは、final product stateのfieldを次の64bit bitplaneとして表せる。
457
+
458
+ ```text
459
+ L0 L1 L2 // L3の3 bitplane
460
+ E0 E1 E2 E3 // E4の4 bitplane
461
+ BSIGN // sign plane, 1 = negative
462
+ ```
463
+
464
+ 各bit位置は元の同じlaneを保持する。例えばbit 37は、すべてのbitplaneで元のlane 37を指す。
465
+
466
+ ### D1. Phase mask is an intermediate selector
467
+
468
+ `L2,L1,L0`から、phase `p=0..7`に属するlaneだけが1になる64bit mask `M[p]` をBoolean演算で生成できる。
469
+
470
+ 例:
471
+
472
+ ```text
473
+ M[101] = L2 & ~L1 & L0
474
+ ```
475
+
476
+ このmaskは**最終値でもpopulationそのものでもなく、中間routing / selector情報**である。必要ならその場で生成し、集計後に捨ててよい。8本を永続保存する必要はない。
477
+
478
+ `L3`の3bit decoderは、中間2bit groupを共有するtreeとして生成できるため、8個の完全独立比較を行う必要はない。
479
+
480
+ ### D2. Sign handling in bit form
481
+
482
+ bitset自体は `+1 / -1` を直接保持できないため、signed populationが必要な場合はsign planeを明示的に使う。
483
+
484
+ ```text
485
+ Hpos = POPCOUNT(M & ~BSIGN)
486
+ Hneg = POPCOUNT(M & BSIGN)
487
+ Hsigned = Hpos - Hneg
488
+ ```
489
+
490
+ または同値な:
491
+
492
+ ```text
493
+ N = POPCOUNT(M)
494
+ Nneg = POPCOUNT(M & BSIGN)
495
+ Hsigned = N - 2*Nneg
496
+ ```
497
+
498
+ この式はExcelの `(+ POPCOUNT) - (- POPCOUNT)` と同じ母集団を数える。XORを用いる代数的恒等式も存在するが、bucket外のnegative laneを一時的に数えるため、reference実装では上のtable-local式を優先する。
499
+
500
+ ### D3. Important: phase POPCOUNT alone is not enough for the direct 8-accumulator path
501
+
502
+ `POPCOUNT(M[p])`だけではE4の重み `2^E` を失う。同じphase内でも `E=3` と `E=11` は寄与が異なるため、direct pathでは最終的に:
503
+
504
+ ```text
505
+ A[p] += ±(1 << E)
506
+ ```
507
+
508
+ というweighted reductionが必要である。bit transpose / phase maskはそのroutingをwideに処理するための中間表現であり、POPCOUNTの使用自体は必須ではない。
509
+
510
+ backend equivalent:
511
+
512
+ - scalar 64-bit CPU: `uint64_t` bitplanes / masks
513
+ - AVX2 / AVX-512: vector mask extraction, mask logic, optional vector/scalar popcount
514
+ - ARM NEON/SVE: predicate / bit operation / count
515
+ - CUDA: 32-lane ballot / lane predicate / subgroup reduction
516
+ - FPGA/ASIC: 3-to-8 decoder + sign + shifted payload routing
517
+
518
+ CPUではTile64、GPUではTile32を最初の理論整合性ターゲットとする。同じBoolean式をword幅だけ変えて検証し、その後に巨大bit配列やよりwideなSIMDへ拡張する。
519
+
520
+ ## 8. Stage E — Two exact reduction families
521
+
522
+ v0.5では、**direct 8-phase routed accumulation** と **state POPCOUNT histogram** を、どちらもexactなLNS8 reduction familyとして明示的に分ける。
523
+
524
+ ### E1. Direct 8-phase routed accumulation — preferred correctness baseline
525
+
526
+ 各product stateについて:
527
+
528
+ ```text
529
+ p = L3
530
+ e = E4
531
+ v = (sign ? -1 : +1) × (1 << e)
532
+ A[p] += v
533
+ ```
534
+
535
+ K個のproductを処理し終わった時点で残る数値状態は8本のsigned integer `A[0..7]` だけである。K=4096 / 5120でも最終state数は8本で変わらない。
536
+
537
+ この経路はPOPCOUNTを必須としない。CPU/GPUでの性能PoCでは、phase mask、masked add、grouped reduction、SWAR、SIMD、ballotなどを比較し、K項を逐次scalar addへ戻さない実装を探す。
538
+
539
+ ### E2. State POPCOUNT histogram — dependency-breaking candidate
540
+
541
+ state `s`についてmembership maskを作る。
542
+
543
+ ```text
544
+ M[s] = (state == s)
545
+ H[s] = POPCOUNT(M[s])
546
+ ```
547
+
548
+ signed valueなら:
549
+
550
+ ```text
551
+ Hpos[s] = POPCOUNT(M[s] & ~negative_mask)
552
+ Hneg[s] = POPCOUNT(M[s] & negative_mask)
553
+ Hsigned[s] = Hpos[s] - Hneg[s]
554
+ ```
555
+
556
+ または:
557
+
558
+ ```text
559
+ N = POPCOUNT(M[s])
560
+ Nneg = POPCOUNT(M[s] & negative_mask)
561
+ Hsigned[s] = N - 2*Nneg
562
+ ```
563
+
564
+ histogram pathではfloating-point valueを作らず、同一stateをpopulationへ先に畳み込める。これはpure fractional-LNS pathや、state cardinalityが小さいworkloadでのdependency breakingに有利な可能性がある。
565
+
566
+ ### E3. Exact equivalence
567
+
568
+ 2-D histogramを使う場合:
569
+
570
+ ```text
571
+ H[p][o] = signed population of phase p, octave o
572
+ A[p] = Σ_o H[p][o] × 2^o
573
+ ```
574
+
575
+ であり、E1のdirect accumulationと同じ `A[p]` になる。PoCではこの一致をbit-exactに要求する。
576
+
577
+ ## 9. Intermediate-state organization
578
+
579
+ full histogramを保持する場合は次の2表現がある。
580
+
581
+ ### Full-state form
582
+
583
+ ```text
584
+ H[k] // k = 1..127、zero/signは必要に応じて別処理
585
+ ```
586
+
587
+ ### 2-D LNS form
588
+
589
+ ```text
590
+ H[phase][octave] // 8 × 16 signed counts
591
+ ```
592
+
593
+ 一方、direct 8-phase pathではhistogramを永続化せず、直ちに:
594
+
595
+ ```text
596
+ A[phase] = Σ sign × 2^octave
597
+ ```
598
+
599
+ へfoldするため、最終的なinteger stateは8本だけである。
600
+
601
+ したがってv0.5では:
602
+
603
+ ```text
604
+ full-state / 2-D histogram = optional intermediate representation
605
+ A[0..7] = dyadic direct-reduction state
606
+ ```
607
+
608
+ と区別する。histogramはstate populationを保持するのでpure-LNS foldingに有用、A[8]は最終dyadic materializationへ最短で到達する。
609
+
610
+ ## 10. Stage F — 8-phase finalization and pure-LNS alternative
611
+
612
+ direct pathではStage Eの時点で `A[p]` が得られている。histogram pathでは次式で同じ `A[p]` を生成する。
613
+
614
+ 各phase `p`について:
615
+
616
+ ```text
617
+ R[p] = Σ_o H[p][o] × 2^o
618
+ ```
619
+
620
+ これはinteger shift/addだけで実装できる。
621
+
622
+ ```text
623
+ R[p] = H[p][0]
624
+ + (H[p][1] << 1)
625
+ + (H[p][2] << 2)
626
+ + ...
627
+ + (H[p][15] << 15)
628
+ ```
629
+
630
+ 8個のphase reductionは互いに独立である。
631
+
632
+ benchmark候補:
633
+
634
+ - direct shift/add
635
+ - balanced add tree
636
+ - Horner form
637
+ - SIMD: one lane per phase
638
+
639
+ ### Phase coefficients
640
+
641
+ ```text
642
+ F[p] = 2^((p - 2)/8)
643
+ ```
644
+
645
+ と定義する。
646
+
647
+ scale前の数値和は:
648
+
649
+ ```text
650
+ S = 2^-9 × Σ_p R[p] × F[p]
651
+ ```
652
+
653
+ 2の冪でないphase係数は8種類しかない。
654
+
655
+ この8-phase finalizationをdyadic referenceの優先方式とする。direct pathではPOPCOUNT histogramを経由せず、K項から直接 `A[0..7]` を得てもよい。histogram pathではPOPCOUNT + integer shift/addを経由して同じ8本へ到達する。いずれも最終的な非2冪係数処理は最大8項だけである。
656
+
657
+ ---
658
+
659
+ ### F1. POPCOUNTの役割 — reduction dependencyの切断
660
+
661
+ 128要素tileを通常のaccumulatorで処理すると、次の加算は前のpartial sumに依存し、長いproducer/consumer chainができる。BitStateはその前に問題をstate predicateとpopulation countへ変換する。
662
+
663
+ ```text
664
+ many product lanes
665
+ ↓
666
+ independent state membership masks
667
+ ↓
668
+ independent POPCOUNTs / signed populations
669
+ ↓
670
+ small state set
671
+ ↓
672
+ short balanced reduction tree
673
+ ```
674
+
675
+ したがって性能仮説は単なる「POPCOUNTが速い」ではない。**state-domain population reductionによって、最終reduction開始前に逐次数値依存の大部分を消せるか**が中心である。
676
+
677
+ 上記のshift/add materializerは代数的に分かりやすく、correctness baselineとして引き続き重要である。ただしv0.5では、dyadic direct pathに加えてpopulation / pure-LNS経路も比較する。
678
+
679
+ ### F2. Relative fractional-LNS path — 一度もlinear値を作らない経路
680
+
681
+ 非zero LNS8 magnitude state `k`について、内部log座標を **LNS8 code単位** で持つ。
682
+
683
+ ```text
684
+ lambda = k + fractional_code_offset
685
+ 1 code unit = 1/8 octave
686
+ ```
687
+
688
+ 保存時のLNS8は整数codeだが、reduction内部ではcodeの小数bitを許容する。これは内部accumulator表現であり、新しいstorage formatを要求しない。
689
+
690
+ signed POPCOUNT後、同一state `k`のsigned populationを `C[k]` とする。`C[k] != 0` なら、同じ大きさの寄与はpairwise加算を始める前に一つのfractional-LNS stateへ畳み込める。
691
+
692
+ ```text
693
+ bucket_sign = sign(C[k])
694
+ bucket_lambda = k + 8*log2(abs(C[k]))
695
+ ```
696
+
697
+ Tile128なら `abs(C[k])` は最大128なので、population項は `1..128 -> 8*log2(n)` のtiny LUT(または等価なinteger実装)だけでよい。この処理はlinear値を生成しない。
698
+
699
+ base74 / 8-code gridでは8つのphase table `000..111` を上位octaveの8 anchor `120..127` 周辺へ正規化できる。code122はexactな64 anchorである。1 octaveが厳密に+8 codeなので、octave位置の移動は整数code offsetで表せる。
700
+
701
+ ```text
702
+ k = 8*q + phase
703
+ = (120 + phase) + 8*(q - 15)
704
+ ```
705
+
706
+ したがって各tableのstateは、既知の整数octave offsetと小さなfractional correctionだけで同一のrelative fractional-LNS座標系へ持ってこられる。
707
+
708
+ ### F3. LNS add/sub primitive
709
+
710
+ 二つのfractional-LNS magnitude coordinateを `a >= b`、差を `d=a-b`(LNS8 code単位)とする。
711
+
712
+ 同符号:
713
+
714
+ ```text
715
+ out = a + kappa_plus(d)
716
+ kappa_plus(d) = 8*log2(1 + 2^(-d/8))
717
+ ```
718
+
719
+ 異符号、`d > 0`:
720
+
721
+ ```text
722
+ out = a + kappa_minus(d)
723
+ kappa_minus(d) = 8*log2(1 - 2^(-d/8))
724
+ ```
725
+
726
+ 出力符号は大きいmagnitude側。magnitudeが同じ異符号入力はexact zeroへ相殺する。
727
+
728
+ runtimeで一般の`log2()`や`pow()`を呼ぶ必要はない。`kappa_plus` / `kappa_minus`は、選択したfractional-code precisionで量子化した差をindexにするsmall LUTとして実装する。既存のinteger-code threshold加算は、この式を整数LNS8 codeへ直ちに丸める粗粒度special caseである。pure-LNS pathでは中間roundingを避けるためfractional code bitsを保持する。
729
+
730
+ ### F4. Serial accumulatorではなくbalanced tree
731
+
732
+ population bucketは左から逐次accumulateせずbalanced treeで縮約する。最後の8 phase aggregateなら:
733
+
734
+ ```text
735
+ 8 states → 4 LNS add/sub を並列
736
+ → 2 LNS add/sub を並列
737
+ → 1 LNS add/sub
738
+ ```
739
+
740
+ 最終8-state reductionのdependency depthは3段。各phase内に複数octave bucketが残る場合も同じprimitiveをbalanced treeで使う。sparse-state maskならleaf数そのものを減らせる。
741
+
742
+ 内部LUT precisionが有限ならreduction orderも誤差要因になるため、PoCでは複数のfractional precisionとtree orderをFP64 referenceに対して測定する。
743
+
744
+ ### F5. shared scaleもlog-domainから出さない
745
+
746
+ 正のshared group scale `d_scale`は、state reduction後にlogarithmic offsetとして適用できる。
747
+
748
+ ```text
749
+ lambda_scaled = lambda_sum + 8*log2(d_scale)
750
+ ```
751
+
752
+ scale offsetはconverterで事前計算するか、quantization groupごとに1回cacheできる。pure-LNS pathでは最後のlinear multiplyもlog-code ADDへ置換できる。
753
+
754
+ ### F6. 二つの経路を混同しない
755
+
756
+ POPCOUNT後は少なくとも次を別々にbenchmarkする。
757
+
758
+ 1. **Dyadic reference path:** signed population → integer shift/add → fixed-point/Q9.6 → optional LNS8 re-encode。
759
+ 2. **Pure-LNS candidate path:** signed population → population-log LUT → fractional-LNS add/sub tree → log-scale offset → final LNS8 rounding。
760
+
761
+ 前者はcorrectness/performance baselineとして重要。後者は「GEMV reduction全体をlinear accumulator無しでstate/log座標のまま完結できるか」を検証する、より強い仮説である。
762
+
763
+ ## 11. Fixed-point materialization and Q9.6 — reference / escape path
764
+
765
+ 個々のLNS stateをreduction前に直接Q9.6へ丸めてはならない。
766
+
767
+ 単体ではQ9.6の1 LSB未満の小さなstateでも、多数集まれば有意になるためである。
768
+
769
+ phase coefficient stageではQ9.6より広い内部fixed-point precisionを使う。
770
+
771
+ reference実装候補:
772
+
773
+ ```text
774
+ R[p] : signed INT32/INT64
775
+ F_fixed[p] : high-precision fixed coefficient, 例 Q1.15 / Q2.14
776
+ phase_product[p] : widened integer multiply
777
+ SUM_internal : INT64
778
+ ```
779
+
780
+ 最後に1回だけQ9.6へ丸める。
781
+
782
+ ```text
783
+ Q9.6_raw = rounded_shift(SUM_internal)
784
+ ```
785
+
786
+ 内部係数formatは推測で決めず、error measurementから選定する。
787
+
788
+ ---
789
+
790
+ ## 12. Stage G — Q9.6 → LNS8
791
+
792
+ 標準re-encoding path:
793
+
794
+ ```text
795
+ Q9.6
796
+ ↓
797
+ zero test
798
+ ↓
799
+ sign extraction
800
+ ↓
801
+ absolute value
802
+ ↓
803
+ MSB / leading-one detection
804
+ ↓
805
+ normalization
806
+ ↓
807
+ MSB + next 5 bits = 1.xxxxx (6 significant binary bits)
808
+ ↓
809
+ 32-entry LUT
810
+ ↓
811
+ LNS8 phase + possible octave carry
812
+ ↓
813
+ compose LNS8 code
814
+ ```
815
+
816
+ 汎用`log2()`は不要。
817
+
818
+ LUTは、実装で使用するQ9.6 input domain全体について、nearest-LNS8 mappingを総当たりで生成する。
819
+
820
+ 以下も比較する。
821
+
822
+ - 5 significant bits
823
+ - 6 significant bits — default
824
+ - 7 significant bits
825
+
826
+ exact nearest-LNS8 conversionとの不一致率を測定する。
827
+
828
+ ---
829
+
830
+ ## 13. Q4_0 weight path
831
+
832
+ Q4_0 weight storageは4bit packedのまま維持する。
833
+
834
+ benchmarkで明確な利点が出ない限り、モデルを8bit LNS8 weightへ展開して保存しない。
835
+
836
+ Q4_0 coefficient:
837
+
838
+ ```text
839
+ -8 -7 -6 -5 -4 -3 -2 -1 0 +1 +2 +3 +4 +5 +6 +7
840
+ ```
841
+
842
+ 絶対値Q4 coefficientからLNS8 exponent offsetへのtiny LUT:
843
+
844
+ ```text
845
+ |q| : 1 2 3 4 5 6 7 8
846
+ Δ : 0 8 13 16 19 21 22 24 // nearest 1/8-octave approximation
847
+ ```
848
+
849
+ reference path:
850
+
851
+ ```text
852
+ Q4 nibble
853
+ ↓
854
+ small LUT
855
+ ↓
856
+ weight LNS offset + sign
857
+ ↓
858
+ packed exponent ADD with activation
859
+ ↓
860
+ product state
861
+ ```
862
+
863
+ Q4 block scale handling候補:
864
+
865
+ 1. FP reference: `q × d`を復元してからLNS8 encode
866
+ 2. Preconverted log-scale: `d`を1回LNS codeへ変換し、exponent offsetとして加算
867
+ 3. Converter-generated custom scale metadata
868
+
869
+ 2、3の追加量子化誤差を1と比較測定する。
870
+
871
+ ---
872
+
873
+ ## 14. Ternary / Bonsai2 fast path
874
+
875
+ Ternary weight groupはBitStateと特に相性がよい。
876
+
877
+ ### Rotation-aware integration point
878
+
879
+ Bonsai2 fast pathでは、ternary weightだけを見るのではなく、**対応するHadamard / sign transform適用後のactivation domain**を入力契約とする。primal-basis activationをそのままternary POPCOUNT pathへ入れてはならない。
880
+
881
+ また、token embedding lookupはrotation-aware runtime contractの境界であり、必要なinverse transformを維持する。multimodal入力についても、vision towerそのものは非回転であり得るが、projected visual tokensがlanguage modelへ入った後は同じlanguage-side basis contractへ従う。
882
+
883
+ したがってBitState kernelの主integration pointは次のいずれかとする。
884
+
885
+ 1. WHT / sign transform完了後のactivationをLNS8 / state representationへencodeする。
886
+ 2. WHT / sign transformとstate encodingをfuseし、中間FP materializationを削減する。
887
+
888
+ 後者は性能最適化候補であり、最初のcorrectness実装では1を使用する。
889
+
890
+ 128-weight group:
891
+
892
+ ```text
893
+ w_i = d × t_i
894
+ t_i ∈ {-1, 0, +1}
895
+ ```
896
+
897
+ 共通scale `d`は128要素のpopulation reduction外に置ける。
898
+
899
+ ```text
900
+ Σ_i w_i a_i
901
+ = d × Σ_i t_i a_i
902
+ ```
903
+
904
+ したがってweight magnitude処理はBoolean selectionまで単純化できる。
905
+
906
+ ```text
907
+ W_nonzero
908
+ W_negative
909
+ ```
910
+
911
+ weightごとのmagnitude multiplicationは不要。
912
+
913
+ ### Signed population count
914
+
915
+ activation magnitude state `s`について:
916
+
917
+ ```text
918
+ A_s = mask of activation lanes whose magnitude state == s
919
+ M = A_s & W_nonzero
920
+ PNEG = activation_sign XOR W_negative
921
+
922
+ N = POPCOUNT(M)
923
+ Nneg = POPCOUNT(M & PNEG)
924
+ Cs = N - 2*Nneg
925
+ ```
926
+
927
+ `Cs`はternary weight selection適用後のactivation state `s`のsigned populationである。
928
+
929
+ group scaleは128要素のstate reduction完了後にのみ適用する。
930
+
931
+ 二つの推奨flowを比較する。
932
+
933
+ Reference / 検証flow:
934
+
935
+ ```text
936
+ 128 activations + ternary masks
937
+ ↓
938
+ state-set intersection
939
+ ↓
940
+ POPCOUNT signed histogram
941
+ ↓
942
+ dyadic integer shift/add
943
+ ↓
944
+ wide fixed-point / optional Q9.6
945
+ ↓
946
+ shared group scale d
947
+ ```
948
+
949
+ Pure-LNS flow:
950
+
951
+ ```text
952
+ 128 activations + ternary masks
953
+ ↓
954
+ state-set intersection
955
+ ↓
956
+ POPCOUNT signed histogram ← dependency-breaking reduction
957
+ ↓
958
+ non-zero population buckets
959
+ ↓
960
+ population count -> fractional-LNS offset
961
+ ↓
962
+ balanced signed LNS add/sub tree
963
+ ↓
964
+ shared scaleをlog-code offsetとしてADD
965
+ ↓
966
+ 境界でのみ最終LNS8 rounding
967
+ ```
968
+
969
+ pure-LNS pathがv0.5でも残す強い仮説である。GEMV内部reductionではFP、Q9.6、その他linear accumulatorを必須としない。reference pathはdifferential testingのため必ず残す。
970
+
971
+ ---
972
+
973
+ ## 15. CPU implementation plan
974
+
975
+ ### Reference 0 — scalar correctness
976
+
977
+ - final product stateを各laneで明示decode
978
+ - `A[L3] += sign × (1 << E4)` を直接実行
979
+ - exact double/FP64 direct decodeと比較
980
+ - performance目標なし
981
+
982
+ ### Reference 1 — scalar histogram equivalence
983
+
984
+ - `H[phase][octave]`生成
985
+ - `A[p] = Σ_o H[p][o] << o`
986
+ - Reference 0の `A[0..7]` とbit-exact一致を要求
987
+
988
+ ### CPU PoC A — Tile64 bitslice direct 8-phase path
989
+
990
+ - 64 final product states
991
+ - `L2,L1,L0,E3..E0,BSIGN` のbitplane / equivalent mask extraction
992
+ - 3bit phase decoderから中間selector mask
993
+ - E4の `±(1<<E)` weighted contributionを8 phase accumulatorへ縮約
994
+ - 64項 → 8 partial accumulators
995
+ - two Tile64でExcel v0.8の128項validationを再現
996
+
997
+ ### CPU PoC B — Tile64 signed histogram path
998
+
999
+ - full-stateまたはphase-local membership masks
1000
+ - `Hpos/Hneg` または `N-2*Nneg`
1001
+ - POPCNT64
1002
+ - histogram → `A[0..7]`
1003
+
1004
+ ### CPU PoC B2 — pure fractional-LNS reduction
1005
+
1006
+ - signed POPCOUNT histogramを入力
1007
+ - non-zero bucket populationをtiny count-to-log LUTでfold
1008
+ - configurable fractional bitsを持つlog coordinate
1009
+ - balanced `kappa+ / kappa-` LUT add/sub tree
1010
+ - direct 8-phase dyadic pathとの総cycles / dependency depthを比較
1011
+
1012
+ ### CPU PoC C — AVX2 / ordinary 64-bit CPU
1013
+
1014
+ 最初は特殊巨大bit配列を前提にせず、標準的なinteger / bit primitiveだけで成立性を確認する。
1015
+
1016
+ 候補primitive:
1017
+
1018
+ - packed byte ADD for LNS product generation
1019
+ - 64-bit scalar bitplanes / masks
1020
+ - AND / OR / XOR / NOT / SHIFT
1021
+ - POPCNT64 where useful
1022
+ - `pshufb` / vector mask extraction
1023
+ - 8 independent integer accumulators
1024
+
1025
+ ### CPU PoC D — AVX-512 / wider bitsets
1026
+
1027
+ correctnessがTile64で確定した後に評価する。
1028
+
1029
+ - mask registers
1030
+ - VPOPCNT
1031
+ - wider state-set construction
1032
+ - multiple output dots in flight
1033
+ - direct 8-phase masked/grouped reduction
1034
+
1035
+ ## 16. GPU implementation plan
1036
+
1037
+ CPUの命令列ではなく、同じalgorithmic abstractionを32-lane warpへ写像する。
1038
+
1039
+ 最初の整合性ターゲット:
1040
+
1041
+ ```text
1042
+ Tile32 = one 32-bit ballot mask
1043
+ ```
1044
+
1045
+ CPU Tile64と同じfieldを:
1046
+
1047
+ ```text
1048
+ L0 L1 L2
1049
+ E0 E1 E2 E3
1050
+ SIGN
1051
+ ```
1052
+
1053
+ の32bit maskとして生成し、同じphase routing / signed-count式を使う。
1054
+
1055
+ GPU objective:
1056
+
1057
+ ```text
1058
+ packed LNS product ADD / sign XOR
1059
+ ↓
1060
+ 32-lane final product states
1061
+ ↓
1062
+ ballot / lane predicates
1063
+ ↓
1064
+ 8-way phase routing
1065
+ ├─ direct ±(1<<E) integer reduction to A[0..7]
1066
+ └─ optional signed POPCOUNT histogram
1067
+ ↓
1068
+ 8-state finalization or pure-LNS tree
1069
+ ```
1070
+
1071
+ 候補:
1072
+
1073
+ - warp ballot
1074
+ - `__popc` / `__popcll`
1075
+ - `__match_any_sync`
1076
+ - warp-level grouping / segmented reduction
1077
+ - shared-memory 8-accumulator
1078
+ - register-resident partial accumulators
1079
+
1080
+ 最低限benchmarkするもの:
1081
+
1082
+ 1. scalar/lane reference `A[L3] += ±(1<<E4)`
1083
+ 2. ballot-generated phase masks + direct 8-accumulator reduction
1084
+ 3. phase-local signed histogram
1085
+ 4. full-state histogram / match-any grouping
1086
+
1087
+ CPU風bit transposeを機械的に移植しない。最初にTile32とTile64で数学的等価性を確定し、その後にlarge bitset / wider subgroup / custom layoutを検討する。
1088
+
1089
+ ## 17. FPGA / custom logic model
1090
+
1091
+ streaming implementationは素直に構成できる。
1092
+
1093
+ ```text
1094
+ packed state generation
1095
+ ↓
1096
+ state-set decoder / bit-vector formation
1097
+ ↓
1098
+ population counters
1099
+ ↓
1100
+ compressed signed state populations
1101
+ ├─ reference: shift/add → fixed-point
1102
+ └─ pure-LNS: count-log LUT → balanced kappa± tree
1103
+ ↓
1104
+ log-scale offset
1105
+ ↓
1106
+ LNS8
1107
+ ```
1108
+
1109
+ ternary weightでは:
1110
+
1111
+ ```text
1112
+ activation state masks
1113
+ AND ternary non-zero/sign masks
1114
+ POPCOUNT
1115
+ ```
1116
+
1117
+ によって通常のmultiplier logicの大部分を置換できる。
1118
+
1119
+ ---
1120
+
1121
+ ## 18. Correctness test plan
1122
+
1123
+ ### Exhaustive micro-tests
1124
+
1125
+ 1. 全LNS8 non-zero code pair
1126
+ 2. 全sign combination
1127
+ 3. zero × anything
1128
+ 4. saturation boundary
1129
+ 5. Q9.6 → LNS8 full input range
1130
+ 6. 5-bit / 6-bit / 7-bit re-log LUT comparison
1131
+ 7. same-sign fractional-LNS pair addとFP64の一致
1132
+ 8. 異符号add/subとexact cancellation
1133
+ 9. population count folding: `n`個の同一state vs `k + 8*log2(n)`、`n=1..128`
1134
+ 10. 複数internal fractional precisionでbalanced-tree pure-LNS reductionとdirect FP64を比較
1135
+
1136
+ ### Dot-product tests
1137
+
1138
+ randomおよびadversarial vectorで:
1139
+
1140
+ ```text
1141
+ K = 32
1142
+ K = 64
1143
+ K = 128
1144
+ K = 256
1145
+ K = 4096
1146
+ K = 8192
1147
+ ```
1148
+
1149
+ reference:
1150
+
1151
+ - FP32 original
1152
+ - ordinary Q4_0 dequantized dot
1153
+ - exact LNS8→FP64 dot
1154
+ - BitState histogram path
1155
+
1156
+ 誤差を次の要因へ分離する。
1157
+
1158
+ 1. source quantization error
1159
+ 2. Q4→LNS mapping error
1160
+ 3. histogram arithmetic error
1161
+ 4. fixed-point materialization error
1162
+ 5. Q9.6 rounding error
1163
+ 6. Q9.6→LNS8 re-encoding error
1164
+
1165
+ 選んだfinite-state representationに対してhistogram自体はexactであるべき。
1166
+
1167
+ ---
1168
+
1169
+ ## 19. Performance test plan
1170
+
1171
+ 各stageを個別およびfusedで測定する。
1172
+
1173
+ ### Micro-kernels
1174
+
1175
+ A. Q4 packed decode / LUT
1176
+
1177
+ B. packed product-state ADD
1178
+
1179
+ C. state decomposition
1180
+
1181
+ D. state-tile packing / bit transpose
1182
+
1183
+ E. state mask generation
1184
+
1185
+ F. POPCOUNT histogram
1186
+
1187
+ G. histogram shift/add reduction
1188
+
1189
+ H. eight phase coefficients / dyadic reference finalization
1190
+
1191
+ I. population-count → fractional-LNS offset LUT
1192
+
1193
+ J. `kappa+ / kappa-` fractional-LNS add/sub primitive
1194
+
1195
+ K. balanced 8→4→2→1 LNS reduction
1196
+
1197
+ L. Q9.6 → LNS8 reference boundary
1198
+
1199
+ M. fully fused dot/GEMV kernel
1200
+
1201
+ ### Metrics
1202
+
1203
+ CPU:
1204
+
1205
+ - cycles / output
1206
+ - cycles / weight
1207
+ - instructions / weight
1208
+ - uops
1209
+ - IPC
1210
+ - POPCNT throughput
1211
+ - dependent reduction depth / critical-path stages
1212
+ - fractional-LNS LUT access数とadd/sub stage数
1213
+ - L1/L2 misses
1214
+ - effective GB/s
1215
+ - actual memory GB/s
1216
+
1217
+ GPU:
1218
+
1219
+ - kernel latency
1220
+ - weights/s
1221
+ - effective GB/s
1222
+ - memory throughput
1223
+ - registers/thread
1224
+ - occupancy
1225
+ - warp stall reason
1226
+ - ballot/match/popcount instruction cost
1227
+
1228
+ 主要比較指標はnominal FLOPSではなくend-to-end latencyとする。
1229
+
1230
+ ---
1231
+
1232
+ ## 20. Primary baselines
1233
+
1234
+ CPU:
1235
+
1236
+ - llama.cpp Q4_0 GEMV / dot path
1237
+ - scalar Q4_0 dequant + dot
1238
+ - optimized INT8 dot where available
1239
+ - T-MAC-like LUT path where practical
1240
+
1241
+ GPU:
1242
+
1243
+ - existing Q4 dequant + CUDA GEMV/GEMM
1244
+ - Tensor Core path
1245
+ - CUDA-core low-bit path
1246
+
1247
+ 主workload:
1248
+
1249
+ ```text
1250
+ LLM decode
1251
+ batch = 1 first
1252
+ small batch second
1253
+ prefill later
1254
+ ```
1255
+
1256
+ ---
1257
+
1258
+ ## 21. Optimization priorities
1259
+
1260
+ 優先順位:
1261
+
1262
+ 1. memory traffic
1263
+ 2. weight unpack / ternary mask load
1264
+ 3. reusable state-set construction
1265
+ 4. bit transpose / ballot / equivalent grouping
1266
+ 5. POPCOUNT throughputとstate-domain dependency breaking
1267
+ 6. sparse histogram / non-zero bucket layout
1268
+ 7. fractional-LNS count foldingと`kappa±` LUT cost
1269
+ 8. balanced reduction critical path
1270
+ 9. dyadic shift/add reference materializer
1271
+ 10. Q9.6 / final boundary conversion
1272
+
1273
+ 最終変換を先に過剰最適化しない。中心測定は、POPCOUNT-based state reductionが、state-set/LUT overheadを上回るだけのdependent arithmeticを消せるかどうかである。
1274
+
1275
+ ---
1276
+
1277
+ ## 22. Patent claim review — 実装文脈の確認であり、設計制約ではない
1278
+
1279
+ 上記の技術設計は、まず技術的合理性と性能に基づいて最適化する。
1280
+
1281
+ 関連familyのうち、現在成立している米国NVIDIA特許の請求項を確認した範囲では、一般的なsoftware LNS bucket accumulation、一般的なphase/octave decomposition、あるいはBitStateのpopulation countingを、広く一般概念として請求しているとは読めない。
1282
+
1283
+ ### US11886980B2
1284
+
1285
+ 独立請求項1、16、19は、概ね次の組合せを中心とする。
1286
+
1287
+ - image or sound data
1288
+ - tensor core
1289
+ - 少��くとも1つのneural-network layer
1290
+ - sign / quotient / remainderとして表現されたlogarithmic value
1291
+ - remainder componentに従ってquotient componentをsortingし、partial sumsを生成
1292
+ - 指定されたneural-network result
1293
+
1294
+ 従属請求項では、別accumulator、shifted binary-one quotient contribution、scaling、re-log conversion等が追加される。
1295
+
1296
+ ### US12118454B2
1297
+
1298
+ continuationも、独立請求項では同様に具体的な組合せを維持する。
1299
+
1300
+ - image or sound data
1301
+ - tensor core / neural-network layer
1302
+ - quotient/remainder sorting into partial sums
1303
+ - downstream application use
1304
+
1305
+ 従属請求項でseparate accumulatorやshifted-one contribution等が追加される。
1306
+
1307
+ ### US12033060B2
1308
+
1309
+ 独立請求項は、より具体的に **asynchronous accumulator** architectureへ向けられている。例:
1310
+
1311
+ - bit position `j`を指定するbit-selection input
1312
+ - asynchronous accumulator outputへ`2^j`を加える
1313
+ - claim 1におけるpositive/negative paired asynchronous accumulators
1314
+ - 他の独立請求項におけるtoggle flip-flop accumulator-cell implementation
1315
+ - claim 7におけるquotient-derived one-hot bit selection
1316
+ - system claim 16におけるasynchronous accumulator array
1317
+
1318
+ BitStateの通常CPU/GPU上のBoolean mask + POPCOUNT histogramは、このaccumulator architectureとは異なる。
1319
+
1320
+ ### US12141225B2
1321
+
1322
+ 独立請求項1は、特に次を要求する。
1323
+
1324
+ - asynchronous accumulator array
1325
+ - remainder valueごとに対応するaccumulator
1326
+ - quotientが選択したsingle accumulator bitをincrement / decrement
1327
+ - partial-sum bitを計算するpaired toggle flip-flops
1328
+
1329
+ software state-set / POPCOUNT histogramは、この構造を用いない。
1330
+
1331
+ ### Engineering conclusion
1332
+
1333
+ したがってBitState設計を、次の一般的な数値技法を避けるためだけに人工的に弱くする必要はない。
1334
+
1335
+ - phase/octave extraction
1336
+ - state grouping
1337
+ - shift/add reconstruction
1338
+ - fixed coefficients
1339
+ - leading-one re-encoding
1340
+
1341
+ これらは有用な一般的数値技法であり、それ自体が上記成立済み独立請求項の内容ではない。
1342
+
1343
+ NVIDIAのissued familyにもquotient/remainder bucket accumulationが含まれ、8-phase decompositionと数学的に近い部分がある。この類似は実装比較では明記する。一方v0.5のsoftware仮説は、**wide reusable state set + POPCOUNTをdependency-breaking reductionとして使うこと**、さらに任意で **linear partial-sum materializationをせずpure fractional-LNS reduction treeへ接続すること**を中心に置く。これはengineering上の区別であり、法的結論ではない。
1344
+
1345
+ BitStateの実装焦点は次へ更新する。
1346
+
1347
+ ```text
1348
+ finite states
1349
+ ↓
1350
+ wide reusable state-set representation
1351
+ ↓
1352
+ parallel population counting
1353
+ ↓
1354
+ dependency-broken compressed state domain
1355
+ ├─ dyadic reference materialization
1356
+ └─ pure fractional-LNS reduction tree
1357
+ ```
1358
+
1359
+ 本節はengineering claim reviewであり、法的なnon-infringement opinionではない。
1360
+
1361
+ ### Pending applications
1362
+
1363
+ 米国の公開済みpending applicationは、final enforceable claim setを持つ成立特許と同列には扱わない。
1364
+
1365
+ 将来のclaim changeをmonitorする意味はある。米国法上、公開後に成立したclaimがsubstantially identicalであり、actual notice等の要件を満たす場合には、限定的なprovisional-rightsの問題が生じ得る。
1366
+
1367
+ したがって:
1368
+
1369
+ - pending applicationを現在のissued-patent boundaryとして表現しない
1370
+ - formal FTOが必要なら完全に無視もしない
1371
+ - core implementation restrictionとは分離し、monitoring sectionとして扱う
1372
+
1373
+ ---
1374
+
1375
+ ## 23. Coding-agent workflow
1376
+
1377
+ coding agentは、単一layoutを最初から正解と仮定せず、実験的に進める。
1378
+
1379
+ ### Phase 1 — correctness
1380
+
1381
+ 実装:
1382
+
1383
+ ```text
1384
+ exact LNS8 / FP64 direct decode
1385
+ scalar direct 8-phase accumulation: A[L3] += sign × (1<<E4)
1386
+ Tile64 ×2 bitslice-style reference equivalent to Excel v0.8
1387
+ scalar signed histogram
1388
+ histogram -> A[0..7] equivalence
1389
+ fractional-LNS pair add/sub reference
1390
+ population-count -> log-offset LUT
1391
+ balanced pure-LNS reduction
1392
+ optional Q9.6 boundary
1393
+ ```
1394
+
1395
+ `direct decode == direct A[8] == histogram-derived A[8]` をexact一致させる。pure-LNSのinternal rounding errorは別項目として測り、state/routing correctnessと混同しない。
1396
+
1397
+ ### Phase 2 — CPU Tile64 routing / state counting
1398
+
1399
+ 比較実装:
1400
+
1401
+ - Tile64 direct 8-phase routed accumulator
1402
+ - Tile32 word-width-equivalent reference
1403
+ - phase-mask generation cost
1404
+ - phase-local signed histogram
1405
+ - direct full-state masks
1406
+ - SWAR transpose / mask extraction
1407
+ - AVX2 LUT/unpack
1408
+
1409
+ stageごとのcycleを報告し、特に `product ADD`, `state-set/phase-mask build`, `K→8 weighted reduction`, `final 8-state materialization` を分離する。
1410
+
1411
+ ### Phase 3 — ternary 128-group path
1412
+
1413
+ 実装:
1414
+
1415
+ ```text
1416
+ W_nonzero
1417
+ W_negative
1418
+ activation state masks
1419
+ POPCOUNT signed histogram
1420
+ one scale application per 128-weight group
1421
+ ```
1422
+
1423
+ 既存Bonsai2 kernel pathと直接比較する。
1424
+
1425
+ ### Phase 3B — dependency-breaking pure-LNS CPU path
1426
+
1427
+ 既存dyadic materializerと、linear accumulatorを一度も作らない経路を比較する。
1428
+
1429
+ - signed population buckets
1430
+ - count-to-log folding
1431
+ - relative fractional-LNS normalization
1432
+ - balanced `kappa±` reduction
1433
+ - shared scale as log offset
1434
+ - output境界でのみLNS8 rounding
1435
+
1436
+ 総cyclesとcritical-path/dependency depthを別々に報告する。
1437
+
1438
+ ### Phase 4 — GPU
1439
+
1440
+ 独立したCUDA方式を実装:
1441
+
1442
+ - Tile32 ballot phase masks
1443
+ - 8-way direct integer accumulation
1444
+ - match-any
1445
+ - shared / register histogram
1446
+
1447
+ CPU bit-transpose実装を機械的に移植しない。Tile32でCPU Tile64と同じ `A[0..7]` が得られることを先に確認する。
1448
+
1449
+ ### Phase 5 — fused optimization
1450
+
1451
+ profiling後にのみ、実際にtrafficまたはinstruction overheadを削減できるstageをfuseする。
1452
+
1453
+ slowで明快なreference implementationはdifferential testing用に保持する。
1454
+
1455
+ ---
1456
+
1457
+ ## 24. Success criteria
1458
+
1459
+ BitStateは、少なくとも1つの実用backendで、model qualityを許容範囲に維持しながらend-to-end advantageを示せれば成功である。
1460
+
1461
+ 最も期待する初期成功例:
1462
+
1463
+ ```text
1464
+ low-bit / ternary model
1465
+ + batch-1 decode
1466
+ + bandwidth-sensitive hardware
1467
+ + state counting cheaper than dequantize + MAC
1468
+ ```
1469
+
1470
+ dense prefillにおけるTensor Coreのpeak throughputへ勝つ必要はない。
1471
+
1472
+ CPU-only win、ternary-only win、batch-1 decode winのいずれでも、技術的には十分意味がある。
1473
+
1474
+ ---
1475
+
1476
+ ## 25. 一文定義
1477
+
1478
+ > **BitState/HSDRは、reductionを有限state domainへ移して逐次数値依存を切断する。wide state setとPOPCOUNTで多数の低bit寄与を並列圧縮し、残った小さなstate集合だけを縮約する。必要なら、その最終縮約もlinear実数化せずfractional-LNSのまま完結させる。**
1479
+
1480
+ ---
1481
+
1482
+ ## Patent claim reviewで参照した資料
1483
+
1484
+ - https://patents.google.com/patent/US11886980B2/en
1485
+ - https://patents.google.com/patent/US12033060B2/en
1486
+ - https://patents.google.com/patent/US12141225B2/en
1487
+ - https://patents.google.com/patent/US12118454B2/en
1488
+ - https://www.law.cornell.edu/uscode/text/35/154
Bitstate/BitState_LNS8_base74_8step_zone8_popcount_demo_EN_v0.9_RMSNorm.xlsx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be12316c899f86dd9cc74bab78aacb704e61669231cde6d4eaf93502e5651ce2
3
+ size 196171
Bitstate/BitState_LNS8_base74_8step_zone8_popcount_demo_JA_v0.9_RMSNorm.xlsx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ce36adb81a6dd4eab4a60537c5809fd64f66277d090da98bd16fc6698cc67a46
3
+ size 197520
Bitstate/BitState_PoC_experiment_matrix_EN_v0.5.md ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BitState / HSDR PoC Experiment Matrix — v0.5
2
+
3
+ Purpose: make the experiment falsifiable, separate state-count correctness from numerical representation error, and test the new dependency-breaking pure-LNS path against the dyadic reference path.
4
+
5
+ ## A. Correctness layers
6
+
7
+ | ID | Test | Input | Reference | Candidate output | Required result |
8
+ |---|---|---|---|---|---|
9
+ | C0 | LNS8 decode | all codes 0..127, both signs | FP64 formula | decoded value | exact within FP rounding |
10
+ | C1 | phase/band decomposition | all non-zero codes | direct formula | `phase=k&7`, `band=k>>3` | exact |
11
+ | C2 | direct 8-phase accumulation | K=128 vectors | direct LNS8->FP64 sum | `A[L3] += ±(1<<E4)` | exact within FP rounding |
12
+ | C2b | ternary signed population | K=128 vectors | direct state tally | POPCOUNT histogram | bit-exact state counts |
13
+ | C2c | direct-vs-histogram phase state | same vectors | direct `A[0..7]` | histogram `sum_o H[p][o]<<o` | bit-exact `A[0..7]` |
14
+ | C3 | dyadic reconstruction | signed histogram | exact LNS8->FP64 sum | shift/add reference | equivalent within FP rounding |
15
+ | C4 | population folding | `n=1..128` identical states | FP64 `n*x` | `k + 8*log2(n)` | exact in real math; quantify LUT rounding |
16
+ | C5 | fractional LNS same-sign add | exhaustive/sampled state pairs | FP64 add then log | `kappa+` path | quantify internal-code error |
17
+ | C6 | fractional LNS subtract | opposite signs incl. equality | FP64 subtraction | `kappa-` + zero handling | exact zero for equal magnitude; quantify others |
18
+ | C7 | balanced pure-LNS tree | random/adversarial K=128 | direct FP64 ternary dot | tree result | error vs internal fractional precision |
19
+ | C8 | shared scale in log domain | random positive scales | direct `d*sum` | log-offset scale | quantify scale-offset error |
20
+ | C9 | Q9.6 boundary | relevant output range | FP64 / wide fixed | Q9.6 | quantify rounding/saturation; not assumed lossless |
21
+
22
+ ## B. State-distribution sweep
23
+
24
+ | Distribution | Why |
25
+ |---|---|
26
+ | one repeated state | best case for population folding |
27
+ | 8 phases, one band | isolates phase reduction |
28
+ | one phase, 16 bands | tests intra-phase balanced LNS reduction |
29
+ | octave/band sweep | tests exact +8-code relative positioning |
30
+ | uniform random codes | high-cardinality stress case |
31
+ | Gaussian-like encoded values | generic realistic distribution |
32
+ | captured post-Hadamard Bonsai 2 activations | primary real workload |
33
+
34
+ For each 64/128-element tile record unique-state count `U`, non-zero signed bucket count `B`, and active phase count `P`.
35
+
36
+ ## C. Weight-distribution sweep
37
+
38
+ | Pattern | Purpose |
39
+ |---|---|
40
+ | all +1 | sign-free reference |
41
+ | alternating +/- | exact cancellation stress |
42
+ | {-1,0,+1} uniform | generic ternary |
43
+ | measured Bonsai 2 trit distribution | real workload |
44
+ | high zero fraction | tests skip / nonzero mask benefit |
45
+
46
+ ## D. Reuse / amortization sweep
47
+
48
+ Build activation state representation once and reuse it for `N` output rows.
49
+
50
+ ```text
51
+ N = 1, 8, 32, 128, 512, 2048
52
+ ```
53
+
54
+ Report separately:
55
+
56
+ ```text
57
+ T_encode = activation LNS8 encoding
58
+ T_stateset = reusable state-set construction
59
+ T_intersect = one row's weight-mask intersection + POPCOUNT
60
+ T_reduce_dyad = dyadic reference reduction
61
+ T_reduce_lns = pure fractional-LNS reduction
62
+ T_scale = scale application / log-offset handling
63
+
64
+ T_total_ref(N) = T_encode + T_stateset + N*(T_intersect + T_reduce_dyad + T_scale)
65
+ T_total_lns(N) = T_encode + T_stateset + N*(T_intersect + T_reduce_lns + T_scale)
66
+ ```
67
+
68
+ ## E. Kernel variants
69
+
70
+ | ID | Variant | Purpose |
71
+ |---|---|---|
72
+ | B0 | current fork ternary dot | baseline |
73
+ | B1 | scalar direct decode | simple reference |
74
+ | D0 | scalar direct 8-phase accumulator | correctness baseline for `A[L3] += ±(1<<E4)` |
75
+ | D1 | Tile64 direct phase-mask routing | primary ordinary-CPU K→8 reduction candidate |
76
+ | D2 | Tile32 word-width equivalent | CPU/GPU width-invariance correctness check |
77
+ | P0 | scalar signed histogram | validates state-count math |
78
+ | P1 | full-state masks rebuilt per row | intentionally unamortized comparison |
79
+ | P2 | reusable sparse state masks | primary dependency-breaking hypothesis |
80
+ | P3 | phase-local signed histogram | compares explicit population path with direct A[8] |
81
+ | P4 | AVX2/SWAR state construction + POPCNT | optimized CPU state front-end |
82
+ | R0 | dyadic shift/add materializer | reference post-POPCOUNT path |
83
+ | L0 | scalar fractional-LNS add/sub | correctness reference |
84
+ | L1 | balanced sparse-bucket pure-LNS tree | primary pure-LNS CPU hypothesis |
85
+ | L2 | 8-phase normalized `120..127` anchor path | tests relative-state normalization |
86
+ | P5 | PTQ1_0 integrated decode | dense-packing follow-up |
87
+ | G0 | CUDA ballot state-per-lane | GPU front-end candidate |
88
+ | G1 | CUDA match-any grouping + balanced state reduction | GPU candidate |
89
+
90
+ ## F. K sweep
91
+
92
+ ```text
93
+ K = 32, 64, 128, 256, 4096, 5120, 8192
94
+ ```
95
+
96
+ K=128 is especially important because it aligns with Bonsai 2 scale groups.
97
+
98
+ ## G. Fractional-LNS precision sweep
99
+
100
+ Test internal fractional code bits independently of storage LNS8:
101
+
102
+ ```text
103
+ F = 4, 6, 8, 10, 12, 14
104
+ ```
105
+
106
+ For each `F`, generate `count_log[n]`, `kappa_plus[d]`, and `kappa_minus[d]` from a high-precision reference. Measure pairwise error, full-tree error, and LUT footprint.
107
+
108
+ ## H. Reduction-order / dependency sweep
109
+
110
+ Compare:
111
+
112
+ ```text
113
+ serial left fold
114
+ balanced tree
115
+ phase-local balanced tree -> 8-phase 8->4->2->1 tree
116
+ sparse global balanced tree
117
+ ```
118
+
119
+ Report both operation count and **dependent critical-path stages**. This is a first-class metric, not a secondary implementation detail.
120
+
121
+ ## I. CPU metrics
122
+
123
+ - ns / 128-weight group
124
+ - cycles / output row
125
+ - cycles / weight
126
+ - instructions / weight
127
+ - uops / IPC
128
+ - product-state ADD cost
129
+ - phase-mask / selector build cost
130
+ - direct K→8 weighted reduction cost
131
+ - state-set build cost for histogram variants
132
+ - unique states / non-zero signed buckets
133
+ - POPCNT count and throughput
134
+ - fractional-LNS LUT loads
135
+ - dependent LNS-add/sub stages
136
+ - L1/L2 misses
137
+ - actual memory bandwidth
138
+ - crossover reuse count vs baseline
139
+
140
+ ## J. End-to-end metrics
141
+
142
+ - TG128
143
+ - PP512 as secondary context
144
+ - model load / memory footprint unchanged unless explicitly testing metadata
145
+ - quality/perplexity or task check vs existing fork
146
+ - LNS8 saturation / underflow rate
147
+ - pure-LNS vs dyadic end-to-end delta
148
+
149
+ ## K. Reporting rule
150
+
151
+ Always report at least these components separately:
152
+
153
+ ```text
154
+ 1. activation / product-state encoding
155
+ 2. phase-mask / selector construction
156
+ 3. direct K→8 weighted reduction
157
+ 4. state-set construction + POPCOUNT (histogram variants)
158
+ 5. eight-phase dyadic finalization
159
+ 6. pure-LNS reduction cost
160
+ 7. dependent critical-path depth
161
+ 8. end-to-end integrated result
162
+ ```
163
+
164
+ This prevents a fast microkernel from hiding conversion costs and prevents a slow first implementation from hiding the benefit of reusable state sets or a shallower reduction dependency graph.
165
+
166
+ ## L. Reference artifact for direct 8-phase correctness
167
+
168
+ Use `LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx` as the visual reference for the direct path:
169
+
170
+ - `Inputs_128` -> two Tile64 blocks;
171
+ - `S1|E4|L3` decomposition;
172
+ - phase masks as intermediate routing selectors;
173
+ - `±(1<<E4)` routed into eight integer accumulators;
174
+ - equality against direct decoded sum.
175
+
176
+ This workbook proves correctness of the reduction identity, not performance.
177
+
178
+ ## M. Stop / continue decision
179
+
180
+ Continue optimization if at least one realistic region shows either:
181
+
182
+ - lower per-output-row cost after amortization;
183
+ - materially lower dependent reduction depth with competitive total work;
184
+ - lower memory traffic with competitive latency;
185
+ - pure-LNS finalization beats or simplifies the dyadic materializer without unacceptable quality loss.
186
+
187
+ Stop or redesign if real post-Hadamard state cardinality, mask/POPCOUNT cost, or fractional-LNS LUT/reduction cost leaves no crossover against the existing ternary kernel.
Bitstate/BitState_PoC_performance_expectations_JA_v0.5.md ADDED
@@ -0,0 +1,584 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BitState / HSDR PoC — 性能期待値と実装ターゲット v0.5
2
+
3
+ **Status:** 実装前の性能仮説 / PoC motivation。性能保証ではない。
4
+ **Primary target:** low-bit / ternary LLM GEMV、batch-1 decodeを最優先。
5
+ **Reference:** `BitState_PoC_technical_brief_EN_v0.5.md`、`BitState_HSDR_v0.5_ja_WHT-RMSNorm-premise.md`、`LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx`
6
+
7
+ ---
8
+
9
+ ## 1. 何を狙うPoCか
10
+
11
+ BitState/HSDRの現在の中心課題は、LNS8 productを実数へ展開してから通常のMACを行うのではなく、productを最後まで低bit stateとして扱い、巨大なGEMV/GEMM reductionを8本のphase aggregateへ落とせるか、という点にある。
12
+
13
+ final product stateを
14
+
15
+ ```text
16
+ S1 | E4 | L3
17
+ ```
18
+
19
+ とみなすと、direct dyadic pathは各productについて
20
+
21
+ ```text
22
+ phase = L3
23
+ payload = sign * (1 << E4)
24
+ A[phase] += payload
25
+ ```
26
+
27
+ となる。
28
+
29
+ したがって、1 output dotに `K=4096` / `5120` のproductがあっても、最終的に残す必要がある整数状態は
30
+
31
+ ```text
32
+ A[0] ... A[7]
33
+ ```
34
+
35
+ の8本だけである。最後に8種類のfractional-log係数を適用し、8項だけを数値実体化すればよい。
36
+
37
+ BitStateが性能上狙いたいのは、Tensor Coreのpeak FLOPSと競争することではない。
38
+
39
+ > **decode-likeな低arithmetical-intensity領域で、product生成とreductionをADD / SHIFT / Boolean / mask / ballot / POPCOUNT中心へ落とし、計算側の時間をweight-fetch時間の陰へ押し込む。最終的にkernelをmemory-bandwidth-boundへ近づける。**
40
+
41
+ これが達成できれば、BitState PoCとして非常に強い成功とみなす。
42
+
43
+ ---
44
+
45
+ ## 2. 現在比較すべき4方式
46
+
47
+ | 方式 | 強み | 弱み | 特に期待する領域 |
48
+ |---|---|---|---|
49
+ | **A. direct masked ADD** | explicit transpose不要。`L3 -> selector`、`E4 -> shift`、`S1 -> +/-` をそのまま整数SIMD/SIMTへ写像できる | 8-wayのmask / grouped reductionを各Tileで処理する必要がある | 最初のperformance baseline。CPU/GPU双方で実装しやすい |
50
+ | **B. bitslice + POPCOUNT** | transpose後は1 Boolean演算で32/64 laneを同時処理できる。複数条件をbitset上で合成できる | CPUではstate-set生成 / bit transposeの固定費がある | 64-lane state processingの密度がtranspose費を上回るかを検証 |
51
+ | **B + state-set再利用** | state-set構築費を多数output rowへamortizeできる。row側がAND/XOR/POPCOUNT中心まで単純化する可能性 | activation側state-setを再利用可能なモデル / factorizationが必要 | ternary、shared-scale、Bonsai2系。最も大化けする可能性がある |
52
+ | **GPU ballot版B** | CPUのexplicit transposeをsubgroup ballotで置換できる。subgroupのlane集合がそのままbitsetになる | subgroup feature差、register pressure、workgroup merge設計が必要 | GPUでBitState state-set化を最も自然に試せる候補 |
53
+
54
+ AとBはfallback関係ではない。同じ数学を異なるhardware representationで実装する競合kernelである。
55
+
56
+ ```text
57
+ A: lane/SIMD/SIMT state
58
+ -> phase selector
59
+ -> masked/grouped integer reduction
60
+ -> A[8]
61
+
62
+ B: lane state
63
+ -> bitset / ballot state-set
64
+ -> Boolean membership intersection
65
+ -> POPCOUNT / shift reduction
66
+ -> A[8]
67
+ ```
68
+
69
+ ---
70
+
71
+ ## 3. 期待できる性能上の理由
72
+
73
+ ### 3.1 LNS productの主演算をMULから整数ADDへ置換できる
74
+
75
+ LNS8 magnitude productは、必要なlog-domain offsetを反映した後、基本的にcode ADDとして生成できる。
76
+
77
+ ```text
78
+ activation code
79
+ + weight / scale log offset
80
+ ---------------------------
81
+ final product state
82
+ ```
83
+
84
+ signはXOR / maskで処理する。
85
+
86
+ 低bit product生成の段階で一般的なFP/INT multiplyを避けられることが最初の利点である。
87
+
88
+ ### 3.2 K個のproductをK個の実数へ展開しない
89
+
90
+ 通常の見方では、`K=4096`なら4096個のproductを数値として扱い、長いreductionを行う。
91
+
92
+ BitStateではproductを
93
+
94
+ ```text
95
+ S1 = sign
96
+ E4 = power-of-two shift position
97
+ L3 = 8-way destination
98
+ ```
99
+
100
+ として扱うため、K個すべてについて「実数値そのもの」を知る必要がない。
101
+
102
+ ### 3.3 最終linear materializationは8項だけ
103
+
104
+ K項の処理後に残るのは `A[0..7]` だけであり、2の冪でない係数を適用する場所は8 phaseだけでよい。
105
+
106
+ したがって最終materializationそのものは支配項になりにくい。支配項は **K product states -> 8 aggregates** のrouting / reductionである。
107
+
108
+ ### 3.4 8 accumulatorでdependencyを分散できる
109
+
110
+ 1本のserial accumulatorへK回依存するのではなく、phaseごとに独立した8本へ分けられる。
111
+
112
+ ```text
113
+ A0 A1 A2 A3 A4 A5 A6 A7
114
+ ```
115
+
116
+ CPUでは複数execution port / SIMD lane、GPUではsubgroup / warp内groupingを使って、依存鎖を複数本へ分散できる可能性がある。
117
+
118
+ ### 3.5 bitslice後は条件判定密度が��常に高い
119
+
120
+ Tile64をbitsliceすると、例えば
121
+
122
+ ```text
123
+ M[101] = L2 & ~L1 & L0
124
+ ```
125
+
126
+ という数個のBoolean演算で、64 laneすべてについて `phase==5` のmembershipを一斉に生成できる。
127
+
128
+ さらに `E4` / sign条件も同じ64bit bitsetへANDできる。
129
+
130
+ ```text
131
+ phase condition
132
+ AND exponent condition
133
+ AND sign condition
134
+ -> 64-lane membership mask
135
+ -> POPCOUNT
136
+ ```
137
+
138
+ この「64 scalar predicatesを1本のBoolean expressionへ変換する」密度が、transpose費を超えられるかがCPU bitslice PoCの中心テーマになる。
139
+
140
+ ### 3.6 GPUではbit transposeがballotへ置き換わる可能性がある
141
+
142
+ GPU subgroupでは、各invocationが持つpredicateをballotで一つのsubgroup maskへ投影できる。
143
+
144
+ ```text
145
+ 32/64 lane state
146
+ ↓
147
+ subgroup ballot
148
+ ↓
149
+ state membership bitset
150
+ ```
151
+
152
+ したがってCPUで必要なexplicit bit transposeが、GPUではbackend-native subgroup operationへ変わる。
153
+
154
+ これはGPU版Bを特に試す価値がある理由である。
155
+
156
+ ---
157
+
158
+ ## 4. CPUターゲット
159
+
160
+ ### 4.1 理論整合性ターゲット
161
+
162
+ 最初のcorrectness確認は特殊vector命令を要求しない。
163
+
164
+ ```text
165
+ 64-bit integer
166
+ AND / OR / XOR / NOT
167
+ SHIFT
168
+ integer ADD / SUB
169
+ POPCOUNT
170
+ ```
171
+
172
+ でTile64のstate-set pathを表現できる。
173
+
174
+ ### 4.2 実用PoCの推奨baseline: x86-64 AVX2 + POPCNT
175
+
176
+ 最初に性能を測るCPU baselineとしては、**AVX2 + POPCNT**が扱いやすい。
177
+
178
+ A方式では、native masked ADDがなくても
179
+
180
+ ```text
181
+ vector compare
182
+ -> AND / zeroing
183
+ -> vector ADD
184
+ ```
185
+
186
+ でphase選択を実装できる。
187
+
188
+ B方式では、AVX2/SWARをstate extraction / transpose front-endに使い、最終membership maskをscalar 64-bit POPCNTへ渡す構成を試せる。
189
+
190
+ このbaselineなら、特殊な最新CPUだけを対象にせず、比較的広いx86-64環境でPoCできる。
191
+
192
+ ### 4.3 上位最適化: AVX-512
193
+
194
+ AVX-512では次が期待できる。
195
+
196
+ - native mask register
197
+ - masked integer ADD
198
+ - wider vector lane processing
199
+ - CPUによってはvector POPCOUNT
200
+ - 多数のvector registerによるregister-resident working set
201
+
202
+ A方式ではmask routingがより直接的になり、B方式ではmask/state-set処理をさらにwideにできる。
203
+
204
+ ただしAVX-512をBitState成立の必須条件にはしない。**AVX2で成立性を見て、AVX-512で上限を探る**順序を推奨する。
205
+
206
+ ### 4.4 ARM
207
+
208
+ NEON / SVEも論理的には適用可能である。特にSVE predicateはphase membership maskと概念的な相性が良い。
209
+
210
+ 最初のPoCはx86-64を優先し、数値仕様とstate semanticsを固定した後にbackendを増やす。
211
+
212
+ ---
213
+
214
+ ## 5. GPUターゲット — Vulkan Computeを第一級backendとして扱える
215
+
216
+ CUDA固有機能はBitStateの必須条件ではない。
217
+
218
+ BitStateで主に必要なのは:
219
+
220
+ ```text
221
+ integer ADD / SHIFT
222
+ Boolean ops
223
+ subgroup ballot
224
+ bit count
225
+ subgroup shuffle / reduction
226
+ workgroup shared memory
227
+ ```
228
+
229
+ であり、Vulkan Computeで表現可能である。
230
+
231
+ ### 5.1 実用baselineとしてVulkan 1.3を推奨
232
+
233
+ Vulkan subgroup自体は1.1以降のcore APIでqueryできるため、理論上の最低線は1.3より古くてもよい。
234
+
235
+ 一方PoCの実用baselineとして **Vulkan 1.3** を選ぶ理由は:
236
+
237
+ - modern compute toolchainとして扱いやすい
238
+ - subgroup size controlがcoreへ昇格している
239
+ - NVIDIA / AMD / Intelを同じbackend abstractionで試せる
240
+ - CUDA専用kernelにせず、BitStateのbackend independenceを検証できる
241
+
242
+ ためである。
243
+
244
+ ただし `BALLOT` / `ARITHMETIC` / `SHUFFLE` はdevice capabilityをqueryして使用する。Vulkan version番号だけで全機能を仮定しない。
245
+
246
+ ### 5.2 shaderInt8は有利だが必須ではない
247
+
248
+ Vulkan 1.3ではshaderInt8は必須機能ではないため、feature queryが必要である。
249
+
250
+ shaderInt8が使える場合:
251
+
252
+ ```text
253
+ packed LNS8
254
+ -> low-bit / INT8 product state generation
255
+ -> subgroup state reduction
256
+ ```
257
+
258
+ を狙う。
259
+
260
+ 無い場合でも:
261
+
262
+ ```text
263
+ packed uint8 storage
264
+ -> uint32へ展開
265
+ -> integer ADD / SHIFT / Boolean / subgroup ops
266
+ ```
267
+
268
+ でBitStateのアルゴリズム自体は検証できる。
269
+
270
+ したがって古めのVulkan GPUもPoC対象から即座に外す必要はない。
271
+
272
+ ### 5.3 subgroup widthを固定しない
273
+
274
+ BitState GPU backendは
275
+
276
+ ```text
277
+ Tile = subgroup size
278
+ ```
279
+
280
+ として設計し、32固定を前提にしない。
281
+
282
+ NVIDIA的な32-laneでも、AMD等の別subgroup幅でも、同じstate-set semanticsを維持する。
283
+
284
+ subgroup sizeを要求できるdeviceでは、32/64等をbenchmarkして最適点を測る。
285
+
286
+ ---
287
+
288
+ ## 6. GPUで期待したいHSDR階層
289
+
290
+ 例えば `K=4096` を複数subgroupで処理する。
291
+
292
+ ```text
293
+ Global memory
294
+ ↓
295
+ packed low-bit load
296
+ ↓
297
+ product state generation
298
+ ↓
299
+ subgroup-local state reduction
300
+ ↓
301
+ 各subgroup: A[0..7]
302
+ ↓
303
+ workgroup shared memory
304
+ ↓
305
+ 複数subgroupの8-way partialをmerge
306
+ ↓
307
+ A[0..7]
308
+ ��
309
+ 8 phaseだけfinal materialization
310
+ ```
311
+
312
+ 例えば4 subgroupでK方向を分割した場合、subgroup間へ渡す中間結果は概念上
313
+
314
+ ```text
315
+ 4 subgroup × 8 integers = 32 integers
316
+ ```
317
+
318
+ まで縮められる。
319
+
320
+ この階層はHSDRの
321
+
322
+ ```text
323
+ individual products
324
+ -> subgroup-local state aggregates
325
+ -> workgroup-local 8 aggregates
326
+ -> final 8-value materialization
327
+ ```
328
+
329
+ とGPU hardware hierarchyが自然に一致する可能性がある。
330
+
331
+ ---
332
+
333
+ ## 7. 性能期待値 — 予測ではなくEngineering Target
334
+
335
+ 現段階で「x倍高速」と予測する根拠はない。PoCでは、次の順に成功レベルを定義する。
336
+
337
+ ### Level 0 — correctness
338
+
339
+ - direct decode
340
+ - direct 8-phase accumulation
341
+ - bitslice / POPCOUNT histogram
342
+ - GPU subgroup implementation
343
+
344
+ が同じ `A[0..7]` と最終値を返す。
345
+
346
+ ここはbit-exactを要求する。
347
+
348
+ ### Level 1 — compute structureが成立
349
+
350
+ profile上、次が確認できる。
351
+
352
+ - product生成がpacked integer ADD中心
353
+ - K項をFP productへ展開していない
354
+ - state routing / population reductionがregister / subgroup中心
355
+ - intermediate stateをglobal memoryへ大量に書き戻していない
356
+ - final materializationが支配項でない
357
+
358
+ この時点でBitStateの計算モデルがhardwareへ自然に写像できたと判断する。
359
+
360
+ ### Level 2 — optimized low-bit kernelと同じ性能圏へ入る
361
+
362
+ 初期実装の最重要目標は、いきなり既存kernelを上回ることではない。
363
+
364
+ **layout / register scheduling / subgroup mappingを詰めた後に、既存optimized low-bit GEMVと同じorderのlatencyへ入れるか**を見る。
365
+
366
+ ここまで来れば、state reductionという異なる計算方式に実装投資する価値が十分ある。
367
+
368
+ ### Level 3 — memory-bandwidth-boundへ近づく
369
+
370
+ 最も狙いたい状態は:
371
+
372
+ ```text
373
+ compute/state-reduction cost <= weight supply cost
374
+ ```
375
+
376
+ となり、profile上の支配要因がALU / dependencyではなくmemory trafficへ移ること。
377
+
378
+ これはBitStateの本命成功条件である。
379
+
380
+ ### Stretch Goal — sustainable memory bandwidthの70〜80%+付近
381
+
382
+ これは**予測値ではなく、やる気の出るstretch target**とする。
383
+
384
+ 対象hardwareで別途測定したsustainable streaming bandwidthに対し、decode kernelの実効weight bandwidthが70〜80%級まで到達し、compute側が明確に支配項でなくなれば非常に強い結果である。
385
+
386
+ hardware、weight layout、cache、batch、quantization formatによって達成可能値は大きく変わるため、数値そのものを性能保証として扱わない。
387
+
388
+ ---
389
+
390
+ ## 8. どの方式に最も期待するか
391
+
392
+ ### A. direct masked ADD — 最初に作るべき基準kernel
393
+
394
+ 期待理由:
395
+
396
+ - transpose不要
397
+ - 数学が単純
398
+ - CPU SIMD / GPU SIMTへ直接写像可能
399
+ - Excel / scalar referenceとの比較が容易
400
+
401
+ この方式が十分速ければ、BitStateはPOPCOUNTを必須とせず成立する。
402
+
403
+ ### B. CPU bitslice + POPCOUNT — 転置後の高密度処理に期待
404
+
405
+ 期待理由:
406
+
407
+ - Tile64全体を64bit Boolean演算で扱える
408
+ - membership条件の合成が非常に安い
409
+ - intermediate maskのlive rangeが短い
410
+ - 64 scalar predicateを少数Boolean opsへ縮められる
411
+
412
+ 勝負点は明確で、**transpose/state-set build costを後段の64-lane同時処理が回収できるか**である。
413
+
414
+ ### B + state-set reuse — CPU側で最も大化けし得る候補
415
+
416
+ activation state-setを一度作り、多数output rowでreuseできる場合、固定費を大きく償却できる。
417
+
418
+ 特にternary weightならrowごとの処理を
419
+
420
+ ```text
421
+ activation state mask
422
+ AND weight non-zero/sign mask
423
+ -> POPCOUNT
424
+ ```
425
+
426
+ へ寄せられる可能性がある。
427
+
428
+ この領域はBitState/HSDRのdependency-breaking仮説と最も強く噛み合う。
429
+
430
+ ### GPU ballot版B — GPU側で最も試したい候補
431
+
432
+ CPUのbit transposeをsubgroup ballotへ置換できるため、B方式の最大の弱点が軽くなる可能性がある。
433
+
434
+ さらにGPUには:
435
+
436
+ - 多数subgroupの同時実行
437
+ - subgroup register communication
438
+ - workgroup shared memory
439
+ - output-row parallelism
440
+
441
+ がある。
442
+
443
+ BitStateの
444
+
445
+ ```text
446
+ state -> subgroup set -> 8 aggregates -> workgroup merge
447
+ ```
448
+
449
+ という階層がhardware topologyへ自然に一致するかもしれない。
450
+
451
+ **GPU PoCを作る理由として最も期待値が高い部分の一つである。**
452
+
453
+ ---
454
+
455
+ ## 9. 最初の実装順序
456
+
457
+ 性能探索は次の順序を推奨する。
458
+
459
+ 1. **Scalar direct A[8]**
460
+ correctness reference。`A[L3] += ±(1<<E4)`。
461
+
462
+ 2. **AVX2 direct masked/grouped ADD**
463
+ transposeなしの現実的CPU baseline。
464
+
465
+ 3. **Tile64 bitslice + POPCOUNT**
466
+ transpose費を含むB方式の実測。
467
+
468
+ 4. **reusable state-set variant**
469
+ ternary / Bonsai2等でoutput-row reuseを測る。
470
+
471
+ 5. **Vulkan Compute direct subgroup reduction**
472
+ GPU A方式。
473
+
474
+ 6. **Vulkan subgroup ballot state-set reduction**
475
+ GPU B��式。explicit transposeをballotへ置換。
476
+
477
+ 7. **multi-subgroup K split + workgroup 8-way merge**
478
+ `K=4096/5120`を本来のGEMV形状で測る。
479
+
480
+ 8. **model integration**
481
+ end-to-end decode latency / TG128 / effective bandwidthを測る。
482
+
483
+ ---
484
+
485
+ ## 10. 測るべきもの
486
+
487
+ 単一のmicrokernel nsだけでは判断しない。
488
+
489
+ 最低限、以下を分離して測る。
490
+
491
+ ```text
492
+ 1. weight / activation load
493
+ 2. low-bit decode
494
+ 3. product-state ADD / XOR
495
+ 4. phase selector / state-set build
496
+ 5. routing / masked reduction / POPCOUNT
497
+ 6. K -> A[8] reduction
498
+ 7. subgroup/workgroup merge
499
+ 8. final 8-phase materialization
500
+ 9. total kernel latency
501
+ 10. effective weight bandwidth
502
+ ```
503
+
504
+ CPU:
505
+
506
+ - cycles / output row
507
+ - cycles / weight
508
+ - instructions / weight
509
+ - uops / IPC
510
+ - POPCNT throughput
511
+ - cache misses
512
+ - measured memory bandwidth
513
+ - register spillの有無
514
+
515
+ GPU:
516
+
517
+ - kernel latency
518
+ - effective GB/s
519
+ - achieved memory throughput
520
+ - registers / invocation
521
+ - occupancy
522
+ - subgroup width
523
+ - ballot / shuffle / arithmetic cost
524
+ - shared-memory traffic
525
+ - spill / local-memory traffic
526
+ - memory stall vs execution stall
527
+
528
+ ---
529
+
530
+ ## 11. PoCの価値は「勝つこと」だけではない
531
+
532
+ このPoCは、Tensor Coreを置き換えるという主張ではない。
533
+
534
+ また、BitStateが必ず既存optimized GEMVを上回るという性能claimでもない。
535
+
536
+ 検証したい問いは狭い。
537
+
538
+ > **巨大Kを持つ低bit GEMVにおいて、productを実数化せずstateのまま扱い、8-way routing / wide Boolean state-set / subgroup ballot / POPCOUNTによってreduction overheadを十分小さくし、kernelをmemory-bandwidth-boundへ押し込める領域が存在するか。**
539
+
540
+ 負けても、次のどこが支配したかを切り分けられればPoCとして価値がある。
541
+
542
+ ```text
543
+ product decode
544
+ phase routing
545
+ bit transpose
546
+ state-set construction
547
+ POPCOUNT
548
+ register pressure
549
+ subgroup reduction
550
+ shared-memory merge
551
+ memory traffic
552
+ ```
553
+
554
+ 逆に、一つでも現実的なCPU/GPUで
555
+
556
+ ```text
557
+ compute overhead << model weight transfer cost
558
+ ```
559
+
560
+ へ近づく領域が見つかれば、BitStateをさらに詰める十分な理由になる。
561
+
562
+ ---
563
+
564
+ ## 12. 一緒に試してほしいポイント
565
+
566
+ BitStateは現時点では性能仮説であり、実装者の知見で大きく化ける余地がある。
567
+
568
+ 特に知見が欲しいのは:
569
+
570
+ - AVX2/AVX-512での最小cost 8-way integer routing
571
+ - Tile64 bit transposeの実測最小cost
572
+ - POPCNTとmask-generationのinstruction scheduling
573
+ - register-resident state-set layout
574
+ - Vulkan subgroup ballot / arithmeticの実測cost
575
+ - 32 vs 64 subgroup sizeの差
576
+ - multi-subgroup K splitの最適数
577
+ - workgroup shared-memory mergeの最小化
578
+ - low-bit weight loadとstate reductionのpipeline / double buffering
579
+ - model row layoutとcoalesced / sequential memory accessの統合
580
+
581
+ このPoCで最も知りたいのは、「理論上できるか」ではなく、**ordinary CPU/GPUの生のinteger / bit hardwareへ落としたとき、どこまでmemory wallへ迫れるか**である。
582
+
583
+ > **目標はFLOPSを増やすことではない。必要なFLOPSそのものを減らし、最後までstateとして運び、weightを読み切る速度に計算を追いつかせること。**
584
+
Bitstate/BitState_PoC_review_notes_JA_v0.5.md ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BitState / HSDR v0.5 PoC向けレビュー補足
2
+
3
+ ## 結論
4
+
5
+ v0.5では、BitState/HSDRの中心説明を次へ更新する。
6
+
7
+ > **state-domainへ移すことでreduction dependencyを切断する。**
8
+
9
+ POPCOUNTは重要な候補primitiveだが、v0.5では必須要件から外す。最も単純なcorrectness baselineは、final product state `S1|E4|L3` から `A[L3] += sign×(1<<E4)` として8本のinteger accumulatorへ直接縮約する方式である。
10
+
11
+ 既存v0.3の `8-ZONE -> integer shift/add -> Q9.6` 経路は捨てず、correctness / performanceのreferenceとして残す。その上でv0.5でも、POPCOUNT後もlinear値へ実体化せず、relative fractional-LNSで最後まで縮約する経路を追加する。
12
+
13
+ ## 1. v0.5で明示する8点
14
+
15
+ 1. state-domain reductionの目的は**dependency breaking**である。
16
+ 2. activation state-setはoutput row間でreuseし、構築費をamortizeする。
17
+ 3. signed POPCOUNT後の同一state populationは、`k + 8*log2(|count|)`で一つのfractional-LNS stateへfoldできる。
18
+ 4. 8 phase / 16 bandは相対log位置として扱え、`+8 code = x2`をoctave移動にそのまま使える。
19
+ 5. 最終state集合はserial accumulatorではなくbalanced LNS add/sub treeで縮約する。
20
+ 6. dyadic reference pathとpure-LNS pathを別々に測る。
21
+ 7. direct 8-phase pathをcorrectness baselineとして明示する。
22
+ 8. phase maskは中間routing selectorであり、POPCOUNT自体を必須条件にしない。
23
+
24
+ ## 2. 既存設計から変わらない部分
25
+
26
+ - LNS8 base74 / 8 codes per octave
27
+ - `phase = code & 7`, `band = code >> 3`
28
+ - ternary weightをnonzero/sign maskへ落とす
29
+ - Bonsai 2ではHadamard/sign transform後activationが入力契約
30
+ - group scaleは128-weight population reductionの外側
31
+ - CPUではTile64/Tile128、SWAR/AVX2/AVX-512候補
32
+ - GPUではballot/match-any等のbackend-native set construction
33
+ - negative resultも有用とするPoC方針
34
+
35
+ ## 2.1 v0.5で追加したdirect 8-phase baseline
36
+
37
+ 現行Excel `LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx` の手順を、correctness referenceとして明示する。
38
+
39
+ ```text
40
+ final product state = S1 | E4 | L3
41
+ phase = L3
42
+ payload = sign × (1 << E4)
43
+ A[phase] += payload
44
+ ```
45
+
46
+ 128項ならTile64×2、GPU想定ならTile32×4として同じ数学を検証できる。`L2,L1,L0`から作るphase maskは64/32 laneの所属を示す中間selectorであり、最終値ではない。
47
+
48
+ full histogramを使う場合も:
49
+
50
+ ```text
51
+ A[p] = Σ_o H[p][o] × 2^o
52
+ ```
53
+
54
+ となるため、direct pathとbit-exactに同じ8本へ到達する。
55
+
56
+ 重要なのは、実GEMM/GEMVのK=4096/5120でも**全productを最終的に8本へ縮約する**こと。8本まで来れば最後の実数係数処理は小さい。したがって性能評価の本丸は `packed 8-bit product ADD` と `K→8 weighted routing/reduction` である。
57
+
58
+ ## 3. dependency breakingを性能モデルに入れる
59
+
60
+ 従来の評価は主に総instructions/cyclesを見ていたが、v0.5では次も必須にする。
61
+
62
+ ```text
63
+ - state-setを作る固定費
64
+ - POPCOUNTで何個のlane寄与を一括縮約できたか
65
+ - POPCOUNT後のnon-zero bucket数
66
+ - dependent arithmetic stage数
67
+ - serial foldとbalanced treeのcritical-path差
68
+ ```
69
+
70
+ 128項だけでなく、K=4096/5120の実dotでも「K個のproduct stateを8本へどう縮約するか」を見る。direct pathは8本の独立integer accumulator、histogram pathはstate-domain population reductionとして比較する。
71
+
72
+ ## 4. pure fractional-LNS path
73
+
74
+ signed population `C[k]` がnon-zeroなら、同じstateの重複は
75
+
76
+ ```text
77
+ sign = sign(C[k])
78
+ lambda = k + 8*log2(abs(C[k]))
79
+ ```
80
+
81
+ へfoldできる。Tile128ではcountは最大128なのでtiny LUTでよい。
82
+
83
+ その後、same-sign / opposite-signのLNS add/sub correction LUTを使う。
84
+
85
+ ```text
86
+ kappa_plus(d) = 8*log2(1 + 2^(-d/8))
87
+ kappa_minus(d) = 8*log2(1 - 2^(-d/8))
88
+ ```
89
+
90
+ 最終8 phaseは `8->4->2->1` の3-stage treeで統合する。phase内に複数bandが残る場合も同じprimitiveをbalanced treeで使う。
91
+
92
+ ## 5. `120..127` anchor / code122の位置づけ
93
+
94
+ 上位octaveの8 codeをphaseごとの正規化anchorとして使える。
95
+
96
+ ```text
97
+ k = 8*q + phase
98
+ = (120 + phase) + 8*(q - 15)
99
+ ```
100
+
101
+ code122はexact 64のpower-of-two anchor。重要なのは「64という実数へ変換する」ことではなく、**既知の相対log位置へstateを移す**ことである。
102
+
103
+ このrelative-state viewはBitStateの思想に合う。値をdecodeして計算するのではなく、表現できるbit/state位置のままどこまで操作を完結できるかを追う。
104
+
105
+ ## 6. dyadic pathとの区別
106
+
107
+ v0.5の優先dyadic referenceは、full histogramを必須とせず、各productから直接: `A[L3] += sign × (1<<E4)` とする。histogramを使う場合の `count << band` はこのdirect pathとexactに同じ8本を生成する別実装である。どちらも数学的にはlinear/dyadic accumulatorである。
108
+
109
+ v0.5のpure-LNS pathは、そこまで実体化せず、population countをlog offset化してLNS add/sub treeへ直接入れる。
110
+
111
+ 両者を混同せず、同じhistogram入力からA/B比較する。
112
+
113
+ ## 7. Excel demoの更新方針
114
+
115
+ 既存sheetは残す。
116
+
117
+ - `Zone_8`: dyadic reference
118
+ - `Popcount_8Table`: state-domain POPCOUNT検証
119
+ - `Pairwise_Add`: integer LNS8 rounded pair add
120
+ - `BitSlice128_一致確認` (Excel v0.8): Tile64×2のbitslice/routing説明とdirect 8-phase accumulatorの一致確認
121
+
122
+ 追加sheetでは:
123
+
124
+ - population count -> fractional-code offset
125
+ - 8 phaseのrelative fractional-LNS coordinate
126
+ - balanced 8->4->2->1 signed LNS reduction
127
+ - direct decodeとの比較
128
+
129
+ を可視化する。Excelの`LOG/POWER`はruntime命令を意味せず、correctness referenceとしてのみ使う。
130
+
131
+ ## 8. PoCの優先順位
132
+
133
+ 1. state-set reuseが成立するか
134
+ 2. POPCOUNT後のbucket cardinalityが十分小さいか
135
+ 3. dependency depthが実際に短くなるか
136
+ 4. pure-LNS LUT/treeのcostと精度
137
+ 5. dyadic pathとの直接比較
138
+ 6. 最後にPTQ1 decode統合とend-to-end TG128
139
+
140
+ ## 9. 成功条件
141
+
142
+ 以下のどれか一つでも出れば次段階へ進む価値がある。
143
+
144
+ - reusable state set後のrow costが既存経路に接近/勝利
145
+ - state-domain化でdependent stage数が大幅に減り、総latencyも競争力を持つ
146
+ - pure-LNS finalizationがdyadic materializerより軽い
147
+ - 特定CPUでcompact ternary traffic + POPCOUNT + short reduction treeが有利
148
+ - end-to-end TG128に小さくても再現可能な改善
149
+
150
+ 負けた場合も、state cardinality / mask build / POPCOUNT / LUT / dependency depth / packing decodeのどこが支配したか分かればPoCとして価値がある。
Bitstate/LNS8_0.125octave_base74_ja_v0.5.md ADDED
@@ -0,0 +1,1012 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # LNS8 仕様(0.125 octave/code・base74・8-ZONE / direct 8-phase / pure-LNS reduction版 v0.5)
2
+
3
+ ## 0. 仕様の核
4
+
5
+ このLNS8は、**1 codeを `log2` で `1/8 = 0.125`** として定義する。
6
+
7
+ - `8 codes = 1 octave = x2` を**厳密**に成立させる
8
+ - `code 74 = 1.0`
9
+ - `code 0 = exact zero`
10
+ - 非zero magnitudeは `code 1..127`
11
+ - 下位3bitをそのまま **8-way accumulator ZONE / phase table** として利用できる
12
+ - final product stateを `L3` で8本へrouteし、`±(1<<E4)`をinteger accumulatorへ直接縮約できる
13
+ - state-domain POPCOUNT histogramも別のexact pathとして利用できる
14
+ - referenceのdyadic shift/addに加え、fractional-LNSのまま最終reductionする候補を持つ
15
+ - 1 codeあたりのdB幅は派生値として
16
+
17
+ \[
18
+ \boxed{\Delta_{\rm dB}=\frac{20\log_{10}2}{8}=0.752574989160\ {\rm dB/code}}
19
+ \]
20
+
21
+ となる。
22
+
23
+ この仕様では旧 `0.1 octave/code` 版の `10 codes = x2 / code 61 = 1` ではなく、
24
+
25
+ \[
26
+ \boxed{8\ {codes} = 1\ {octave} = 6.020599913\ldots\ {dB} = \times2\quad\text{exact}}
27
+ \]
28
+
29
+ を基準にする。
30
+
31
+ ---
32
+
33
+ ## 1. 8bit構成
34
+
35
+ ```text
36
+ bit7 : sign
37
+ bit6..0 : magnitude code k
38
+ ```
39
+
40
+ Magnitude code:
41
+
42
+ ```text
43
+ 0 = exact zero
44
+ 1..127 = nonzero magnitude
45
+ ```
46
+
47
+ 符号はsign-magnitude方式とし、負のzeroは作らない。zeroは常に `0x00` に正規化する。
48
+
49
+ ---
50
+
51
+ ## 2. 数値定義
52
+
53
+ Magnitude codeを \(k=1\ldots127\) とすると、絶対値は
54
+
55
+ \[
56
+ \boxed{|x|=2^{(k-74)/8}}
57
+ \]
58
+
59
+ で定義する。
60
+
61
+ 逆変換は
62
+
63
+ \[
64
+ \boxed{k=\operatorname{round}\left(74+8\log_2|x|\right)}
65
+ \]
66
+
67
+ とし、通常の保存形式では結果を `1..127` にclampする。exact zeroのみ `k=0` とする。
68
+
69
+ ### 中心値
70
+
71
+ \[
72
+ \boxed{\text{code }74=1.0}
73
+ \]
74
+
75
+ ### octaveとの厳密対応
76
+
77
+ ```text
78
+ code差 +8 = x2
79
+ code差 +16 = x4
80
+ code差 +24 = x8
81
+ code差 -8 = x1/2
82
+ code差 -16 = x1/4
83
+ ```
84
+
85
+ この関係は近似ではなく、定義上厳密である。
86
+
87
+ ---
88
+
89
+ ## 3. 1 codeの倍率と量子化誤差
90
+
91
+ 隣接code比は
92
+
93
+ \[
94
+ \boxed{r=2^{1/8}=1.090507732665}
95
+ \]
96
+
97
+ なので、1 codeで約 `9.051%` 増加する。
98
+
99
+ 最近傍丸め時のhalf-stepは \(2^{1/16}\) なので、相対誤差の最大値は概ね
100
+
101
+ \[
102
+ \boxed{+4.427\% / -4.240\%}
103
+ \]
104
+
105
+ 程度。
106
+
107
+ 旧 `10 codes/octave` 版より量子化間隔はやや粗くなるが、`8 = 2^3` とすることで、下位3bitをそのままZONEとして利用できることを優先する。
108
+
109
+ ---
110
+
111
+ ## 4. ダイナミックレンジ
112
+
113
+ 最小非zeroは `k=1`:
114
+
115
+ \[
116
+ |x|=2^{-73/8}=\boxed{0.001791023522}
117
+ \]
118
+
119
+ 最大は `k=127`:
120
+
121
+ \[
122
+ |x|=2^{53/8}=\boxed{98.7014928261}
123
+ \]
124
+
125
+ したがって保存形式の有効範囲は
126
+
127
+ \[
128
+ \boxed{0,\quad \pm0.0017910\ldots\pm98.701493}
129
+ \]
130
+
131
+ となる。
132
+
133
+ ---
134
+
135
+ ## 5. Q9.6との対応
136
+
137
+ Q9.6 raw整数を \(q\) とすると
138
+
139
+ \[
140
+ x=\frac{q}{64}=q\,2^{-6}
141
+ \]
142
+
143
+ なので、非zero値のLNS codeは
144
+
145
+ \[
146
+ \begin{aligned}
147
+ k
148
+ &=\operatorname{round}\left(74+8\log_2\frac{|q|}{64}\right)\\
149
+ &=\boxed{\operatorname{round}\left(26+8\log_2|q|\right)}
150
+ \end{aligned}
151
+ \]
152
+
153
+ まで簡約できる。
154
+
155
+ 特に \(|q|=2^p\) のときは
156
+
157
+ \[
158
+ \boxed{k=26+8p}
159
+ \]
160
+
161
+ が**厳密**に成立する。
162
+
163
+ ```text
164
+ raw 1 = 1/64 = 0.015625 -> code 26 exact
165
+ raw 2 = 1/32 -> code 34 exact
166
+ raw 4 = 1/16 -> code 42 exact
167
+ raw 8 = 1/8 -> code 50 exact
168
+ raw 16 = 1/4 -> code 58 exact
169
+ raw 32 = 1/2 -> code 66 exact
170
+ raw 64 = 1 -> code 74 exact
171
+ raw 128 = 2 -> code 82 exact
172
+ raw 256 = 4 -> code 90 exact
173
+ raw 512 = 8 -> code 98 exact
174
+ ```
175
+
176
+ この性質により、Q9.6との変換でも2進octaveとの対応が崩れない。
177
+
178
+ ---
179
+
180
+ ## 6. Q9.6 -> LNS8 変換方式A:direct LUT
181
+
182
+ 最も単純で、追加の近似誤差を入れない方式。
183
+
184
+ LNS8の最大表現値 `code 127` はQ9.6 raw換算で
185
+
186
+ \[
187
+ 98.7014928261\times64
188
+ =\boxed{6316.89554087}
189
+ \]
190
+
191
+ となる。
192
+
193
+ 最近傍量子化で `code 127` が選ばれ始めるlog中点は `126.5` なので、Q9.6 integer rawでは
194
+
195
+ \[
196
+ q\ge\boxed{6050}
197
+ \]
198
+
199
+ で `code 127` になる。
200
+
201
+ したがって最小寄りのdirect LUTは次のようにできる。
202
+
203
+ ```text
204
+ q = abs(Q9.6 raw)
205
+
206
+ q == 0
207
+ -> code 0
208
+
209
+ q >= 6050
210
+ -> code 127
211
+
212
+ otherwise
213
+ -> table[q]
214
+ ```
215
+
216
+ `table[0..6049]` なら6050 byte、約 `5.91 KiB`。
217
+
218
+ 表現上限まで単純にtable化する場合でも `table[0..6316]` で6317 byte、約 `6.17 KiB` に収まる。
219
+
220
+ runtimeは概ね
221
+
222
+ ```text
223
+ ABS
224
+ CMP / CLAMP
225
+ LUT
226
+ sign付与
227
+ ```
228
+
229
+ だけで、log演算・除算・FP演算は不要。
230
+
231
+ ---
232
+
233
+ ## 7. Q9.6 -> LNS8 変換方式B:octave + 4bit LUT
234
+
235
+ AVX2 / NEON / SWAR向けにtableを極小化する方式。
236
+
237
+ まず
238
+
239
+ \[
240
+ p=\lfloor\log_2|q|\rfloor
241
+ \]
242
+
243
+ をleading-one/MSB位置から求める。
244
+
245
+ power-of-two基準のbase codeは
246
+
247
+ \[
248
+ \boxed{base=26+8p}
249
+ \]
250
+
251
+ で**厳密**。
252
+
253
+ 次に、leading 1��下の上位4bitをmantissa index `m=0..15` とし、16-entry LUTで補正する。
254
+
255
+ bucket中央を代表値にした一例は
256
+
257
+ ```text
258
+ m : 0 1 2 3 4 5 6 7 8 9 A B C D E F
259
+ C[m] : 0 1 2 2 3 3 4 4 5 5 6 6 7 7 7 8
260
+ ```
261
+
262
+ よって
263
+
264
+ \[
265
+ \boxed{k\approx26+8p+C[m]}
266
+ \]
267
+
268
+ とする。
269
+
270
+ Q9.6 raw `1..6049` に対するこの単純な4bit補正では、厳密な最近傍codeと約 `87.5%` で完全一致し、残りも最大 `+-1 code` に収まる。
271
+
272
+ 実装は概ね
273
+
274
+ ```text
275
+ ABS
276
+ |
277
+ MSB / leading-one判定
278
+ |- p -> (p << 3) -> 8p
279
+ `- 上位4bit -> 16-entry LUT / VPSHUFB
280
+ |
281
+ ADD base 26
282
+ |
283
+ CLAMP 1..127
284
+ |
285
+ sign付与
286
+ ```
287
+
288
+ となる。
289
+
290
+ ---
291
+
292
+ ## 8. LNS8 -> linear形式
293
+
294
+ LNS8側は状態数が256しかないので、出力方向はdirect LUTが基本。
295
+
296
+ ### 256-entry版
297
+
298
+ ```text
299
+ LNS8 -> FP16 : 256 x 2 byte = 512 B
300
+ LNS8 -> BF16 : 256 x 2 byte = 512 B
301
+ LNS8 -> FP32 : 256 x 4 byte = 1 KiB
302
+ LNS8 -> Q9.6 : 256 x 2 byte = 512 B
303
+ ```
304
+
305
+ ### sign分離版
306
+
307
+ Magnitudeだけ128-entry LUTにし、signは出力のsign bitや符号反転で適用できる。
308
+
309
+ ```text
310
+ magnitude -> 128-entry positive LUT
311
+ sign -> XOR / sign-bit insert
312
+ ```
313
+
314
+ ### Q9.6への変換
315
+
316
+ Q9.6 raw magnitudeは
317
+
318
+ \[
319
+ q=\operatorname{round}\left(64\cdot2^{(k-74)/8}\right)
320
+ \]
321
+
322
+ だが、64=`2^6` なので
323
+
324
+ \[
325
+ \boxed{q=\operatorname{round}\left(2^{(k-26)/8}\right)}
326
+ \]
327
+
328
+ まで簡約できる。
329
+
330
+ anchor codeでは
331
+
332
+ ```text
333
+ code 26 -> raw 1
334
+ code 34 -> raw 2
335
+ code 42 -> raw 4
336
+ code 50 -> raw 8
337
+ code 58 -> raw 16
338
+ code 66 -> raw 32
339
+ code 74 -> raw 64
340
+ code 82 -> raw 128
341
+ code 90 -> raw 256
342
+ code 98 -> raw 512
343
+ code 106 -> raw 1024
344
+ code 114 -> raw 2048
345
+ code 122 -> raw 4096
346
+ code 127 -> raw about 6317
347
+ ```
348
+
349
+ となる。
350
+
351
+ ---
352
+
353
+ ## 9. LNS同士の「乗算」
354
+
355
+ 同じLNS gridなら
356
+
357
+ \[
358
+ x_a=2^{(k_a-74)/8},\qquad
359
+ x_b=2^{(k_b-74)/8}
360
+ \]
361
+
362
+ なので
363
+
364
+ \[
365
+ x_ax_b=2^{(k_a+k_b-148)/8}
366
+ \]
367
+
368
+ したがってproduct codeは
369
+
370
+ \[
371
+ \boxed{k_p=k_a+k_b-74}
372
+ \]
373
+
374
+ で**厳密**。
375
+
376
+ つまりMagnitudeの乗算は
377
+
378
+ \[
379
+ \boxed{\text{MUL}\rightarrow\text{integer ADD}}
380
+ \]
381
+
382
+ へ置換できる。
383
+
384
+ signは
385
+
386
+ \[
387
+ \boxed{s_p=s_a\oplus s_b}
388
+ \]
389
+
390
+ zeroはmask/skipで処理する。
391
+
392
+ weight/scale側をあらかじめ
393
+
394
+ \[
395
+ \Delta k=k_w-74
396
+ \]
397
+
398
+ として保存・展開できれば
399
+
400
+ ```text
401
+ activation code + signed log offset
402
+ ```
403
+
404
+ だけにできる。
405
+
406
+ **注意:** accumulation用のproduct codeは、保存用LNS8へ一度saturateせず、より広いsigned integerで保持する。先に `1..127` へclampするとZONE/octave情報が失われる。
407
+
408
+ ---
409
+
410
+ ## 10. Bonsai 2 ternaryの場合
411
+
412
+ 重みが
413
+
414
+ \[
415
+ w=t\,s,\qquad t\in\{-1,0,+1\}
416
+ \]
417
+
418
+ なら、group scale \(s\) をofflineでLNS offsetへ変換しておく。
419
+
420
+ ```text
421
+ t = 0
422
+ -> zero / skip
423
+
424
+ t = +1
425
+ -> signそのまま
426
+
427
+ t = -1
428
+ -> sign XOR
429
+ ```
430
+
431
+ Magnitude側は
432
+
433
+ \[
434
+ \boxed{k_p=k_a+k_s-74}
435
+ \]
436
+
437
+ またはscale側を中心化して
438
+
439
+ \[
440
+ \boxed{k_p=k_a+\Delta k_s}
441
+ \]
442
+
443
+ とできる。
444
+
445
+ runtimeの支配処理はADD、XOR、mask、shift、reductionへ寄せられる。
446
+
447
+ ---
448
+
449
+ ## 11. 8-ZONE疑似LNSネイティブ加算
450
+
451
+ この版の大きな目的は、`8 codes = x2` を利用して**下位3bitをそのまま加算ZONEにする**こと。
452
+
453
+ Magnitude code \(k\) を
454
+
455
+ \[
456
+ \boxed{k=8q+z},\qquad z=0\ldots7
457
+ \]
458
+
459
+ と分解する。
460
+
461
+ 実装上は
462
+
463
+ ```text
464
+ z = k & 7 # lower 3 bits = ZONE
465
+ q = k >> 3 # octave-side quotient
466
+ ```
467
+
468
+ でよい。
469
+
470
+ `base74` は `74 = 9*8 + 2` なので、数値は
471
+
472
+ \[
473
+ \begin{aligned}
474
+ |x|
475
+ &=2^{(8q+z-74)/8}\\
476
+ &=2^{q-9}\,2^{(z-2)/8}\\
477
+ &=\boxed{2^{-9}\,2^q\,M_z}
478
+ \end{aligned}
479
+ \]
480
+
481
+ ただし
482
+
483
+ \[
484
+ \boxed{M_z=2^{(z-2)/8}}
485
+ \]
486
+
487
+ で、\(M_z\) は8種類しかない。
488
+
489
+ ### 11.1 8本のinteger accumulator
490
+
491
+ 符号 \(s_i\in\{-1,+1\}\) として、ZONEごとに
492
+
493
+ \[
494
+ \boxed{A_z=\sum_{i:z_i=z} s_i\,2^{q_i}}
495
+ \]
496
+
497
+ をintegerで保持する。
498
+
499
+ 全体の和は最後に
500
+
501
+ \[
502
+ \boxed{S=2^{-9}\sum_{z=0}^7 M_z A_z}
503
+ \]
504
+
505
+ とすればよい。
506
+
507
+ したがって128項のdot productでも、各項で実数化する代わりに
508
+
509
+ ```text
510
+ product code
511
+ |
512
+ +-- lower 3bit -> ZONE 0..7
513
+ `-- upper part -> shift / octave
514
+ |
515
+ ACC[ZONE] += or -= (1 << shift)
516
+ ```
517
+
518
+ として集約し、最後に最大8本だけlinear/FPへ戻せる。
519
+
520
+ 量子化済みLNS値に対するこの分解自体は代数的に厳密であり、追加誤差は最後の係数 \(M_z\) の実装精度にのみ依存する。
521
+
522
+ ### 11.2 LNS乗算とZONE判定の融合
523
+
524
+ product codeを
525
+
526
+ \[
527
+ p=k_a+k_b-74
528
+ \]
529
+
530
+ とする。`p` は保存用7bit範囲外でもよい。
531
+
532
+ 数学的には
533
+
534
+ \[
535
+ q_p=\left\lfloor\frac{p}{8}\right\rfloor,\qquad
536
+ z_p=p-8q_p\in\{0,\ldots,7\}
537
+ \]
538
+
539
+ と分解する。
540
+
541
+ 2の補数かつ算術右shiftの実装では、実質的に
542
+
543
+ ```text
544
+ zone = p & 7
545
+ q = arithmetic_floor_shift(p, 3)
546
+ ```
547
+
548
+ として処理できる。
549
+
550
+ つまり**LNS productを作った直後の���位3bitが、そのまま次のaccumulator ZONEになる**。
551
+
552
+ ### 11.3 SIMD/GPU上の論点
553
+
554
+ 数値変換は単純になるが、最終的にはlaneごとの `ZONE -> accumulator` routing/reductionが必要。
555
+
556
+ 候補:
557
+
558
+ ```text
559
+ scalar CPU : acc[zone] indexed add
560
+ SSE/AVX2 : mask / shuffle / horizontal reduction
561
+ AVX-512 : mask + wider reduction
562
+ ARM NEON : table/mask reduction
563
+ CUDA : warp ballot / grouping / reduction
564
+ Vulkan : subgroup ballot/reduction、利用可能ならpartitioned subgroup
565
+ FPGA/ASIC : 3-to-8 decoder / demux
566
+ ```
567
+
568
+ 8 ZONEは2の冪なので、旧10-ZONE案の `/10`、`%10`、`>=10`補正を不要にできる点が重要。
569
+
570
+ ---
571
+
572
+ ### 11.3A Tile64 / Tile32 bitslice reference — Excel v0.8 procedure
573
+
574
+ correctness / bit-level implementation referenceとして、`LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx` の手順を採用する。
575
+
576
+ final product stateを:
577
+
578
+ ```text
579
+ S1 | E4 | L3
580
+ ```
581
+
582
+ とし、CPU Tile64では128項を `64 + 64`、GPU想定Tile32では `32 × 4` のようにword幅へ分割する。
583
+
584
+ bitslice / ballot後の主なbitplaneは:
585
+
586
+ ```text
587
+ L0 L1 L2 // phase / ZONE selector
588
+ E0 E1 E2 E3 // octave
589
+ SIGN // 1 = negative
590
+ ```
591
+
592
+ `L2,L1,L0`から生成する `phase mask` は、64/32 laneのうちどのlaneがZONE 0..7へ所属するかを示す**中間selector**である。最終数値ではなく、必要に応じてその場で生成して捨ててよい。
593
+
594
+ 例:
595
+
596
+ ```text
597
+ M[101] = L2 & ~L1 & L0
598
+ ```
599
+
600
+ 最終的に各laneの寄与は:
601
+
602
+ ```text
603
+ payload = sign × (1 << E4)
604
+ ACC[L3] += payload
605
+ ```
606
+
607
+ であり、8本の `ACC[0..7]` に縮約する。
608
+
609
+ このdirect pathはPOPCOUNTを必須としない。POPCOUNTを使う場合は、`E4`を失わないfull-state / `(phase, octave)` populationを作る必要がある。`POPCOUNT(phase_mask)`だけではoctave weight `2^E`を復元できない。
610
+
611
+ signed populationをbitsetで取る場合は:
612
+
613
+ ```text
614
+ P = POPCOUNT(M & ~SIGN)
615
+ N = POPCOUNT(M & SIGN)
616
+ C = P - N
617
+ ```
618
+
619
+ または:
620
+
621
+ ```text
622
+ T = POPCOUNT(M)
623
+ N = POPCOUNT(M & SIGN)
624
+ C = T - 2*N
625
+ ```
626
+
627
+ がExcelの `+ POPCOUNT - - POPCOUNT` とexactに同値である。
628
+
629
+ ### 11.3B Direct 8-accumulator and histogram equivalence
630
+
631
+ full signed histogramを:
632
+
633
+ ```text
634
+ H[z][q]
635
+ ```
636
+
637
+ とすると:
638
+
639
+ ```text
640
+ ACC[z] = Σ_q H[z][q] × 2^q
641
+ ```
642
+
643
+ である。したがって:
644
+
645
+ ```text
646
+ per-lane direct routing: ACC[z] += sign × (1 << q)
647
+ ```
648
+
649
+ と:
650
+
651
+ ```text
652
+ histogram -> signed_count << q -> ACC[z]
653
+ ```
654
+
655
+ は代数的に同じ結果になる。
656
+
657
+ GEMM/GEMVでK=4096 / 5120のdotを処理する場合も、全K product stateをこの8本へ縮約する。8本へ到達した後の実数化は最大8項なので、性能上の主要課題は**K項のproduct generationとK→8 weighted routing / reduction**である。
658
+
659
+ ---
660
+
661
+ ### 11.4 state-domain reductionの目的 — dependency breaking
662
+
663
+ 8-ZONE分解の目的は、K個のproduct contributionを最終的に8本のphase aggregateへ縮約することにある。v0.5では、`ACC[L3] += sign × (1<<E4)` のdirect pathを最も単純なcorrectness baselineとし、wide mask + POPCOUNT histogramはlane-wise dependencyをさらに切る別候補として扱う。
664
+
665
+ ```text
666
+ many final LNS product states
667
+ ↓
668
+ phase routing / state-set construction
669
+ ├─ direct ±(1<<E4) → 8 integer accumulators
670
+ └─ signed POPCOUNT histogram → 8 integer accumulators / pure-LNS
671
+ ↓
672
+ small final reduction
673
+ ```
674
+
675
+ これにより、128項を左から逐次127回加算するdependency chainではなく、独立したset/count処理と短い最終treeへ変換できる。
676
+
677
+ ### 11.5 population countをfractional-LNS stateへfoldする
678
+
679
+ state `k`のsigned populationを `C[k]` とする。`C[k] != 0` のとき、同じLNS値が `|C[k]|` 個あるので、その合計magnitudeはlog座標上で
680
+
681
+ \[
682
+ \boxed{\lambda_k = k + 8\log_2|C[k]|}
683
+ \]
684
+
685
+ と表せる。符号は `sign(C[k])`。
686
+
687
+ Tile128では `|C[k]|=1..128` しかないため、runtimeでは一般log演算ではなく、小さなcount-to-log LUTでよい。ここではlinear実数へ戻さない。
688
+
689
+ ### 11.6 120..127 anchorへのrelative normalization
690
+
691
+ 上位octave `q=15` の8 codeをphaseごとのanchorとして使える。
692
+
693
+ ```text
694
+ phase 000 -> code120
695
+ phase 001 -> code121
696
+ phase 010 -> code122 = 64 exact
697
+ phase 011 -> code123
698
+ phase 100 -> code124
699
+ phase 101 -> code125
700
+ phase 110 -> code126
701
+ phase 111 -> code127
702
+ ```
703
+
704
+ 任意のstateは
705
+
706
+ \[
707
+ \boxed{k=(120+z)+8(q-15)}
708
+ \]
709
+
710
+ なので、octave差はexactな整数code offset。`code122=64`を実数化する必要はなく、既知のrelative log位置として使えばよい。
711
+
712
+ ### 11.7 fractional-LNS add/sub
713
+
714
+ 内部ではinteger LNS8 codeへ毎回丸めず、codeの小数bitを保持する。
715
+
716
+ `a >= b`, `d=a-b`をLNS8 code単位とする。
717
+
718
+ 同符号:
719
+
720
+ \[
721
+ \boxed{a\oplus b=a+8\log_2(1+2^{-d/8})}
722
+ \]
723
+
724
+ 異符号、`d>0`:
725
+
726
+ \[
727
+ \boxed{a\ominus b=a+8\log_2(1-2^{-d/8})}
728
+ \]
729
+
730
+ 同じmagnitudeの���符号はexact zero。
731
+
732
+ runtimeでは `kappa_plus(d)` / `kappa_minus(d)` をsmall LUT化する。既存のPairwise_Addの整数threshold ruleは、結果を直ちにLNS8 integer codeへ丸める粗粒度版であり、pure-LNS reductionではより多いfractional bitsを保持する。
733
+
734
+ ### 11.8 balanced reduction tree
735
+
736
+ 8 phase aggregateを一つへまとめるときはserial accumulatorではなく
737
+
738
+ ```text
739
+ 8 -> 4 -> 2 -> 1
740
+ ```
741
+
742
+ のbalanced treeを使う。最終8-state reductionのdependent stageは3段。各phase内に複数band bucketが残る場合も同じprimitiveでbalancedに縮約する。
743
+
744
+ これによりBitStateの中心は
745
+
746
+ \[
747
+ \boxed{\text{arithmetic reduction} \rightarrow \text{state-set reduction} \rightarrow \text{short LNS tree}}
748
+ \]
749
+
750
+ となる。
751
+
752
+ ### 11.9 shared scaleもlog offsetとして適用可能
753
+
754
+ 正のgroup scale `d_s` は最終stateへ
755
+
756
+ \[
757
+ \boxed{\lambda_{scaled}=\lambda_{sum}+8\log_2 d_s}
758
+ \]
759
+
760
+ を加えればよい。converterでscale offsetを事前計算するか、group単位でcacheすれば、pure-LNS pathではlinear multiplyを不要にできる。
761
+
762
+ ### 11.10 二つのoptimized path
763
+
764
+ 実装では次を両方残して比較する。
765
+
766
+ **A. Dyadic reference path**
767
+
768
+ ```text
769
+ POPCOUNT histogram
770
+ -> signed count << band
771
+ -> 8 integer accumulators
772
+ -> fixed coefficient / fixed-point
773
+ -> Q9.6 or LNS8
774
+ ```
775
+
776
+ **B. Pure-LNS candidate path**
777
+
778
+ ```text
779
+ POPCOUNT histogram
780
+ -> count-to-log folding
781
+ -> relative fractional-LNS states
782
+ -> balanced kappa+/- tree
783
+ -> log-scale offset
784
+ -> final LNS8 rounding
785
+ ```
786
+
787
+ Aは非常にhardware-friendlyなreference。BはGEMV reduction中にlinear accumulatorを一度も作らない、より強い候補である。
788
+
789
+ ## 12. overflow / underflow
790
+
791
+ ### overflow
792
+
793
+ **保存用LNS8へ戻す場合**に `k>127` なら
794
+
795
+ \[
796
+ \boxed{k=127}
797
+ \]
798
+
799
+ へsaturateする。
800
+
801
+ ただし、8-ZONE accumulatorへ直接流すproduct中間値は、ZONE/octaveを失わないよう保存範囲へ先にsaturateしない。
802
+
803
+ ### underflow
804
+
805
+ wire/storage formatとしては従来通り
806
+
807
+ ```text
808
+ exact zero -> code 0
809
+ nonzero below range -> code 1
810
+ above range -> code 127
811
+ ```
812
+
813
+ を基本とする。
814
+
815
+ FP16/BF16/FP32 activationを量子化する実装では、極小値をcode1へ持ち上げる影響を避けるため、別途 `flush-to-zero threshold` を設けてもよい。このthresholdは**LNS8数値形式そのものではなくquantizer policy**として扱う。
816
+
817
+ Q9.6由来では最小nonzero `1/64` がcode26で表現できるため、下側rangeには十分余裕がある。
818
+
819
+ ---
820
+
821
+ ## 13. SWAR / SIMD実装上の性質
822
+
823
+ Magnitudeは7bit (`0..127`) なので、signを分離すれば8bit laneで扱いやすい。
824
+
825
+ ```text
826
+ stored LNS8
827
+ |
828
+ sign = bit7
829
+ mag = bit6..0
830
+ |
831
+ packed byte ADD / SWAR / SIMD
832
+ ```
833
+
834
+ さらに今回の形式では
835
+
836
+ ```text
837
+ lower 3 bits = ZONE
838
+ upper bits = octave-side field
839
+ ```
840
+
841
+ として直接利用できる。
842
+
843
+ 64bit scalar ALUなら8個のbyte lane、AVX2なら32個のbyte laneを並列処理できる。
844
+
845
+ 実装方針はハードごとに変更してよいが、数値仕様は共通にする。
846
+
847
+ ```text
848
+ scalar CPU : direct LUT / SWAR / 8-ZONE indexed accumulation
849
+ SSSE3/AVX2 : VPSHUFB / mask / packed integer ops
850
+ AVX-512 : wider SIMD + native mask
851
+ ARM NEON : saturating vector ops + table lookup
852
+ CUDA : SIMT / warp masks / integer reduction
853
+ Vulkan : integer ADD + bit extract + subgroup reduction
854
+ FPGA/ASIC : 3-to-8 decoder + shift + integer accumulators
855
+ ```
856
+
857
+ ---
858
+
859
+ ## 14. 数値表
860
+
861
+ `k=0` はexact zero。
862
+
863
+ 定義:
864
+
865
+ \[
866
+ \boxed{|x|=2^{(k-74)/8}}
867
+ \]
868
+
869
+ | code | 値 | code | 値 | code | 値 |
870
+ | ------:| ------------:| ------:| ------------:| -------:| -------------:|
871
+ | 1 | 0.001791 | 44 | 0.074325 | 87 | 3.084422 |
872
+ | **2** | **0.001953** | 45 | 0.081052 | 88 | 3.363586 |
873
+ | 3 | 0.002130 | 46 | 0.088388 | 89 | 3.668016 |
874
+ | 4 | 0.002323 | 47 | 0.096388 | **90** | **4.000000** |
875
+ | 5 | 0.002533 | 48 | 0.105112 | 91 | 4.362031 |
876
+ | 6 | 0.002762 | 49 | 0.114626 | 92 | 4.756828 |
877
+ | 7 | 0.003012 | **50** | **0.125000** | 93 | 5.187358 |
878
+ | 8 | 0.003285 | 51 | 0.136313 | 94 | 5.656854 |
879
+ | 9 | 0.003582 | 52 | 0.148651 | 95 | 6.168843 |
880
+ | **10** | **0.003906** | 53 | 0.162105 | 96 | 6.727171 |
881
+ | 11 | 0.004260 | 54 | 0.176777 | 97 | 7.336032 |
882
+ | 12 | 0.004645 | 55 | 0.192776 | **98** | **8.000000** |
883
+ | 13 | 0.005066 | 56 | 0.210224 | 99 | 8.724062 |
884
+ | 14 | 0.005524 | 57 | 0.229251 | 100 | 9.513657 |
885
+ | 15 | 0.006024 | **58** | **0.250000** | 101 | 10.374716 |
886
+ | 16 | 0.006570 | 59 | 0.272627 | 102 | 11.313708 |
887
+ | 17 | 0.007164 | 60 | 0.297302 | 103 | 12.337687 |
888
+ | **18** | **0.007812** | 61 | 0.324210 | 104 | 13.454343 |
889
+ | 19 | 0.008520 | 62 | 0.353553 | 105 | 14.672065 |
890
+ | 20 | 0.009291 | 63 | 0.385553 | **106** | **16.000000** |
891
+ | 21 | 0.010132 | 64 | 0.420448 | 107 | 17.448124 |
892
+ | 22 | 0.011049 | 65 | 0.458502 | 108 | 19.027314 |
893
+ | 23 | 0.012049 | **66** | **0.500000** | 109 | 20.749433 |
894
+ | 24 | 0.013139 | 67 | 0.545254 | 110 | 22.627417 |
895
+ | 25 | 0.014328 | 68 | 0.594604 | 111 | 24.675373 |
896
+ | **26** | **0.015625** | 69 | 0.648420 | 112 | 26.908685 |
897
+ | 27 | 0.017039 | 70 | 0.707107 | 113 | 29.344129 |
898
+ | 28 | 0.018581 | 71 | 0.771105 | **114** | **32.000000** |
899
+ | 29 | 0.020263 | 72 | 0.840896 | 115 | 34.896247 |
900
+ | 30 | 0.022097 | 73 | 0.917004 | 116 | 38.054628 |
901
+ | 31 | 0.024097 | **74** | **1.000000** | 117 | 41.498866 |
902
+ | 32 | 0.026278 | 75 | 1.090508 | 118 | 45.254834 |
903
+ | 33 | 0.028656 | 76 | 1.189207 | 119 | 49.350746 |
904
+ | **34** | **0.031250** | 77 | 1.296840 | 120 | 53.817371 |
905
+ | 35 | 0.034078 | 78 | 1.414214 | 121 | 58.688259 |
906
+ | 36 | 0.037163 | 79 | 1.542211 | **122** | **64.000000** |
907
+ | 37 | 0.040526 | 80 | 1.681793 | 123 | 69.792495 |
908
+ | 38 | 0.044194 | 81 | 1.834008 | 124 | 76.109255 |
909
+ | 39 | 0.048194 | **82** | **2.000000** | 125 | 82.997731 |
910
+ | 40 | 0.052556 | 83 | 2.181015 | 126 | 90.509668 |
911
+ | 41 | 0.057313 | 84 | 2.378414 | **127** | **98.701493** |
912
+ | **42** | **0.062500** | 85 | 2.593679 | | |
913
+ | 43 | 0.068157 | 86 | 2.828427 | | |
914
+
915
+ ---
916
+
917
+ ## 15. 重要anchor
918
+
919
+ | code | 値 |
920
+ | ----:| -----------------------:|
921
+ | 2 | **1/512 = 0.001953125** |
922
+ | 10 | **1/256 = 0.00390625** |
923
+ | 18 | **1/128 = 0.0078125** |
924
+ | 26 | **1/64 = 0.015625** |
925
+ | 34 | **1/32 = 0.03125** |
926
+ | 42 | **1/16 = 0.0625** |
927
+ | 50 | **1/8 = 0.125** |
928
+ | 58 | **1/4 = 0.25** |
929
+ | 66 | **1/2 = 0.5** |
930
+ | 74 | **1** |
931
+ | 82 | **2** |
932
+ | 90 | **4** |
933
+ | 98 | **8** |
934
+ | 106 | **16** |
935
+ | 114 | **32** |
936
+ | 122 | **64** |
937
+ | 127 | 約 **98.701493** |
938
+
939
+ 覚え方は
940
+
941
+ \[
942
+ \boxed{\textbf{「74が1、8 codeで厳密に2倍」}}
943
+ \]
944
+
945
+ でよい。
946
+
947
+ また、`74 mod 8 = 2` なので、2の整数冪anchorはすべて
948
+
949
+ \[
950
+ \boxed{ZONE=2}
951
+ \]
952
+
953
+ に並ぶ。
954
+
955
+ ---
956
+
957
+ ## 16. 実装思想
958
+
959
+ このLNS8は万能数値形式ではなく、主に
960
+
961
+ - hidden activationの保存・搬送
962
+ - low-bit / ternary weightとの積生成
963
+ - GEMM/GEMV支配部のMUL削減
964
+ - 既存CPU/GPU上でのinteger/SIMD化
965
+
966
+ に使う。
967
+
968
+ ### Reference path
969
+
970
+ 最初のPoCや品質確認では、各productをlinear integerへ戻して通常のaccumulationを行う。
971
+
972
+ ```text
973
+ LNS8 / low-bit weight
974
+ |
975
+ log-code ADD / XOR / mask
976
+ |
977
+ LNS -> Q9.6 / INT LUT
978
+ |
979
+ INT16 product
980
+ |
981
+ INT32 accumulation
982
+ ```
983
+
984
+ ### Optimized 8-ZONE / state-domain paths
985
+
986
+ 高速化版ではproductを実数化せず、下位3bitで8 phaseへ分類する。最も単純なexact pathは `±(1<<E4)` を8本のinteger accumulatorへ直接routeする方式で、POPCOUNT histogramはdependency breakingを強める別候補として比較する。
987
+
988
+ ```text
989
+ LNS8 / low-bit weight
990
+ ↓
991
+ integer ADD / XOR
992
+ ↓
993
+ final product state = S1|E4|L3
994
+ ↓
995
+ phase routing / state masks
996
+ ├─ direct ±(1<<E4) → 8 integer accumulators
997
+ └─ signed POPCOUNT histogram
998
+ ├─ dyadic → same 8 accumulators
999
+ └─ pure fractional-LNS tree
1000
+ ```
1001
+
1002
+ 128項、あるいは実GEMM/GEMVで4096/5120項のdotを、最終的に8本のphase aggregateへ縮約することが目的である。direct pathでは8本の独立integer accumulator、population pathではstate集合の並列集計を使い、最終8項の実体化は小さなreductionとして扱う。
1003
+
1004
+ RMSNorm、SiLU/GELU、Softmax、RoPEなど、GEMV/GEMM支配部以外の複雑な非線形処理は必要に応じてFP16/BF16/FP32へ展開してよい。
1005
+
1006
+ \[
1007
+ \boxed{
1008
+ \textbf{LNS8は保存・搬送・積生成に使い、\\
1009
+ state-domain POPCOUNTでreduction dependencyを先に切り、\\
1010
+ 最終縮約はdyadicまたはpure-LNSの短いtreeへ寄せる。}
1011
+ }
1012
+ \]
Bitstate/bitstate_poc_test_vectors_v3.json ADDED
@@ -0,0 +1,3388 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "format": "BitState/HSDR correctness vectors v3 — direct 8-phase + histogram equivalence",
3
+ "lns8": {
4
+ "base_code": 74,
5
+ "codes_per_octave": 8,
6
+ "zero_code": 0,
7
+ "magnitude_formula": "2^((code-74)/8)",
8
+ "sign_encoding": "separate +/-1"
9
+ },
10
+ "q9_6_rounding": "round half away from zero after multiplying by 64",
11
+ "histogram_reconstruction": "R[p]=sum_o H[p][o]*2^o; S=scale*2^-9*sum_p R[p]*2^((p-2)/8)",
12
+ "cases": [
13
+ {
14
+ "name": "all_unity_positive",
15
+ "K": 128,
16
+ "scale": 1.0,
17
+ "notes": "All activations are +1.0, all ternary weights +1. Expected sum 128.",
18
+ "activation_codes": [
19
+ 74,
20
+ 74,
21
+ 74,
22
+ 74,
23
+ 74,
24
+ 74,
25
+ 74,
26
+ 74,
27
+ 74,
28
+ 74,
29
+ 74,
30
+ 74,
31
+ 74,
32
+ 74,
33
+ 74,
34
+ 74,
35
+ 74,
36
+ 74,
37
+ 74,
38
+ 74,
39
+ 74,
40
+ 74,
41
+ 74,
42
+ 74,
43
+ 74,
44
+ 74,
45
+ 74,
46
+ 74,
47
+ 74,
48
+ 74,
49
+ 74,
50
+ 74,
51
+ 74,
52
+ 74,
53
+ 74,
54
+ 74,
55
+ 74,
56
+ 74,
57
+ 74,
58
+ 74,
59
+ 74,
60
+ 74,
61
+ 74,
62
+ 74,
63
+ 74,
64
+ 74,
65
+ 74,
66
+ 74,
67
+ 74,
68
+ 74,
69
+ 74,
70
+ 74,
71
+ 74,
72
+ 74,
73
+ 74,
74
+ 74,
75
+ 74,
76
+ 74,
77
+ 74,
78
+ 74,
79
+ 74,
80
+ 74,
81
+ 74,
82
+ 74,
83
+ 74,
84
+ 74,
85
+ 74,
86
+ 74,
87
+ 74,
88
+ 74,
89
+ 74,
90
+ 74,
91
+ 74,
92
+ 74,
93
+ 74,
94
+ 74,
95
+ 74,
96
+ 74,
97
+ 74,
98
+ 74,
99
+ 74,
100
+ 74,
101
+ 74,
102
+ 74,
103
+ 74,
104
+ 74,
105
+ 74,
106
+ 74,
107
+ 74,
108
+ 74,
109
+ 74,
110
+ 74,
111
+ 74,
112
+ 74,
113
+ 74,
114
+ 74,
115
+ 74,
116
+ 74,
117
+ 74,
118
+ 74,
119
+ 74,
120
+ 74,
121
+ 74,
122
+ 74,
123
+ 74,
124
+ 74,
125
+ 74,
126
+ 74,
127
+ 74,
128
+ 74,
129
+ 74,
130
+ 74,
131
+ 74,
132
+ 74,
133
+ 74,
134
+ 74,
135
+ 74,
136
+ 74,
137
+ 74,
138
+ 74,
139
+ 74,
140
+ 74,
141
+ 74,
142
+ 74,
143
+ 74,
144
+ 74,
145
+ 74,
146
+ 74
147
+ ],
148
+ "activation_signs": [
149
+ 1,
150
+ 1,
151
+ 1,
152
+ 1,
153
+ 1,
154
+ 1,
155
+ 1,
156
+ 1,
157
+ 1,
158
+ 1,
159
+ 1,
160
+ 1,
161
+ 1,
162
+ 1,
163
+ 1,
164
+ 1,
165
+ 1,
166
+ 1,
167
+ 1,
168
+ 1,
169
+ 1,
170
+ 1,
171
+ 1,
172
+ 1,
173
+ 1,
174
+ 1,
175
+ 1,
176
+ 1,
177
+ 1,
178
+ 1,
179
+ 1,
180
+ 1,
181
+ 1,
182
+ 1,
183
+ 1,
184
+ 1,
185
+ 1,
186
+ 1,
187
+ 1,
188
+ 1,
189
+ 1,
190
+ 1,
191
+ 1,
192
+ 1,
193
+ 1,
194
+ 1,
195
+ 1,
196
+ 1,
197
+ 1,
198
+ 1,
199
+ 1,
200
+ 1,
201
+ 1,
202
+ 1,
203
+ 1,
204
+ 1,
205
+ 1,
206
+ 1,
207
+ 1,
208
+ 1,
209
+ 1,
210
+ 1,
211
+ 1,
212
+ 1,
213
+ 1,
214
+ 1,
215
+ 1,
216
+ 1,
217
+ 1,
218
+ 1,
219
+ 1,
220
+ 1,
221
+ 1,
222
+ 1,
223
+ 1,
224
+ 1,
225
+ 1,
226
+ 1,
227
+ 1,
228
+ 1,
229
+ 1,
230
+ 1,
231
+ 1,
232
+ 1,
233
+ 1,
234
+ 1,
235
+ 1,
236
+ 1,
237
+ 1,
238
+ 1,
239
+ 1,
240
+ 1,
241
+ 1,
242
+ 1,
243
+ 1,
244
+ 1,
245
+ 1,
246
+ 1,
247
+ 1,
248
+ 1,
249
+ 1,
250
+ 1,
251
+ 1,
252
+ 1,
253
+ 1,
254
+ 1,
255
+ 1,
256
+ 1,
257
+ 1,
258
+ 1,
259
+ 1,
260
+ 1,
261
+ 1,
262
+ 1,
263
+ 1,
264
+ 1,
265
+ 1,
266
+ 1,
267
+ 1,
268
+ 1,
269
+ 1,
270
+ 1,
271
+ 1,
272
+ 1,
273
+ 1,
274
+ 1,
275
+ 1,
276
+ 1
277
+ ],
278
+ "ternary_weights": [
279
+ 1,
280
+ 1,
281
+ 1,
282
+ 1,
283
+ 1,
284
+ 1,
285
+ 1,
286
+ 1,
287
+ 1,
288
+ 1,
289
+ 1,
290
+ 1,
291
+ 1,
292
+ 1,
293
+ 1,
294
+ 1,
295
+ 1,
296
+ 1,
297
+ 1,
298
+ 1,
299
+ 1,
300
+ 1,
301
+ 1,
302
+ 1,
303
+ 1,
304
+ 1,
305
+ 1,
306
+ 1,
307
+ 1,
308
+ 1,
309
+ 1,
310
+ 1,
311
+ 1,
312
+ 1,
313
+ 1,
314
+ 1,
315
+ 1,
316
+ 1,
317
+ 1,
318
+ 1,
319
+ 1,
320
+ 1,
321
+ 1,
322
+ 1,
323
+ 1,
324
+ 1,
325
+ 1,
326
+ 1,
327
+ 1,
328
+ 1,
329
+ 1,
330
+ 1,
331
+ 1,
332
+ 1,
333
+ 1,
334
+ 1,
335
+ 1,
336
+ 1,
337
+ 1,
338
+ 1,
339
+ 1,
340
+ 1,
341
+ 1,
342
+ 1,
343
+ 1,
344
+ 1,
345
+ 1,
346
+ 1,
347
+ 1,
348
+ 1,
349
+ 1,
350
+ 1,
351
+ 1,
352
+ 1,
353
+ 1,
354
+ 1,
355
+ 1,
356
+ 1,
357
+ 1,
358
+ 1,
359
+ 1,
360
+ 1,
361
+ 1,
362
+ 1,
363
+ 1,
364
+ 1,
365
+ 1,
366
+ 1,
367
+ 1,
368
+ 1,
369
+ 1,
370
+ 1,
371
+ 1,
372
+ 1,
373
+ 1,
374
+ 1,
375
+ 1,
376
+ 1,
377
+ 1,
378
+ 1,
379
+ 1,
380
+ 1,
381
+ 1,
382
+ 1,
383
+ 1,
384
+ 1,
385
+ 1,
386
+ 1,
387
+ 1,
388
+ 1,
389
+ 1,
390
+ 1,
391
+ 1,
392
+ 1,
393
+ 1,
394
+ 1,
395
+ 1,
396
+ 1,
397
+ 1,
398
+ 1,
399
+ 1,
400
+ 1,
401
+ 1,
402
+ 1,
403
+ 1,
404
+ 1,
405
+ 1,
406
+ 1
407
+ ],
408
+ "expected_direct_fp64": 128.0,
409
+ "expected_histogram_fp64": 128.0,
410
+ "expected_abs_difference": 0.0,
411
+ "expected_q9_6_raw_lround": 8192,
412
+ "expected_q9_6_value_lround": 128.0,
413
+ "phase_integer_reduction_R": [
414
+ 0,
415
+ 0,
416
+ 65536,
417
+ 0,
418
+ 0,
419
+ 0,
420
+ 0,
421
+ 0
422
+ ],
423
+ "state_domain_v2": {
424
+ "nonzero_signed_state_buckets": 1,
425
+ "balanced_global_tree_depth_upper_bound": 0,
426
+ "population_buckets": [
427
+ {
428
+ "code": 74,
429
+ "phase": 2,
430
+ "band": 9,
431
+ "signed_count": 128,
432
+ "sign": 1,
433
+ "population_log_offset_code_units": 56.0,
434
+ "fractional_lns_code": 130.0
435
+ }
436
+ ],
437
+ "phase_fractional_lns_reference": [
438
+ {
439
+ "phase": 0,
440
+ "sign": 0,
441
+ "fractional_lns_code_unscaled": null
442
+ },
443
+ {
444
+ "phase": 1,
445
+ "sign": 0,
446
+ "fractional_lns_code_unscaled": null
447
+ },
448
+ {
449
+ "phase": 2,
450
+ "sign": 1,
451
+ "fractional_lns_code_unscaled": 130.0
452
+ },
453
+ {
454
+ "phase": 3,
455
+ "sign": 0,
456
+ "fractional_lns_code_unscaled": null
457
+ },
458
+ {
459
+ "phase": 4,
460
+ "sign": 0,
461
+ "fractional_lns_code_unscaled": null
462
+ },
463
+ {
464
+ "phase": 5,
465
+ "sign": 0,
466
+ "fractional_lns_code_unscaled": null
467
+ },
468
+ {
469
+ "phase": 6,
470
+ "sign": 0,
471
+ "fractional_lns_code_unscaled": null
472
+ },
473
+ {
474
+ "phase": 7,
475
+ "sign": 0,
476
+ "fractional_lns_code_unscaled": null
477
+ }
478
+ ],
479
+ "expected_final_sign": 1,
480
+ "expected_final_fractional_lns_code": 130.0,
481
+ "expected_nearest_lns8_magnitude_code": 127,
482
+ "note": "fractional_lns_code fields are correctness references from exact math; runtime candidate uses count-log and kappa LUTs without linear materialization"
483
+ },
484
+ "expected_direct_8phase_accumulators": [
485
+ 0,
486
+ 0,
487
+ 65536,
488
+ 0,
489
+ 0,
490
+ 0,
491
+ 0,
492
+ 0
493
+ ],
494
+ "direct_8phase_note": "Same 8 integers as histogram sum_o H[phase][o]<<o; intended as exact dyadic routing reference."
495
+ },
496
+ {
497
+ "name": "alternating_weight_cancel",
498
+ "K": 128,
499
+ "scale": 1.0,
500
+ "notes": "Exact cancellation with unit activations.",
501
+ "activation_codes": [
502
+ 74,
503
+ 74,
504
+ 74,
505
+ 74,
506
+ 74,
507
+ 74,
508
+ 74,
509
+ 74,
510
+ 74,
511
+ 74,
512
+ 74,
513
+ 74,
514
+ 74,
515
+ 74,
516
+ 74,
517
+ 74,
518
+ 74,
519
+ 74,
520
+ 74,
521
+ 74,
522
+ 74,
523
+ 74,
524
+ 74,
525
+ 74,
526
+ 74,
527
+ 74,
528
+ 74,
529
+ 74,
530
+ 74,
531
+ 74,
532
+ 74,
533
+ 74,
534
+ 74,
535
+ 74,
536
+ 74,
537
+ 74,
538
+ 74,
539
+ 74,
540
+ 74,
541
+ 74,
542
+ 74,
543
+ 74,
544
+ 74,
545
+ 74,
546
+ 74,
547
+ 74,
548
+ 74,
549
+ 74,
550
+ 74,
551
+ 74,
552
+ 74,
553
+ 74,
554
+ 74,
555
+ 74,
556
+ 74,
557
+ 74,
558
+ 74,
559
+ 74,
560
+ 74,
561
+ 74,
562
+ 74,
563
+ 74,
564
+ 74,
565
+ 74,
566
+ 74,
567
+ 74,
568
+ 74,
569
+ 74,
570
+ 74,
571
+ 74,
572
+ 74,
573
+ 74,
574
+ 74,
575
+ 74,
576
+ 74,
577
+ 74,
578
+ 74,
579
+ 74,
580
+ 74,
581
+ 74,
582
+ 74,
583
+ 74,
584
+ 74,
585
+ 74,
586
+ 74,
587
+ 74,
588
+ 74,
589
+ 74,
590
+ 74,
591
+ 74,
592
+ 74,
593
+ 74,
594
+ 74,
595
+ 74,
596
+ 74,
597
+ 74,
598
+ 74,
599
+ 74,
600
+ 74,
601
+ 74,
602
+ 74,
603
+ 74,
604
+ 74,
605
+ 74,
606
+ 74,
607
+ 74,
608
+ 74,
609
+ 74,
610
+ 74,
611
+ 74,
612
+ 74,
613
+ 74,
614
+ 74,
615
+ 74,
616
+ 74,
617
+ 74,
618
+ 74,
619
+ 74,
620
+ 74,
621
+ 74,
622
+ 74,
623
+ 74,
624
+ 74,
625
+ 74,
626
+ 74,
627
+ 74,
628
+ 74,
629
+ 74
630
+ ],
631
+ "activation_signs": [
632
+ 1,
633
+ 1,
634
+ 1,
635
+ 1,
636
+ 1,
637
+ 1,
638
+ 1,
639
+ 1,
640
+ 1,
641
+ 1,
642
+ 1,
643
+ 1,
644
+ 1,
645
+ 1,
646
+ 1,
647
+ 1,
648
+ 1,
649
+ 1,
650
+ 1,
651
+ 1,
652
+ 1,
653
+ 1,
654
+ 1,
655
+ 1,
656
+ 1,
657
+ 1,
658
+ 1,
659
+ 1,
660
+ 1,
661
+ 1,
662
+ 1,
663
+ 1,
664
+ 1,
665
+ 1,
666
+ 1,
667
+ 1,
668
+ 1,
669
+ 1,
670
+ 1,
671
+ 1,
672
+ 1,
673
+ 1,
674
+ 1,
675
+ 1,
676
+ 1,
677
+ 1,
678
+ 1,
679
+ 1,
680
+ 1,
681
+ 1,
682
+ 1,
683
+ 1,
684
+ 1,
685
+ 1,
686
+ 1,
687
+ 1,
688
+ 1,
689
+ 1,
690
+ 1,
691
+ 1,
692
+ 1,
693
+ 1,
694
+ 1,
695
+ 1,
696
+ 1,
697
+ 1,
698
+ 1,
699
+ 1,
700
+ 1,
701
+ 1,
702
+ 1,
703
+ 1,
704
+ 1,
705
+ 1,
706
+ 1,
707
+ 1,
708
+ 1,
709
+ 1,
710
+ 1,
711
+ 1,
712
+ 1,
713
+ 1,
714
+ 1,
715
+ 1,
716
+ 1,
717
+ 1,
718
+ 1,
719
+ 1,
720
+ 1,
721
+ 1,
722
+ 1,
723
+ 1,
724
+ 1,
725
+ 1,
726
+ 1,
727
+ 1,
728
+ 1,
729
+ 1,
730
+ 1,
731
+ 1,
732
+ 1,
733
+ 1,
734
+ 1,
735
+ 1,
736
+ 1,
737
+ 1,
738
+ 1,
739
+ 1,
740
+ 1,
741
+ 1,
742
+ 1,
743
+ 1,
744
+ 1,
745
+ 1,
746
+ 1,
747
+ 1,
748
+ 1,
749
+ 1,
750
+ 1,
751
+ 1,
752
+ 1,
753
+ 1,
754
+ 1,
755
+ 1,
756
+ 1,
757
+ 1,
758
+ 1,
759
+ 1
760
+ ],
761
+ "ternary_weights": [
762
+ 1,
763
+ -1,
764
+ 1,
765
+ -1,
766
+ 1,
767
+ -1,
768
+ 1,
769
+ -1,
770
+ 1,
771
+ -1,
772
+ 1,
773
+ -1,
774
+ 1,
775
+ -1,
776
+ 1,
777
+ -1,
778
+ 1,
779
+ -1,
780
+ 1,
781
+ -1,
782
+ 1,
783
+ -1,
784
+ 1,
785
+ -1,
786
+ 1,
787
+ -1,
788
+ 1,
789
+ -1,
790
+ 1,
791
+ -1,
792
+ 1,
793
+ -1,
794
+ 1,
795
+ -1,
796
+ 1,
797
+ -1,
798
+ 1,
799
+ -1,
800
+ 1,
801
+ -1,
802
+ 1,
803
+ -1,
804
+ 1,
805
+ -1,
806
+ 1,
807
+ -1,
808
+ 1,
809
+ -1,
810
+ 1,
811
+ -1,
812
+ 1,
813
+ -1,
814
+ 1,
815
+ -1,
816
+ 1,
817
+ -1,
818
+ 1,
819
+ -1,
820
+ 1,
821
+ -1,
822
+ 1,
823
+ -1,
824
+ 1,
825
+ -1,
826
+ 1,
827
+ -1,
828
+ 1,
829
+ -1,
830
+ 1,
831
+ -1,
832
+ 1,
833
+ -1,
834
+ 1,
835
+ -1,
836
+ 1,
837
+ -1,
838
+ 1,
839
+ -1,
840
+ 1,
841
+ -1,
842
+ 1,
843
+ -1,
844
+ 1,
845
+ -1,
846
+ 1,
847
+ -1,
848
+ 1,
849
+ -1,
850
+ 1,
851
+ -1,
852
+ 1,
853
+ -1,
854
+ 1,
855
+ -1,
856
+ 1,
857
+ -1,
858
+ 1,
859
+ -1,
860
+ 1,
861
+ -1,
862
+ 1,
863
+ -1,
864
+ 1,
865
+ -1,
866
+ 1,
867
+ -1,
868
+ 1,
869
+ -1,
870
+ 1,
871
+ -1,
872
+ 1,
873
+ -1,
874
+ 1,
875
+ -1,
876
+ 1,
877
+ -1,
878
+ 1,
879
+ -1,
880
+ 1,
881
+ -1,
882
+ 1,
883
+ -1,
884
+ 1,
885
+ -1,
886
+ 1,
887
+ -1,
888
+ 1,
889
+ -1
890
+ ],
891
+ "expected_direct_fp64": 0.0,
892
+ "expected_histogram_fp64": 0.0,
893
+ "expected_abs_difference": 0.0,
894
+ "expected_q9_6_raw_lround": 0,
895
+ "expected_q9_6_value_lround": 0.0,
896
+ "phase_integer_reduction_R": [
897
+ 0,
898
+ 0,
899
+ 0,
900
+ 0,
901
+ 0,
902
+ 0,
903
+ 0,
904
+ 0
905
+ ],
906
+ "state_domain_v2": {
907
+ "nonzero_signed_state_buckets": 0,
908
+ "balanced_global_tree_depth_upper_bound": 0,
909
+ "population_buckets": [],
910
+ "phase_fractional_lns_reference": [
911
+ {
912
+ "phase": 0,
913
+ "sign": 0,
914
+ "fractional_lns_code_unscaled": null
915
+ },
916
+ {
917
+ "phase": 1,
918
+ "sign": 0,
919
+ "fractional_lns_code_unscaled": null
920
+ },
921
+ {
922
+ "phase": 2,
923
+ "sign": 0,
924
+ "fractional_lns_code_unscaled": null
925
+ },
926
+ {
927
+ "phase": 3,
928
+ "sign": 0,
929
+ "fractional_lns_code_unscaled": null
930
+ },
931
+ {
932
+ "phase": 4,
933
+ "sign": 0,
934
+ "fractional_lns_code_unscaled": null
935
+ },
936
+ {
937
+ "phase": 5,
938
+ "sign": 0,
939
+ "fractional_lns_code_unscaled": null
940
+ },
941
+ {
942
+ "phase": 6,
943
+ "sign": 0,
944
+ "fractional_lns_code_unscaled": null
945
+ },
946
+ {
947
+ "phase": 7,
948
+ "sign": 0,
949
+ "fractional_lns_code_unscaled": null
950
+ }
951
+ ],
952
+ "expected_final_sign": 0,
953
+ "expected_final_fractional_lns_code": null,
954
+ "expected_nearest_lns8_magnitude_code": 0,
955
+ "note": "fractional_lns_code fields are correctness references from exact math; runtime candidate uses count-log and kappa LUTs without linear materialization"
956
+ },
957
+ "expected_direct_8phase_accumulators": [
958
+ 0,
959
+ 0,
960
+ 0,
961
+ 0,
962
+ 0,
963
+ 0,
964
+ 0,
965
+ 0
966
+ ],
967
+ "direct_8phase_note": "Same 8 integers as histogram sum_o H[phase][o]<<o; intended as exact dyadic routing reference."
968
+ },
969
+ {
970
+ "name": "weight_zeros_and_signs",
971
+ "K": 128,
972
+ "scale": 0.75,
973
+ "notes": "Mixture of 0.5/2.0 activation magnitudes, signs, and zero ternary weights.",
974
+ "activation_codes": [
975
+ 66,
976
+ 82,
977
+ 66,
978
+ 82,
979
+ 66,
980
+ 82,
981
+ 66,
982
+ 82,
983
+ 66,
984
+ 82,
985
+ 66,
986
+ 82,
987
+ 66,
988
+ 82,
989
+ 66,
990
+ 82,
991
+ 66,
992
+ 82,
993
+ 66,
994
+ 82,
995
+ 66,
996
+ 82,
997
+ 66,
998
+ 82,
999
+ 66,
1000
+ 82,
1001
+ 66,
1002
+ 82,
1003
+ 66,
1004
+ 82,
1005
+ 66,
1006
+ 82,
1007
+ 66,
1008
+ 82,
1009
+ 66,
1010
+ 82,
1011
+ 66,
1012
+ 82,
1013
+ 66,
1014
+ 82,
1015
+ 66,
1016
+ 82,
1017
+ 66,
1018
+ 82,
1019
+ 66,
1020
+ 82,
1021
+ 66,
1022
+ 82,
1023
+ 66,
1024
+ 82,
1025
+ 66,
1026
+ 82,
1027
+ 66,
1028
+ 82,
1029
+ 66,
1030
+ 82,
1031
+ 66,
1032
+ 82,
1033
+ 66,
1034
+ 82,
1035
+ 66,
1036
+ 82,
1037
+ 66,
1038
+ 82,
1039
+ 66,
1040
+ 82,
1041
+ 66,
1042
+ 82,
1043
+ 66,
1044
+ 82,
1045
+ 66,
1046
+ 82,
1047
+ 66,
1048
+ 82,
1049
+ 66,
1050
+ 82,
1051
+ 66,
1052
+ 82,
1053
+ 66,
1054
+ 82,
1055
+ 66,
1056
+ 82,
1057
+ 66,
1058
+ 82,
1059
+ 66,
1060
+ 82,
1061
+ 66,
1062
+ 82,
1063
+ 66,
1064
+ 82,
1065
+ 66,
1066
+ 82,
1067
+ 66,
1068
+ 82,
1069
+ 66,
1070
+ 82,
1071
+ 66,
1072
+ 82,
1073
+ 66,
1074
+ 82,
1075
+ 66,
1076
+ 82,
1077
+ 66,
1078
+ 82,
1079
+ 66,
1080
+ 82,
1081
+ 66,
1082
+ 82,
1083
+ 66,
1084
+ 82,
1085
+ 66,
1086
+ 82,
1087
+ 66,
1088
+ 82,
1089
+ 66,
1090
+ 82,
1091
+ 66,
1092
+ 82,
1093
+ 66,
1094
+ 82,
1095
+ 66,
1096
+ 82,
1097
+ 66,
1098
+ 82,
1099
+ 66,
1100
+ 82,
1101
+ 66,
1102
+ 82
1103
+ ],
1104
+ "activation_signs": [
1105
+ -1,
1106
+ 1,
1107
+ 1,
1108
+ -1,
1109
+ 1,
1110
+ 1,
1111
+ -1,
1112
+ 1,
1113
+ 1,
1114
+ -1,
1115
+ 1,
1116
+ 1,
1117
+ -1,
1118
+ 1,
1119
+ 1,
1120
+ -1,
1121
+ 1,
1122
+ 1,
1123
+ -1,
1124
+ 1,
1125
+ 1,
1126
+ -1,
1127
+ 1,
1128
+ 1,
1129
+ -1,
1130
+ 1,
1131
+ 1,
1132
+ -1,
1133
+ 1,
1134
+ 1,
1135
+ -1,
1136
+ 1,
1137
+ 1,
1138
+ -1,
1139
+ 1,
1140
+ 1,
1141
+ -1,
1142
+ 1,
1143
+ 1,
1144
+ -1,
1145
+ 1,
1146
+ 1,
1147
+ -1,
1148
+ 1,
1149
+ 1,
1150
+ -1,
1151
+ 1,
1152
+ 1,
1153
+ -1,
1154
+ 1,
1155
+ 1,
1156
+ -1,
1157
+ 1,
1158
+ 1,
1159
+ -1,
1160
+ 1,
1161
+ 1,
1162
+ -1,
1163
+ 1,
1164
+ 1,
1165
+ -1,
1166
+ 1,
1167
+ 1,
1168
+ -1,
1169
+ 1,
1170
+ 1,
1171
+ -1,
1172
+ 1,
1173
+ 1,
1174
+ -1,
1175
+ 1,
1176
+ 1,
1177
+ -1,
1178
+ 1,
1179
+ 1,
1180
+ -1,
1181
+ 1,
1182
+ 1,
1183
+ -1,
1184
+ 1,
1185
+ 1,
1186
+ -1,
1187
+ 1,
1188
+ 1,
1189
+ -1,
1190
+ 1,
1191
+ 1,
1192
+ -1,
1193
+ 1,
1194
+ 1,
1195
+ -1,
1196
+ 1,
1197
+ 1,
1198
+ -1,
1199
+ 1,
1200
+ 1,
1201
+ -1,
1202
+ 1,
1203
+ 1,
1204
+ -1,
1205
+ 1,
1206
+ 1,
1207
+ -1,
1208
+ 1,
1209
+ 1,
1210
+ -1,
1211
+ 1,
1212
+ 1,
1213
+ -1,
1214
+ 1,
1215
+ 1,
1216
+ -1,
1217
+ 1,
1218
+ 1,
1219
+ -1,
1220
+ 1,
1221
+ 1,
1222
+ -1,
1223
+ 1,
1224
+ 1,
1225
+ -1,
1226
+ 1,
1227
+ 1,
1228
+ -1,
1229
+ 1,
1230
+ 1,
1231
+ -1,
1232
+ 1
1233
+ ],
1234
+ "ternary_weights": [
1235
+ -1,
1236
+ 0,
1237
+ 1,
1238
+ -1,
1239
+ 0,
1240
+ 1,
1241
+ -1,
1242
+ 0,
1243
+ 1,
1244
+ -1,
1245
+ 0,
1246
+ 1,
1247
+ -1,
1248
+ 0,
1249
+ 1,
1250
+ -1,
1251
+ 0,
1252
+ 1,
1253
+ -1,
1254
+ 0,
1255
+ 1,
1256
+ -1,
1257
+ 0,
1258
+ 1,
1259
+ -1,
1260
+ 0,
1261
+ 1,
1262
+ -1,
1263
+ 0,
1264
+ 1,
1265
+ -1,
1266
+ 0,
1267
+ 1,
1268
+ -1,
1269
+ 0,
1270
+ 1,
1271
+ -1,
1272
+ 0,
1273
+ 1,
1274
+ -1,
1275
+ 0,
1276
+ 1,
1277
+ -1,
1278
+ 0,
1279
+ 1,
1280
+ -1,
1281
+ 0,
1282
+ 1,
1283
+ -1,
1284
+ 0,
1285
+ 1,
1286
+ -1,
1287
+ 0,
1288
+ 1,
1289
+ -1,
1290
+ 0,
1291
+ 1,
1292
+ -1,
1293
+ 0,
1294
+ 1,
1295
+ -1,
1296
+ 0,
1297
+ 1,
1298
+ -1,
1299
+ 0,
1300
+ 1,
1301
+ -1,
1302
+ 0,
1303
+ 1,
1304
+ -1,
1305
+ 0,
1306
+ 1,
1307
+ -1,
1308
+ 0,
1309
+ 1,
1310
+ -1,
1311
+ 0,
1312
+ 1,
1313
+ -1,
1314
+ 0,
1315
+ 1,
1316
+ -1,
1317
+ 0,
1318
+ 1,
1319
+ -1,
1320
+ 0,
1321
+ 1,
1322
+ -1,
1323
+ 0,
1324
+ 1,
1325
+ -1,
1326
+ 0,
1327
+ 1,
1328
+ -1,
1329
+ 0,
1330
+ 1,
1331
+ -1,
1332
+ 0,
1333
+ 1,
1334
+ -1,
1335
+ 0,
1336
+ 1,
1337
+ -1,
1338
+ 0,
1339
+ 1,
1340
+ -1,
1341
+ 0,
1342
+ 1,
1343
+ -1,
1344
+ 0,
1345
+ 1,
1346
+ -1,
1347
+ 0,
1348
+ 1,
1349
+ -1,
1350
+ 0,
1351
+ 1,
1352
+ -1,
1353
+ 0,
1354
+ 1,
1355
+ -1,
1356
+ 0,
1357
+ 1,
1358
+ -1,
1359
+ 0,
1360
+ 1,
1361
+ -1,
1362
+ 0
1363
+ ],
1364
+ "expected_direct_fp64": 79.125,
1365
+ "expected_histogram_fp64": 79.125,
1366
+ "expected_abs_difference": 0.0,
1367
+ "expected_q9_6_raw_lround": 5064,
1368
+ "expected_q9_6_value_lround": 79.125,
1369
+ "phase_integer_reduction_R": [
1370
+ 0,
1371
+ 0,
1372
+ 54016,
1373
+ 0,
1374
+ 0,
1375
+ 0,
1376
+ 0,
1377
+ 0
1378
+ ],
1379
+ "state_domain_v2": {
1380
+ "nonzero_signed_state_buckets": 2,
1381
+ "balanced_global_tree_depth_upper_bound": 1,
1382
+ "population_buckets": [
1383
+ {
1384
+ "code": 66,
1385
+ "phase": 2,
1386
+ "band": 8,
1387
+ "signed_count": 43,
1388
+ "sign": 1,
1389
+ "population_log_offset_code_units": 43.41011803761678,
1390
+ "fractional_lns_code": 109.41011803761678
1391
+ },
1392
+ {
1393
+ "code": 82,
1394
+ "phase": 2,
1395
+ "band": 10,
1396
+ "signed_count": 42,
1397
+ "sign": 1,
1398
+ "population_log_offset_code_units": 43.138539382230086,
1399
+ "fractional_lns_code": 125.13853938223008
1400
+ }
1401
+ ],
1402
+ "phase_fractional_lns_reference": [
1403
+ {
1404
+ "phase": 0,
1405
+ "sign": 0,
1406
+ "fractional_lns_code_unscaled": null
1407
+ },
1408
+ {
1409
+ "phase": 1,
1410
+ "sign": 0,
1411
+ "fractional_lns_code_unscaled": null
1412
+ },
1413
+ {
1414
+ "phase": 2,
1415
+ "sign": 1,
1416
+ "fractional_lns_code_unscaled": 127.76879350965748
1417
+ },
1418
+ {
1419
+ "phase": 3,
1420
+ "sign": 0,
1421
+ "fractional_lns_code_unscaled": null
1422
+ },
1423
+ {
1424
+ "phase": 4,
1425
+ "sign": 0,
1426
+ "fractional_lns_code_unscaled": null
1427
+ },
1428
+ {
1429
+ "phase": 5,
1430
+ "sign": 0,
1431
+ "fractional_lns_code_unscaled": null
1432
+ },
1433
+ {
1434
+ "phase": 6,
1435
+ "sign": 0,
1436
+ "fractional_lns_code_unscaled": null
1437
+ },
1438
+ {
1439
+ "phase": 7,
1440
+ "sign": 0,
1441
+ "fractional_lns_code_unscaled": null
1442
+ }
1443
+ ],
1444
+ "expected_final_sign": 1,
1445
+ "expected_final_fractional_lns_code": 124.44849351542673,
1446
+ "expected_nearest_lns8_magnitude_code": 124,
1447
+ "note": "fractional_lns_code fields are correctness references from exact math; runtime candidate uses count-log and kappa LUTs without linear materialization"
1448
+ },
1449
+ "expected_direct_8phase_accumulators": [
1450
+ 0,
1451
+ 0,
1452
+ 54016,
1453
+ 0,
1454
+ 0,
1455
+ 0,
1456
+ 0,
1457
+ 0
1458
+ ],
1459
+ "direct_8phase_note": "Same 8 integers as histogram sum_o H[phase][o]<<o; intended as exact dyadic routing reference."
1460
+ },
1461
+ {
1462
+ "name": "boundary_and_zero_codes",
1463
+ "K": 128,
1464
+ "scale": 0.125,
1465
+ "notes": "Exercises code 0, minimum non-zero code 1, unity code 74, and maximum code 127.",
1466
+ "activation_codes": [
1467
+ 1,
1468
+ 127,
1469
+ 74,
1470
+ 0,
1471
+ 1,
1472
+ 127,
1473
+ 74,
1474
+ 0,
1475
+ 1,
1476
+ 127,
1477
+ 74,
1478
+ 0,
1479
+ 1,
1480
+ 127,
1481
+ 74,
1482
+ 0,
1483
+ 1,
1484
+ 127,
1485
+ 74,
1486
+ 0,
1487
+ 1,
1488
+ 127,
1489
+ 74,
1490
+ 0,
1491
+ 1,
1492
+ 127,
1493
+ 74,
1494
+ 0,
1495
+ 1,
1496
+ 127,
1497
+ 74,
1498
+ 0,
1499
+ 1,
1500
+ 127,
1501
+ 74,
1502
+ 0,
1503
+ 1,
1504
+ 127,
1505
+ 74,
1506
+ 0,
1507
+ 1,
1508
+ 127,
1509
+ 74,
1510
+ 0,
1511
+ 1,
1512
+ 127,
1513
+ 74,
1514
+ 0,
1515
+ 1,
1516
+ 127,
1517
+ 74,
1518
+ 0,
1519
+ 1,
1520
+ 127,
1521
+ 74,
1522
+ 0,
1523
+ 1,
1524
+ 127,
1525
+ 74,
1526
+ 0,
1527
+ 1,
1528
+ 127,
1529
+ 74,
1530
+ 0,
1531
+ 1,
1532
+ 127,
1533
+ 74,
1534
+ 0,
1535
+ 1,
1536
+ 127,
1537
+ 74,
1538
+ 0,
1539
+ 1,
1540
+ 127,
1541
+ 74,
1542
+ 0,
1543
+ 1,
1544
+ 127,
1545
+ 74,
1546
+ 0,
1547
+ 1,
1548
+ 127,
1549
+ 74,
1550
+ 0,
1551
+ 1,
1552
+ 127,
1553
+ 74,
1554
+ 0,
1555
+ 1,
1556
+ 127,
1557
+ 74,
1558
+ 0,
1559
+ 1,
1560
+ 127,
1561
+ 74,
1562
+ 0,
1563
+ 1,
1564
+ 127,
1565
+ 74,
1566
+ 0,
1567
+ 1,
1568
+ 127,
1569
+ 74,
1570
+ 0,
1571
+ 1,
1572
+ 127,
1573
+ 74,
1574
+ 0,
1575
+ 1,
1576
+ 127,
1577
+ 74,
1578
+ 0,
1579
+ 1,
1580
+ 127,
1581
+ 74,
1582
+ 0,
1583
+ 1,
1584
+ 127,
1585
+ 74,
1586
+ 0,
1587
+ 1,
1588
+ 127,
1589
+ 74,
1590
+ 0,
1591
+ 1,
1592
+ 127,
1593
+ 74,
1594
+ 0
1595
+ ],
1596
+ "activation_signs": [
1597
+ -1,
1598
+ 1,
1599
+ 1,
1600
+ 1,
1601
+ 1,
1602
+ -1,
1603
+ 1,
1604
+ 1,
1605
+ 1,
1606
+ 1,
1607
+ -1,
1608
+ 1,
1609
+ 1,
1610
+ 1,
1611
+ 1,
1612
+ -1,
1613
+ 1,
1614
+ 1,
1615
+ 1,
1616
+ 1,
1617
+ -1,
1618
+ 1,
1619
+ 1,
1620
+ 1,
1621
+ 1,
1622
+ -1,
1623
+ 1,
1624
+ 1,
1625
+ 1,
1626
+ 1,
1627
+ -1,
1628
+ 1,
1629
+ 1,
1630
+ 1,
1631
+ 1,
1632
+ -1,
1633
+ 1,
1634
+ 1,
1635
+ 1,
1636
+ 1,
1637
+ -1,
1638
+ 1,
1639
+ 1,
1640
+ 1,
1641
+ 1,
1642
+ -1,
1643
+ 1,
1644
+ 1,
1645
+ 1,
1646
+ 1,
1647
+ -1,
1648
+ 1,
1649
+ 1,
1650
+ 1,
1651
+ 1,
1652
+ -1,
1653
+ 1,
1654
+ 1,
1655
+ 1,
1656
+ 1,
1657
+ -1,
1658
+ 1,
1659
+ 1,
1660
+ 1,
1661
+ 1,
1662
+ -1,
1663
+ 1,
1664
+ 1,
1665
+ 1,
1666
+ 1,
1667
+ -1,
1668
+ 1,
1669
+ 1,
1670
+ 1,
1671
+ 1,
1672
+ -1,
1673
+ 1,
1674
+ 1,
1675
+ 1,
1676
+ 1,
1677
+ -1,
1678
+ 1,
1679
+ 1,
1680
+ 1,
1681
+ 1,
1682
+ -1,
1683
+ 1,
1684
+ 1,
1685
+ 1,
1686
+ 1,
1687
+ -1,
1688
+ 1,
1689
+ 1,
1690
+ 1,
1691
+ 1,
1692
+ -1,
1693
+ 1,
1694
+ 1,
1695
+ 1,
1696
+ 1,
1697
+ -1,
1698
+ 1,
1699
+ 1,
1700
+ 1,
1701
+ 1,
1702
+ -1,
1703
+ 1,
1704
+ 1,
1705
+ 1,
1706
+ 1,
1707
+ -1,
1708
+ 1,
1709
+ 1,
1710
+ 1,
1711
+ 1,
1712
+ -1,
1713
+ 1,
1714
+ 1,
1715
+ 1,
1716
+ 1,
1717
+ -1,
1718
+ 1,
1719
+ 1,
1720
+ 1,
1721
+ 1,
1722
+ -1,
1723
+ 1,
1724
+ 1
1725
+ ],
1726
+ "ternary_weights": [
1727
+ -1,
1728
+ 0,
1729
+ 1,
1730
+ 1,
1731
+ -1,
1732
+ 0,
1733
+ 1,
1734
+ 1,
1735
+ -1,
1736
+ 0,
1737
+ 1,
1738
+ 1,
1739
+ -1,
1740
+ 0,
1741
+ 1,
1742
+ 1,
1743
+ -1,
1744
+ 0,
1745
+ 1,
1746
+ 1,
1747
+ -1,
1748
+ 0,
1749
+ 1,
1750
+ 1,
1751
+ -1,
1752
+ 0,
1753
+ 1,
1754
+ 1,
1755
+ -1,
1756
+ 0,
1757
+ 1,
1758
+ 1,
1759
+ -1,
1760
+ 0,
1761
+ 1,
1762
+ 1,
1763
+ -1,
1764
+ 0,
1765
+ 1,
1766
+ 1,
1767
+ -1,
1768
+ 0,
1769
+ 1,
1770
+ 1,
1771
+ -1,
1772
+ 0,
1773
+ 1,
1774
+ 1,
1775
+ -1,
1776
+ 0,
1777
+ 1,
1778
+ 1,
1779
+ -1,
1780
+ 0,
1781
+ 1,
1782
+ 1,
1783
+ -1,
1784
+ 0,
1785
+ 1,
1786
+ 1,
1787
+ -1,
1788
+ 0,
1789
+ 1,
1790
+ 1,
1791
+ -1,
1792
+ 0,
1793
+ 1,
1794
+ 1,
1795
+ -1,
1796
+ 0,
1797
+ 1,
1798
+ 1,
1799
+ -1,
1800
+ 0,
1801
+ 1,
1802
+ 1,
1803
+ -1,
1804
+ 0,
1805
+ 1,
1806
+ 1,
1807
+ -1,
1808
+ 0,
1809
+ 1,
1810
+ 1,
1811
+ -1,
1812
+ 0,
1813
+ 1,
1814
+ 1,
1815
+ -1,
1816
+ 0,
1817
+ 1,
1818
+ 1,
1819
+ -1,
1820
+ 0,
1821
+ 1,
1822
+ 1,
1823
+ -1,
1824
+ 0,
1825
+ 1,
1826
+ 1,
1827
+ -1,
1828
+ 0,
1829
+ 1,
1830
+ 1,
1831
+ -1,
1832
+ 0,
1833
+ 1,
1834
+ 1,
1835
+ -1,
1836
+ 0,
1837
+ 1,
1838
+ 1,
1839
+ -1,
1840
+ 0,
1841
+ 1,
1842
+ 1,
1843
+ -1,
1844
+ 0,
1845
+ 1,
1846
+ 1,
1847
+ -1,
1848
+ 0,
1849
+ 1,
1850
+ 1,
1851
+ -1,
1852
+ 0,
1853
+ 1,
1854
+ 1
1855
+ ],
1856
+ "expected_direct_fp64": 2.495970197075761,
1857
+ "expected_histogram_fp64": 2.495970197075761,
1858
+ "expected_abs_difference": 0.0,
1859
+ "expected_q9_6_raw_lround": 160,
1860
+ "expected_q9_6_value_lround": 2.5,
1861
+ "phase_integer_reduction_R": [
1862
+ 0,
1863
+ -18,
1864
+ 10240,
1865
+ 0,
1866
+ 0,
1867
+ 0,
1868
+ 0,
1869
+ 0
1870
+ ],
1871
+ "state_domain_v2": {
1872
+ "nonzero_signed_state_buckets": 2,
1873
+ "balanced_global_tree_depth_upper_bound": 1,
1874
+ "population_buckets": [
1875
+ {
1876
+ "code": 1,
1877
+ "phase": 1,
1878
+ "band": 0,
1879
+ "signed_count": -18,
1880
+ "sign": -1,
1881
+ "population_log_offset_code_units": 33.3594000115385,
1882
+ "fractional_lns_code": 34.3594000115385
1883
+ },
1884
+ {
1885
+ "code": 74,
1886
+ "phase": 2,
1887
+ "band": 9,
1888
+ "signed_count": 20,
1889
+ "sign": 1,
1890
+ "population_log_offset_code_units": 34.5754247590989,
1891
+ "fractional_lns_code": 108.5754247590989
1892
+ }
1893
+ ],
1894
+ "phase_fractional_lns_reference": [
1895
+ {
1896
+ "phase": 0,
1897
+ "sign": 0,
1898
+ "fractional_lns_code_unscaled": null
1899
+ },
1900
+ {
1901
+ "phase": 1,
1902
+ "sign": -1,
1903
+ "fractional_lns_code_unscaled": 34.3594000115385
1904
+ },
1905
+ {
1906
+ "phase": 2,
1907
+ "sign": 1,
1908
+ "fractional_lns_code_unscaled": 108.5754247590989
1909
+ },
1910
+ {
1911
+ "phase": 3,
1912
+ "sign": 0,
1913
+ "fractional_lns_code_unscaled": null
1914
+ },
1915
+ {
1916
+ "phase": 4,
1917
+ "sign": 0,
1918
+ "fractional_lns_code_unscaled": null
1919
+ },
1920
+ {
1921
+ "phase": 5,
1922
+ "sign": 0,
1923
+ "fractional_lns_code_unscaled": null
1924
+ },
1925
+ {
1926
+ "phase": 6,
1927
+ "sign": 0,
1928
+ "fractional_lns_code_unscaled": null
1929
+ },
1930
+ {
1931
+ "phase": 7,
1932
+ "sign": 0,
1933
+ "fractional_lns_code_unscaled": null
1934
+ }
1935
+ ],
1936
+ "expected_final_sign": 1,
1937
+ "expected_final_fractional_lns_code": 84.5568056633843,
1938
+ "expected_nearest_lns8_magnitude_code": 85,
1939
+ "note": "fractional_lns_code fields are correctness references from exact math; runtime candidate uses count-log and kappa LUTs without linear materialization"
1940
+ },
1941
+ "expected_direct_8phase_accumulators": [
1942
+ 0,
1943
+ -18,
1944
+ 10240,
1945
+ 0,
1946
+ 0,
1947
+ 0,
1948
+ 0,
1949
+ 0
1950
+ ],
1951
+ "direct_8phase_note": "Same 8 integers as histogram sum_o H[phase][o]<<o; intended as exact dyadic routing reference."
1952
+ },
1953
+ {
1954
+ "name": "phase_sweep_near_unity",
1955
+ "K": 128,
1956
+ "scale": 1.5,
1957
+ "notes": "All 8 phases around unity are exercised repeatedly.",
1958
+ "activation_codes": [
1959
+ 70,
1960
+ 71,
1961
+ 72,
1962
+ 73,
1963
+ 74,
1964
+ 75,
1965
+ 76,
1966
+ 77,
1967
+ 70,
1968
+ 71,
1969
+ 72,
1970
+ 73,
1971
+ 74,
1972
+ 75,
1973
+ 76,
1974
+ 77,
1975
+ 70,
1976
+ 71,
1977
+ 72,
1978
+ 73,
1979
+ 74,
1980
+ 75,
1981
+ 76,
1982
+ 77,
1983
+ 70,
1984
+ 71,
1985
+ 72,
1986
+ 73,
1987
+ 74,
1988
+ 75,
1989
+ 76,
1990
+ 77,
1991
+ 70,
1992
+ 71,
1993
+ 72,
1994
+ 73,
1995
+ 74,
1996
+ 75,
1997
+ 76,
1998
+ 77,
1999
+ 70,
2000
+ 71,
2001
+ 72,
2002
+ 73,
2003
+ 74,
2004
+ 75,
2005
+ 76,
2006
+ 77,
2007
+ 70,
2008
+ 71,
2009
+ 72,
2010
+ 73,
2011
+ 74,
2012
+ 75,
2013
+ 76,
2014
+ 77,
2015
+ 70,
2016
+ 71,
2017
+ 72,
2018
+ 73,
2019
+ 74,
2020
+ 75,
2021
+ 76,
2022
+ 77,
2023
+ 70,
2024
+ 71,
2025
+ 72,
2026
+ 73,
2027
+ 74,
2028
+ 75,
2029
+ 76,
2030
+ 77,
2031
+ 70,
2032
+ 71,
2033
+ 72,
2034
+ 73,
2035
+ 74,
2036
+ 75,
2037
+ 76,
2038
+ 77,
2039
+ 70,
2040
+ 71,
2041
+ 72,
2042
+ 73,
2043
+ 74,
2044
+ 75,
2045
+ 76,
2046
+ 77,
2047
+ 70,
2048
+ 71,
2049
+ 72,
2050
+ 73,
2051
+ 74,
2052
+ 75,
2053
+ 76,
2054
+ 77,
2055
+ 70,
2056
+ 71,
2057
+ 72,
2058
+ 73,
2059
+ 74,
2060
+ 75,
2061
+ 76,
2062
+ 77,
2063
+ 70,
2064
+ 71,
2065
+ 72,
2066
+ 73,
2067
+ 74,
2068
+ 75,
2069
+ 76,
2070
+ 77,
2071
+ 70,
2072
+ 71,
2073
+ 72,
2074
+ 73,
2075
+ 74,
2076
+ 75,
2077
+ 76,
2078
+ 77,
2079
+ 70,
2080
+ 71,
2081
+ 72,
2082
+ 73,
2083
+ 74,
2084
+ 75,
2085
+ 76,
2086
+ 77
2087
+ ],
2088
+ "activation_signs": [
2089
+ 1,
2090
+ 1,
2091
+ -1,
2092
+ -1,
2093
+ 1,
2094
+ 1,
2095
+ -1,
2096
+ -1,
2097
+ 1,
2098
+ 1,
2099
+ -1,
2100
+ -1,
2101
+ 1,
2102
+ 1,
2103
+ -1,
2104
+ -1,
2105
+ 1,
2106
+ 1,
2107
+ -1,
2108
+ -1,
2109
+ 1,
2110
+ 1,
2111
+ -1,
2112
+ -1,
2113
+ 1,
2114
+ 1,
2115
+ -1,
2116
+ -1,
2117
+ 1,
2118
+ 1,
2119
+ -1,
2120
+ -1,
2121
+ 1,
2122
+ 1,
2123
+ -1,
2124
+ -1,
2125
+ 1,
2126
+ 1,
2127
+ -1,
2128
+ -1,
2129
+ 1,
2130
+ 1,
2131
+ -1,
2132
+ -1,
2133
+ 1,
2134
+ 1,
2135
+ -1,
2136
+ -1,
2137
+ 1,
2138
+ 1,
2139
+ -1,
2140
+ -1,
2141
+ 1,
2142
+ 1,
2143
+ -1,
2144
+ -1,
2145
+ 1,
2146
+ 1,
2147
+ -1,
2148
+ -1,
2149
+ 1,
2150
+ 1,
2151
+ -1,
2152
+ -1,
2153
+ 1,
2154
+ 1,
2155
+ -1,
2156
+ -1,
2157
+ 1,
2158
+ 1,
2159
+ -1,
2160
+ -1,
2161
+ 1,
2162
+ 1,
2163
+ -1,
2164
+ -1,
2165
+ 1,
2166
+ 1,
2167
+ -1,
2168
+ -1,
2169
+ 1,
2170
+ 1,
2171
+ -1,
2172
+ -1,
2173
+ 1,
2174
+ 1,
2175
+ -1,
2176
+ -1,
2177
+ 1,
2178
+ 1,
2179
+ -1,
2180
+ -1,
2181
+ 1,
2182
+ 1,
2183
+ -1,
2184
+ -1,
2185
+ 1,
2186
+ 1,
2187
+ -1,
2188
+ -1,
2189
+ 1,
2190
+ 1,
2191
+ -1,
2192
+ -1,
2193
+ 1,
2194
+ 1,
2195
+ -1,
2196
+ -1,
2197
+ 1,
2198
+ 1,
2199
+ -1,
2200
+ -1,
2201
+ 1,
2202
+ 1,
2203
+ -1,
2204
+ -1,
2205
+ 1,
2206
+ 1,
2207
+ -1,
2208
+ -1,
2209
+ 1,
2210
+ 1,
2211
+ -1,
2212
+ -1,
2213
+ 1,
2214
+ 1,
2215
+ -1,
2216
+ -1
2217
+ ],
2218
+ "ternary_weights": [
2219
+ 0,
2220
+ 1,
2221
+ 1,
2222
+ 1,
2223
+ 1,
2224
+ 0,
2225
+ 1,
2226
+ 1,
2227
+ 1,
2228
+ 1,
2229
+ 0,
2230
+ 1,
2231
+ 1,
2232
+ 1,
2233
+ 1,
2234
+ 0,
2235
+ 1,
2236
+ 1,
2237
+ 1,
2238
+ 1,
2239
+ 0,
2240
+ 1,
2241
+ 1,
2242
+ 1,
2243
+ 1,
2244
+ 0,
2245
+ 1,
2246
+ 1,
2247
+ 1,
2248
+ 1,
2249
+ 0,
2250
+ 1,
2251
+ 1,
2252
+ 1,
2253
+ 1,
2254
+ 0,
2255
+ 1,
2256
+ 1,
2257
+ 1,
2258
+ 1,
2259
+ 0,
2260
+ 1,
2261
+ 1,
2262
+ 1,
2263
+ 1,
2264
+ 0,
2265
+ 1,
2266
+ 1,
2267
+ 1,
2268
+ 1,
2269
+ 0,
2270
+ 1,
2271
+ 1,
2272
+ 1,
2273
+ 1,
2274
+ 0,
2275
+ 1,
2276
+ 1,
2277
+ 1,
2278
+ 1,
2279
+ 0,
2280
+ 1,
2281
+ 1,
2282
+ 1,
2283
+ 1,
2284
+ 0,
2285
+ 1,
2286
+ 1,
2287
+ 1,
2288
+ 1,
2289
+ 0,
2290
+ 1,
2291
+ 1,
2292
+ 1,
2293
+ 1,
2294
+ 0,
2295
+ 1,
2296
+ 1,
2297
+ 1,
2298
+ 1,
2299
+ 0,
2300
+ 1,
2301
+ 1,
2302
+ 1,
2303
+ 1,
2304
+ 0,
2305
+ 1,
2306
+ 1,
2307
+ 1,
2308
+ 1,
2309
+ 0,
2310
+ 1,
2311
+ 1,
2312
+ 1,
2313
+ 1,
2314
+ 0,
2315
+ 1,
2316
+ 1,
2317
+ 1,
2318
+ 1,
2319
+ 0,
2320
+ 1,
2321
+ 1,
2322
+ 1,
2323
+ 1,
2324
+ 0,
2325
+ 1,
2326
+ 1,
2327
+ 1,
2328
+ 1,
2329
+ 0,
2330
+ 1,
2331
+ 1,
2332
+ 1,
2333
+ 1,
2334
+ 0,
2335
+ 1,
2336
+ 1,
2337
+ 1,
2338
+ 1,
2339
+ 0,
2340
+ 1,
2341
+ 1,
2342
+ 1,
2343
+ 1,
2344
+ 0,
2345
+ 1,
2346
+ 1
2347
+ ],
2348
+ "expected_direct_fp64": -15.863352201126354,
2349
+ "expected_histogram_fp64": -15.863352201126357,
2350
+ "expected_abs_difference": 3.552713678800501e-15,
2351
+ "expected_q9_6_raw_lround": -1015,
2352
+ "expected_q9_6_value_lround": -15.859375,
2353
+ "phase_integer_reduction_R": [
2354
+ -6656,
2355
+ -6656,
2356
+ 6656,
2357
+ 6144,
2358
+ -6656,
2359
+ -6656,
2360
+ 3072,
2361
+ 3328
2362
+ ],
2363
+ "state_domain_v2": {
2364
+ "nonzero_signed_state_buckets": 8,
2365
+ "balanced_global_tree_depth_upper_bound": 3,
2366
+ "population_buckets": [
2367
+ {
2368
+ "code": 70,
2369
+ "phase": 6,
2370
+ "band": 8,
2371
+ "signed_count": 12,
2372
+ "sign": 1,
2373
+ "population_log_offset_code_units": 28.67970000576925,
2374
+ "fractional_lns_code": 98.67970000576925
2375
+ },
2376
+ {
2377
+ "code": 71,
2378
+ "phase": 7,
2379
+ "band": 8,
2380
+ "signed_count": 13,
2381
+ "sign": 1,
2382
+ "population_log_offset_code_units": 29.603517745128737,
2383
+ "fractional_lns_code": 100.60351774512874
2384
+ },
2385
+ {
2386
+ "code": 72,
2387
+ "phase": 0,
2388
+ "band": 9,
2389
+ "signed_count": -13,
2390
+ "sign": -1,
2391
+ "population_log_offset_code_units": 29.603517745128737,
2392
+ "fractional_lns_code": 101.60351774512874
2393
+ },
2394
+ {
2395
+ "code": 73,
2396
+ "phase": 1,
2397
+ "band": 9,
2398
+ "signed_count": -13,
2399
+ "sign": -1,
2400
+ "population_log_offset_code_units": 29.603517745128737,
2401
+ "fractional_lns_code": 102.60351774512874
2402
+ },
2403
+ {
2404
+ "code": 74,
2405
+ "phase": 2,
2406
+ "band": 9,
2407
+ "signed_count": 13,
2408
+ "sign": 1,
2409
+ "population_log_offset_code_units": 29.603517745128737,
2410
+ "fractional_lns_code": 103.60351774512874
2411
+ },
2412
+ {
2413
+ "code": 75,
2414
+ "phase": 3,
2415
+ "band": 9,
2416
+ "signed_count": 12,
2417
+ "sign": 1,
2418
+ "population_log_offset_code_units": 28.67970000576925,
2419
+ "fractional_lns_code": 103.67970000576925
2420
+ },
2421
+ {
2422
+ "code": 76,
2423
+ "phase": 4,
2424
+ "band": 9,
2425
+ "signed_count": -13,
2426
+ "sign": -1,
2427
+ "population_log_offset_code_units": 29.603517745128737,
2428
+ "fractional_lns_code": 105.60351774512874
2429
+ },
2430
+ {
2431
+ "code": 77,
2432
+ "phase": 5,
2433
+ "band": 9,
2434
+ "signed_count": -13,
2435
+ "sign": -1,
2436
+ "population_log_offset_code_units": 29.603517745128737,
2437
+ "fractional_lns_code": 106.60351774512874
2438
+ }
2439
+ ],
2440
+ "phase_fractional_lns_reference": [
2441
+ {
2442
+ "phase": 0,
2443
+ "sign": -1,
2444
+ "fractional_lns_code_unscaled": 101.60351774512874
2445
+ },
2446
+ {
2447
+ "phase": 1,
2448
+ "sign": -1,
2449
+ "fractional_lns_code_unscaled": 102.60351774512873
2450
+ },
2451
+ {
2452
+ "phase": 2,
2453
+ "sign": 1,
2454
+ "fractional_lns_code_unscaled": 103.60351774512874
2455
+ },
2456
+ {
2457
+ "phase": 3,
2458
+ "sign": 1,
2459
+ "fractional_lns_code_unscaled": 103.67970000576925
2460
+ },
2461
+ {
2462
+ "phase": 4,
2463
+ "sign": -1,
2464
+ "fractional_lns_code_unscaled": 105.60351774512874
2465
+ },
2466
+ {
2467
+ "phase": 5,
2468
+ "sign": -1,
2469
+ "fractional_lns_code_unscaled": 106.60351774512874
2470
+ },
2471
+ {
2472
+ "phase": 6,
2473
+ "sign": 1,
2474
+ "fractional_lns_code_unscaled": 98.67970000576925
2475
+ },
2476
+ {
2477
+ "phase": 7,
2478
+ "sign": 1,
2479
+ "fractional_lns_code_unscaled": 100.60351774512874
2480
+ }
2481
+ ],
2482
+ "expected_final_sign": -1,
2483
+ "expected_final_fractional_lns_code": 105.90100611680009,
2484
+ "expected_nearest_lns8_magnitude_code": 106,
2485
+ "note": "fractional_lns_code fields are correctness references from exact math; runtime candidate uses count-log and kappa LUTs without linear materialization"
2486
+ },
2487
+ "expected_direct_8phase_accumulators": [
2488
+ -6656,
2489
+ -6656,
2490
+ 6656,
2491
+ 6144,
2492
+ -6656,
2493
+ -6656,
2494
+ 3072,
2495
+ 3328
2496
+ ],
2497
+ "direct_8phase_note": "Same 8 integers as histogram sum_o H[phase][o]<<o; intended as exact dyadic routing reference."
2498
+ },
2499
+ {
2500
+ "name": "deterministic_random_7408",
2501
+ "K": 128,
2502
+ "scale": 0.4375,
2503
+ "notes": "Deterministic mixed-state vector, seed 7408.",
2504
+ "activation_codes": [
2505
+ 0,
2506
+ 95,
2507
+ 96,
2508
+ 68,
2509
+ 77,
2510
+ 102,
2511
+ 20,
2512
+ 28,
2513
+ 104,
2514
+ 37,
2515
+ 33,
2516
+ 43,
2517
+ 78,
2518
+ 98,
2519
+ 37,
2520
+ 19,
2521
+ 81,
2522
+ 27,
2523
+ 52,
2524
+ 40,
2525
+ 89,
2526
+ 87,
2527
+ 72,
2528
+ 97,
2529
+ 34,
2530
+ 52,
2531
+ 91,
2532
+ 95,
2533
+ 51,
2534
+ 49,
2535
+ 52,
2536
+ 79,
2537
+ 0,
2538
+ 43,
2539
+ 52,
2540
+ 0,
2541
+ 29,
2542
+ 47,
2543
+ 29,
2544
+ 73,
2545
+ 77,
2546
+ 54,
2547
+ 41,
2548
+ 99,
2549
+ 30,
2550
+ 21,
2551
+ 46,
2552
+ 64,
2553
+ 105,
2554
+ 35,
2555
+ 36,
2556
+ 55,
2557
+ 104,
2558
+ 54,
2559
+ 19,
2560
+ 27,
2561
+ 95,
2562
+ 0,
2563
+ 48,
2564
+ 75,
2565
+ 106,
2566
+ 105,
2567
+ 48,
2568
+ 94,
2569
+ 30,
2570
+ 34,
2571
+ 86,
2572
+ 74,
2573
+ 79,
2574
+ 49,
2575
+ 102,
2576
+ 29,
2577
+ 80,
2578
+ 0,
2579
+ 78,
2580
+ 96,
2581
+ 49,
2582
+ 22,
2583
+ 92,
2584
+ 66,
2585
+ 61,
2586
+ 86,
2587
+ 48,
2588
+ 51,
2589
+ 64,
2590
+ 64,
2591
+ 101,
2592
+ 86,
2593
+ 32,
2594
+ 61,
2595
+ 0,
2596
+ 24,
2597
+ 57,
2598
+ 42,
2599
+ 62,
2600
+ 71,
2601
+ 57,
2602
+ 70,
2603
+ 83,
2604
+ 71,
2605
+ 44,
2606
+ 57,
2607
+ 95,
2608
+ 30,
2609
+ 100,
2610
+ 57,
2611
+ 50,
2612
+ 86,
2613
+ 82,
2614
+ 53,
2615
+ 24,
2616
+ 91,
2617
+ 94,
2618
+ 41,
2619
+ 81,
2620
+ 61,
2621
+ 82,
2622
+ 104,
2623
+ 95,
2624
+ 85,
2625
+ 42,
2626
+ 88,
2627
+ 106,
2628
+ 80,
2629
+ 92,
2630
+ 110,
2631
+ 79,
2632
+ 44
2633
+ ],
2634
+ "activation_signs": [
2635
+ -1,
2636
+ 1,
2637
+ -1,
2638
+ -1,
2639
+ -1,
2640
+ -1,
2641
+ -1,
2642
+ -1,
2643
+ 1,
2644
+ 1,
2645
+ 1,
2646
+ -1,
2647
+ -1,
2648
+ 1,
2649
+ -1,
2650
+ 1,
2651
+ 1,
2652
+ -1,
2653
+ -1,
2654
+ -1,
2655
+ 1,
2656
+ 1,
2657
+ 1,
2658
+ -1,
2659
+ 1,
2660
+ -1,
2661
+ 1,
2662
+ 1,
2663
+ -1,
2664
+ 1,
2665
+ 1,
2666
+ -1,
2667
+ -1,
2668
+ -1,
2669
+ 1,
2670
+ 1,
2671
+ 1,
2672
+ -1,
2673
+ -1,
2674
+ 1,
2675
+ 1,
2676
+ 1,
2677
+ -1,
2678
+ 1,
2679
+ -1,
2680
+ -1,
2681
+ 1,
2682
+ -1,
2683
+ 1,
2684
+ -1,
2685
+ -1,
2686
+ 1,
2687
+ -1,
2688
+ -1,
2689
+ -1,
2690
+ -1,
2691
+ -1,
2692
+ 1,
2693
+ -1,
2694
+ 1,
2695
+ 1,
2696
+ -1,
2697
+ 1,
2698
+ 1,
2699
+ 1,
2700
+ 1,
2701
+ -1,
2702
+ 1,
2703
+ 1,
2704
+ -1,
2705
+ 1,
2706
+ 1,
2707
+ 1,
2708
+ 1,
2709
+ -1,
2710
+ 1,
2711
+ -1,
2712
+ 1,
2713
+ 1,
2714
+ 1,
2715
+ 1,
2716
+ -1,
2717
+ -1,
2718
+ -1,
2719
+ 1,
2720
+ 1,
2721
+ -1,
2722
+ -1,
2723
+ 1,
2724
+ 1,
2725
+ 1,
2726
+ -1,
2727
+ -1,
2728
+ 1,
2729
+ 1,
2730
+ -1,
2731
+ 1,
2732
+ 1,
2733
+ -1,
2734
+ -1,
2735
+ 1,
2736
+ 1,
2737
+ 1,
2738
+ 1,
2739
+ 1,
2740
+ 1,
2741
+ 1,
2742
+ -1,
2743
+ -1,
2744
+ 1,
2745
+ -1,
2746
+ -1,
2747
+ -1,
2748
+ 1,
2749
+ -1,
2750
+ 1,
2751
+ -1,
2752
+ -1,
2753
+ -1,
2754
+ -1,
2755
+ 1,
2756
+ -1,
2757
+ -1,
2758
+ -1,
2759
+ 1,
2760
+ -1,
2761
+ 1,
2762
+ 1
2763
+ ],
2764
+ "ternary_weights": [
2765
+ 1,
2766
+ 0,
2767
+ 1,
2768
+ 0,
2769
+ 1,
2770
+ 0,
2771
+ 0,
2772
+ -1,
2773
+ -1,
2774
+ 1,
2775
+ 1,
2776
+ 1,
2777
+ -1,
2778
+ -1,
2779
+ 1,
2780
+ -1,
2781
+ 1,
2782
+ 1,
2783
+ -1,
2784
+ 0,
2785
+ 1,
2786
+ 1,
2787
+ 1,
2788
+ -1,
2789
+ -1,
2790
+ -1,
2791
+ -1,
2792
+ 1,
2793
+ 1,
2794
+ 0,
2795
+ -1,
2796
+ 1,
2797
+ 1,
2798
+ -1,
2799
+ 1,
2800
+ -1,
2801
+ 1,
2802
+ 1,
2803
+ 0,
2804
+ 1,
2805
+ 1,
2806
+ -1,
2807
+ -1,
2808
+ 1,
2809
+ 1,
2810
+ -1,
2811
+ 1,
2812
+ 1,
2813
+ 1,
2814
+ 1,
2815
+ 0,
2816
+ 1,
2817
+ 1,
2818
+ -1,
2819
+ 1,
2820
+ -1,
2821
+ 0,
2822
+ 1,
2823
+ -1,
2824
+ -1,
2825
+ 1,
2826
+ -1,
2827
+ -1,
2828
+ 1,
2829
+ -1,
2830
+ -1,
2831
+ 0,
2832
+ 0,
2833
+ -1,
2834
+ -1,
2835
+ 1,
2836
+ -1,
2837
+ 1,
2838
+ 1,
2839
+ 1,
2840
+ 1,
2841
+ -1,
2842
+ 0,
2843
+ 1,
2844
+ 1,
2845
+ -1,
2846
+ 0,
2847
+ -1,
2848
+ 1,
2849
+ 1,
2850
+ 1,
2851
+ -1,
2852
+ 1,
2853
+ 0,
2854
+ -1,
2855
+ 1,
2856
+ 1,
2857
+ -1,
2858
+ 0,
2859
+ 0,
2860
+ -1,
2861
+ 0,
2862
+ 1,
2863
+ -1,
2864
+ 1,
2865
+ 1,
2866
+ 0,
2867
+ -1,
2868
+ 1,
2869
+ 0,
2870
+ 0,
2871
+ 1,
2872
+ 1,
2873
+ 1,
2874
+ 1,
2875
+ 1,
2876
+ -1,
2877
+ 1,
2878
+ -1,
2879
+ -1,
2880
+ 0,
2881
+ -1,
2882
+ -1,
2883
+ -1,
2884
+ 1,
2885
+ 1,
2886
+ 1,
2887
+ 1,
2888
+ 1,
2889
+ 1,
2890
+ 1,
2891
+ -1,
2892
+ -1
2893
+ ],
2894
+ "expected_direct_fp64": 16.50192339368321,
2895
+ "expected_histogram_fp64": 16.50192339368321,
2896
+ "expected_abs_difference": 0.0,
2897
+ "expected_q9_6_raw_lround": 1056,
2898
+ "expected_q9_6_value_lround": 16.5,
2899
+ "phase_integer_reduction_R": [
2900
+ -9424,
2901
+ 25360,
2902
+ -3776,
2903
+ 4456,
2904
+ 4232,
2905
+ 2884,
2906
+ -5864,
2907
+ 1568
2908
+ ],
2909
+ "state_domain_v2": {
2910
+ "nonzero_signed_state_buckets": 44,
2911
+ "balanced_global_tree_depth_upper_bound": 6,
2912
+ "population_buckets": [
2913
+ {
2914
+ "code": 19,
2915
+ "phase": 3,
2916
+ "band": 2,
2917
+ "signed_count": -2,
2918
+ "sign": -1,
2919
+ "population_log_offset_code_units": 8.0,
2920
+ "fractional_lns_code": 27.0
2921
+ },
2922
+ {
2923
+ "code": 21,
2924
+ "phase": 5,
2925
+ "band": 2,
2926
+ "signed_count": 1,
2927
+ "sign": 1,
2928
+ "population_log_offset_code_units": 0.0,
2929
+ "fractional_lns_code": 21.0
2930
+ },
2931
+ {
2932
+ "code": 24,
2933
+ "phase": 0,
2934
+ "band": 3,
2935
+ "signed_count": -2,
2936
+ "sign": -1,
2937
+ "population_log_offset_code_units": 8.0,
2938
+ "fractional_lns_code": 32.0
2939
+ },
2940
+ {
2941
+ "code": 28,
2942
+ "phase": 4,
2943
+ "band": 3,
2944
+ "signed_count": 1,
2945
+ "sign": 1,
2946
+ "population_log_offset_code_units": 0.0,
2947
+ "fractional_lns_code": 28.0
2948
+ },
2949
+ {
2950
+ "code": 30,
2951
+ "phase": 6,
2952
+ "band": 3,
2953
+ "signed_count": -1,
2954
+ "sign": -1,
2955
+ "population_log_offset_code_units": 0.0,
2956
+ "fractional_lns_code": 30.0
2957
+ },
2958
+ {
2959
+ "code": 33,
2960
+ "phase": 1,
2961
+ "band": 4,
2962
+ "signed_count": 1,
2963
+ "sign": 1,
2964
+ "population_log_offset_code_units": 0.0,
2965
+ "fractional_lns_code": 33.0
2966
+ },
2967
+ {
2968
+ "code": 34,
2969
+ "phase": 2,
2970
+ "band": 4,
2971
+ "signed_count": -2,
2972
+ "sign": -1,
2973
+ "population_log_offset_code_units": 8.0,
2974
+ "fractional_lns_code": 42.0
2975
+ },
2976
+ {
2977
+ "code": 35,
2978
+ "phase": 3,
2979
+ "band": 4,
2980
+ "signed_count": -1,
2981
+ "sign": -1,
2982
+ "population_log_offset_code_units": 0.0,
2983
+ "fractional_lns_code": 35.0
2984
+ },
2985
+ {
2986
+ "code": 42,
2987
+ "phase": 2,
2988
+ "band": 5,
2989
+ "signed_count": 1,
2990
+ "sign": 1,
2991
+ "population_log_offset_code_units": 0.0,
2992
+ "fractional_lns_code": 42.0
2993
+ },
2994
+ {
2995
+ "code": 46,
2996
+ "phase": 6,
2997
+ "band": 5,
2998
+ "signed_count": 1,
2999
+ "sign": 1,
3000
+ "population_log_offset_code_units": 0.0,
3001
+ "fractional_lns_code": 46.0
3002
+ },
3003
+ {
3004
+ "code": 47,
3005
+ "phase": 7,
3006
+ "band": 5,
3007
+ "signed_count": -1,
3008
+ "sign": -1,
3009
+ "population_log_offset_code_units": 0.0,
3010
+ "fractional_lns_code": 47.0
3011
+ },
3012
+ {
3013
+ "code": 48,
3014
+ "phase": 0,
3015
+ "band": 6,
3016
+ "signed_count": 1,
3017
+ "sign": 1,
3018
+ "population_log_offset_code_units": 0.0,
3019
+ "fractional_lns_code": 48.0
3020
+ },
3021
+ {
3022
+ "code": 49,
3023
+ "phase": 1,
3024
+ "band": 6,
3025
+ "signed_count": 2,
3026
+ "sign": 1,
3027
+ "population_log_offset_code_units": 8.0,
3028
+ "fractional_lns_code": 57.0
3029
+ },
3030
+ {
3031
+ "code": 50,
3032
+ "phase": 2,
3033
+ "band": 6,
3034
+ "signed_count": 1,
3035
+ "sign": 1,
3036
+ "population_log_offset_code_units": 0.0,
3037
+ "fractional_lns_code": 50.0
3038
+ },
3039
+ {
3040
+ "code": 51,
3041
+ "phase": 3,
3042
+ "band": 6,
3043
+ "signed_count": -2,
3044
+ "sign": -1,
3045
+ "population_log_offset_code_units": 8.0,
3046
+ "fractional_lns_code": 59.0
3047
+ },
3048
+ {
3049
+ "code": 52,
3050
+ "phase": 4,
3051
+ "band": 6,
3052
+ "signed_count": 2,
3053
+ "sign": 1,
3054
+ "population_log_offset_code_units": 8.0,
3055
+ "fractional_lns_code": 60.0
3056
+ },
3057
+ {
3058
+ "code": 53,
3059
+ "phase": 5,
3060
+ "band": 6,
3061
+ "signed_count": 1,
3062
+ "sign": 1,
3063
+ "population_log_offset_code_units": 0.0,
3064
+ "fractional_lns_code": 53.0
3065
+ },
3066
+ {
3067
+ "code": 55,
3068
+ "phase": 7,
3069
+ "band": 6,
3070
+ "signed_count": 1,
3071
+ "sign": 1,
3072
+ "population_log_offset_code_units": 0.0,
3073
+ "fractional_lns_code": 55.0
3074
+ },
3075
+ {
3076
+ "code": 57,
3077
+ "phase": 1,
3078
+ "band": 7,
3079
+ "signed_count": 1,
3080
+ "sign": 1,
3081
+ "population_log_offset_code_units": 0.0,
3082
+ "fractional_lns_code": 57.0
3083
+ },
3084
+ {
3085
+ "code": 61,
3086
+ "phase": 5,
3087
+ "band": 7,
3088
+ "signed_count": -2,
3089
+ "sign": -1,
3090
+ "population_log_offset_code_units": 8.0,
3091
+ "fractional_lns_code": 69.0
3092
+ },
3093
+ {
3094
+ "code": 64,
3095
+ "phase": 0,
3096
+ "band": 8,
3097
+ "signed_count": 1,
3098
+ "sign": 1,
3099
+ "population_log_offset_code_units": 0.0,
3100
+ "fractional_lns_code": 64.0
3101
+ },
3102
+ {
3103
+ "code": 66,
3104
+ "phase": 2,
3105
+ "band": 8,
3106
+ "signed_count": 1,
3107
+ "sign": 1,
3108
+ "population_log_offset_code_units": 0.0,
3109
+ "fractional_lns_code": 66.0
3110
+ },
3111
+ {
3112
+ "code": 70,
3113
+ "phase": 6,
3114
+ "band": 8,
3115
+ "signed_count": 1,
3116
+ "sign": 1,
3117
+ "population_log_offset_code_units": 0.0,
3118
+ "fractional_lns_code": 70.0
3119
+ },
3120
+ {
3121
+ "code": 72,
3122
+ "phase": 0,
3123
+ "band": 9,
3124
+ "signed_count": 1,
3125
+ "sign": 1,
3126
+ "population_log_offset_code_units": 0.0,
3127
+ "fractional_lns_code": 72.0
3128
+ },
3129
+ {
3130
+ "code": 73,
3131
+ "phase": 1,
3132
+ "band": 9,
3133
+ "signed_count": 1,
3134
+ "sign": 1,
3135
+ "population_log_offset_code_units": 0.0,
3136
+ "fractional_lns_code": 73.0
3137
+ },
3138
+ {
3139
+ "code": 75,
3140
+ "phase": 3,
3141
+ "band": 9,
3142
+ "signed_count": -1,
3143
+ "sign": -1,
3144
+ "population_log_offset_code_units": 0.0,
3145
+ "fractional_lns_code": 75.0
3146
+ },
3147
+ {
3148
+ "code": 79,
3149
+ "phase": 7,
3150
+ "band": 9,
3151
+ "signed_count": -3,
3152
+ "sign": -1,
3153
+ "population_log_offset_code_units": 12.679700005769249,
3154
+ "fractional_lns_code": 91.67970000576925
3155
+ },
3156
+ {
3157
+ "code": 81,
3158
+ "phase": 1,
3159
+ "band": 10,
3160
+ "signed_count": 2,
3161
+ "sign": 1,
3162
+ "population_log_offset_code_units": 8.0,
3163
+ "fractional_lns_code": 89.0
3164
+ },
3165
+ {
3166
+ "code": 83,
3167
+ "phase": 3,
3168
+ "band": 10,
3169
+ "signed_count": 1,
3170
+ "sign": 1,
3171
+ "population_log_offset_code_units": 0.0,
3172
+ "fractional_lns_code": 83.0
3173
+ },
3174
+ {
3175
+ "code": 85,
3176
+ "phase": 5,
3177
+ "band": 10,
3178
+ "signed_count": -1,
3179
+ "sign": -1,
3180
+ "population_log_offset_code_units": 0.0,
3181
+ "fractional_lns_code": 85.0
3182
+ },
3183
+ {
3184
+ "code": 86,
3185
+ "phase": 6,
3186
+ "band": 10,
3187
+ "signed_count": -2,
3188
+ "sign": -1,
3189
+ "population_log_offset_code_units": 8.0,
3190
+ "fractional_lns_code": 94.0
3191
+ },
3192
+ {
3193
+ "code": 87,
3194
+ "phase": 7,
3195
+ "band": 10,
3196
+ "signed_count": 1,
3197
+ "sign": 1,
3198
+ "population_log_offset_code_units": 0.0,
3199
+ "fractional_lns_code": 87.0
3200
+ },
3201
+ {
3202
+ "code": 88,
3203
+ "phase": 0,
3204
+ "band": 11,
3205
+ "signed_count": -1,
3206
+ "sign": -1,
3207
+ "population_log_offset_code_units": 0.0,
3208
+ "fractional_lns_code": 88.0
3209
+ },
3210
+ {
3211
+ "code": 89,
3212
+ "phase": 1,
3213
+ "band": 11,
3214
+ "signed_count": 1,
3215
+ "sign": 1,
3216
+ "population_log_offset_code_units": 0.0,
3217
+ "fractional_lns_code": 89.0
3218
+ },
3219
+ {
3220
+ "code": 92,
3221
+ "phase": 4,
3222
+ "band": 11,
3223
+ "signed_count": 2,
3224
+ "sign": 1,
3225
+ "population_log_offset_code_units": 8.0,
3226
+ "fractional_lns_code": 100.0
3227
+ },
3228
+ {
3229
+ "code": 95,
3230
+ "phase": 7,
3231
+ "band": 11,
3232
+ "signed_count": 1,
3233
+ "sign": 1,
3234
+ "population_log_offset_code_units": 0.0,
3235
+ "fractional_lns_code": 95.0
3236
+ },
3237
+ {
3238
+ "code": 97,
3239
+ "phase": 1,
3240
+ "band": 12,
3241
+ "signed_count": 1,
3242
+ "sign": 1,
3243
+ "population_log_offset_code_units": 0.0,
3244
+ "fractional_lns_code": 97.0
3245
+ },
3246
+ {
3247
+ "code": 98,
3248
+ "phase": 2,
3249
+ "band": 12,
3250
+ "signed_count": -1,
3251
+ "sign": -1,
3252
+ "population_log_offset_code_units": 0.0,
3253
+ "fractional_lns_code": 98.0
3254
+ },
3255
+ {
3256
+ "code": 99,
3257
+ "phase": 3,
3258
+ "band": 12,
3259
+ "signed_count": 1,
3260
+ "sign": 1,
3261
+ "population_log_offset_code_units": 0.0,
3262
+ "fractional_lns_code": 99.0
3263
+ },
3264
+ {
3265
+ "code": 101,
3266
+ "phase": 5,
3267
+ "band": 12,
3268
+ "signed_count": 1,
3269
+ "sign": 1,
3270
+ "population_log_offset_code_units": 0.0,
3271
+ "fractional_lns_code": 101.0
3272
+ },
3273
+ {
3274
+ "code": 102,
3275
+ "phase": 6,
3276
+ "band": 12,
3277
+ "signed_count": 1,
3278
+ "sign": 1,
3279
+ "population_log_offset_code_units": 0.0,
3280
+ "fractional_lns_code": 102.0
3281
+ },
3282
+ {
3283
+ "code": 104,
3284
+ "phase": 0,
3285
+ "band": 13,
3286
+ "signed_count": -1,
3287
+ "sign": -1,
3288
+ "population_log_offset_code_units": 0.0,
3289
+ "fractional_lns_code": 104.0
3290
+ },
3291
+ {
3292
+ "code": 105,
3293
+ "phase": 1,
3294
+ "band": 13,
3295
+ "signed_count": 2,
3296
+ "sign": 1,
3297
+ "population_log_offset_code_units": 8.0,
3298
+ "fractional_lns_code": 113.0
3299
+ },
3300
+ {
3301
+ "code": 110,
3302
+ "phase": 6,
3303
+ "band": 13,
3304
+ "signed_count": -1,
3305
+ "sign": -1,
3306
+ "population_log_offset_code_units": 0.0,
3307
+ "fractional_lns_code": 110.0
3308
+ }
3309
+ ],
3310
+ "phase_fractional_lns_reference": [
3311
+ {
3312
+ "phase": 0,
3313
+ "sign": -1,
3314
+ "fractional_lns_code_unscaled": 105.61699059064368
3315
+ },
3316
+ {
3317
+ "phase": 1,
3318
+ "sign": 1,
3319
+ "fractional_lns_code_unscaled": 118.04213700021415
3320
+ },
3321
+ {
3322
+ "phase": 2,
3323
+ "sign": -1,
3324
+ "fractional_lns_code_unscaled": 97.06114439489473
3325
+ },
3326
+ {
3327
+ "phase": 3,
3328
+ "sign": 1,
3329
+ "fractional_lns_code_unscaled": 99.97226813872025
3330
+ },
3331
+ {
3332
+ "phase": 4,
3333
+ "sign": 1,
3334
+ "fractional_lns_code_unscaled": 100.37699129691221
3335
+ },
3336
+ {
3337
+ "phase": 5,
3338
+ "sign": 1,
3339
+ "fractional_lns_code_unscaled": 96.95084359392658
3340
+ },
3341
+ {
3342
+ "phase": 6,
3343
+ "sign": -1,
3344
+ "fractional_lns_code_unscaled": 106.14135510507049
3345
+ },
3346
+ {
3347
+ "phase": 7,
3348
+ "sign": 1,
3349
+ "fractional_lns_code_unscaled": 91.91767875292166
3350
+ }
3351
+ ],
3352
+ "expected_final_sign": 1,
3353
+ "expected_final_fractional_lns_code": 106.35649826822939,
3354
+ "expected_nearest_lns8_magnitude_code": 106,
3355
+ "note": "fractional_lns_code fields are correctness references from exact math; runtime candidate uses count-log and kappa LUTs without linear materialization"
3356
+ },
3357
+ "expected_direct_8phase_accumulators": [
3358
+ -9424,
3359
+ 25360,
3360
+ -3776,
3361
+ 4456,
3362
+ 4232,
3363
+ 2884,
3364
+ -5864,
3365
+ 1568
3366
+ ],
3367
+ "direct_8phase_note": "Same 8 integers as histogram sum_o H[phase][o]<<o; intended as exact dyadic routing reference."
3368
+ }
3369
+ ],
3370
+ "reduction_model": {
3371
+ "core_principle": "move reduction into the state domain to break serial arithmetic dependency",
3372
+ "reference_path": "signed state histogram -> dyadic integer shift/add -> wide fixed/Q9.6 optional",
3373
+ "pure_lns_path": "signed state histogram -> count-to-log fold -> fractional-LNS balanced add/sub -> scale log offset -> LNS8",
3374
+ "population_fold": "lambda_bucket = code + 8*log2(abs(signed_count)); sign = sign(signed_count)",
3375
+ "same_sign_add": "out=max(a,b)+8*log2(1+2^(-abs(a-b)/8))",
3376
+ "opposite_sign_add": "out=max(a,b)+8*log2(1-2^(-abs(a-b)/8)); equal magnitudes cancel to zero",
3377
+ "anchor_identity": "code = (120 + phase) + 8*(band - 15); code122 is exact 64",
3378
+ "final_phase_tree": "8 -> 4 -> 2 -> 1 balanced reduction"
3379
+ },
3380
+ "direct_8phase_reduction": {
3381
+ "excel_reference": "LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx",
3382
+ "product_layout": "S1|E4|L3",
3383
+ "formula": "A[L3] += sign * (1 << E4)",
3384
+ "final_formula": "S=scale*2^-9*sum_p A[p]*2^((p-2)/8)",
3385
+ "correctness_invariant": "direct decoded sum == direct A[0..7] reconstruction == histogram-derived A[0..7] reconstruction",
3386
+ "note": "phase masks are intermediate routing selectors; POPCOUNT is optional for the direct dyadic path"
3387
+ }
3388
+ }