JustGRPO icon indicating copy to clipboard operation
JustGRPO copied to clipboard

[Bug] FSDP AssertionError: "Non-root FSDP instance's _is_root should not have been set yet" during forward pass

Open maomaocun opened this issue 10 months ago • 3 comments

HELLO!

I encountered an AssertionError related to FSDP internals during the training loop. The error occurs specifically within the logprob_loss function when performing a forward pass on the model (model(x)).

It appears that the FSDP instance state is inconsistent, triggering the assertion: Non-root FSDP instance's _is_root should not have been set yet or should have been set to False.

To Reproduce Steps to reproduce the behavior:

  1. Run the training script using the provided configuration.
  2. The error triggers when the code reaches the logprob_loss calculation in grpo.py.

Code Snippet Context Based on the traceback, the crash happens here:

# grpo.py, line 40, in logprob_loss
logits = model(x).logits / temperature

Stack Trace

[rank0]: Traceback (most recent call last):
[rank0]:   File "/path/to/project/train.py", line 251, in <module>
[rank0]:     train(config)
[rank0]:   File "/path/to/project/train.py", line 182, in train
[rank0]:     logprob_loss(
[rank0]:   File "/path/to/project/grpo.py", line 40, in logprob_loss
[rank0]:     logits = model(x).logits / temperature
[rank0]:              ^^^^^^^^
[rank0]:   File ".../site-packages/torch/nn/modules/module.py", line 1775, in _wrapped_call_impl
[rank0]:     return self._call_impl(*args, **kwargs)
[rank0]:   ...
[rank0]:   File ".../site-packages/torch/distributed/fsdp/fully_sharded_data_parallel.py", line 835, in forward
[rank0]:     args, kwargs = _root_pre_forward(self, self, args, kwargs)
[rank0]:   File ".../site-packages/torch/distributed/fsdp/_runtime_utils.py", line 517, in _root_pre_forward
[rank0]:     _lazy_init(state, module)
[rank0]:   File ".../site-packages/torch/distributed/fsdp/_runtime_utils.py", line 140, in _lazy_init
[rank0]:     _share_state_and_init_handle_attrs(state, root_module)
[rank0]:   File ".../site-packages/torch/distributed/fsdp/_runtime_utils.py", line 209, in _share_state_and_init_handle_attrs
[rank0]:     _p_assert(
[rank0]:   File ".../site-packages/torch/distributed/utils.py", line 159, in _p_assert
[rank0]:     raise AssertionError(s)
[rank0]: AssertionError: Non-root FSDP instance's `_is_root` should not have been set yet or should have been set to `False`

Environment

  • Python Version: 3.12
  • PyTorch Version: 2.9。0
  • CUDA/ROCm version: 12.9
  • Hardware: 8xH200

Additional context This error typically suggests that model(x) is being called on a module that FSDP considers a "non-root" instance, or there is an issue with how the model was wrapped/initialized (e.g., calling forward on a submodule directly or incorrect mixed usage of _is_root).


maomaocun avatar Jan 23 '26 13:01 maomaocun

Hi, thanks for reporting this issue!

We tried to reproduce this on our end but couldn't trigger the same error. This suggests the issue might be related to specific environment configurations rather than a fundamental code bug.

To help us investigate further, could you please provide more details about your setup?

  1. Full environment info:

    • Output of pip list (especially versions of accelerate, transformers, torch)
    • Output of nvidia-smi (driver version, etc.)
  2. When does the error occur?

    • Does it fail on the very first forward pass, or after training for some steps?
  3. Accelerate config:

    • Are you using our provided configs/fsdp.yaml, or a custom configuration?
    • Output of accelerate env
  4. Launch command:

    • The exact command you used to launch the training

Also, as a quick test, could you try adding accelerator.wait_for_everyone() right before the logprob_loss() call in train.py (around line 179) and see if the issue persists? This would help us determine if it's a synchronization/timing issue.

Thanks!

nzl-thu avatar Jan 24 '26 04:01 nzl-thu

Since no response has been received, we are closing this issue for now.

LeapLabTHU avatar Feb 03 '26 08:02 LeapLabTHU

Finally, on Claude's recommendation, I temporarily fixed this issue by changing line 153 in train.py from:
model=accelerator.unwrap_model(model),
to:
model=model,

This issue may be caused by inaccordance among some packages, and this solution works, and the authors already provide the concret package version at https://github.com/LeapLabTHU/JustGRPO/issues/7 . @maomaocun

duterscmy avatar Mar 13 '26 11:03 duterscmy