tpu-inference 0.12.0.dev20251222__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tests/__init__.py +13 -0
- tests/core/__init__.py +13 -0
- tests/core/test_core_tpu.py +513 -0
- tests/core/test_disagg_executor.py +60 -0
- tests/core/test_disagg_utils.py +67 -0
- tests/core/test_dp_scheduler.py +724 -0
- tests/core/test_init.py +63 -0
- tests/distributed/__init__.py +13 -0
- tests/distributed/test_distributed_utils.py +120 -0
- tests/distributed/test_tpu_connector.py +478 -0
- tests/e2e/__init__.py +13 -0
- tests/e2e/test_async_scheduler.py +211 -0
- tests/e2e/test_data_parallel.py +393 -0
- tests/e2e/test_local_disagg.py +257 -0
- tests/e2e/test_model_loader.py +268 -0
- tests/e2e/test_multi_modal_inference.py +111 -0
- tests/e2e/test_pipeline_parallel.py +265 -0
- tests/e2e/test_runai_model_streamer_loader.py +104 -0
- tests/e2e/test_sampling_params.py +269 -0
- tests/e2e/test_speculative_decoding.py +291 -0
- tests/e2e/test_structured_decoding.py +46 -0
- tests/executors/__init__.py +13 -0
- tests/executors/test_ray_distributed_executor.py +199 -0
- tests/experimental/__init__.py +13 -0
- tests/experimental/test_llama3_jax_stashed.py +208 -0
- tests/kernels/__init__.py +13 -0
- tests/kernels/collectives/__init__.py +13 -0
- tests/kernels/collectives/all_gather_matmul_kernel_test.py +69 -0
- tests/kernels/fused_moe_v1_test.py +388 -0
- tests/kernels/gmm_test.py +205 -0
- tests/kernels/mla_v1_test.py +498 -0
- tests/kernels/quantized_matmul_kernel_test.py +159 -0
- tests/kernels/ragged_kv_cache_update_v2_test.py +248 -0
- tests/kernels/ragged_paged_attention_kernel_v2_test.py +414 -0
- tests/kernels/ragged_paged_attention_kernel_v3_hd64_test.py +565 -0
- tests/kernels/ragged_paged_attention_kernel_v3_test.py +520 -0
- tests/layers/__init__.py +13 -0
- tests/layers/common/__init__.py +13 -0
- tests/layers/common/test_attention_interface.py +156 -0
- tests/layers/common/test_quantization.py +149 -0
- tests/layers/jax/__init__.py +13 -0
- tests/layers/jax/attention/__init__.py +13 -0
- tests/layers/jax/attention/test_common_attention.py +103 -0
- tests/layers/jax/attention/test_deepseek_v3_attention.py +233 -0
- tests/layers/jax/attention/test_llama4_attention.py +135 -0
- tests/layers/jax/moe/__init__.py +13 -0
- tests/layers/jax/moe/test_deepseek_moe.py +235 -0
- tests/layers/jax/sample/__init__.py +13 -0
- tests/layers/jax/sample/test_rejection_sampler.py +1624 -0
- tests/layers/jax/sample/test_sampling.py +115 -0
- tests/layers/jax/sample/test_sampling_metadata.py +254 -0
- tests/layers/jax/test_layers.py +155 -0
- tests/layers/jax/test_qwix.py +969 -0
- tests/layers/jax/test_rope.py +93 -0
- tests/layers/jax/test_sharding.py +159 -0
- tests/layers/jax/test_transformer_block.py +152 -0
- tests/layers/vllm/__init__.py +13 -0
- tests/layers/vllm/test_attention.py +363 -0
- tests/layers/vllm/test_awq.py +405 -0
- tests/layers/vllm/test_compressed_tensors_moe.py +202 -0
- tests/layers/vllm/test_compressed_tensors_w8a8_fp8.py +403 -0
- tests/layers/vllm/test_compressed_tensors_w8a8_int8.py +426 -0
- tests/layers/vllm/test_fp8.py +17 -0
- tests/layers/vllm/test_mxfp4.py +297 -0
- tests/layers/vllm/test_unquantized.py +621 -0
- tests/layers/vllm/utils.py +72 -0
- tests/lora/__init__.py +13 -0
- tests/lora/conftest.py +46 -0
- tests/lora/test_bgmv.py +57 -0
- tests/lora/test_layers.py +666 -0
- tests/lora/test_lora.py +147 -0
- tests/lora/test_lora_perf.py +67 -0
- tests/lora/utils.py +88 -0
- tests/models/__init__.py +13 -0
- tests/models/common/__init__.py +13 -0
- tests/models/common/test_model_loader.py +455 -0
- tests/models/jax/__init__.py +13 -0
- tests/models/jax/test_deepseek_v3.py +401 -0
- tests/models/jax/test_llama3.py +184 -0
- tests/models/jax/test_llama4.py +298 -0
- tests/models/jax/test_llama_eagle3.py +197 -0
- tests/models/jax/test_llama_guard_4.py +242 -0
- tests/models/jax/test_qwen2.py +172 -0
- tests/models/jax/test_qwen2_5_vl.py +606 -0
- tests/models/jax/test_qwen3.py +169 -0
- tests/models/jax/test_weight_loading.py +180 -0
- tests/models/jax/utils/__init__.py +13 -0
- tests/models/jax/utils/test_multi_modal_utils.py +212 -0
- tests/platforms/__init__.py +13 -0
- tests/platforms/test_tpu_platform.py +54 -0
- tests/runner/__init__.py +13 -0
- tests/runner/test_block_table.py +395 -0
- tests/runner/test_input_batch.py +226 -0
- tests/runner/test_kv_cache.py +220 -0
- tests/runner/test_kv_cache_manager.py +498 -0
- tests/runner/test_multimodal_manager.py +429 -0
- tests/runner/test_persistent_batch_manager.py +84 -0
- tests/runner/test_speculative_decoding_manager.py +368 -0
- tests/runner/test_structured_decoding_manager.py +220 -0
- tests/runner/test_tpu_runner.py +202 -0
- tests/runner/test_tpu_runner_dp.py +1033 -0
- tests/runner/test_tpu_runner_mesh.py +200 -0
- tests/runner/test_utils.py +411 -0
- tests/spec_decode/__init__.py +13 -0
- tests/spec_decode/test_eagle3.py +311 -0
- tests/test_base.py +215 -0
- tests/test_envs.py +280 -0
- tests/test_tpu_info.py +134 -0
- tests/test_utils.py +193 -0
- tests/worker/__init__.py +13 -0
- tests/worker/tpu_worker_test.py +414 -0
- tpu_inference/__init__.py +67 -0
- tpu_inference/core/__init__.py +13 -0
- tpu_inference/core/core_tpu.py +786 -0
- tpu_inference/core/disagg_executor.py +118 -0
- tpu_inference/core/disagg_utils.py +49 -0
- tpu_inference/core/sched/__init__.py +13 -0
- tpu_inference/core/sched/dp_scheduler.py +814 -0
- tpu_inference/distributed/__init__.py +13 -0
- tpu_inference/distributed/jax_parallel_state.py +81 -0
- tpu_inference/distributed/tpu_connector.py +732 -0
- tpu_inference/distributed/utils.py +112 -0
- tpu_inference/env_override.py +9 -0
- tpu_inference/envs.py +191 -0
- tpu_inference/executors/__init__.py +13 -0
- tpu_inference/executors/ray_distributed_executor.py +399 -0
- tpu_inference/experimental/__init__.py +13 -0
- tpu_inference/experimental/llama3_jax_stashed.py +272 -0
- tpu_inference/kernels/__init__.py +13 -0
- tpu_inference/kernels/collectives/__init__.py +13 -0
- tpu_inference/kernels/collectives/all_gather_matmul.py +741 -0
- tpu_inference/kernels/collectives/all_gather_matmul_tuned_block_sizes.py +65 -0
- tpu_inference/kernels/collectives/util.py +47 -0
- tpu_inference/kernels/flash_attention/__init__.py +13 -0
- tpu_inference/kernels/flash_attention/kernel.py +772 -0
- tpu_inference/kernels/fused_moe/__init__.py +13 -0
- tpu_inference/kernels/fused_moe/v1/__init__.py +13 -0
- tpu_inference/kernels/fused_moe/v1/kernel.py +1612 -0
- tpu_inference/kernels/megablox/__init__.py +13 -0
- tpu_inference/kernels/megablox/common.py +54 -0
- tpu_inference/kernels/megablox/gmm.py +646 -0
- tpu_inference/kernels/mla/__init__.py +13 -0
- tpu_inference/kernels/mla/v1/__init__.py +13 -0
- tpu_inference/kernels/mla/v1/kernel.py +1340 -0
- tpu_inference/kernels/quantized_matmul/__init__.py +13 -0
- tpu_inference/kernels/quantized_matmul/kernel.py +456 -0
- tpu_inference/kernels/quantized_matmul/tuned_block_sizes.py +609 -0
- tpu_inference/kernels/quantized_matmul/util.py +58 -0
- tpu_inference/kernels/ragged_paged_attention/__init__.py +13 -0
- tpu_inference/kernels/ragged_paged_attention/v2/__init__.py +13 -0
- tpu_inference/kernels/ragged_paged_attention/v2/kernel.py +876 -0
- tpu_inference/kernels/ragged_paged_attention/v2/ragged_kv_cache_update.py +288 -0
- tpu_inference/kernels/ragged_paged_attention/v2/tuned_block_sizes.py +1482 -0
- tpu_inference/kernels/ragged_paged_attention/v3/__init__.py +13 -0
- tpu_inference/kernels/ragged_paged_attention/v3/kernel.py +1594 -0
- tpu_inference/kernels/ragged_paged_attention/v3/kernel_hd64.py +1586 -0
- tpu_inference/kernels/ragged_paged_attention/v3/tuned_block_sizes.py +4460 -0
- tpu_inference/kernels/ragged_paged_attention/v3/tuned_block_sizes_hd64.py +548 -0
- tpu_inference/kernels/ragged_paged_attention/v3/util.py +65 -0
- tpu_inference/layers/__init__.py +13 -0
- tpu_inference/layers/common/__init__.py +13 -0
- tpu_inference/layers/common/attention_interface.py +403 -0
- tpu_inference/layers/common/attention_metadata.py +48 -0
- tpu_inference/layers/common/binary_search.py +295 -0
- tpu_inference/layers/common/quant_methods.py +23 -0
- tpu_inference/layers/common/quantization.py +270 -0
- tpu_inference/layers/common/sharding.py +600 -0
- tpu_inference/layers/jax/__init__.py +13 -0
- tpu_inference/layers/jax/attention/__init__.py +13 -0
- tpu_inference/layers/jax/attention/attention.py +268 -0
- tpu_inference/layers/jax/attention/deepseek_v3_attention.py +547 -0
- tpu_inference/layers/jax/attention/gpt_oss_attention.py +275 -0
- tpu_inference/layers/jax/attention/llama4_attention.py +167 -0
- tpu_inference/layers/jax/base.py +165 -0
- tpu_inference/layers/jax/constants.py +101 -0
- tpu_inference/layers/jax/layers.py +315 -0
- tpu_inference/layers/jax/misc.py +30 -0
- tpu_inference/layers/jax/moe/__init__.py +13 -0
- tpu_inference/layers/jax/moe/deepseek_v3_moe.py +615 -0
- tpu_inference/layers/jax/moe/gpt_oss_moe.py +199 -0
- tpu_inference/layers/jax/moe/moe.py +249 -0
- tpu_inference/layers/jax/pp_utils.py +53 -0
- tpu_inference/layers/jax/rope.py +294 -0
- tpu_inference/layers/jax/rope_interface.py +228 -0
- tpu_inference/layers/jax/sample/__init__.py +13 -0
- tpu_inference/layers/jax/sample/rejection_sampler.py +528 -0
- tpu_inference/layers/jax/sample/sampling.py +110 -0
- tpu_inference/layers/jax/sample/sampling_metadata.py +90 -0
- tpu_inference/layers/jax/transformer_block.py +121 -0
- tpu_inference/layers/vllm/__init__.py +13 -0
- tpu_inference/layers/vllm/attention.py +221 -0
- tpu_inference/layers/vllm/fused_moe.py +502 -0
- tpu_inference/layers/vllm/linear_common.py +221 -0
- tpu_inference/layers/vllm/quantization/__init__.py +55 -0
- tpu_inference/layers/vllm/quantization/awq.py +221 -0
- tpu_inference/layers/vllm/quantization/common.py +124 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/__init__.py +13 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/compressed_tensors.py +135 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/compressed_tensors_moe.py +266 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/schemes/__init__.py +13 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_fp8.py +222 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_int8.py +150 -0
- tpu_inference/layers/vllm/quantization/fp8.py +118 -0
- tpu_inference/layers/vllm/quantization/mxfp4.py +396 -0
- tpu_inference/layers/vllm/quantization/unquantized.py +416 -0
- tpu_inference/layers/vllm/sharding.py +244 -0
- tpu_inference/logger.py +10 -0
- tpu_inference/lora/__init__.py +13 -0
- tpu_inference/lora/torch_lora_ops.py +98 -0
- tpu_inference/lora/torch_punica_tpu.py +310 -0
- tpu_inference/models/__init__.py +13 -0
- tpu_inference/models/common/__init__.py +13 -0
- tpu_inference/models/common/model_loader.py +520 -0
- tpu_inference/models/jax/__init__.py +13 -0
- tpu_inference/models/jax/deepseek_v3.py +978 -0
- tpu_inference/models/jax/gpt_oss.py +508 -0
- tpu_inference/models/jax/jax_intermediate_tensor.py +93 -0
- tpu_inference/models/jax/llama3.py +436 -0
- tpu_inference/models/jax/llama4.py +643 -0
- tpu_inference/models/jax/llama_eagle3.py +350 -0
- tpu_inference/models/jax/llama_guard_4.py +375 -0
- tpu_inference/models/jax/qwen2.py +390 -0
- tpu_inference/models/jax/qwen2_5_vl.py +1232 -0
- tpu_inference/models/jax/qwen3.py +318 -0
- tpu_inference/models/jax/utils/__init__.py +13 -0
- tpu_inference/models/jax/utils/file_utils.py +110 -0
- tpu_inference/models/jax/utils/multi_modal_utils.py +177 -0
- tpu_inference/models/jax/utils/qwix/__init__.py +13 -0
- tpu_inference/models/jax/utils/qwix/qwix_utils.py +713 -0
- tpu_inference/models/jax/utils/weight_utils.py +621 -0
- tpu_inference/models/vllm/__init__.py +13 -0
- tpu_inference/models/vllm/vllm_model_wrapper.py +307 -0
- tpu_inference/models/vllm/vllm_model_wrapper_context.py +59 -0
- tpu_inference/platforms/__init__.py +16 -0
- tpu_inference/platforms/tpu_platform.py +258 -0
- tpu_inference/runner/__init__.py +13 -0
- tpu_inference/runner/block_table.py +122 -0
- tpu_inference/runner/compilation_manager.py +890 -0
- tpu_inference/runner/input_batch.py +435 -0
- tpu_inference/runner/kv_cache.py +166 -0
- tpu_inference/runner/kv_cache_manager.py +508 -0
- tpu_inference/runner/lora_utils.py +106 -0
- tpu_inference/runner/multimodal_manager.py +231 -0
- tpu_inference/runner/persistent_batch_manager.py +296 -0
- tpu_inference/runner/speculative_decoding_manager.py +262 -0
- tpu_inference/runner/structured_decoding_manager.py +101 -0
- tpu_inference/runner/tpu_runner.py +1768 -0
- tpu_inference/runner/utils.py +426 -0
- tpu_inference/spec_decode/__init__.py +13 -0
- tpu_inference/spec_decode/jax/__init__.py +13 -0
- tpu_inference/spec_decode/jax/eagle3.py +430 -0
- tpu_inference/tpu_info.py +92 -0
- tpu_inference/utils.py +345 -0
- tpu_inference/worker/__init__.py +13 -0
- tpu_inference/worker/tpu_worker.py +468 -0
- tpu_inference-0.12.0.dev20251222.dist-info/METADATA +106 -0
- tpu_inference-0.12.0.dev20251222.dist-info/RECORD +260 -0
- tpu_inference-0.12.0.dev20251222.dist-info/WHEEL +5 -0
- tpu_inference-0.12.0.dev20251222.dist-info/licenses/LICENSE +201 -0
- tpu_inference-0.12.0.dev20251222.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
# Copyright 2025 Google LLC
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
import functools
|
|
16
|
+
|
|
17
|
+
import jax
|
|
18
|
+
from jax import numpy as jnp
|
|
19
|
+
from jax.sharding import Mesh
|
|
20
|
+
from jax.sharding import PartitionSpec as P
|
|
21
|
+
|
|
22
|
+
from tpu_inference.kernels.megablox.gmm import gmm
|
|
23
|
+
from tpu_inference.layers.vllm.linear_common import \
|
|
24
|
+
slice_sharded_tensor_for_concatenation
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def activation_fn(activation: str, x1: jax.Array, x2: jax.Array) -> jax.Array:
|
|
28
|
+
match activation:
|
|
29
|
+
case "silu":
|
|
30
|
+
return jax.nn.silu(x1) * x2
|
|
31
|
+
case "swigluoai":
|
|
32
|
+
return _swigluoai(x1, x2)
|
|
33
|
+
case _:
|
|
34
|
+
raise NotImplementedError(
|
|
35
|
+
f"FusedMoE does not support {activation} activation")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _swigluoai(x1: jax.Array,
|
|
39
|
+
x2: jax.Array,
|
|
40
|
+
alpha=1.702,
|
|
41
|
+
limit=7.0) -> jax.Array:
|
|
42
|
+
x1 = jnp.clip(x1, a_max=limit)
|
|
43
|
+
x2 = jnp.clip(x2, a_min=-limit, a_max=limit)
|
|
44
|
+
|
|
45
|
+
gated_activation = x1 * jax.nn.sigmoid(alpha * x1)
|
|
46
|
+
|
|
47
|
+
return gated_activation * (x2 + 1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _round_up_to_multiple_of_128_within_limit(x: int, limit: int) -> int:
|
|
51
|
+
"""
|
|
52
|
+
Rounds the given integer `x` up to the nearest multiple of 128, without
|
|
53
|
+
exceeding the specified `limit`.
|
|
54
|
+
|
|
55
|
+
If `x` is less than or equal to 128, returns 128.
|
|
56
|
+
If `x` is less than `limit`, returns the smallest multiple of 128 greater
|
|
57
|
+
than or equal to `x`.
|
|
58
|
+
If `x` is greater than or equal to `limit`, searches for the largest
|
|
59
|
+
multiple of 128 less than or equal to `limit` (down to 512) that divides `x`
|
|
60
|
+
evenly, and returns it.
|
|
61
|
+
If no such candidate is found, returns `limit`.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
x (int): The integer to round up.
|
|
65
|
+
limit (int): The upper bound (must be a multiple of 128).
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
int: The rounded value according to the rules above.
|
|
69
|
+
|
|
70
|
+
Raises:
|
|
71
|
+
AssertionError: If `limit` is less than 128 or not a multiple of 128.
|
|
72
|
+
"""
|
|
73
|
+
assert limit >= 128 and limit % 128 == 0
|
|
74
|
+
if x <= 128:
|
|
75
|
+
return 128
|
|
76
|
+
if x < limit:
|
|
77
|
+
return (x + 127) // 128 * 128
|
|
78
|
+
for candidate in range(limit, 511, -128):
|
|
79
|
+
if x % candidate == 0:
|
|
80
|
+
return candidate
|
|
81
|
+
return limit
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _get_tiling_size_for_gmm_kernel(m: int, k: int, n: int,
|
|
85
|
+
g: int) -> tuple[int, int, int]:
|
|
86
|
+
"""
|
|
87
|
+
Calculate optimal tiling sizes for a GMM kernel in a Mixture of Experts
|
|
88
|
+
(MoE) setting.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
m (int): The total number of tokens.
|
|
92
|
+
n (int): The output feature dimension.
|
|
93
|
+
k (int): The input feature dimension.
|
|
94
|
+
g (int): The number of experts.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
tuple[int, int, int]: A tuple (tm, tk, tn)
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
# TODO(Chengji): increase the upper limit tiling size of m when we can set
|
|
101
|
+
# the vmem size to be used for gmm kernel.
|
|
102
|
+
# NOTE: In average each expert has m // g tokens, but as it might be
|
|
103
|
+
# unbalanced, here we doubled the token size when choosing tiling size of m.
|
|
104
|
+
# 2m//g can be either greater or less than 512. If there are 32 tokens and
|
|
105
|
+
# topk=2, m=topk * num_tokens=64, in this case, 2*m//g will be less than
|
|
106
|
+
# 512.
|
|
107
|
+
tm = _round_up_to_multiple_of_128_within_limit(2 * m // g, 512)
|
|
108
|
+
tm = min(tm, m) # there's a requirement that m % tm == 0
|
|
109
|
+
# k/n correspond to n_input_features/n_output_features in the matmul so they
|
|
110
|
+
# are normally greater than 2048, unless the num shards is large.
|
|
111
|
+
tk = _round_up_to_multiple_of_128_within_limit(k, 2048)
|
|
112
|
+
tn = _round_up_to_multiple_of_128_within_limit(n, 2048)
|
|
113
|
+
return tm, tk, tn
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def tensor_sharded_gmm_merged_column_parallel(
|
|
117
|
+
lhs: jax.Array,
|
|
118
|
+
rhs: jax.Array,
|
|
119
|
+
rhs_scale: jax.Array | None,
|
|
120
|
+
rhs_bias: jax.Array | None,
|
|
121
|
+
group_sizes: jax.Array,
|
|
122
|
+
mesh: Mesh,
|
|
123
|
+
) -> list[jax.Array]:
|
|
124
|
+
|
|
125
|
+
def _gmm(lhs, rhs, rhs_scale, rhs_bias, group_sizes):
|
|
126
|
+
m, g, n, k = lhs.shape[0], *rhs.shape
|
|
127
|
+
tm, tk, tn = _get_tiling_size_for_gmm_kernel(m, k, n, g)
|
|
128
|
+
return gmm(
|
|
129
|
+
lhs,
|
|
130
|
+
rhs,
|
|
131
|
+
group_sizes,
|
|
132
|
+
rhs_scale=rhs_scale,
|
|
133
|
+
rhs_bias=rhs_bias,
|
|
134
|
+
preferred_element_type=lhs.dtype,
|
|
135
|
+
tiling=(tm, tk, tn),
|
|
136
|
+
transpose_rhs=True,
|
|
137
|
+
group_offset=jnp.array(0),
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
rhs_scale_spec = None if rhs_scale is None else P(None, None, None,
|
|
141
|
+
"model")
|
|
142
|
+
rhs_bias_spec = None if rhs_bias is None else P(None, None, "model")
|
|
143
|
+
|
|
144
|
+
gmm_result = jax.shard_map(
|
|
145
|
+
_gmm,
|
|
146
|
+
mesh=mesh,
|
|
147
|
+
in_specs=(
|
|
148
|
+
P("data", None),
|
|
149
|
+
P(None, "model", None),
|
|
150
|
+
rhs_scale_spec,
|
|
151
|
+
rhs_bias_spec,
|
|
152
|
+
P("data"),
|
|
153
|
+
),
|
|
154
|
+
out_specs=(P("data", "model")),
|
|
155
|
+
check_vma=False,
|
|
156
|
+
)(lhs, rhs, rhs_scale, rhs_bias, group_sizes)
|
|
157
|
+
|
|
158
|
+
tp_size = mesh.shape["model"]
|
|
159
|
+
intermediate_size = gmm_result.shape[-1] // 2
|
|
160
|
+
output_sizes = [intermediate_size, intermediate_size]
|
|
161
|
+
return slice_sharded_tensor_for_concatenation(gmm_result, output_sizes,
|
|
162
|
+
tp_size)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def tensor_sharded_gmm_row_parallel(
|
|
166
|
+
lhs: jax.Array,
|
|
167
|
+
rhs: jax.Array,
|
|
168
|
+
rhs_scale: jax.Array | None,
|
|
169
|
+
rhs_bias: jax.Array | None,
|
|
170
|
+
group_sizes: jax.Array,
|
|
171
|
+
mesh: Mesh,
|
|
172
|
+
) -> jax.Array:
|
|
173
|
+
|
|
174
|
+
def _gmm_all_reduce(lhs, rhs, rhs_scale, rhs_bias, group_sizes):
|
|
175
|
+
m, g, n, k = lhs.shape[0], *rhs.shape
|
|
176
|
+
tm, tk, tn = _get_tiling_size_for_gmm_kernel(m, k, n, g)
|
|
177
|
+
if rhs_bias is not None:
|
|
178
|
+
shard_id = jax.lax.axis_index("model")
|
|
179
|
+
rhs_bias = jnp.where(shard_id == 0, rhs_bias, 0)
|
|
180
|
+
out = gmm(
|
|
181
|
+
lhs,
|
|
182
|
+
rhs,
|
|
183
|
+
group_sizes,
|
|
184
|
+
rhs_scale=rhs_scale,
|
|
185
|
+
rhs_bias=rhs_bias,
|
|
186
|
+
preferred_element_type=lhs.dtype,
|
|
187
|
+
tiling=(tm, tk, tn),
|
|
188
|
+
transpose_rhs=True,
|
|
189
|
+
group_offset=jnp.array(0),
|
|
190
|
+
)
|
|
191
|
+
return jax.lax.psum(out, axis_name="model")
|
|
192
|
+
|
|
193
|
+
num_blocks = 1 if rhs_scale is None else rhs_scale.shape[1]
|
|
194
|
+
rhs_scale_spec = None if num_blocks == 1 else P(None, "model", None, None)
|
|
195
|
+
rhs_bias_spec = None if rhs_bias is None else P(None, None, None)
|
|
196
|
+
gmm_result = jax.shard_map(
|
|
197
|
+
_gmm_all_reduce,
|
|
198
|
+
mesh=mesh,
|
|
199
|
+
in_specs=(
|
|
200
|
+
P("data", "model"),
|
|
201
|
+
P(None, None, "model"),
|
|
202
|
+
rhs_scale_spec,
|
|
203
|
+
rhs_bias_spec,
|
|
204
|
+
P("data"),
|
|
205
|
+
),
|
|
206
|
+
out_specs=(P("data")),
|
|
207
|
+
check_vma=False,
|
|
208
|
+
)(lhs, rhs, rhs_scale, rhs_bias, group_sizes)
|
|
209
|
+
|
|
210
|
+
return gmm_result.astype(lhs.dtype)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def expert_sharded_gmm(
|
|
214
|
+
lhs: jax.Array,
|
|
215
|
+
rhs: jax.Array,
|
|
216
|
+
rhs_scale: jax.Array | None,
|
|
217
|
+
rhs_bias: jax.Array | None,
|
|
218
|
+
group_sizes: jax.Array,
|
|
219
|
+
is_last_expert: bool,
|
|
220
|
+
mesh: Mesh,
|
|
221
|
+
) -> jax.Array:
|
|
222
|
+
ep_size = mesh.shape["model"]
|
|
223
|
+
|
|
224
|
+
num_experts = rhs.shape[0]
|
|
225
|
+
num_experts_per_shard = num_experts // ep_size
|
|
226
|
+
group_offset = jnp.arange(0, num_experts, num_experts_per_shard)
|
|
227
|
+
|
|
228
|
+
def _gmm(lhs, rhs, rhs_scale, rhs_bias, group_sizes, group_offset):
|
|
229
|
+
m, g, n, k = lhs.shape[0], *rhs.shape
|
|
230
|
+
tm, tk, tn = _get_tiling_size_for_gmm_kernel(m, k, n, g)
|
|
231
|
+
|
|
232
|
+
gmm_res = gmm(
|
|
233
|
+
lhs=lhs,
|
|
234
|
+
rhs=rhs,
|
|
235
|
+
rhs_scale=rhs_scale,
|
|
236
|
+
rhs_bias=rhs_bias,
|
|
237
|
+
group_sizes=group_sizes,
|
|
238
|
+
preferred_element_type=lhs.dtype,
|
|
239
|
+
tiling=(tm, tk, tn),
|
|
240
|
+
transpose_rhs=True,
|
|
241
|
+
group_offset=group_offset[0],
|
|
242
|
+
)
|
|
243
|
+
return gmm_res
|
|
244
|
+
|
|
245
|
+
# The result from gmm on each shard has the same shape, but only the rows
|
|
246
|
+
# for this shard has non-zero values. Taking below as an working example:
|
|
247
|
+
# A, A, A, A 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0
|
|
248
|
+
# A, A, A, A 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0
|
|
249
|
+
# A, A, A, A 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0
|
|
250
|
+
# 0, 0, 0, 0 B, B, B, B 0, 0, 0, 0 0, 0, 0, 0
|
|
251
|
+
# 0, 0, 0, 0 B, B, B, B 0, 0, 0, 0 0, 0, 0, 0
|
|
252
|
+
# 0, 0, 0, 0 0, 0, 0, 0 C, C, C, C 0, 0, 0, 0
|
|
253
|
+
# 0, 0, 0, 0 0, 0, 0, 0 C, C, C, C 0, 0, 0, 0
|
|
254
|
+
# 0, 0, 0, 0 0, 0, 0, 0 C, C, C, C 0, 0, 0, 0
|
|
255
|
+
# 0, 0, 0, 0 0, 0, 0, 0 C, C, C, C 0, 0, 0, 0
|
|
256
|
+
# 0, 0, 0, 0 0, 0, 0, 0 C, C, C, C 0, 0, 0, 0
|
|
257
|
+
# 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0 D, D, D, D
|
|
258
|
+
# 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0 D, D, D, D
|
|
259
|
+
# 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0 D, D, D, D
|
|
260
|
+
# 0, 0, 0, 0 0, 0, 0, 0 0, 0, 0, 0 D, D, D, D
|
|
261
|
+
# shard-0 shard-1 shard-2 shard-3
|
|
262
|
+
# Each shards has 3 (row A), 2 (row B), 5 (row C) and 4 (row D).
|
|
263
|
+
lhs_spec = P("model") if is_last_expert else P()
|
|
264
|
+
rhs_scale_spec = None if rhs_scale is None else P("model")
|
|
265
|
+
rhs_bias_spec = None if rhs_bias is None else P("model")
|
|
266
|
+
gmm_res = jax.shard_map(
|
|
267
|
+
_gmm,
|
|
268
|
+
mesh=mesh,
|
|
269
|
+
in_specs=(
|
|
270
|
+
lhs_spec,
|
|
271
|
+
P("model", None, None),
|
|
272
|
+
rhs_scale_spec,
|
|
273
|
+
rhs_bias_spec,
|
|
274
|
+
P(),
|
|
275
|
+
P("model"),
|
|
276
|
+
),
|
|
277
|
+
out_specs=(P("model", None)),
|
|
278
|
+
check_vma=False,
|
|
279
|
+
)(lhs, rhs, rhs_scale, rhs_bias, group_sizes, group_offset)
|
|
280
|
+
|
|
281
|
+
if not is_last_expert:
|
|
282
|
+
return gmm_res
|
|
283
|
+
|
|
284
|
+
# For i-th shard, it is responsible groups (AKA experts) from
|
|
285
|
+
# i*num_experts_per_shard to (i+1)*num_experts_per_shard We sum them up to
|
|
286
|
+
# get total rows in that shard, and that is the size for shard to send to
|
|
287
|
+
# its peers. This is also the number of non-zero rows from the gmm results.
|
|
288
|
+
# In the working example, send_sizes would be [3, 2, 5, 4].
|
|
289
|
+
|
|
290
|
+
# group_sizes has shape of [num_tokens_per_shard * num_experts_per_shard].
|
|
291
|
+
# So reshaping to [num_tokens_per_shard, num_experts_per_shard] and applying
|
|
292
|
+
# sum(axis=1) will get desired send_sizes shaped [num_tokens_per_shard].
|
|
293
|
+
send_sizes = group_sizes.reshape(-1, num_experts_per_shard).sum(axis=1)
|
|
294
|
+
# In the working example, input_offsets would be [0, 3, 5, 10]
|
|
295
|
+
input_offsets = jnp.concatenate((jnp.array([0]), send_sizes.cumsum()[:-1]))
|
|
296
|
+
output_offsets = input_offsets
|
|
297
|
+
recv_sizes = send_sizes
|
|
298
|
+
|
|
299
|
+
def _ragged_all_to_all(operand, input_offsets, send_sizes, output_offsets,
|
|
300
|
+
recv_sizes):
|
|
301
|
+
output = jnp.zeros_like(operand)
|
|
302
|
+
|
|
303
|
+
# input_offsets, send_sizes and output_offsets are sharded and there is
|
|
304
|
+
# only 1 elemnt in each shard, we are taking the 0-th element from them
|
|
305
|
+
# just so that jnp.repeat generates the arrays with correct shape.
|
|
306
|
+
input_offsets_of_shard = jnp.repeat(input_offsets[0], ep_size)
|
|
307
|
+
send_sizes_of_shard = jnp.repeat(send_sizes[0], ep_size)
|
|
308
|
+
output_offsets_of_shard = jnp.repeat(output_offsets[0], ep_size)
|
|
309
|
+
|
|
310
|
+
# recv_sizes is replicated across shards, because all the shards receive
|
|
311
|
+
# the same data and write to the output in the same way (same
|
|
312
|
+
# output_offsets and same recv_sizes) and thus generates replicated
|
|
313
|
+
# output.
|
|
314
|
+
recv_sizes_of_shard = recv_sizes
|
|
315
|
+
|
|
316
|
+
# In the working example, for each shard, the values of the offsets and
|
|
317
|
+
# sizes would be:
|
|
318
|
+
# shard-0 shard-1 shard-2 shard-3
|
|
319
|
+
# input_offsets_of_shard [0, 0, 0, 0] [3, 3, 3, 3] [5, 5, 5, 5] [10,10,10,10]
|
|
320
|
+
# send_sizes_of_shard [3, 3, 3, 3] [2, 2, 2, 2] [5, 5, 5, 5] [4, 4, 4, 4 ]
|
|
321
|
+
# output_offsets_of_shard [0, 0, 0, 0] [0, 0, 0, 0] [0, 0, 0, 0] [10,10,10,10]
|
|
322
|
+
# recv_sizes_of_shard [3, 2, 5, 4] [3, 2, 5, 4] [3, 2, 5, 4] [3, 2, 5, 4]
|
|
323
|
+
return jax.lax.ragged_all_to_all(
|
|
324
|
+
operand,
|
|
325
|
+
output,
|
|
326
|
+
input_offsets_of_shard,
|
|
327
|
+
send_sizes_of_shard,
|
|
328
|
+
output_offsets_of_shard,
|
|
329
|
+
recv_sizes_of_shard,
|
|
330
|
+
axis_name="model",
|
|
331
|
+
)
|
|
332
|
+
|
|
333
|
+
# Use ragged_all_to_all to send the result from gmm for each expert to all
|
|
334
|
+
# the shards. In the working example, the result would be:
|
|
335
|
+
# A, A, A, A A, A, A, A A, A, A, A A, A, A, A
|
|
336
|
+
# A, A, A, A A, A, A, A A, A, A, A A, A, A, A
|
|
337
|
+
# A, A, A, A A, A, A, A A, A, A, A A, A, A, A
|
|
338
|
+
# B, B, B, B B, B, B, B B, B, B, B B, B, B, B
|
|
339
|
+
# B, B, B, B B, B, B, B B, B, B, B B, B, B, B
|
|
340
|
+
# C, C, C, C C, C, C, C C, C, C, C C, C, C, C
|
|
341
|
+
# C, C, C, C C, C, C, C C, C, C, C C, C, C, C
|
|
342
|
+
# C, C, C, C C, C, C, C C, C, C, C C, C, C, C
|
|
343
|
+
# C, C, C, C C, C, C, C C, C, C, C C, C, C, C
|
|
344
|
+
# C, C, C, C C, C, C, C C, C, C, C C, C, C, C
|
|
345
|
+
# D, D, D, D D, D, D, D D, D, D, D D, D, D, D
|
|
346
|
+
# D, D, D, D D, D, D, D D, D, D, D D, D, D, D
|
|
347
|
+
# D, D, D, D D, D, D, D D, D, D, D D, D, D, D
|
|
348
|
+
# D, D, D, D D, D, D, D D, D, D, D D, D, D, D
|
|
349
|
+
# shard-0 shard-1 shard-2 shard-3
|
|
350
|
+
return jax.shard_map(
|
|
351
|
+
_ragged_all_to_all,
|
|
352
|
+
mesh=mesh,
|
|
353
|
+
in_specs=(P("model", None), P("model"), P("model"), P("model"), P()),
|
|
354
|
+
out_specs=(P()),
|
|
355
|
+
check_vma=False,
|
|
356
|
+
)(gmm_res, input_offsets, send_sizes, output_offsets, recv_sizes)
|
|
357
|
+
|
|
358
|
+
|
|
359
|
+
@functools.partial(
|
|
360
|
+
jax.jit,
|
|
361
|
+
static_argnames=(
|
|
362
|
+
"topk",
|
|
363
|
+
"renormalize",
|
|
364
|
+
"mesh",
|
|
365
|
+
"use_ep",
|
|
366
|
+
"activation",
|
|
367
|
+
),
|
|
368
|
+
)
|
|
369
|
+
def fused_moe_func(
|
|
370
|
+
hidden_states: jax.Array,
|
|
371
|
+
w1: jax.Array,
|
|
372
|
+
w2: jax.Array,
|
|
373
|
+
w1_scale: jax.Array | None,
|
|
374
|
+
w2_scale: jax.Array | None,
|
|
375
|
+
w1_bias: jax.Array | None,
|
|
376
|
+
w2_bias: jax.Array | None,
|
|
377
|
+
gating_output: jax.Array,
|
|
378
|
+
topk: int,
|
|
379
|
+
renormalize: bool,
|
|
380
|
+
mesh: Mesh,
|
|
381
|
+
use_ep: bool,
|
|
382
|
+
activation: str,
|
|
383
|
+
) -> jax.Array:
|
|
384
|
+
"""Route tokens in hidden_states into each experts based on routing.
|
|
385
|
+
|
|
386
|
+
Args:
|
|
387
|
+
hidden_states: [num_tokens, hidden_size]
|
|
388
|
+
w1: first moe weights [num_experts, intermediate_size * 2, hidden_size]
|
|
389
|
+
w2: second moe weights [num_experts, hidden_size, intermediate_size]
|
|
390
|
+
w1_scale: w1 scale [num_experts, num_blocks, 1, intermediate_size * 2]
|
|
391
|
+
w2_scale: w2 scale [num_experts, num_blocks, 1, hidden_size]
|
|
392
|
+
w1_bias: optional bias of w1 [num_experts, 1, intermediate_size * 2]
|
|
393
|
+
w2_bias: optional bias of w2 [num_experts, 1, hidden_size]
|
|
394
|
+
gating_output: routing information of tokens [num_tokens, num_experts]
|
|
395
|
+
topk: number of experts to choose per token.
|
|
396
|
+
renormalize: normalize gating_output.
|
|
397
|
+
mesh: mesh to perform moe.
|
|
398
|
+
use_ep: use expert parallelism.
|
|
399
|
+
activation: activation function to perform on the output of w1.
|
|
400
|
+
|
|
401
|
+
Returns:
|
|
402
|
+
Output of moe operation [num_tokens, hidden_size]
|
|
403
|
+
"""
|
|
404
|
+
num_tokens, hidden_size = hidden_states.shape
|
|
405
|
+
global_num_experts, _, padded_hidden_size = w1.shape
|
|
406
|
+
dtype = hidden_states.dtype
|
|
407
|
+
|
|
408
|
+
assert (num_tokens * topk) % 16 == 0, (
|
|
409
|
+
"The kernel requires num_tokens * topk to be a multiple of "
|
|
410
|
+
f"16 but got {num_tokens}*{topk}={num_tokens*topk}")
|
|
411
|
+
|
|
412
|
+
assert gating_output.shape == (num_tokens, global_num_experts)
|
|
413
|
+
|
|
414
|
+
topk_weights = jax.nn.softmax(gating_output.astype(jnp.float32), axis=-1)
|
|
415
|
+
topk_weights, topk_indices = jax.lax.top_k(topk_weights, k=topk)
|
|
416
|
+
if renormalize:
|
|
417
|
+
topk_weights = topk_weights / topk_weights.sum(axis=-1, keepdims=True)
|
|
418
|
+
topk_weights = topk_weights.astype(dtype)
|
|
419
|
+
|
|
420
|
+
def _process_tokens_locally(hidden_states_local, topk_indices_local):
|
|
421
|
+
num_tokens_local = hidden_states_local.shape[0]
|
|
422
|
+
topk_indices_flat = topk_indices_local.flatten()
|
|
423
|
+
topk_argsort_indices = jnp.argsort(topk_indices_flat)
|
|
424
|
+
topk_argsort_revert_indices = jnp.argsort(topk_argsort_indices)
|
|
425
|
+
token_indices = jnp.arange(num_tokens_local,
|
|
426
|
+
dtype=jnp.int32).repeat(topk)
|
|
427
|
+
token_indices_sorted = token_indices[topk_argsort_indices]
|
|
428
|
+
group_sizes_local = jnp.bincount(topk_indices_flat,
|
|
429
|
+
length=global_num_experts)
|
|
430
|
+
|
|
431
|
+
x = hidden_states_local[token_indices_sorted]
|
|
432
|
+
return x, group_sizes_local, topk_argsort_revert_indices
|
|
433
|
+
|
|
434
|
+
x, group_sizes, topk_argsort_revert_indices = jax.shard_map(
|
|
435
|
+
_process_tokens_locally,
|
|
436
|
+
mesh=mesh,
|
|
437
|
+
in_specs=(P("data", None), P("data", None)),
|
|
438
|
+
out_specs=(P("data", None), P("data"), P("data")),
|
|
439
|
+
)(hidden_states, topk_indices)
|
|
440
|
+
|
|
441
|
+
x = jnp.pad(x, ((0, 0), (0, padded_hidden_size - hidden_size)))
|
|
442
|
+
|
|
443
|
+
if use_ep:
|
|
444
|
+
x = expert_sharded_gmm(
|
|
445
|
+
x,
|
|
446
|
+
w1,
|
|
447
|
+
w1_scale,
|
|
448
|
+
w1_bias,
|
|
449
|
+
group_sizes,
|
|
450
|
+
is_last_expert=False,
|
|
451
|
+
mesh=mesh,
|
|
452
|
+
)
|
|
453
|
+
x1, x2 = jnp.split(x, 2, -1)
|
|
454
|
+
|
|
455
|
+
x = activation_fn(activation, x1, x2)
|
|
456
|
+
|
|
457
|
+
x = expert_sharded_gmm(
|
|
458
|
+
x,
|
|
459
|
+
w2,
|
|
460
|
+
w2_scale,
|
|
461
|
+
w2_bias,
|
|
462
|
+
group_sizes,
|
|
463
|
+
is_last_expert=True,
|
|
464
|
+
mesh=mesh,
|
|
465
|
+
)
|
|
466
|
+
else:
|
|
467
|
+
x1, x2 = tensor_sharded_gmm_merged_column_parallel(
|
|
468
|
+
x,
|
|
469
|
+
w1,
|
|
470
|
+
w1_scale,
|
|
471
|
+
w1_bias,
|
|
472
|
+
group_sizes,
|
|
473
|
+
mesh=mesh,
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
x = activation_fn(activation, x1, x2)
|
|
477
|
+
|
|
478
|
+
x = tensor_sharded_gmm_row_parallel(
|
|
479
|
+
x,
|
|
480
|
+
w2,
|
|
481
|
+
w2_scale,
|
|
482
|
+
w2_bias,
|
|
483
|
+
group_sizes,
|
|
484
|
+
mesh=mesh,
|
|
485
|
+
)
|
|
486
|
+
|
|
487
|
+
def _finalize_output(x_local, topk_argsort_revert_indices_local,
|
|
488
|
+
topk_weights_local):
|
|
489
|
+
x_local = x_local[topk_argsort_revert_indices_local].reshape(
|
|
490
|
+
-1, topk, padded_hidden_size)
|
|
491
|
+
x_local = x_local * jnp.expand_dims(topk_weights_local, axis=-1)
|
|
492
|
+
x_local = x_local.sum(axis=-2)
|
|
493
|
+
return x_local
|
|
494
|
+
|
|
495
|
+
x = jax.shard_map(
|
|
496
|
+
_finalize_output,
|
|
497
|
+
mesh=mesh,
|
|
498
|
+
in_specs=(P("data", None), P("data"), P("data", None)),
|
|
499
|
+
out_specs=(P("data", None)),
|
|
500
|
+
)(x, topk_argsort_revert_indices, topk_weights)
|
|
501
|
+
|
|
502
|
+
return x[:num_tokens, :hidden_size]
|