Skip to content

perf(mobile): bound KTO training memory per block - #3

Merged
BESTTOOLBOX merged 1 commit into
codex/parallel-demo-dashboardfrom
codex/kto-bounded-memory
Oct 15, 2026
Merged

BESTTOOLBOX merged 1 commit into
codex/parallel-demo-dashboardfrom
codex/kto-bounded-memory

Conversation

@Johnnybyzhang

Copy link
Copy Markdown

Summary

These changes let FedCampus's disk-backed KTO trainer run one Qwen decoder block at a time with bounded memory.

  • Per-block forward: QwenModel(cfg, /*allocate_weights=*/false) skips weight allocation. forward_attention_block, forward_mlp_block and forward_block let a caller run one layer with weights it loads itself.
  • Backward memory: Engine::run_backward disables graph recording during backward. It frees each node's saved tensors once all of its consumers have run, instead of holding the forward graph until backward finishes.
  • Tokenizer: tokenizer.json is read as a stream to pull out only the root added_tokens array, instead of copying the full vocabulary into memory twice. This also fixes brackets inside token strings, which used to close the array early.

Testing

  • test_tokenizer_factory passes, including the new test_streamed_added_tokens.
  • test_autograd_leaf_accum passes.
  • On a physical iPhone 14 Pro, FedCampus's bounded trainer ran foreground and overnight background KTO with Qwen3 0.6B. Peak footprint was 191 MB foreground and about 80 MB per background session.

🤖 Generated with Claude Code

Supports the FedCampus disk-backed KTO trainer, which streams one decoder
block at a time instead of holding the whole Qwen model.

- QwenModel can be constructed without allocating weights, and exposes
  forward_attention_block / forward_mlp_block / forward_block so a caller can
  run one layer with weights it loads itself.
- Engine::run_backward disables graph recording during backward and frees
  each node's saved tensors once all of its consumers have run, instead of
  keeping the forward graph alive until backward finishes.
- The BPE tokenizer streams tokenizer.json to extract the root added_tokens
  array instead of reading the whole vocabulary into memory twice. It also
  handles brackets inside token strings, which previously closed the array
  early. Covered by test_streamed_added_tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-13T07:26:15.024373Z ce73d66 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ce73d66c78

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +225 to +226
QwenModel::QwenModel(const QwenConfig& cfg, bool allocate_weights) : config_(cfg) {
if (!allocate_weights) { layers_.resize(cfg.num_hidden_layers); return; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve strict shape checks in allocation-free mode

When allocate_weights is false, every weight slot remains null, so qwen_assign_checked skips its strict_shape_check && slot comparison even though strict checking defaults to enabled. A malformed streamed checkpoint can therefore install an incorrectly sized normalization weight and make rms_norm_affine read past its buffer, or accept projection shapes that fail only during the later forward pass. Validate assigned tensors against dimensions derived from QwenConfig rather than relying on allocated placeholders.

Useful? React with 👍 / 👎.

Comment on lines +335 to +336
while (array_depth && f.get(c)) {
if (slice.size() >= 1024 * 1024) throw std::runtime_error("added_tokens array exceeds 1 MiB");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not reject valid large added-token arrays

Any valid tokenizer.json whose root added_tokens array exceeds 1 MiB now throws during tokenizer construction, whereas the previous parser had no such size restriction. Tokenizers with many added vocabulary entries or long added-token contents therefore become unloadable; preserve bounded memory by parsing entries incrementally instead of imposing an undocumented fixed limit.

Useful? React with 👍 / 👎.

@BESTTOOLBOX
BESTTOOLBOX merged commit 4683432 into codex/parallel-demo-dashboard Oct 15, 2026
0 of 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