tpu-inference 0.11.1.dev202511150811__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.
Potentially problematic release.
This version of tpu-inference might be problematic. Click here for more details.
- tests/__init__.py +0 -0
- tests/core/__init__.py +0 -0
- tests/core/test_core_tpu.py +513 -0
- tests/core/test_disagg_executor.py +60 -0
- tests/core/test_disagg_utils.py +53 -0
- tests/core/test_dp_scheduler.py +899 -0
- tests/core/test_init.py +49 -0
- tests/kernels/__init__.py +0 -0
- tests/kernels/fused_moe_v1_test.py +105 -0
- tests/kernels/mla_v1_test.py +396 -0
- tests/kernels/quantized_matmul_kernel_test.py +191 -0
- tests/kernels/ragged_kv_cache_update_v2_test.py +234 -0
- tests/kernels/ragged_paged_attention_kernel_v2_test.py +400 -0
- tests/kernels/ragged_paged_attention_kernel_v3_hd64_test.py +549 -0
- tests/kernels/ragged_paged_attention_kernel_v3_test.py +504 -0
- tests/lora/__init__.py +0 -0
- tests/lora/conftest.py +32 -0
- tests/lora/test_bgmv.py +43 -0
- tests/lora/test_layers.py +654 -0
- tests/lora/test_lora.py +133 -0
- tests/lora/utils.py +96 -0
- tests/test_base.py +201 -0
- tests/test_envs.py +182 -0
- tests/test_quantization.py +836 -0
- tests/test_tpu_info.py +120 -0
- tests/test_utils.py +236 -0
- tpu_inference/__init__.py +34 -0
- tpu_inference/core/__init__.py +0 -0
- tpu_inference/core/core_tpu.py +786 -0
- tpu_inference/core/disagg_executor.py +118 -0
- tpu_inference/core/disagg_utils.py +51 -0
- tpu_inference/core/sched/__init__.py +0 -0
- tpu_inference/core/sched/dp_scheduler.py +523 -0
- tpu_inference/distributed/__init__.py +0 -0
- tpu_inference/distributed/jax_parallel_state.py +67 -0
- tpu_inference/distributed/tpu_connector.py +728 -0
- tpu_inference/distributed/utils.py +59 -0
- tpu_inference/env_override.py +9 -0
- tpu_inference/envs.py +107 -0
- tpu_inference/executors/__init__.py +0 -0
- tpu_inference/executors/ray_distributed_executor.py +362 -0
- tpu_inference/experimental/__init__.py +0 -0
- tpu_inference/experimental/llama3_jax_stashed.py +258 -0
- tpu_inference/kernels/__init__.py +0 -0
- tpu_inference/kernels/collectives/__init__.py +0 -0
- tpu_inference/kernels/collectives/all_gather_matmul.py +735 -0
- tpu_inference/kernels/collectives/all_gather_matmul_tuned_block_sizes.py +60 -0
- tpu_inference/kernels/collectives/util.py +47 -0
- tpu_inference/kernels/flash_attention/__init__.py +0 -0
- tpu_inference/kernels/flash_attention/kernel.py +772 -0
- tpu_inference/kernels/fused_moe/__init__.py +0 -0
- tpu_inference/kernels/fused_moe/v1/__init__.py +0 -0
- tpu_inference/kernels/fused_moe/v1/kernel.py +1035 -0
- tpu_inference/kernels/mla/__init__.py +0 -0
- tpu_inference/kernels/mla/v1/__init__.py +0 -0
- tpu_inference/kernels/mla/v1/kernel.py +1349 -0
- tpu_inference/kernels/quantized_matmul/__init__.py +0 -0
- tpu_inference/kernels/quantized_matmul/kernel.py +395 -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 +0 -0
- tpu_inference/kernels/ragged_paged_attention/v2/__init__.py +0 -0
- tpu_inference/kernels/ragged_paged_attention/v2/kernel.py +875 -0
- tpu_inference/kernels/ragged_paged_attention/v2/ragged_kv_cache_update.py +287 -0
- tpu_inference/kernels/ragged_paged_attention/v2/tuned_block_sizes.py +1482 -0
- tpu_inference/kernels/ragged_paged_attention/v3/__init__.py +0 -0
- tpu_inference/kernels/ragged_paged_attention/v3/kernel.py +1478 -0
- tpu_inference/kernels/ragged_paged_attention/v3/kernel_hd64.py +1482 -0
- tpu_inference/kernels/ragged_paged_attention/v3/tuned_block_sizes.py +4147 -0
- tpu_inference/kernels/ragged_paged_attention/v3/tuned_block_sizes_hd64.py +367 -0
- tpu_inference/kernels/ragged_paged_attention/v3/util.py +51 -0
- tpu_inference/layers/__init__.py +0 -0
- tpu_inference/layers/common/__init__.py +0 -0
- tpu_inference/layers/common/attention_interface.py +390 -0
- tpu_inference/layers/common/attention_metadata.py +34 -0
- tpu_inference/layers/common/binary_search.py +295 -0
- tpu_inference/layers/common/quant_methods.py +8 -0
- tpu_inference/layers/common/sharding.py +582 -0
- tpu_inference/layers/jax/__init__.py +0 -0
- tpu_inference/layers/jax/attention/__init__.py +0 -0
- tpu_inference/layers/jax/attention/attention.py +255 -0
- tpu_inference/layers/jax/attention/deepseek_v3_attention.py +354 -0
- tpu_inference/layers/jax/attention/gpt_oss_attention.py +262 -0
- tpu_inference/layers/jax/attention/llama4_attention.py +153 -0
- tpu_inference/layers/jax/base.py +151 -0
- tpu_inference/layers/jax/constants.py +88 -0
- tpu_inference/layers/jax/layers.py +301 -0
- tpu_inference/layers/jax/misc.py +16 -0
- tpu_inference/layers/jax/moe/__init__.py +0 -0
- tpu_inference/layers/jax/moe/deepseek_v3_moe.py +608 -0
- tpu_inference/layers/jax/moe/gpt_oss_moe.py +185 -0
- tpu_inference/layers/jax/moe/moe.py +209 -0
- tpu_inference/layers/jax/rope.py +280 -0
- tpu_inference/layers/jax/rope_interface.py +214 -0
- tpu_inference/layers/jax/sample/__init__.py +0 -0
- tpu_inference/layers/jax/sample/rejection_sampler.py +515 -0
- tpu_inference/layers/jax/sample/sampling.py +96 -0
- tpu_inference/layers/jax/sample/sampling_metadata.py +76 -0
- tpu_inference/layers/jax/transformer_block.py +107 -0
- tpu_inference/layers/vllm/__init__.py +0 -0
- tpu_inference/layers/vllm/attention.py +221 -0
- tpu_inference/layers/vllm/fused_moe.py +507 -0
- tpu_inference/layers/vllm/linear_common.py +186 -0
- tpu_inference/layers/vllm/quantization/__init__.py +39 -0
- tpu_inference/layers/vllm/quantization/awq.py +207 -0
- tpu_inference/layers/vllm/quantization/common.py +105 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/__init__.py +0 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/compressed_tensors.py +120 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/compressed_tensors_moe.py +203 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/schemes/__init__.py +0 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_fp8.py +208 -0
- tpu_inference/layers/vllm/quantization/compressed_tensors/schemes/compressed_tensors_w8a8_int8.py +136 -0
- tpu_inference/layers/vllm/quantization/mxfp4.py +266 -0
- tpu_inference/layers/vllm/quantization/unquantized.py +386 -0
- tpu_inference/layers/vllm/sharding.py +230 -0
- tpu_inference/logger.py +10 -0
- tpu_inference/lora/__init__.py +0 -0
- tpu_inference/lora/torch_lora_ops.py +103 -0
- tpu_inference/lora/torch_punica_tpu.py +311 -0
- tpu_inference/mock/__init__.py +0 -0
- tpu_inference/mock/vllm_config_utils.py +28 -0
- tpu_inference/mock/vllm_envs.py +1219 -0
- tpu_inference/mock/vllm_logger.py +212 -0
- tpu_inference/mock/vllm_logging_utils.py +15 -0
- tpu_inference/models/__init__.py +0 -0
- tpu_inference/models/common/__init__.py +0 -0
- tpu_inference/models/common/model_loader.py +444 -0
- tpu_inference/models/jax/__init__.py +0 -0
- tpu_inference/models/jax/deepseek_v3.py +868 -0
- tpu_inference/models/jax/gpt_oss.py +492 -0
- tpu_inference/models/jax/jax_intermediate_tensor.py +79 -0
- tpu_inference/models/jax/llama3.py +375 -0
- tpu_inference/models/jax/llama4.py +629 -0
- tpu_inference/models/jax/llama_eagle3.py +333 -0
- tpu_inference/models/jax/phi3.py +376 -0
- tpu_inference/models/jax/qwen2.py +375 -0
- tpu_inference/models/jax/qwen2_5_vl.py +1103 -0
- tpu_inference/models/jax/qwen3.py +302 -0
- tpu_inference/models/jax/utils/__init__.py +0 -0
- tpu_inference/models/jax/utils/file_utils.py +96 -0
- tpu_inference/models/jax/utils/multi_modal_utils.py +163 -0
- tpu_inference/models/jax/utils/quantization/__init__.py +0 -0
- tpu_inference/models/jax/utils/quantization/configs/fp8_all_modules_w_only.yaml +5 -0
- tpu_inference/models/jax/utils/quantization/configs/fp8_default.yaml +6 -0
- tpu_inference/models/jax/utils/quantization/configs/int8_all_modules_w_only.yaml +5 -0
- tpu_inference/models/jax/utils/quantization/configs/int8_default.yaml +6 -0
- tpu_inference/models/jax/utils/quantization/mxfp4_utils.py +105 -0
- tpu_inference/models/jax/utils/quantization/quantization_utils.py +653 -0
- tpu_inference/models/jax/utils/weight_utils.py +529 -0
- tpu_inference/models/vllm/__init__.py +0 -0
- tpu_inference/models/vllm/vllm_model_wrapper.py +286 -0
- tpu_inference/models/vllm/vllm_model_wrapper_context.py +45 -0
- tpu_inference/platforms/__init__.py +2 -0
- tpu_inference/platforms/tpu_platform.py +269 -0
- tpu_inference/runner/__init__.py +0 -0
- tpu_inference/runner/block_table.py +122 -0
- tpu_inference/runner/compilation_manager.py +780 -0
- tpu_inference/runner/input_batch.py +435 -0
- tpu_inference/runner/kv_cache.py +132 -0
- tpu_inference/runner/kv_cache_manager.py +479 -0
- tpu_inference/runner/lora_utils.py +92 -0
- tpu_inference/runner/multimodal_manager.py +217 -0
- tpu_inference/runner/persistent_batch_manager.py +244 -0
- tpu_inference/runner/speculative_decoding_manager.py +248 -0
- tpu_inference/runner/structured_decoding_manager.py +88 -0
- tpu_inference/runner/tpu_runner.py +1620 -0
- tpu_inference/runner/utils.py +426 -0
- tpu_inference/spec_decode/__init__.py +0 -0
- tpu_inference/spec_decode/jax/__init__.py +0 -0
- tpu_inference/spec_decode/jax/eagle3.py +367 -0
- tpu_inference/tpu_info.py +77 -0
- tpu_inference/utils.py +317 -0
- tpu_inference/worker/__init__.py +0 -0
- tpu_inference/worker/tpu_worker.py +321 -0
- tpu_inference-0.11.1.dev202511150811.dist-info/METADATA +107 -0
- tpu_inference-0.11.1.dev202511150811.dist-info/RECORD +179 -0
- tpu_inference-0.11.1.dev202511150811.dist-info/WHEEL +5 -0
- tpu_inference-0.11.1.dev202511150811.dist-info/licenses/LICENSE +201 -0
- tpu_inference-0.11.1.dev202511150811.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import math
|
|
3
|
+
from typing import Any, Callable, Optional, Tuple
|
|
4
|
+
|
|
5
|
+
import jax
|
|
6
|
+
import jax.numpy as jnp
|
|
7
|
+
from jax.experimental import shard_map
|
|
8
|
+
from jax.experimental.pallas.ops.tpu.paged_attention import paged_attention
|
|
9
|
+
from jax.experimental.pallas.ops.tpu.splash_attention import \
|
|
10
|
+
splash_attention_kernel as splash
|
|
11
|
+
from jax.experimental.pallas.ops.tpu.splash_attention import \
|
|
12
|
+
splash_attention_mask as mask_lib
|
|
13
|
+
from jax.sharding import Mesh
|
|
14
|
+
from jax.sharding import PartitionSpec as P
|
|
15
|
+
|
|
16
|
+
import tpu_inference.kernels.ragged_paged_attention.v3.kernel as rpa
|
|
17
|
+
import tpu_inference.kernels.ragged_paged_attention.v3.kernel_hd64 as rpa_hd64
|
|
18
|
+
from tpu_inference.kernels.flash_attention.kernel import flash_attention
|
|
19
|
+
from tpu_inference.layers.common.attention_metadata import AttentionMetadata
|
|
20
|
+
from tpu_inference.layers.common.sharding import ShardingAxisName
|
|
21
|
+
from tpu_inference.utils import get_megacore
|
|
22
|
+
|
|
23
|
+
MAX_ALLOWED_PAGE_INDICES_N = (
|
|
24
|
+
128 * 1024
|
|
25
|
+
) # Based on experiments on v5e, 256x1024 results in smem oom but 128x1024 not. TODO: Adjust this based on TPU version.
|
|
26
|
+
|
|
27
|
+
ragged_paged_attention = rpa.ragged_paged_attention
|
|
28
|
+
get_kv_cache_shape = rpa.get_kv_cache_shape
|
|
29
|
+
|
|
30
|
+
ragged_paged_attention_hd64 = rpa_hd64.ragged_paged_attention_hd64
|
|
31
|
+
get_kv_cache_shape_hd64 = rpa_hd64.get_kv_cache_shape
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def sharded_flash_attention(
|
|
35
|
+
mesh: Mesh,
|
|
36
|
+
causal: bool = True,
|
|
37
|
+
sm_scale: Optional[float] = None,
|
|
38
|
+
vmem_limit_bytes: int | None = None,
|
|
39
|
+
) -> Callable[..., Any]:
|
|
40
|
+
in_specs = (
|
|
41
|
+
P("data", "model", None, None), # q
|
|
42
|
+
P("data", "model", None, None), # k
|
|
43
|
+
P("data", "model", None, None), # v
|
|
44
|
+
P(), # segment_ids
|
|
45
|
+
)
|
|
46
|
+
out_specs = P("data", "model", None, None)
|
|
47
|
+
|
|
48
|
+
def _flash_attention(q, k, v, segment_ids):
|
|
49
|
+
return flash_attention(q,
|
|
50
|
+
k,
|
|
51
|
+
v,
|
|
52
|
+
segment_ids=segment_ids,
|
|
53
|
+
sm_scale=sm_scale,
|
|
54
|
+
causal=causal,
|
|
55
|
+
vmem_limit_bytes=vmem_limit_bytes)
|
|
56
|
+
|
|
57
|
+
return jax.jit(
|
|
58
|
+
shard_map.shard_map(_flash_attention,
|
|
59
|
+
mesh=mesh,
|
|
60
|
+
in_specs=in_specs,
|
|
61
|
+
out_specs=out_specs,
|
|
62
|
+
check_rep=False))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def sharded_paged_attention(
|
|
66
|
+
mesh: Mesh,
|
|
67
|
+
attn_logits_soft_cap: Optional[float] = None,
|
|
68
|
+
) -> Callable[..., Any]:
|
|
69
|
+
"""Shards GQA PagedAttention along KV heads."""
|
|
70
|
+
in_specs = (
|
|
71
|
+
P(None, "model", None), # q
|
|
72
|
+
P("model", None, None, None), # k
|
|
73
|
+
P("model", None, None, None), # v
|
|
74
|
+
P(), # lengths
|
|
75
|
+
P(), # page_indices
|
|
76
|
+
)
|
|
77
|
+
out_specs = P(None, "model", None)
|
|
78
|
+
|
|
79
|
+
def _paged_attention_fn(q, k, v, lengths, page_indices):
|
|
80
|
+
if page_indices.size > MAX_ALLOWED_PAGE_INDICES_N:
|
|
81
|
+
raise ValueError(
|
|
82
|
+
"This will result in smem OOM. Use `paged_attention_with_guarded_smem` to run with minibatches."
|
|
83
|
+
)
|
|
84
|
+
return paged_attention(
|
|
85
|
+
q,
|
|
86
|
+
k,
|
|
87
|
+
v,
|
|
88
|
+
lengths,
|
|
89
|
+
page_indices,
|
|
90
|
+
attn_logits_soft_cap=attn_logits_soft_cap,
|
|
91
|
+
pages_per_compute_block=min(
|
|
92
|
+
16, page_indices.shape[1]), # 512 / page_size:32,
|
|
93
|
+
megacore_mode="kv_head" if get_megacore() else None,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
return jax.jit(
|
|
97
|
+
shard_map.shard_map(
|
|
98
|
+
_paged_attention_fn,
|
|
99
|
+
mesh=mesh,
|
|
100
|
+
in_specs=in_specs,
|
|
101
|
+
out_specs=out_specs,
|
|
102
|
+
check_rep=False,
|
|
103
|
+
))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# TODO(xiangxu): merge this with sharded_paged_attention
|
|
107
|
+
@functools.partial(jax.jit, static_argnums=[0])
|
|
108
|
+
def paged_attention_with_guarded_smem(
|
|
109
|
+
paged_attention_kernel: Callable,
|
|
110
|
+
q: jax.Array,
|
|
111
|
+
k_pages: jax.Array,
|
|
112
|
+
v_pages: jax.Array,
|
|
113
|
+
lengths: jax.Array,
|
|
114
|
+
page_indices: jax.Array,
|
|
115
|
+
):
|
|
116
|
+
# Addresses b/336316706. Summary:
|
|
117
|
+
# Paged attention kernel stores `lengths` (batch_size * 4 bytes) and `page_indices` (batch_size * num_blocks_per_seq * 4 bytes) in SMEM.
|
|
118
|
+
# Capacity of SMEM is quite limited which is also TPU version dependent. Models with higher context length or higher batch size, can cause OOM in SMEM.
|
|
119
|
+
# There are two solutions:
|
|
120
|
+
# 1. Reduce blocks per seq by increasing page size.
|
|
121
|
+
# 2. Splitting the batch into several minibatches (Higher perf based on my benchmark).
|
|
122
|
+
|
|
123
|
+
batch_size, blocks_per_seq = page_indices.shape
|
|
124
|
+
|
|
125
|
+
if page_indices.size <= MAX_ALLOWED_PAGE_INDICES_N:
|
|
126
|
+
return paged_attention_kernel(q, k_pages, v_pages, lengths,
|
|
127
|
+
page_indices)
|
|
128
|
+
|
|
129
|
+
mini_batch_size = MAX_ALLOWED_PAGE_INDICES_N // blocks_per_seq
|
|
130
|
+
|
|
131
|
+
# If batch_size is not disible by mini_batch_size,
|
|
132
|
+
# we set mini_batch_size to a smaller value, i.e GCD,
|
|
133
|
+
# which will trigger more kernel launches but it's fine.
|
|
134
|
+
# TODO: Fix --decode_seqs_padding with this limitation.
|
|
135
|
+
mini_batch_size = math.gcd(batch_size, mini_batch_size)
|
|
136
|
+
|
|
137
|
+
num_kernel_launches = batch_size // mini_batch_size
|
|
138
|
+
|
|
139
|
+
outputs = jnp.zeros_like(q).reshape(
|
|
140
|
+
(num_kernel_launches, mini_batch_size, *q.shape[1:]))
|
|
141
|
+
q = q.reshape((num_kernel_launches, mini_batch_size, *q.shape[1:]))
|
|
142
|
+
seq_lens = lengths.reshape((num_kernel_launches, mini_batch_size))
|
|
143
|
+
block_indices = page_indices.reshape(
|
|
144
|
+
(num_kernel_launches, mini_batch_size, page_indices.shape[1]))
|
|
145
|
+
|
|
146
|
+
for i in range(num_kernel_launches):
|
|
147
|
+
outputs = outputs.at[i].set(
|
|
148
|
+
paged_attention_kernel(q[i], k_pages, v_pages, seq_lens[i],
|
|
149
|
+
block_indices[i]))
|
|
150
|
+
|
|
151
|
+
outputs = outputs.reshape((batch_size, *outputs.shape[2:]))
|
|
152
|
+
|
|
153
|
+
return outputs
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
# ruff: noqa: E741
|
|
157
|
+
def update_cache(
|
|
158
|
+
is_prefill,
|
|
159
|
+
cache,
|
|
160
|
+
indices,
|
|
161
|
+
operand,
|
|
162
|
+
prefill_seq_len=None,
|
|
163
|
+
sliding_window=None,
|
|
164
|
+
) -> jax.Array:
|
|
165
|
+
|
|
166
|
+
# (8, 55640, 32, 128) (1, 8, 256, 128) -> K (8, 8, 32, 128)
|
|
167
|
+
# I = B * T // S
|
|
168
|
+
# k cache, operand
|
|
169
|
+
|
|
170
|
+
B, K, T, H = operand.shape
|
|
171
|
+
K_c, L, S, H = cache.shape
|
|
172
|
+
assert K == K_c
|
|
173
|
+
# NOTE: The cache updating is pretty tricky:
|
|
174
|
+
# 1. The random access updating cache is not as performant as the slice updating.
|
|
175
|
+
# If the random access is necessary, make sure the indexing count is as small as possible.
|
|
176
|
+
# 2. The random access updating may trigger extra tranpose (memory copy) of cache,
|
|
177
|
+
# which is a disaster because the cache is huge. This is a data formatting op inserted by
|
|
178
|
+
# the XLA compiler and not well documented.
|
|
179
|
+
# To mitigate the issues above:
|
|
180
|
+
# For prefill:
|
|
181
|
+
# We reshape the operand so that we can update the cache in block wise, which only requires the block indices.
|
|
182
|
+
# For decode:
|
|
183
|
+
# We reshape the cache so that we can update the cache in token wise, which only requires the token indices (block_id + offset).
|
|
184
|
+
if is_prefill:
|
|
185
|
+
# In the case of sliding window, we should select sliding_window tokens from actual prompt, not from the padded tokens.
|
|
186
|
+
if sliding_window and T > sliding_window:
|
|
187
|
+
assert B == 1
|
|
188
|
+
start_index = jax.lax.max(0, prefill_seq_len - sliding_window)
|
|
189
|
+
operand = jax.lax.dynamic_slice_in_dim(
|
|
190
|
+
operand, start_index, sliding_window,
|
|
191
|
+
axis=2) # TODO: @pooyam Perf check this.
|
|
192
|
+
T = sliding_window
|
|
193
|
+
|
|
194
|
+
I = B * T // S
|
|
195
|
+
# cache: (K, L, S, H)
|
|
196
|
+
# operand: (B, K, T, H) -> (K, I, S, H)
|
|
197
|
+
# indices: (B, T // S) -> (I,)
|
|
198
|
+
operand = jnp.swapaxes(operand, 0, 1).reshape(K, I, S, H)
|
|
199
|
+
indices = indices.reshape(I)
|
|
200
|
+
cache = cache.at[:, indices, :, :].set(operand)
|
|
201
|
+
else:
|
|
202
|
+
# cache: (K, L, S, H) -> (K, L * S, H)
|
|
203
|
+
# operand: (B, K, 1, H) -> (K, B, H)
|
|
204
|
+
# indices: (B,)
|
|
205
|
+
cache = cache.reshape(K, L * S, H)
|
|
206
|
+
operand = jnp.swapaxes(operand, 0, 1).reshape(K, B, H)
|
|
207
|
+
# NOTE: `cache.[:, indices, :].set()` will trigger the extra tranpose of the cache.
|
|
208
|
+
# The `jnp.arange(K)[..., None]` trick is to avoid it. WTF?
|
|
209
|
+
cache = cache.at[jnp.arange(K)[..., None], indices, :].set(operand)
|
|
210
|
+
cache = cache.reshape(K, L, S, H)
|
|
211
|
+
return cache
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
@functools.partial(
|
|
215
|
+
jax.jit, static_argnames=["window_size", "attn_logits_soft_cap", "is_mqa"])
|
|
216
|
+
def apply_splash(q, k, v, window_size, attn_logits_soft_cap,
|
|
217
|
+
is_mqa) -> jax.Array:
|
|
218
|
+
# q: (batch_size, num_heads, seq_len, head_dim)
|
|
219
|
+
num_heads = q.shape[1]
|
|
220
|
+
q_seq_len = q.shape[2]
|
|
221
|
+
kv_seq_len = k.shape[2]
|
|
222
|
+
assert kv_seq_len >= q_seq_len
|
|
223
|
+
|
|
224
|
+
masks = [
|
|
225
|
+
mask_lib.LocalMask((q_seq_len, kv_seq_len), (window_size, 0),
|
|
226
|
+
kv_seq_len - q_seq_len) for _ in range(num_heads)
|
|
227
|
+
]
|
|
228
|
+
mask = mask_lib.MultiHeadMask(tuple((m for m in masks)))
|
|
229
|
+
block_sizes = splash.BlockSizes.get_default()
|
|
230
|
+
|
|
231
|
+
if is_mqa:
|
|
232
|
+
attn = splash.make_splash_mqa_single_device(
|
|
233
|
+
mask,
|
|
234
|
+
block_sizes=block_sizes,
|
|
235
|
+
attn_logits_soft_cap=attn_logits_soft_cap)
|
|
236
|
+
else:
|
|
237
|
+
attn = splash.make_splash_mha_single_device(
|
|
238
|
+
mask,
|
|
239
|
+
block_sizes=block_sizes,
|
|
240
|
+
attn_logits_soft_cap=attn_logits_soft_cap)
|
|
241
|
+
attn = jax.vmap(attn)
|
|
242
|
+
outputs = attn(q, k, v, None)
|
|
243
|
+
|
|
244
|
+
return outputs
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def sharded_splash_attention(
|
|
248
|
+
mesh: Mesh,
|
|
249
|
+
window_size: Optional[int] = None,
|
|
250
|
+
attn_logits_soft_cap: Optional[float] = None,
|
|
251
|
+
is_mqa: bool = False,
|
|
252
|
+
) -> Callable[..., Any]:
|
|
253
|
+
in_specs = (
|
|
254
|
+
P("data", "model", None, None), # q
|
|
255
|
+
P("data", "model", None, None), # k
|
|
256
|
+
P("data", "model", None, None), # vx
|
|
257
|
+
)
|
|
258
|
+
out_specs = P("data", "model", None, None)
|
|
259
|
+
return jax.jit(
|
|
260
|
+
shard_map.shard_map(
|
|
261
|
+
functools.partial(
|
|
262
|
+
apply_splash,
|
|
263
|
+
window_size=window_size,
|
|
264
|
+
attn_logits_soft_cap=attn_logits_soft_cap,
|
|
265
|
+
is_mqa=is_mqa,
|
|
266
|
+
),
|
|
267
|
+
mesh=mesh,
|
|
268
|
+
in_specs=in_specs,
|
|
269
|
+
out_specs=out_specs,
|
|
270
|
+
check_rep=False,
|
|
271
|
+
))
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def sharded_ragged_paged_attention(
|
|
275
|
+
mesh: Mesh,
|
|
276
|
+
q: jax.Array,
|
|
277
|
+
k: jax.Array,
|
|
278
|
+
v: jax.Array,
|
|
279
|
+
kv_cache: jax.Array,
|
|
280
|
+
kv_lens: jax.Array,
|
|
281
|
+
page_indices: jax.Array,
|
|
282
|
+
cu_q_lens: jax.Array,
|
|
283
|
+
distribution: jax.Array,
|
|
284
|
+
attention_sink: jax.Array | None,
|
|
285
|
+
sm_scale: float,
|
|
286
|
+
attention_chunk_size: int | None = None,
|
|
287
|
+
q_scale: float | None = None,
|
|
288
|
+
k_scale: float | None = None,
|
|
289
|
+
v_scale: float | None = None,
|
|
290
|
+
):
|
|
291
|
+
"""Shards along KV heads."""
|
|
292
|
+
|
|
293
|
+
qkv_spec = P(ShardingAxisName.ATTN_DATA, ShardingAxisName.ATTN_HEAD, None)
|
|
294
|
+
kv_cache_spec = P(ShardingAxisName.ATTN_DATA, None,
|
|
295
|
+
ShardingAxisName.ATTN_HEAD, None, None)
|
|
296
|
+
in_specs = (
|
|
297
|
+
qkv_spec, # q
|
|
298
|
+
qkv_spec, # k
|
|
299
|
+
qkv_spec, # v
|
|
300
|
+
kv_cache_spec, # kv cache
|
|
301
|
+
P(ShardingAxisName.ATTN_DATA), # kv_lens
|
|
302
|
+
P(ShardingAxisName.ATTN_DATA), # page_indices
|
|
303
|
+
P(ShardingAxisName.ATTN_DATA), # cu_q_lens
|
|
304
|
+
P(ShardingAxisName.ATTN_DATA), # distribution
|
|
305
|
+
)
|
|
306
|
+
out_specs = (qkv_spec, kv_cache_spec)
|
|
307
|
+
|
|
308
|
+
args = (q, k, v, kv_cache, kv_lens, page_indices, cu_q_lens, distribution)
|
|
309
|
+
|
|
310
|
+
use_hd64 = q.shape[-1] == 64
|
|
311
|
+
func = ragged_paged_attention_hd64 if use_hd64 else ragged_paged_attention
|
|
312
|
+
|
|
313
|
+
if attention_sink is not None:
|
|
314
|
+
if not use_hd64:
|
|
315
|
+
raise NotImplementedError(
|
|
316
|
+
"Attention sink support is only available when head_dim==64")
|
|
317
|
+
|
|
318
|
+
in_specs += (P(ShardingAxisName.ATTN_HEAD), )
|
|
319
|
+
args += (attention_sink, )
|
|
320
|
+
|
|
321
|
+
def _ragged_paged_attention(*args):
|
|
322
|
+
return func(
|
|
323
|
+
*args,
|
|
324
|
+
sm_scale=sm_scale,
|
|
325
|
+
sliding_window=attention_chunk_size,
|
|
326
|
+
q_scale=q_scale,
|
|
327
|
+
k_scale=k_scale,
|
|
328
|
+
v_scale=v_scale,
|
|
329
|
+
)
|
|
330
|
+
|
|
331
|
+
return shard_map.shard_map(
|
|
332
|
+
_ragged_paged_attention,
|
|
333
|
+
mesh=mesh,
|
|
334
|
+
in_specs=in_specs,
|
|
335
|
+
out_specs=out_specs,
|
|
336
|
+
check_rep=False,
|
|
337
|
+
)(*args)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def attention(
|
|
341
|
+
kv_cache: jax.Array,
|
|
342
|
+
q: jax.Array,
|
|
343
|
+
k: jax.Array,
|
|
344
|
+
v: jax.Array,
|
|
345
|
+
attention_metadata: AttentionMetadata,
|
|
346
|
+
mesh: Mesh,
|
|
347
|
+
head_dim_original: int | None = None, # before padding,
|
|
348
|
+
attention_chunk_size: int | None = None,
|
|
349
|
+
q_scale: float | None = None,
|
|
350
|
+
k_scale: float | None = None,
|
|
351
|
+
v_scale: float | None = None,
|
|
352
|
+
sinks: jax.Array | None = None,
|
|
353
|
+
) -> Tuple[jax.Array, jax.Array]:
|
|
354
|
+
# T: seq_len
|
|
355
|
+
# N: num_heads
|
|
356
|
+
# K: num_kv_heads
|
|
357
|
+
# D: hidden_size
|
|
358
|
+
# H: head_dim
|
|
359
|
+
# L: num_blocks
|
|
360
|
+
# S: block_size
|
|
361
|
+
|
|
362
|
+
# TODO(jevinjiang, cuiq): transpose q weight offline.
|
|
363
|
+
# q: (T, N, H)
|
|
364
|
+
# k,v: (T, K, H)
|
|
365
|
+
|
|
366
|
+
if head_dim_original is None:
|
|
367
|
+
head_dim_original = q.shape[-1]
|
|
368
|
+
|
|
369
|
+
md = attention_metadata
|
|
370
|
+
|
|
371
|
+
# (T, N, H)
|
|
372
|
+
output, kv_cache = sharded_ragged_paged_attention(
|
|
373
|
+
mesh,
|
|
374
|
+
q,
|
|
375
|
+
k,
|
|
376
|
+
v,
|
|
377
|
+
kv_cache,
|
|
378
|
+
md.seq_lens,
|
|
379
|
+
md.block_tables,
|
|
380
|
+
md.query_start_loc,
|
|
381
|
+
md.request_distribution,
|
|
382
|
+
sinks,
|
|
383
|
+
sm_scale=head_dim_original**-0.5,
|
|
384
|
+
attention_chunk_size=attention_chunk_size,
|
|
385
|
+
q_scale=q_scale,
|
|
386
|
+
k_scale=k_scale,
|
|
387
|
+
v_scale=v_scale,
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
return kv_cache, output
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
from dataclasses import dataclass, field
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import jax
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@functools.partial(
|
|
9
|
+
jax.tree_util.register_dataclass,
|
|
10
|
+
data_fields=[
|
|
11
|
+
"input_positions",
|
|
12
|
+
"block_tables",
|
|
13
|
+
"seq_lens",
|
|
14
|
+
"query_start_loc",
|
|
15
|
+
"request_distribution",
|
|
16
|
+
],
|
|
17
|
+
meta_fields=[],
|
|
18
|
+
drop_fields=["query_start_loc_cpu", "seq_lens_cpu"],
|
|
19
|
+
)
|
|
20
|
+
@dataclass
|
|
21
|
+
class AttentionMetadata(object):
|
|
22
|
+
# (padded_total_num_scheduled_tokens,)
|
|
23
|
+
input_positions: jax.Array
|
|
24
|
+
# (max_num_seqs * max_num_blocks_per_req,)
|
|
25
|
+
block_tables: jax.Array = None
|
|
26
|
+
# (max_num_seqs,)
|
|
27
|
+
seq_lens: jax.Array = None
|
|
28
|
+
# (max_num_seqs + 1,)
|
|
29
|
+
query_start_loc: jax.Array = None
|
|
30
|
+
# (3,)
|
|
31
|
+
request_distribution: jax.Array = None
|
|
32
|
+
|
|
33
|
+
query_start_loc_cpu: Any = field(init=False)
|
|
34
|
+
seq_lens_cpu: Any = field(init=False)
|