mktnhr commited on
Commit
42db578
·
verified ·
1 Parent(s): 9eb0db8

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +317 -3
README.md CHANGED
@@ -1,3 +1,317 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # BitState / HSDR PoC Technical Brief — v0.5
2
+
3
+ Status: experimental engineering proposal, not a performance claim.
4
+ Primary target: low-bit / ternary LLM GEMV, batch-1 decode first.
5
+ Suggested first integration target: PrismML llama.cpp fork / Bonsai 2, CPU first.
6
+
7
+ ## 1. Core hypothesis: move reduction into the state domain
8
+
9
+ BitState/HSDR v0.5 centers the PoC on one implementation question:
10
+
11
+ > **Move the reduction into the state domain to break the serial arithmetic reduction dependency.**
12
+
13
+ A conventional accumulator lets every new add depend on the previous partial sum. BitState instead:
14
+
15
+ 1. represents activation/weight products as final LNS8-like `S1|E4|L3` states using integer ADD/XOR;
16
+ 2. routes each product by `L3` toward one of eight phase aggregates;
17
+ 3. uses `±(1<<E4)` as the exact dyadic payload for the direct baseline;
18
+ 4. optionally constructs wide state-set masks / POPCOUNT histograms to reduce many lanes in parallel;
19
+ 5. reduces only the resulting eight dyadic accumulators or compressed non-zero state set;
20
+ 6. optionally keeps the population path entirely in fractional-LNS form.
21
+
22
+ The performance question is now split cleanly: **can K product states be reduced to eight phase aggregates cheaply enough on ordinary CPU/GPU integer/bit hardware?** POPCOUNT is one candidate mechanism, not a mandatory primitive for the direct eight-accumulator path.
23
+
24
+ ## 2. Why Bonsai 2 is a useful first target
25
+
26
+ Bonsai 2 provides a clean ternary test case:
27
+
28
+ - weights are ternary: `{-1,0,+1}`;
29
+ - PQ2_0 and PTQ1_0 use 128-weight groups with a shared scale;
30
+ - the runtime already has a model-specific Hadamard/sign-transform contract;
31
+ - one activation tile is reused across many output rows in GEMV;
32
+ - the PoC does not require a new GGUF / ggml type.
33
+
34
+ PQ2_0 or pre-decoded ternary masks are preferred for the first CPU microbenchmark so the BitState cost can be separated from PTQ1_0 dense base-3 unpacking.
35
+
36
+ References:
37
+ - https://github.com/PrismML-Eng/Bonsai-demo
38
+ - https://github.com/PrismML-Eng/Bonsai-demo/blob/main/MODEL-FORMATS.md
39
+ - https://github.com/PrismML-Eng/llama.cpp
40
+
41
+ ## 3. LNS8 state used by the PoC
42
+
43
+ ```text
44
+ code 0 = exact zero
45
+ code 1..127 = non-zero magnitude
46
+ sign = separate
47
+ |x| = 2^((code - 74)/8)
48
+ 8 codes = exactly x2
49
+ code 74 = 1.0
50
+ ```
51
+
52
+ For non-zero `k`:
53
+
54
+ ```text
55
+ phase = k & 7 // 0..7
56
+ band = k >> 3 // 0..15
57
+ ```
58
+
59
+ and
60
+
61
+ ```text
62
+ |x| = 2^-9 * 2^band * F[phase]
63
+ F[p] = 2^((p - 2)/8)
64
+ ```
65
+
66
+ The exact power-of-two band axis is what permits cheap relative positioning; only eight phase states remain inside one octave.
67
+
68
+ ## 3.1 Preferred dyadic correctness baseline: direct eight-phase accumulation
69
+
70
+ After all log-domain terms belonging to a product have been applied, interpret the final product as:
71
+
72
+ ```text
73
+ S1 | E4 | L3
74
+ ```
75
+
76
+ and reduce each lane as:
77
+
78
+ ```text
79
+ phase = L3
80
+ payload = sign * (1 << E4)
81
+ A[phase] += payload
82
+ ```
83
+
84
+ For one GEMV/GEMM output dot, all `K` products (including realistic `K=4096` / `5120`) reduce to only eight signed integer accumulators. Final materialization is:
85
+
86
+ ```text
87
+ S = 2^-9 * sum_p A[p] * F[p]
88
+ F[p] = 2^((p-2)/8)
89
+ ```
90
+
91
+ This is algebraically identical to building `H[phase][band]` first and then computing `A[p] = sum_b H[p][b] << b`.
92
+
93
+ The correctness reference is the procedure used in `LNS8_base74_8step_zone8_popcount_demo_JA_v0.8_bitslice128_validation.xlsx`: `Inputs_128` is treated as two Tile64 blocks, `L3` creates phase selector masks, `E4` supplies the power-of-two payload, and S1 supplies the sign. Phase masks are intermediate routing values, not final populations.
94
+
95
+ POPCOUNT is therefore **optional** for the direct dyadic path. It remains a separate implementation candidate when explicit state populations provide a cheaper dependency-breaking reduction.
96
+
97
+ ## 4. Ternary state-domain population reduction
98
+
99
+ For a 128-weight group:
100
+
101
+ ```text
102
+ w_i = d * t_i
103
+ t_i in {-1,0,+1}
104
+ ```
105
+
106
+ For the factorized ternary variant, the shared scale `d` may remain outside the population reduction. In the generic product-state reference, any scale/log offset that is intended to change the routed product code must be applied before `L3/E4` decomposition. These are separate variants and must not be mixed. Represent each ternary row with:
107
+
108
+ ```text
109
+ W_nonzero
110
+ W_negative
111
+ ```
112
+
113
+ For activation magnitude state `s`:
114
+
115
+ ```text
116
+ A_s = lanes with activation magnitude state s
117
+ M = A_s & W_nonzero
118
+ PNEG = activation_sign XOR W_negative
119
+ N = popcount(M)
120
+ Nneg = popcount(M & PNEG)
121
+ C_s = N - 2*Nneg
122
+ ```
123
+
124
+ `C_s` is the signed population of state `s` after ternary selection. The wide masks and POPCOUNTs are independent across active states, which is the dependency-breaking step.
125
+
126
+ ## 5. Two post-POPCOUNT paths
127
+
128
+ ### 5.1 Dyadic reference path
129
+
130
+ Keep the existing exact/algebraically simple baseline:
131
+
132
+ ```text
133
+ signed state histogram
134
+ -> per-phase integer shift/add
135
+ -> wide fixed-point
136
+ -> optional Q9.6
137
+ -> scale
138
+ -> optional LNS8 re-encode
139
+ ```
140
+
141
+ This path is essential for correctness comparison and may itself be competitive.
142
+
143
+ ### 5.2 Pure fractional-LNS candidate
144
+
145
+ A stronger v0.5 candidate is to avoid a linear accumulator entirely on the population path.
146
+
147
+ For signed state population `C[k] != 0`, fold repeated identical states first:
148
+
149
+ ```text
150
+ bucket_sign = sign(C[k])
151
+ bucket_lambda = k + 8*log2(abs(C[k]))
152
+ ```
153
+
154
+ For Tile128, `abs(C[k])` is only `1..128`, so the population correction can be a tiny LUT.
155
+
156
+ The eight phase tables may be normalized around anchor codes `120..127`; code 122 is the exact value 64. The identity
157
+
158
+ ```text
159
+ k = 8*band + phase
160
+ = (120 + phase) + 8*(band - 15)
161
+ ```
162
+
163
+ means band movement is only an integer code offset. The internal coordinate retains fractional code bits; it is not forced back to 8-bit LNS after each add.
164
+
165
+ Pairwise same-sign add for `a >= b`, `diff=a-b`:
166
+
167
+ ```text
168
+ out = a + kappa_plus(diff)
169
+ kappa_plus(diff) = 8*log2(1 + 2^(-diff/8))
170
+ ```
171
+
172
+ Opposite sign, `diff > 0`:
173
+
174
+ ```text
175
+ out = a + kappa_minus(diff)
176
+ kappa_minus(diff) = 8*log2(1 - 2^(-diff/8))
177
+ ```
178
+
179
+ Equal opposite magnitudes cancel to exact zero. Runtime uses small correction LUTs rather than general log/pow functions.
180
+
181
+ After each phase is compressed, reduce the eight phase aggregates as a balanced tree:
182
+
183
+ ```text
184
+ 8 -> 4 -> 2 -> 1
185
+ ```
186
+
187
+ This gives only three dependent LNS-add stages for the final eight-state reduction. The same balanced primitive can be used within a phase when multiple octave buckets remain.
188
+
189
+ A positive shared scale can remain in the log domain:
190
+
191
+ ```text
192
+ lambda_scaled = lambda_sum + 8*log2(d)
193
+ ```
194
+
195
+ so the pure path can stay logarithmic until the final LNS8 boundary.
196
+
197
+ ## 6. Critical implementation rule: reuse activation state sets
198
+
199
+ For batch-1 GEMV, the activation tile is common to many weight rows. Separate fixed and repeated costs:
200
+
201
+ ```text
202
+ activation encode / state-set construction // once per activation tile
203
+ weight-row mask intersection + POPCOUNT // repeated per row
204
+ compressed-state reduction // repeated per row/group
205
+ ```
206
+
207
+ Useful reuse sweep:
208
+
209
+ ```text
210
+ N = 1, 8, 32, 128, 512, 2048+
211
+ ```
212
+
213
+ Report the crossover instead of assuming it.
214
+
215
+ ## 7. Minimal PoC sequence
216
+
217
+ ### Phase A — standalone correctness
218
+
219
+ Implement:
220
+
221
+ - FP64 direct ternary dot;
222
+ - exact LNS8 decode reference;
223
+ - scalar direct eight-phase accumulation `A[L3] += ±(1<<E4)`;
224
+ - Tile64×2 reference matching the Excel v0.8 128-input procedure;
225
+ - signed POPCOUNT histogram;
226
+ - histogram-to-`A[0..7]` equivalence;
227
+ - dyadic final materialization;
228
+ - fractional-LNS pair add/sub reference;
229
+ - count-to-log folding for `n=1..128`;
230
+ - balanced pure-LNS reduction at several internal fractional precisions.
231
+
232
+ `direct decode == direct A[8] == histogram-derived A[8]` must be exact. Pure-LNS rounding error is a separate measurement.
233
+
234
+ ### Phase B — synthetic CPU microbenchmark
235
+
236
+ Compare:
237
+
238
+ 1. direct decoded ternary dot;
239
+ 2. scalar/direct eight-phase routed accumulator;
240
+ 3. Tile64 phase-mask + direct eight-accumulator reduction;
241
+ 4. signed histogram + dyadic reconstruction;
242
+ 5. signed histogram + pure fractional-LNS reduction.
243
+
244
+ Sweep state cardinality, ternary zero density, output-row reuse, `K`, and internal LNS precision.
245
+
246
+ ### Phase C — fork integration
247
+
248
+ First integrated target:
249
+
250
+ - CPU;
251
+ - batch 1;
252
+ - one ternary vec-dot / GEMV path;
253
+ - post-Hadamard activation domain;
254
+ - PQ2_0 first if implementation simplicity matters.
255
+
256
+ ### Phase D — end to end
257
+
258
+ Measure TG128, decode latency, state-set build cost, POPCOUNT cost, dependent reduction depth, memory traffic, cycles/output, and quality delta from activation LNS8 encoding.
259
+
260
+ ## 8. State-set implementation candidates
261
+
262
+ - direct eight-phase selector masks from `L2,L1,L0`;
263
+ - sparse `(state_id, lane_mask)` pairs for only active states;
264
+ - phase-local masks;
265
+ - SWAR / AVX2 byte-lane extraction + scalar POPCNT;
266
+ - AVX-512 mask/VPOPCNT where available;
267
+ - GPU ballot / match-any rather than literal CPU bit transpose.
268
+
269
+ Record unique states per tile for histogram variants, but also report the direct-path cost separately: product-state generation, phase-mask construction, K→8 weighted reduction, and final eight-phase materialization.
270
+
271
+ ## 9. Metrics that matter
272
+
273
+ CPU:
274
+
275
+ - ns / 128-weight group;
276
+ - cycles / output row and / weight;
277
+ - uops / IPC;
278
+ - state-set build cycles;
279
+ - POPCNT throughput;
280
+ - non-zero state buckets;
281
+ - fractional-LNS LUT accesses;
282
+ - dependent reduction depth / critical-path LNS stages;
283
+ - L1/L2 misses and memory bandwidth;
284
+ - reuse crossover vs baseline.
285
+
286
+ Correctness/quality:
287
+
288
+ - state histogram exactness;
289
+ - pure-LNS error vs FP64 by internal fractional precision;
290
+ - cancellation cases;
291
+ - LNS8 saturation / underflow;
292
+ - model quality / perplexity or task checks.
293
+
294
+ ## 10. Falsification criteria
295
+
296
+ The PoC should be allowed to fail cleanly. Redesign or stop if:
297
+
298
+ - state-set construction cannot be amortized;
299
+ - real activations have too many unique states;
300
+ - mask/POPCOUNT work exceeds the existing ternary path;
301
+ - pure-LNS LUT/add stages recreate a long critical path;
302
+ - required fractional precision makes the path too expensive;
303
+ - activation LNS8 quality loss is unacceptable;
304
+ - conversion/scale handling cancels the compute saving.
305
+
306
+ A negative result is useful if the limiting term is isolated.
307
+
308
+ ## 11. What this PoC is not claiming
309
+
310
+ - not a new GGUF type;
311
+ - not a replacement for all GEMM/GEMV kernels;
312
+ - not a claim that POPCOUNT always beats SIMD/Tensor Core MAC;
313
+ - not a claim of research novelty;
314
+ - not a requirement to use Q9.6 internally;
315
+ - not a claim that pure-LNS will necessarily beat the dyadic reference path.
316
+
317
+ The narrow question is whether **state-domain dependency breaking plus population reduction** has a competitive region in real low-bit / ternary decode workloads, and whether a pure fractional-LNS final reduction improves that region.