fix(weighter): joint_update_aware follow-ups to #58 - #59
Merged
Conversation
Behaviour-preserving fixes plus the docs #58 left out. Default hyper-parameters and the solver math are deliberately untouched. - Use eval mode when extracting embeddings: _extract_embeddings forced model.train() before a no_grad forward, turning dropout on and adding noise to S and s. _compute_eval_target already used eval mode; this makes the two consistent and restores the original mode afterwards. No effect at the default lora_dropout 0.0. - Make the anchor direction u averageable: _compute_eval_target pulled a single eval batch, so at the default target_batch_size 1 u was one sample's embedding, not the 'eval set mean' its docstring claimed. Adds target_num_batches (default 1, unchanged); set 0 to average the whole eval set. The mean is sample-weighted so a short trailing batch no longer overcounts. - Drop the unused seed parameter: stored and exposed in components.yaml but never read, implying controllable randomness. - Document the method in skills/how_to_use.md (choices, algorithm table, examples tree, run command) and the Data Reweighting tables in both READMEs, mirroring what #54 did for ADAPT. - Add the missing train_step to the example config, matching loss.yaml and adapt.yaml. Verified: solver returns bit-identical weights at shipped defaults, target_num_batches=1 reproduces the previous single-batch u, model is left in train mode after embedding extraction, component still builds through the registry from the shipped components.yaml. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
概述
joint_update_aware(#58)的 follow-up:修几处保持行为不变的问题,并补上 #58 没有更新的文档。默认超参和 solver 的数学都没有改动 —— β / τ / damping 的取值属于需要实验数据支撑的决定,我把相关观察单独写在 #58 的 review comment 里,留给 @tongruiliu 定夺。
改动
1. 取 embedding 时改用 eval 模式
_extract_embeddings在no_grad前向之前无条件调了model.train():这一步只为取 embedding,不需要训练模式,而 train 模式会打开 dropout,让余弦 Gram 矩阵
S和效用向量s带上随机噪声。同文件的_compute_eval_target用的是model.eval(),两处不一致。改为 eval 模式并在结束后恢复原模式。本仓库
lora_dropout默认 0.0,所以当前默认配置下没有实际影响;一旦用户开启 dropout 就会中。2. 让 anchor 方向
u可以真正做平均_compute_eval_target只取一个 eval batch 求均值,而target_batch_size默认是 1 —— 也就是说默认情况下u是单个样本的 embedding,而不是 docstring 里写的"eval 集平均句向量"。(对比adapt_weighter._refresh_anchors是遍历整个 eval 集。)新增
target_num_batches:1,行为与之前完全一致0则遍历整个 eval 集另外均值改为按样本数加权(
sum / count而非逐 batch 的mean再平均),这样末尾不满的那个 batch 不会被当成一个完整 batch 计权。3. 移除未使用的
seedseed被存进self.seed但从未读取,且暴露在components.yaml里,会让人以为可以控制该组件的随机性。4. 补文档
#58 只改了 4 个文件,漏了文档;#54(ADAPT)当时是连
skills/how_to_use.md一起改的。本 PR 补齐同样的位置:skills/how_to_use.md:component_name的 choices、算法表、examples/目录说明、运行命令README.md/README-zh.md:Data Reweighting 表格并说明了这个方法与前面几个 pointwise 方法的区别(在全局 batch 上求解、需要
eval_dataset、beta: 0/objective_mode: align_only可退化为 pointwise)。5. 示例配置补
train_steptrain_step默认为 0,所以原示例实际走的是num_train_epochs,而loss.yaml/adapt.yaml都给了train_step: 500。补上以保持一致。验证
不依赖 GPU 的等价性检查,全部通过:
_solve_weights的输出target_num_batches=1utarget_num_batches=0_extract_embeddings之后的model.trainingTruecomponents.yaml经 registry 构造组件另外
py_compile通过,两个 yaml 均可yaml.safe_load,CI 的 import check 项在本地等价验证通过。🤖 Generated with Claude Code