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

⚡️ Enable training with CUDA #59

Closed

Conversation

emprice
Copy link

@emprice emprice commented Nov 24, 2024

PR implementing the feature suggested in #58. The integer matmul operation needed for a masked autoregressive transform should be explicitly carried out on CPU and then migrated to the current default device. All other tensors get created on the default device anyway. This way, if the user calls torch.set_default_device('cuda'), that preference will be followed.

I have modified existing tests to make sure that no existing code should be broken by my modification; all tests are now performed on CPU and with CUDA to be sure they pass either way.

@emprice
Copy link
Author

emprice commented Nov 24, 2024

Looks like torch.set_default_device() and torch.get_default_device() are newer than I realized. torch.set_default_device() is only used for testing, and those could easily be conditionally skipped. Not sure how to fix the issue with torch.get_default_device(). Converting to a NumPy array and then using torch.tensor is a possibility as a workaround.

Copy link
Member

@francois-rozet francois-rozet left a comment

Choose a reason for hiding this comment

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

Thank you for your PR. Unfortunately, I think there is a better approach to address #58.

The cast to .int() in MaskedMLP is there because matmul is not possible for boolean tensors on CPU (and CUDA). We can simply cast to .double() instead to support CUDA. I tried in b5fc6cc and it works perfectly.

Concerning the CI, instead of modifying many (but not all) tests, it would be better to add a global option to pytest to run all tests with set_device_default("cuda"). I tried in 2f325e4, which allows to run

pytest --device cuda

Comment on lines +273 to +280
# PyTorch doesn't support this operation for integer arrays on CUDA devices
precedence = (
adjacency.cpu().int() @ adjacency.cpu().int().t() == adjacency.sum(dim=-1).cpu()
)
try:
precedence = precedence.to(torch.get_default_device())
except AttributeError:
precedence = torch.tensor(precedence.detach().cpu().numpy())
Copy link
Member

Choose a reason for hiding this comment

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

The cast to .int() is here because matmul is not possible for boolean tensors on CPU (and CUDA). We can simply cast to .double() instead to support CUDA.

@emprice
Copy link
Author

emprice commented Nov 24, 2024

No longer needed -- #58 has been fixed!

@emprice emprice closed this Nov 24, 2024
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