liger-kernel-nightly 0.6.2.dev20251011154427__py3-none-any.whl → 0.6.4.dev20251202054858__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/chunked_loss/cosine_similarity_loss.py +13 -4
- liger_kernel/chunked_loss/fused_linear_distillation.py +13 -2
- liger_kernel/chunked_loss/fused_linear_ppo.py +21 -5
- liger_kernel/chunked_loss/grpo_loss.py +8 -5
- liger_kernel/chunked_loss/jsd_loss.py +18 -5
- liger_kernel/ops/cross_entropy.py +65 -11
- liger_kernel/ops/dyt.py +5 -2
- liger_kernel/ops/fused_add_rms_norm.py +5 -1
- liger_kernel/ops/fused_linear_cross_entropy.py +43 -13
- liger_kernel/ops/geglu.py +2 -1
- liger_kernel/ops/group_norm.py +2 -1
- liger_kernel/ops/grpo_loss.py +3 -1
- liger_kernel/ops/layer_norm.py +86 -66
- liger_kernel/ops/poly_norm.py +390 -0
- liger_kernel/ops/rms_norm.py +7 -2
- liger_kernel/ops/tiled_mlp.py +136 -0
- liger_kernel/ops/utils.py +2 -0
- liger_kernel/transformers/__init__.py +27 -0
- liger_kernel/transformers/cross_entropy.py +8 -3
- liger_kernel/transformers/functional.py +29 -6
- liger_kernel/transformers/fused_linear_cross_entropy.py +8 -3
- liger_kernel/transformers/grpo_loss.py +56 -1
- liger_kernel/transformers/model/falcon_h1.py +19 -5
- liger_kernel/transformers/model/gemma.py +17 -6
- liger_kernel/transformers/model/gemma2.py +14 -5
- liger_kernel/transformers/model/gemma3.py +25 -12
- liger_kernel/transformers/model/glm4.py +16 -4
- liger_kernel/transformers/model/glm4v.py +16 -4
- liger_kernel/transformers/model/glm4v_moe.py +23 -4
- liger_kernel/transformers/model/hunyuan_v1.py +134 -0
- liger_kernel/transformers/model/internvl.py +12 -5
- liger_kernel/transformers/model/llama.py +14 -5
- liger_kernel/transformers/model/llama4.py +16 -4
- liger_kernel/transformers/model/llava.py +12 -4
- liger_kernel/transformers/model/loss_utils.py +31 -3
- liger_kernel/transformers/model/mistral.py +15 -6
- liger_kernel/transformers/model/mixtral.py +16 -7
- liger_kernel/transformers/model/mllama.py +12 -4
- liger_kernel/transformers/model/olmo2.py +16 -4
- liger_kernel/transformers/model/olmo3.py +142 -0
- liger_kernel/transformers/model/output_classes.py +147 -0
- liger_kernel/transformers/model/paligemma.py +22 -5
- liger_kernel/transformers/model/phi3.py +14 -7
- liger_kernel/transformers/model/qwen2.py +16 -3
- liger_kernel/transformers/model/qwen2_5_vl.py +14 -6
- liger_kernel/transformers/model/qwen2_vl.py +16 -4
- liger_kernel/transformers/model/qwen3.py +20 -5
- liger_kernel/transformers/model/qwen3_moe.py +19 -5
- 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 +15 -6
- liger_kernel/transformers/model/smolvlm.py +158 -0
- liger_kernel/transformers/monkey_patch.py +594 -19
- liger_kernel/transformers/poly_norm.py +42 -0
- liger_kernel/transformers/rms_norm.py +7 -0
- liger_kernel/transformers/rope.py +43 -0
- liger_kernel/transformers/swiglu.py +17 -0
- liger_kernel/transformers/tiled_mlp.py +133 -0
- liger_kernel/utils.py +25 -0
- {liger_kernel_nightly-0.6.2.dev20251011154427.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/METADATA +4 -1
- liger_kernel_nightly-0.6.4.dev20251202054858.dist-info/RECORD +118 -0
- liger_kernel_nightly-0.6.2.dev20251011154427.dist-info/RECORD +0 -107
- {liger_kernel_nightly-0.6.2.dev20251011154427.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/LICENSE +0 -0
- {liger_kernel_nightly-0.6.2.dev20251011154427.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/NOTICE +0 -0
- {liger_kernel_nightly-0.6.2.dev20251011154427.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/WHEEL +0 -0
- {liger_kernel_nightly-0.6.2.dev20251011154427.dist-info → liger_kernel_nightly-0.6.4.dev20251202054858.dist-info}/top_level.txt +0 -0
|
@@ -3,6 +3,7 @@ from typing import Optional
|
|
|
3
3
|
import torch
|
|
4
4
|
|
|
5
5
|
from liger_kernel.ops.cross_entropy import LigerCrossEntropyFunction
|
|
6
|
+
from liger_kernel.transformers.functional import CrossEntropyOutput
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class LigerCrossEntropyLoss(torch.nn.Module):
|
|
@@ -15,6 +16,7 @@ class LigerCrossEntropyLoss(torch.nn.Module):
|
|
|
15
16
|
reduction: str = "mean",
|
|
16
17
|
softcap: Optional[float] = None,
|
|
17
18
|
return_z_loss: bool = False,
|
|
19
|
+
return_token_accuracy: bool = False,
|
|
18
20
|
):
|
|
19
21
|
super().__init__()
|
|
20
22
|
assert (label_smoothing >= 0) and (label_smoothing <= 1), (
|
|
@@ -33,9 +35,10 @@ class LigerCrossEntropyLoss(torch.nn.Module):
|
|
|
33
35
|
self.reduction = reduction
|
|
34
36
|
self.softcap = softcap
|
|
35
37
|
self.return_z_loss = return_z_loss
|
|
38
|
+
self.return_token_accuracy = return_token_accuracy
|
|
36
39
|
|
|
37
40
|
def forward(self, _input: torch.Tensor, target: torch.Tensor):
|
|
38
|
-
loss, z_loss = LigerCrossEntropyFunction.apply(
|
|
41
|
+
loss, z_loss, token_accuracy = LigerCrossEntropyFunction.apply(
|
|
39
42
|
_input,
|
|
40
43
|
target,
|
|
41
44
|
self.weight,
|
|
@@ -45,7 +48,9 @@ class LigerCrossEntropyLoss(torch.nn.Module):
|
|
|
45
48
|
self.reduction,
|
|
46
49
|
self.softcap,
|
|
47
50
|
self.return_z_loss,
|
|
51
|
+
self.return_token_accuracy,
|
|
48
52
|
)
|
|
49
|
-
if not self.return_z_loss:
|
|
53
|
+
if not self.return_z_loss and not self.return_token_accuracy:
|
|
50
54
|
return loss
|
|
51
|
-
|
|
55
|
+
|
|
56
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
1
2
|
from typing import Optional
|
|
2
3
|
|
|
4
|
+
import torch
|
|
5
|
+
|
|
3
6
|
from liger_kernel.ops.cross_entropy import LigerCrossEntropyFunction
|
|
4
7
|
from liger_kernel.ops.dyt import LigerDyTFunction
|
|
5
8
|
from liger_kernel.ops.fused_add_rms_norm import LigerFusedAddRMSNormFunction
|
|
@@ -12,6 +15,7 @@ from liger_kernel.ops.jsd import LigerJSDFunction
|
|
|
12
15
|
from liger_kernel.ops.kl_div import LigerKLDivLossFunction
|
|
13
16
|
from liger_kernel.ops.layer_norm import LigerLayerNormFunction
|
|
14
17
|
from liger_kernel.ops.multi_token_attention import LigerMultiTokenAttentionFunction
|
|
18
|
+
from liger_kernel.ops.poly_norm import LigerPolyNormFunction
|
|
15
19
|
from liger_kernel.ops.qwen2vl_mrope import LigerQwen2VLMRopeFunction
|
|
16
20
|
from liger_kernel.ops.rms_norm import LigerRMSNormFunction
|
|
17
21
|
from liger_kernel.ops.rope import LigerRopeFunction
|
|
@@ -21,6 +25,13 @@ from liger_kernel.ops.swiglu import LigerSiLUMulFunction
|
|
|
21
25
|
from liger_kernel.ops.tvd import LigerTVDLossFunction
|
|
22
26
|
|
|
23
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
|
+
|
|
24
35
|
# conform to the function signature in https://pytorch.org/docs/stable/generated/torch.nn.functional.cross_entropy.html
|
|
25
36
|
# `weight` and `size_average` are placeholders and not implemented yet
|
|
26
37
|
def liger_cross_entropy(
|
|
@@ -35,8 +46,9 @@ def liger_cross_entropy(
|
|
|
35
46
|
lse_square_scale: float = 0.0,
|
|
36
47
|
softcap: Optional[float] = None,
|
|
37
48
|
return_z_loss: bool = False,
|
|
49
|
+
return_token_accuracy: bool = False,
|
|
38
50
|
):
|
|
39
|
-
loss, z_loss = LigerCrossEntropyFunction.apply(
|
|
51
|
+
loss, z_loss, token_accuracy = LigerCrossEntropyFunction.apply(
|
|
40
52
|
input,
|
|
41
53
|
target,
|
|
42
54
|
weight,
|
|
@@ -46,10 +58,13 @@ def liger_cross_entropy(
|
|
|
46
58
|
reduction,
|
|
47
59
|
softcap,
|
|
48
60
|
return_z_loss,
|
|
61
|
+
return_token_accuracy,
|
|
49
62
|
)
|
|
50
|
-
|
|
63
|
+
|
|
64
|
+
if not return_z_loss and not return_token_accuracy:
|
|
51
65
|
return loss
|
|
52
|
-
|
|
66
|
+
|
|
67
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
53
68
|
|
|
54
69
|
|
|
55
70
|
def liger_fused_linear_cross_entropy(
|
|
@@ -66,8 +81,9 @@ def liger_fused_linear_cross_entropy(
|
|
|
66
81
|
return_z_loss: bool = False,
|
|
67
82
|
accum_dtype=None,
|
|
68
83
|
use_token_scaling: bool = False,
|
|
84
|
+
return_token_accuracy: bool = False,
|
|
69
85
|
):
|
|
70
|
-
loss, z_loss = LigerFusedLinearCrossEntropyFunction.apply(
|
|
86
|
+
loss, z_loss, token_accuracy = LigerFusedLinearCrossEntropyFunction.apply(
|
|
71
87
|
input,
|
|
72
88
|
weight,
|
|
73
89
|
target,
|
|
@@ -81,10 +97,13 @@ def liger_fused_linear_cross_entropy(
|
|
|
81
97
|
return_z_loss,
|
|
82
98
|
accum_dtype,
|
|
83
99
|
use_token_scaling,
|
|
100
|
+
return_token_accuracy,
|
|
84
101
|
)
|
|
85
|
-
|
|
102
|
+
|
|
103
|
+
if not return_z_loss and not return_token_accuracy:
|
|
86
104
|
return loss
|
|
87
|
-
|
|
105
|
+
|
|
106
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
88
107
|
|
|
89
108
|
|
|
90
109
|
def liger_fused_linear_jsd(
|
|
@@ -258,6 +277,10 @@ def liger_rms_norm(X, W, eps, offset: float = 0.0, casting_mode: str = "llama",
|
|
|
258
277
|
return LigerRMSNormFunction.apply(X, W, eps, offset, casting_mode, in_place)
|
|
259
278
|
|
|
260
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
|
+
|
|
261
284
|
def liger_fused_add_rms_norm(X, R, W, eps, offset: float = 0.0, casting_mode: str = "llama", in_place: bool = True):
|
|
262
285
|
return LigerFusedAddRMSNormFunction.apply(X, R, W, eps, offset, casting_mode, in_place)
|
|
263
286
|
|
|
@@ -3,6 +3,7 @@ from typing import Optional
|
|
|
3
3
|
import torch
|
|
4
4
|
|
|
5
5
|
from liger_kernel.ops.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyFunction
|
|
6
|
+
from liger_kernel.transformers.functional import CrossEntropyOutput
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
@@ -17,6 +18,7 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
|
17
18
|
return_z_loss: bool = False,
|
|
18
19
|
accum_dtype: Optional[torch.dtype] = None,
|
|
19
20
|
use_token_scaling: bool = False,
|
|
21
|
+
return_token_accuracy: bool = False,
|
|
20
22
|
):
|
|
21
23
|
super().__init__()
|
|
22
24
|
assert (label_smoothing >= 0) and (label_smoothing <= 1), (
|
|
@@ -37,9 +39,10 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
|
37
39
|
self.return_z_loss = return_z_loss
|
|
38
40
|
self.accum_dtype = accum_dtype
|
|
39
41
|
self.use_token_scaling = use_token_scaling
|
|
42
|
+
self.return_token_accuracy = return_token_accuracy
|
|
40
43
|
|
|
41
44
|
def forward(self, lin_weight, _input, target, bias=None):
|
|
42
|
-
loss, z_loss = LigerFusedLinearCrossEntropyFunction.apply(
|
|
45
|
+
loss, z_loss, token_accuracy = LigerFusedLinearCrossEntropyFunction.apply(
|
|
43
46
|
_input,
|
|
44
47
|
lin_weight,
|
|
45
48
|
target,
|
|
@@ -53,7 +56,9 @@ class LigerFusedLinearCrossEntropyLoss(torch.nn.Module):
|
|
|
53
56
|
self.return_z_loss,
|
|
54
57
|
self.accum_dtype,
|
|
55
58
|
self.use_token_scaling,
|
|
59
|
+
self.return_token_accuracy,
|
|
56
60
|
)
|
|
57
|
-
if not self.return_z_loss:
|
|
61
|
+
if not self.return_z_loss and not self.return_token_accuracy:
|
|
58
62
|
return loss
|
|
59
|
-
|
|
63
|
+
|
|
64
|
+
return CrossEntropyOutput(loss=loss, z_loss=z_loss, token_accuracy=token_accuracy)
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
|
|
3
|
+
from liger_kernel.chunked_loss.fused_linear_ppo import LigerFusedLinearPPOBase
|
|
1
4
|
from liger_kernel.ops.grpo_loss import GrpoLossFunction
|
|
2
5
|
|
|
3
6
|
|
|
@@ -13,12 +16,20 @@ def triton_grpo_loss(
|
|
|
13
16
|
eps_low=0.2,
|
|
14
17
|
eps_high=0.4,
|
|
15
18
|
inplace=True,
|
|
19
|
+
loss_type="dapo",
|
|
20
|
+
max_completion_length=None,
|
|
21
|
+
importance_sampling_level="token",
|
|
22
|
+
reduce=False,
|
|
16
23
|
):
|
|
17
24
|
assert logits is not None and completion_ids is not None and advantages is not None, (
|
|
18
25
|
"must provide logits、completion_ids and advantages"
|
|
19
26
|
)
|
|
27
|
+
if importance_sampling_level != "token":
|
|
28
|
+
raise ValueError(
|
|
29
|
+
f"Triton GRPO loss only supports token-level importance sampling. Got {importance_sampling_level}."
|
|
30
|
+
)
|
|
20
31
|
|
|
21
|
-
|
|
32
|
+
per_token_loss, per_token_kl, is_clipped = GrpoLossFunction.apply(
|
|
22
33
|
logits,
|
|
23
34
|
old_logp,
|
|
24
35
|
ref_logp,
|
|
@@ -31,6 +42,50 @@ def triton_grpo_loss(
|
|
|
31
42
|
eps_high,
|
|
32
43
|
inplace,
|
|
33
44
|
)
|
|
45
|
+
if not reduce:
|
|
46
|
+
return per_token_loss, per_token_kl, is_clipped
|
|
47
|
+
|
|
48
|
+
loss = _reduce_grpo_loss(
|
|
49
|
+
per_token_loss,
|
|
50
|
+
completion_mask,
|
|
51
|
+
loss_type=loss_type,
|
|
52
|
+
max_completion_length=max_completion_length,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
metrics = []
|
|
56
|
+
if beta != 0.0 and per_token_kl is not None:
|
|
57
|
+
metrics.append(_masked_mean(per_token_kl, completion_mask))
|
|
58
|
+
metrics.append(_masked_mean(is_clipped.float(), completion_mask))
|
|
59
|
+
return loss, metrics
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _reduce_grpo_loss(per_token_loss, completion_mask, loss_type, max_completion_length):
|
|
63
|
+
mask = completion_mask
|
|
64
|
+
if mask is None:
|
|
65
|
+
mask = torch.ones_like(per_token_loss, dtype=per_token_loss.dtype, device=per_token_loss.device)
|
|
66
|
+
mask = mask.to(per_token_loss.dtype)
|
|
67
|
+
|
|
68
|
+
if loss_type == "grpo":
|
|
69
|
+
per_seq = (per_token_loss * mask).sum(-1) / mask.sum(-1).clamp(min=1.0)
|
|
70
|
+
return per_seq.mean()
|
|
71
|
+
if loss_type == "bnpo":
|
|
72
|
+
return (per_token_loss * mask).sum() / mask.sum().clamp(min=1.0)
|
|
73
|
+
if loss_type == "dr_grpo":
|
|
74
|
+
if max_completion_length is None:
|
|
75
|
+
raise ValueError("max_completion_length must be provided when using loss_type='dr_grpo'")
|
|
76
|
+
batch = per_token_loss.shape[0]
|
|
77
|
+
return (per_token_loss * mask).sum() / (batch * max_completion_length)
|
|
78
|
+
if loss_type == "dapo":
|
|
79
|
+
normalizer = LigerFusedLinearPPOBase._compute_dapo_normalizer(mask)
|
|
80
|
+
return (per_token_loss * mask).sum() / normalizer
|
|
81
|
+
raise ValueError(f"Unsupported loss_type '{loss_type}' for Triton GRPO loss.")
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _masked_mean(values, mask):
|
|
85
|
+
if mask is None:
|
|
86
|
+
mask = torch.ones_like(values, dtype=values.dtype, device=values.device)
|
|
87
|
+
mask = mask.to(values.dtype)
|
|
88
|
+
return (values * mask).sum() / mask.sum().clamp(min=1.0)
|
|
34
89
|
|
|
35
90
|
|
|
36
91
|
# This is a demo how to use grpo_loss in GRPOTrainer. The Trl version must be 0.16
|
|
@@ -4,12 +4,12 @@ from typing import Union
|
|
|
4
4
|
|
|
5
5
|
import torch
|
|
6
6
|
|
|
7
|
-
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
8
|
-
|
|
9
7
|
if TYPE_CHECKING:
|
|
10
8
|
from transformers.models.falcon_h1.modeling_falcon_h1 import FalconHybridMambaAttentionDynamicCache
|
|
11
9
|
|
|
12
10
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
11
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
12
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
def lce_forward(
|
|
@@ -26,8 +26,9 @@ def lce_forward(
|
|
|
26
26
|
cache_position: Optional[torch.LongTensor] = None,
|
|
27
27
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
28
28
|
skip_logits: Optional[bool] = None,
|
|
29
|
+
return_dict: Optional[bool] = None,
|
|
29
30
|
**kwargs,
|
|
30
|
-
) -> Union[tuple,
|
|
31
|
+
) -> Union[tuple, LigerCausalLMOutputWithPast]:
|
|
31
32
|
r"""
|
|
32
33
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
33
34
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
@@ -54,6 +55,7 @@ def lce_forward(
|
|
|
54
55
|
output_hidden_states = (
|
|
55
56
|
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
|
56
57
|
)
|
|
58
|
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
|
57
59
|
|
|
58
60
|
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
|
59
61
|
outputs = self.model(
|
|
@@ -77,6 +79,8 @@ def lce_forward(
|
|
|
77
79
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
78
80
|
logits = None
|
|
79
81
|
loss = None
|
|
82
|
+
token_accuracy = None
|
|
83
|
+
|
|
80
84
|
# if in training mode, don't materialize logits
|
|
81
85
|
if skip_logits and labels is None:
|
|
82
86
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -85,8 +89,9 @@ def lce_forward(
|
|
|
85
89
|
# By default, if in training mode, don't materialize logits
|
|
86
90
|
skip_logits = self.training and labels is not None
|
|
87
91
|
|
|
92
|
+
# Compute loss
|
|
88
93
|
if skip_logits:
|
|
89
|
-
|
|
94
|
+
result = LigerForCausalLMLoss(
|
|
90
95
|
hidden_states=kept_hidden_states,
|
|
91
96
|
lm_head_weight=self.lm_head.weight,
|
|
92
97
|
labels=labels,
|
|
@@ -94,15 +99,24 @@ def lce_forward(
|
|
|
94
99
|
hidden_size=self.config.hidden_size,
|
|
95
100
|
**kwargs,
|
|
96
101
|
)
|
|
102
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
97
103
|
else:
|
|
98
104
|
logits = self.lm_head(kept_hidden_states)
|
|
99
105
|
if labels is not None or shift_labels is not None:
|
|
100
106
|
loss = self.loss_function(logits=logits, labels=labels, vocab_size=self.config.vocab_size, **kwargs)
|
|
101
107
|
|
|
102
|
-
|
|
108
|
+
if not return_dict:
|
|
109
|
+
output = (logits,) + outputs[1:]
|
|
110
|
+
output = ((loss,) + output) if loss is not None else output
|
|
111
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
112
|
+
return output
|
|
113
|
+
|
|
114
|
+
# Return custom output class with token_accuracy field
|
|
115
|
+
return LigerCausalLMOutputWithPast(
|
|
103
116
|
loss=loss,
|
|
104
117
|
logits=logits,
|
|
105
118
|
past_key_values=outputs.past_key_values,
|
|
106
119
|
hidden_states=outputs.hidden_states,
|
|
107
120
|
attentions=outputs.attentions,
|
|
121
|
+
token_accuracy=token_accuracy,
|
|
108
122
|
)
|
|
@@ -12,6 +12,8 @@ from transformers.utils.deprecation import deprecate_kwarg
|
|
|
12
12
|
|
|
13
13
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
14
14
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
15
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
16
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
15
17
|
|
|
16
18
|
|
|
17
19
|
def lce_forward_deprecated(
|
|
@@ -147,7 +149,7 @@ def lce_forward(
|
|
|
147
149
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
148
150
|
skip_logits: Optional[bool] = None,
|
|
149
151
|
**kwargs,
|
|
150
|
-
) -> Union[Tuple,
|
|
152
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
151
153
|
r"""
|
|
152
154
|
Args:
|
|
153
155
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -209,6 +211,7 @@ def lce_forward(
|
|
|
209
211
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
210
212
|
logits = None
|
|
211
213
|
loss = None
|
|
214
|
+
token_accuracy = None
|
|
212
215
|
|
|
213
216
|
if skip_logits and labels is None and shift_labels is None:
|
|
214
217
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -217,8 +220,9 @@ def lce_forward(
|
|
|
217
220
|
# By default, if in training mode, don't materialize logits
|
|
218
221
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
219
222
|
|
|
223
|
+
# Compute loss
|
|
220
224
|
if skip_logits:
|
|
221
|
-
|
|
225
|
+
result = LigerForCausalLMLoss(
|
|
222
226
|
hidden_states=kept_hidden_states,
|
|
223
227
|
lm_head_weight=self.lm_head.weight,
|
|
224
228
|
labels=labels,
|
|
@@ -226,6 +230,7 @@ def lce_forward(
|
|
|
226
230
|
hidden_size=self.config.hidden_size,
|
|
227
231
|
**kwargs,
|
|
228
232
|
)
|
|
233
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
229
234
|
else:
|
|
230
235
|
logits = self.lm_head(kept_hidden_states)
|
|
231
236
|
if labels is not None or shift_labels is not None:
|
|
@@ -238,13 +243,19 @@ def lce_forward(
|
|
|
238
243
|
)
|
|
239
244
|
|
|
240
245
|
if not return_dict:
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
246
|
+
output_tuple = (logits,) + outputs[1:]
|
|
247
|
+
if loss is not None:
|
|
248
|
+
output_tuple = (loss,) + output_tuple
|
|
249
|
+
if token_accuracy is not None:
|
|
250
|
+
output_tuple = output_tuple + (token_accuracy,)
|
|
251
|
+
return output_tuple
|
|
252
|
+
|
|
253
|
+
# Return custom output class with token_accuracy field
|
|
254
|
+
return LigerCausalLMOutputWithPast(
|
|
245
255
|
loss=loss,
|
|
246
256
|
logits=logits,
|
|
247
257
|
past_key_values=outputs.past_key_values,
|
|
248
258
|
hidden_states=outputs.hidden_states,
|
|
249
259
|
attentions=outputs.attentions,
|
|
260
|
+
token_accuracy=token_accuracy,
|
|
250
261
|
)
|
|
@@ -13,6 +13,8 @@ from transformers.utils.deprecation import deprecate_kwarg
|
|
|
13
13
|
|
|
14
14
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
15
15
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
16
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
17
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
16
18
|
|
|
17
19
|
logger = logging.getLogger(__name__)
|
|
18
20
|
|
|
@@ -158,7 +160,7 @@ def lce_forward(
|
|
|
158
160
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
159
161
|
skip_logits: Optional[bool] = None,
|
|
160
162
|
**kwargs,
|
|
161
|
-
) -> Union[Tuple,
|
|
163
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
162
164
|
r"""
|
|
163
165
|
Args:
|
|
164
166
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -225,6 +227,7 @@ def lce_forward(
|
|
|
225
227
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
226
228
|
logits = None
|
|
227
229
|
loss = None
|
|
230
|
+
token_accuracy = None
|
|
228
231
|
|
|
229
232
|
if skip_logits and labels is None and shift_labels is None:
|
|
230
233
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -233,8 +236,9 @@ def lce_forward(
|
|
|
233
236
|
# By default, if in training mode, don't materialize logits
|
|
234
237
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
235
238
|
|
|
239
|
+
# Compute loss
|
|
236
240
|
if skip_logits:
|
|
237
|
-
|
|
241
|
+
result = LigerForCausalLMLoss(
|
|
238
242
|
hidden_states=kept_hidden_states,
|
|
239
243
|
lm_head_weight=self.lm_head.weight,
|
|
240
244
|
labels=labels,
|
|
@@ -243,6 +247,7 @@ def lce_forward(
|
|
|
243
247
|
final_logit_softcapping=self.config.final_logit_softcapping,
|
|
244
248
|
**kwargs,
|
|
245
249
|
)
|
|
250
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
246
251
|
|
|
247
252
|
else:
|
|
248
253
|
logits = self.lm_head(kept_hidden_states)
|
|
@@ -262,13 +267,17 @@ def lce_forward(
|
|
|
262
267
|
)
|
|
263
268
|
|
|
264
269
|
if not return_dict:
|
|
265
|
-
|
|
266
|
-
|
|
270
|
+
output_tuple = (logits,) + outputs[1:]
|
|
271
|
+
output_tuple = (loss,) + output_tuple if loss is not None else output_tuple
|
|
272
|
+
output_tuple = output_tuple + (token_accuracy,) if token_accuracy is not None else output_tuple
|
|
273
|
+
return output_tuple
|
|
267
274
|
|
|
268
|
-
|
|
275
|
+
# Return custom output class with token_accuracy field
|
|
276
|
+
return LigerCausalLMOutputWithPast(
|
|
269
277
|
loss=loss,
|
|
270
278
|
logits=logits,
|
|
271
279
|
past_key_values=outputs.past_key_values,
|
|
272
280
|
hidden_states=outputs.hidden_states,
|
|
273
281
|
attentions=outputs.attentions,
|
|
282
|
+
token_accuracy=token_accuracy,
|
|
274
283
|
)
|
|
@@ -7,12 +7,13 @@ import torch.nn as nn
|
|
|
7
7
|
|
|
8
8
|
from transformers.cache_utils import Cache
|
|
9
9
|
from transformers.cache_utils import HybridCache
|
|
10
|
-
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
11
|
-
from transformers.models.gemma3.modeling_gemma3 import Gemma3CausalLMOutputWithPast
|
|
12
10
|
from transformers.utils import logging
|
|
13
11
|
|
|
14
12
|
from liger_kernel.transformers.fused_linear_cross_entropy import LigerFusedLinearCrossEntropyLoss
|
|
15
13
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
14
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
15
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
16
|
+
from liger_kernel.transformers.model.output_classes import LigerGemma3CausalLMOutputWithPast
|
|
16
17
|
|
|
17
18
|
logger = logging.get_logger(__name__)
|
|
18
19
|
|
|
@@ -33,7 +34,7 @@ def causal_forward(
|
|
|
33
34
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
34
35
|
skip_logits: Optional[bool] = None,
|
|
35
36
|
**loss_kwargs,
|
|
36
|
-
) -> Union[Tuple,
|
|
37
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
37
38
|
r"""
|
|
38
39
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
39
40
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
@@ -98,12 +99,14 @@ def causal_forward(
|
|
|
98
99
|
shift_labels = loss_kwargs.pop("shift_labels", None)
|
|
99
100
|
loss = None
|
|
100
101
|
logits = None
|
|
102
|
+
token_accuracy = None
|
|
101
103
|
|
|
102
104
|
if skip_logits is None:
|
|
103
105
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
104
106
|
|
|
107
|
+
# Compute loss
|
|
105
108
|
if skip_logits:
|
|
106
|
-
|
|
109
|
+
result = LigerForCausalLMLoss(
|
|
107
110
|
hidden_states=kept_hidden_states,
|
|
108
111
|
lm_head_weight=self.lm_head.weight,
|
|
109
112
|
labels=labels,
|
|
@@ -112,7 +115,7 @@ def causal_forward(
|
|
|
112
115
|
final_logit_softcapping=self.config.final_logit_softcapping,
|
|
113
116
|
**loss_kwargs,
|
|
114
117
|
)
|
|
115
|
-
|
|
118
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
116
119
|
else:
|
|
117
120
|
logits = self.lm_head(kept_hidden_states)
|
|
118
121
|
if self.config.final_logit_softcapping is not None:
|
|
@@ -129,15 +132,19 @@ def causal_forward(
|
|
|
129
132
|
)
|
|
130
133
|
|
|
131
134
|
if not return_dict:
|
|
132
|
-
|
|
133
|
-
|
|
135
|
+
output_tuple = (logits,) + outputs[1:]
|
|
136
|
+
output_tuple = (loss,) + output_tuple if loss is not None else output_tuple
|
|
137
|
+
output_tuple = output_tuple + (token_accuracy,) if token_accuracy is not None else output_tuple
|
|
138
|
+
return output_tuple
|
|
134
139
|
|
|
135
|
-
|
|
140
|
+
# Return custom output class with token_accuracy field
|
|
141
|
+
return LigerCausalLMOutputWithPast(
|
|
136
142
|
loss=loss,
|
|
137
143
|
logits=logits,
|
|
138
144
|
past_key_values=outputs.past_key_values,
|
|
139
145
|
hidden_states=outputs.hidden_states,
|
|
140
146
|
attentions=outputs.attentions,
|
|
147
|
+
token_accuracy=token_accuracy,
|
|
141
148
|
)
|
|
142
149
|
|
|
143
150
|
|
|
@@ -159,7 +166,7 @@ def multimodal_forward(
|
|
|
159
166
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
160
167
|
skip_logits: Optional[bool] = None,
|
|
161
168
|
**lm_kwargs,
|
|
162
|
-
) -> Union[tuple,
|
|
169
|
+
) -> Union[tuple, LigerGemma3CausalLMOutputWithPast]:
|
|
163
170
|
r"""
|
|
164
171
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
165
172
|
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
|
@@ -235,6 +242,7 @@ def multimodal_forward(
|
|
|
235
242
|
|
|
236
243
|
loss = None
|
|
237
244
|
logits = None
|
|
245
|
+
token_accuracy = None
|
|
238
246
|
if skip_logits and labels is None:
|
|
239
247
|
raise ValueError("skip_logits is True, but labels is None")
|
|
240
248
|
|
|
@@ -261,7 +269,9 @@ def multimodal_forward(
|
|
|
261
269
|
shift_labels = shift_labels.view(-1).to(hidden_device)
|
|
262
270
|
|
|
263
271
|
lce = LigerFusedLinearCrossEntropyLoss()
|
|
264
|
-
|
|
272
|
+
result = lce(self.lm_head.weight, shift_hidden_states, shift_labels)
|
|
273
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
274
|
+
|
|
265
275
|
else:
|
|
266
276
|
logits = self.lm_head(kept_hidden_states)
|
|
267
277
|
if labels is not None:
|
|
@@ -306,13 +316,16 @@ def multimodal_forward(
|
|
|
306
316
|
|
|
307
317
|
if not return_dict:
|
|
308
318
|
output = (logits,) + outputs[1:]
|
|
309
|
-
|
|
319
|
+
output = (loss,) + output if loss is not None else output
|
|
320
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
321
|
+
return output
|
|
310
322
|
|
|
311
|
-
return
|
|
323
|
+
return LigerGemma3CausalLMOutputWithPast(
|
|
312
324
|
loss=loss,
|
|
313
325
|
logits=logits,
|
|
314
326
|
past_key_values=outputs.past_key_values,
|
|
315
327
|
hidden_states=outputs.hidden_states,
|
|
316
328
|
attentions=outputs.attentions,
|
|
317
329
|
image_hidden_states=outputs.image_hidden_states,
|
|
330
|
+
token_accuracy=token_accuracy,
|
|
318
331
|
)
|
|
@@ -5,10 +5,11 @@ from typing import Union
|
|
|
5
5
|
|
|
6
6
|
import torch
|
|
7
7
|
|
|
8
|
-
from transformers.modeling_outputs import CausalLMOutputWithPast
|
|
9
8
|
from transformers.utils.deprecation import deprecate_kwarg
|
|
10
9
|
|
|
11
10
|
from liger_kernel.transformers.model.loss_utils import LigerForCausalLMLoss
|
|
11
|
+
from liger_kernel.transformers.model.loss_utils import unpack_cross_entropy_result
|
|
12
|
+
from liger_kernel.transformers.model.output_classes import LigerCausalLMOutputWithPast
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
@deprecate_kwarg("num_logits_to_keep", version="4.50", new_name="logits_to_keep")
|
|
@@ -28,7 +29,7 @@ def lce_forward(
|
|
|
28
29
|
logits_to_keep: Union[int, torch.Tensor] = 0,
|
|
29
30
|
skip_logits: Optional[bool] = None,
|
|
30
31
|
**kwargs,
|
|
31
|
-
) -> Union[Tuple,
|
|
32
|
+
) -> Union[Tuple, LigerCausalLMOutputWithPast]:
|
|
32
33
|
r"""
|
|
33
34
|
Args:
|
|
34
35
|
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
|
@@ -91,6 +92,7 @@ def lce_forward(
|
|
|
91
92
|
shift_labels = kwargs.pop("shift_labels", None)
|
|
92
93
|
logits = None
|
|
93
94
|
loss = None
|
|
95
|
+
token_accuracy = None
|
|
94
96
|
|
|
95
97
|
if skip_logits and labels is None and shift_labels is None:
|
|
96
98
|
raise ValueError("skip_logits is True, but labels and shift_labels are None")
|
|
@@ -99,8 +101,9 @@ def lce_forward(
|
|
|
99
101
|
# By default, if in training mode, don't materialize logits
|
|
100
102
|
skip_logits = self.training and (labels is not None or shift_labels is not None)
|
|
101
103
|
|
|
104
|
+
# Compute loss
|
|
102
105
|
if skip_logits:
|
|
103
|
-
|
|
106
|
+
result = LigerForCausalLMLoss(
|
|
104
107
|
hidden_states=kept_hidden_states,
|
|
105
108
|
lm_head_weight=self.lm_head.weight,
|
|
106
109
|
labels=labels,
|
|
@@ -108,6 +111,7 @@ def lce_forward(
|
|
|
108
111
|
hidden_size=self.config.hidden_size,
|
|
109
112
|
**kwargs,
|
|
110
113
|
)
|
|
114
|
+
loss, _, token_accuracy = unpack_cross_entropy_result(result)
|
|
111
115
|
|
|
112
116
|
else:
|
|
113
117
|
logits = self.lm_head(kept_hidden_states)
|
|
@@ -120,10 +124,18 @@ def lce_forward(
|
|
|
120
124
|
**kwargs,
|
|
121
125
|
)
|
|
122
126
|
|
|
123
|
-
|
|
127
|
+
if not return_dict:
|
|
128
|
+
output = (logits,) + outputs[1:]
|
|
129
|
+
output = ((loss,) + output) if loss is not None else output
|
|
130
|
+
output = output + (token_accuracy,) if token_accuracy is not None else output
|
|
131
|
+
return output
|
|
132
|
+
|
|
133
|
+
# Return custom output class with token_accuracy field
|
|
134
|
+
return LigerCausalLMOutputWithPast(
|
|
124
135
|
loss=loss,
|
|
125
136
|
logits=logits,
|
|
126
137
|
past_key_values=outputs.past_key_values,
|
|
127
138
|
hidden_states=outputs.hidden_states,
|
|
128
139
|
attentions=outputs.attentions,
|
|
140
|
+
token_accuracy=token_accuracy,
|
|
129
141
|
)
|