refactor(tensor,arithmetic)!: O(chunk) conversion/accumulate scratch (#296 Stage 2)#338
Merged
Conversation
…— fixed 256-element streaming replaces tensor-sized VLAs (#296 Stage 2) - chunk infrastructure: ODT_CONVERSION_CHUNK_ELEMS (256, multiple of 8 so packed chunk starts stay byte-aligned), packChunkGuarded (abort-on-overflow, same message/exit as the old fit guard), unpack{Sign,Zero}ExtendChunk, public dequantChunkToFloat, quantizeFloatToAsym split into grid + emit - every element-local TensorConversion cell rewritten as a chunk stream: pack family, four two-pass derived-grid cross-cells, nine unpack/dequant cells (two became staging-free); grid scans never round, exactly one roundByMode per element in element order -- SR-RNG streams bit-identical - streamed accumulate engines (FixedGrid/Rescale/AsymRescale via incSrc_t; float* signatures unchanged) + tensor-typed epilogue entry points; new accumulateSymInt32IntoSymInt32Rescale reproduces Strategy A bit-for-bit (pinned against the live Add.c implementation); executeOp Phase-4 staging VLAs and incrementAsFloatView deleted; the float->SYM_INT32 dynamic-rescale arm stays byte-identical as the documented residual (spec §5) - packFitGuarded deleted (last caller gone); TensorConversion compiles clean under -Werror=vla - stack-watermark budgets recalibrated to measured Stage-2 peaks: float 27768 B (+440 B chunk-buffer frames), sym 51968 B (repack chain eliminated; was 100080 post-Stage-1, 149008 pre-#296) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
6341930 to
c9872f6
Compare
38b2797 to
a059828
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #334 (Stage 1) — merge that first; this PR then retargets to develop. Note the known stacked-PR caveat: CI is vacuous until retarget (ci.yml triggers on main/develop targets only); local verification below stands in until then, and the workflow run must be verified after retarget.
Stage 2 of the bounded-scratch design (#296; spec
docs/superpowers/specs/2026-07-09-bounded-scratch-design.md). Executed via SDD: five task reviews + final whole-branch review (verdict: merge-ready after one fix wave, applied).What changes
Every element-local conversion/accumulate path streams in fixed 256-element chunks (
ODT_CONVERSION_CHUNK_ELEMS, multiple of 8 so packed chunk starts stay byte-aligned for any qBits):packChunkGuarded(fit-guard → abort-on-overflow, same message/exit; the old check-all-before-write existed only for an unrecoverableexit(1)),unpack{Sign,Zero}ExtendChunk, publicdequantChunkToFloat,quantizeFloatToAsymsplit into grid-derivation + chunked emit.TensorConversion.ccompiles clean under-Werror=vla.accumulateSymInt32IntoSymInt32Rescalereproduces Strategy A bit-for-bit — pinned against the liveAdd.cimplementation in-process (testAccDynamicSymIntoSymBitEqualsAddSymInplace). executeOp's Phase-4 staging VLAs andincrementAsFloatVieware gone.Bit-identity
The acceptance bar for every rewrite: same per-element expressions, grid scans never round, exactly one
roundByModeper element in element order (SR-RNG streams unchanged). Sanctioned deltas only: n==0 no-ops (old was VLA-UB), abort-on-overflow timing, the two watermark fixture values. Whole-branch review verified the epilogue routing table combination-by-combination (no arm lost or silently changed) and chunk-size contracts at every internal call site.Measured effect (HAR, EPOCHS=1, macOS arm64)
Total provisioned training RAM (heap categories + stack): sym8 ≈ 201.6 KiB now beats float ≈ 207.9 KiB — the #322 inversion is resolved at HAR scale; sym8's total dropped 32 % vs pre-#296. Remaining stack floor = one unpacked operand row + rawData at the largest tensor (Option-4/chunked-kernels territory, documented residual).
Verification
ctest --preset unit_test_debug72/72;uv run pytest29/29; examples rot-guard builds all targets.Closes #296 (manual close after develop merge; closing comment will record the residual table: funnel operand row + rawData, float→SYM_INT32 arm, kernel/layer VLAs, VLA-aware alloc-locality check → Option-4 follow-ups).
🤖 Generated with Claude Code