Skip to content

fix(trainer): release the device cache through the framework helper before the metrics gather - #343

Merged
tastelikefeet merged 1 commit into
modelscope:mainfrom
li-lizhe:fix/optimizer-group-device-cache-release
Nov 6, 2026
Merged

tastelikefeet merged 1 commit into
modelscope:mainfrom
li-lizhe:fix/optimizer-group-device-cache-release

Conversation

@li-lizhe

Copy link
Copy Markdown
Contributor

OptimizerGroup.calculate_metrics releases the caching allocator before the metrics gather, for the reason written next to it: the collective behind the gather takes device memory outside torch's caching allocator, so the rank with the least free memory raises inside it while the other ranks wait there.

The release was written as torch.cuda.empty_cache() behind a torch.cuda.is_available() and torch.cuda.is_initialized() guard, so on an Ascend NPU it never ran. calculate_metrics runs on every backend, and the repository already has the backend-agnostic accessor for this: Torch.empty_cache() in twinkle/utils/framework.py dispatches to torch.npu on an NPU, to CUDA on a GPU, and does nothing when there is no accelerator. This change calls that helper instead.

On a CUDA machine the behaviour is unchanged; on an NPU the gather now runs with the freed memory available, which is what the comment describes as the difference between "one rank raised inside all_gather_object while the other seven waited in it forever" and "the same step completes in 5 ms".

Tests

tests/model/test_optimizer_group_cache_release.py pins the release to the shared helper and keeps the existing condition that a step without metrics does not touch the allocator.

revision result
main (5337cc3) + this test file 1 failed, 1 passed — AssertionError: calculate_metrics must release the device cache before the metrics gather
this branch 2 passed

Run on a Linux CPU host, Python 3.11.15, torch 2.8.0+cpu, pytest. yapf -d --style setup.cfg, flake8 and isort --check-only are clean on the changed file.

Not tested: the 8xH20 reproduction in the comment and the same gather on an Ascend NPU (this machine has no NPU). The change swaps a CUDA-only call for the helper the repository uses elsewhere for the same purpose, so the accelerator behaviour follows Torch.empty_cache().

OptimizerGroup.calculate_metrics released the caching allocator through
torch.cuda.empty_cache() behind a CUDA-only guard, so the release before the metrics
gather never ran on an Ascend NPU. Use the framework's Torch.empty_cache(), which
dispatches to the accelerator in use.

Signed-off-by: li-lizhe <[email protected]>
@li-lizhe

li-lizhe commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@tastelikefeet Friendly ping on this PR when you have a moment — thanks!

@tastelikefeet

Copy link
Copy Markdown
Collaborator

Thanks for contribution! Sorry for replying late. We are now busying writing swift v5 code, so PR review may be a little slower

@tastelikefeet
tastelikefeet merged commit decf0de into modelscope:main Nov 6, 2026
2 of 3 checks passed
tastelikefeet added a commit that referenced this pull request Nov 28, 2026
main carried 17 commits (#311..#291) over merge-base 96dab68. v5 had already ported
everything up to #312 (a4ddc3b) and moved server/client to twinkle_cs, so:

- server/client (src/twinkle/server, twinkle_cs/*, tests/server, tests/twinkle_client):
  kept the v5 tree; the legacy src/twinkle/server path and the old ci_container_test.sh
  stay removed on v5.
- loss: #291 detached DFT weights, #335 Liger CE contract (delegates micro_batch_scale /
  batch_normalizer to CrossEntropyLoss), #334 chunked CE comments, #336 InfoNCE tests.
- model: #343 Torch.empty_cache (auto-merged), #385 fused_lm_rl ported onto the v5 task
  registry (processor/tasks.py + model/tasks.py FusedRL + transformers postprocess),
  #386 NPU fused CE fast path in selective_log_softmax.
- twinkle_agentic: #345 check kinds (hidden_python / sql_query / answer_one_of /
  answer_number / web_receipt / lean) adapted to the episode-scoped Env interface;
  AgenticChallenger gains the _verify_check / _proposal_reward hooks.

Known: cookbook/rsi and tests/cookbook/rsi (#345) are written against main's stateless
Env / AgenticChallenger API and do not run on v5 yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants