[Fix] GLM 4.7 + NVFP4 + MTP (#17166)
This commit is contained in:
@@ -96,6 +96,7 @@ from sglang.srt.model_loader.weight_utils import (
|
||||
get_quant_config,
|
||||
gguf_quant_weights_iterator,
|
||||
initialize_dummy_weights,
|
||||
maybe_add_mtp_safetensors,
|
||||
multi_thread_pt_weights_iterator,
|
||||
multi_thread_safetensors_weights_iterator,
|
||||
np_cache_weights_iterator,
|
||||
@@ -321,6 +322,9 @@ class DefaultModelLoader(BaseModelLoader):
|
||||
fall_back_to_pt: bool = True
|
||||
"""Whether .pt weights can be used."""
|
||||
|
||||
model_config: Optional["ModelConfig"] = None
|
||||
"""The model configuration (for checking architecture, etc)."""
|
||||
|
||||
@classmethod
|
||||
def init_new(cls, model_config: ModelConfig, model):
|
||||
return cls(
|
||||
@@ -328,6 +332,7 @@ class DefaultModelLoader(BaseModelLoader):
|
||||
model_config.revision,
|
||||
prefix="",
|
||||
fall_back_to_pt=getattr(model, "fall_back_to_pt_during_load", True),
|
||||
model_config=model_config,
|
||||
)
|
||||
|
||||
def __init__(self, load_config: LoadConfig):
|
||||
@@ -471,6 +476,15 @@ class DefaultModelLoader(BaseModelLoader):
|
||||
hf_folder, hf_weights_files, use_safetensors = self._prepare_weights(
|
||||
source.model_or_path, source.revision, source.fall_back_to_pt
|
||||
)
|
||||
|
||||
if use_safetensors and source.model_config is not None:
|
||||
hf_weights_files = maybe_add_mtp_safetensors(
|
||||
hf_weights_files,
|
||||
hf_folder,
|
||||
"model.safetensors.index.json",
|
||||
source.model_config.hf_config,
|
||||
)
|
||||
|
||||
if self.load_config.load_format == LoadFormat.NPCACHE:
|
||||
# Currently np_cache only support *.bin checkpoints
|
||||
assert use_safetensors is False
|
||||
|
||||
@@ -594,6 +594,44 @@ def filter_duplicate_safetensors_files(
|
||||
return hf_weights_files
|
||||
|
||||
|
||||
def maybe_add_mtp_safetensors(
|
||||
hf_weights_files: List[str], hf_folder: str, index_file: str, hf_config
|
||||
) -> List[str]:
|
||||
"""
|
||||
Auto-detect and add mtp.safetensors for GLM4Moe MTP/NextN models if:
|
||||
1. mtp.safetensors exists in the model directory
|
||||
2. mtp.safetensors is NOT in the index (checkpoint packaging bug)
|
||||
3. Model architecture is Glm4MoeForCausalLM with num_nextn_predict_layers > 0
|
||||
|
||||
This works around incorrectly packaged FP4 checkpoints like
|
||||
baseten-admin/glm-4.7-fp4 where mtp.safetensors exists but
|
||||
isn't referenced in model.safetensors.index.json.
|
||||
"""
|
||||
# Only apply for GLM4Moe architecture with nextn layers
|
||||
arch = getattr(hf_config, "architectures", [None])[0]
|
||||
num_nextn_layers = getattr(hf_config, "num_nextn_predict_layers", 0)
|
||||
if not (
|
||||
arch in ["Glm4MoeForCausalLM", "Glm4MoeForCausalLMNextN"]
|
||||
and num_nextn_layers > 0
|
||||
):
|
||||
return hf_weights_files
|
||||
|
||||
# Check if mtp.safetensors exists and is not already in the file list
|
||||
mtp_path = os.path.join(hf_folder, "mtp.safetensors")
|
||||
if not os.path.isfile(mtp_path) or mtp_path in hf_weights_files:
|
||||
return hf_weights_files
|
||||
|
||||
# mtp.safetensors exists but not in index - this is a bug
|
||||
logger.warning(
|
||||
f"Found mtp.safetensors but it's not referenced in {index_file}. "
|
||||
f"This is a checkpoint packaging bug. Auto-adding it for loading. "
|
||||
f"Please report this to the checkpoint provider."
|
||||
)
|
||||
|
||||
# Add it to the files list
|
||||
return hf_weights_files + [mtp_path]
|
||||
|
||||
|
||||
def filter_files_not_needed_for_inference(hf_weights_files: List[str]) -> List[str]:
|
||||
"""
|
||||
Exclude files that are not needed for inference.
|
||||
|
||||
Reference in New Issue
Block a user