|
| 1 | +# Copyright 2026, The FlagOS Contributors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# Generated by KernelGen: https://github.com/flagos-ai/KernelGen |
| 16 | +import logging |
| 17 | + |
| 18 | +import torch |
| 19 | +import triton |
| 20 | +import triton.language as tl |
| 21 | + |
| 22 | +import flag_gems |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +@triton.jit |
| 28 | +def _special_i1e_kernel(x_ptr, out_ptr, n_elements, BLOCK_SIZE: tl.constexpr): |
| 29 | + pid = tl.program_id(axis=0) |
| 30 | + block_start = pid * BLOCK_SIZE |
| 31 | + offsets = block_start + tl.arange(0, BLOCK_SIZE) |
| 32 | + mask = offsets < n_elements |
| 33 | + |
| 34 | + x = tl.load(x_ptr + offsets, mask=mask, other=0.0) |
| 35 | + |
| 36 | + # Compute in fp32 for accuracy/stability. |
| 37 | + # i1e(x) = i1(x) * exp(-|x|). Fold the exponential scaling directly into |
| 38 | + # the Cephes I1 approximation so the large-|x| branch never overflows. |
| 39 | + xf = x.to(tl.float32) |
| 40 | + ax = tl.abs(xf) |
| 41 | + |
| 42 | + # Small region: |x| <= 3.75. I1(x) = x * P(y^2), y = x / 3.75. |
| 43 | + y = xf / 3.75 |
| 44 | + y2 = y * y |
| 45 | + p = 0.00032411 |
| 46 | + p = 0.00301532 + y2 * p |
| 47 | + p = 0.02658733 + y2 * p |
| 48 | + p = 0.15084934 + y2 * p |
| 49 | + p = 0.51498869 + y2 * p |
| 50 | + p = 0.87890594 + y2 * p |
| 51 | + p = 0.5 + y2 * p |
| 52 | + small = xf * p * tl.exp(-ax) |
| 53 | + |
| 54 | + # Large region: |x| > 3.75. I1(x) ~ exp(|x|)/sqrt(|x|) * Q(3.75/|x|). |
| 55 | + # Multiplying by exp(-|x|) cancels the exponential prefactor. |
| 56 | + t = 3.75 / tl.maximum(ax, 1e-20) |
| 57 | + q = -0.00420059 |
| 58 | + q = 0.01787654 + t * q |
| 59 | + q = -0.02895312 + t * q |
| 60 | + q = 0.02282967 + t * q |
| 61 | + q = -0.01031555 + t * q |
| 62 | + q = 0.00163801 + t * q |
| 63 | + q = -0.00362018 + t * q |
| 64 | + q = -0.03988024 + t * q |
| 65 | + q = 0.39894228 + t * q |
| 66 | + large = q / tl.sqrt(tl.maximum(ax, 1e-20)) |
| 67 | + # I1 is odd, so i1e is odd as well. |
| 68 | + large = tl.where(xf < 0, -large, large) |
| 69 | + |
| 70 | + y_out = tl.where(ax <= 3.75, small, large) |
| 71 | + tl.store(out_ptr + offsets, y_out.to(x.dtype), mask=mask) |
| 72 | + |
| 73 | + |
| 74 | +def _run_special_i1e_kernel(x: torch.Tensor, out: torch.Tensor): |
| 75 | + if x.device.type != flag_gems.device or out.device.type != flag_gems.device: |
| 76 | + raise ValueError(f"Tensors must be {flag_gems.device} tensors") |
| 77 | + assert x.dtype in ( |
| 78 | + torch.float16, |
| 79 | + torch.bfloat16, |
| 80 | + torch.float32, |
| 81 | + torch.float64, |
| 82 | + ), "Unsupported dtype" |
| 83 | + assert out.dtype == x.dtype, "Output dtype must match input dtype" |
| 84 | + |
| 85 | + x_c = x.contiguous() |
| 86 | + out_c = out.contiguous() |
| 87 | + |
| 88 | + n_elements = out_c.numel() |
| 89 | + if n_elements == 0: |
| 90 | + return out |
| 91 | + |
| 92 | + grid = lambda meta: (triton.cdiv(n_elements, meta["BLOCK_SIZE"]),) |
| 93 | + _special_i1e_kernel[grid](x_c, out_c, n_elements, BLOCK_SIZE=1024) |
| 94 | + |
| 95 | + if out_c.data_ptr() != out.data_ptr(): |
| 96 | + out.copy_(out_c) |
| 97 | + return out |
| 98 | + |
| 99 | + |
| 100 | +def special_i1e(x: torch.Tensor): |
| 101 | + """ |
| 102 | + ATen wrapper: special_i1e(Tensor self) -> Tensor |
| 103 | + """ |
| 104 | + logger.debug("GEMS SPECIAL_I1E") |
| 105 | + out = torch.empty_like(x) |
| 106 | + return _run_special_i1e_kernel(x, out) |
0 commit comments