| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | # Copyright (c) 2026 LightSeek Foundation |
| 5 | # |
| 6 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | # of this software and associated documentation files (the "Software"), to deal |
| 8 | # in the Software without restriction, including without limitation the rights |
| 9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | # copies of the Software, and to permit persons to whom the Software is |
| 11 | # furnished to do so, subject to the following conditions: |
| 12 | # |
| 13 | # The above copyright notice and this permission notice shall be included in |
| 14 | # all copies or substantial portions of the Software. |
| 15 | # |
| 16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | # SOFTWARE. |
| 23 | |
| 24 | from vllm.triton_utils import aggregate, gl, gluon, tl |
| 25 | |
| 26 | _INV_LN2_VALUE = 1.4426950408889634 |
| 27 | _INV_LN2 = tl.constexpr(_INV_LN2_VALUE) |
| 28 | _LN2_VALUE = 0.6931471805599453 |
| 29 | _LN2 = tl.constexpr(_LN2_VALUE) |
| 30 | _PROPAGATE_NAN_ALL = gl.constexpr(tl.PropagateNan.ALL) |
| 31 | |
| 32 | |
| 33 | @gluon.jit |
| 34 | def maximum(a, b, propagate_nan: gl.constexpr = _PROPAGATE_NAN_ALL): |
| 35 | return gl.maximum(a, b, propagate_nan=propagate_nan) |
| 36 | |
| 37 | |
| 38 | @gluon.jit |
| 39 | def max(input, axis=None, keep_dims=False): |
| 40 | return gl.reduce(input, axis, maximum, keep_dims=keep_dims) |
| 41 | |
| 42 | |
| 43 | @aggregate |
| 44 | class InputStrides: |
| 45 | stride_t: gl.constexpr |
| 46 | stride_h: gl.constexpr |
| 47 | stride_d: gl.constexpr |
| 48 | |
| 49 | @gluon.constexpr_function |
| 50 | def __init__(self, stride_t, stride_h, stride_d): |
| 51 | self.stride_t = gl.constexpr(stride_t) |
| 52 | self.stride_h = gl.constexpr(stride_h) |
| 53 | self.stride_d = gl.constexpr(stride_d) |
| 54 | |
| 55 | @gluon.jit |
| 56 | def offsets(self, token, head, dim): |
| 57 | return (token * self.stride_t + head * self.stride_h + dim * self.stride_d).to( |
| 58 | gl.int32 |
| 59 | ) |
| 60 | |
| 61 | |
| 62 | @aggregate |
| 63 | class PagedKVStrides: |
| 64 | stride_b: gl.constexpr |
| 65 | stride_p: gl.constexpr |
| 66 | stride_h: gl.constexpr |
| 67 | stride_d: gl.constexpr |
| 68 | |
| 69 | @gluon.constexpr_function |
| 70 | def __init__(self, stride_b, stride_p, stride_h, stride_d): |
| 71 | self.stride_b = gl.constexpr(stride_b) |
| 72 | self.stride_p = gl.constexpr(stride_p) |
| 73 | self.stride_h = gl.constexpr(stride_h) |
| 74 | self.stride_d = gl.constexpr(stride_d) |
| 75 | |
| 76 | @gluon.jit |
| 77 | def offsets(self, page, token, head, dim): |
| 78 | # KV pools can exceed the 32-bit buffer-offset range. Keep the page |
| 79 | # arithmetic in int64, matching the original kernel's large-cache path. |
| 80 | return ( |
| 81 | page.to(gl.int64) * self.stride_b |
| 82 | + token.to(gl.int64) * self.stride_p |
| 83 | + head * self.stride_h |
| 84 | + dim * self.stride_d |
| 85 | ) |