-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Use CuPy for CUDA graphs #2811
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
Use CuPy for CUDA graphs #2811
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
54d658e
Add CuPY to dependencies
WoosukKwon cf03499
Add CuPy utils
WoosukKwon 902d63f
Add CuPy states
WoosukKwon 1e4036e
Use CuPy all reduce when enabled
WoosukKwon 97ff0e1
yapf
WoosukKwon e9a0d03
Use CuPy for CUDA graphs
WoosukKwon 58aee0a
Add comment
WoosukKwon 68ab9f8
yapf
WoosukKwon bd5557d
Compatible with cupy 13.0
WoosukKwon 639db01
Fix cupy_port bug
WoosukKwon 0ebbb08
Merge branch 'main' into add-cupy
WoosukKwon 4ee2e0d
Address reviews
WoosukKwon 3c733ea
Roll back to cupy 12.3
WoosukKwon 2a40508
Add minor comment
WoosukKwon 7020f71
Clean up at exit
WoosukKwon 233221e
Add clean up methods
WoosukKwon 3c211a9
Improve cleanup procedure
WoosukKwon b9f318b
Merge branch 'main' into add-cupy
WoosukKwon a0380f4
Minor
WoosukKwon 9b5b227
Merge branch 'main' into add-cupy
WoosukKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,122 @@ | ||||||
| """CuPy utilities for all-reduce. | ||||||
|
|
||||||
| We use CuPy all-reduce instead of torch.distributed.all_reduce when capturing | ||||||
| CUDA graphs, because torch.distributed.all_reduce causes errors when capturing | ||||||
| CUDA graphs. | ||||||
|
|
||||||
| TODO: Remove this file when torch.distributed.all_reduce is fixed. | ||||||
| """ | ||||||
| import contextlib | ||||||
| from unittest.mock import patch | ||||||
|
|
||||||
| import torch | ||||||
| from torch.distributed import ReduceOp | ||||||
|
|
||||||
| try: | ||||||
| import cupy | ||||||
| from cupy.cuda import nccl | ||||||
| from cupyx.distributed._nccl_comm import NCCLBackend, _get_nccl_dtype_and_count | ||||||
| except ImportError as e: | ||||||
| cupy = e | ||||||
| nccl = None | ||||||
|
|
||||||
| class NCCLBackend: | ||||||
| ... | ||||||
|
|
||||||
|
|
||||||
| _OP_MAPPING = { | ||||||
| ReduceOp.SUM: "sum", | ||||||
| ReduceOp.PRODUCT: "prod", | ||||||
| ReduceOp.MIN: "min", | ||||||
| ReduceOp.MAX: "max", | ||||||
| } | ||||||
|
|
||||||
| _NCCL_BACKEND = None | ||||||
| _WORLD_SIZE = 0 | ||||||
|
|
||||||
| _get_nccl_dtype_and_count_orginal = _get_nccl_dtype_and_count | ||||||
|
|
||||||
|
|
||||||
| def _get_nccl_dtype_and_count_bf16(*args, **kwargs): | ||||||
| """Patch/hack to force bf16 dtype in cupy NCCL. | ||||||
|
|
||||||
| cupy doesn't support bf16 by default, but the underlying NCCL | ||||||
| kernels do. We can just force the dtype to be bf16 and it will | ||||||
| work fine.""" | ||||||
| dtype, count = _get_nccl_dtype_and_count_orginal(*args, **kwargs) | ||||||
| # Hardcoded to always return bf16 dtype | ||||||
| dtype = nccl.NCCL_BFLOAT16 | ||||||
| return dtype, count | ||||||
|
|
||||||
|
|
||||||
| def is_initialized() -> bool: | ||||||
| """Returns whether the NCCL backend is initialized.""" | ||||||
| return _NCCL_BACKEND is not None | ||||||
|
|
||||||
|
|
||||||
| @contextlib.contextmanager | ||||||
| def set_cupy_stream(stream: torch.cuda.Stream) -> None: | ||||||
| """Set the cuda stream for communication""" | ||||||
| cupy_stream = cupy.cuda.ExternalStream(stream.cuda_stream, | ||||||
| stream.device_index) | ||||||
| with cupy_stream: | ||||||
| yield | ||||||
|
|
||||||
|
|
||||||
| def init_process_group(world_size: int, rank: int, host: str, | ||||||
| port: int) -> None: | ||||||
| """Initializes the CuPy NCCL backend. | ||||||
|
|
||||||
| # TODO: handle NCCL timeouts. | ||||||
| """ | ||||||
| assert not is_initialized() | ||||||
|
|
||||||
| if isinstance(cupy, Exception): | ||||||
| raise ImportError( | ||||||
| "NCCLBackend is not available. Please install cupy==13.0.0.") from cupy | ||||||
|
||||||
| "NCCLBackend is not available. Please install cupy==13.0.0.") from cupy | |
| "NCCLBackend is not available. Please install cupy-cuda12x==13.0.0.") from cupy |
Collaborator
Author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing it out. Actually, there are two issues:
- I changed the PR to use cupy 12.3 instead of 13.0 because cupy 13.0 does not support python 3.8 (I wasn't able to find the wheel in pypi).
- Users need to install different versions of cupy depending on their env. For example, CUDA 11.8 users should install cupy-cuda11x. ROCm users should install cupy-rocm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.