Support nvidia/NVIDIA-Nemotron-Nano-9B-v2-FP8/NVFP4 (#11866)
This commit is contained in:
@@ -180,11 +180,12 @@ def _get_quantization_config(
|
||||
model_config: ModelConfig,
|
||||
load_config: LoadConfig,
|
||||
packed_modules_mapping: Dict[str, List[str]],
|
||||
remap_prefix: Dict[str, str] | None = None,
|
||||
) -> Optional[QuantizationConfig]:
|
||||
"""Get the quantization config."""
|
||||
if model_config.quantization is not None:
|
||||
quant_config = get_quant_config(
|
||||
model_config, load_config, packed_modules_mapping
|
||||
model_config, load_config, packed_modules_mapping, remap_prefix
|
||||
)
|
||||
# (yizhang2077) workaround for nvidia/Llama-4-Maverick-17B-128E-Eagle3
|
||||
if quant_config is None:
|
||||
@@ -220,6 +221,7 @@ def _initialize_model(
|
||||
"""Initialize a model with the given configurations."""
|
||||
model_class, _ = get_model_architecture(model_config)
|
||||
packed_modules_mapping = getattr(model_class, "packed_modules_mapping", {})
|
||||
remap_prefix = getattr(model_class, "remap_prefix", None)
|
||||
if _is_npu:
|
||||
packed_modules_mapping.update(
|
||||
{
|
||||
@@ -243,7 +245,7 @@ def _initialize_model(
|
||||
)
|
||||
|
||||
quant_config = _get_quantization_config(
|
||||
model_config, load_config, packed_modules_mapping
|
||||
model_config, load_config, packed_modules_mapping, remap_prefix
|
||||
)
|
||||
|
||||
# Build kwargs conditionally
|
||||
|
||||
@@ -37,7 +37,10 @@ from sglang.srt.configs.model_config import ModelConfig
|
||||
from sglang.srt.distributed import get_tensor_model_parallel_rank
|
||||
from sglang.srt.layers.dp_attention import get_attention_tp_rank
|
||||
from sglang.srt.layers.quantization import QuantizationConfig, get_quantization_config
|
||||
from sglang.srt.layers.quantization.modelopt_quant import ModelOptFp4Config
|
||||
from sglang.srt.layers.quantization.modelopt_quant import (
|
||||
ModelOptFp4Config,
|
||||
ModelOptFp8Config,
|
||||
)
|
||||
from sglang.srt.utils import find_local_repo_dir, log_info_on_rank0, print_warning_once
|
||||
from sglang.utils import is_in_ci
|
||||
|
||||
@@ -135,11 +138,26 @@ def convert_bin_to_safetensor_file(
|
||||
raise RuntimeError(f"The output tensors do not match for key {k}")
|
||||
|
||||
|
||||
def replace_prefix(key: str, prefix_mapping: dict[str, str]) -> str:
|
||||
for prefix, new_prefix in prefix_mapping.items():
|
||||
if key.startswith(prefix):
|
||||
key = key.replace(prefix, new_prefix, 1)
|
||||
return key
|
||||
|
||||
|
||||
def replace_substrings(key: str, substring_mapping: dict[str, str]) -> str:
|
||||
for substr, new_substr in substring_mapping.items():
|
||||
if substr in key:
|
||||
key = key.replace(substr, new_substr)
|
||||
return key
|
||||
|
||||
|
||||
# TODO(woosuk): Move this to other place.
|
||||
def get_quant_config(
|
||||
model_config: ModelConfig,
|
||||
load_config: LoadConfig,
|
||||
packed_modules_mapping: Dict[str, List[str]],
|
||||
remap_prefix: Dict[str, str] | None = None,
|
||||
) -> QuantizationConfig:
|
||||
quant_cls = get_quantization_config(model_config.quantization)
|
||||
|
||||
@@ -209,38 +227,33 @@ def get_quant_config(
|
||||
quant_config_file = quant_config_files[0]
|
||||
with open(quant_config_file) as f:
|
||||
config = json.load(f)
|
||||
if remap_prefix is not None:
|
||||
exclude_modules = [
|
||||
replace_prefix(key, remap_prefix)
|
||||
for key in config["quantization"]["exclude_modules"]
|
||||
]
|
||||
config["quantization"]["exclude_modules"] = exclude_modules
|
||||
config["packed_modules_mapping"] = packed_modules_mapping
|
||||
|
||||
if model_config.quantization == "bitsandbytes":
|
||||
config["adapter_name_or_path"] = model_name_or_path
|
||||
elif model_config.quantization == "modelopt":
|
||||
if config["producer"]["name"] == "modelopt":
|
||||
elif model_config.quantization.startswith("modelopt") and (
|
||||
config["producer"]["name"].startswith("modelopt")
|
||||
):
|
||||
quant_algo = config["quantization"]["quant_algo"]
|
||||
if quant_algo is None:
|
||||
# (yizhang2077) workaround for nvidia/Llama-4-Maverick-17B-128E-Eagle3
|
||||
if config["quantization"]["quant_algo"] is None:
|
||||
if (
|
||||
model_config.hf_config.architectures[0]
|
||||
!= "LlamaForCausalLMEagle3"
|
||||
):
|
||||
raise ValueError(
|
||||
f"Invalid quant_config, quantization method: {model_config.quantization},"
|
||||
f"hf architectures: {model_config.hf_config.architectures[0]}. "
|
||||
)
|
||||
return None
|
||||
if "FP4" in config["quantization"]["quant_algo"]:
|
||||
return ModelOptFp4Config.from_config(config)
|
||||
else:
|
||||
return quant_cls.from_config(config)
|
||||
elif model_config.quantization == "modelopt_fp8":
|
||||
if config["producer"]["name"] == "modelopt_fp8":
|
||||
return quant_cls.from_config(config)
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Unsupported quantization config"
|
||||
f" found for {model_config.quantization} in {f}."
|
||||
)
|
||||
elif model_config.quantization == "w8a8_int8":
|
||||
config["packed_modules_mapping"] = packed_modules_mapping
|
||||
|
||||
return quant_cls.from_config(config)
|
||||
if model_config.hf_config.architectures[0] != "LlamaForCausalLMEagle3":
|
||||
raise ValueError(
|
||||
f"Invalid quant_config, quantization method: {model_config.quantization},"
|
||||
f"hf architectures: {model_config.hf_config.architectures[0]}. "
|
||||
)
|
||||
return None
|
||||
elif quant_algo == "FP8" or model_config.quantization == "modelopt_fp8":
|
||||
return ModelOptFp8Config.from_config(config)
|
||||
elif "FP4" in quant_algo:
|
||||
return ModelOptFp4Config.from_config(config)
|
||||
return quant_cls.from_config(config)
|
||||
|
||||
|
||||
def find_local_hf_snapshot_dir(
|
||||
|
||||
Reference in New Issue
Block a user