vllm.model_executor.layers.fused_moe.moe_align_block_size
moe_align_block_size
¶
moe_align_block_size(
topk_ids: Tensor,
block_size: int,
num_experts: int,
expert_map: Optional[Tensor] = None,
pad_sorted_ids: bool = False,
) -> tuple[Tensor, Tensor, Tensor]
Aligns the token distribution across experts to be compatible with block size for matrix multiplication.
Note: In the case of expert_parallel, moe_align_block_size initially considers all experts as valid and aligns all tokens appropriately. Before the function returns it marks the experts_ids that are not in the current GPU rank as -1 so the MoE matmuls could skip those blocks. This requires the num_experts input arg to be the num global experts.
- topk_ids: A tensor of shape [total_tokens, top_k] representing the top-k expert indices for each token.
- block_size: The block size used in block matrix multiplication.
- num_experts: The total number of experts.
- expert_map: A tensor of shape [num_experts] that maps the expert index from the global space to the local index space of the current expert parallel shard. If the expert is not in the current expert parallel shard, the mapping is set to -1.
- pad_sorted_ids: A flag indicating whether the sorted_token_ids length should be padded to a multiple of block_size,
- sorted_token_ids: A tensor containing the sorted token indices according to their allocated expert.
- expert_ids: A tensor indicating the assigned expert index for each block.
- num_tokens_post_padded: The total number of tokens after padding, ensuring divisibility by block_size.
This function pads the number of tokens that each expert needs to process so that it is divisible by block_size. Padding ensures that during block matrix multiplication, the dimensions align correctly.
Example: Given topk_ids = [[2, 3, 4], [1, 2, 4], [1, 3, 4], [1, 2, 3]], block_size = 4, and num_experts = 4: - We initially have 12 tokens (after repeating 'top_k' times) and 4 experts, with each expert needing to process 3 tokens. - As block_size is 4, we pad 1 token for each expert. - First, flatten topk_ids to [2, 3, 4, 1, 2, 4, 1, 3, 4, 1, 2, 3]. - Then append padding tokens [12, 12, 12, 12] for each block. - After sorting by expert index, we obtain token_ids [3, 6, 9, 12, 0, 4, 10, 12, 1, 7, 11, 12, 2, 5, 8, 12]. Tokens 12 are non-existent (padding) and are ignored in the subsequent matrix multiplication. - The padding ensures that the total number of tokens is now divisible by block_size for proper block matrix operations.
Source code in vllm/model_executor/layers/fused_moe/moe_align_block_size.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
moe_align_block_size_stage1
¶
moe_align_block_size_stage1(
topk_ids_ptr,
tokens_cnts_ptr,
num_experts: constexpr,
numel: constexpr,
tokens_per_thread: constexpr,
)
Source code in vllm/model_executor/layers/fused_moe/moe_align_block_size.py
moe_align_block_size_stage2
¶
Source code in vllm/model_executor/layers/fused_moe/moe_align_block_size.py
moe_align_block_size_stage3
¶
moe_align_block_size_stage3(
total_tokens_post_pad_ptr,
tokens_cnts_ptr,
cumsum_ptr,
num_experts: constexpr,
block_size: constexpr,
)
Source code in vllm/model_executor/layers/fused_moe/moe_align_block_size.py
moe_align_block_size_stage4
¶
moe_align_block_size_stage4(
topk_ids_ptr,
sorted_token_ids_ptr,
expert_ids_ptr,
tokens_cnts_ptr,
cumsum_ptr,
num_experts: constexpr,
block_size: constexpr,
numel: constexpr,
tokens_per_thread: constexpr,
)
Source code in vllm/model_executor/layers/fused_moe/moe_align_block_size.py
moe_align_block_size_triton
¶
moe_align_block_size_triton(
topk_ids: Tensor,
num_experts: int,
block_size: int,
sorted_token_ids: Tensor,
expert_ids: Tensor,
num_tokens_post_pad: Tensor,
) -> None