Skip to content
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

Fix a crash in NeMo 2.0 during module._apply(lambda t: t.cpu()) #1502

Merged
merged 6 commits into from
Feb 25, 2025

Conversation

guyueh1
Copy link
Contributor

@guyueh1 guyueh1 commented Feb 22, 2025

Description

In Nemo 2.0 during job exit, lightning calls a module._apply(lambda t: t.cpu()) on the GPT model which triggers an illegal memory access error in the TE dequantize kernel. This PR fixes the issue.

Fixes # (issue)

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

Please list the changes introduced in this PR:

  • Change A
  • Change B

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Collaborator

@timmoon10 timmoon10 left a comment

Choose a reason for hiding this comment

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

Can you explain the race condition that this is fixing? From what I can tell, Float8Tensor.cpu should already synchronize the GPU:

def cpu(self, memory_format=torch.preserve_format) -> torch.Tensor:
# pylint: disable=missing-function-docstring
return self.dequantize().cpu(memory_format=memory_format)

The actual problem is later in Float8Tensor._set_data:
self.data = self._quantizer.quantize(tensor)

We are passing a CPU tensor into the quantize kernel, and I don't think we ever move it to GPU. This doesn't explain why this PR fixes the IMA, so I could have missed something.

If my interpretation is the actual root cause, the quickest fix is to modify Float8Tensor._set_data with:

self.data = self._quantizer.quantize(tensor.to(device=self.device))

More long-term fixes are to handle CPU tensors in the quantize function or to support CPU Float8Tensors.

@guyueh1
Copy link
Contributor Author

guyueh1 commented Feb 24, 2025

@timmoon10
Re: We are passing a CPU tensor into the quantize kernel, and I don't think we ever move it to GPU.
I checked the device of 'tensor' in debug mode, after torch.cuda.synchronize() it is on cpu, however, the self._quantizer.quantize(tensor) worked fine. So there should be some code to handle cpu or move it to gpu somewhere.

I also don't fully understand the race condition, it just happened to work. I will dig into it, and reply here my findings.

@guyueh1
Copy link
Contributor Author

guyueh1 commented Feb 24, 2025

Findings:

  1. Adding torch.cuda.synchronize() at beginning of _set_data works (the original proposal of this PR)
  2. @timmoon10 's suggestion (self.data = self._quantizer.quantize(tensor.to(device=self.device))) also works
    Still digging why IMA happens

@guyueh1
Copy link
Contributor Author

guyueh1 commented Feb 24, 2025

I decide to revert the torch.cuda.synchronize() change because I can't understand why it would work; I apply a new way to fix it by making sure the tensor is moved to self.device if it were on CPU. I confirmed it fixed the IMA.
@timmoon10 what you think about the current version

@guyueh1 guyueh1 requested a review from timmoon10 February 25, 2025 15:58
@timmoon10
Copy link
Collaborator

/te-ci pytorch

Copy link
Collaborator

@timmoon10 timmoon10 left a comment

Choose a reason for hiding this comment

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

LGTM

@timmoon10 timmoon10 merged commit 9351a17 into NVIDIA:main Feb 25, 2025
11 of 12 checks passed
timmoon10 added a commit that referenced this pull request Feb 26, 2025
* Fix a crash with module._apply(lambda t: t.cpu())

Signed-off-by: Guyue Huang <[email protected]>

* Add comments

Signed-off-by: Guyue Huang <[email protected]>

* Make sure tensor is moved to dst device before quantizer quantizes

Signed-off-by: Guyue Huang <[email protected]>

---------

Signed-off-by: Guyue Huang <[email protected]>
Co-authored-by: Tim Moon <[email protected]>
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