Skip to content

Commit 9a64b3e

Browse files
Antigravity Agentclaude
andcommitted
fix: Add quantizeWeightsInPlace function to bitnet_forward
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5ba7745 commit 9a64b3e

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/vibeec/bitnet_forward.zig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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|)
3860
pub fn quantizeToTernary(weights: []const f32, output: []i8, scale: *f32) void {

0 commit comments

Comments
 (0)