Datasets:
Download code/reference/16_gemm_reducescatter.py from willychan21/ParallelKernelBench_Problems: direct link, hf CLI and curl.
- Browser
- Download file 627 Bytes
-
https://huggingface.co/datasets/willychan21/ParallelKernelBench_Problems/resolve/aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/16_gemm_reducescatter.py
- Command line
-
hf download hf://datasets/willychan21/ParallelKernelBench_Problems@aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/16_gemm_reducescatter.py
-
curl -L -o 16_gemm_reducescatter.py https://huggingface.co/datasets/willychan21/ParallelKernelBench_Problems/resolve/aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/16_gemm_reducescatter.py
627 Bytes
| import torch | |
| import torch.distributed as dist | |
| def solution( | |
| A_local: torch.Tensor, | |
| B_local: torch.Tensor, | |
| ) -> torch.Tensor: | |
| rank = dist.get_rank() | |
| world_size = dist.get_world_size() | |
| M, K_local = A_local.shape | |
| K_B, N = B_local.shape | |
| M_local = M // world_size | |
| A_local = A_local.contiguous() | |
| B_local = B_local.contiguous() | |
| C_partial = torch.matmul(A_local, B_local) | |
| C_local = torch.empty((M_local, N), dtype=C_partial.dtype, device=C_partial.device) | |
| dist.reduce_scatter_tensor(C_local, C_partial, op=dist.ReduceOp.SUM) | |
| return C_local | |