Datasets:
Download code/reference/20_blocked_fp8_dequantize.py from willychan21/ParallelKernelBench_Problems: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/datasets/willychan21/ParallelKernelBench_Problems/resolve/aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/20_blocked_fp8_dequantize.py
- Command line
-
hf download hf://datasets/willychan21/ParallelKernelBench_Problems@aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/20_blocked_fp8_dequantize.py
-
curl -L -o 20_blocked_fp8_dequantize.py https://huggingface.co/datasets/willychan21/ParallelKernelBench_Problems/resolve/aa1bb86f2c59256de88c97e6d1447957e673607e/code/reference/20_blocked_fp8_dequantize.py
1.33 kB
| import torch | |
| import torch.distributed as dist | |
| import triton | |
| import triton.language as tl | |
| def block_fp8_dequant_kernel(y_ptr, s_ptr, x_ptr, num_elements, BLOCK_SIZE: tl.constexpr): | |
| pid = tl.program_id(axis=0) | |
| offs = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) | |
| mask = offs < num_elements | |
| s = tl.load(s_ptr + pid) | |
| y = tl.load(y_ptr + offs, mask=mask).to(tl.float32) | |
| tl.store(x_ptr + offs, y * s, mask=mask) | |
| def solution( | |
| local_y: torch.Tensor, | |
| local_s: torch.Tensor, | |
| block_size: int = 128, | |
| ) -> torch.Tensor: | |
| world_size = dist.get_world_size() | |
| chunk_shape = local_y.shape[1:] | |
| chunk_numel = local_y.numel() // world_size | |
| num_elements = local_y.numel() | |
| assert chunk_numel % block_size == 0, ( | |
| f"Chunk size {chunk_numel} must be divisible by block_size ({block_size})" | |
| ) | |
| y_flat = local_y.view(-1) | |
| s_flat = local_s.view(-1) | |
| x_flat = torch.empty(num_elements, device=local_y.device, dtype=torch.float32) | |
| if num_elements > 0: | |
| grid = (triton.cdiv(num_elements, block_size),) | |
| block_fp8_dequant_kernel[grid]( | |
| y_flat, s_flat, x_flat, num_elements, BLOCK_SIZE=block_size | |
| ) | |
| x = x_flat.view(world_size, *chunk_shape) | |
| out = torch.empty_like(x) | |
| dist.all_to_all_single(out, x) | |
| return out | |