8000 fix: remove reference_model_buffers in fsdp2 by yuki-666 · Pull Request #558 · NVIDIA-NeMo/RL · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: remove reference_model_buffers in fsdp2 #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
19 changes: 5 additions & 14 deletions nemo_rl/models/policy/dtensor_policy_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,6 @@ def __init__(
self.reference_model_state_dict = get_cpu_state_dict(
self.model.state_dict().items(), pin_memory=True
)
self.reference_model_buffers = get_cpu_state_dict(
self.model.named_buffers(), pin_memory=True
)

if init_optimizer:
optimizer_cls = import_class_from_path(self.cfg["optimizer"]["name"])
Expand Down Expand Up @@ -768,32 +765,26 @@ def use_reference_model(self) -> Generator[None, None, None]:
"""
with torch.no_grad():
try:
# Save train model state_dict
curr_state_dict = get_cpu_state_dict(
self.model.state_dict().items(), pin_memory=True
)
curr_buffers = get_cpu_state_dict(
self.model.named_buffers(), pin_memory=True
)

# Swap reference model state_dict to self.model
for k, v in self.model.state_dict().items():
val = to_local_if_dtensor(v)
val.copy_(self.reference_model_state_dict[k])

for k, v in self.model.named_buffers():
val = to_local_if_dtensor(v)
val.copy_(self.reference_model_buffers[k])

# - self.model is the original reference_model, now on CUDA
# - curr_state_dict is the train model, now on CPU
yield

finally:
# Restore train model state_dict
for k, v in self.model.state_dict().items():
val = to_local_if_dtensor(v)
val.copy_(curr_state_dict[k])

for k, v in self.model.named_buffers():
val = to_local_if_dtensor(v)
val.copy_(curr_buffers[k])

def get_reference_policy_logprobs(
self, data: BatchedDataDict[Any], micro_batch_size: Optional[int] = None
) -> BatchedDataDict[ReferenceLogprobOutputSpec]:
Expand Down
Loading
0