fla-core 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fla/__init__.py +4 -0
- fla/modules/__init__.py +33 -0
- fla/modules/activations.py +548 -0
- fla/modules/convolution.py +1143 -0
- fla/modules/feature_map.py +300 -0
- fla/modules/fused_bitlinear.py +632 -0
- fla/modules/fused_cross_entropy.py +419 -0
- fla/modules/fused_kl_div.py +324 -0
- fla/modules/fused_linear_cross_entropy.py +620 -0
- fla/modules/fused_norm_gate.py +1253 -0
- fla/modules/grpo.py +411 -0
- fla/modules/l2norm.py +285 -0
- fla/modules/l2warp.py +38 -0
- fla/modules/layernorm.py +1433 -0
- fla/modules/layernorm_gated.py +528 -0
- fla/modules/mlp.py +132 -0
- fla/modules/parallel.py +42 -0
- fla/modules/rotary.py +500 -0
- fla/modules/token_shift.py +543 -0
- fla/ops/__init__.py +53 -0
- fla/ops/abc/__init__.py +7 -0
- fla/ops/abc/chunk.py +1117 -0
- fla/ops/abc/naive.py +96 -0
- fla/ops/attn/__init__.py +7 -0
- fla/ops/attn/decoding.py +182 -0
- fla/ops/attn/parallel.py +730 -0
- fla/ops/based/__init__.py +9 -0
- fla/ops/based/fused_chunk.py +373 -0
- fla/ops/based/naive.py +72 -0
- fla/ops/based/parallel.py +408 -0
- fla/ops/comba/__init__.py +7 -0
- fla/ops/comba/chunk.py +342 -0
- fla/ops/comba/fused_recurrent.py +332 -0
- fla/ops/comba/utils.py +172 -0
- fla/ops/comba/wy_fast.py +423 -0
- fla/ops/common/__init__.py +1 -0
- fla/ops/common/chunk_delta_h.py +527 -0
- fla/ops/common/chunk_h.py +384 -0
- fla/ops/common/chunk_h_parallel.py +551 -0
- fla/ops/common/chunk_h_split.py +596 -0
- fla/ops/common/chunk_o.py +681 -0
- fla/ops/common/chunk_scaled_dot_kkt.py +305 -0
- fla/ops/common/fused_chunk.py +629 -0
- fla/ops/common/fused_recurrent.py +567 -0
- fla/ops/delta_rule/__init__.py +11 -0
- fla/ops/delta_rule/chunk.py +311 -0
- fla/ops/delta_rule/fused_chunk.py +6 -0
- fla/ops/delta_rule/fused_recurrent.py +535 -0
- fla/ops/delta_rule/naive.py +120 -0
- fla/ops/delta_rule/parallel.py +403 -0
- fla/ops/delta_rule/wy_fast.py +294 -0
- fla/ops/forgetting_attn/__init__.py +7 -0
- fla/ops/forgetting_attn/parallel.py +64 -0
- fla/ops/gated_delta_product/__init__.py +5 -0
- fla/ops/gated_delta_product/chunk.py +305 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_h.py +502 -0
- fla/ops/gated_delta_product/chunk_deltaproduct_o.py +154 -0
- fla/ops/gated_delta_product/chunk_ref.py +68 -0
- fla/ops/gated_delta_product/naive.py +36 -0
- fla/ops/gated_delta_rule/__init__.py +7 -0
- fla/ops/gated_delta_rule/chunk.py +340 -0
- fla/ops/gated_delta_rule/fused_recurrent.py +351 -0
- fla/ops/gated_delta_rule/wy_fast.py +296 -0
- fla/ops/generalized_delta_rule/__init__.py +9 -0
- fla/ops/generalized_delta_rule/dplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/dplr/chunk.py +362 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_bwd.py +367 -0
- fla/ops/generalized_delta_rule/dplr/chunk_A_fwd.py +198 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_bwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_h_fwd.py +175 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_bwd.py +430 -0
- fla/ops/generalized_delta_rule/dplr/chunk_o_fwd.py +125 -0
- fla/ops/generalized_delta_rule/dplr/fused_recurrent.py +268 -0
- fla/ops/generalized_delta_rule/dplr/naive.py +96 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_bwd.py +164 -0
- fla/ops/generalized_delta_rule/dplr/wy_fast_fwd.py +284 -0
- fla/ops/generalized_delta_rule/iplr/__init__.py +7 -0
- fla/ops/generalized_delta_rule/iplr/chunk.py +492 -0
- fla/ops/generalized_delta_rule/iplr/fused_recurrent.py +452 -0
- fla/ops/generalized_delta_rule/iplr/naive.py +69 -0
- fla/ops/generalized_delta_rule/iplr/wy_fast.py +300 -0
- fla/ops/gla/__init__.py +11 -0
- fla/ops/gla/chunk.py +1298 -0
- fla/ops/gla/fused_chunk.py +642 -0
- fla/ops/gla/fused_recurrent.py +111 -0
- fla/ops/gla/naive.py +41 -0
- fla/ops/gsa/__init__.py +9 -0
- fla/ops/gsa/chunk.py +1133 -0
- fla/ops/gsa/fused_recurrent.py +536 -0
- fla/ops/gsa/naive.py +69 -0
- fla/ops/hgrn/__init__.py +9 -0
- fla/ops/hgrn/chunk.py +282 -0
- fla/ops/hgrn/fused_recurrent.py +308 -0
- fla/ops/hgrn/naive.py +63 -0
- fla/ops/lightning_attn/__init__.py +9 -0
- fla/ops/lightning_attn/chunk.py +84 -0
- fla/ops/lightning_attn/fused_recurrent.py +84 -0
- fla/ops/linear_attn/__init__.py +11 -0
- fla/ops/linear_attn/chunk.py +76 -0
- fla/ops/linear_attn/fused_chunk.py +61 -0
- fla/ops/linear_attn/fused_recurrent.py +35 -0
- fla/ops/linear_attn/naive.py +64 -0
- fla/ops/linear_attn/utils.py +10 -0
- fla/ops/log_linear_attn/__init__.py +7 -0
- fla/ops/log_linear_attn/chunk.py +1904 -0
- fla/ops/log_linear_attn/naive.py +50 -0
- fla/ops/mesa_net/__init__.py +5 -0
- fla/ops/mesa_net/chunk.py +371 -0
- fla/ops/mesa_net/chunk_cg_solver_bwd.py +162 -0
- fla/ops/mesa_net/chunk_cg_solver_fwd.py +186 -0
- fla/ops/mesa_net/chunk_h_fwd.py +170 -0
- fla/ops/mesa_net/chunk_h_kk_intra_bwd.py +191 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd.py +209 -0
- fla/ops/mesa_net/chunk_h_kv_intra_bwd_separate.py +300 -0
- fla/ops/mesa_net/decoding_one_step.py +175 -0
- fla/ops/mesa_net/naive.py +131 -0
- fla/ops/nsa/__init__.py +9 -0
- fla/ops/nsa/compression.py +536 -0
- fla/ops/nsa/naive.py +103 -0
- fla/ops/nsa/parallel.py +879 -0
- fla/ops/nsa/utils.py +92 -0
- fla/ops/path_attn/__init__.py +7 -0
- fla/ops/path_attn/cumprod_householder_bwd.py +140 -0
- fla/ops/path_attn/cumprod_householder_fwd.py +119 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd.py +140 -0
- fla/ops/path_attn/intra_chunk_preprocess_bwd_prepare.py +208 -0
- fla/ops/path_attn/intra_chunk_preprocess_fwd.py +168 -0
- fla/ops/path_attn/parallel.py +270 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dkv.py +189 -0
- fla/ops/path_attn/parallel_path_bwd_inter_dqh.py +200 -0
- fla/ops/path_attn/parallel_path_bwd_intra.py +172 -0
- fla/ops/path_attn/parallel_path_fwd.py +195 -0
- fla/ops/path_attn/prepare_k_cache.py +74 -0
- fla/ops/path_attn/transform_q.py +108 -0
- fla/ops/rebased/__init__.py +7 -0
- fla/ops/rebased/naive.py +27 -0
- fla/ops/rebased/parallel.py +464 -0
- fla/ops/retention/__init__.py +13 -0
- fla/ops/retention/chunk.py +77 -0
- fla/ops/retention/fused_chunk.py +78 -0
- fla/ops/retention/fused_recurrent.py +33 -0
- fla/ops/retention/naive.py +15 -0
- fla/ops/retention/parallel.py +71 -0
- fla/ops/rwkv4/__init__.py +7 -0
- fla/ops/rwkv4/fused_recurrent.py +472 -0
- fla/ops/rwkv6/__init__.py +9 -0
- fla/ops/rwkv6/chunk.py +1300 -0
- fla/ops/rwkv6/chunk_naive.py +43 -0
- fla/ops/rwkv6/fused_recurrent.py +673 -0
- fla/ops/rwkv6/recurrent_naive.py +103 -0
- fla/ops/rwkv7/__init__.py +10 -0
- fla/ops/rwkv7/channel_mixing.py +323 -0
- fla/ops/rwkv7/chunk.py +78 -0
- fla/ops/rwkv7/fused_addcmul.py +293 -0
- fla/ops/rwkv7/fused_k_update.py +346 -0
- fla/ops/rwkv7/fused_recurrent.py +334 -0
- fla/ops/rwkv7/gate_output_correction.py +244 -0
- fla/ops/simple_gla/__init__.py +13 -0
- fla/ops/simple_gla/chunk.py +303 -0
- fla/ops/simple_gla/fused_chunk.py +108 -0
- fla/ops/simple_gla/fused_recurrent.py +112 -0
- fla/ops/simple_gla/naive.py +114 -0
- fla/ops/simple_gla/parallel.py +722 -0
- fla/ops/titans/__init__.py +7 -0
- fla/ops/titans/log_impl.py +153 -0
- fla/ops/titans/naive.py +375 -0
- fla/ops/ttt/__init__.py +9 -0
- fla/ops/ttt/chunk.py +1441 -0
- fla/ops/ttt/fused_chunk.py +831 -0
- fla/ops/ttt/naive.py +126 -0
- fla/ops/utils/__init__.py +58 -0
- fla/ops/utils/constant.py +6 -0
- fla/ops/utils/cumsum.py +465 -0
- fla/ops/utils/index.py +133 -0
- fla/ops/utils/logcumsumexp.py +52 -0
- fla/ops/utils/logsumexp.py +80 -0
- fla/ops/utils/matmul.py +245 -0
- fla/ops/utils/op.py +62 -0
- fla/ops/utils/pack.py +208 -0
- fla/ops/utils/pooling.py +207 -0
- fla/ops/utils/softmax.py +106 -0
- fla/ops/utils/solve_tril.py +381 -0
- fla/utils.py +462 -0
- fla_core-0.3.2.dist-info/METADATA +575 -0
- fla_core-0.3.2.dist-info/RECORD +188 -0
- fla_core-0.3.2.dist-info/WHEEL +5 -0
- fla_core-0.3.2.dist-info/licenses/LICENSE +21 -0
- fla_core-0.3.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import math
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
import torch
|
|
9
|
+
import torch.nn.functional as F
|
|
10
|
+
from torch import nn
|
|
11
|
+
|
|
12
|
+
from fla.modules.activations import fast_gelu_impl, sigmoid, sqrelu, swish
|
|
13
|
+
from fla.modules.layernorm import layer_norm
|
|
14
|
+
from fla.utils import checkpoint
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@checkpoint
|
|
18
|
+
def flatten_diag_outer_product(x, y):
|
|
19
|
+
z = torch.einsum("...i,...j->...ij", x, y)
|
|
20
|
+
N = z.size(-1)
|
|
21
|
+
indicies = torch.triu_indices(N, N)
|
|
22
|
+
return z[..., indicies[0], indicies[1]]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@checkpoint
|
|
26
|
+
def flatten_diag_outer_product_off1(x, y):
|
|
27
|
+
z = torch.einsum("...i,...j->...ij", x, y)
|
|
28
|
+
N = z.size(-1)
|
|
29
|
+
indicies = torch.triu_indices(N, N, 1)
|
|
30
|
+
indices2 = torch.arange(0, N)
|
|
31
|
+
return z[..., indicies[0], indicies[1]], z[..., indices2, indices2]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def is_power_of_2(n):
|
|
35
|
+
return (n & (n - 1) == 0) and n != 0
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class HedgehogFeatureMap(nn.Module):
|
|
39
|
+
|
|
40
|
+
r"""
|
|
41
|
+
Hedgehog feature map as introduced in
|
|
42
|
+
`The Hedgehog & the Porcupine: Expressive Linear Attentions with Softmax Mimicry <https://arxiv.org/abs/2402.04347>`_
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
def __init__(
|
|
46
|
+
self,
|
|
47
|
+
head_dim: int
|
|
48
|
+
) -> HedgehogFeatureMap:
|
|
49
|
+
super().__init__()
|
|
50
|
+
# Trainable map
|
|
51
|
+
self.layer = nn.Linear(head_dim, head_dim)
|
|
52
|
+
self.init_weights_()
|
|
53
|
+
|
|
54
|
+
def init_weights_(self):
|
|
55
|
+
"""Initialize trainable map as identity"""
|
|
56
|
+
with torch.no_grad():
|
|
57
|
+
identity = torch.eye(*self.layer.weight.shape[-2:], dtype=torch.float)
|
|
58
|
+
self.layer.weight.copy_(identity.to(self.layer.weight))
|
|
59
|
+
nn.init.zeros_(self.layer.bias)
|
|
60
|
+
|
|
61
|
+
def forward(self, x: torch.Tensor):
|
|
62
|
+
x = self.layer(x) # shape b, h, l, d
|
|
63
|
+
return torch.cat([2*x, -2*x], dim=-1).softmax(-1)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class T2RFeatureMap(nn.Module):
|
|
67
|
+
|
|
68
|
+
r"""
|
|
69
|
+
Simple linear mapping feature map as in
|
|
70
|
+
`Finetuning Pretrained Transformers into RNNs <https://arxiv.org/abs/2103.13076>`_
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
def __init__(
|
|
74
|
+
self,
|
|
75
|
+
head_dim: int,
|
|
76
|
+
dot_dim: int = None,
|
|
77
|
+
bias: Optional[bool] = False
|
|
78
|
+
) -> T2RFeatureMap:
|
|
79
|
+
super().__init__()
|
|
80
|
+
# Trainable map
|
|
81
|
+
if dot_dim is None:
|
|
82
|
+
dot_dim = head_dim
|
|
83
|
+
|
|
84
|
+
self.head_dim = head_dim
|
|
85
|
+
self.dot_dim = dot_dim
|
|
86
|
+
self.bias = bias
|
|
87
|
+
|
|
88
|
+
self.layer = nn.Linear(head_dim, dot_dim, bias=bias)
|
|
89
|
+
|
|
90
|
+
def __repr__(self) -> str:
|
|
91
|
+
return f"{self.__class__.__name__}(head_dim={self.head_dim}, dot_dim={self.dot_dim}, bias={self.bias})"
|
|
92
|
+
|
|
93
|
+
def forward(self, x: torch.Tensor):
|
|
94
|
+
return self.layer(x).relu()
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class DPFPFeatureMap(nn.Module):
|
|
98
|
+
|
|
99
|
+
r"""
|
|
100
|
+
Deterministic Parameter-Free Projection (DPFP) feature map in
|
|
101
|
+
`Linear Transformers Are Secretly Fast Weight Programmers <https://arxiv.org/abs/2102.11174>`_
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def __init__(
|
|
105
|
+
self,
|
|
106
|
+
head_dim: int,
|
|
107
|
+
nu: int = 4
|
|
108
|
+
) -> DPFPFeatureMap:
|
|
109
|
+
super().__init__()
|
|
110
|
+
self.nu = nu
|
|
111
|
+
|
|
112
|
+
def forward(self, x: torch.Tensor):
|
|
113
|
+
x = torch.cat([x.relu(), -x.relu()], dim=-1)
|
|
114
|
+
x_rolled = torch.cat([x.roll(shifts=j, dims=-1) for j in range(1, self.nu+1)], dim=-1)
|
|
115
|
+
x_repeat = torch.cat([x] * self.nu, dim=-1)
|
|
116
|
+
return x_repeat * x_rolled
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class HadamardFeatureMap(nn.Module):
|
|
120
|
+
def __init__(
|
|
121
|
+
self,
|
|
122
|
+
head_dim: int
|
|
123
|
+
) -> HadamardFeatureMap:
|
|
124
|
+
super().__init__()
|
|
125
|
+
# Trainable map
|
|
126
|
+
self.layer1 = nn.Linear(head_dim, head_dim)
|
|
127
|
+
self.layer2 = nn.Linear(head_dim, head_dim)
|
|
128
|
+
|
|
129
|
+
def forward(self, x: torch.Tensor):
|
|
130
|
+
return self.layer1(x) * self.layer2(x)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class LearnableOuterProductFeatureMap(nn.Module):
|
|
134
|
+
def __init__(
|
|
135
|
+
self,
|
|
136
|
+
head_dim: int,
|
|
137
|
+
feature_dim: int
|
|
138
|
+
) -> LearnableOuterProductFeatureMap:
|
|
139
|
+
super().__init__()
|
|
140
|
+
# Trainable map
|
|
141
|
+
self.layer1 = nn.Linear(head_dim, feature_dim, bias=False)
|
|
142
|
+
self.layer2 = nn.Linear(head_dim, feature_dim, bias=False)
|
|
143
|
+
self.normalizer = feature_dim ** -0.5
|
|
144
|
+
|
|
145
|
+
def forward(self, x: torch.Tensor):
|
|
146
|
+
return flatten_diag_outer_product(self.layer1(x), self.layer2(x))
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
class LearnablePolySketchNonNegativeFeatureMap(nn.Module):
|
|
150
|
+
|
|
151
|
+
def __init__(
|
|
152
|
+
self,
|
|
153
|
+
head_dim: int,
|
|
154
|
+
sketch_size: Optional[int] = None,
|
|
155
|
+
degree: Optional[int] = 2
|
|
156
|
+
) -> LearnablePolySketchNonNegativeFeatureMap:
|
|
157
|
+
super().__init__()
|
|
158
|
+
|
|
159
|
+
assert is_power_of_2(degree) and degree >= 2, f"The degree {degree} must be a power of 2"
|
|
160
|
+
|
|
161
|
+
self.head_dim = head_dim
|
|
162
|
+
self.sketch_size = sketch_size if sketch_size is not None else head_dim
|
|
163
|
+
self.degree = degree
|
|
164
|
+
|
|
165
|
+
self.gamma = nn.Parameter(torch.ones(head_dim))
|
|
166
|
+
self.beta = nn.Parameter(torch.zeros(head_dim))
|
|
167
|
+
# NOTE: the sketch layers defined here are quite different from the original paper
|
|
168
|
+
# currently we simply use linear layers without any non-linear activations
|
|
169
|
+
self.sketches1 = nn.ModuleList([
|
|
170
|
+
nn.Linear(head_dim, sketch_size, bias=False),
|
|
171
|
+
*[nn.Linear(sketch_size, sketch_size, bias=False) for _ in range(int(math.log2(self.degree)) - 2)]
|
|
172
|
+
])
|
|
173
|
+
self.sketches2 = nn.ModuleList([
|
|
174
|
+
nn.Linear(head_dim, sketch_size, bias=False),
|
|
175
|
+
*[nn.Linear(sketch_size, sketch_size, bias=False) for _ in range(int(math.log2(self.degree)) - 2)]
|
|
176
|
+
])
|
|
177
|
+
|
|
178
|
+
def forward(self, x: torch.Tensor):
|
|
179
|
+
# Section 2.1
|
|
180
|
+
x = layer_norm(x, self.gamma, self.beta)
|
|
181
|
+
# first map the input to sketch size with learnable parameters
|
|
182
|
+
x = self.sketches1[0](x) * self.sketches2[0](x) * self.head_dim ** -0.5
|
|
183
|
+
for i in range(1, int(math.log2(self.degree)) - 1):
|
|
184
|
+
x = self.sketches1[i](x) * self.sketches2[i](x) * self.head_dim ** -0.5
|
|
185
|
+
# do sketch mapping for log2(p) - 1 times in total
|
|
186
|
+
# do p=2 mapping to ensure non-negativity
|
|
187
|
+
return flatten_diag_outer_product(x, x)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class TaylorFeatureMap(nn.Module):
|
|
191
|
+
def __init__(
|
|
192
|
+
self,
|
|
193
|
+
head_dim: int
|
|
194
|
+
) -> TaylorFeatureMap:
|
|
195
|
+
super().__init__()
|
|
196
|
+
self.head_dim = head_dim
|
|
197
|
+
self.r2 = math.sqrt(2)
|
|
198
|
+
self.rd = math.sqrt(self.head_dim)
|
|
199
|
+
self.rrd = math.sqrt(self.rd)
|
|
200
|
+
|
|
201
|
+
def forward(self, x: torch.Tensor):
|
|
202
|
+
x2_1, x2_2 = flatten_diag_outer_product_off1(x, x)
|
|
203
|
+
return torch.cat([torch.ones_like(x[..., 0:1]), x / self.rrd, x2_2 / (self.rd * self.r2), x2_1 / self.rd], dim=-1)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class RebasedFeatureMap(nn.Module):
|
|
207
|
+
|
|
208
|
+
def __init__(
|
|
209
|
+
self,
|
|
210
|
+
head_dim: int,
|
|
211
|
+
use_gamma: Optional[bool] = True,
|
|
212
|
+
use_beta: Optional[bool] = True,
|
|
213
|
+
normalize: Optional[bool] = True
|
|
214
|
+
) -> RebasedFeatureMap:
|
|
215
|
+
super().__init__()
|
|
216
|
+
|
|
217
|
+
self.head_dim = head_dim
|
|
218
|
+
self.use_gamma = use_gamma
|
|
219
|
+
self.use_beta = use_beta
|
|
220
|
+
self.normalize = normalize
|
|
221
|
+
|
|
222
|
+
self.gamma = None
|
|
223
|
+
self.beta = None
|
|
224
|
+
if use_gamma:
|
|
225
|
+
self.gamma = nn.Parameter(torch.ones(head_dim))
|
|
226
|
+
if use_beta:
|
|
227
|
+
self.beta = nn.Parameter(torch.zeros(head_dim))
|
|
228
|
+
|
|
229
|
+
def forward(self, x: torch.Tensor, flatten: Optional[bool] = True):
|
|
230
|
+
if self.use_beta and self.use_gamma and self.normalize:
|
|
231
|
+
x = layer_norm(x, self.gamma, self.beta)
|
|
232
|
+
elif self.normalize:
|
|
233
|
+
x = F.layer_norm(x, (self.head_dim,), self.gamma, self.beta)
|
|
234
|
+
elif self.use_gamma and self.use_beta:
|
|
235
|
+
x = torch.addcmul(self.beta, x, self.gamma)
|
|
236
|
+
elif self.use_gamma:
|
|
237
|
+
x = x.mul(self.gamma)
|
|
238
|
+
else:
|
|
239
|
+
raise RuntimeError(f"Not supported combination of `use_gamma`, `use_beta` and `normalize`, "
|
|
240
|
+
f"which is currentlt set as (`{self.use_gamma}`, `{self.use_beta}`, `{self.normalize}`)")
|
|
241
|
+
if not flatten:
|
|
242
|
+
return x
|
|
243
|
+
x2_1, x2_2 = flatten_diag_outer_product_off1(x, x)
|
|
244
|
+
# rebased use learnable parameters to approximate any quadratic function
|
|
245
|
+
return torch.cat([x2_2 * self.head_dim ** -0.5, x2_1 * (2 / self.head_dim) ** 0.5], dim=-1)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class ReLUFeatureMap(nn.Module):
|
|
249
|
+
|
|
250
|
+
def __init__(
|
|
251
|
+
self,
|
|
252
|
+
) -> ReLUFeatureMap:
|
|
253
|
+
super().__init__()
|
|
254
|
+
|
|
255
|
+
def forward(self, x: torch.Tensor):
|
|
256
|
+
return F.relu(x)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
class SquaredReLUFeatureMap(nn.Module):
|
|
260
|
+
|
|
261
|
+
def __init__(
|
|
262
|
+
self,
|
|
263
|
+
) -> SquaredReLUFeatureMap:
|
|
264
|
+
super().__init__()
|
|
265
|
+
|
|
266
|
+
def forward(self, x: torch.Tensor):
|
|
267
|
+
return sqrelu(x)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class GELUFeatureMap(nn.Module):
|
|
271
|
+
|
|
272
|
+
def __init__(
|
|
273
|
+
self,
|
|
274
|
+
) -> GELUFeatureMap:
|
|
275
|
+
super().__init__()
|
|
276
|
+
|
|
277
|
+
def forward(self, x: torch.Tensor):
|
|
278
|
+
return fast_gelu_impl(x)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class SwishFeatureMap(nn.Module):
|
|
282
|
+
|
|
283
|
+
def __init__(
|
|
284
|
+
self,
|
|
285
|
+
) -> SwishFeatureMap:
|
|
286
|
+
super().__init__()
|
|
287
|
+
|
|
288
|
+
def forward(self, x: torch.Tensor):
|
|
289
|
+
return swish(x)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
class SigmoidFeatureMap(nn.Module):
|
|
293
|
+
|
|
294
|
+
def __init__(
|
|
295
|
+
self,
|
|
296
|
+
) -> SigmoidFeatureMap:
|
|
297
|
+
super().__init__()
|
|
298
|
+
|
|
299
|
+
def forward(self, x: torch.Tensor):
|
|
300
|
+
return sigmoid(x)
|