Skip to content

Add opt-in Qwen/Gemma checkpointing, disk offload and streaming backward - #11

Merged
BESTTOOLBOX merged 2 commits into
mainfrom
codex/qwen-gemma-memory-optimizations
Nov 12, 2026
Merged

BESTTOOLBOX merged 2 commits into
mainfrom
codex/qwen-gemma-memory-optimizations

Conversation

@AndyLu666

@AndyLu666 AndyLu666 commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Publish the locally developed memory paths through the current public API, rather than replacing the repository with the local training tree.

  • Add deterministic activation checkpointing with dependency capture, nested autograd-state restoration, and recomputation of block forwards.
  • Add frozen CPU FP32 backbone disk offload: checked disk round trips, owner-handle release, on-demand block reload, and logical residency / payload-I/O counters. LoRA and optimizer state remain resident.
  • Implement row-recomputed streaming attention backward for Q/K/V, and wire all three opt-in features into Qwen/Gemma models and the existing Qwen MMLU / Gemma 270M / Gemma 1B training CLIs.
  • Add tests, installed-header support, usage documentation, and a separate numerical validation/limitations note.

All three model switches default to off. GPT-2 fixes are deliberately excluded. This does not include or merge #6 / #7, change published experiment results, or upload models/data/binaries.

Validation

macOS arm64, Apple Clang; no phone/CUDA training claim:

  • Release with BLAS on and off: 23 actual CTest checks pass in each configuration; the additional TokenizerHFGolden entry self-skips because its external fixture is absent.
  • Warnings-as-errors build and regression suite pass.
  • ASan + UBSan with the memory pool disabled: all four new feature test targets pass.
  • Legacy-engine library build, installed-package consumer, and all three training CLI builds pass.
  • New CLI validation script: 12 asset-free subcases pass.
  • Attention tests cover direct forward/backward parity, 144 finite differences, masking, and direct checkpoint + attention composition.
  • Tiny Qwen/Gemma tests cover all eight combinations: gradients at identical parameter snapshots, plus independent three-update Adam trajectories.
  • Real-weight two-update diagnostics complete with adapters saved: Qwen2.5-0.5B / PIQA-derived MCQ input and Gemma 270M / PIQA JSONL.
  • Default-off Qwen matches all 540,672 adapter elements from a separately built current upstream. Checkpoint-only matches its dense baseline exactly; combined Qwen optimizations match streaming-only exactly. Gemma combined versus baseline maximum adapter difference: 1.67035e-7 across 1,898,496 elements.

Important numerical and resource limits

Streaming is not bitwise-equivalent to dense/GEMM attention. In the real Qwen two-update diagnostic, three of 540,672 adapter coordinates exceed the original absolute 2e-5 comparison limit; maximum difference is 6.54949e-5. That strict check remains a failure, not a claimed pass. At the worst first-step coordinate, saved Adam moments reproduce the amplification of near-zero FP32 gradient differences with unchanged epsilon=1e-8. This evidence does not establish equal long-run task accuracy.

The docs disclose this result and recommend standard attention when strict historical adapter reproduction is required. No optimizer settings were changed to make the check pass.

Other limits: CPU FP32, deterministic first-order checkpoint regions, no RNG replay / attention dropout / softcapping / mask gradients; legacy autograd keeps the standard-attention training fallback. Offload budget is not a process RSS cap; saved tensors/external aliases can retain storage, and OS caching means payload bytes are not physical disk I/O. No saving percentage or speedup is claimed. Real Gemma 1B and mobile-device training are not validated in this PR.

Reproduce

cmake -S operator -B build -DCMAKE_BUILD_TYPE=Release -DUSE_BLAS=ON -DBUILD_TESTS=ON
cmake --build build -j 4
ctest --test-dir build --output-on-failure
# Also run with -DUSE_BLAS=OFF in a separate build directory.

See docs/MEMORY_OPTIMIZATIONS.md for model/CLI commands and docs/MEMORY_OPTIMIZATION_VALIDATION.md for numerical scope. The current-main base is 086b1a0; unrelated existing files are preserved.

Release integration follow-up

The first Actions run rejected the historical operator/opt_ops directory and exposed a missing ripgrep dependency in release auditing. The sharder now lives under the public operator/finetune_ops/memory path; the release blacklist is unchanged. CI installs ripgrep when missing, and the audit fails explicitly if it is unavailable. Rebuilt BLAS/noBLAS, WERROR, sanitizer, legacy and clean installed-consumer checks pass after relocation. No training arithmetic was changed by the follow-up. Remote Actions validates the complete release tree.

Final CI status

Both push and pull-request Actions completed successfully on ffbc47a249701fdf9ad100817f1ad3ab672cd814. The PR workflow passed release auditing, WERROR build/CTest, both package consumers, Android visualizer build, all five existing training smoke runs and energy scheduler integration. The numerical limitations above are unchanged; a green build is not a claim of long-run accuracy equivalence.

…ing backward

Keep default training paths and GPT-2 unchanged. Include primitive/model/CLI regression tests, installation support, and explicit numerical limitations.
@BESTTOOLBOX
BESTTOOLBOX merged commit f970589 into main Nov 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants