-
Notifications
You must be signed in to change notification settings - Fork 48
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
[Distributed] add nccl primitives #280
Merged
Merged
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
7278ebc
add nccl lib finding
soodoshll 69bd505
run all reduce example
soodoshll 4760ac6
update
soodoshll 829d07c
update
soodoshll 08d74f9
update
soodoshll 3d896e6
add primitives/cuda/nccl.py
soodoshll fd9dc91
add ncclcomm to cuda context
soodoshll 1a7795c
update
soodoshll ddabfdb
update
soodoshll 925bc8e
create ncclprimitive
soodoshll 21f3eae
update
soodoshll ad94ce0
add test.py
soodoshll 7349ce8
add include_dirs/linking_dirs/linking_libs/... to IRModule
yaoyaoding a1d3c50
update
soodoshll f0a0f86
reorganize dirs
soodoshll bd29659
update
soodoshll 2bc1680
clean
soodoshll c2ac73b
refactor
soodoshll 4bbecc3
update
soodoshll 9f5b51d
update
soodoshll a384167
format
soodoshll 29ded23
Merge branch 'main' of github.com:hidet-org/hidet into nccl
soodoshll 5dd7090
fix bugs
yaoyaoding File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,64 @@ | ||
import hidet | ||
import multiprocessing | ||
from multiprocessing import Process | ||
import hidet.cuda.nccl | ||
from hidet.cuda.nccl import NcclUniqueId, create_comm, ncclDataType, ncclRedOp | ||
from hidet.ffi.runtime_api import NCCLRuntimeAPI | ||
from hidet.lang import attrs | ||
from hidet.ir.primitives.cuda.nccl import all_reduce | ||
from hidet.ir.type import data_type | ||
from hidet.ir import Task | ||
from hidet.graph.ops.utils import input_like, compute | ||
from hidet.utils import prod | ||
from hidet.drivers import build_task, build_ir_module | ||
from hidet.libinfo import get_nccl_include_dirs, get_nccl_library_search_dirs | ||
from hidet.runtime import load_compiled_module | ||
|
||
print("NCCL version:", NCCLRuntimeAPI.get_version()) | ||
|
||
def run(world_size, rank, shared_id, barrier): | ||
if rank == 0: | ||
NCCLRuntimeAPI.get_unique_id(shared_id) | ||
barrier.wait() | ||
hidet.cuda.set_device(rank) | ||
|
||
print('initialize', rank) | ||
comm = create_comm(world_size, shared_id, rank) | ||
|
||
device = f"cuda:{rank}" | ||
send = hidet.randn([2, 2], device=device) | ||
recv = hidet.empty([2, 2], device=device) | ||
|
||
dtype = data_type('float32') | ||
shape = [2, 2] | ||
nbytes = dtype.nbytes * prod(shape) | ||
|
||
with hidet.script_module() as script_module: | ||
@hidet.script | ||
def launch(send: dtype[shape], recv: dtype[shape]): | ||
attrs.func_kind = 'public' | ||
all_reduce(0, send, recv, nbytes, ncclDataType.float32, ncclRedOp.sum) | ||
|
||
ir_module = script_module.ir_module() | ||
ir_module.target = 'cuda' | ||
ir_module.include_dirs.extend(get_nccl_include_dirs()) | ||
ir_module.linking_dirs.extend(get_nccl_library_search_dirs()) | ||
ir_module.include_headers.append(["nccl.h"]) | ||
ir_module.linking_libs.append(":libnccl.so.2") | ||
out_dir = f'./.cache/all_reduce_{rank}' | ||
build_ir_module(ir_module, out_dir, target='cuda') | ||
compiled_module = load_compiled_module(out_dir) | ||
compiled_module(send, recv) | ||
s = hidet.cuda.current_stream() | ||
s.synchronize() | ||
print(recv) | ||
|
||
world_size = 4 | ||
barrier = multiprocessing.Barrier(world_size) | ||
shared_id = multiprocessing.Value(NcclUniqueId, lock=False) | ||
processes = [Process(target=run, args=(world_size, i, shared_id, barrier)) for i in range(world_size)] | ||
|
||
for p in processes: | ||
p.start() | ||
for p in processes: | ||
p.join() |
This file contains 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 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 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 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,31 @@ | ||
from enum import IntEnum | ||
|
||
from ..ffi import NcclUniqueId, NcclCommunicator, nccl_runtime_api | ||
|
||
class ncclDataType(IntEnum): | ||
int8 = 0 | ||
char = 0 | ||
uint8 = 1 | ||
int32 = 2 | ||
int = 2 | ||
uint32 = 3 | ||
int64 = 4 | ||
uint64 = 5 | ||
float16 = 6 | ||
half = 6 | ||
float32 = 7 | ||
float = 7 | ||
float64 = 8 | ||
double = 8 | ||
bfloat = 9 | ||
|
||
class ncclRedOp(IntEnum): | ||
sum = 0 | ||
prod = 1 | ||
max = 2 | ||
min = 3 | ||
avg = 4 | ||
|
||
def create_comm(nranks: int, unique_id: NcclUniqueId, rank: int): | ||
handle = nccl_runtime_api.comm_init_rank(nranks, unique_id, rank) | ||
return NcclCommunicator(handle) |
This file contains 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 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 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 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.
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.
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.
Let's put this part in "hidet/cuda/nccl/ffi.py", and leave "hidet/ffi/ffi.py" to only contain the hidet runtime library.
In the future, when we want to add other library (e.g., cudnn library), we can put to "hidet/cuda/cudnn" and also its ffi.py.