Skip to content

Support group_mlp offload for TE op fuser#1

Open
lhb8125 wants to merge 3 commits into
sraman-rgb:srelu-fused-grouped-mlpfrom
lhb8125:feat/group-mlp-offload-on-srelu-fuser
Open

Support group_mlp offload for TE op fuser#1
lhb8125 wants to merge 3 commits into
sraman-rgb:srelu-fused-grouped-mlpfrom
lhb8125:feat/group-mlp-offload-on-srelu-fuser

Conversation

@lhb8125

@lhb8125 lhb8125 commented May 19, 2026

Copy link
Copy Markdown

What does this PR do ?

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact the @mcore-oncall.

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either eharper@nvidia.com or zijiey@nvidia.com.

Comment on lines +839 to +840
if offload_nbytes == 0:
return src_tensor

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is correct. GroupedTensor.prepare_for_saving removes all the buffers within the GroupedTensor, so we need to call restore_from_saved even if no buffers were moved to CPU.

Comment on lines +828 to +837
if not isinstance(buffer, torch.Tensor):
buffer_states.append(None)
elif _regular_tensor_needs_offloading(buffer, self.min_offloaded_tensor_size):
buffer_states.append(
self._offload_tensor(buffer, pin_memory=pin_memory, use_cpu_pool=use_cpu_pool)
)
buffer.record_stream(self.d2h_stream)
offload_nbytes += buffer.numel() * buffer.element_size()
else:
buffer_states.append((_TE_GROUPED_TENSOR_RESIDENT_BUFFER_STATE, buffer))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This implementation is correct, but we could make it a bit nicer by using a consistent structure for offloaded and non-offloaded buffers.

Suggested change
if not isinstance(buffer, torch.Tensor):
buffer_states.append(None)
elif _regular_tensor_needs_offloading(buffer, self.min_offloaded_tensor_size):
buffer_states.append(
self._offload_tensor(buffer, pin_memory=pin_memory, use_cpu_pool=use_cpu_pool)
)
buffer.record_stream(self.d2h_stream)
offload_nbytes += buffer.numel() * buffer.element_size()
else:
buffer_states.append((_TE_GROUPED_TENSOR_RESIDENT_BUFFER_STATE, buffer))
is_offloaded = (
buffer is not None
and _regular_tensor_needs_offloading(buffer, self.min_offloaded_tensor_size)
)
buffer_state = (
self._offload_tensor(buffer, pin_memory, use_cpu_pool=use_cpu_pool)
if is_offloaded
else buffer
)
buffer_states.append((is_offloaded, buffer_state))

The reloading function becomes:

    def _reload_te_grouped_tensor(self, state, non_blocking=None):
        """Reload TE GroupedTensor backing buffers and reconstruct the wrapper."""
        debug_rank("------reload TE GroupedTensor")
        _, tensor_obj, buffer_states, _ = state

        buffers = []
        for is_offloaded, buffer_state in buffer_states:
            if is_offloaded:
                buffers.append(self._reload_tensor(buffer_state, non_blocking=non_blocking))
            else:
                buffers.append(buffer_state)

        return te_grouped_tensor_restore_from_saved(tensor_obj, buffers)

Comment on lines +863 to +867
def offload(self, src_tensor, pin_memory=True, use_cpu_pool=True):
"""Offload a tensor-like saved activation."""
if is_te_grouped_tensor(src_tensor):
return self._offload_te_grouped_tensor(src_tensor, pin_memory, use_cpu_pool)
return self._offload_tensor(src_tensor, pin_memory, use_cpu_pool)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct, but I'm curious why we don't have similar logic for QuantizedTensor, which should have similar problems as GroupedTensor. If we already have TE infrastructure for CPU offloading for quantized tensors, then we should use that for grouped tensors instead of implementing custom logic in Mcore.

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