-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmodel.py
278 lines (245 loc) · 10.5 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# ===----------------------------------------------------------------------=== #
# Copyright (c) 2024, Modular Inc. All rights reserved.
#
# Licensed under the Apache License v2.0 with LLVM Exceptions:
# https://llvm.org/LICENSE.txt
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===----------------------------------------------------------------------=== #
from __future__ import annotations
import logging
import numpy as np
from dataprocessing import batch_padded_tokens_and_mask
from max.driver import Tensor
from max.dtype import DType
from max.engine import InferenceSession, Model
from max.pipelines import ModelOutputs, PipelineModel, TextContext
from max.pipelines.kv_cache import (
KVCacheManager,
KVCacheParams,
KVCacheStrategy,
estimate_kv_cache_size,
load_kv_manager,
)
from .graph import _build_graph
class CoderModel(PipelineModel):
def execute(self, *model_inputs: Tensor) -> ModelOutputs:
model_outputs = self.model.execute(
*model_inputs,
copy_inputs_to_device=(
self.pipeline_config.cache_strategy == KVCacheStrategy.NAIVE
),
)
if self.pipeline_config.enable_echo:
return ModelOutputs(
next_token_logits=model_outputs[0], logits=model_outputs[1]
)
else:
return ModelOutputs(next_token_logits=model_outputs[0])
def _prepare_continuous_initial_token_inputs(
self, context_batch: list[TextContext]
) -> tuple[Tensor, ...]:
# Get tokens and seq_ids
tokens = [ctx.next_tokens for ctx in context_batch]
# Get input_row_offsets: start and end position of each batch in the
# combined total_seq_len dimension.
input_row_offsets = Tensor.from_numpy(
np.cumsum(
[0] + [ctx.seq_len for ctx in context_batch],
dtype=np.uint32,
)
).to(self.pipeline_config.device)
# Create a ragged token vector of length: sum(len(t) for t in tokens).
next_tokens_batch = np.concatenate(tokens)
next_tokens_batch = Tensor.from_numpy(next_tokens_batch).to(
self.pipeline_config.device
)
return (next_tokens_batch, input_row_offsets)
def _prepare_naive_initial_token_inputs(
self, context_batch: list[TextContext]
) -> tuple[Tensor, ...]:
# Get tokens and seq_ids
tokens = [ctx.next_tokens for ctx in context_batch]
# Pad tokens and compute attention mask for the batch.
max_seq_len = self.kv_manager.max_sequence_length
start_pos = [max_seq_len] * len(context_batch)
next_tokens_batch, _, attn_mask = batch_padded_tokens_and_mask(
start_pos=start_pos,
tokens=tokens,
pad_to_multiple_of=self.pipeline_config.pad_to_multiple_of,
)
return (next_tokens_batch, attn_mask)
def prepare_initial_token_inputs(
self, context_batch: list[TextContext]
) -> tuple[Tensor, ...]:
"""Prepare the inputs for the first pass in multistep execution."""
if self.pipeline_config.cache_strategy == KVCacheStrategy.CONTINUOUS:
return self._prepare_continuous_initial_token_inputs(context_batch)
else:
return self._prepare_naive_initial_token_inputs(context_batch)
def _prepare_continuous_next_token_inputs(
self,
next_tokens: Tensor,
prev_model_inputs: tuple[Tensor, ...],
):
_, old_row_offsets = prev_model_inputs
row_offsets_size = old_row_offsets.shape[0]
next_row_offsets = self._input_row_offsets_prealloc[:row_offsets_size]
next_token_inputs = (next_tokens, next_row_offsets)
return next_token_inputs
def _prepare_naive_next_token_inputs(
self,
next_tokens: Tensor,
prev_model_inputs: tuple[Tensor, ...],
):
prev_tokens, prev_attn_mask = prev_model_inputs
batch_size = prev_tokens.shape[0]
start_pos = [prev_attn_mask.shape[-1]] * batch_size
next_tokens_batch, _, attn_mask = batch_padded_tokens_and_mask(
start_pos=start_pos,
tokens=next_tokens,
pad_to_multiple_of=self.pipeline_config.pad_to_multiple_of,
)
next_token_inputs = (next_tokens_batch, attn_mask)
return next_token_inputs
def prepare_next_token_inputs(
self,
next_tokens: Tensor,
prev_model_inputs: tuple[Tensor, ...],
) -> tuple[Tensor, ...]:
"""Prepare the inputs for the next token in multistep execution.
This should avoid any device synchronization or copy operations.
"""
if self.pipeline_config.cache_strategy == KVCacheStrategy.CONTINUOUS:
return self._prepare_continuous_next_token_inputs(
next_tokens, prev_model_inputs
)
else:
return self._prepare_naive_next_token_inputs(
next_tokens, prev_model_inputs
)
def _get_kv_params(self) -> KVCacheParams:
cache_dtype = (
DType.float32
if self.pipeline_config.quantization_encoding.quantization_encoding
is not None
else self.pipeline_config.dtype
)
return KVCacheParams(
dtype=cache_dtype,
n_kv_heads=self.pipeline_config.huggingface_config.num_key_value_heads,
head_dim=self.pipeline_config.huggingface_config.hidden_size
// self.pipeline_config.huggingface_config.num_attention_heads,
cache_strategy=self.pipeline_config.cache_strategy,
)
def load_kv_manager(self, session: InferenceSession) -> KVCacheManager:
return load_kv_manager(
params=self._get_kv_params(),
max_cache_batch_size=self.pipeline_config.max_cache_batch_size,
max_seq_len=self.pipeline_config.huggingface_config.max_seq_len,
num_layers=self.pipeline_config.huggingface_config.num_hidden_layers,
devices=[self.pipeline_config.device],
session=session,
)
def estimate_kv_cache_size(self) -> int:
return estimate_kv_cache_size(
params=self._get_kv_params(),
max_cache_batch_size=self.pipeline_config.max_cache_batch_size,
max_seq_len=self.pipeline_config.huggingface_config.max_seq_len,
num_layers=self.pipeline_config.huggingface_config.num_hidden_layers,
devices=[self.pipeline_config.device],
)
def load_model(
self,
session: InferenceSession,
) -> Model:
# Pre-allocate a buffer for input_row_offsets in multistep execution.
# We do this to avoid materializing and copying a buffer with each multistep step
self._input_row_offsets_prealloc = Tensor.from_numpy(
np.arange(
self.pipeline_config.max_cache_batch_size + 1, dtype=np.uint32
)
).to(self.pipeline_config.device)
# Read in weights.
self._weights = self.pipeline_config.load_weights()
if serialized_path := self.pipeline_config.serialized_model_path:
# Hydrate all weights to be referenced by the serialized path.
weights_registry = {}
for name, tensor in self._weights._tensors.items():
weights_registry[name] = tensor.data
logging.info("Loading serialized model from ", serialized_path)
return session.load(
serialized_path, weights_registry=weights_registry
)
else:
logging.info("Building model...")
graph = _build_graph(
self.pipeline_config,
self._weights,
self._get_kv_params(),
kv_manager=self.kv_manager,
)
logging.info("Compiling...")
model = session.load(
graph,
weights_registry=self._weights.allocated_weights, # type: ignore
)
if (
export_path
:= self.pipeline_config.save_to_serialized_model_path
):
logging.info("Exporting serialized model to %s", export_path)
model._export_mef(export_path)
return model
def compute_log_probabilities(
self,
model_inputs: Sequence[Tensor],
model_outputs: ModelOutputs,
next_tokens: Tensor,
batch_top_n: list[int],
batch_echo: list[bool],
) -> list[LogProbabilities | None] | None:
if any(echo for echo in batch_echo):
if model_outputs.logits is None:
warnings.warn(
"Could not get logprobs with echo because the full logits"
f" were not returned by {self.pipeline_config.short_name}"
" model. Please ensure that this model is started with "
"`--enable-echo`."
)
assert (
not self.pipeline_config.enable_echo
), "Echo was enabled but logits were not returned."
return None
logits = model_outputs.logits.to(CPU()).to_numpy()
next_token_logits = model_outputs.next_token_logits.to(CPU()).to_numpy()
sampled_tokens = next_tokens.to(CPU()).to_numpy()
# Handle batched inputs.
token_tensor, _, valid_length_tensor = model_inputs
tokens = token_tensor.to(CPU()).to_numpy()
valid_lengths = valid_length_tensor.to(CPU()).to_numpy()
def _get_logits_and_samples(
batch_index: int, echo: bool
) -> tuple[np.ndarray, np.ndarray]:
if echo:
seq_len = valid_lengths[batch_index]
padded_tokens = tokens[batch_index]
assert model_outputs.logits is not None
batch_logits = logits[batch_index, :seq_len]
samples = np.concatenate(
(
padded_tokens[1:seq_len],
sampled_tokens[batch_index : batch_index + 1],
)
)
else:
batch_logits = next_token_logits[batch_index : batch_index + 1]
samples = sampled_tokens[batch_index : batch_index + 1]
return batch_logits, samples
return compute_log_probabilities(
_get_logits_and_samples, batch_top_n, batch_echo
)