Spaces:
Running on Zero
Running on Zero
| # Copyright 2025 Tencent Inc. All Rights Reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import torch | |
| import triton | |
| import triton.language as tl | |
| # modified from https://github.com/deepseek-ai/DeepSeek-V3/blob/main/inference/kernel.py | |
| fp8_gemm_configs = [ | |
| triton.Config( | |
| {"BLOCK_SIZE_M": block_m, "BLOCK_SIZE_N": block_n, "BLOCK_SIZE_K": 128}, | |
| num_stages=num_stages, | |
| num_warps=8, | |
| ) | |
| for block_m in [16, 32, 64] | |
| for block_n in [32, 64, 128] | |
| for num_stages in [3, 4, 5, 6] | |
| ] | |
| def _fp8_gemm_triton_block_kernel( | |
| a_ptr, | |
| b_ptr, | |
| c_ptr, | |
| a_s_ptr, | |
| b_s_ptr, | |
| M, | |
| N: tl.constexpr, | |
| K: tl.constexpr, | |
| BLOCK_SIZE_M: tl.constexpr, | |
| BLOCK_SIZE_N: tl.constexpr, | |
| BLOCK_SIZE_K: tl.constexpr, | |
| ): | |
| """ | |
| Performs a matrix multiplication operation on FP8 matrices with scaling factors. | |
| """ | |
| pid_m = tl.program_id(axis=0) | |
| pid_n = tl.program_id(axis=1) | |
| k = tl.cdiv(K, BLOCK_SIZE_K) | |
| offs_m = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M | |
| offs_n = (pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)) % N | |
| offs_k = tl.arange(0, BLOCK_SIZE_K) | |
| a_ptrs = a_ptr + offs_m[:, None] * K + offs_k[None, :] | |
| b_ptrs = b_ptr + offs_n[None, :] * K + offs_k[:, None] | |
| a_s_ptrs = a_s_ptr + offs_m * k | |
| b_s_ptrs = b_s_ptr + (offs_n // BLOCK_SIZE_K) * k | |
| accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32) | |
| for i in range(k): | |
| a = tl.load(a_ptrs, mask=offs_k[None, :] < K - i * BLOCK_SIZE_K, other=0.0) | |
| b = tl.load(b_ptrs, mask=offs_k[:, None] < K - i * BLOCK_SIZE_K, other=0.0) | |
| a_s = tl.load(a_s_ptrs) | |
| b_s = tl.load(b_s_ptrs) | |
| accumulator += tl.dot(a, b) * a_s[:, None] * b_s[None, :] | |
| a_ptrs += BLOCK_SIZE_K | |
| b_ptrs += BLOCK_SIZE_K | |
| a_s_ptrs += 1 | |
| b_s_ptrs += 1 | |
| c = accumulator.to(c_ptr.dtype.element_ty) | |
| offs_m = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) | |
| offs_n = pid_n * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N) | |
| c_ptrs = c_ptr + offs_m[:, None] * N + offs_n[None, :] | |
| mask = (offs_m[:, None] < M) & (offs_n[None, :] < N) | |
| tl.store(c_ptrs, c, mask=mask) | |
| # triton fp8 gemm for fp8 per-block weight & fp8 per-group activation | |
| # modified from https://github.com/deepseek-ai/DeepSeek-V3/blob/main/inference/kernel.py | |
| def fp8_gemm_triton_block( | |
| a: torch.Tensor, | |
| a_s: torch.Tensor, | |
| b: torch.Tensor, | |
| b_s: torch.Tensor, | |
| out_dtype=torch.bfloat16, | |
| bias=None, | |
| ) -> torch.Tensor: | |
| """ | |
| Perform a matrix multiplication using FP8 precision. | |
| """ | |
| assert a.is_contiguous() and b.is_contiguous() | |
| assert a_s.is_contiguous() and b_s.is_contiguous() | |
| K = a.size(-1) | |
| M = a.numel() // K | |
| N = b.size(0) | |
| c = a.new_empty(*a.size()[:-1], N, dtype=out_dtype) | |
| def grid(meta): | |
| return ( | |
| triton.cdiv(M, meta["BLOCK_SIZE_M"]), | |
| triton.cdiv(N, meta["BLOCK_SIZE_N"]), | |
| ) | |
| _fp8_gemm_triton_block_kernel[grid](a, b, c, a_s, b_s, M, N, K) | |
| if bias is not None: | |
| c += bias | |
| return c | |