[BUGFIX] Fix accuracy regression for NVIDIA-Nemotron-3-Nano-30B-A3B-NVFP4 with TP>1 (#34476)
Signed-off-by: Vadim Gimpelson <vadim.gimpelson@gmail.com>
This commit is contained in:
@@ -17,6 +17,7 @@ from vllm.forward_context import ForwardContext, get_forward_context
|
|||||||
from vllm.model_executor.custom_op import CustomOp, PluggableLayer
|
from vllm.model_executor.custom_op import CustomOp, PluggableLayer
|
||||||
from vllm.model_executor.layers.linear import (
|
from vllm.model_executor.layers.linear import (
|
||||||
ColumnParallelLinear,
|
ColumnParallelLinear,
|
||||||
|
MergedColumnParallelLinear,
|
||||||
RowParallelLinear,
|
RowParallelLinear,
|
||||||
)
|
)
|
||||||
from vllm.model_executor.layers.mamba.abstract import MambaBase
|
from vllm.model_executor.layers.mamba.abstract import MambaBase
|
||||||
@@ -301,94 +302,127 @@ class MambaMixer2(MambaBase, PluggableLayer):
|
|||||||
self.groups_ssm_state_size = self.n_groups * self.ssm_state_size
|
self.groups_ssm_state_size = self.n_groups * self.ssm_state_size
|
||||||
self.conv_dim = intermediate_size + 2 * self.groups_ssm_state_size
|
self.conv_dim = intermediate_size + 2 * self.groups_ssm_state_size
|
||||||
|
|
||||||
# Use ColumnParallelLinear with custom weight loaders for both cases:
|
if n_groups % self.tp_size == 0:
|
||||||
# - When n_groups % tp_size == 0: standard sharding without duplication
|
self.conv1d = MergedColumnParallelLinear(
|
||||||
# - When n_groups == 1: groups are duplicated across TP ranks
|
input_size=conv_kernel_size,
|
||||||
# The custom weight loader handles both cases correctly.
|
output_sizes=[
|
||||||
|
intermediate_size,
|
||||||
|
self.groups_ssm_state_size,
|
||||||
|
self.groups_ssm_state_size,
|
||||||
|
],
|
||||||
|
bias=use_conv_bias,
|
||||||
|
quant_config=None,
|
||||||
|
prefix=f"{prefix}.conv1d",
|
||||||
|
)
|
||||||
|
|
||||||
self.conv1d = ColumnParallelLinear(
|
self.in_proj = MergedColumnParallelLinear(
|
||||||
input_size=conv_kernel_size,
|
input_size=hidden_size,
|
||||||
output_size=self.conv_dim,
|
output_sizes=[
|
||||||
bias=use_conv_bias,
|
intermediate_size,
|
||||||
quant_config=None,
|
intermediate_size,
|
||||||
prefix=f"{prefix}.conv1d",
|
self.groups_ssm_state_size,
|
||||||
)
|
self.groups_ssm_state_size,
|
||||||
|
self.num_heads,
|
||||||
self.in_proj = ColumnParallelLinear(
|
],
|
||||||
input_size=hidden_size,
|
bias=use_bias,
|
||||||
output_size=intermediate_size + self.conv_dim + self.num_heads,
|
quant_config=quant_config,
|
||||||
bias=use_bias,
|
prefix=f"{prefix}.in_proj",
|
||||||
quant_config=quant_config,
|
)
|
||||||
prefix=f"{prefix}.in_proj",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Configure shard settings for the custom weight loader:
|
|
||||||
# - group_shard_settings handles group duplication when n_groups == 1
|
|
||||||
# - When n_groups % tp_size == 0, extra=0 and duplicate_groups=False
|
|
||||||
group_shard_settings = (
|
|
||||||
self.groups_ssm_state_size, # expected model size
|
|
||||||
(self.n_groups - n_groups) * self.ssm_state_size, # extra dims assigned
|
|
||||||
n_groups == 1, # duplicate groups when n_groups == 1
|
|
||||||
)
|
|
||||||
intermediate_settings = (intermediate_size, 0, False)
|
|
||||||
head_settings = (self.num_heads, 0, False)
|
|
||||||
|
|
||||||
# Apply custom weight loaders for conv1d (bias and weight)
|
|
||||||
delattr(self.conv1d.bias, "weight_loader")
|
|
||||||
set_weight_attrs(
|
|
||||||
self.conv1d.bias,
|
|
||||||
{
|
|
||||||
"weight_loader": mamba_v2_sharded_weight_loader(
|
|
||||||
[
|
|
||||||
intermediate_settings,
|
|
||||||
group_shard_settings,
|
|
||||||
group_shard_settings,
|
|
||||||
],
|
|
||||||
self.tp_size,
|
|
||||||
tp_rank,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
delattr(self.conv1d.weight, "weight_loader")
|
|
||||||
set_weight_attrs(
|
|
||||||
self.conv1d.weight,
|
|
||||||
{
|
|
||||||
"weight_loader": mamba_v2_sharded_weight_loader(
|
|
||||||
[
|
|
||||||
intermediate_settings,
|
|
||||||
group_shard_settings,
|
|
||||||
group_shard_settings,
|
|
||||||
],
|
|
||||||
self.tp_size,
|
|
||||||
tp_rank,
|
|
||||||
)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
# Create the custom weight loader for in_proj
|
|
||||||
mamba_loader = mamba_v2_sharded_weight_loader(
|
|
||||||
[
|
|
||||||
intermediate_settings, # for gate
|
|
||||||
intermediate_settings,
|
|
||||||
group_shard_settings,
|
|
||||||
group_shard_settings,
|
|
||||||
head_settings, # for dt
|
|
||||||
],
|
|
||||||
self.tp_size,
|
|
||||||
tp_rank,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Apply the custom weight loader to in_proj.weight
|
|
||||||
# Works for both non-quantized (Parameter) and quantized
|
|
||||||
# (ModelWeightParameter which extends BasevLLMParameter)
|
|
||||||
if isinstance(self.in_proj.weight, BasevLLMParameter):
|
|
||||||
# For BasevLLMParameter subclasses (quantized layers like FP8)
|
|
||||||
self.in_proj.weight.weight_loader = mamba_loader
|
|
||||||
else:
|
else:
|
||||||
# For standard Parameter (non-quantized layers)
|
# This is the n_groups == 1 case,
|
||||||
delattr(self.in_proj.weight, "weight_loader")
|
# where we need to duplicate groups if TP>1.
|
||||||
set_weight_attrs(self.in_proj.weight, {"weight_loader": mamba_loader})
|
|
||||||
|
self.conv1d = ColumnParallelLinear(
|
||||||
|
input_size=conv_kernel_size,
|
||||||
|
output_size=self.conv_dim,
|
||||||
|
bias=use_conv_bias,
|
||||||
|
quant_config=None,
|
||||||
|
prefix=f"{prefix}.conv1d",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.in_proj = ColumnParallelLinear(
|
||||||
|
input_size=hidden_size,
|
||||||
|
output_size=intermediate_size + self.conv_dim + self.num_heads,
|
||||||
|
bias=use_bias,
|
||||||
|
quant_config=quant_config,
|
||||||
|
prefix=f"{prefix}.in_proj",
|
||||||
|
)
|
||||||
|
|
||||||
|
# - because in_proj is a concatenation of 3 weights, we
|
||||||
|
# need to interleave them before sharding
|
||||||
|
# - use the custom weight loader mamba_v2_sharded_weight_loader
|
||||||
|
# for conv1d.bias, covn1d.weight and in_proj.weight
|
||||||
|
# - need to set these settings, to assign the groups
|
||||||
|
# to the head shards
|
||||||
|
group_shard_settings = (
|
||||||
|
self.groups_ssm_state_size, # expected model size
|
||||||
|
(self.n_groups - n_groups) * self.ssm_state_size, # extra dims assigned
|
||||||
|
n_groups == 1, # if there was only one group
|
||||||
|
)
|
||||||
|
intermediate_settings = (intermediate_size, 0, False)
|
||||||
|
head_settings = (self.num_heads, 0, False)
|
||||||
|
|
||||||
|
# - the weight already has a "weight_loader" attribute
|
||||||
|
# which set_weight_attrs will raise if we do not
|
||||||
|
# delete before trying to override it
|
||||||
|
# - ditto for the other two weights below
|
||||||
|
delattr(self.conv1d.bias, "weight_loader")
|
||||||
|
set_weight_attrs(
|
||||||
|
self.conv1d.bias,
|
||||||
|
{
|
||||||
|
"weight_loader": mamba_v2_sharded_weight_loader(
|
||||||
|
[
|
||||||
|
intermediate_settings,
|
||||||
|
group_shard_settings,
|
||||||
|
group_shard_settings,
|
||||||
|
],
|
||||||
|
self.tp_size,
|
||||||
|
tp_rank,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
delattr(self.conv1d.weight, "weight_loader")
|
||||||
|
set_weight_attrs(
|
||||||
|
self.conv1d.weight,
|
||||||
|
{
|
||||||
|
"weight_loader": mamba_v2_sharded_weight_loader(
|
||||||
|
[
|
||||||
|
intermediate_settings,
|
||||||
|
group_shard_settings,
|
||||||
|
group_shard_settings,
|
||||||
|
],
|
||||||
|
self.tp_size,
|
||||||
|
tp_rank,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create the custom weight loader for Mamba sharding with group
|
||||||
|
# replication. This handles the interleaved projections correctly.
|
||||||
|
mamba_loader = mamba_v2_sharded_weight_loader(
|
||||||
|
[
|
||||||
|
intermediate_settings, # for gate
|
||||||
|
intermediate_settings,
|
||||||
|
group_shard_settings,
|
||||||
|
group_shard_settings,
|
||||||
|
head_settings, # for dt
|
||||||
|
],
|
||||||
|
self.tp_size,
|
||||||
|
tp_rank,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Apply the custom weight loader to in_proj.weight
|
||||||
|
# Works for both non-quantized (Parameter) and quantized
|
||||||
|
# (ModelWeightParameter which extends BasevLLMParameter)
|
||||||
|
if isinstance(self.in_proj.weight, BasevLLMParameter):
|
||||||
|
# For BasevLLMParameter subclasses (quantized layers like FP8)
|
||||||
|
# These have a weight_loader property that can be directly set
|
||||||
|
self.in_proj.weight.weight_loader = mamba_loader
|
||||||
|
else:
|
||||||
|
# For standard Parameter (non-quantized layers)
|
||||||
|
delattr(self.in_proj.weight, "weight_loader")
|
||||||
|
set_weight_attrs(self.in_proj.weight, {"weight_loader": mamba_loader})
|
||||||
|
|
||||||
# unsqueeze to fit conv1d weights shape into the linear weights shape.
|
# unsqueeze to fit conv1d weights shape into the linear weights shape.
|
||||||
# Can't do this in `weight_loader` since it already exists in
|
# Can't do this in `weight_loader` since it already exists in
|
||||||
|
|||||||
Reference in New Issue
Block a user