From 6976a134ddbb572f073c00dc7893a27c303db2f9 Mon Sep 17 00:00:00 2001 From: Dmitrii Vasilev Date: Sun, 18 Oct 2026 11:19:47 +0700 Subject: [PATCH] spec(ml): train_slice_model -- reuse file_io_contract's byte_sum instead of a copy (Closes #10206) #10186 added byte_total to specs/ml/train_slice_model.t27, a body identical to the private byte_sum in specs/ml/file_io_contract.t27, which the spec already reaches through `use ml::file_io_contract`. The Duplicate Body Ratchet has been red on master since. byte_sum becomes pub; byte_total is deleted and its two callers call byte_sum. No ledger is blessed. Lab, master 94d279326 + this change: train_slice_model 5 tests, 0 FAIL, 0 vacuous; file_io_contract 0 of 12 vacuous; host_io_model 0 of 3 vacuous. The --backend ml binary exits 0 and writes the same 379-byte /tmp/t27_train_slice.bin, sha256 0dfdef22...cdbeb; with that path made a directory it exits 134. python3 tools/dupe_scan.py exits 0: "no new duplicate body, and none of the known groups grew". Co-Authored-By: Claude Opus 5.5 --- specs/ml/file_io_contract.t27 | 2 +- specs/ml/train_slice_model.t27 | 15 ++------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/specs/ml/file_io_contract.t27 b/specs/ml/file_io_contract.t27 index 07969972ec..6ea9b36c56 100644 --- a/specs/ml/file_io_contract.t27 +++ b/specs/ml/file_io_contract.t27 @@ -235,7 +235,7 @@ pub fn get_u32(buf: []const u8, off: i32) -> u32 { return x; } -fn byte_sum(buf: []const u8, n: i32) -> u32 { +pub fn byte_sum(buf: []const u8, n: i32) -> u32 { var s : u32 = 0; var i : i32 = 0; while (i < n) { diff --git a/specs/ml/train_slice_model.t27 b/specs/ml/train_slice_model.t27 index 332cce6ded..3698852929 100644 --- a/specs/ml/train_slice_model.t27 +++ b/specs/ml/train_slice_model.t27 @@ -95,17 +95,6 @@ fn get_f32(buf: []const u8, off: i32) -> f64 { return @floatCast(f); } -fn byte_total(buf: []const u8, n: i32) -> u32 { - var s : u32 = 0; - var i : i32 = 0; - while (i < n) { - const b : u32 = @intCast(buf[@intCast(i)]); - s = s + b; - i = i + 1; - } - return s; -} - /// Train the slice for `steps` steps of learning rate `lr` from its fixed initial state. /// losses[0] is the mean loss before any step, losses[1] the mean loss after the last one. pub fn train(steps: u32, lr: f64, losses: []f64) -> void { @@ -148,7 +137,7 @@ pub fn pack(buf: []u8, steps: u32, losses: []const f64) -> void { buf[@intCast(OFF_TRITS + k)] = code; k = k + 1; } - put_u32(buf, OFF_SUM, byte_total(buf, OFF_SUM)); + put_u32(buf, OFF_SUM, byte_sum(buf, OFF_SUM)); } /// Accept a checkpoint image only if its magic, its checksum and the losses it carries are right: @@ -157,7 +146,7 @@ pub fn check_image(buf: []const u8) -> i32 { if (buf[0] != MAGIC0 or buf[1] != MAGIC1 or buf[2] != MAGIC2 or buf[3] != MAGIC3) { return TRAIN_ERR_MAGIC; } - if (get_u32(buf, OFF_SUM) != byte_total(buf, OFF_SUM)) { + if (get_u32(buf, OFF_SUM) != byte_sum(buf, OFF_SUM)) { return TRAIN_ERR_SUM; } const first : f64 = get_f32(buf, OFF_FIRST);