{"record":{"id":"1c5163e1ae9ef712","repo":"huggingface/transformers","slug":"found-model-config-return-loss-true-loss-comput","errorCode":null,"errorMessage":"Found 'model.config.return_loss=True'. Loss computation is not supported during export. Please set 'model.config.return_loss=False' before calling export().","messagePattern":"Found 'model\\.config\\.return_loss=True'\\. Loss computation is not supported during export\\. Please set 'model\\.config\\.return_loss=False' before calling export\\(\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/exporters/utils.py","lineNumber":369,"sourceCode":"\n    - Strips label inputs (`labels`, `future_values`) — loss computation is unsupported.\n    - Pops output flags (`use_cache`, `return_dict`, …) from `inputs` so they don't appear\n      as traced kwargs; the values are returned for the trace block to apply onto\n      `model.config`.\n    - 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)","sourceCodeStart":351,"sourceCodeEnd":387,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/exporters/utils.py#L351-L387","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before\nmodel.config.return_loss = True\nexport_model(model, inputs)\n\n// after\nmodel.config.return_loss = False\nexport_model(model, inputs)","handlingStrategy":"validation","validationCode":"def ensure_export_ready(model):\n    cfg = getattr(model, \"config\", None)\n    if cfg is not None and getattr(cfg, \"return_loss\", False):\n        cfg.return_loss = False\n    return model","typeGuard":"def is_loss_free_for_export(model) -> bool:\n    return not getattr(getattr(model, \"config\", None), \"return_loss\", False)","tryCatchPattern":"try:\n    export_model(model, inputs)\nexcept ValueError as e:\n    if \"return_loss\" in str(e):\n        model.config.return_loss = False\n        export_model(model, inputs)\n    else:\n        raise","preventionTips":["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"],"tags":["export","config","loss","transformers"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}