Skip to content

Commit b86c660

Browse files
authored
Fix hybrid extra state size mismatch (#3499)
Signed-off-by: Evgeny <etsykunov@nvidia.com>
1 parent 9c33764 commit b86c660

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

‎tests/pytorch/distributed/fsdp2_tests/run_fsdp2_fused_adam.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,8 +2192,13 @@ def run_continuation_step(current_model, current_optimizer):
21922192
with te.autocast(enabled=True, recipe=hybrid_recipe):
21932193
ref_output = model(x).clone()
21942194

2195+
# CustomRecipe's _extra_state pickle size isn't guaranteed across models/
2196+
# ranks, so strip it like the DelayedScaling case above (#1860).
2197+
model_state = {
2198+
k: v for k, v in model.state_dict().items() if not k.endswith("_extra_state")
2199+
}
21952200
save_state = {
2196-
"model": model.state_dict(),
2201+
"model": model_state,
21972202
"optimizer": optimizer.state_dict(),
21982203
}
21992204
dcp.save(save_state, checkpoint_id=checkpoint_dir)
@@ -2213,12 +2218,15 @@ def run_continuation_step(current_model, current_optimizer):
22132218
F.mse_loss(out_tmp, target).backward()
22142219
optimizer2.step()
22152220

2221+
model2_state = {
2222+
k: v for k, v in model2.state_dict().items() if not k.endswith("_extra_state")
2223+
}
22162224
state_to_load = {
2217-
"model": model2.state_dict(),
2225+
"model": model2_state,
22182226
"optimizer": optimizer2.state_dict(),
22192227
}
22202228
dcp.load(state_to_load, checkpoint_id=checkpoint_dir)
2221-
model2.load_state_dict(state_to_load["model"])
2229+
model2.load_state_dict(state_to_load["model"], strict=False)
22222230
optimizer2.load_state_dict(state_to_load["optimizer"])
22232231

22242232
with torch.no_grad():

0 commit comments

Comments
 (0)