Datasets:
Download code/reference/14_gemm_allgather.py from willychan21/ParallelKernelBench_Problems: direct link, hf CLI and curl.
- Browser
- Download file 518 Bytes
-
https://huggingface.co/datasets/willychan21/ParallelKernelBench_Problems/resolve/aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/14_gemm_allgather.py
- Command line
-
hf download hf://datasets/willychan21/ParallelKernelBench_Problems@aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/14_gemm_allgather.py
-
curl -L -o 14_gemm_allgather.py https://huggingface.co/datasets/willychan21/ParallelKernelBench_Problems/resolve/aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/14_gemm_allgather.py
518 Bytes
| import torch | |
| import torch.distributed as dist | |
| def solution( | |
| A: torch.Tensor, | |
| B: torch.Tensor, | |
| ) -> torch.Tensor: | |
| rank = dist.get_rank() | |
| world_size = dist.get_world_size() | |
| M, K = A.shape | |
| K_B, N_local = B.shape | |
| A = A.contiguous() | |
| B = B.contiguous() | |
| C_local = torch.matmul(A, B) | |
| C_gathered = [torch.zeros_like(C_local) for _ in range(world_size)] | |
| dist.all_gather(C_gathered, C_local) | |
| C = torch.cat(C_gathered, dim=1) | |
| return C | |