Instructions to use kernels-community/activation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use kernels-community/activation with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("kernels-community/activation", version=1) - Notebooks
- Google Colab
- Kaggle
File size: 5,407 Bytes
74459d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | import torch
import torch.nn as nn
from ._ops import ops
class SiluAndMul(nn.Module):
"""An activation function for SwiGLU.
The function computes x -> silu(x[:d]) * x[d:] where d = x.shape[-1] // 2.
Shapes:
x: (num_tokens, 2 * d) or (batch_size, seq_len, 2 * d)
return: (num_tokens, d) or (batch_size, seq_len, d)
"""
can_torch_compile: bool = True
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
ops.silu_and_mul(out, x)
return out
class Silu(nn.Module):
"""An activation function for SiLU.
The function computes x -> silu(x).
Shapes:
x: (num_tokens, d) or (batch_size, seq_len, d)
return: (num_tokens, d) or (batch_size, seq_len, d)
"""
can_torch_compile: bool = True
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
out = torch.empty_like(x)
ops.silu(out, x)
return out
class Gelu(nn.Module):
"""An activation function for GELU.
The function computes x -> gelu(x).
Shapes:
x: (num_tokens, d) or (batch_size, seq_len, d)
return: (num_tokens, d) or (batch_size, seq_len, d)
"""
can_torch_compile: bool = True
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
out = torch.empty_like(x)
ops.gelu(out, x)
return out
class GeluTanh(nn.Module):
"""An activation function for GELU with `tanh` approximation.
The function computes x -> gelu_tanh(x).
Shapes:
x: (num_tokens, d) or (batch_size, seq_len, d)
return: (num_tokens, d) or (batch_size, seq_len, d)
"""
can_torch_compile: bool = True
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
out = torch.empty_like(x)
ops.gelu_tanh(out, x)
return out
class MulAndSilu(nn.Module):
"""An activation function for SwiGLU.
The function computes x -> x[:d] * silu(x[d:]) where d = x.shape[-1] // 2.
Shapes:
x: (num_tokens, 2 * d) or (batch_size, seq_len, 2 * d)
return: (num_tokens, d) or (batch_size, seq_len, d)
"""
can_torch_compile: bool = True
def forward(self, x: torch.Tensor) -> torch.Tensor:
if not x.is_contiguous():
x = x.contiguous()
d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
ops.mul_and_silu(out, x)
return out
class GeluAndMul(nn.Module):
"""An activation function for GeGLU.
The function computes x -> GELU(x[:d]) * x[d:] where d = x.shape[-1] // 2.
Shapes:
x: (batch_size, seq_len, 2 * d) or (num_tokens, 2 * d)
return: (batch_size, seq_len, d) or (num_tokens, d)
"""
can_torch_compile: bool = True
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
ops.gelu_and_mul(out, x)
return out
class GeluTanhAndMul(nn.Module):
can_torch_compile: bool = True
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
ops.gelu_tanh_and_mul(out, x)
return out
class FatreluAndMul(nn.Module):
"""An activation function for FATReLU.
The function computes x -> FATReLU(x[:d]) * x[d:] where
d = x.shape[-1] // 2.
This is used in openbmb/MiniCPM-S-1B-sft.
Shapes:
x: (num_tokens, 2 * d) or (batch_size, seq_len, 2 * d)
return: (num_tokens, d) or (batch_size, seq_len, d)
"""
can_torch_compile: bool = True
def __init__(self, threshold: float = 0.0):
super().__init__()
self.threshold = threshold
def forward(self, x: torch.Tensor):
if not x.is_contiguous():
x = x.contiguous()
d = x.shape[-1] // 2
output_shape = x.shape[:-1] + (d,)
out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
ops.fatrelu_and_mul(out, x, self.threshold)
return out
class FastGELU(nn.Module):
can_torch_compile: bool = True
def forward(self, x: torch.Tensor) -> torch.Tensor:
if not x.is_contiguous():
x = x.contiguous()
out = torch.empty_like(x)
ops.gelu_fast(out, x)
return out
class NewGELU(nn.Module):
can_torch_compile: bool = True
def forward(self, x: torch.Tensor) -> torch.Tensor:
if not x.is_contiguous():
x = x.contiguous()
out = torch.empty_like(x)
ops.gelu_new(out, x)
return out
class QuickGELU(nn.Module):
can_torch_compile: bool = True
def forward(self, x: torch.Tensor) -> torch.Tensor:
if not x.is_contiguous():
x = x.contiguous()
out = torch.empty_like(x)
ops.gelu_quick(out, x)
return out
|