{"record":{"id":"9235ba4f926cbf45","repo":"hiyouga/LlamaFactory","slug":"the-model-is-not-a-pretrainedmodel-export-abort","errorCode":null,"errorMessage":"The model is not a `PreTrainedModel`, export aborted.","messagePattern":"The model is not a `PreTrainedModel`, export aborted\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/train/tuner.py","lineNumber":195,"sourceCode":"    model_args, data_args, finetuning_args, _ = get_infer_args(args)\n\n    if model_args.export_dir is None:\n        raise ValueError(\"Please specify `export_dir` to save model.\")\n\n    if model_args.adapter_name_or_path is not None and model_args.export_quantization_bit is not None:\n        raise ValueError(\"Please merge adapters before quantizing the model.\")\n\n    tokenizer_module = load_tokenizer(model_args)\n    tokenizer = tokenizer_module[\"tokenizer\"]\n    processor = tokenizer_module[\"processor\"]\n    template = get_template_and_fix_tokenizer(tokenizer, data_args)\n    model = load_model(tokenizer, model_args, finetuning_args)  # must after fixing tokenizer to resize vocab\n\n    if getattr(model, \"quantization_method\", None) is not None and model_args.adapter_name_or_path is not None:\n        raise ValueError(\"Cannot merge adapters to a quantized model.\")\n\n    if not isinstance(model, PreTrainedModel):\n        raise ValueError(\"The model is not a `PreTrainedModel`, export aborted.\")\n\n    if getattr(model, \"quantization_method\", None) is not None:  # quantized model adopts float16 type\n        setattr(model.config, \"torch_dtype\", torch.float16)\n    else:\n        if model_args.infer_dtype == \"auto\":\n            output_dtype = getattr(model.config, \"torch_dtype\", torch.float32)\n            if output_dtype == torch.float32:  # if infer_dtype is auto, try using half precision first\n                output_dtype = infer_optim_dtype(torch.bfloat16)\n        else:\n            output_dtype = getattr(torch, model_args.infer_dtype)\n\n        setattr(model.config, \"torch_dtype\", output_dtype)\n        model = model.to(output_dtype)\n        logger.info_rank0(f\"Convert model dtype to: {output_dtype}.\")\n\n    # Prepare save arguments (safe_serialization removed in transformers v5.0.0)\n    save_kwargs = {\n        \"save_directory\": model_args.export_dir,","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/train/tuner.py#L177-L213","documentation":"export_model (src/llamafactory/train/tuner.py:195) requires the loaded object to be a transformers `PreTrainedModel` because it calls model.save_pretrained with sharding/dtype logic. If load_model returned another type (KTransformers model, vLLM engine object, or a custom wrapper), export aborts.","triggerScenarios":"Calling export with `use_kt: true` (KTransformers backend), a megatron/hyper-parallel style model, or any config that makes load_model return a non-HF object.","commonSituations":"Users who trained or loaded with an alternate backend and then reuse the same model_args for `llamafactory-cli export`.","solutions":["Strip backend-specific flags (use_kt etc.) from the export config so load_model returns a PreTrainedModel.","For KTransformers, use its own weight-saving tooling instead of llamafactory-cli export.","If a custom loader is involved, ensure it returns a PreTrainedModel before export."],"exampleFix":"# before (export yaml)\nuse_kt: true\nmodel_name_or_path: ...\n\n# after\nmodel_name_or_path: ...   # plain HF load","handlingStrategy":"type-guard","validationCode":"from transformers import PreTrainedModel\nassert isinstance(model, PreTrainedModel), \"export requires an HF PreTrainedModel\"","typeGuard":"from transformers import PreTrainedModel\ndef is_exportable(model) -> bool:\n    return isinstance(model, PreTrainedModel)","tryCatchPattern":null,"preventionTips":["Keep export configs free of backend flags (use_kt etc.).","Maintain separate train and export config files per backend."],"tags":["export","ktransformers","model-type"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}