|
| 1 | +import infinicore |
| 2 | +from infinicore.lib import _infinicore |
| 3 | + |
| 4 | +__all__ = ["causal_softmax", "rms_norm", "silu", "swiglu"] |
| 5 | + |
| 6 | + |
| 7 | +def causal_softmax( |
| 8 | + input: infinicore.Tensor, |
| 9 | + out=None |
| 10 | +) -> infinicore.Tensor: |
| 11 | + r"""Apply a causal softmax function. |
| 12 | + """ |
| 13 | + |
| 14 | + if out is None: |
| 15 | + return infinicore.Tensor(_infinicore.causal_softmax(input._underlying)) |
| 16 | + |
| 17 | + _infinicore.causal_softmax_(out._underlying, input._underlying) |
| 18 | + |
| 19 | + return out |
| 20 | + |
| 21 | + |
| 22 | +def rms_norm( |
| 23 | + input: infinicore.Tensor, |
| 24 | + normalized_shape: list[int], |
| 25 | + weight: infinicore.Tensor, |
| 26 | + eps: float = 1e-5, |
| 27 | + out=None |
| 28 | +) -> infinicore.Tensor: |
| 29 | + r"""Apply Root Mean Square Layer Normalization. |
| 30 | + """ |
| 31 | + |
| 32 | + assert normalized_shape == weight.shape, "normalized_shape does not match weight.shape." |
| 33 | + |
| 34 | + if out is None: |
| 35 | + return infinicore.Tensor( |
| 36 | + _infinicore.rms_norm(input._underlying, weight._underlying, eps) |
| 37 | + ) |
| 38 | + |
| 39 | + _infinicore.rms_norm_(out._underlying, input._underlying, weight._underlying, eps) |
| 40 | + |
| 41 | + return out |
| 42 | + |
| 43 | + |
| 44 | +def silu(input: infinicore.Tensor, inplace: bool = False, out=None) -> infinicore.Tensor: |
| 45 | + r"""Apply the Sigmoid Linear Unit (SiLU) function, element-wise. |
| 46 | + """ |
| 47 | + |
| 48 | + if inplace: |
| 49 | + _infinicore.silu_(input._underlying, input._underlying) |
| 50 | + return input |
| 51 | + |
| 52 | + if out is None: |
| 53 | + return infinicore.Tensor(_infinicore.silu(input._underlying)) |
| 54 | + |
| 55 | + _infinicore.silu_(out._underlying, input._underlying) |
| 56 | + |
| 57 | + return out |
| 58 | + |
| 59 | + |
| 60 | +def swiglu(input: infinicore.Tensor, other: infinicore.Tensor, out=None): |
| 61 | + r"""Apply the Swish-Gated Linear Unit (SwiGLU) function, element-wise. |
| 62 | + """ |
| 63 | + |
| 64 | + if out is None: |
| 65 | + return infinicore.Tensor(_infinicore.swiglu(input._underlying, other._underlying)) |
| 66 | + |
| 67 | + _infinicore.swiglu_(out._underlying, input._underlying, other._underlying) |
| 68 | + |
| 69 | + return out |
0 commit comments