-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement new approach with mark_dirty()
- Loading branch information
Showing
4 changed files
with
57 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,30 @@ | ||
from torch.nn import CrossEntropyLoss | ||
import torch | ||
|
||
from liger_kernel.ops.cross_entropy import LigerCrossEntropyFunction | ||
|
||
|
||
class LigerCrossEntropyLoss(CrossEntropyLoss): | ||
def __init__(self, *args, **kwargs): | ||
super(LigerCrossEntropyLoss, self).__init__(*args, **kwargs) | ||
assert (self.label_smoothing >= 0) and ( | ||
self.label_smoothing <= 1 | ||
class LigerCrossEntropyLoss(torch.nn.Module): | ||
def __init__(self, ignore_index=-100, label_smoothing=0.0, reduction="mean"): | ||
super().__init__() | ||
assert (label_smoothing >= 0) and ( | ||
label_smoothing <= 1 | ||
), f"label_smoothing must be between 0.0 and 1.0. Got: {self.label_smoothing}" | ||
assert self.reduction in { | ||
assert reduction in { | ||
"mean", | ||
"sum", | ||
"none", | ||
}, f"reduction must be one of 'mean', 'sum', or 'none'. Got: {self.reduction}" | ||
self.ignore_index = ignore_index | ||
self.label_smoothing = label_smoothing | ||
self.reduction = reduction | ||
|
||
def forward(self, _input, target): | ||
return LigerCrossEntropyFunction.apply( | ||
_input, target, self.ignore_index, self.label_smoothing, self.reduction | ||
def forward(self, _input, target, inplace): | ||
loss, _ = LigerCrossEntropyFunction.apply( | ||
_input, | ||
target, | ||
ignore_index=self.ignore_index, | ||
label_smoothing=self.label_smoothing, | ||
reduction=self.reduction, | ||
inplace=inplace, | ||
) | ||
return loss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters