|
| 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 | + |
| 17 | +import logging |
| 18 | +import math |
| 19 | + |
| 20 | +import torch |
| 21 | +import triton |
| 22 | +import triton.language as tl |
| 23 | + |
| 24 | +from flag_gems import runtime |
| 25 | +from flag_gems.runtime import torch_device_fn |
| 26 | +from flag_gems.utils import libentry |
| 27 | +from flag_gems.utils.random_utils import ( |
| 28 | + philox_backend_seed_offset, |
| 29 | + uint_to_uniform_float, |
| 30 | +) |
| 31 | + |
| 32 | +logger = logging.getLogger("flag_gems." + __name__) |
| 33 | + |
| 34 | + |
| 35 | +def _alpha_dropout_affine(p: float): |
| 36 | + """Compute affine parameters a, b for alpha dropout.""" |
| 37 | + _ALPHA = 1.6732632423543772848170429916717 |
| 38 | + _SCALE = 1.0507009873554804934193349852946 |
| 39 | + alpha_prime = -_ALPHA * _SCALE |
| 40 | + if p == 0.0: |
| 41 | + return 1.0, 0.0 |
| 42 | + a = 1.0 / math.sqrt((1.0 - p) * (1.0 + p * alpha_prime * alpha_prime)) |
| 43 | + b = -a * p * alpha_prime |
| 44 | + return a, b |
| 45 | + |
| 46 | + |
| 47 | +@libentry() |
| 48 | +@triton.autotune( |
| 49 | + configs=runtime.get_tuned_config("alpha_dropout"), |
| 50 | + key=["N"], |
| 51 | +) |
| 52 | +@triton.jit(do_not_specialize=["philox_seed", "philox_offset"]) |
| 53 | +def alpha_dropout_forward_kernel( |
| 54 | + X, |
| 55 | + Y, |
| 56 | + N, |
| 57 | + p, |
| 58 | + a, |
| 59 | + b, |
| 60 | + philox_seed, |
| 61 | + philox_offset, |
| 62 | + BLOCK: tl.constexpr, |
| 63 | +): |
| 64 | + """Metax-optimized alpha dropout forward kernel. |
| 65 | +
|
| 66 | + Uses UNROLL=4 with num_warps <= 8 to stay within the 512-thread |
| 67 | + hardware limit on Metax GPUs. |
| 68 | + """ |
| 69 | + UNROLL: tl.constexpr = 4 |
| 70 | + philox_seed = philox_seed.to(tl.int64) |
| 71 | + philox_offset = philox_offset.to(tl.int64) |
| 72 | + c0 = (philox_offset & 0xFFFFFFFF).to(tl.uint32) |
| 73 | + c1 = ((philox_offset >> 32) & 0xFFFFFFFF).to(tl.uint32) |
| 74 | + i4 = tl.program_id(0) * BLOCK + tl.arange(0, BLOCK) |
| 75 | + c0 += i4 |
| 76 | + _O = c0 * 0 |
| 77 | + r0, r1, r2, r3 = tl.philox(philox_seed, c0, c1, _O, _O) |
| 78 | + r0 = uint_to_uniform_float(r0) |
| 79 | + r1 = uint_to_uniform_float(r1) |
| 80 | + r2 = uint_to_uniform_float(r2) |
| 81 | + r3 = uint_to_uniform_float(r3) |
| 82 | + |
| 83 | + mask0 = r0 > p |
| 84 | + mask1 = r1 > p |
| 85 | + mask2 = r2 > p |
| 86 | + mask3 = r3 > p |
| 87 | + |
| 88 | + alpha_prime = -1.7580993408473766 |
| 89 | + |
| 90 | + off_0 = tl.program_id(0) * BLOCK * UNROLL + tl.arange(0, BLOCK) |
| 91 | + off_1 = off_0 + BLOCK |
| 92 | + off_2 = off_1 + BLOCK |
| 93 | + off_3 = off_2 + BLOCK |
| 94 | + |
| 95 | + x0 = tl.load( |
| 96 | + X + off_0, |
| 97 | + mask=off_0 < N, |
| 98 | + other=0.0, |
| 99 | + eviction_policy="evict_first", |
| 100 | + ) |
| 101 | + x1 = tl.load( |
| 102 | + X + off_1, |
| 103 | + mask=off_1 < N, |
| 104 | + other=0.0, |
| 105 | + eviction_policy="evict_first", |
| 106 | + ) |
| 107 | + x2 = tl.load( |
| 108 | + X + off_2, |
| 109 | + mask=off_2 < N, |
| 110 | + other=0.0, |
| 111 | + eviction_policy="evict_first", |
| 112 | + ) |
| 113 | + x3 = tl.load( |
| 114 | + X + off_3, |
| 115 | + mask=off_3 < N, |
| 116 | + other=0.0, |
| 117 | + eviction_policy="evict_first", |
| 118 | + ) |
| 119 | + |
| 120 | + y0 = tl.where(mask0, a * x0 + b, a * alpha_prime + b) |
| 121 | + y1 = tl.where(mask1, a * x1 + b, a * alpha_prime + b) |
| 122 | + y2 = tl.where(mask2, a * x2 + b, a * alpha_prime + b) |
| 123 | + y3 = tl.where(mask3, a * x3 + b, a * alpha_prime + b) |
| 124 | + |
| 125 | + tl.store( |
| 126 | + Y + off_0, |
| 127 | + y0, |
| 128 | + mask=off_0 < N, |
| 129 | + eviction_policy="evict_first", |
| 130 | + ) |
| 131 | + tl.store( |
| 132 | + Y + off_1, |
| 133 | + y1, |
| 134 | + mask=off_1 < N, |
| 135 | + eviction_policy="evict_first", |
| 136 | + ) |
| 137 | + tl.store( |
| 138 | + Y + off_2, |
| 139 | + y2, |
| 140 | + mask=off_2 < N, |
| 141 | + eviction_policy="evict_first", |
| 142 | + ) |
| 143 | + tl.store( |
| 144 | + Y + off_3, |
| 145 | + y3, |
| 146 | + mask=off_3 < N, |
| 147 | + eviction_policy="evict_first", |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +def alpha_dropout(input, p=0.5, train=True): |
| 152 | + UNROLL = 4 |
| 153 | + logger.debug("GEMS_METAX ALPHA_DROPOUT") |
| 154 | + if not train or p == 0: |
| 155 | + return input.clone() |
| 156 | + if p == 1: |
| 157 | + return torch.zeros_like(input) |
| 158 | + |
| 159 | + assert 0.0 < p < 1.0, "p must be in (0, 1)" |
| 160 | + |
| 161 | + device = input.device |
| 162 | + input = input.contiguous() |
| 163 | + out = torch.empty_like(input) |
| 164 | + N = input.numel() |
| 165 | + grid_fn = lambda meta: (triton.cdiv(N, meta["BLOCK"] * UNROLL),) |
| 166 | + increment = triton.cdiv(N, UNROLL) |
| 167 | + |
| 168 | + a, b = _alpha_dropout_affine(p) |
| 169 | + |
| 170 | + with torch_device_fn.device(device): |
| 171 | + philox_seed, philox_offset = philox_backend_seed_offset(increment) |
| 172 | + alpha_dropout_forward_kernel[grid_fn]( |
| 173 | + input, out, N, p, a, b, philox_seed, philox_offset |
| 174 | + ) |
| 175 | + return out |
0 commit comments