{"record":{"id":"6758943991ea20de","repo":"hiyouga/LlamaFactory","slug":"megatron-bridge-is-incompatible-with-deepspeed","errorCode":null,"errorMessage":"Megatron Bridge is incompatible with DeepSpeed.","messagePattern":"Megatron Bridge is incompatible with DeepSpeed\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/hparams/parser.py","lineNumber":441,"sourceCode":"            raise ValueError(\"`neat_packing` cannot be set as True except SFT.\")\n\n        if data_args.train_on_prompt or data_args.mask_history:\n            raise ValueError(\"`train_on_prompt` or `mask_history` cannot be set as True except SFT.\")\n\n    if finetuning_args.stage == \"sft\" and training_args.do_predict and not training_args.predict_with_generate:\n        raise ValueError(\"Please enable `predict_with_generate` to save model predictions.\")\n\n    if finetuning_args.use_megatron_bridge:\n        if finetuning_args.use_mca or finetuning_args.use_hyper_parallel:\n            raise ValueError(\"Megatron Bridge cannot be used together with MCA or HyperParallel.\")\n        if finetuning_args.stage not in [\"pt\", \"sft\"]:\n            raise ValueError(\"Megatron Bridge only supports the `pt` and `sft` stages.\")\n        if finetuning_args.finetuning_type not in [\"full\", \"lora\"]:\n            raise ValueError(\"Megatron Bridge only supports `full` and `lora` finetuning.\")\n        if model_args.quantization_bit is not None:\n            raise ValueError(\"Quantized models are not supported with Megatron Bridge.\")\n        if training_args.deepspeed is not None:\n            raise ValueError(\"Megatron Bridge is incompatible with DeepSpeed.\")\n        if mb_args is None:\n            raise ValueError(\"Megatron Bridge arguments are missing. Please set USE_MEGATRON_BRIDGE=1.\")\n        _validate_megatron_bridge_parallel_args(mb_args, training_args.world_size)\n        finetuning_args.megatron_bridge_args = mb_args\n\n    if finetuning_args.stage in [\"rm\", \"ppo\"] and training_args.load_best_model_at_end:\n        raise ValueError(\"RM and PPO stages do not support `load_best_model_at_end`.\")\n\n    if finetuning_args.stage == \"ppo\":\n        if not training_args.do_train:\n            raise ValueError(\"PPO training does not support evaluation, use the SFT stage to evaluate models.\")\n\n        if model_args.shift_attn:\n            raise ValueError(\"PPO training is incompatible with S^2-Attn.\")\n\n        if finetuning_args.reward_model_type == \"lora\" and model_args.use_kt:\n            raise ValueError(\"KTransformers does not support lora reward model.\")\n","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/hparams/parser.py#L423-L459","documentation":"DeepSpeed ZeRO sharding and Megatron's own distributed runtime both try to own the process group and optimizer state; running both is impossible. When use_megatron_bridge is true and training_args.deepspeed is set (a DeepSpeed config file path), the parser raises ValueError.","triggerScenarios":"USE_MEGATRON_BRIDGE=1 together with deepspeed: ds_z2_config.json (or --deepspeed on the CLI) in the training args of a train config.","commonSituations":"Cluster templates that always inject a deepspeed block; migrating a DeepSpeed SFT recipe to Megatron Bridge and forgetting to strip the deepspeed key; CI passing --deepspeed globally.","solutions":["Delete the deepspeed key (or pass null) from the config when using Megatron Bridge; use Megatron parallel sizes (TP/PP/CP/EP) for sharding instead.","If you specifically need ZeRO optimizations, unset USE_MEGATRON_BRIDGE and stay on the DeepSpeed path.","Check launcher scripts for hardcoded --deepspeed flags."],"exampleFix":"# before\nexport USE_MEGATRON_BRIDGE=1\ndeepspeed: ds_z3_config.json\n\n# after\nexport USE_MEGATRON_BRIDGE=1\n# deepspeed removed; use tensor/pipeline parallelism instead\ntensor_model_parallel_size: 2\npipeline_model_parallel_size: 2","handlingStrategy":"validation","validationCode":"import os\n\nif os.environ.get(\"USE_MEGATRON_BRIDGE\") == \"1\" and cfg.get(\"deepspeed\"):\n    raise SystemExit(\"Megatron Bridge is incompatible with DeepSpeed; remove the deepspeed config\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep DeepSpeed configs and Megatron configs in separate directories so they are never merged by hand.","Check launcher scripts for hardcoded --deepspeed before switching backends."],"tags":["megatron","deepspeed","distributed","config","backend-conflict"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}