huggingface/transformers · error · ValueError
Found 'model.config.return_loss=True'. Loss computation is n
Error message
Found 'model.config.return_loss=True'. Loss computation is not supported during export. Please set 'model.config.return_loss=False' before calling export().
What it means
The exporters input preparer refuses to trace a model whose config still has return_loss=True. Export (torch export / ONNX-style tracing) only captures the forward inference path, and loss computation branches on labels are not traceable, so the exporter hard-fails before tracing. This check exists so the exported graph never silently drops a loss the user expected.
Source
Thrown at src/transformers/exporters/utils.py:369
- Strips label inputs (`labels`, `future_values`) — loss computation is unsupported.
- Pops output flags (`use_cache`, `return_dict`, …) from `inputs` so they don't appear
as traced kwargs; the values are returned for the trace block to apply onto
`model.config`.
- Pre-computes data-dependent vision/audio kwargs registered via
`@register_export_input_preparer` and writes them into `inputs`.
- Casts input tensors to match the model's `dtype` / `device`.
"""
# Strip label inputs — loss computation is not supported during export.
for label_key in ("labels", "future_values"):
value = inputs.pop(label_key, None)
if value is not None:
raise ValueError(
f"Found '{label_key}' in inputs. Loss computation is not supported during export. "
f"Please remove '{label_key}' from your inputs before calling export()."
)
if hasattr(model, "config") and getattr(model.config, "return_loss", False):
raise ValueError(
"Found 'model.config.return_loss=True'. Loss computation is not supported during export. "
"Please set 'model.config.return_loss=False' before calling export()."
)
if inputs.get("return_loss", False):
raise ValueError(
"Found 'return_loss=True' in inputs. Loss computation is not supported during export. "
"Please remove 'return_loss' from your inputs or set it to False."
)
# Pop output flags from `inputs` and return them so the caller can decide how to
# honour them during the trace (we don't want them as traced kwargs).
output_flags = {flag: inputs.pop(flag) for flag in _OUTPUT_FLAGS if flag in inputs}
# Pre-compute data-dependent vision/audio tensors that use loops, .tolist(),
# repeat_interleave, or itertools.groupby — untraceable by dynamo.
# TODO: use the collator API once it covers these cases.
with torch.no_grad():
precompute_export_inputs(model, inputs)View on GitHub (pinned to a597f97485)
Solutions
- Set model.config.return_loss = False before calling export()
- Load a fresh inference copy of the model (e.g. AutoModel.from_pretrained(...) without training flags) and export that
- Make sure 'labels' / 'future_values' are also absent from the inputs dict (they are checked separately)
Example fix
// before model.config.return_loss = True export_model(model, inputs) // after model.config.return_loss = False export_model(model, inputs)
Defensive patterns
Strategy: validation
Validate before calling
def ensure_export_ready(model):
cfg = getattr(model, "config", None)
if cfg is not None and getattr(cfg, "return_loss", False):
cfg.return_loss = False
return model Type guard
def is_loss_free_for_export(model) -> bool:
return not getattr(getattr(model, "config", None), "return_loss", False) Try / catch
try:
export_model(model, inputs)
except ValueError as e:
if "return_loss" in str(e):
model.config.return_loss = False
export_model(model, inputs)
else:
raise Prevention
- Keep separate model instances for training and export
- Run a pre-export checklist that clears return_loss on config and inputs
- Assert inputs contain only forward keys before export
When it happens
Trigger: Calling transformers export utilities (e.g. export() / decompose_for_generation) on a model where model.config.return_loss was left True, typically after using the same model instance for training or loss evaluation.
Common situations: Reusing a fine-tuned/training model object for export; multimodal models (e.g. some vision-language or audio models) whose configs default return_loss=True; upgrading to a transformers version where export became strict about loss flags.
Related errors
- Found 'return_loss=True' in inputs. Loss computation is not
- export_config_dict must contain key 'export_format' set to e
- Unknown exporter type, got {name} - supported exporters are:
- Unsupported export config: {export_config_dict!r}. Registere
- Per-component `config` dict is missing entries for: {sorted(
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/1c5163e1ae9ef712.
Report an issue: GitHub.