-
Notifications
You must be signed in to change notification settings - Fork 53
NVSHMEM4Py Integration #33
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
Open
Willy-Chan
wants to merge
2
commits into
perplexityai:master
Choose a base branch
from
Willy-Chan:nvshmem4py_bindings_integration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+210
−256
Open
Changes from 1 commit
Commits
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 |
---|---|---|
@@ -1,14 +1,10 @@ | ||
#include <torch/library.h> | ||
|
||
#include "bindings/all_to_all_ops.h" | ||
#include "bindings/nvshmem.h" | ||
#include "core/registration.h" | ||
|
||
using namespace pplx; | ||
|
||
TORCH_LIBRARY(pplx_kernels, m) { | ||
register_nvshmem_ops(m); | ||
register_all_to_all_ops(m); | ||
} | ||
TORCH_LIBRARY(pplx_kernels, m) { register_all_to_all_ops(m); } | ||
|
||
REGISTER_EXTENSION(TORCH_EXTENSION_NAME) | ||
REGISTER_EXTENSION(TORCH_EXTENSION_NAME) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,16 +1,6 @@ | ||
from . import ops as ops | ||
from .all_to_all import ( | ||
AllToAll as AllToAll, | ||
) | ||
from .all_to_all import AllToAll as AllToAll | ||
from .nvshmem import ( | ||
nvshmem_alloc_empty_unique_id as nvshmem_alloc_empty_unique_id, | ||
nvshmem_alltoall as nvshmem_alltoall, | ||
nvshmem_barrier_all as nvshmem_barrier_all, | ||
nvshmem_barrier_all_on_current_stream as nvshmem_barrier_all_on_current_stream, | ||
nvshmem_finalize as nvshmem_finalize, | ||
nvshmem_get_unique_id as nvshmem_get_unique_id, | ||
PyTorchStreamWrapper as PyTorchStreamWrapper, | ||
nvshmem_init as nvshmem_init, | ||
nvshmem_my_pe as nvshmem_my_pe, | ||
nvshmem_n_pes as nvshmem_n_pes, | ||
nvshmem_unique_id_size as nvshmem_unique_id_size, | ||
) |
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 |
---|---|---|
@@ -1,60 +1,45 @@ | ||
# pyright: reportCallIssue=false | ||
|
||
from collections.abc import Sequence | ||
from typing import Any, Optional | ||
|
||
import torch | ||
import nvshmem.core as nvshmem # type: ignore[import] | ||
import torch.distributed as dist | ||
|
||
from .ops import _ops | ||
|
||
###### NVSHMEM ###### | ||
|
||
|
||
def nvshmem_get_unique_id() -> torch.Tensor: | ||
return _ops.nvshmem_get_unique_id() | ||
|
||
|
||
def nvshmem_unique_id_size() -> int: | ||
return _ops.nvshmem_unique_id_size() | ||
|
||
|
||
def nvshmem_alloc_empty_unique_id() -> torch.Tensor: | ||
return torch.zeros(nvshmem_unique_id_size(), dtype=torch.uint8, device="cpu") | ||
|
||
|
||
def nvshmem_init(uid: torch.Tensor, rank: int, world_size: int) -> int: | ||
status = _ops.nvshmem_init(uid, rank, world_size) | ||
torch.cuda.synchronize() | ||
return status | ||
|
||
|
||
def nvshmem_alltoall(dest: torch.Tensor, source: torch.Tensor) -> None: | ||
return _ops.nvshmem_alltoall(dest, source) | ||
|
||
|
||
def nvshmem_finalize() -> None: | ||
torch.cuda.synchronize() | ||
_ops.nvshmem_finalize() | ||
|
||
|
||
def nvshmem_my_pe() -> int: | ||
return _ops.nvshmem_my_pe() | ||
|
||
|
||
def nvshmem_n_pes() -> int: | ||
return _ops.nvshmem_n_pes() | ||
|
||
|
||
def nvshmem_malloc( | ||
shape: Sequence[int], | ||
dtype: torch.dtype, | ||
device: torch.device, | ||
) -> torch.Tensor: | ||
return _ops.nvshmem_malloc(shape, dtype, device) | ||
|
||
|
||
def nvshmem_barrier_all() -> None: | ||
_ops.nvshmem_barrier_all() | ||
|
||
|
||
def nvshmem_barrier_all_on_current_stream() -> None: | ||
_ops.nvshmem_barrier_all_on_current_stream() | ||
def nvshmem_init( | ||
global_rank: int, | ||
local_rank: int, | ||
world_size: int, | ||
device: Any, | ||
uid: Optional[Any] = None, | ||
) -> None: | ||
uniqueid = nvshmem.get_unique_id(empty=True) | ||
if local_rank == 0: | ||
uniqueid = nvshmem.get_unique_id() | ||
broadcast_objects = [uniqueid] | ||
else: | ||
broadcast_objects = [None] | ||
|
||
dist.broadcast_object_list(broadcast_objects, src=0) | ||
dist.barrier() | ||
|
||
nvshmem.init( | ||
device=device, | ||
uid=broadcast_objects[0], | ||
rank=global_rank, | ||
nranks=world_size, | ||
initializer_method="uid", | ||
) | ||
|
||
|
||
# This stream wrapper returns the format required by CUDA Python. This workaround will be removed when nvshmem4py supports Torch stream interoperability. | ||
# For more information see: https://nvidia.github.io/cuda-python/cuda-core/latest/interoperability.html#cuda-stream-protocol | ||
class PyTorchStreamWrapper: | ||
def __init__(self, pt_stream: Any) -> None: | ||
self.pt_stream = pt_stream | ||
self.handle = pt_stream.cuda_stream | ||
|
||
def __cuda_stream__(self) -> tuple[int, int]: | ||
stream_id = self.pt_stream.cuda_stream | ||
return (0, stream_id) |
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
Oops, something went wrong.
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.