Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions src/transformers/configuration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,19 @@ def __post_init__(self, **kwargs):
# Keys are always strings in JSON so convert ids to int
self.id2label = {int(key): value for key, value in self.id2label.items()}

# `problem_type="single_label_classification"` with `num_labels=1` is degenerate: applying
# cross-entropy to a single logit yields a constant zero loss. Reject this combination with a
# clear message pointing users to the intended setup (use `num_labels=2` for binary
# classification, or `problem_type="regression"` for a single-output regression head).
# See https://github.com/huggingface/transformers/issues/45479.
if self.problem_type == "single_label_classification" and self.num_labels == 1:
raise ValueError(
'`problem_type="single_label_classification"` requires `num_labels > 1`. With '
"`num_labels=1` the cross-entropy loss is degenerate and always zero. For binary "
'classification use `num_labels=2`, or use `problem_type="regression"` for a '
"single-output regression head."
)
Comment thread
Rocketknight1 marked this conversation as resolved.
Outdated
Comment thread
Rocketknight1 marked this conversation as resolved.

# BC for rotary embeddings. We will pop out legacy keys from kwargs and rename to new format
if hasattr(self, "rope_parameters"):
kwargs = self.convert_rope_params_to_dict(**kwargs)
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/test_configuration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,20 @@ def test_loading_config_do_not_raise_future_warnings(self):
warnings.simplefilter("error")
PreTrainedConfig.from_pretrained("bert-base-uncased")

def test_single_label_classification_requires_more_than_one_label(self):
"""Regression test for https://github.com/huggingface/transformers/issues/45479.

`problem_type="single_label_classification"` with `num_labels=1` used to silently produce a
degenerate zero cross-entropy loss. It must now raise a clear error at config construction.
"""
with self.assertRaises(ValueError):
BertConfig(num_labels=1, problem_type="single_label_classification")

# Valid combinations must still work.
BertConfig(num_labels=2, problem_type="single_label_classification")
BertConfig(num_labels=1, problem_type="regression")
BertConfig(num_labels=1) # problem_type left unset is fine; it is inferred at forward time.

Comment thread
Rocketknight1 marked this conversation as resolved.
Outdated
def test_get_text_config(self):
"""Tests the `get_text_config` method."""
# 1. model with only text input -> returns the original config instance
Expand Down
Loading