Skip to content

Debug: num_items_in_batch on a different device from loss. #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion unsloth_zoo/loss_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from triton import __version__ as triton_version
major, minor = torch.cuda.get_device_capability()
import inspect
from typing import Union

global HAS_CUT_CROSS_ENTROPY
global UNSLOTH_STUDIO_ENABLED
Expand Down Expand Up @@ -159,12 +160,15 @@ def fused_linear_cross_entropy(
hidden_states : torch.Tensor,
lm_weight : torch.Tensor,
labels : torch.Tensor,
num_items_in_batch : int = None,
num_items_in_batch : Union[int, torch.Tensor] = None,
ignore_index : int = -100,
reduction : str = "mean",
logit_softcapping : float = 0,
accuracy_threshold : str = "auto",
):
if isinstance(num_items_in_batch, torch.Tensor):
num_items_in_batch = num_items_in_batch.detach().cpu().item() # `torch.Tensor` -> `int`.

# All Unsloth Zoo code licensed under LGPLv3
reduction = "sum" if num_items_in_batch is not None else "mean"
if logit_softcapping == 0: logit_softcapping = None
Expand Down