-
Notifications
You must be signed in to change notification settings - Fork 6
MPI_Allgatherv in mpi_allgather function
#186
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
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1442fb3
Add MPI_Allgatherv
rohanbabbar04 ebcbe8f
Add MPI_Allgather for uniform and MPI_Allgatherv for variable length …
rohanbabbar04 b62fcbe
Add ncp.ascontiguousarray in allgather
rohanbabbar04 2f1225a
Update docstring
rohanbabbar04 f5093bd
Minor Fix
rohanbabbar04 6ecbfdc
Add _prepare_allgather_inputs_mpi and update unroll gather recv
rohanbabbar04 9350594
Change _prepare_allgather_inputs to _prepare_allgather_inputs_nccl
rohanbabbar04 ffcfcfa
Minor fix in tests_ncclutils
rohanbabbar04 811506d
Remove int() in chunk size calculation
rohanbabbar04 175a396
Add comments and improve functions
rohanbabbar04 4fbc5c9
Minor fix in nccl_asarray
rohanbabbar04 75abc1e
Minor change in the docstring
rohanbabbar04 fe05882
Change param name to buffer_chunk_shape
rohanbabbar04 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,89 +1,58 @@ | ||
| __all__ = [ | ||
| "_prepare_allgather_inputs", | ||
| "_unroll_allgather_recv" | ||
| ] | ||
|
|
||
|
|
||
| import numpy as np | ||
| from pylops.utils.backend import get_module | ||
|
|
||
|
|
||
| # TODO: return type annotation for both cupy and numpy | ||
| def _prepare_allgather_inputs(send_buf, send_buf_shapes, engine): | ||
| r""" Prepare send_buf and recv_buf for NCCL allgather (nccl_allgather) | ||
| def _unroll_allgather_recv(recv_buf, buffer_chunk_shape, send_buf_shapes, displs=None) -> list: | ||
| r"""Unroll recv_buf after Buffered Allgather (MPI and NCCL) | ||
|
|
||
| Buffered Allgather (MPI and NCCL) requires the sending buffer to have the same size for every device. | ||
| Therefore, padding is required when the array is not evenly partitioned across | ||
| all the ranks. The padding is applied such that the each dimension of the sending buffers | ||
| is equal to the max size of that dimension across all ranks. | ||
| Depending on the provided parameters, the function: | ||
| - uses ``displs`` and element counts to extract variable-sized chunks. | ||
| - removes padding and reshapes each chunk using ``chunk_shape``. | ||
|
|
||
| Similarly, each receiver buffer (recv_buf) is created with size equal to :math:n_rank \cdot send_buf.size | ||
|
|
||
| Parameters | ||
| ---------- | ||
| send_buf : :obj: `numpy.ndarray` or `cupy.ndarray` or array-like | ||
| The data buffer from the local GPU to be sent for allgather. | ||
| send_buf_shapes: :obj:`list` | ||
| A list of shapes for each GPU send_buf (used to calculate padding size) | ||
| engine : :obj:`str` | ||
| Engine used to store array (``numpy`` or ``cupy``) | ||
|
|
||
| Returns | ||
| ------- | ||
| send_buf: :obj:`cupy.ndarray` | ||
| A buffer containing the data and padded elements to be sent by this rank. | ||
| recv_buf : :obj:`cupy.ndarray` | ||
| An empty, padded buffer to gather data from all GPUs. | ||
| """ | ||
| ncp = get_module(engine) | ||
| sizes_each_dim = list(zip(*send_buf_shapes)) | ||
| send_shape = tuple(map(max, sizes_each_dim)) | ||
| pad_size = [ | ||
| (0, s_shape - l_shape) for s_shape, l_shape in zip(send_shape, send_buf.shape) | ||
| ] | ||
|
|
||
| send_buf = ncp.pad( | ||
| send_buf, pad_size, mode="constant", constant_values=0 | ||
| ) | ||
|
|
||
| ndev = len(send_buf_shapes) | ||
| recv_buf = ncp.zeros(ndev * send_buf.size, dtype=send_buf.dtype) | ||
|
|
||
| return send_buf, recv_buf | ||
|
|
||
|
|
||
| def _unroll_allgather_recv(recv_buf, padded_send_buf_shape, send_buf_shapes) -> list: | ||
| r"""Unrolll recv_buf after Buffered Allgather (MPI and NCCL) | ||
|
|
||
| Remove the padded elements in recv_buff, extract an individual array from each device and return them as a list of arrays | ||
| Each GPU may send array with a different shape, so the return type has to be a list of array | ||
| instead of the concatenated array. | ||
| Each rank may send an array with a different shape, so the return type is a list of arrays | ||
| instead of a concatenated array. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| recv_buf: :obj:`cupy.ndarray` or array-like | ||
| The data buffer returned from nccl_allgather call | ||
| padded_send_buf_shape: :obj:`tuple`:int | ||
| The size of send_buf after padding used in nccl_allgather | ||
| The data buffer returned from the allgather call | ||
| send_buf_shapes: :obj:`list` | ||
| A list of original shapes for each GPU send_buf prior to padding | ||
|
|
||
| A list of original shapes of each rank's send_buf before any padding. | ||
| buffer_chunk_shape : tuple | ||
| Shape of each rank’s data as stored in ``recv_buf``. This should match | ||
| the layout used during allgather: use the padded send buffer shape when | ||
| padding is applied (e.g., NCCL), or the original send buffer shape when | ||
| no padding is used. | ||
| displs : list, optional | ||
| Starting offsets in recv_buf for each rank's data, used when chunks have | ||
| variable sizes (e.g., mpi_allgather with displacements). | ||
| Returns | ||
| ------- | ||
| chunks: :obj:`list` | ||
| A list of `cupy.ndarray` from each GPU with the padded element removed | ||
| chunks : list of ndarray | ||
| List of arrays (NumPy or CuPy, depending on ``engine``), one per rank, | ||
| reconstructed to their original shapes with any padding removed. | ||
| """ | ||
| ndev = len(send_buf_shapes) | ||
| # extract an individual array from each device | ||
| chunk_size = np.prod(padded_send_buf_shape) | ||
| chunks = [ | ||
| recv_buf[i * chunk_size:(i + 1) * chunk_size] for i in range(ndev) | ||
| ] | ||
|
|
||
| # Remove padding from each array: the padded value may appear somewhere | ||
| # in the middle of the flat array and thus the reshape and slicing for each dimension is required | ||
| for i in range(ndev): | ||
| slicing = tuple(slice(0, end) for end in send_buf_shapes[i]) | ||
| chunks[i] = chunks[i].reshape(padded_send_buf_shape)[slicing] | ||
|
|
||
| return chunks | ||
| if displs is not None: | ||
| recvcounts = [int(np.prod(shape)) for shape in send_buf_shapes] | ||
| # Slice recv_buf using displacements and then reconstruct the original-shaped chunk. | ||
| return [ | ||
| recv_buf[displs[i]:displs[i] + recvcounts[i]].reshape(send_buf_shapes[i]) | ||
| for i in range(ndev) | ||
| ] | ||
| else: | ||
| # extract an individual array from each device | ||
| chunk_size = np.prod(buffer_chunk_shape) | ||
| chunks = [ | ||
| recv_buf[i * chunk_size:(i + 1) * chunk_size] | ||
| for i in range(ndev) | ||
| ] | ||
| # Remove padding from each array: the padded value may appear somewhere | ||
| # in the middle of the flat array and thus the reshape and slicing for each dimension is required | ||
| for i in range(ndev): | ||
| slicing = tuple(slice(0, end) for end in send_buf_shapes[i]) | ||
| chunks[i] = chunks[i].reshape(buffer_chunk_shape)[slicing] | ||
| return chunks | ||
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
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.