Repository navigation
perf(mobile): bound KTO training memory per block - #3
Conversation
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| QwenModel::QwenModel(const QwenConfig& cfg, bool allocate_weights) : config_(cfg) { | ||
| if (!allocate_weights) { layers_.resize(cfg.num_hidden_layers); return; } |
There was a problem hiding this comment.
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 👍 / 👎.
| while (array_depth && f.get(c)) { | ||
| if (slice.size() >= 1024 * 1024) throw std::runtime_error("added_tokens array exceeds 1 MiB"); |
There was a problem hiding this comment.
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 👍 / 👎.
4683432
into
codex/parallel-demo-dashboard
Summary
These changes let FedCampus's disk-backed KTO trainer run one Qwen decoder block at a time with bounded memory.
QwenModel(cfg, /*allocate_weights=*/false)skips weight allocation.forward_attention_block,forward_mlp_blockandforward_blocklet a caller run one layer with weights it loads itself.Engine::run_backwarddisables 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.jsonis read as a stream to pull out only the rootadded_tokensarray, 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_factorypasses, including the newtest_streamed_added_tokens.test_autograd_leaf_accumpasses.🤖 Generated with Claude Code