Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/instructlab/training/main_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
)
import torch
import torch.distributed
from torch.nn import CrossEntropyLoss

# First Party
from instructlab.training import config
Expand Down Expand Up @@ -153,6 +154,7 @@ def setup_model(
model = GPTDolomiteForCausalLM.from_pretrained(
**base_model_args,
)
model.loss_fct = CrossEntropyLoss(reduction="sum")
elif args.use_liger:
# TODO(osilkin): we duplicate some checks here because someone may run this script through
# torchrun directly and not `run_training`. To fix this, we should eventually move everything
Expand All @@ -164,6 +166,7 @@ def setup_model(
try:
# Third Party
from liger_kernel.transformers import AutoLigerKernelForCausalLM
from liger_kernel.transformers.cross_entropy import LigerCrossEntropyLoss
except ImportError as e:
raise ValueError(
"Liger kernels are not installed. Please install Liger kernels using the following command: pip install liger-kernel"
Expand All @@ -175,8 +178,10 @@ def setup_model(
model = AutoLigerKernelForCausalLM.from_pretrained(
**base_model_args, cross_entropy=True, fused_linear_cross_entropy=False
)
model.loss_fct = LigerCrossEntropyLoss(reduction="sum")
else:
model = AutoModelForCausalLM.from_pretrained(**base_model_args)
model.loss_fct = CrossEntropyLoss(reduction="sum")

# store the base model args so we can recall them later if saving a LoRA model
args.base_model_args = base_model_args
Expand Down
3 changes: 1 addition & 2 deletions src/instructlab/training/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ def reduce_sum_forward(
shift_labels = shift_labels.view(-1)
# Ensure tensors are on the same device
shift_labels = shift_labels.to(shift_logits.device)
loss_fct = torch.nn.CrossEntropyLoss(reduction="sum")
loss = loss_fct(shift_logits, shift_labels)
loss = model.loss_fct(shift_logits, shift_labels)

if not return_dict:
return ((loss,) + output) if loss is not None else output
Expand Down