-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
[wip] add nccl allocator and symm memory and enable TP all reduce for nccl symm #21383
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
Draft
Amir-19
wants to merge
14
commits into
vllm-project:main
Choose a base branch
from
Amir-19:nccl_symm_allreduce
base: main
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.
+337
−30
Draft
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4daed93
add nccl symm memory
Amir-19 fc6960a
wip
Amir-19 92ba937
wip
Amir-19 8c0372f
wip
Amir-19 d625672
wip
Amir-19 f4c2640
wip
Amir-19 04a152a
wip
Amir-19 e0d95ee
wip
Amir-19 c8a3d50
wip
Amir-19 f4a24ed
wip
Amir-19 1817c59
wip
Amir-19 ee9cbc1
wip
Amir-19 4858fd2
wip
Amir-19 f119bbc
wip
Amir-19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
||
import multiprocessing | ||
import os | ||
|
||
import numpy as np | ||
import pytest | ||
import torch | ||
import torch.distributed | ||
|
||
from vllm.distributed.communication_op import ( # noqa | ||
tensor_model_parallel_all_reduce, | ||
) | ||
from vllm.distributed.device_communicators.pynccl import PyNcclCommunicator | ||
from vllm.distributed.device_communicators.pynccl_wrapper import NCCLLibrary | ||
from vllm.distributed.device_communicators.pynccl_allocator import ( | ||
get_nccl_mem_pool, | ||
) | ||
|
||
from vllm.distributed.parallel_state import ( | ||
ensure_model_parallel_initialized, | ||
get_world_group, | ||
graph_capture, | ||
init_distributed_environment, | ||
) | ||
from vllm.utils import update_environment_variables | ||
|
||
|
||
def distributed_run(fn, world_size): | ||
number_of_processes = world_size | ||
processes: list[multiprocessing.Process] = [] | ||
for i in range(number_of_processes): | ||
env: dict[str, str] = {} | ||
env["RANK"] = str(i) | ||
env["LOCAL_RANK"] = str(i) | ||
env["WORLD_SIZE"] = str(number_of_processes) | ||
env["LOCAL_WORLD_SIZE"] = str(number_of_processes) | ||
env["MASTER_ADDR"] = "localhost" | ||
env["MASTER_PORT"] = "12345" | ||
p = multiprocessing.Process(target=fn, args=(env,)) | ||
processes.append(p) | ||
p.start() | ||
|
||
for p in processes: | ||
p.join() | ||
|
||
for p in processes: | ||
assert p.exitcode == 0 | ||
|
||
|
||
def worker_fn_wrapper(fn): | ||
# `multiprocessing.Process` cannot accept environment variables directly | ||
# so we need to pass the environment variables as arguments | ||
# and update the environment variables in the function | ||
def wrapped_fn(env): | ||
update_environment_variables(env) | ||
local_rank = os.environ["LOCAL_RANK"] | ||
device = torch.device(f"cuda:{local_rank}") | ||
torch.cuda.set_device(device) | ||
init_distributed_environment() | ||
fn() | ||
|
||
return wrapped_fn | ||
|
||
|
||
@worker_fn_wrapper | ||
def multiple_allreduce_worker_fn(): | ||
device = torch.device(f"cuda:{torch.distributed.get_rank()}") | ||
groups = [ | ||
torch.distributed.new_group(ranks=[0, 1], backend="gloo"), | ||
torch.distributed.new_group(ranks=[2, 3], backend="gloo"), | ||
] | ||
group = groups[0] if torch.distributed.get_rank() in [0, 1] else groups[1] | ||
pynccl_comm = PyNcclCommunicator(group=group, device=device) | ||
with torch.cuda.use_mem_pool(get_nccl_mem_pool()): | ||
symm_tensor = torch.ones( | ||
16, 1024, 1024, dtype=torch.float32, device=device | ||
) | ||
win = pynccl_comm.register_comm_window(symm_tensor) | ||
stream = torch.cuda.default_stream() | ||
# two groups can communicate independently | ||
if torch.distributed.get_rank() in [0, 1]: | ||
tensor = pynccl_comm.all_reduce(symm_tensor, stream=stream) | ||
tensor = pynccl_comm.all_reduce(symm_tensor, stream=stream) | ||
torch.cuda.synchronize() | ||
assert torch.all(tensor == 4).cpu().item() | ||
else: | ||
tensor = pynccl_comm.all_reduce(symm_tensor, stream=stream) | ||
torch.cuda.synchronize() | ||
assert torch.all(tensor == 2).cpu().item() | ||
pynccl_comm.deregister_comm_window(win) | ||
|
||
|
||
|
||
@pytest.mark.skipif( | ||
torch.cuda.device_count() < 4, | ||
reason="Need at least 4 GPUs to run the test.", | ||
) | ||
def test_pynccl_multiple_allreduce(): | ||
# this tests pynccl for multiple tp groups, in a standalone way | ||
# i.e. call `pynccl_comm.all_reduce` directly | ||
distributed_run(multiple_allreduce_worker_fn, 4) |
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
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,92 @@ | ||
import tempfile | ||
import torch | ||
from torch.cuda.memory import CUDAPluggableAllocator | ||
from vllm.distributed.parallel_state import GroupCoordinator | ||
|
||
nccl_allocator_source = """ | ||
#include <nccl.h> | ||
#include <c10/cuda/CUDAGuard.h> | ||
extern "C" { | ||
|
||
void* nccl_alloc_plug(size_t size, int device, void* stream) { | ||
void* ptr; | ||
at::cuda::OptionalCUDAGuard gpuGuard(device); | ||
ncclResult_t err = ncclMemAlloc(&ptr, size); | ||
return ptr; | ||
|
||
} | ||
|
||
void nccl_free_plug(void* ptr, size_t size, int device, void* stream) { | ||
at::cuda::OptionalCUDAGuard gpuGuard(device); | ||
ncclResult_t err = ncclMemFree(ptr); | ||
} | ||
|
||
} | ||
""" | ||
|
||
_allocator = None | ||
_mem_pool = None | ||
_registered_base_addrs = set() | ||
_graph_pool_id = None | ||
|
||
|
||
def get_nccl_mem_pool(): | ||
global _allocator, _mem_pool | ||
if _mem_pool is None: | ||
out_dir = tempfile.gettempdir() | ||
nccl_allocator_libname = "nccl_allocator" | ||
torch.utils.cpp_extension.load_inline( | ||
name=nccl_allocator_libname, | ||
cpp_sources=nccl_allocator_source, | ||
with_cuda=True, | ||
extra_ldflags=["-lnccl"], | ||
verbose=True, | ||
Amir-19 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
is_python_module=False, | ||
build_directory=out_dir, | ||
) | ||
|
||
_allocator = CUDAPluggableAllocator( | ||
f"{out_dir}/{nccl_allocator_libname}.so", | ||
"nccl_alloc_plug", | ||
"nccl_free_plug", | ||
).allocator() | ||
_mem_pool = torch.cuda.MemPool(_allocator) | ||
|
||
return _mem_pool | ||
|
||
|
||
class use_symmetric_memory: | ||
def __init__(self, group_coordinator: GroupCoordinator): | ||
self.group_coordinator = group_coordinator | ||
self._mem_pool_ctx = torch.cuda.use_mem_pool(get_nccl_mem_pool()) | ||
self.is_graph_capture = torch.cuda.is_current_stream_capturing() | ||
self.device = torch.cuda.current_device() | ||
|
||
def __enter__(self): | ||
if self.is_graph_capture: | ||
assert ( | ||
_graph_pool_id is not None | ||
), "graph_pool_id is not set under graph capture" | ||
torch._C._cuda_endAllocateCurrentStreamToPool( | ||
self.device, _graph_pool_id | ||
) | ||
self._mem_pool_ctx.__enter__() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
global _registered_base_addrs | ||
self._mem_pool_ctx.__exit__(exc_type, exc_val, exc_tb) | ||
for segment in get_nccl_mem_pool().snapshot(): | ||
if segment["address"] not in _registered_base_addrs: | ||
# Check symmetric is maintained across all ranks | ||
# TODO | ||
self.group_coordinator.pynccl_comm.register_comm_window_raw( | ||
segment["address"], segment["total_size"] | ||
) | ||
_registered_base_addrs.add(segment["address"]) | ||
|
||
if self.is_graph_capture: | ||
assert ( | ||
_graph_pool_id is not None | ||
), "graph_pool_id is not set under graph capture" | ||
torch._C._cuda_beginAllocateToPool(self.device, _graph_pool_id) |
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.