|
6 | 6 | from lightllm.utils.config_utils import ffn_use_tanh_approximate_gelu |
7 | 7 |
|
8 | 8 |
|
| 9 | +@triton.jit |
| 10 | +def _silu_and_mul_quant_fp8( |
| 11 | + input_ptr, |
| 12 | + quant_ptr, |
| 13 | + scale_ptr, |
| 14 | + input_stride: tl.constexpr, |
| 15 | + size_n: tl.constexpr, |
| 16 | + groups, |
| 17 | + scale_stride_n, |
| 18 | + limit, |
| 19 | + fp8_max: tl.constexpr, |
| 20 | + GROUP_SIZE: tl.constexpr, |
| 21 | + GROUPS_PER_CTA: tl.constexpr, |
| 22 | + USE_LIMIT: tl.constexpr, |
| 23 | + USE_GELU: tl.constexpr, |
| 24 | +): |
| 25 | + ids = tl.program_id(0) * GROUPS_PER_CTA + tl.arange(0, GROUPS_PER_CTA) |
| 26 | + rows = ids // (size_n // GROUP_SIZE) |
| 27 | + group_cols = ids % (size_n // GROUP_SIZE) |
| 28 | + cols = tl.arange(0, GROUP_SIZE)[None, :] |
| 29 | + offsets = rows[:, None] * input_stride + group_cols[:, None] * GROUP_SIZE + cols |
| 30 | + mask = ids[:, None] < groups |
| 31 | + gate = tl.load(input_ptr + offsets, mask, 0).to(tl.float32) |
| 32 | + up = tl.load(input_ptr + offsets + size_n, mask, 0).to(tl.float32) |
| 33 | + if USE_LIMIT: |
| 34 | + gate = tl.minimum(gate, limit) |
| 35 | + up = tl.minimum(tl.maximum(up, -limit), limit) |
| 36 | + if USE_GELU: |
| 37 | + cubed = gate * gate * gate |
| 38 | + arg = 0.7978845608028654 * (gate + 0.044715 * cubed) |
| 39 | + gate = 0.5 * gate * (1.0 + (2.0 / (1.0 + tl.exp(-2.0 * arg)) - 1.0)) |
| 40 | + else: |
| 41 | + gate = gate / (1 + tl.exp(-gate)) |
| 42 | + gate = gate.to(input_ptr.dtype.element_ty) |
| 43 | + # Preserve both rounding boundaries of activation -> BF16 buffer -> FP8. |
| 44 | + value = (up * gate.to(tl.float32)).to(input_ptr.dtype.element_ty).to(tl.float32) |
| 45 | + scale = tl.maximum(tl.max(tl.abs(value), axis=1), 1e-10) / fp8_max |
| 46 | + scale = tl.exp2(tl.ceil(tl.log2(scale))) |
| 47 | + quant = tl.clamp(value / scale[:, None], -fp8_max, fp8_max).to(quant_ptr.dtype.element_ty) |
| 48 | + tl.store(quant_ptr + ids[:, None] * GROUP_SIZE + cols, quant, mask) |
| 49 | + tl.store(scale_ptr + rows + group_cols * scale_stride_n, scale, ids < groups) |
| 50 | + |
| 51 | + |
| 52 | +@autotune( |
| 53 | + kernel_name="silu_and_mul_quant_fp8:v1", |
| 54 | + configs_gen_func=lambda: [ |
| 55 | + {"GROUPS_PER_CTA": g, "num_warps": w} for g, w in [(1, 1), (4, 1), (4, 4), (8, 4), (16, 4)] |
| 56 | + ], |
| 57 | + static_key_func=lambda input, group_size, dtype, limit: { |
| 58 | + "N": input.shape[1] // 2, |
| 59 | + "input_dtype": str(input.dtype), |
| 60 | + "output_dtype": str(dtype), |
| 61 | + "group_size": group_size, |
| 62 | + "clamp": limit is not None, |
| 63 | + "gelu": ffn_use_tanh_approximate_gelu() and limit is None, |
| 64 | + }, |
| 65 | + run_key_func=lambda input: input.shape[0], |
| 66 | +) |
| 67 | +def silu_and_mul_quant_fp8(input, group_size, dtype, limit=None, alloc_func=torch.empty, run_config=None): |
| 68 | + """Fuse blocked activation with FP8 quantization and TMA-aligned UE8M0 scales.""" |
| 69 | + assert input.ndim == 2 and input.stride(1) == 1 |
| 70 | + assert input.shape[1] % (2 * group_size) == 0 and group_size == triton.next_power_of_2(group_size) |
| 71 | + m, n = input.shape[0], input.shape[1] // 2 |
| 72 | + quant = alloc_func((m, n), device=input.device, dtype=dtype) |
| 73 | + aligned_m = triton.cdiv(m, 4) * 4 |
| 74 | + scale = alloc_func((n // group_size, aligned_m), device=input.device, dtype=torch.float32).T[:m, :] |
| 75 | + if run_config is None: |
| 76 | + run_config = {"GROUPS_PER_CTA": 4, "num_warps": 4} |
| 77 | + groups = m * n // group_size |
| 78 | + _silu_and_mul_quant_fp8[(triton.cdiv(groups, run_config["GROUPS_PER_CTA"]),)]( |
| 79 | + input, |
| 80 | + quant, |
| 81 | + scale, |
| 82 | + input.stride(0), |
| 83 | + n, |
| 84 | + groups, |
| 85 | + scale.stride(1), |
| 86 | + limit, |
| 87 | + torch.finfo(dtype).max, |
| 88 | + group_size, |
| 89 | + USE_LIMIT=limit is not None, |
| 90 | + USE_GELU=ffn_use_tanh_approximate_gelu() and limit is None, |
| 91 | + **run_config, |
| 92 | + ) |
| 93 | + return quant, scale |
| 94 | + |
| 95 | + |
9 | 96 | @triton.jit |
10 | 97 | def _silu_and_mul_kernel_fast( |
11 | 98 | input_ptr, |
|
0 commit comments