-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_decoder_onnxruntime.py
More file actions
344 lines (299 loc) · 12 KB
/
Copy pathencoder_decoder_onnxruntime.py
File metadata and controls
344 lines (299 loc) · 12 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""Run one Stackformers encoder-decoder eagerly and through cached ONNX Runtime graphs."""
from __future__ import annotations
from collections.abc import Sequence
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any, NamedTuple
import numpy as np
import onnxruntime as ort
import torch
import torch.nn as nn
from torch import Tensor
from stackformers import (
CachedDecoderWrapper,
DecoderCrossAttentionCache,
DecoderCrossAttentionCacheBuilder,
PaddedInput,
TransformerDecoder,
TransformerEncoder,
make_padded_input,
plain_decoder_config,
plain_encoder_config,
)
DIM = 128
HEADS = 2
LAYERS = 2
EXAMPLE_BATCH = 2
MAX_SOURCE_TOKENS = 6
MAX_TARGET_TOKENS = 4
CROSS_CACHE_NAMES = ("cross_kv_cache", "context_mask")
class ExampleResult(NamedTuple):
"""Files and parity measurements produced by :func:`run_example`."""
encoder_path: Path
cache_builder_path: Path
cached_decoder_path: Path
prefix_lengths: tuple[int, ...]
max_abs_error: float
class EncoderModel(nn.Module):
"""Standalone source encoder that returns decoder-ready memory."""
def __init__(self) -> None:
"""Build an export-friendly Stackformers encoder preset."""
super().__init__()
self.encoder = TransformerEncoder(plain_encoder_config(DIM, HEADS, LAYERS, ff_mult=3.0))
def forward(self, source_input: PaddedInput) -> PaddedInput:
"""Encode source features while retaining their mask and absolute positions."""
context = self.encoder(source_input)
return source_input._replace(x=context)
class EncoderDecoderModel(nn.Module):
"""Compose independently executable Stackformers encoder and decoder modules."""
def __init__(self) -> None:
"""Build matching encoder and decoder presets with export-friendly dimensions."""
super().__init__()
self.encoder = EncoderModel()
self.decoder = TransformerDecoder(plain_decoder_config(DIM, HEADS, LAYERS, ff_mult=3.0))
def forward(self, source_input: PaddedInput, target_input: PaddedInput) -> Tensor:
"""Run the ordinary uncached encoder-decoder path."""
memory = self.encoder(source_input)
return self.decoder(target_input, memory)
def _make_input(batch: int, tokens: int, *, seed: int) -> PaddedInput:
"""Create deterministic dense padded input for export examples and runtime checks."""
generator = torch.Generator().manual_seed(seed)
x = torch.randn(batch, tokens, DIM, generator=generator)
mask = torch.ones(batch, tokens, dtype=torch.bool)
return make_padded_input(x, mask)
def _padded_input_dynamic_shapes(
module: nn.Module,
input: PaddedInput,
*,
token_dim_name: str,
) -> Any:
"""Describe dynamic batch and token dimensions for one padded-input graph."""
batch = torch.export.Dim("batch", min=1, max=EXAMPLE_BATCH)
tokens = torch.export.Dim(token_dim_name, min=1, max=MAX_SOURCE_TOKENS)
shapes = torch.export.ShapesCollection()
for tensor in input:
shapes[tensor] = {0: batch, 1: tokens}
return shapes.dynamic_shapes(module, (input,))
def _cached_decoder_dynamic_shapes(
module: nn.Module,
target_input: PaddedInput,
cross_cache: DecoderCrossAttentionCache,
self_kv_cache: Tensor,
step_i: Tensor,
) -> Any:
"""Describe dynamic batch, context, and past dimensions for one-token decoding."""
batch = torch.export.Dim("batch", min=1, max=EXAMPLE_BATCH)
source_tokens = torch.export.Dim("source_tokens", min=1, max=MAX_SOURCE_TOKENS)
past_tokens = torch.export.Dim("past_tokens", min=0, max=MAX_TARGET_TOKENS)
shapes = torch.export.ShapesCollection()
for tensor in target_input:
shapes[tensor] = {0: batch}
shapes[cross_cache.kv] = {2: batch, 4: source_tokens}
shapes[cross_cache.context.mask] = {0: batch, 1: source_tokens}
shapes[self_kv_cache] = {2: batch, 4: past_tokens}
return shapes.dynamic_shapes(
module,
(target_input, cross_cache, self_kv_cache, step_i),
)
def _export_models(
model: EncoderDecoderModel,
source_input: PaddedInput,
target_input: PaddedInput,
output_dir: Path,
) -> tuple[Path, Path, Path]:
"""Export separate encoder, cache-construction, and repeated-decoder graphs."""
output_dir.mkdir(parents=True, exist_ok=True)
encoder = model.encoder.eval()
cache_builder = DecoderCrossAttentionCacheBuilder(model.decoder).eval()
cached_decoder = CachedDecoderWrapper(model.decoder).eval()
with torch.no_grad():
memory = encoder(source_input)
cross_cache = cache_builder(memory)
target_token = _token(target_input, 0)
self_kv_cache = target_input.x.new_zeros(
LAYERS,
2,
target_input.x.shape[0],
model.decoder.config.self_attn.effective_kv_heads,
MAX_TARGET_TOKENS - 2,
model.decoder.config.self_attn.dim_head,
)
step_i = torch.tensor([MAX_TARGET_TOKENS - 2], dtype=torch.int64)
encoder_program = torch.onnx.export(
encoder,
(source_input,),
dynamo=True,
dynamic_shapes=_padded_input_dynamic_shapes(
encoder,
source_input,
token_dim_name="source_tokens",
),
input_names=["source_x", "source_mask", "source_positions"],
output_names=["memory_x", "memory_mask", "memory_positions"],
)
cache_program = torch.onnx.export(
cache_builder,
(memory,),
dynamo=True,
dynamic_shapes=_padded_input_dynamic_shapes(
cache_builder,
memory,
token_dim_name="source_tokens",
),
input_names=["memory_x", "memory_mask", "memory_positions"],
output_names=list(CROSS_CACHE_NAMES),
)
decoder_program = torch.onnx.export(
cached_decoder,
(target_token, cross_cache, self_kv_cache, step_i),
dynamo=True,
dynamic_shapes=_cached_decoder_dynamic_shapes(
cached_decoder,
target_token,
cross_cache,
self_kv_cache,
step_i,
),
input_names=[
"target_x",
"target_mask",
"target_positions",
*CROSS_CACHE_NAMES,
"self_kv_cache",
"step_i",
],
output_names=["decoder_output", "self_kv_cache_out"],
)
assert isinstance(encoder_program, torch.onnx.ONNXProgram)
assert isinstance(cache_program, torch.onnx.ONNXProgram)
assert isinstance(decoder_program, torch.onnx.ONNXProgram)
encoder_path = output_dir / "encoder.onnx"
cache_builder_path = output_dir / "decoder_cross_cache.onnx"
cached_decoder_path = output_dir / "cached_decoder.onnx"
encoder_program.save(encoder_path)
cache_program.save(cache_builder_path)
decoder_program.save(cached_decoder_path)
return encoder_path, cache_builder_path, cached_decoder_path
def _numpy(tensor: Tensor) -> np.ndarray:
"""Detach one CPU tensor for an ONNX Runtime input."""
return tensor.detach().numpy()
def _array_outputs(outputs: Sequence[object]) -> tuple[np.ndarray, ...]:
"""Validate tensor-valued ONNX Runtime outputs at the external-data boundary."""
arrays: list[np.ndarray] = []
for output in outputs:
if not isinstance(output, np.ndarray):
raise TypeError(f"expected ndarray output, received {type(output).__name__}")
arrays.append(output)
return tuple(arrays)
def _padded_feeds(prefix: str, input: PaddedInput) -> dict[str, np.ndarray]:
"""Flatten a padded input into the explicit names used by the ONNX graphs."""
return {
f"{prefix}_x": _numpy(input.x),
f"{prefix}_mask": _numpy(input.mask),
f"{prefix}_positions": _numpy(input.abs_positions),
}
def _prefix(input: PaddedInput, tokens: int) -> PaddedInput:
"""Take one growing autoregressive prefix without rebuilding its metadata."""
return PaddedInput(
x=input.x[:, :tokens],
mask=input.mask[:, :tokens],
abs_positions=input.abs_positions[:, :tokens],
)
def _token(input: PaddedInput, step: int) -> PaddedInput:
"""Select the one target token consumed by a cached decoder invocation."""
return PaddedInput(
x=input.x[:, step : step + 1],
mask=input.mask[:, step : step + 1],
abs_positions=input.abs_positions[:, step : step + 1],
)
def _run_onnxruntime(
model: EncoderDecoderModel,
encoder_path: Path,
cache_builder_path: Path,
cached_decoder_path: Path,
) -> tuple[tuple[int, ...], float]:
"""Pass encoder memory through the decoder cache and verify growing prefixes."""
source_input = _make_input(1, MAX_SOURCE_TOKENS - 1, seed=3)
full_target = _make_input(1, MAX_TARGET_TOKENS - 1, seed=4)
encoder_session = ort.InferenceSession(
str(encoder_path),
providers=["CPUExecutionProvider"],
)
cache_session = ort.InferenceSession(
str(cache_builder_path),
providers=["CPUExecutionProvider"],
)
decoder_session = ort.InferenceSession(
str(cached_decoder_path),
providers=["CPUExecutionProvider"],
)
memory_names = ("memory_x", "memory_mask", "memory_positions")
memory_values = _array_outputs(
encoder_session.run(list(memory_names), _padded_feeds("source", source_input))
)
memory_feeds = dict(zip(memory_names, memory_values, strict=True))
cache_values = _array_outputs(cache_session.run(list(CROSS_CACHE_NAMES), memory_feeds))
cache_feeds = dict(zip(CROSS_CACHE_NAMES, cache_values, strict=True))
self_kv_cache = np.zeros(
(LAYERS, 2, 1, HEADS, 0, DIM // HEADS),
dtype=np.float32,
)
prefix_lengths = tuple(range(1, full_target.x.shape[1] + 1))
max_abs_error = 0.0
with torch.no_grad():
for step, prefix_length in enumerate(prefix_lengths):
target_token = _token(full_target, step)
expected = model(source_input, _prefix(full_target, prefix_length))[:, -1:]
feeds = {
**_padded_feeds("target", target_token),
**cache_feeds,
"self_kv_cache": self_kv_cache,
"step_i": np.asarray([step], dtype=np.int64),
}
actual, self_kv_cache = _array_outputs(
decoder_session.run(
["decoder_output", "self_kv_cache_out"],
feeds,
)
)
np.testing.assert_allclose(actual, _numpy(expected), rtol=1e-4, atol=1e-5)
max_abs_error = max(max_abs_error, float(np.max(np.abs(actual - _numpy(expected)))))
return prefix_lengths, max_abs_error
def run_example(output_dir: Path) -> ExampleResult:
"""Export and execute all deployment graphs, returning paths and parity measurements."""
torch.manual_seed(0)
model = EncoderDecoderModel().eval()
source_input = _make_input(EXAMPLE_BATCH, MAX_SOURCE_TOKENS, seed=1)
target_input = _make_input(EXAMPLE_BATCH, MAX_TARGET_TOKENS, seed=2)
with torch.no_grad():
_ = model(source_input, target_input)
encoder_path, cache_builder_path, cached_decoder_path = _export_models(
model,
source_input,
target_input,
output_dir,
)
prefix_lengths, max_abs_error = _run_onnxruntime(
model,
encoder_path,
cache_builder_path,
cached_decoder_path,
)
return ExampleResult(
encoder_path=encoder_path,
cache_builder_path=cache_builder_path,
cached_decoder_path=cached_decoder_path,
prefix_lengths=prefix_lengths,
max_abs_error=max_abs_error,
)
def main() -> None:
"""Run the demonstration in a temporary directory and print the verified result."""
with TemporaryDirectory(prefix="stackformers-onnx-") as temp_dir:
result = run_example(Path(temp_dir))
print(f"encoder: {result.encoder_path}")
print(f"cache builder: {result.cache_builder_path}")
print(f"cached decoder: {result.cached_decoder_path}")
print(f"decoded prefixes: {result.prefix_lengths}")
print(f"maximum absolute error: {result.max_abs_error:.3e}")
if __name__ == "__main__":
main()