Skip to content
Closed

Test #26

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ uv pip install -e . --torch-backend=auto

保存的模型,可以参考[模型卡片的示例代码](https://modelscope.cn/models/Qwen/Qwen3.5-35B-A3B)进行推理。


```python
# test env: transformers==5.2.0 megatron-core==0.16.1
import os
Expand Down
4 changes: 2 additions & 2 deletions src/mcore_bridge/bridge/gpt_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def _set_module(self, mg_module, hf_state_dict, hf_prefix: str, to_mcore: bool):
new_state_dict = {}
for k, v in hf_state_dict.items():
if self._peft_format:
if '.lora_A.' in k or '.lora_B.' in k or '.modules_to_save.' in k:
if ('.lora_A.' in k or '.lora_B.' in k or '.modules_to_save.' in k) and f'{self._adapter_name}.' in k:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The check f'{self._adapter_name}.' in k is potentially too broad and could lead to incorrect matches if one adapter name is a prefix of another (e.g., default and default_new). Since keys are dot-separated, it is safer to include the leading dot in the check to ensure it matches the specific adapter component.

Suggested change
if ('.lora_A.' in k or '.lora_B.' in k or '.modules_to_save.' in k) and f'{self._adapter_name}.' in k:
if ('.lora_A.' in k or '.lora_B.' in k or '.modules_to_save.' in k) and f'.{self._adapter_name}.' in k:

k = k.replace(f'{self._adapter_name}.', '')
new_state_dict[k] = v
else:
Expand Down Expand Up @@ -1703,7 +1703,7 @@ def export_weights(
self.config = mg_models[0].config
with torch.no_grad():
for k, v in self._convert(mg_models, {}, hf_prefix, False, tqdm_desc=tqdm_desc):
if converter:
if converter and v is not None:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Skipping the converter when v is None causes inconsistent key names across different ranks in a distributed environment. When only_master_rank=True is used, non-master ranks yield v=None. If the converter is skipped on these ranks, they will yield the original Megatron keys, while the master rank yields the converted (HuggingFace) keys. This mismatch can break downstream components that expect consistent keys across all ranks. The converter should be called even when v is None, and it is the responsibility of the converter implementation to handle None values.

Suggested change
if converter and v is not None:
if converter:

kv = converter(k, v)
if kv is None:
continue
Expand Down