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,324 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
from typing import Tuple
|
|
4
|
+
|
|
5
|
+
import torch
|
|
6
|
+
import torch.nn as nn
|
|
7
|
+
import torch.nn.functional as F
|
|
8
|
+
import triton
|
|
9
|
+
import triton.language as tl
|
|
10
|
+
|
|
11
|
+
from fla.ops.utils.op import exp, log
|
|
12
|
+
from fla.utils import input_guard, is_amd
|
|
13
|
+
|
|
14
|
+
# The hard limit of TRITON_MAX_TENSOR_NUMEL is 1048576
|
|
15
|
+
# https://github.com/triton-lang/triton/blob/ba42a5c68fd0505f8c42f4202d53be0f8d9a5fe0/python/triton/language/core.py#L19
|
|
16
|
+
# However, setting limit as 65536 as in LayerNorm tutorial is faster because of less register spilling
|
|
17
|
+
# The optimal maximum block size depends on your hardware, your kernel, and your dtype
|
|
18
|
+
MAX_FUSED_SIZE = 65536 // 2
|
|
19
|
+
STATIC_WARPS = 32 if not is_amd else 16
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@triton.jit
|
|
23
|
+
def kl_div_kernel(
|
|
24
|
+
logits,
|
|
25
|
+
target_logits,
|
|
26
|
+
loss,
|
|
27
|
+
s_logits,
|
|
28
|
+
s_loss,
|
|
29
|
+
reduction: tl.constexpr,
|
|
30
|
+
N: tl.constexpr,
|
|
31
|
+
V: tl.constexpr,
|
|
32
|
+
BV: tl.constexpr
|
|
33
|
+
):
|
|
34
|
+
# https://github.com/triton-lang/triton/issues/1058
|
|
35
|
+
# If N*V is too large, i_n * stride will overflow out of int32, so we convert to int64
|
|
36
|
+
i_n = tl.program_id(0).to(tl.int64)
|
|
37
|
+
|
|
38
|
+
logits += i_n * s_logits
|
|
39
|
+
target_logits += i_n * s_logits
|
|
40
|
+
|
|
41
|
+
# m is the max value. use the notation from the paper
|
|
42
|
+
sm = float('-inf')
|
|
43
|
+
tm = float('-inf')
|
|
44
|
+
# d is the sum. use the notation from the paper
|
|
45
|
+
sd, td = 0.0, 0.0
|
|
46
|
+
|
|
47
|
+
NV = tl.cdiv(V, BV)
|
|
48
|
+
for iv in range(0, NV):
|
|
49
|
+
o_x = iv * BV + tl.arange(0, BV)
|
|
50
|
+
# for student
|
|
51
|
+
b_sl = tl.load(logits + o_x, mask=o_x < V, other=float('-inf'))
|
|
52
|
+
b_sm = tl.max(b_sl)
|
|
53
|
+
m_new = tl.maximum(sm, b_sm)
|
|
54
|
+
sd = sd * exp(sm - m_new) + tl.sum(exp(b_sl - m_new))
|
|
55
|
+
sm = m_new
|
|
56
|
+
# for teacher
|
|
57
|
+
b_tl = tl.load(target_logits + o_x, mask=o_x < V, other=float('-inf'))
|
|
58
|
+
b_tm = tl.max(b_tl)
|
|
59
|
+
m_new = tl.maximum(tm, b_tm)
|
|
60
|
+
td = td * exp(tm - m_new) + tl.sum(exp(b_tl - m_new))
|
|
61
|
+
tm = m_new
|
|
62
|
+
|
|
63
|
+
b_loss = 0.
|
|
64
|
+
# KL(y_true || y) = exp(y_true) * (log(y_true) - log(y))
|
|
65
|
+
for iv in range(0, NV):
|
|
66
|
+
o_x = iv * BV + tl.arange(0, BV)
|
|
67
|
+
b_sl = tl.load(logits + o_x, mask=o_x < V, other=float('-inf'))
|
|
68
|
+
b_tl = tl.load(target_logits + o_x, mask=o_x < V, other=float('-inf'))
|
|
69
|
+
b_sp_log = b_sl - sm - log(sd)
|
|
70
|
+
b_tp_log = b_tl - tm - log(td)
|
|
71
|
+
b_sp = exp(b_sp_log)
|
|
72
|
+
b_tp = exp(b_tp_log)
|
|
73
|
+
b_kl = tl.where(o_x < V, b_tp * (b_tp_log - b_sp_log), 0)
|
|
74
|
+
b_dl = -b_tp + b_sp
|
|
75
|
+
b_loss += tl.sum(b_kl)
|
|
76
|
+
if reduction == 'batchmean':
|
|
77
|
+
b_dl = b_dl / N
|
|
78
|
+
tl.store(logits + o_x, b_dl, mask=o_x < V)
|
|
79
|
+
|
|
80
|
+
# Normalize the loss by the number of elements if reduction is 'batchmean'
|
|
81
|
+
if reduction == 'batchmean':
|
|
82
|
+
b_loss = b_loss / N
|
|
83
|
+
|
|
84
|
+
tl.store(loss + i_n * s_loss, b_loss)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@triton.jit
|
|
88
|
+
def elementwise_mul_kernel(
|
|
89
|
+
x,
|
|
90
|
+
g,
|
|
91
|
+
N: tl.constexpr,
|
|
92
|
+
B: tl.constexpr
|
|
93
|
+
):
|
|
94
|
+
"""
|
|
95
|
+
This function multiplies each element of the tensor pointed by x with the value pointed by g.
|
|
96
|
+
The multiplication is performed in-place on the tensor pointed by x.
|
|
97
|
+
|
|
98
|
+
Parameters:
|
|
99
|
+
x:
|
|
100
|
+
Pointer to the input tensor.
|
|
101
|
+
g:
|
|
102
|
+
Pointer to the gradient output value.
|
|
103
|
+
N (int):
|
|
104
|
+
The number of columns in the input tensor.
|
|
105
|
+
B (int):
|
|
106
|
+
The block size for Triton operations.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
# Get the program ID and convert it to int64 to avoid overflow
|
|
110
|
+
i_x = tl.program_id(0).to(tl.int64)
|
|
111
|
+
o_x = i_x * B + tl.arange(0, B)
|
|
112
|
+
|
|
113
|
+
# Load the gradient output value
|
|
114
|
+
b_g = tl.load(g)
|
|
115
|
+
b_x = tl.load(x + o_x, mask=o_x < N)
|
|
116
|
+
tl.store(x + o_x, b_x * b_g, mask=o_x < N)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def fused_kl_div_forward(
|
|
120
|
+
x: torch.Tensor,
|
|
121
|
+
target_x: torch.Tensor,
|
|
122
|
+
weight: torch.Tensor,
|
|
123
|
+
target_weight: torch.Tensor,
|
|
124
|
+
reduction: str = 'batchmean'
|
|
125
|
+
):
|
|
126
|
+
device = x.device
|
|
127
|
+
|
|
128
|
+
# ideally, we would like to achieve the same memory consumption as [N, H],
|
|
129
|
+
# so the expected chunk size should be:
|
|
130
|
+
# NC = ceil(V / H)
|
|
131
|
+
# C = ceil(N / NC)
|
|
132
|
+
# for ex: N = 4096*4, V = 32000, H = 4096 ==> NC = 8, C = ceil(N / NC) = 2048
|
|
133
|
+
N, H, V = *x.shape, weight.shape[0]
|
|
134
|
+
BV = min(MAX_FUSED_SIZE, triton.next_power_of_2(V))
|
|
135
|
+
# TODO: in real cases, we may need to limit the number of chunks NC to
|
|
136
|
+
# ensure the precisions of accumulated gradients
|
|
137
|
+
NC = min(8, triton.cdiv(V, H))
|
|
138
|
+
C = triton.next_power_of_2(triton.cdiv(N, NC))
|
|
139
|
+
NC = triton.cdiv(N, C)
|
|
140
|
+
|
|
141
|
+
dx = torch.zeros_like(x, device=device)
|
|
142
|
+
dw = torch.zeros_like(weight, device=device) if weight is not None else None
|
|
143
|
+
# we use fp32 for loss accumulator
|
|
144
|
+
loss = torch.zeros(N, dtype=torch.float32, device=device)
|
|
145
|
+
|
|
146
|
+
for ic in range(NC):
|
|
147
|
+
start, end = ic * C, min((ic + 1) * C, N)
|
|
148
|
+
# [C, N]
|
|
149
|
+
c_sx = x[start:end]
|
|
150
|
+
c_tx = target_x[start:end]
|
|
151
|
+
# when doing matmul, use the original precision
|
|
152
|
+
# [C, V]
|
|
153
|
+
c_sl = F.linear(c_sx, weight)
|
|
154
|
+
c_tl = F.linear(c_tx, target_weight)
|
|
155
|
+
|
|
156
|
+
# unreduced loss
|
|
157
|
+
c_loss = loss[start:end]
|
|
158
|
+
|
|
159
|
+
# Here we calculate the gradient of c_sx in place so we can save memory.
|
|
160
|
+
kl_div_kernel[(c_sx.shape[0],)](
|
|
161
|
+
logits=c_sl,
|
|
162
|
+
target_logits=c_tl,
|
|
163
|
+
loss=c_loss,
|
|
164
|
+
s_logits=c_sl.stride(-2),
|
|
165
|
+
s_loss=c_loss.stride(-1),
|
|
166
|
+
reduction=reduction,
|
|
167
|
+
N=N,
|
|
168
|
+
V=V,
|
|
169
|
+
BV=BV,
|
|
170
|
+
num_warps=STATIC_WARPS
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
# gradient of logits is computed in-place by the above triton kernel and is of shape: C x V
|
|
174
|
+
# thus dx[start: end] should be of shape: C x H
|
|
175
|
+
# additionally, since we are chunking the inputs, observe that the loss and gradients are calculated only
|
|
176
|
+
# on `n_non_ignore` tokens. However, the gradient of the input should be calculated for all tokens.
|
|
177
|
+
# Thus, we need an additional scaling factor of (n_non_ignore/total) to scale the gradients.
|
|
178
|
+
# [C, H]
|
|
179
|
+
|
|
180
|
+
dx[start:end] = torch.mm(c_sl, weight)
|
|
181
|
+
|
|
182
|
+
if weight is not None:
|
|
183
|
+
torch.addmm(input=dw, mat1=c_sl.t(), mat2=c_sx, out=dw)
|
|
184
|
+
|
|
185
|
+
loss = loss.sum()
|
|
186
|
+
return loss, dx, dw
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def fused_kl_div_backward(
|
|
190
|
+
do: torch.Tensor,
|
|
191
|
+
dx: torch.Tensor,
|
|
192
|
+
dw: torch.Tensor
|
|
193
|
+
):
|
|
194
|
+
# If cross entropy is the last layer, do is 1.0. Skip the mul to save time
|
|
195
|
+
if torch.ne(do, torch.tensor(1.0, device=do.device)):
|
|
196
|
+
# We use a Triton kernel instead of a PyTorch operation because modifying inputs in-place
|
|
197
|
+
# for gradient storage and backward multiple times causes anomalies with PyTorch but not with Triton.
|
|
198
|
+
N, H = dx.shape
|
|
199
|
+
B = min(MAX_FUSED_SIZE, triton.next_power_of_2(H))
|
|
200
|
+
|
|
201
|
+
elementwise_mul_kernel[(triton.cdiv(N * H, B),)](
|
|
202
|
+
x=dx,
|
|
203
|
+
g=do,
|
|
204
|
+
N=N*H,
|
|
205
|
+
B=B,
|
|
206
|
+
num_warps=STATIC_WARPS,
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
# handle dw
|
|
210
|
+
if dw is not None:
|
|
211
|
+
V, H = dw.shape
|
|
212
|
+
elementwise_mul_kernel[(triton.cdiv(V * H, B),)](
|
|
213
|
+
x=dw,
|
|
214
|
+
g=do,
|
|
215
|
+
N=V*H,
|
|
216
|
+
B=B,
|
|
217
|
+
num_warps=STATIC_WARPS,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
return dx, dw
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
class FusedKLDivLossFunction(torch.autograd.Function):
|
|
224
|
+
|
|
225
|
+
@staticmethod
|
|
226
|
+
@input_guard
|
|
227
|
+
def forward(
|
|
228
|
+
ctx,
|
|
229
|
+
x: torch.Tensor,
|
|
230
|
+
target_x: torch.Tensor,
|
|
231
|
+
weight: torch.Tensor,
|
|
232
|
+
target_weight: torch.Tensor,
|
|
233
|
+
reduction: str
|
|
234
|
+
):
|
|
235
|
+
loss, dx, dw = fused_kl_div_forward(
|
|
236
|
+
x=x,
|
|
237
|
+
target_x=target_x,
|
|
238
|
+
weight=weight,
|
|
239
|
+
target_weight=target_weight,
|
|
240
|
+
reduction=reduction
|
|
241
|
+
)
|
|
242
|
+
ctx.save_for_backward(dx, dw)
|
|
243
|
+
return loss
|
|
244
|
+
|
|
245
|
+
@staticmethod
|
|
246
|
+
@input_guard
|
|
247
|
+
def backward(ctx, do):
|
|
248
|
+
dx, dw = ctx.saved_tensors
|
|
249
|
+
dx, dw = fused_kl_div_backward(do, dx, dw)
|
|
250
|
+
return dx, None, dw, None, None
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def fused_kl_div_loss(
|
|
254
|
+
x: torch.Tensor,
|
|
255
|
+
target_x: torch.Tensor,
|
|
256
|
+
weight: torch.Tensor,
|
|
257
|
+
target_weight: torch.Tensor,
|
|
258
|
+
reduction: str = 'batchmean'
|
|
259
|
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
260
|
+
"""
|
|
261
|
+
Args:
|
|
262
|
+
x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
|
263
|
+
target_x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
|
264
|
+
weight (torch.Tensor): [vocab_size, hidden_size]
|
|
265
|
+
where `vocab_size` is the number of classes.
|
|
266
|
+
target_weight (torch.Tensor): [vocab_size, hidden_size]
|
|
267
|
+
where `vocab_size` is the number of classes.
|
|
268
|
+
reduction:
|
|
269
|
+
Specifies the reduction to apply to the output: 'batchmean'. Default: 'batchmean'.
|
|
270
|
+
Returns:
|
|
271
|
+
loss
|
|
272
|
+
"""
|
|
273
|
+
return FusedKLDivLossFunction.apply(
|
|
274
|
+
x,
|
|
275
|
+
target_x,
|
|
276
|
+
weight,
|
|
277
|
+
target_weight,
|
|
278
|
+
reduction
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
class FusedKLDivLoss(nn.Module):
|
|
283
|
+
|
|
284
|
+
def __init__(
|
|
285
|
+
self,
|
|
286
|
+
reduction: str = 'batchmean'
|
|
287
|
+
):
|
|
288
|
+
"""
|
|
289
|
+
Args:
|
|
290
|
+
reduction:
|
|
291
|
+
Specifies the reduction to apply to the output: 'batchmean'. Default: 'batchmean'.
|
|
292
|
+
"""
|
|
293
|
+
super().__init__()
|
|
294
|
+
|
|
295
|
+
assert reduction in ['batchmean'], f"reduction: {reduction} is not supported"
|
|
296
|
+
|
|
297
|
+
self.reduction = reduction
|
|
298
|
+
|
|
299
|
+
def forward(
|
|
300
|
+
self,
|
|
301
|
+
x: torch.Tensor,
|
|
302
|
+
target_x: torch.Tensor,
|
|
303
|
+
weight: torch.Tensor,
|
|
304
|
+
target_weight: torch.Tensor
|
|
305
|
+
):
|
|
306
|
+
"""
|
|
307
|
+
Args:
|
|
308
|
+
x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
|
309
|
+
target_x (torch.Tensor): [batch_size * seq_len, hidden_size]
|
|
310
|
+
weight (torch.Tensor): [vocab_size, hidden_size]
|
|
311
|
+
where `vocab_size` is the number of classes.
|
|
312
|
+
target_weight (torch.Tensor): [vocab_size, hidden_size]
|
|
313
|
+
where `vocab_size` is the number of classes.
|
|
314
|
+
Returns:
|
|
315
|
+
loss
|
|
316
|
+
"""
|
|
317
|
+
loss = fused_kl_div_loss(
|
|
318
|
+
x=x,
|
|
319
|
+
target_x=target_x,
|
|
320
|
+
weight=weight,
|
|
321
|
+
target_weight=target_weight,
|
|
322
|
+
reduction=self.reduction
|
|
323
|
+
)
|
|
324
|
+
return loss
|