初始化项目,由ModelHub XC社区提供模型
Model: ayh015/myLightningOPD Source: Original Platform
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import torch
|
||||
|
||||
from slime.backends.megatron_utils.misc_utils import strip_param_name_prefix
|
||||
|
||||
|
||||
def remove_padding(name: str, param: torch.Tensor, vocab_size: int) -> torch.Tensor:
|
||||
"""
|
||||
Remove vocab padding: param[:vocab_size] for embedding/output layers, else unchanged.
|
||||
"""
|
||||
if strip_param_name_prefix(name) in {"embedding.word_embeddings.weight", "output_layer.weight"}:
|
||||
return param[:vocab_size]
|
||||
return param
|
||||
@@ -0,0 +1,110 @@
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import re
|
||||
|
||||
import torch
|
||||
|
||||
from slime.utils.fp8_kernel import blockwise_cast_to_fp8_triton
|
||||
|
||||
from ...sglang import quant_weight_ue8m0, should_deepgemm_weight_requant_ue8m0, transform_scale_ue8m0
|
||||
|
||||
|
||||
def quantize_params(args, megatron_name, converted_named_params, quantization_config):
|
||||
if quantization_config is None:
|
||||
return converted_named_params
|
||||
assert quantization_config["quant_method"] == "fp8"
|
||||
assert quantization_config["fmt"] == "e4m3"
|
||||
assert quantization_config["activation_scheme"] == "dynamic"
|
||||
weight_block_size = quantization_config.get("weight_block_size", None)
|
||||
|
||||
decoder_layers_pattern = r"module\.module\.decoder\.layers\.(\d+)\.(.+)"
|
||||
match = re.match(decoder_layers_pattern, megatron_name)
|
||||
|
||||
if not match:
|
||||
# check mtp layers
|
||||
mtp_layer_pattern = r"module\.module\.mtp\.layers\.(\d+)\.(.+)"
|
||||
match = re.match(mtp_layer_pattern, megatron_name)
|
||||
if not match:
|
||||
return converted_named_params
|
||||
layer_idx, rest = match.groups()
|
||||
rest = rest.replace("transformer_layer.", "")
|
||||
else:
|
||||
layer_idx, rest = match.groups()
|
||||
|
||||
# experts
|
||||
expert_pattern = r"mlp.experts\.(.+)\.weight(\d+)"
|
||||
match = re.match(expert_pattern, rest)
|
||||
if match:
|
||||
rest, expert_idx = match.groups()
|
||||
if rest in [
|
||||
"linear_fc1",
|
||||
"linear_fc2",
|
||||
]:
|
||||
quantize_named_params = []
|
||||
for converted_name, param in converted_named_params:
|
||||
# skip bf16 weight_scale and input_scale
|
||||
# TODO: find a clearer way.
|
||||
if converted_name.endswith("_scale"):
|
||||
continue
|
||||
quantize_named_params.extend(_quantize_param(converted_name, param, weight_block_size))
|
||||
|
||||
return quantize_named_params
|
||||
|
||||
# shared expert
|
||||
shared_expert_pattern = r"mlp.shared_experts\.(.+)"
|
||||
match = re.match(shared_expert_pattern, rest)
|
||||
if match:
|
||||
rest = match.groups()[0]
|
||||
if rest in [
|
||||
"linear_fc1.weight",
|
||||
"linear_fc2.weight",
|
||||
]:
|
||||
quantize_named_params = []
|
||||
for converted_name, param in converted_named_params:
|
||||
quantize_named_params.extend(_quantize_param(converted_name, param, weight_block_size))
|
||||
|
||||
return quantize_named_params
|
||||
|
||||
if rest in [
|
||||
"self_attention.linear_proj.weight",
|
||||
"self_attention.linear_qkv.weight",
|
||||
"mlp.linear_fc1.weight",
|
||||
"mlp.linear_fc2.weight",
|
||||
# mla
|
||||
"self_attention.linear_q_proj.weight",
|
||||
"self_attention.linear_q_down_proj.weight",
|
||||
"self_attention.linear_q_up_proj.weight",
|
||||
"self_attention.linear_kv_down_proj.weight",
|
||||
"self_attention.linear_kv_up_proj.weight",
|
||||
]:
|
||||
quantize_named_params = []
|
||||
for converted_name, param in converted_named_params:
|
||||
quantize_named_params.extend(_quantize_param(converted_name, param, weight_block_size))
|
||||
|
||||
return quantize_named_params
|
||||
|
||||
# for other parameters, we just return the original converted_named_params
|
||||
return converted_named_params
|
||||
|
||||
|
||||
def _quantize_param(name, weight, weight_block_size):
|
||||
assert name.endswith(".weight"), f"Expected weight parameter, got {name}"
|
||||
FP8_MIN = torch.finfo(torch.float8_e4m3fn).min
|
||||
FP8_MAX = torch.finfo(torch.float8_e4m3fn).max
|
||||
if weight_block_size is not None:
|
||||
if should_deepgemm_weight_requant_ue8m0 and should_deepgemm_weight_requant_ue8m0(
|
||||
weight_block_size=weight_block_size
|
||||
):
|
||||
qweight, scale = quant_weight_ue8m0(weight, weight_block_size=weight_block_size)
|
||||
scale = transform_scale_ue8m0(scale, mn=qweight.shape[-2])
|
||||
else:
|
||||
qweight, scale = blockwise_cast_to_fp8_triton(weight, weight_block_size)
|
||||
scale_name = name.replace(".weight", ".weight_scale_inv")
|
||||
else:
|
||||
# per tensor quant
|
||||
scale = weight.abs().max().clamp(min=1e-12).to(torch.float32) / FP8_MAX
|
||||
qweight = (weight / scale).clamp(min=FP8_MIN, max=FP8_MAX).to(torch.float8_e4m3fn)
|
||||
scale = scale.view(1)
|
||||
scale_name = name.replace(".weight", ".weight_scale")
|
||||
return [(name, qweight), (scale_name, scale)]
|
||||
Reference in New Issue
Block a user