{"record":{"id":"bd3d175fbbb1bc9d","repo":"huggingface/transformers","slug":"found-return-loss-true-in-inputs-loss-computati","errorCode":null,"errorMessage":"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.","messagePattern":"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\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/exporters/utils.py","lineNumber":374,"sourceCode":"    - Pre-computes data-dependent vision/audio kwargs registered via\n      `@register_export_input_preparer` and writes them into `inputs`.\n    - Casts input tensors to match the model's `dtype` / `device`.\n    \"\"\"\n    # Strip label inputs — loss computation is not supported during export.\n    for label_key in (\"labels\", \"future_values\"):\n        value = inputs.pop(label_key, None)\n        if value is not None:\n            raise ValueError(\n                f\"Found '{label_key}' in inputs. Loss computation is not supported during export. \"\n                f\"Please remove '{label_key}' from your inputs before calling export().\"\n            )\n    if hasattr(model, \"config\") and getattr(model.config, \"return_loss\", False):\n        raise ValueError(\n            \"Found 'model.config.return_loss=True'. Loss computation is not supported during export. \"\n            \"Please set 'model.config.return_loss=False' before calling export().\"\n        )\n    if inputs.get(\"return_loss\", False):\n        raise ValueError(\n            \"Found 'return_loss=True' in inputs. Loss computation is not supported during export. \"\n            \"Please remove 'return_loss' from your inputs or set it to False.\"\n        )\n\n    # Pop output flags from `inputs` and return them so the caller can decide how to\n    # honour them during the trace (we don't want them as traced kwargs).\n    output_flags = {flag: inputs.pop(flag) for flag in _OUTPUT_FLAGS if flag in inputs}\n\n    # Pre-compute data-dependent vision/audio tensors that use loops, .tolist(),\n    # repeat_interleave, or itertools.groupby — untraceable by dynamo.\n    # TODO: use the collator API once it covers these cases.\n    with torch.no_grad():\n        precompute_export_inputs(model, inputs)\n\n    # Cast all input tensors to match the model's dtype and device (e.g. cache objects\n    # created before the model was moved to bfloat16/CUDA by a backend preparation step).\n    dtype = module_dtype(model)\n    device = module_device(model)","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/exporters/utils.py#L356-L392","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ninputs = {\"input_ids\": ids, \"return_loss\": True}\nexport_model(model, inputs)\n\n// after\ninputs = {\"input_ids\": ids}\nexport_model(model, inputs)","handlingStrategy":"validation","validationCode":"EXPORT_FORBIDDEN = {\"labels\", \"future_values\", \"return_loss\"}\ninputs = {k: v for k, v in inputs.items() if k not in EXPORT_FORBIDDEN}","typeGuard":"def is_inference_inputs(inputs: dict) -> bool:\n    return not ({\"labels\", \"future_values\", \"return_loss\"} & inputs.keys())","tryCatchPattern":"try:\n    export_model(model, inputs)\nexcept ValueError as e:\n    if \"return_loss\" in str(e):\n        inputs.pop(\"return_loss\", None)\n        export_model(model, inputs)\n    else:\n        raise","preventionTips":["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"],"tags":["export","inputs","loss","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}