huggingface/transformers · error · ValueError
Found 'return_loss=True' in inputs. Loss computation is not
Error message
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.
What it means
The export input preparer found return_loss=True passed directly in the inputs dict. Because tracing only supports the inference path, any request for loss computation in inputs is rejected before the trace starts. The error names the exact offending key so the caller can strip it.
Source
Thrown at src/transformers/exporters/utils.py:374
- 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)
# Cast all input tensors to match the model's dtype and device (e.g. cache objects
# created before the model was moved to bfloat16/CUDA by a backend preparation step).
dtype = module_dtype(model)
device = module_device(model)View on GitHub (pinned to a597f97485)
Solutions
- Remove return_loss from the inputs dict or set it to False before export()
- Build a dedicated inference-only inputs dict (only forward keys: input_ids, pixel_values, attention_mask, etc.)
- If you need loss in the exported graph, use a custom export path — the built-in exporter will not support it
Example fix
// before
inputs = {"input_ids": ids, "return_loss": True}
export_model(model, inputs)
// after
inputs = {"input_ids": ids}
export_model(model, inputs) Defensive patterns
Strategy: validation
Validate before calling
EXPORT_FORBIDDEN = {"labels", "future_values", "return_loss"}
inputs = {k: v for k, v in inputs.items() if k not in EXPORT_FORBIDDEN} Type guard
def is_inference_inputs(inputs: dict) -> bool:
return not ({"labels", "future_values", "return_loss"} & inputs.keys()) Try / catch
try:
export_model(model, inputs)
except ValueError as e:
if "return_loss" in str(e):
inputs.pop("return_loss", None)
export_model(model, inputs)
else:
raise Prevention
- Build a dedicated inference inputs dict rather than reusing training batches
- Strip training-only keys in one filter step before any export call
- Log the input keys right before export to catch stray flags
When it happens
Trigger: Calling export() with an inputs dict that contains return_loss: True (e.g. inputs built for training/eval and reused for export), while model.config.return_loss is False or unset.
Common situations: Reusing a batch dict produced for training in an export script; pipelines that add return_loss for metrics; version changes where return_loss moved from config to forward kwargs.
Related errors
- Found 'model.config.return_loss=True'. Loss computation is n
- decompose_prefill_decode failed for {type(model).__name__}.
- decompose_multimodal failed for {type(model).__name__}. Inpu
- Found '{label_key}' in inputs. Loss computation is not suppo
- decompose_prefill_decode expected at least {num_new_tokens}
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/bd3d175fbbb1bc9d.
Report an issue: GitHub.