vllm.model_executor.layers.linear
¶
Classes:
-
ColumnParallelLinear–Linear layer with column parallelism.
-
DCPGroupColumnParallelLinear–Column-parallel linear whose weight is sharded across DCP groups.
-
KimiK3MergedQKVGateLinear–Kimi-K3 QKV-A projection fused with its output-gate projection.
-
LinearBase–Base linear layer.
-
LinearMethodBase–Base class for different (maybe quantized) linear methods.
-
MergedColumnParallelLinear–Packed linear layers with column parallelism.
-
MinimaxM3QKVParallelLinearWithIndexer–QKV projection fused with a lightning-indexer's index_q/index_k.
-
QKVParallelLinear–Linear layers for the attention's QKV transformation.
-
ReplicatedLinear–Replicated linear layer.
-
RowParallelLinear–Linear layer with row parallelism.
-
StackedColumnParallelLinear–Column-parallel linear whose weight is several checkpoint tensors stacked
-
UnquantizedLinearMethod–Linear method without quantization.
Functions:
-
adjust_scalar_to_fused_array–For fused modules (QKV and MLP) we have an array of length
-
apply_flattened–Call
fnonxflattened to 2D, if that can be done without a copy. -
register_weight_loader_v2_supported_method–Decorator to register a LinearMethod as supporting weight_loader_v2.
ColumnParallelLinear
¶
Bases: LinearBase
Linear layer with column parallelism.
The linear layer is defined as Y = XA + b. A is parallelized along its second dimension as A = [A_1, ..., A_p].
Parameters:
-
(input_size¶int) –first dimension of matrix A.
-
(output_size¶int) –second dimension of matrix A.
-
(bias¶bool, default:True) –If true, add bias.
-
(gather_output¶bool, default:False) –If true, call all-gather on output and make Y available to all GPUs, otherwise, every GPU will have its output which is Y_i = XA_i
-
(skip_bias_add¶bool, default:False) –This was added to enable performance optimizations where bias can be fused with other element-wise operations. we skip adding bias but instead return it.
-
(params_dtype¶dtype | None, default:None) –Data type for the parameters.
-
(quant_config¶QuantizationConfig | None, default:None) –Quantization configure.
-
(prefix¶str, default:'') –The name of the layer in the state dict, including all parents (e.g. model.layers.0.qkv_proj)
-
(return_bias¶bool, default:True) –If true, return bias together with outputs in forward pass.
-
(disable_tp¶bool, default:False) –If true, weights matrix won't be sharded through tp rank.
-
(tp_rank¶int | None, default:None) –Override the tensor-parallel rank used for sharding. Defaults to the global TP rank. Used to shard at a coarser granularity than one shard per rank (see
DCPGroupColumnParallelLinear). -
(tp_size¶int | None, default:None) –Override the tensor-parallel world size used for sharding. Defaults to the global TP world size.
DCPGroupColumnParallelLinear
¶
Bases: ColumnParallelLinear
Column-parallel linear whose weight is sharded across DCP groups.
With Decode Context Parallelism (DCP) the KV cache is sharded across a DCP
group, so MLA decode must attend the group's full head set. This layer shards
its output across DCP groups (effective tp size tp_size //
dcp_world_size) rather than across every rank, so each rank in a group
holds the whole group's heads, letting decode skip the query all-gather.
forward returns the group's full head set. _local_view
extracts this rank's TP shard for prefill.
_local_view(out)
¶
Slice this rank's tp head shard from a group-heads output.
out is head-shaped, i.e. (..., group_heads, head_dim).
KimiK3MergedQKVGateLinear
¶
Bases: MergedColumnParallelLinear
Kimi-K3 QKV-A projection fused with its output-gate projection.
NOTE: Kimi-K3-specific. This is tailored to its replicated Q/KV latent
projections and tensor-parallel output gate; it is not a general-purpose
linear layer. It lives here alongside MergedColumnParallelLinear, whose
sharding and weight-loading machinery it reuses.
The per-rank shard layout is [q_a | kv_a | gate]. The latent shards
stay replicated across tensor-parallel ranks while the gate shard is
tensor-parallel, matching the standalone projections they replace.
LinearBase
¶
Bases: PluggableLayer
Base linear layer.
Parameters:
-
(input_size¶int) –input dimension of the linear layer.
-
(output_size¶int) –output dimension of the linear layer.
-
(skip_bias_add¶bool, default:False) –If true, skip adding bias but instead return it.
-
(params_dtype¶dtype | None, default:None) –Data type for the parameters.
-
(quant_config¶QuantizationConfig | None, default:None) –Quantization configure.
-
(prefix¶str, default:'') –Prefix for parameter names.
-
(return_bias¶bool, default:True) –If true, return bias together with outputs in forward pass.
-
(disable_tp¶bool, default:False) –If true, tensor parallelism will be disabled for this layer.
LinearMethodBase
¶
Bases: QuantizeMethodBase
Base class for different (maybe quantized) linear methods.
Methods:
-
apply–Apply the weights in layer to the input tensor.
-
create_weights–Create weights for a linear layer.
apply(layer, x, bias=None)
abstractmethod
¶
Apply the weights in layer to the input tensor. Expects create_weights to have been called before on the layer.
create_weights(layer, input_size_per_partition, output_partition_sizes, input_size, output_size, params_dtype, **extra_weight_attrs)
abstractmethod
¶
Create weights for a linear layer. The weights will be set as attributes of the layer.
Parameters:
-
(layer¶Module) –The layer that is using the LinearMethodBase factory.
-
(input_size_per_partition¶int) –Size of the weight input dim on rank X.
-
(output_partition_sizes¶list[int]) –Sizes of the output dim of each logical weight on rank X. E.g., output_partition_sizes for QKVLinear is a list contains the width of Wq, Wk, Wv on rank X.
-
(input_size¶int) –Size of the input dim of the weight across all ranks.
-
(output_size¶int) –Size of the output dim of the weight across all ranks.
-
(params_dtype¶dtype) –Datatype of the parameters.
MergedColumnParallelLinear
¶
Bases: StackedColumnParallelLinear
Packed linear layers with column parallelism.
Similar to ColumnParallelLinear, but the weight matrix is concatenated along the output dimension. When the weight matrix is loaded, the different partitions are sharded separately.
Parameters:
-
(input_size¶int) –input dimension of the linear layer.
-
(output_sizes¶list[int]) –list of output dimensions of the linear layer.
-
(bias¶bool, default:True) –If true, add bias.
-
(gather_output¶bool, default:False) –If true, call all-gather on output and make the output available to all GPUs, otherwise, every GPU will have its own output.
-
(skip_bias_add¶bool, default:False) –This was added to enable performance optimizations where bias can be fused with other element-wise operations. we skip adding bias but instead return it.
-
(params_dtype¶dtype | None, default:None) –Data type for the parameters.
-
(quant_config¶QuantizationConfig | None, default:None) –Quantization configure.
-
(prefix¶str, default:'') –The name of the layer in the state dict, including all parents (e.g. model.layers.0.qkv_proj)
-
(return_bias¶bool, default:True) –If true, return bias together with outputs in forward pass.
-
(disable_tp¶bool, default:False) –If true, all weights matrix won't be sharded, this layer will be treated as a "Replicated" MergedLinear.
-
(padding_size¶int, default:0) –Extra output rows on each TP rank, appended after the real output_sizes. These rows are zeroed and are not checkpoint shards.
_load_fused_module_from_checkpoint(param, loaded_weight, output_sizes=None)
¶
Handle special case for models where MLP layers are already fused on disk. In this case, we have no shard id. This function determines the shard id by splitting these layers and then calls the weight loader using the shard id.
An example of a model with these fused layers: https://huggingface.co/microsoft/Phi-3-mini-4k-instruct
MinimaxM3QKVParallelLinearWithIndexer
¶
Bases: QKVParallelLinear
QKV projection fused with a lightning-indexer's index_q/index_k.
NOTE: MiniMax-M3-specific. This is tailored to the M3 sparse-attention layers (it assumes the indexer's head count equals the KV head count and shares the main head_dim); it is not a general-purpose linear layer. It lives here only to sit alongside QKVParallelLinear, whose sharding / weight-loading machinery it reuses.
A single column-parallel GEMM emits, per rank::
[q | k | v | index_q | index_k]
index_q must have the same head count as the KV heads
(total_num_index_heads == total_num_kv_heads) and index_head_size ==
head_size, so it shards exactly like K/V -- including the KV-head
replication path when tp_size > total_num_kv_heads (this is what makes
a TP size greater than the KV-head count work). index_k is a single
shared head, replicated to every rank.
replicate_index_q moves index_q onto that replicated path too, so
every rank projects all the index heads rather than its own. That is what
indexer context parallelism needs: it inverts the split, scoring every head
against a shard of the blocks instead of one head against all of them.
QKVParallelLinear
¶
Bases: StackedColumnParallelLinear
Linear layers for the attention's QKV transformation.
Linear layers for the linear transformation of the query, key, and value vectors in the attention layer. The weight matrix is concatenated along the output dimension. The layer is parallelized along the head dimension. When the number of key/value heads is smaller than the number of query heads (e.g., multi-query/grouped-query attention), the key/value head may be replicated while the query heads are partitioned.
Parameters:
-
(hidden_size¶int) –input hidden state size of the transformer.
-
(head_size¶int) –size of each attention head.
-
(total_num_heads¶int) –total number of attention query heads.
-
(total_num_kv_heads¶int | None, default:None) –total number of attention key/value heads. If None, assume total_num_kv_heads = total_num_heads.
-
(bias¶bool, default:True) –If true, add bias.
-
(skip_bias_add¶bool, default:False) –This was added to enable performance optimizations where bias can be fused with other element-wise operations. we skip adding bias but instead return it.
-
(params_dtype¶dtype | None, default:None) –Data type for the parameters.
-
(quant_config¶QuantizationConfig | None, default:None) –Quantization configure.
-
(prefix¶str, default:'') –The name of the layer in the state dict, including all parents (e.g. model.layers.0.qkv_proj)
-
(return_bias¶bool, default:True) –If true, return bias together with outputs in forward pass.
-
(disable_tp¶bool, default:False) –If true, weights matrix won't be sharded through tp rank.
_load_fused_module_from_checkpoint(param, loaded_weight)
¶
Handle special case for models where QKV layers are already fused on disk. In this case, we have no shard id. This function determines the shard id by splitting these layers and then calls the weight loader using the shard id.
An example of a model with these fused layers: https://huggingface.co/microsoft/Phi-3-mini-4k-instruct
ReplicatedLinear
¶
Bases: LinearBase
Replicated linear layer.
Parameters:
-
(input_size¶int) –input dimension of the linear layer.
-
(output_size¶int) –output dimension of the linear layer.
-
(bias¶bool, default:True) –If true, add bias.
-
(skip_bias_add¶bool, default:False) –If true, skip adding bias but instead return it.
-
(params_dtype¶dtype | None, default:None) –Data type for the parameters.
-
(quant_config¶QuantizationConfig | None, default:None) –Quantization configure.
-
(prefix¶str, default:'') –The name of the layer in the state dict, including all parents (e.g. model.layers.0.qkv_proj)
-
(return_bias¶bool, default:True) –If true, return bias together with outputs in forward pass.
-
(disable_tp¶bool, default:False) –Take no effect for replicated linear layers.
RowParallelLinear
¶
Bases: LinearBase
Linear layer with row parallelism.
The linear layer is defined as Y = XA + b. A is parallelized along its first dimension and X along its second dimension as: - - | A_1 | | . | A = | . | X = [X_1, ..., X_p] | . | | A_p | - -
Parameters:
-
(input_size¶int) –first dimension of matrix A.
-
(output_size¶int) –second dimension of matrix A.
-
(bias¶bool, default:True) –If true, add bias. Note that bias is not parallelized.
-
(input_is_parallel¶bool, default:True) –If true, we assume that the input is already split across the GPUs and we do not split again.
-
(skip_bias_add¶bool, default:False) –This was added to enable performance optimization where bias can be fused with other element-wise operations. We skip adding bias but instead return it.
-
(params_dtype¶dtype | None, default:None) –Data type for the parameters.
-
(reduce_results¶bool, default:True) –If true, call all-reduce on output and make Y available to all GPUs, otherwise, every GPU will have its output which is Y = X_iA_i
-
(quant_config¶QuantizationConfig | None, default:None) –Quantization configure.
-
(prefix¶str, default:'') –The name of the layer in the state dict, including all parents (e.g. model.layers.0.down_proj)
-
(return_bias¶bool, default:True) –If true, return bias together with outputs in forward pass.
-
(disable_tp¶bool, default:False) –If true, weights matrix won't be sharded through tp rank.
StackedColumnParallelLinear
¶
Bases: ColumnParallelLinear
Column-parallel linear whose weight is several checkpoint tensors stacked along the output dimension, each addressed by a shard id.
Keeps track of which shards a load delivered to each parameter so the loader can reject a parameter that was only partly filled.
Methods:
-
pop_missing_shards–<param>.<shard>for each shard this load never filled, then forget -
validate_shard_id–Check
shard_idnames shards of this layer and, ifparamis given,
Attributes:
shard_ids
instance-attribute
¶
Every shard this layer can receive.
_get_shard_size_mapping(loaded_shard_id)
¶
Rows of this rank's parameter that belong to the shard.
pop_missing_shards()
¶
<param>.<shard> for each shard this load never filled, then forget
the record so the next load starts fresh.
A shard with no rows on this rank is never expected, and a parameter nothing was loaded into is left to the loader's parameter-level check.
validate_shard_id(shard_id, param=None)
¶
Check shard_id names shards of this layer and, if param is given,
record them as loaded into it.
UnquantizedLinearMethod
¶
adjust_scalar_to_fused_array(param_data, loaded_weight, shard_id)
¶
For fused modules (QKV and MLP) we have an array of length N that holds 1 scale for each "logical" matrix. So the param is an array of length N. The loaded_weight corresponds to one of the shards on disk. Here, we slice the param based on the shard_id for loading.
apply_flattened(fn, x)
¶
Call fn on x flattened to 2D, if that can be done without a copy.
Linear methods and kernels may assume 2D activations, but callers such as
the Transformers modeling backend pass [batch, seq, hidden] activations.
register_weight_loader_v2_supported_method(cls)
¶
Decorator to register a LinearMethod as supporting weight_loader_v2.