Repository navigation
fix(model): preserve fixed micro-batching for packed inputs - #417
Conversation
kevssim
left a comment
There was a problem hiding this comment.
🤖 本评论由 AI(Factory Droid)自动生成的代码审查,仅供参考,请以人工判断为准。
默认配置下这个修复是正确的:与 0058e35b 之前的行为一致。但开启 packing 后,非默认的 micro_batch_order 和动态分批仍会触发同一个 TypeError,详见行内评论。
另外建议补一个回归测试(P3):在 tests/model/test_micro_batch.py 中用 packed 输入调用 plan_micro_batches,覆盖默认配置和至少一种非默认 micro_batch_order。
| """ | ||
| if not inputs: | ||
| raise ValueError("cannot plan micro-batches for empty inputs") | ||
| if not config.dynamic_batching and config.micro_batch_order == "none": |
There was a problem hiding this comment.
🤖 本评论由 AI(Factory Droid)自动生成的代码审查,仅供参考,请以人工判断为准。
[P2] 建议修复 sequence_length 对 packed 输入的支持,而不是只绕开一条路径
这两行只在「动态分批关闭且 micro_batch_order="none"」时跳过长度读取。开启 packing 后(每个输入是一组样本组成的列表),只要设置了下面任一项,仍会在第 1 步前报同样的 TypeError: list indices must be integers or slices, not str:
micro_batch_order="descending"或"ends_small"(TransformersModel文档中公开的选项)dynamic_batching=True
另外 dp_balance.py:243 和 megatron.py:574 也会对同样的输入调用 sequence_length。
根因是第 57 行的 sequence_length 假定每个输入都是字典。仓库里 count_loss_tokens(src/twinkle/loss/utils.py:7)已经处理了这种情况:遇到列表就把每个样本的结果相加。建议照着改:
def sequence_length(model_input) -> int:
if isinstance(model_input, (list, tuple)):
return sum(sequence_length(item) for item in model_input)
input_ids = model_input["input_ids"]
return int(input_ids.shape[-1]) if hasattr(input_ids, "shape") else len(input_ids)这样所有路径都能用,这里新加的两行也就不需要了。
本地复现(PR 分支的 micro_batch.py,torch 用空模块替代,packed 输入):
| 配置 | 结果 |
|---|---|
| 默认 | [[0, 1], [2]] |
micro_batch_order="descending" |
TypeError |
dynamic_batching=True, max_tokens_per_micro_batch=8 |
TypeError |
There was a problem hiding this comment.
已按要求将修复放在共享的 sequence_length() 中。并且在 tests/model/test_micro_batch.py 中新增回归用例,全部通过。
PR type
PR information
引入微批排序后,默认固定分批路径也会读取每个输入的 input_ids。开启 packing 时,Swift 传入的每个元素是一组样本组成的列表,微批规划把它当作字典读取,导致训练在第 1 步前报 TypeError: list indices must be integers or slices, not str。
当动态分批关闭且 micro_batch_order='none' 时,恢复按输入数量直接分组。显式排序和动态分批仍沿用现有逻辑。