liger-kernel-nightly 0.4.0.dev20241107052928__py3-none-any.whl → 0.6.3.dev20251121010306__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.
Potentially problematic release.
This version of liger-kernel-nightly might be problematic. Click here for more details.
- liger_kernel/__init__.py +0 -0
- liger_kernel/chunked_loss/README.md +25 -0
- liger_kernel/chunked_loss/__init__.py +8 -0
- liger_kernel/chunked_loss/cosine_similarity_loss.py +136 -0
- liger_kernel/chunked_loss/cpo_loss.py +157 -0
- liger_kernel/chunked_loss/dpo_loss.py +229 -0
- liger_kernel/chunked_loss/functional.py +17 -0
- liger_kernel/chunked_loss/fused_linear_distillation.py +292 -0
- liger_kernel/chunked_loss/fused_linear_ppo.py +350 -0
- liger_kernel/chunked_loss/fused_linear_preference.py +433 -0
- liger_kernel/chunked_loss/fused_linear_unpaired_preference.py +341 -0
- liger_kernel/chunked_loss/grpo_loss.py +304 -0
- liger_kernel/chunked_loss/jsd_loss.py +200 -0
- liger_kernel/chunked_loss/kto_loss.py +210 -0
- liger_kernel/chunked_loss/orpo_loss.py +144 -0
- liger_kernel/chunked_loss/simpo_loss.py +165 -0
- liger_kernel/env_report.py +21 -4
- liger_kernel/ops/cross_entropy.py +235 -84
- liger_kernel/ops/dyt.py +157 -0
- liger_kernel/ops/experimental/embedding.py +1 -3
- liger_kernel/ops/experimental/mm_int8int2.py +3 -9
- liger_kernel/ops/fused_add_rms_norm.py +412 -0
- liger_kernel/ops/fused_linear_cross_entropy.py +197 -75
- liger_kernel/ops/fused_linear_jsd.py +17 -34
- liger_kernel/ops/fused_neighborhood_attention.py +1022 -0
- liger_kernel/ops/geglu.py +7 -18
- liger_kernel/ops/group_norm.py +305 -0
- liger_kernel/ops/grpo_loss.py +310 -0
- liger_kernel/ops/jsd.py +46 -21
- liger_kernel/ops/kl_div.py +23 -19
- liger_kernel/ops/layer_norm.py +150 -86
- liger_kernel/ops/llama4_rope.py +225 -0
- liger_kernel/ops/multi_token_attention.py +207 -0
- liger_kernel/ops/poly_norm.py +386 -0
- liger_kernel/ops/qwen2vl_mrope.py +222 -0
- liger_kernel/ops/rms_norm.py +314 -84
- liger_kernel/ops/rope.py +32 -34
- liger_kernel/ops/softmax.py +201 -0
- liger_kernel/ops/sparsemax.py +179 -0
- liger_kernel/ops/swiglu.py +5 -9
- liger_kernel/ops/tiled_mlp.py +136 -0
- liger_kernel/ops/tvd.py +207 -0
- liger_kernel/ops/utils.py +8 -4
- liger_kernel/transformers/__init__.py +199 -24
- liger_kernel/transformers/auto_model.py +6 -13
- liger_kernel/transformers/cross_entropy.py +33 -20
- liger_kernel/transformers/dyt.py +22 -0
- liger_kernel/transformers/experimental/__init__.py +5 -0
- liger_kernel/transformers/experimental/embedding.py +1 -3
- liger_kernel/transformers/fsdp.py +55 -0
- liger_kernel/transformers/functional.py +291 -13
- liger_kernel/transformers/fused_add_rms_norm.py +39 -0
- liger_kernel/transformers/fused_linear_cross_entropy.py +43 -14
- liger_kernel/transformers/fused_linear_jsd.py +1 -4
- liger_kernel/transformers/fused_neighborhood_attention.py +234 -0
- liger_kernel/transformers/geglu.py +1 -4
- liger_kernel/transformers/group_norm.py +50 -0
- liger_kernel/transformers/grpo_loss.py +98 -0
- liger_kernel/transformers/jsd.py +2 -7
- liger_kernel/transformers/kl_div.py +1 -3
- liger_kernel/transformers/layer_norm.py +3 -9
- liger_kernel/transformers/llama4_rope.py +93 -0
- liger_kernel/transformers/model/falcon_h1.py +122 -0
- liger_kernel/transformers/model/gemma.py +77 -77
- liger_kernel/transformers/model/gemma2.py +283 -0
- liger_kernel/transformers/model/gemma3.py +331 -0
- liger_kernel/transformers/model/glm4.py +141 -0
- liger_kernel/transformers/model/glm4v.py +163 -0
- liger_kernel/transformers/model/glm4v_moe.py +172 -0
- liger_kernel/transformers/model/internvl.py +157 -0
- liger_kernel/transformers/model/llama.py +128 -79
- liger_kernel/transformers/model/llama4.py +121 -0
- liger_kernel/transformers/model/llava.py +344 -0
- liger_kernel/transformers/model/loss_utils.py +95 -0
- liger_kernel/transformers/model/mistral.py +68 -64
- liger_kernel/transformers/model/mixtral.py +75 -91
- liger_kernel/transformers/model/mllama.py +63 -68
- liger_kernel/transformers/model/olmo2.py +141 -0
- liger_kernel/transformers/model/output_classes.py +147 -0
- liger_kernel/transformers/model/paligemma.py +432 -0
- liger_kernel/transformers/model/phi3.py +59 -213
- liger_kernel/transformers/model/qwen2.py +75 -72
- liger_kernel/transformers/model/qwen2_5_vl.py +163 -0
- liger_kernel/transformers/model/qwen2_vl.py +78 -98
- liger_kernel/transformers/model/qwen3.py +136 -0
- liger_kernel/transformers/model/qwen3_moe.py +152 -0
- liger_kernel/transformers/model/qwen3_next.py +146 -0
- liger_kernel/transformers/model/qwen3_vl.py +150 -0
- liger_kernel/transformers/model/qwen3_vl_moe.py +126 -0
- liger_kernel/transformers/model/smollm3.py +199 -0
- liger_kernel/transformers/model/smolvlm.py +158 -0
- liger_kernel/transformers/monkey_patch.py +2106 -289
- liger_kernel/transformers/multi_token_attention.py +64 -0
- liger_kernel/transformers/poly_norm.py +42 -0
- liger_kernel/transformers/qwen2vl_mrope.py +20 -0
- liger_kernel/transformers/rms_norm.py +57 -6
- liger_kernel/transformers/rope.py +45 -2
- liger_kernel/transformers/softmax.py +12 -0
- liger_kernel/transformers/sparsemax.py +16 -0
- liger_kernel/transformers/swiglu.py +23 -8
- liger_kernel/transformers/tiled_mlp.py +133 -0
- liger_kernel/transformers/trainer/__init__.py +4 -0
- liger_kernel/transformers/trainer/orpo_trainer.py +130 -0
- liger_kernel/transformers/tvd.py +13 -0
- liger_kernel/triton/__init__.py +1 -3
- liger_kernel/triton/monkey_patch.py +1 -3
- liger_kernel/utils.py +71 -0
- {liger_kernel_nightly-0.4.0.dev20241107052928.dist-info → liger_kernel_nightly-0.6.3.dev20251121010306.dist-info}/METADATA +150 -137
- liger_kernel_nightly-0.6.3.dev20251121010306.dist-info/RECORD +116 -0
- {liger_kernel_nightly-0.4.0.dev20241107052928.dist-info → liger_kernel_nightly-0.6.3.dev20251121010306.dist-info}/WHEEL +1 -1
- liger_kernel_nightly-0.4.0.dev20241107052928.dist-info/RECORD +0 -48
- {liger_kernel_nightly-0.4.0.dev20241107052928.dist-info → liger_kernel_nightly-0.6.3.dev20251121010306.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.4.0.dev20241107052928.dist-info → liger_kernel_nightly-0.6.3.dev20251121010306.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.4.0.dev20241107052928.dist-info → liger_kernel_nightly-0.6.3.dev20251121010306.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from typing import Callable
|
|
3
|
+
|
|
4
|
+
from torch.distributed.fsdp import FullyShardedDataParallel
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class _FSDPForwardRedirection:
|
|
8
|
+
"""
|
|
9
|
+
Modified based on
|
|
10
|
+
https://github.com/Lightning-AI/pytorch-lightning/blob/d3f9c83d6efa4f1def36aa6c199600946cdb9117/src/lightning/pytorch/strategies/strategy.py#L601-L648
|
|
11
|
+
Redirect a method call through FullyShardedDataParallel.forward so that the FSDP module's root pre-forward and
|
|
12
|
+
post-forward can be properly executed around the method call.
|
|
13
|
+
This is needed in cases where we call a submodule of a FSDP module. For instance, when we want to call only
|
|
14
|
+
the `LlamaModel` part out of a FSDP-wrapped `LlamaForCausalLM` to get the hidden states without involving
|
|
15
|
+
GPU-memory-heavy `lm_head` and cross entropy computation, doing this directly (i.e. `model.model.forward()`)
|
|
16
|
+
will not work because the first `nn.Embedding` layer is not independently wrapped as a FSDP module (because of
|
|
17
|
+
the transformer-based wrapping policy), and not calling it through FSDP root module forward will not all-gather
|
|
18
|
+
its parameter, thus resulting in "RuntimeError: 'weight' must be 2-D" error. Similarly, if we want to call just
|
|
19
|
+
the `lm_head` part of a model, we need this trick too to properly get its params all-gathered.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __call__(
|
|
23
|
+
self,
|
|
24
|
+
wrapper_module: FullyShardedDataParallel,
|
|
25
|
+
method: Callable,
|
|
26
|
+
*args: Any,
|
|
27
|
+
**kwargs: Any,
|
|
28
|
+
):
|
|
29
|
+
"""Reroutes a method call through the `wrapper_module`'s `forward` method.
|
|
30
|
+
Args:
|
|
31
|
+
wrapper_module: The module that has `original_module` wrapped.
|
|
32
|
+
original_module: The module that was wrapped inside `wrapper_module`.
|
|
33
|
+
method_name: The name of the method that should be called on the `original_module` after inputs get
|
|
34
|
+
redirected through the `wrapper_module`'s `forward` method.
|
|
35
|
+
*args: The positional arguments to the method `method_name`. They will get passed to a patched
|
|
36
|
+
`forward` method instead.
|
|
37
|
+
**kwargs: The keyword arguments to the method `method_name`. They will get passed to a patched
|
|
38
|
+
`forward` method instead.
|
|
39
|
+
"""
|
|
40
|
+
assert isinstance(wrapper_module, FullyShardedDataParallel)
|
|
41
|
+
original_module = wrapper_module._fsdp_wrapped_module
|
|
42
|
+
original_forward = original_module.forward
|
|
43
|
+
|
|
44
|
+
def wrapped_forward(*_args: Any, **_kwargs: Any) -> Any:
|
|
45
|
+
# Unpatch ourselves immediately before calling the method `method_name`
|
|
46
|
+
# because itself may want to call the real `forward`
|
|
47
|
+
original_module.forward = original_forward # type: ignore[method-assign]
|
|
48
|
+
# Call the actual method e.g. `.training_step(...)`
|
|
49
|
+
out = method(*_args, **_kwargs)
|
|
50
|
+
return out
|
|
51
|
+
|
|
52
|
+
# Patch the original_module's forward so we can redirect the arguments back to the real method
|
|
53
|
+
original_module.forward = wrapped_forward # type: ignore[method-assign]
|
|
54
|
+
wrapper_output = wrapper_module(*args, **kwargs)
|
|
55
|
+
return wrapper_output
|
|
@@ -1,23 +1,301 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import torch
|
|
5
|
+
|
|
1
6
|
from liger_kernel.ops.cross_entropy import LigerCrossEntropyFunction
|
|
2
|
-
from liger_kernel.ops.
|
|
3
|
-
|
|
4
|
-
|
|
7
|
+
from liger_kernel.ops.dyt import LigerDyTFunction
|
|
8
|
+
from liger_kernel.ops.fused_add_rms_norm import LigerFusedAddRMSNormFunction
|
|
9
|
+
from liger_kernel.ops.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyFunction
|
|
5
10
|
from liger_kernel.ops.fused_linear_jsd import LigerFusedLinearJSDFunction
|
|
11
|
+
from liger_kernel.ops.fused_neighborhood_attention import LigerFusedNeighborhoodAttentionFunction
|
|
6
12
|
from liger_kernel.ops.geglu import LigerGELUMulFunction
|
|
13
|
+
from liger_kernel.ops.group_norm import LigerGroupNormFunction
|
|
7
14
|
from liger_kernel.ops.jsd import LigerJSDFunction
|
|
8
15
|
from liger_kernel.ops.kl_div import LigerKLDivLossFunction
|
|
9
16
|
from liger_kernel.ops.layer_norm import LigerLayerNormFunction
|
|
17
|
+
from liger_kernel.ops.multi_token_attention import LigerMultiTokenAttentionFunction
|
|
18
|
+
from liger_kernel.ops.poly_norm import LigerPolyNormFunction
|
|
19
|
+
from liger_kernel.ops.qwen2vl_mrope import LigerQwen2VLMRopeFunction
|
|
10
20
|
from liger_kernel.ops.rms_norm import LigerRMSNormFunction
|
|
11
21
|
from liger_kernel.ops.rope import LigerRopeFunction
|
|
22
|
+
from liger_kernel.ops.softmax import LigerSoftmaxFunction
|
|
23
|
+
from liger_kernel.ops.sparsemax import LigerSparsemaxFunction
|
|
12
24
|
from liger_kernel.ops.swiglu import LigerSiLUMulFunction
|
|
25
|
+
from liger_kernel.ops.tvd import LigerTVDLossFunction
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass
|
|
29
|
+
class CrossEntropyOutput:
|
|
30
|
+
loss: torch.Tensor
|
|
31
|
+
z_loss: Optional[torch.Tensor] = None
|
|
32
|
+
token_accuracy: Optional[torch.Tensor] = None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# conform to the function signature in https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html
|
|
36
|
+
# `weight` and `size_average` are placeholders and not implemented yet
|
|
37
|
+
def liger_cross_entropy(
|
|
38
|
+
input,
|
|
39
|
+
target,
|
|
40
|
+
weight=None,
|
|
41
|
+
size_average=None,
|
|
42
|
+
ignore_index: int = -100,
|
|
43
|
+
reduce=None,
|
|
44
|
+
reduction: str = "mean",
|
|
45
|
+
label_smoothing: float = 0.0,
|
|
46
|
+
lse_square_scale: float = 0.0,
|
|
47
|
+
softcap: Optional[float] = None,
|
|
48
|
+
return_z_loss: bool = False,
|
|
49
|
+
return_token_accuracy: bool = False,
|
|
50
|
+
):
|
|
51
|
+
loss, z_loss, token_accuracy = LigerCrossEntropyFunction.apply(
|
|
52
|
+
input,
|
|
53
|
+
target,
|
|
54
|
+
weight,
|
|
55
|
+
ignore_index,
|
|
56
|
+
lse_square_scale,
|
|
57
|
+
label_smoothing,
|
|
58
|
+
reduction,
|
|
59
|
+
softcap,
|
|
60
|
+
return_z_loss,
|
|
61
|
+
return_token_accuracy,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
if not return_z_loss and not return_token_accuracy:
|
|
65
|
+
return loss
|
|
66
|
+
|
|
67
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def liger_fused_linear_cross_entropy(
|
|
71
|
+
input,
|
|
72
|
+
weight,
|
|
73
|
+
target,
|
|
74
|
+
bias=None,
|
|
75
|
+
ce_weight=None,
|
|
76
|
+
ignore_index: int = -100,
|
|
77
|
+
lse_square_scale: float = 0.0,
|
|
78
|
+
label_smoothing: float = 0.0,
|
|
79
|
+
reduction: str = "mean",
|
|
80
|
+
softcap: Optional[float] = None,
|
|
81
|
+
return_z_loss: bool = False,
|
|
82
|
+
accum_dtype=None,
|
|
83
|
+
use_token_scaling: bool = False,
|
|
84
|
+
return_token_accuracy: bool = False,
|
|
85
|
+
):
|
|
86
|
+
loss, z_loss, token_accuracy = LigerFusedLinearCrossEntropyFunction.apply(
|
|
87
|
+
input,
|
|
88
|
+
weight,
|
|
89
|
+
target,
|
|
90
|
+
bias,
|
|
91
|
+
ce_weight,
|
|
92
|
+
ignore_index,
|
|
93
|
+
lse_square_scale,
|
|
94
|
+
label_smoothing,
|
|
95
|
+
reduction,
|
|
96
|
+
softcap,
|
|
97
|
+
return_z_loss,
|
|
98
|
+
accum_dtype,
|
|
99
|
+
use_token_scaling,
|
|
100
|
+
return_token_accuracy,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
if not return_z_loss and not return_token_accuracy:
|
|
104
|
+
return loss
|
|
105
|
+
|
|
106
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def liger_fused_linear_jsd(
|
|
110
|
+
student_input,
|
|
111
|
+
student_weight,
|
|
112
|
+
teacher_input,
|
|
113
|
+
teacher_weight,
|
|
114
|
+
shift_labels=None,
|
|
115
|
+
jsd_beta: float = 0.5,
|
|
116
|
+
ignore_index: int = -100,
|
|
117
|
+
temperature: float = 1.0,
|
|
118
|
+
):
|
|
119
|
+
return LigerFusedLinearJSDFunction.apply(
|
|
120
|
+
student_input,
|
|
121
|
+
student_weight,
|
|
122
|
+
teacher_input,
|
|
123
|
+
teacher_weight,
|
|
124
|
+
shift_labels,
|
|
125
|
+
jsd_beta,
|
|
126
|
+
ignore_index,
|
|
127
|
+
temperature,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def liger_geglu(a, b):
|
|
132
|
+
return LigerGELUMulFunction.apply(a, b)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def liger_group_norm(
|
|
136
|
+
X,
|
|
137
|
+
affine_scaling_weight,
|
|
138
|
+
affine_shifting_bias,
|
|
139
|
+
num_channels,
|
|
140
|
+
num_groups,
|
|
141
|
+
eps,
|
|
142
|
+
):
|
|
143
|
+
return LigerGroupNormFunction.apply(
|
|
144
|
+
X,
|
|
145
|
+
affine_scaling_weight,
|
|
146
|
+
affine_shifting_bias,
|
|
147
|
+
num_channels,
|
|
148
|
+
num_groups,
|
|
149
|
+
eps,
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def liger_jsd(
|
|
154
|
+
input,
|
|
155
|
+
target,
|
|
156
|
+
shift_labels=None,
|
|
157
|
+
beta: float = 0.5,
|
|
158
|
+
ignore_index: int = -100,
|
|
159
|
+
):
|
|
160
|
+
return LigerJSDFunction.apply(
|
|
161
|
+
input,
|
|
162
|
+
target,
|
|
163
|
+
shift_labels,
|
|
164
|
+
beta,
|
|
165
|
+
ignore_index,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# conform to the function signature in https://pytorch.org/docs/stable/generated/torch.nn.functional.kl_div.html#torch.nn.functional.kl_div
|
|
170
|
+
# `size_average` and `mean` are being deprecated in torch API and are placeholders here
|
|
171
|
+
def liger_kl_div(
|
|
172
|
+
input,
|
|
173
|
+
target,
|
|
174
|
+
size_average: bool = True,
|
|
175
|
+
reduce: bool = True,
|
|
176
|
+
reduction: str = "mean",
|
|
177
|
+
log_target: bool = False,
|
|
178
|
+
eps: float = 1e-10,
|
|
179
|
+
):
|
|
180
|
+
# Note: the default reduction in torch is `mean`, but being `batchmean` in Liger
|
|
181
|
+
return LigerKLDivLossFunction.apply(
|
|
182
|
+
input,
|
|
183
|
+
target,
|
|
184
|
+
reduction,
|
|
185
|
+
log_target,
|
|
186
|
+
eps,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def liger_sparsemax(
|
|
191
|
+
input,
|
|
192
|
+
dim: int = -1,
|
|
193
|
+
):
|
|
194
|
+
return LigerSparsemaxFunction.apply(input, dim)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def liger_multi_token_attention(
|
|
198
|
+
scores,
|
|
199
|
+
weight,
|
|
200
|
+
bias=None,
|
|
201
|
+
stride: int = 1,
|
|
202
|
+
padding: int = 0,
|
|
203
|
+
dilation: int = 1,
|
|
204
|
+
groups: int = 1,
|
|
205
|
+
sparse: bool = False,
|
|
206
|
+
):
|
|
207
|
+
"""
|
|
208
|
+
Functional interface for multi-token attention.
|
|
209
|
+
|
|
210
|
+
Args:
|
|
211
|
+
scores: Input tensor of shape (B, C_in, L, L)
|
|
212
|
+
weight: Convolution weight tensor of shape (C_out, C_in // groups, K, K)
|
|
213
|
+
bias: Optional bias tensor of shape (C_out,)
|
|
214
|
+
stride: Stride for the convolution (default: 1)
|
|
215
|
+
padding: Padding for the convolution (default: 0)
|
|
216
|
+
dilation: Dilation factor for the convolution (default: 1)
|
|
217
|
+
groups: Number of groups for the convolution (default: 1)
|
|
218
|
+
sparse: Specifies if input tensors are expected to be sparse (default: False)
|
|
219
|
+
Returns:
|
|
220
|
+
Output tensor after applying multi-token attention.
|
|
221
|
+
"""
|
|
222
|
+
return LigerMultiTokenAttentionFunction.apply(scores, weight, bias, stride, padding, dilation, groups, sparse)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def liger_fused_neighborhood_attention(
|
|
226
|
+
query,
|
|
227
|
+
key,
|
|
228
|
+
value,
|
|
229
|
+
kernel_size: int = 7,
|
|
230
|
+
dilation: int = 1,
|
|
231
|
+
scale: float = None,
|
|
232
|
+
):
|
|
233
|
+
"""
|
|
234
|
+
Liger fused neighborhood attention.
|
|
235
|
+
|
|
236
|
+
paper: https://arxiv.org/pdf/2504.16922
|
|
237
|
+
|
|
238
|
+
Args:
|
|
239
|
+
query: Query tensor of shape [batch_size, num_heads, seq_len, head_dim]
|
|
240
|
+
key: Key tensor of shape [batch_size, num_heads, seq_len, head_dim]
|
|
241
|
+
value: Value tensor of shape [batch_size, num_heads, seq_len, head_dim]
|
|
242
|
+
kernel_size: Size of the neighborhood window (default: 7)
|
|
243
|
+
dilation: Dilation factor for the neighborhood (default: 1)
|
|
244
|
+
scale: Scaling factor for attention scores (default: rsqrt(head_dim))
|
|
245
|
+
|
|
246
|
+
Returns:
|
|
247
|
+
Output tensor of shape [batch_size, num_heads, seq_len, head_dim]
|
|
248
|
+
"""
|
|
249
|
+
return LigerFusedNeighborhoodAttentionFunction.apply(query, key, value, kernel_size, dilation, scale)
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def liger_tvd(
|
|
253
|
+
input,
|
|
254
|
+
target,
|
|
255
|
+
shift_labels=None,
|
|
256
|
+
reduction: str = "mean",
|
|
257
|
+
ignore_index: int = -100,
|
|
258
|
+
):
|
|
259
|
+
return LigerTVDLossFunction.apply(
|
|
260
|
+
input,
|
|
261
|
+
target,
|
|
262
|
+
shift_labels,
|
|
263
|
+
reduction,
|
|
264
|
+
ignore_index,
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def liger_layer_norm(X, W, B, eps):
|
|
269
|
+
return LigerLayerNormFunction.apply(X, W, B, eps)
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def liger_qwen2vl_mrope(q, k, cos, sin, mrope_section, unsqueeze_dim=1):
|
|
273
|
+
return LigerQwen2VLMRopeFunction.apply(q, k, cos, sin, mrope_section, unsqueeze_dim)
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def liger_rms_norm(X, W, eps, offset: float = 0.0, casting_mode: str = "llama", in_place: bool = True):
|
|
277
|
+
return LigerRMSNormFunction.apply(X, W, eps, offset, casting_mode, in_place)
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def liger_poly_norm(X, W, B, eps=1e-6, in_place=True):
|
|
281
|
+
return LigerPolyNormFunction.apply(X, W, B, eps, in_place)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def liger_fused_add_rms_norm(X, R, W, eps, offset: float = 0.0, casting_mode: str = "llama", in_place: bool = True):
|
|
285
|
+
return LigerFusedAddRMSNormFunction.apply(X, R, W, eps, offset, casting_mode, in_place)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def liger_rope(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
|
|
289
|
+
return LigerRopeFunction.apply(q, k, cos, sin, position_ids, unsqueeze_dim)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def liger_swiglu(a, b):
|
|
293
|
+
return LigerSiLUMulFunction.apply(a, b)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def liger_softmax(x):
|
|
297
|
+
return LigerSoftmaxFunction.apply(x)
|
|
298
|
+
|
|
13
299
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
liger_fused_linear_cross_entropy = LigerFusedLinearCrossEntropyFunction.apply
|
|
17
|
-
liger_geglu = LigerGELUMulFunction.apply
|
|
18
|
-
liger_rms_norm = LigerRMSNormFunction.apply
|
|
19
|
-
liger_rope = LigerRopeFunction.apply
|
|
20
|
-
liger_layer_norm = LigerLayerNormFunction.apply
|
|
21
|
-
liger_kl_div = LigerKLDivLossFunction.apply
|
|
22
|
-
liger_jsd = LigerJSDFunction.apply
|
|
23
|
-
liger_fused_linear_jsd = LigerFusedLinearJSDFunction.apply
|
|
300
|
+
def liger_dyt(x, alpha, gamma, beta):
|
|
301
|
+
return LigerDyTFunction.apply(x, alpha, gamma, beta)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
|
|
4
|
+
from liger_kernel.ops.fused_add_rms_norm import LigerFusedAddRMSNormFunction
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class LigerFusedAddRMSNorm(nn.Module):
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
hidden_size,
|
|
11
|
+
eps=1e-6,
|
|
12
|
+
offset=0.0,
|
|
13
|
+
casting_mode="llama",
|
|
14
|
+
init_fn="ones",
|
|
15
|
+
in_place=False,
|
|
16
|
+
):
|
|
17
|
+
super().__init__()
|
|
18
|
+
assert init_fn in [
|
|
19
|
+
"ones",
|
|
20
|
+
"zeros",
|
|
21
|
+
], f"init_fn must be either 'ones' or 'zeros', got {init_fn}"
|
|
22
|
+
self.weight = nn.Parameter(torch.ones(hidden_size) if init_fn == "ones" else torch.zeros(hidden_size))
|
|
23
|
+
self.variance_epsilon, self.offset, self.casting_mode, self.in_place = (eps, offset, casting_mode, in_place)
|
|
24
|
+
|
|
25
|
+
def forward(self, hidden_states, residual):
|
|
26
|
+
return LigerFusedAddRMSNormFunction.apply(
|
|
27
|
+
hidden_states,
|
|
28
|
+
residual,
|
|
29
|
+
self.weight,
|
|
30
|
+
self.variance_epsilon,
|
|
31
|
+
self.offset,
|
|
32
|
+
self.casting_mode,
|
|
33
|
+
self.in_place,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
def extra_repr(self):
|
|
37
|
+
return (
|
|
38
|
+
f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}, offset={self.offset}, in_place={self.in_place}"
|
|
39
|
+
)
|
|
@@ -1,35 +1,64 @@
|
|
|
1
|
-
|
|
1
|
+
from typing import Optional
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
LigerFusedLinearCrossEntropyFunction,
|
|
5
|
-
)
|
|
3
|
+
import torch
|
|
6
4
|
|
|
5
|
+
from liger_kernel.ops.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyFunction
|
|
6
|
+
from liger_kernel.transformers.functional import CrossEntropyOutput
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
|
|
9
|
+
class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
9
10
|
def __init__(
|
|
10
11
|
self,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
ce_weight: Optional[torch.FloatTensor] = None,
|
|
13
|
+
ignore_index: int = -100,
|
|
14
|
+
lse_square_scale: float = 0.0,
|
|
15
|
+
label_smoothing: float = 0.0,
|
|
16
|
+
reduction: str = "mean",
|
|
17
|
+
softcap: Optional[float] = None,
|
|
18
|
+
return_z_loss: bool = False,
|
|
19
|
+
accum_dtype: Optional[torch.dtype] = None,
|
|
20
|
+
use_token_scaling: bool = False,
|
|
21
|
+
return_token_accuracy: bool = False,
|
|
15
22
|
):
|
|
16
23
|
super().__init__()
|
|
24
|
+
assert (label_smoothing >= 0) and (label_smoothing <= 1), (
|
|
25
|
+
f"label_smoothing must be between 0.0 and 1.0. Got: {label_smoothing}"
|
|
26
|
+
)
|
|
27
|
+
assert reduction in {
|
|
28
|
+
"mean",
|
|
29
|
+
"sum",
|
|
30
|
+
"none",
|
|
31
|
+
}, f"reduction must be 'mean' or 'sum' or 'none'. Got: {reduction}"
|
|
32
|
+
assert softcap is None or softcap > 0, f"softcap must greater than 0.0 or None. Got: {softcap}"
|
|
33
|
+
self.ce_weight = ce_weight
|
|
17
34
|
self.ignore_index = ignore_index
|
|
35
|
+
self.lse_square_scale = lse_square_scale
|
|
18
36
|
self.label_smoothing = label_smoothing
|
|
19
37
|
self.reduction = reduction
|
|
20
|
-
self.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
38
|
+
self.softcap = softcap
|
|
39
|
+
self.return_z_loss = return_z_loss
|
|
40
|
+
self.accum_dtype = accum_dtype
|
|
41
|
+
self.use_token_scaling = use_token_scaling
|
|
42
|
+
self.return_token_accuracy = return_token_accuracy
|
|
24
43
|
|
|
25
44
|
def forward(self, lin_weight, _input, target, bias=None):
|
|
26
|
-
|
|
45
|
+
loss, z_loss, token_accuracy = LigerFusedLinearCrossEntropyFunction.apply(
|
|
27
46
|
_input,
|
|
28
47
|
lin_weight,
|
|
29
48
|
target,
|
|
30
49
|
bias,
|
|
50
|
+
self.ce_weight,
|
|
31
51
|
self.ignore_index,
|
|
32
52
|
self.lse_square_scale,
|
|
33
53
|
self.label_smoothing,
|
|
34
54
|
self.reduction,
|
|
55
|
+
self.softcap,
|
|
56
|
+
self.return_z_loss,
|
|
57
|
+
self.accum_dtype,
|
|
58
|
+
self.use_token_scaling,
|
|
59
|
+
self.return_token_accuracy,
|
|
35
60
|
)
|
|
61
|
+
if not self.return_z_loss and not self.return_token_accuracy:
|
|
62
|
+
return loss
|
|
63
|
+
|
|
64
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
@@ -12,7 +12,7 @@ class LigerFusedLinearJSD(torch.nn.Module):
|
|
|
12
12
|
the materialization of the large logits tensor.
|
|
13
13
|
|
|
14
14
|
Args:
|
|
15
|
-
jsd_beta (float): coefficient beta of generalized JSD in the
|
|
15
|
+
jsd_beta (float): coefficient beta of generalized JSD in the interval [0, 1]. It implements forward/reverse KL when beta equals 0 and 1 respectively. Default: `0.5`
|
|
16
16
|
ignore_index (int): The index to ignore in the target. Default: `-100`
|
|
17
17
|
temperature (float): temperature in softmax function to control the output probability distribution. Default: `1.0`
|
|
18
18
|
|
|
@@ -70,9 +70,6 @@ class LigerFusedLinearJSD(torch.nn.Module):
|
|
|
70
70
|
|
|
71
71
|
def __init__(self, jsd_beta=0.5, ignore_index=-100, temperature=1.0):
|
|
72
72
|
super().__init__()
|
|
73
|
-
assert (
|
|
74
|
-
jsd_beta > 0 and jsd_beta < 1
|
|
75
|
-
), f"beta must be greater than 0 and less than 1. Got: {jsd_beta}"
|
|
76
73
|
assert temperature != 0, "temperature cannot be 0."
|
|
77
74
|
self.jsd_beta = jsd_beta
|
|
78
75
|
self.temperature = temperature
|