Repository navigation
perf(mobile): bound KTO training memory per block #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,7 +220,10 @@ QwenConfig QwenConfig::from_pretrained(const std::string& path) { | |
| } | ||
|
|
||
| // -------------- Construction -------------- | ||
| QwenModel::QwenModel(const QwenConfig& cfg) : config_(cfg) { | ||
| QwenModel::QwenModel(const QwenConfig& cfg) : QwenModel(cfg, true) {} | ||
|
|
||
| QwenModel::QwenModel(const QwenConfig& cfg, bool allocate_weights) : config_(cfg) { | ||
| if (!allocate_weights) { layers_.resize(cfg.num_hidden_layers); return; } | ||
|
Comment on lines
+225
to
+226
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| const bool defer_large_placeholders = qwen_should_defer_large_placeholders(cfg); | ||
| if (!defer_large_placeholders) { | ||
| embed_tokens_ = std::make_shared<Tensor>(std::vector<int64_t>{cfg.vocab_size, cfg.hidden_size}, kFloat32, kCPU); | ||
|
|
@@ -627,6 +630,30 @@ TensorPtr QwenModel::mlp(const TensorPtr& x, QwenBlock& blk) { | |
| return down; | ||
| } | ||
|
|
||
| TensorPtr QwenModel::forward_attention_block(const TensorPtr& hidden, const TensorPtr& attention_mask, int layer) { | ||
| if (layer < 0 || layer >= config_.num_hidden_layers) throw std::invalid_argument("Invalid block index"); | ||
| TensorPtr pad_mask; | ||
| if (attention_mask) { | ||
| pad_mask = zeros({attention_mask->shape()[0], 1, 1, attention_mask->shape()[1]}, kFloat32, kCPU); | ||
| for (int64_t i = 0; i < attention_mask->numel(); ++i) | ||
| pad_mask->data<float>()[i] = attention_mask->data<float>()[i] > 0.5f ? 0.0f : -1e9f; | ||
| } | ||
| auto& block = layers_[layer]; | ||
| auto norm = rms_norm_affine(hidden, block.input_norm_weight, config_.rms_norm_eps); | ||
| return add(hidden, attention(norm, block, build_causal_mask(hidden->shape()[1]), pad_mask, hidden->shape()[1])); | ||
| } | ||
|
|
||
| TensorPtr QwenModel::forward_mlp_block(const TensorPtr& hidden, int layer) { | ||
| if (layer < 0 || layer >= config_.num_hidden_layers) throw std::invalid_argument("Invalid block index"); | ||
| auto& block = layers_[layer]; | ||
| auto norm = rms_norm_affine(hidden, block.post_norm_weight, config_.rms_norm_eps); | ||
| return add(hidden, mlp(norm, block)); | ||
| } | ||
|
|
||
| TensorPtr QwenModel::forward_block(const TensorPtr& hidden, const TensorPtr& attention_mask, int layer) { | ||
| return forward_mlp_block(forward_attention_block(hidden, attention_mask, layer), layer); | ||
| } | ||
|
|
||
| // -------------- Forward -------------- | ||
| TensorPtr QwenModel::forward_hidden(const TensorPtr& input_ids, const TensorPtr& attention_mask) { | ||
| auto x = embedding_lookup(embed_tokens_, input_ids); // [B,S,H] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any valid
tokenizer.jsonwhose rootadded_tokensarray 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 👍 / 👎.