Skip to content

Commit 7d2a83e

Browse files
committed
fix(engine): guard megatron-bridge on NPU and restore bridge_type field
Combined megatron-bridge compatibility fixes for NPU: - Make megatron.bridge imports defensive in megatron_engine.py and megatron_utils/megatron_lora.py (the latter runs a module-level monkey patch at import time) so modules load on NPU where the CUDA-only megatron-bridge package is unavailable. - Restore the bridge_type field on MegatronEngineConfig (an earlier rebase silently landed it inside MindSpeedEngineConfig after as_dict, which made MegatronEngineConfig(bridge_type=...) raise TypeError).
1 parent 0879f7b commit 7d2a83e

5 files changed

Lines changed: 34 additions & 20 deletions

File tree

areal/api/cli_args.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,22 @@ class MegatronEngineConfig:
921921
account_for_embedding_in_pipeline_split: bool = field(default=False)
922922
account_for_loss_in_pipeline_split: bool = field(default=False)
923923

924+
# Bridge backend used for HF<->Megatron conversion/model creation.
925+
bridge_type: str = field(
926+
default="mbridge",
927+
metadata={
928+
"help": "Bridge backend for MegatronEngine. Choices: 'mbridge' or 'megatron-bridge'.",
929+
"choices": ["mbridge", "megatron-bridge"],
930+
},
931+
)
932+
933+
use_mbridge_save: bool = field(
934+
default=False,
935+
metadata={
936+
"help": "Use mbridge's save method to save gpu memory when saving weights."
937+
},
938+
)
939+
924940

925941
@dataclass
926942
class MindSpeedEngineConfig:
@@ -976,22 +992,6 @@ class MindSpeedEngineConfig:
976992
def as_dict(self) -> dict[str, Any]:
977993
return asdict(self)
978994

979-
# Bridge backend used for HF<->Megatron conversion/model creation.
980-
bridge_type: str = field(
981-
default="mbridge",
982-
metadata={
983-
"help": "Bridge backend for MegatronEngine. Choices: 'mbridge' or 'megatron-bridge'.",
984-
"choices": ["mbridge", "megatron-bridge"],
985-
},
986-
)
987-
988-
use_mbridge_save: bool = field(
989-
default=False,
990-
metadata={
991-
"help": "Use mbridge's save method to save gpu memory when saving weights."
992-
},
993-
)
994-
995995

996996
class SchedulingStrategyType(str, Enum):
997997
separation = "separation"

areal/engine/megatron_engine.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ def _ms_sanitized_get_full_args():
4444
import mbridge
4545
import torch
4646
import torch.distributed as dist
47-
from megatron.bridge import AutoBridge as MegatronBridgeAutoBridge
48-
from megatron.bridge.peft.lora import LoRA as MegatronBridgeLoRA
4947
from megatron.core import parallel_state as mpu
5048
from megatron.core import tensor_parallel
5149
from megatron.core.distributed import DistributedDataParallel as DDP
@@ -109,7 +107,7 @@ def _ms_sanitized_get_full_args():
109107
configure_pipeline_layer_splits,
110108
)
111109
from areal.infra.dist_rollout import DistRolloutCoordinator
112-
from areal.infra.platforms import current_platform
110+
from areal.infra.platforms import current_platform, is_npu_available
113111
from areal.models.mcore.hf_load import load_weights_from_hf_with_mbridge_fast
114112
from areal.models.mcore.hf_save import (
115113
save_critic_value_head,
@@ -268,6 +266,12 @@ def __init__(self, config: TrainEngineConfig):
268266
)
269267
self.quantization_config: dict[str, int | str | list[str]] | None = None
270268
self.bridge_cls: str = getattr(self.mcore_config, "bridge_type", "mbridge")
269+
if self.bridge_cls == "megatron-bridge" and is_npu_available:
270+
raise ValueError(
271+
"bridge_type='megatron-bridge' is not supported on NPU "
272+
"(megatron-bridge has CUDA-only dependencies). "
273+
"Use bridge_type='mbridge' instead."
274+
)
271275
self.bridge_lora: MegatronBridgeLoRA | None = None
272276
self.is_vision_model: bool = False
273277
self.processor = None
@@ -335,6 +339,8 @@ def _apply_megatron_bridge_lora(self) -> None:
335339
"linear_fc1",
336340
"linear_fc2",
337341
]
342+
from megatron.bridge.peft.lora import LoRA as MegatronBridgeLoRA
343+
338344
self.bridge_lora = MegatronBridgeLoRA(
339345
target_modules=target_modules,
340346
dim=self.config.lora_rank,
@@ -619,6 +625,8 @@ def _build_hf_mcore_bridge(self):
619625
raise NotImplementedError(
620626
"Tree training is not supported with bridge_type='megatron-bridge'."
621627
)
628+
from megatron.bridge import AutoBridge as MegatronBridgeAutoBridge
629+
622630
self.bridge = MegatronBridgeAutoBridge.from_hf_pretrained(
623631
self.config.path,
624632
trust_remote_code=True,

areal/engine/megatron_utils/megatron_lora.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,11 @@ def _build_adapter_config_dict(
277277

278278
def _monkey_patch_save_hf_adapter():
279279
"""Add save_hf_adapter to AutoBridge when megatron-bridge does not provide it."""
280-
from megatron.bridge import AutoBridge
280+
try:
281+
from megatron.bridge import AutoBridge
282+
except ImportError:
283+
# megatron-bridge is not installed (e.g. NPU environment); nothing to patch.
284+
return
281285

282286
if hasattr(AutoBridge, "save_hf_adapter"):
283287
# Already exists, no need to patch

docs/en/cli_reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ Refer to Megatron-LM documentation for implementation details.
953953
| `num_layers_in_last_pipeline_stage` | integer \| None | `None` | Number of layers in the last pipeline stage |
954954
| `account_for_embedding_in_pipeline_split` | boolean | `False` | - |
955955
| `account_for_loss_in_pipeline_split` | boolean | `False` | - |
956+
| `bridge_type` | string | `"mbridge"` | Bridge backend for MegatronEngine. Choices: 'mbridge' or 'megatron-bridge'. **Choices:** `mbridge`, `megatron-bridge` |
956957

957958
(section-mind-speed-engine)=
958959

docs/zh/cli_reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ Refer to Megatron-LM documentation for implementation details.
951951
| `num_layers_in_last_pipeline_stage` | integer \| None | `None` | Number of layers in the last pipeline stage |
952952
| `account_for_embedding_in_pipeline_split` | boolean | `False` | - |
953953
| `account_for_loss_in_pipeline_split` | boolean | `False` | - |
954+
| `bridge_type` | string | `"mbridge"` | Bridge backend for MegatronEngine. Choices: 'mbridge' or 'megatron-bridge'. **Choices:** `mbridge`, `megatron-bridge` |
954955

955956
(section-mind-speed-engine)=
956957

0 commit comments

Comments
 (0)