Avoid upstream Omni loading-info speaker crash

This commit is contained in:
Xin Wang
2026-08-27 13:50:02 +08:00
parent 5e34563739
commit d34725778f
+76 -16
View File
@@ -112,6 +112,29 @@ def _choose_dtype(
return getattr(torch, dtype) return getattr(torch, dtype)
def _local_sharded_checkpoint_keys(model_id: str) -> set[str] | None:
"""Read weight names without loading shard tensors when an index exists."""
root = Path(model_id)
if not root.is_dir():
return None
for name in (
"model.safetensors.index.json",
"pytorch_model.bin.index.json",
):
index_path = root / name
if not index_path.is_file():
continue
payload = json.loads(index_path.read_text(encoding="utf-8"))
weight_map = payload.get("weight_map")
if not isinstance(weight_map, dict):
raise RuntimeError(
f"Checkpoint index has no weight_map object: {index_path}"
)
return {str(key) for key in weight_map}
return None
def load_qwen_omni( def load_qwen_omni(
model_id: str = MODEL_ID, model_id: str = MODEL_ID,
*, *,
@@ -169,27 +192,63 @@ def load_qwen_omni(
full_model_class_name = ( full_model_class_name = (
"Qwen2_5OmniForConditionalGeneration" "Qwen2_5OmniForConditionalGeneration"
) )
supports_loading_info = False
else: else:
full_model_class = Qwen2_5OmniModel full_model_class = Qwen2_5OmniModel
full_model_class_name = "Qwen2_5OmniModel" full_model_class_name = "Qwen2_5OmniModel"
supports_loading_info = True
loaded = full_model_class.from_pretrained( if supports_loading_info:
model_id, loaded = full_model_class.from_pretrained(
output_loading_info=True, model_id,
**model_kwargs, output_loading_info=True,
) **model_kwargs,
if not isinstance(loaded, tuple) or len(loaded) != 2: )
raise RuntimeError("ROMA loading did not return checkpoint audit data") if not isinstance(loaded, tuple) or len(loaded) != 2:
full_model, loading_info = loaded raise RuntimeError(
"ROMA loading did not return checkpoint audit data"
)
full_model, loading_info = loaded
missing = [
str(key) for key in loading_info.get("missing_keys", [])
]
unexpected = [
str(key) for key in loading_info.get("unexpected_keys", [])
]
mismatched = [
str(item)
for item in loading_info.get("mismatched_keys", [])
]
errors = [
str(item) for item in loading_info.get("error_msgs", [])
]
checkpoint_audit = "transformers_output_loading_info"
else:
# Upstream Qwen2.5-Omni overrides from_pretrained() and calls
# load_speakers() on its return value. output_loading_info=True
# changes that return value into (model, info), causing the
# upstream override to crash before returning. Load normally and
# recover the unexpected-key audit from the local shard index.
full_model = full_model_class.from_pretrained(
model_id,
**model_kwargs,
)
missing = []
mismatched = []
errors = []
checkpoint_keys = _local_sharded_checkpoint_keys(model_id)
if checkpoint_keys is None:
unexpected = []
checkpoint_audit = (
"upstream_loader; local_shard_index_unavailable"
)
else:
model_keys = set(full_model.state_dict())
unexpected = sorted(checkpoint_keys - model_keys)
checkpoint_audit = (
"upstream_loader_plus_local_shard_key_audit"
)
missing = [str(key) for key in loading_info.get("missing_keys", [])]
unexpected = [
str(key) for key in loading_info.get("unexpected_keys", [])
]
mismatched = [
str(item) for item in loading_info.get("mismatched_keys", [])
]
errors = [str(item) for item in loading_info.get("error_msgs", [])]
non_gate_unexpected = [ non_gate_unexpected = [
key for key in unexpected if not key.startswith("thinker.gate_") key for key in unexpected if not key.startswith("thinker.gate_")
] ]
@@ -222,6 +281,7 @@ def load_qwen_omni(
"loader": "roma", "loader": "roma",
"full_model_class": full_model_class_name, "full_model_class": full_model_class_name,
"checkpoint_layout": "full_qwen2_5_omni", "checkpoint_layout": "full_qwen2_5_omni",
"checkpoint_audit": checkpoint_audit,
"ignored_checkpoint_keys": unexpected, "ignored_checkpoint_keys": unexpected,
"ignored_checkpoint_key_policy": "thinker.gate_*_only", "ignored_checkpoint_key_policy": "thinker.gate_*_only",
"original_speak_head": ( "original_speak_head": (