Add joint_update_aware weighter - #58
Merged
Merged
Conversation
haolpku
added a commit
that referenced
this pull request
Aug 9, 2026
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 Reweighting(
本方法是 batch 级、joint-update-aware 的:一个样本的边际价值不再独立计算,而是取决于当前加权 batch 已经覆盖的方向。它把一个 pointwise效用信号
s修正为单纯形上的 batch 相关权重,求解带熵正则的目标:s_i = <u, z_i>:样本 embeddingz_i对目标方向u的对齐度(pointwise 效用)S = Z Z^T:L2 归一化 embedding 的余弦 Gram 矩阵,作为 梯度交互矩阵的廉价代理(避免存每样本的全参数梯度)
H(w):熵正则,保证内部唯一最优唯一内部最优是定点
w_i = softmax([s - beta·Sw] / tau)_i,用阻尼迭代求解:w <- (1 - rho) w + rho · softmax([s - beta·Sw] / tau)。改动文件
src/dataflex/train/weighter/joint_update_aware_weighter.pyJointUpdateAwareWeightersrc/dataflex/train/weighter/__init__.pyJointUpdateAwareWeightersrc/dataflex/configs/components.yamljoint_update_aware组件及默认超参examples/train_lora/weighters/joint_update_aware.yaml实现要点
Weighter,实现get_weighted_loss,走已有的WeightTrainer动态加权流程(
train_type: dynamic_weight),无需改动 trainer。pooling池化为每样本句向量后 L2 归一化。u:取eval_dataset(anchor / 目标分布集)的平均 embedding,每target_update_step步用当前模型重算并缓存;eval 不可用时回退为当前 batch 的平均 embedding。
all_gather,在 全局 batch 上求解权重,再按 rank 切回本卡;最后
× world_size抵消 DDP 的跨卡梯度平均,使加权目标尺度与单机一致。可配置开关
核心超参:
betataufixed_point_itersdampingtarget_update_steptarget_batch_sizeembed_normalizeembed_layer功能开关:
pooling:last_token(默认)/mean_pool(在 response token 上取均值)——两种句向量池化方式。objective_mode:消融用开关,控制求解时的 score 项full(默认):s - beta·Swalign_only:s(只对齐,退回 pointwise)diverse_only:-beta·Sw(只压冗余)uniform:直接1/B(均匀权重,对照基线)用法
关键配置:
train_type: dynamic_weight、component_name: joint_update_aware,并将eval_dataset设为目标分布 / anchor 数据集。
测试
python -m py_compile通过,components.yaml与示例 yaml 均 YAML 解析通过。