File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -33,6 +33,28 @@ pub const BitNetConfig = struct {
3333// WEIGHT QUANTIZATION (F32 -> Ternary)
3434// ═══════════════════════════════════════════════════════════════════════════════
3535
36+ /// Fake quantization for weights: quantize to ternary and dequantize in-place
37+ /// Follows HuggingFace BitNet: s = 1/mean(|w|), result = round(w*s).clamp(-1,1)/s
38+ pub fn quantizeWeightsInPlace (weights : []f32 ) void {
39+ if (weights .len == 0 ) return ;
40+
41+ // Compute mean absolute value
42+ var sum : f32 = 0.0 ;
43+ for (weights ) | w | {
44+ sum += @abs (w );
45+ }
46+ const absmean = @max (sum / @as (f32 , @floatFromInt (weights .len )), 1e-5 );
47+ const scale = 1.0 / absmean ;
48+
49+ // Quantize to ternary and dequantize
50+ for (weights ) | * w | {
51+ const scaled = w .* * scale ;
52+ const rounded = @round (scaled );
53+ const clamped = @max (-1.0 , @min (1.0 , rounded ));
54+ w .* = clamped / scale ;
55+ }
56+ }
57+
3658/// Quantize F32 weights to ternary {-1, 0, +1}
3759/// Uses absmean quantization: w_q = round(w / scale), scale = mean(|w|)
3860pub fn quantizeToTernary (weights : []const f32 , output : []i8 , scale : * f32 ) void {
You can’t perform that action at this time.
0 commit comments