tpu-inference 0.11.1__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_adapters.py +83 -0
- tests/core/test_core_tpu.py +523 -0
- tests/core/test_disagg_executor.py +60 -0
- tests/core/test_disagg_utils.py +53 -0
- tests/core/test_init.py +49 -0
- tests/kernels/__init__.py +0 -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_test.py +504 -0
- tests/lora/__init__.py +0 -0
- tests/lora/test_lora.py +123 -0
- tests/test_base.py +201 -0
- tests/test_quantization.py +836 -0
- tests/test_tpu_info.py +120 -0
- tests/test_utils.py +218 -0
- tests/tpu_backend_test.py +59 -0
- tpu_inference/__init__.py +30 -0
- tpu_inference/adapters/__init__.py +0 -0
- tpu_inference/adapters/vllm_adapters.py +42 -0
- tpu_inference/adapters/vllm_config_adapters.py +134 -0
- tpu_inference/backend.py +69 -0
- tpu_inference/core/__init__.py +0 -0
- tpu_inference/core/adapters.py +153 -0
- tpu_inference/core/core_tpu.py +776 -0
- tpu_inference/core/disagg_executor.py +117 -0
- tpu_inference/core/disagg_utils.py +51 -0
- tpu_inference/di/__init__.py +0 -0
- tpu_inference/di/abstracts.py +28 -0
- tpu_inference/di/host.py +76 -0
- tpu_inference/di/interfaces.py +51 -0
- tpu_inference/distributed/__init__.py +0 -0
- tpu_inference/distributed/tpu_connector.py +699 -0
- tpu_inference/distributed/utils.py +59 -0
- tpu_inference/executors/__init__.py +0 -0
- tpu_inference/executors/ray_distributed_executor.py +346 -0
- tpu_inference/experimental/__init__.py +0 -0
- tpu_inference/experimental/llama3_jax_stashed.py +258 -0
- tpu_inference/interfaces/__init__.py +0 -0
- tpu_inference/interfaces/cache.py +31 -0
- tpu_inference/interfaces/config.py +47 -0
- tpu_inference/interfaces/config_parts.py +117 -0
- tpu_inference/interfaces/engine.py +51 -0
- tpu_inference/interfaces/outputs.py +22 -0
- tpu_inference/interfaces/params.py +21 -0
- tpu_inference/interfaces/platform.py +74 -0
- tpu_inference/interfaces/request.py +39 -0
- tpu_inference/interfaces/scheduler.py +31 -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/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 +1447 -0
- tpu_inference/kernels/ragged_paged_attention/v3/tuned_block_sizes.py +3834 -0
- tpu_inference/kernels/ragged_paged_attention/v3/util.py +47 -0
- tpu_inference/layers/__init__.py +0 -0
- tpu_inference/layers/common/__init__.py +0 -0
- tpu_inference/layers/common/attention_metadata.py +34 -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 +254 -0
- tpu_inference/layers/jax/attention/deepseek_v3_attention.py +354 -0
- tpu_inference/layers/jax/attention/llama4_attention.py +153 -0
- tpu_inference/layers/jax/attention_interface.py +356 -0
- tpu_inference/layers/jax/base.py +151 -0
- tpu_inference/layers/jax/binary_search.py +295 -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/moe.py +209 -0
- tpu_inference/layers/jax/rope.py +172 -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 +95 -0
- tpu_inference/layers/jax/sample/sampling_metadata.py +69 -0
- tpu_inference/layers/jax/sharding.py +406 -0
- tpu_inference/layers/jax/transformer_block.py +76 -0
- tpu_inference/layers/vllm/__init__.py +0 -0
- tpu_inference/layers/vllm/attention.py +184 -0
- tpu_inference/layers/vllm/fused_moe.py +399 -0
- tpu_inference/layers/vllm/linear_common.py +186 -0
- tpu_inference/layers/vllm/quantization/__init__.py +34 -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 +121 -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/unquantized.py +263 -0
- tpu_inference/layers/vllm/sharding.py +151 -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 +308 -0
- tpu_inference/mock/__init__.py +0 -0
- tpu_inference/mock/vllm_config_utils.py +28 -0
- tpu_inference/mock/vllm_envs.py +1233 -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 +433 -0
- tpu_inference/models/jax/__init__.py +0 -0
- tpu_inference/models/jax/deepseek_v3.py +868 -0
- tpu_inference/models/jax/llama3.py +366 -0
- tpu_inference/models/jax/llama4.py +473 -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 +976 -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 +164 -0
- tpu_inference/models/jax/utils/quantization/__init__.py +0 -0
- tpu_inference/models/jax/utils/quantization/quantization_utils.py +588 -0
- tpu_inference/models/jax/utils/weight_utils.py +510 -0
- tpu_inference/models/vllm/__init__.py +0 -0
- tpu_inference/models/vllm/vllm_model_wrapper.py +272 -0
- tpu_inference/models/vllm/vllm_model_wrapper_context.py +45 -0
- tpu_inference/platforms/__init__.py +2 -0
- tpu_inference/platforms/tpu_jax.py +257 -0
- tpu_inference/runner/__init__.py +0 -0
- tpu_inference/runner/block_table_jax.py +122 -0
- tpu_inference/runner/compilation_manager.py +672 -0
- tpu_inference/runner/input_batch_jax.py +435 -0
- tpu_inference/runner/kv_cache.py +119 -0
- tpu_inference/runner/kv_cache_manager.py +460 -0
- tpu_inference/runner/lora_utils.py +92 -0
- tpu_inference/runner/multimodal_manager.py +208 -0
- tpu_inference/runner/persistent_batch_manager.py +244 -0
- tpu_inference/runner/speculative_decoding_manager.py +250 -0
- tpu_inference/runner/structured_decoding_manager.py +89 -0
- tpu_inference/runner/tpu_jax_runner.py +771 -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 +334 -0
- tpu_inference/tpu_info.py +77 -0
- tpu_inference/utils.py +294 -0
- tpu_inference/worker/__init__.py +0 -0
- tpu_inference/worker/_temporary_vllm_compat.py +129 -0
- tpu_inference/worker/base.py +100 -0
- tpu_inference/worker/tpu_worker_jax.py +321 -0
- tpu_inference-0.11.1.dist-info/METADATA +101 -0
- tpu_inference-0.11.1.dist-info/RECORD +168 -0
- tpu_inference-0.11.1.dist-info/WHEEL +5 -0
- tpu_inference-0.11.1.dist-info/licenses/LICENSE +201 -0
- tpu_inference-0.11.1.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TYPE_CHECKING, Optional
|
|
6
|
+
|
|
7
|
+
import jax
|
|
8
|
+
import jax.numpy as jnp
|
|
9
|
+
import numpy as np
|
|
10
|
+
from vllm.v1.core.sched.output import SchedulerOutput as VllmSchedulerOutput
|
|
11
|
+
from vllm.v1.outputs import DraftTokenIds
|
|
12
|
+
from vllm.v1.spec_decode.ngram_proposer import NgramProposer
|
|
13
|
+
|
|
14
|
+
from tpu_inference.runner import utils as runner_utils
|
|
15
|
+
from tpu_inference.spec_decode.jax.eagle3 import Eagle3Proposer
|
|
16
|
+
from tpu_inference.utils import device_array
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from tpu_inference.layers.common.attention_metadata import \
|
|
20
|
+
AttentionMetadata
|
|
21
|
+
from tpu_inference.runner.tpu_jax_runner import TPUModelRunner
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass
|
|
25
|
+
class SpecDecodeMetadata:
|
|
26
|
+
"""Metadata for speculative decoding on JAX/TPU, containing all necessary indices."""
|
|
27
|
+
draft_token_ids: jnp.ndarray
|
|
28
|
+
draft_lengths: jnp.ndarray
|
|
29
|
+
draft_lengths_cpu: np.ndarray
|
|
30
|
+
target_logits_indices: jnp.ndarray
|
|
31
|
+
bonus_logits_indices: jnp.ndarray
|
|
32
|
+
final_logits_indices: jnp.ndarray
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class SpeculativeDecodingManager:
|
|
36
|
+
|
|
37
|
+
def __init__(self, runner: TPUModelRunner):
|
|
38
|
+
self.runner = runner
|
|
39
|
+
# Cached draft tokens.
|
|
40
|
+
self._draft_token_ids: Optional[list[list[int]]] = None
|
|
41
|
+
|
|
42
|
+
def take_draft_token_ids(self) -> Optional[DraftTokenIds]:
|
|
43
|
+
if self._draft_token_ids is None:
|
|
44
|
+
return None
|
|
45
|
+
req_ids = self.runner.input_batch.req_ids
|
|
46
|
+
draft_token_ids = self._draft_token_ids
|
|
47
|
+
self._draft_token_ids = None
|
|
48
|
+
return DraftTokenIds(req_ids, draft_token_ids)
|
|
49
|
+
|
|
50
|
+
def propose_draft_token_ids(
|
|
51
|
+
self,
|
|
52
|
+
sampled_token_ids: list[list[int]],
|
|
53
|
+
aux_hidden_states: Optional[tuple[jnp.ndarray, ...]],
|
|
54
|
+
attn_metadata: AttentionMetadata,
|
|
55
|
+
spec_decode_metadata: Optional[SpecDecodeMetadata],
|
|
56
|
+
scheduler_output: Optional[VllmSchedulerOutput] = None,
|
|
57
|
+
input_ids: Optional[jnp.ndarray] = None,
|
|
58
|
+
) -> None:
|
|
59
|
+
if self.runner.speculative_config.method == "ngram":
|
|
60
|
+
assert isinstance(self.runner.drafter, NgramProposer)
|
|
61
|
+
self._draft_token_ids = self.runner.drafter.propose(
|
|
62
|
+
sampled_token_ids[:self.runner.input_batch.num_reqs],
|
|
63
|
+
self.runner.input_batch.req_ids,
|
|
64
|
+
self.runner.input_batch.num_tokens_no_spec,
|
|
65
|
+
self.runner.input_batch.token_ids_cpu,
|
|
66
|
+
self.runner.input_batch.spec_decode_unsupported_reqs)
|
|
67
|
+
elif self.runner.speculative_config.method == "eagle3":
|
|
68
|
+
self._draft_token_ids = self.propose_eagle3_draft_token_ids(
|
|
69
|
+
sampled_token_ids,
|
|
70
|
+
aux_hidden_states,
|
|
71
|
+
attn_metadata,
|
|
72
|
+
spec_decode_metadata,
|
|
73
|
+
scheduler_output,
|
|
74
|
+
input_ids,
|
|
75
|
+
)
|
|
76
|
+
else:
|
|
77
|
+
raise NotImplementedError(
|
|
78
|
+
f"Speculative decoding method "
|
|
79
|
+
f"'{self.runner.speculative_config.method}' is not supported.")
|
|
80
|
+
|
|
81
|
+
@functools.partial(jax.jit, static_argnums=(0, ))
|
|
82
|
+
def _convert_list_to_device_array(self, lst: list[int]) -> jnp.ndarray:
|
|
83
|
+
"""Jitted helper function to convert a list to a device array."""
|
|
84
|
+
arr = jnp.array(lst, dtype=jnp.int32)
|
|
85
|
+
return device_array(self.runner.mesh, arr)
|
|
86
|
+
|
|
87
|
+
def propose_eagle3_draft_token_ids(
|
|
88
|
+
self,
|
|
89
|
+
sampled_token_ids: list[list[int]],
|
|
90
|
+
aux_hidden_states: Optional[tuple[jnp.ndarray, ...]],
|
|
91
|
+
attn_metadata: AttentionMetadata,
|
|
92
|
+
spec_decode_metadata: Optional[SpecDecodeMetadata],
|
|
93
|
+
scheduler_output: VllmSchedulerOutput,
|
|
94
|
+
input_ids: jnp.ndarray,
|
|
95
|
+
) -> list[list[int]]:
|
|
96
|
+
assert isinstance(self.runner.drafter, Eagle3Proposer)
|
|
97
|
+
|
|
98
|
+
# TODO(woosuk): Refactor the loop.
|
|
99
|
+
req_ids = self.runner.input_batch.req_ids
|
|
100
|
+
next_token_ids: list[int] = []
|
|
101
|
+
for i, token_ids in enumerate(sampled_token_ids):
|
|
102
|
+
if token_ids:
|
|
103
|
+
# Common case.
|
|
104
|
+
next_token_id = token_ids[-1]
|
|
105
|
+
else:
|
|
106
|
+
# Partial prefill (rare case).
|
|
107
|
+
# Get the next token id from the request state.
|
|
108
|
+
req_id = req_ids[i]
|
|
109
|
+
req_state = self.runner.requests[req_id]
|
|
110
|
+
seq_len = (req_state.num_computed_tokens +
|
|
111
|
+
scheduler_output.num_scheduled_tokens[req_id])
|
|
112
|
+
next_token_id = req_state.get_token_id(seq_len)
|
|
113
|
+
next_token_ids.append(next_token_id)
|
|
114
|
+
|
|
115
|
+
# Pad the batch size to match with existing padding for target model
|
|
116
|
+
pad_len = attn_metadata.seq_lens.shape[0] - len(next_token_ids)
|
|
117
|
+
assert pad_len >= 0
|
|
118
|
+
next_token_ids += [0] * pad_len
|
|
119
|
+
|
|
120
|
+
next_token_ids = self._convert_list_to_device_array(next_token_ids)
|
|
121
|
+
|
|
122
|
+
if spec_decode_metadata is None:
|
|
123
|
+
num_rejected_tokens = None
|
|
124
|
+
else:
|
|
125
|
+
num_draft_tokens = spec_decode_metadata.draft_lengths_cpu
|
|
126
|
+
num_rejected_tokens = [
|
|
127
|
+
int(n) + 1 - len(sampled_token_ids[i]) if n > 0 else 0
|
|
128
|
+
for i, n in enumerate(num_draft_tokens)
|
|
129
|
+
]
|
|
130
|
+
|
|
131
|
+
pad_len = self.runner.max_num_reqs - len(num_rejected_tokens)
|
|
132
|
+
num_rejected_tokens += [0] * pad_len
|
|
133
|
+
num_rejected_tokens = self._convert_list_to_device_array(
|
|
134
|
+
num_rejected_tokens)
|
|
135
|
+
|
|
136
|
+
attn_metadata, target_token_ids, target_hidden_states = self.runner.drafter.prepare_inputs(
|
|
137
|
+
attn_metadata,
|
|
138
|
+
input_ids,
|
|
139
|
+
aux_hidden_states,
|
|
140
|
+
num_rejected_tokens,
|
|
141
|
+
)
|
|
142
|
+
self.runner.kv_caches, draft_token_ids = self.runner.drafter.propose(
|
|
143
|
+
kv_caches=self.runner.kv_caches,
|
|
144
|
+
next_token_ids=next_token_ids,
|
|
145
|
+
attn_metadata=attn_metadata,
|
|
146
|
+
target_token_ids=target_token_ids,
|
|
147
|
+
target_hidden_states=target_hidden_states,
|
|
148
|
+
)
|
|
149
|
+
result = draft_token_ids.tolist()
|
|
150
|
+
return result
|
|
151
|
+
|
|
152
|
+
def get_spec_decode_metadata(
|
|
153
|
+
self,
|
|
154
|
+
num_draft_tokens: np.ndarray,
|
|
155
|
+
cu_num_scheduled_tokens: np.ndarray,
|
|
156
|
+
padded_num_reqs: int,
|
|
157
|
+
) -> SpecDecodeMetadata:
|
|
158
|
+
# Inputs:
|
|
159
|
+
# cu_num_scheduled_tokens: [ 4, 104, 107, 207, 209]
|
|
160
|
+
# num_draft_tokens: [ 3, 0, 2, 0, 1]
|
|
161
|
+
# Outputs:
|
|
162
|
+
# cu_num_draft_tokens: [ 3, 3, 5, 5, 6]
|
|
163
|
+
# logits_indices: [ 0, 1, 2, 3, 103, 104, 105, 106,
|
|
164
|
+
# 206, 207, 208]
|
|
165
|
+
# target_logits_indices: [ 0, 1, 2, 5, 6, 9]
|
|
166
|
+
# bonus_logits_indices: [ 3, 4, 7, 8, 10]
|
|
167
|
+
|
|
168
|
+
# Compute the logits indices.
|
|
169
|
+
# [4, 1, 3, 1, 2]
|
|
170
|
+
num_sampled_tokens = num_draft_tokens + 1
|
|
171
|
+
|
|
172
|
+
# Step 1. cu_num_sampled_tokens: [4, 5, 8, 9, 11]
|
|
173
|
+
# arange: [0, 1, 2, 3, 0, 0, 1, 2, 0, 0, 1]
|
|
174
|
+
cu_num_sampled_tokens = np.cumsum(num_sampled_tokens)
|
|
175
|
+
arange = np.concatenate(
|
|
176
|
+
[self.runner.arange_cpu[:n] for n in num_sampled_tokens])
|
|
177
|
+
# Step 2. [0, 0, 0, 0, 103, 104, 104, 104, 206, 207, 207]
|
|
178
|
+
logits_indices = np.repeat(
|
|
179
|
+
cu_num_scheduled_tokens - num_sampled_tokens, num_sampled_tokens)
|
|
180
|
+
# Step 3. [0, 1, 2, 3, 103, 104, 105, 106, 206, 207, 208]
|
|
181
|
+
logits_indices += arange
|
|
182
|
+
# Compute the bonus logits indices.
|
|
183
|
+
bonus_logits_indices = cu_num_sampled_tokens - 1
|
|
184
|
+
|
|
185
|
+
# Compute the draft logits indices.
|
|
186
|
+
# arange: [0, 1, 2, 0, 1, 0]
|
|
187
|
+
arange = np.concatenate(
|
|
188
|
+
[self.runner.arange_cpu[:n] for n in num_draft_tokens])
|
|
189
|
+
# [0, 0, 0, 5, 5, 9]
|
|
190
|
+
target_logits_indices = np.repeat(
|
|
191
|
+
cu_num_sampled_tokens - num_sampled_tokens, num_draft_tokens)
|
|
192
|
+
# [0, 1, 2, 5, 6, 9]
|
|
193
|
+
target_logits_indices += arange
|
|
194
|
+
|
|
195
|
+
# Compute the draft token ids.
|
|
196
|
+
# draft_token_indices: [ 1, 2, 3, 105, 106, 208]
|
|
197
|
+
draft_token_ids = self.runner.input_ids_cpu[logits_indices]
|
|
198
|
+
draft_token_ids = draft_token_ids[target_logits_indices + 1]
|
|
199
|
+
padded_logits_length = runner_utils.get_padded_token_len(
|
|
200
|
+
self.runner.num_logits_paddings, logits_indices.shape[0])
|
|
201
|
+
padded_logits_indices = np.concatenate([
|
|
202
|
+
logits_indices,
|
|
203
|
+
np.zeros(padded_logits_length - logits_indices.shape[0],
|
|
204
|
+
dtype=np.int32)
|
|
205
|
+
])
|
|
206
|
+
|
|
207
|
+
assert bonus_logits_indices.shape[0] <= padded_num_reqs, (
|
|
208
|
+
f"bonus_logits_indices.shape[0]={bonus_logits_indices.shape[0]} "
|
|
209
|
+
f"padded_num_reqs={padded_num_reqs}")
|
|
210
|
+
|
|
211
|
+
padded_bonus_logits_indices = np.concatenate([
|
|
212
|
+
bonus_logits_indices,
|
|
213
|
+
np.zeros(padded_num_reqs - bonus_logits_indices.shape[0],
|
|
214
|
+
dtype=np.int32)
|
|
215
|
+
])
|
|
216
|
+
padded_num_draft_tokens = np.concatenate([
|
|
217
|
+
num_draft_tokens,
|
|
218
|
+
np.zeros(padded_num_reqs - num_draft_tokens.shape[0],
|
|
219
|
+
dtype=np.int32)
|
|
220
|
+
])
|
|
221
|
+
padded_draft_token_ids = np.concatenate([
|
|
222
|
+
draft_token_ids,
|
|
223
|
+
np.zeros(padded_logits_length - draft_token_ids.shape[0],
|
|
224
|
+
dtype=np.int32)
|
|
225
|
+
])
|
|
226
|
+
padded_target_logits_indices = np.concatenate([
|
|
227
|
+
target_logits_indices,
|
|
228
|
+
np.zeros(padded_logits_length - target_logits_indices.shape[0],
|
|
229
|
+
dtype=np.int32)
|
|
230
|
+
])
|
|
231
|
+
|
|
232
|
+
padded_num_draft_tokens_cpu = padded_num_draft_tokens
|
|
233
|
+
# CPU -> TPU copy.
|
|
234
|
+
(padded_num_draft_tokens, padded_draft_token_ids,
|
|
235
|
+
padded_logits_indices, padded_target_logits_indices,
|
|
236
|
+
padded_bonus_logits_indices) = device_array(
|
|
237
|
+
self.runner.mesh,
|
|
238
|
+
(padded_num_draft_tokens, padded_draft_token_ids,
|
|
239
|
+
padded_logits_indices, padded_target_logits_indices,
|
|
240
|
+
padded_bonus_logits_indices))
|
|
241
|
+
|
|
242
|
+
metadata = SpecDecodeMetadata(
|
|
243
|
+
draft_token_ids=padded_draft_token_ids,
|
|
244
|
+
draft_lengths=padded_num_draft_tokens,
|
|
245
|
+
draft_lengths_cpu=padded_num_draft_tokens_cpu,
|
|
246
|
+
target_logits_indices=padded_target_logits_indices,
|
|
247
|
+
bonus_logits_indices=padded_bonus_logits_indices,
|
|
248
|
+
final_logits_indices=padded_logits_indices,
|
|
249
|
+
)
|
|
250
|
+
return metadata
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
from typing import TYPE_CHECKING, Tuple
|
|
3
|
+
|
|
4
|
+
import jax
|
|
5
|
+
import jax.numpy as jnp
|
|
6
|
+
|
|
7
|
+
from tpu_inference.utils import device_array
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from vllm.v1.core.sched.output import \
|
|
11
|
+
SchedulerOutput as VllmSchedulerOutput
|
|
12
|
+
|
|
13
|
+
from tpu_inference.runner.tpu_jax_runner import TPUModelRunner
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class StructuredDecodingManager:
|
|
17
|
+
|
|
18
|
+
def __init__(self, runner: "TPUModelRunner"):
|
|
19
|
+
self.runner = runner
|
|
20
|
+
|
|
21
|
+
@functools.partial(jax.jit, static_argnums=(0, ))
|
|
22
|
+
def structured_decode_fn(self, require_struct_decoding: jax.Array,
|
|
23
|
+
grammar_bitmask: jax.Array, logits: jax.Array,
|
|
24
|
+
arange: jax.Array) -> jax.Array:
|
|
25
|
+
return jax.lax.cond(
|
|
26
|
+
jnp.any(require_struct_decoding),
|
|
27
|
+
lambda: self._apply_grammar_bitmask_kernel(
|
|
28
|
+
logits, grammar_bitmask, require_struct_decoding, arange),
|
|
29
|
+
lambda: logits)
|
|
30
|
+
|
|
31
|
+
@functools.partial(jax.jit, static_argnums=(0, ))
|
|
32
|
+
def _apply_grammar_bitmask_kernel(self, logits: jax.Array,
|
|
33
|
+
grammar_bitmask: jax.Array,
|
|
34
|
+
require_struct_decoding: jax.Array,
|
|
35
|
+
arange: jax.Array) -> jax.Array:
|
|
36
|
+
|
|
37
|
+
# Unpack the bitmask for the entire batch at once.
|
|
38
|
+
# grammar_bitmask: (B, N) where B=num_reqs, N=cdiv(vocab_size, 32)
|
|
39
|
+
# arange: (32,)
|
|
40
|
+
# (B, N, 1) and (1, 1, 32) broadcast to (B, N, 32)
|
|
41
|
+
unpacked_bitmask = jnp.right_shift(grammar_bitmask[:, :, None],
|
|
42
|
+
arange[None, None, :]) & 1 == 0
|
|
43
|
+
|
|
44
|
+
# Reshape to (B, vocab_size) and apply to logits.
|
|
45
|
+
# (B, N * 32) -> (B, vocab_size)
|
|
46
|
+
unpacked_bitmask = unpacked_bitmask.reshape(
|
|
47
|
+
logits.shape[0], -1)[:, :self.runner.vocab_size]
|
|
48
|
+
|
|
49
|
+
masked_logits = jnp.where(unpacked_bitmask, -jnp.inf, logits)
|
|
50
|
+
|
|
51
|
+
return jnp.where(require_struct_decoding, masked_logits, logits)
|
|
52
|
+
|
|
53
|
+
def prepare_structured_decoding_input(
|
|
54
|
+
self, logits: jax.Array, scheduler_output: "VllmSchedulerOutput"
|
|
55
|
+
) -> Tuple[jax.Array, jax.Array, jax.Array]:
|
|
56
|
+
grammar_bitmask = scheduler_output.grammar_bitmask
|
|
57
|
+
assert grammar_bitmask is not None
|
|
58
|
+
num_reqs, _ = logits.shape
|
|
59
|
+
|
|
60
|
+
# Reset pre-allocated tensors
|
|
61
|
+
self.runner.grammar_bitmask_cpu.fill(0)
|
|
62
|
+
self.runner.require_structured_out_cpu.fill(0)
|
|
63
|
+
|
|
64
|
+
sorted_struct_requests = sorted(
|
|
65
|
+
scheduler_output.structured_output_request_ids.items(),
|
|
66
|
+
key=lambda item: item[1])
|
|
67
|
+
|
|
68
|
+
cumulative_mask_idx = 0
|
|
69
|
+
for req_id, _ in sorted_struct_requests:
|
|
70
|
+
if req_id not in self.runner.input_batch.req_id_to_index:
|
|
71
|
+
continue
|
|
72
|
+
batch_index = self.runner.input_batch.req_id_to_index[req_id]
|
|
73
|
+
self.runner.grammar_bitmask_cpu[batch_index] = grammar_bitmask[
|
|
74
|
+
cumulative_mask_idx]
|
|
75
|
+
# It's not guaranteed that all requests in this batch require
|
|
76
|
+
# structured output, so create a bool tensor to represent
|
|
77
|
+
# the requests that need structured output.
|
|
78
|
+
self.runner.require_structured_out_cpu[batch_index] = True
|
|
79
|
+
cumulative_mask_idx += 1
|
|
80
|
+
|
|
81
|
+
(require_structured_out_cpu,
|
|
82
|
+
grammar_bitmask_cpu, structured_decode_arange) = device_array(
|
|
83
|
+
self.runner.mesh,
|
|
84
|
+
(self.runner.require_structured_out_cpu[:num_reqs],
|
|
85
|
+
self.runner.grammar_bitmask_cpu[:num_reqs],
|
|
86
|
+
self.runner.structured_decode_arange))
|
|
87
|
+
|
|
88
|
+
return (require_structured_out_cpu, grammar_bitmask_cpu,
|
|
89
|
+
structured_decode_arange)
|