File size: 7,921 Bytes
2698a31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# BitState / HSDR PoC Experiment Matrix — v0.5

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.

## A. Correctness layers

| ID | Test | Input | Reference | Candidate output | Required result |
|---|---|---|---|---|---|
| C0 | LNS8 decode | all codes 0..127, both signs | FP64 formula | decoded value | exact within FP rounding |
| C1 | phase/band decomposition | all non-zero codes | direct formula | `phase=k&7`, `band=k>>3` | exact |
| C2 | direct 8-phase accumulation | K=128 vectors | direct LNS8->FP64 sum | `A[L3] += ±(1<<E4)` | exact within FP rounding |
| C2b | ternary signed population | K=128 vectors | direct state tally | POPCOUNT histogram | bit-exact state counts |
| C2c | direct-vs-histogram phase state | same vectors | direct `A[0..7]` | histogram `sum_o H[p][o]<<o` | bit-exact `A[0..7]` |
| C3 | dyadic reconstruction | signed histogram | exact LNS8->FP64 sum | shift/add reference | equivalent within FP rounding |
| C4 | population folding | `n=1..128` identical states | FP64 `n*x` | `k + 8*log2(n)` | exact in real math; quantify LUT rounding |
| C5 | fractional LNS same-sign add | exhaustive/sampled state pairs | FP64 add then log | `kappa+` path | quantify internal-code error |
| C6 | fractional LNS subtract | opposite signs incl. equality | FP64 subtraction | `kappa-` + zero handling | exact zero for equal magnitude; quantify others |
| C7 | balanced pure-LNS tree | random/adversarial K=128 | direct FP64 ternary dot | tree result | error vs internal fractional precision |
| C8 | shared scale in log domain | random positive scales | direct `d*sum` | log-offset scale | quantify scale-offset error |
| C9 | Q9.6 boundary | relevant output range | FP64 / wide fixed | Q9.6 | quantify rounding/saturation; not assumed lossless |

## B. State-distribution sweep

| Distribution | Why |
|---|---|
| one repeated state | best case for population folding |
| 8 phases, one band | isolates phase reduction |
| one phase, 16 bands | tests intra-phase balanced LNS reduction |
| octave/band sweep | tests exact +8-code relative positioning |
| uniform random codes | high-cardinality stress case |
| Gaussian-like encoded values | generic realistic distribution |
| captured post-Hadamard Bonsai 2 activations | primary real workload |

For each 64/128-element tile record unique-state count `U`, non-zero signed bucket count `B`, and active phase count `P`.

## C. Weight-distribution sweep

| Pattern | Purpose |
|---|---|
| all +1 | sign-free reference |
| alternating +/- | exact cancellation stress |
| {-1,0,+1} uniform | generic ternary |
| measured Bonsai 2 trit distribution | real workload |
| high zero fraction | tests skip / nonzero mask benefit |

## D. Reuse / amortization sweep

Build activation state representation once and reuse it for `N` output rows.

```text
N = 1, 8, 32, 128, 512, 2048
```

Report separately:

```text
T_encode       = activation LNS8 encoding
T_stateset     = reusable state-set construction
T_intersect    = one row's weight-mask intersection + POPCOUNT
T_reduce_dyad  = dyadic reference reduction
T_reduce_lns   = pure fractional-LNS reduction
T_scale        = scale application / log-offset handling

T_total_ref(N) = T_encode + T_stateset + N*(T_intersect + T_reduce_dyad + T_scale)
T_total_lns(N) = T_encode + T_stateset + N*(T_intersect + T_reduce_lns  + T_scale)
```

## E. Kernel variants

| ID | Variant | Purpose |
|---|---|---|
| B0 | current fork ternary dot | baseline |
| B1 | scalar direct decode | simple reference |
| D0 | scalar direct 8-phase accumulator | correctness baseline for `A[L3] += ±(1<<E4)` |
| D1 | Tile64 direct phase-mask routing | primary ordinary-CPU K→8 reduction candidate |
| D2 | Tile32 word-width equivalent | CPU/GPU width-invariance correctness check |
| P0 | scalar signed histogram | validates state-count math |
| P1 | full-state masks rebuilt per row | intentionally unamortized comparison |
| P2 | reusable sparse state masks | primary dependency-breaking hypothesis |
| P3 | phase-local signed histogram | compares explicit population path with direct A[8] |
| P4 | AVX2/SWAR state construction + POPCNT | optimized CPU state front-end |
| R0 | dyadic shift/add materializer | reference post-POPCOUNT path |
| L0 | scalar fractional-LNS add/sub | correctness reference |
| L1 | balanced sparse-bucket pure-LNS tree | primary pure-LNS CPU hypothesis |
| L2 | 8-phase normalized `120..127` anchor path | tests relative-state normalization |
| P5 | PTQ1_0 integrated decode | dense-packing follow-up |
| G0 | CUDA ballot state-per-lane | GPU front-end candidate |
| G1 | CUDA match-any grouping + balanced state reduction | GPU candidate |

## F. K sweep

```text
K = 32, 64, 128, 256, 4096, 5120, 8192
```

K=128 is especially important because it aligns with Bonsai 2 scale groups.

## G. Fractional-LNS precision sweep

Test internal fractional code bits independently of storage LNS8:

```text
F = 4, 6, 8, 10, 12, 14
```

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.

## H. Reduction-order / dependency sweep

Compare:

```text
serial left fold
balanced tree
phase-local balanced tree -> 8-phase 8->4->2->1 tree
sparse global balanced tree
```

Report both operation count and **dependent critical-path stages**. This is a first-class metric, not a secondary implementation detail.

## I. CPU metrics

- ns / 128-weight group
- cycles / output row
- cycles / weight
- instructions / weight
- uops / IPC
- product-state ADD cost
- phase-mask / selector build cost
- direct K→8 weighted reduction cost
- state-set build cost for histogram variants
- unique states / non-zero signed buckets
- POPCNT count and throughput
- fractional-LNS LUT loads
- dependent LNS-add/sub stages
- L1/L2 misses
- actual memory bandwidth
- crossover reuse count vs baseline

## J. End-to-end metrics

- TG128
- PP512 as secondary context
- model load / memory footprint unchanged unless explicitly testing metadata
- quality/perplexity or task check vs existing fork
- LNS8 saturation / underflow rate
- pure-LNS vs dyadic end-to-end delta

## K. Reporting rule

Always report at least these components separately:

```text
1. activation / product-state encoding
2. phase-mask / selector construction
3. direct K→8 weighted reduction
4. state-set construction + POPCOUNT (histogram variants)
5. eight-phase dyadic finalization
6. pure-LNS reduction cost
7. dependent critical-path depth
8. end-to-end integrated result
```

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.

## L. Reference artifact for direct 8-phase correctness

Use `LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx` as the visual reference for the direct path:

- `Inputs_128` -> two Tile64 blocks;
- `S1|E4|L3` decomposition;
- phase masks as intermediate routing selectors;
- `±(1<<E4)` routed into eight integer accumulators;
- equality against direct decoded sum.

This workbook proves correctness of the reduction identity, not performance.

## M. Stop / continue decision

Continue optimization if at least one realistic region shows either:

- lower per-output-row cost after amortization;
- materially lower dependent reduction depth with competitive total work;
- lower memory traffic with competitive latency;
- pure-LNS finalization beats or simplifies the dyadic materializer without unacceptable quality loss.

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.