Repository navigation
Conversation
310ac17 to
f4cc142
Compare
|
Nice improvement! The parameter-device check gives a much clearer error for offloaded models. One edge case I noticed from reading the lifecycle methods: In Megatron, that call releases grad-buffer storage unless Would it make sense to cover that transition with a training-readiness check and a regression test, while preserving supported CPU-offload configurations and |
f4cc142 to
9540bea
Compare
Review feedback on NVIDIA-NeMo#3690: the device check alone is not enough. prepare_for_lp_inference() moves parameters onto CUDA, so _assert_model_onloaded() passes, but with keep_train_buffers=False it also releases Megatron grad-buffer storage and moves optimizer state to CPU when offload_optimizer_for_logprob is set. A train(eval_mode=False) reached from there would clear the existing guard while the training state is still parked, dropping accumulated gradients or failing on a CPU-resident optimizer. Record the parking in _training_state_parked at the points that actually do it -- the grad-buffer release and the optimizer offload in each worker's prepare_for_lp_inference -- and clear it in prepare_for_training, which is what restores them. train() consults it via _assert_training_state_restored() only when eval_mode is False, since a forward-only eval pass needs neither buffers nor optimizer. Setting the flag from the offloading branches rather than from keep_train_buffers alone keeps two supported paths quiet: a configuration that offloads nothing never sets it, and keep_train_buffers=True leaves the training state alone, so an already-open train step is unaffected. Tests: the parked-state error and its message, the restored case, the default, and a structural check that each worker which sets the flag also clears it in prepare_for_training -- the asymmetry that would otherwise wedge every later train step. Verified the structural check fails against a worker with the clear removed. Closes NVIDIA-NeMo#1141 Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Vishnu Rajeev <[email protected]>
|
Thanks — you're right, and I confirmed it against the current code rather than taking it on faith.
So What it does. A On preserving the two cases you named. Setting the flag from the offloading branches rather than from
I also checked the call ordering in Tests. The parked-state error and its message, the restored case, the default, plus a structural check that every worker setting the flag also clears it in The branch is also rebased onto current One gap I left deliberately, happy to close it if you'd prefer: |
Policy workers offload model parameters to CPU between phases (finish_inference, offload_after_refit), and the matching prepare_for_* call onloads them again. Reaching a compute API without that call leaves parameters on CPU while the rest of the step assumes CUDA, which surfaces as an opaque CUDA 'illegal memory access' that points nowhere near the missing step. This is easy to hit when writing a custom training loop. Add AbstractPolicyWorker._assert_model_onloaded(), which checks the device of the model's first parameter and raises a RuntimeError naming both the API being entered and the prepare_for_* call that was missed. Wire it into the compute entry points of the DTensor v1/v2 and Megatron workers: train (prepare_for_training) and get_logprobs / score / get_topk_logits (prepare_for_lp_inference). get_reference_policy_logprobs delegates to get_logprobs, so it is covered without a second check. The check is skipped when FSDP cpu_offload is configured, since parameters legitimately stay on CPU during compute in that mode, and when the model has no parameters. Megatron holds a list of virtual pipeline chunks rather than a single module, so both shapes are accepted. Also document the lifecycle contract on the PolicyInterface abstract methods, which previously had no docstrings. Closes NVIDIA-NeMo#1141 Signed-off-by: Vishnu Rajeev <[email protected]>
Review feedback on NVIDIA-NeMo#3690: the device check alone is not enough. prepare_for_lp_inference() moves parameters onto CUDA, so _assert_model_onloaded() passes, but with keep_train_buffers=False it also releases Megatron grad-buffer storage and moves optimizer state to CPU when offload_optimizer_for_logprob is set. A train(eval_mode=False) reached from there would clear the existing guard while the training state is still parked, dropping accumulated gradients or failing on a CPU-resident optimizer. Record the parking in _training_state_parked at the points that actually do it -- the grad-buffer release and the optimizer offload in each worker's prepare_for_lp_inference -- and clear it in prepare_for_training, which is what restores them. train() consults it via _assert_training_state_restored() only when eval_mode is False, since a forward-only eval pass needs neither buffers nor optimizer. Setting the flag from the offloading branches rather than from keep_train_buffers alone keeps two supported paths quiet: a configuration that offloads nothing never sets it, and keep_train_buffers=True leaves the training state alone, so an already-open train step is unaffected. Tests: the parked-state error and its message, the restored case, the default, and a structural check that each worker which sets the flag also clears it in prepare_for_training -- the asymmetry that would otherwise wedge every later train step. Verified the structural check fails against a worker with the clear removed. Rebased onto the removal of the DTensor v1 worker (NVIDIA-NeMo#4198), so the guards now cover the v2 and Megatron workers only. The structural test used to name the three worker modules; it now discovers them from the package and skips any that park nothing, so deleting or adding a backend cannot leave it pointing at a module that is gone. Closes NVIDIA-NeMo#1141 Co-Authored-By: Claude Opus 5 <[email protected]> Signed-off-by: Vishnu Rajeev <[email protected]>
9540bea to
3229f55
Compare
|
Rebased onto current The DTensor v1 worker is gone (#4198), so this PR no longer touches it — the guards now cover The structural test no longer hardcodes the worker list. It named all three modules, which is exactly what went stale here, so it now discovers them from Re-validated on the rebased branch: 14 passed / 1 skipped in Still happy to extend the flag to |
What does this PR do ?
Turns the opaque CUDA
illegal memory accessyou get from skipping aprepare_for_*()step into aRuntimeErrorthat names both the API you called and the prepare step you missed.Issues
Closes #1141
Usage
Policy workers offload model parameters to CPU between phases (
finish_inference,offload_after_refit), and the matchingprepare_for_*call onloads them again. Reaching a compute API without that call leaves parameters on CPU while the rest of the step assumes CUDA. As #1141 notes, that's easy to hit when writing a custom training loop, and the resulting failure points nowhere near the missing step.Before — an
illegal memory accessfrom somewhere deep in the forward pass. After:What changed
AbstractPolicyWorker._assert_model_onloaded()checks the device of the model's first parameter and raises naming the API and the required prepare call. It's wired into the compute entry points of all three policy workers:trainprepare_for_trainingget_logprobs,score,get_topk_logitsprepare_for_lp_inferencetrainprepare_for_trainingget_logprobs,get_topk_logitsprepare_for_lp_inferenceget_reference_policy_logprobsdelegates toget_logprobs, so it's covered without a second check.The check deliberately does not fire in three cases:
cpu_offload— parameters legitimately stay on CPU during compute, so callers passparams_may_be_offloaded=self.cpu_offload. Megatron only hasoptimizer_cpu_offload(params are always onloaded for compute), so it passes nothing.Megatron holds a list of virtual pipeline chunks rather than a single module, so
first_parameter_device()accepts both shapes.I also added docstrings to the
PolicyInterfacelifecycle methods (prepare_for_training,finish_training,prepare_for_lp_inference), which had none — that covers the "or document" half of the issue for someone reading the interface before writing their own loop.Before your PR is "Ready for review"
Pre checks:
tests/unit/models/policy/test_prepare_step_guards.py, 8 tests covering the raise path, the message contents, the module-list shape, and each of the three skip conditions. The real workers are Ray actors needing a GPU, so the guard is exercised through a minimalAbstractPolicyWorkersubclass.Additional Information
pytest tests/unit/models/policy/test_prepare_step_guards.py→ 8 passedruff check/ruff format --check→ cleanpyrefly check→ no new errors versusmainpytest tests/unit/models/policy→ the failures there (test_split_api_wrappers.py,test_utils.py,test_policy_validation.py) reproduce identically on a cleanmainwithTorch not compiled with CUDA enabled; none of those files import the modules touched here.nemo_rl/models/value/workers/) have the sameprepare_for_training/prepare_for_inferencelifecycle and could take the same guard. I left them out to keep this focused on the policy path from the issue — happy to extend if you'd prefer it in one change.