File size: 11,690 Bytes
751ad26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from typing import Literal, Sequence

import numpy as np

from .attention_reference import softmax
from .backends import (
    PreparedPageTorch,
    cuda_available,
    decode_multi_query_step_cuda,
    decode_step_cuda,
    decode_multi_query_step_mps,
    decode_step_mps,
    mix_page_cpu_ref,
    mix_page_cuda,
    mix_page_mps,
    mps_available,
    page_supported_cuda,
    page_supported_mps,
    prepare_page_cuda,
    prepare_page_mps,
    prepare_pages_cuda,
    prepare_pages_mps,
    score_pages_cuda,
    score_pages_mps,
    score_page_cpu_ref,
    score_page_cuda,
    score_page_mps,
)
from .page_cache import PreparedPageCache
from .tracing import ExecutionTrace
from .types import EncodedPage

BackendName = Literal["cpu_ref", "torch_mps", "torch_cuda", "auto"]
PageLike = EncodedPage | PreparedPageTorch


def _resolve_backend(backend: BackendName, page: PageLike) -> Literal["cpu_ref", "torch_mps", "torch_cuda"]:
    if backend == "cpu_ref":
        return "cpu_ref"
    if backend == "torch_mps":
        if not mps_available():
            raise RuntimeError("torch_mps is unavailable on this machine")
        if not page_supported_mps(page):
            raise ValueError("page is unsupported by torch_mps in this phase")
        return "torch_mps"
    if backend == "torch_cuda":
        if not cuda_available():
            raise RuntimeError("torch_cuda is unavailable on this machine")
        if not page_supported_cuda(page):
            raise ValueError("page is unsupported by torch_cuda in this phase")
        return "torch_cuda"
    if isinstance(page, PreparedPageTorch):
        return "torch_cuda" if page.device_type == "cuda" else "torch_mps"
    if cuda_available() and page_supported_cuda(page):
        return "torch_cuda"
    if mps_available() and page_supported_mps(page):
        return "torch_mps"
    return "cpu_ref"


def _prepared_pages_backend(pages: Sequence[PageLike]) -> Literal["torch_mps", "torch_cuda"] | None:
    if not pages or not all(isinstance(page, PreparedPageTorch) for page in pages):
        return None
    device_type = pages[0].device_type
    if any(page.device_type != device_type for page in pages):
        raise ValueError("prepared torch pages must all target the same device")
    return "torch_cuda" if device_type == "cuda" else "torch_mps"


def prepare_page(
    page: PageLike,
    *,
    backend: BackendName = "auto",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> PageLike:
    resolved_backend = _resolve_backend(backend, page)
    if resolved_backend == "torch_mps":
        if cache is not None:
            return cache.prepare_page(page, backend="torch_mps", trace=trace)
        return prepare_page_mps(page, trace=trace)
    if resolved_backend == "torch_cuda":
        if cache is not None:
            return cache.prepare_page(page, backend="torch_cuda", trace=trace)
        return prepare_page_cuda(page, trace=trace)
    return page.source_page if isinstance(page, PreparedPageTorch) else page


def prepare_pages(
    pages: Sequence[PageLike],
    *,
    backend: BackendName = "auto",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> list[PageLike]:
    if pages:
        resolved_backend = _resolve_backend(backend, pages[0])
        if resolved_backend == "torch_mps":
            if cache is not None:
                return cache.prepare_pages(list(pages), backend="torch_mps", trace=trace)
            return prepare_pages_mps(pages, trace=trace)
        if resolved_backend == "torch_cuda":
            if cache is not None:
                return cache.prepare_pages(list(pages), backend="torch_cuda", trace=trace)
            return prepare_pages_cuda(pages, trace=trace)
    return [prepare_page(page, backend=backend, cache=cache, trace=trace) for page in pages]


def score_page(
    query_slice: np.ndarray,
    page: PageLike,
    *,
    backend: BackendName = "auto",
    trace: ExecutionTrace | None = None,
) -> np.ndarray:
    resolved_backend = _resolve_backend(backend, page)
    if resolved_backend == "torch_mps":
        return score_page_mps(query_slice, page, trace=trace)
    if resolved_backend == "torch_cuda":
        return score_page_cuda(query_slice, page, trace=trace)
    return score_page_cpu_ref(query_slice, page, trace=trace)


def score_pages(
    query_slice: np.ndarray,
    pages: Sequence[PageLike],
    *,
    backend: BackendName = "auto",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> list[np.ndarray]:
    if not pages:
        return []

    prepared_pages = prepare_pages(pages, backend=backend, cache=cache, trace=trace)
    prepared_backend = _prepared_pages_backend(prepared_pages)
    if prepared_backend == "torch_mps":
        return score_pages_mps(query_slice, prepared_pages, trace=trace)
    if prepared_backend == "torch_cuda":
        return score_pages_cuda(query_slice, prepared_pages, trace=trace)
    return [score_page(query_slice, page, backend=backend, trace=trace) for page in prepared_pages]


def mix_page(
    attn_weights: np.ndarray,
    page: PageLike,
    *,
    out_acc: np.ndarray | None = None,
    backend: BackendName = "auto",
    trace: ExecutionTrace | None = None,
) -> np.ndarray:
    resolved_backend = _resolve_backend(backend, page)
    if resolved_backend == "torch_mps":
        return mix_page_mps(attn_weights, page, out_acc=out_acc, trace=trace)
    if resolved_backend == "torch_cuda":
        return mix_page_cuda(attn_weights, page, out_acc=out_acc, trace=trace)
    return mix_page_cpu_ref(attn_weights, page, out_acc=out_acc, trace=trace)


def attention_step(
    query_slice: np.ndarray,
    key_page: PageLike,
    value_page: PageLike,
    *,
    backend: BackendName = "cpu_ref",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
    prepared_key_page = prepare_page(key_page, backend=backend, cache=cache, trace=trace)
    prepared_value_page = prepare_page(value_page, backend=backend, cache=cache, trace=trace)
    logits = score_page(query_slice, prepared_key_page, backend=backend, trace=trace)
    weights = softmax(logits)
    output = mix_page(weights, prepared_value_page, backend=backend, trace=trace)
    return logits, weights, output


def decode_step(
    query_slice: np.ndarray,
    key_pages: Sequence[PageLike],
    value_pages: Sequence[PageLike],
    *,
    backend: BackendName = "auto",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
    if len(key_pages) != len(value_pages):
        raise ValueError("key_pages and value_pages must contain the same number of pages")
    if not key_pages:
        raise ValueError("decode_step requires at least one page")

    return decode_step_with_page_logits(
        query_slice,
        key_pages,
        value_pages,
        backend=backend,
        cache=cache,
        trace=trace,
    )


def decode_multi_query_step(
    query_slices: np.ndarray,
    key_pages: Sequence[PageLike],
    value_pages: Sequence[PageLike],
    *,
    backend: BackendName = "auto",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
    queries = np.asarray(query_slices, dtype=np.float32)
    if queries.ndim != 2:
        raise ValueError("query_slices must have shape [query_count, head_dim]")
    if len(key_pages) != len(value_pages):
        raise ValueError("key_pages and value_pages must contain the same number of pages")
    if not key_pages:
        raise ValueError("decode_multi_query_step requires at least one page")

    prepared_key_pages = prepare_pages(key_pages, backend=backend, cache=cache, trace=trace)
    prepared_value_pages = prepare_pages(value_pages, backend=backend, cache=cache, trace=trace)

    prepared_backend = _prepared_pages_backend(prepared_key_pages)
    if prepared_backend is not None and prepared_backend == _prepared_pages_backend(prepared_value_pages):
        if prepared_backend == "torch_cuda":
            return decode_multi_query_step_cuda(
                queries,
                prepared_key_pages,
                prepared_value_pages,
                trace=trace,
            )
        return decode_multi_query_step_mps(
            queries,
            prepared_key_pages,
            prepared_value_pages,
            trace=trace,
        )

    logits_list = []
    weights_list = []
    output_list = []
    for query_slice in queries:
        logits, weights, output = decode_step(
            query_slice,
            prepared_key_pages,
            prepared_value_pages,
            backend=backend,
            trace=trace,
        )
        logits_list.append(logits)
        weights_list.append(weights)
        output_list.append(output)
    return (
        np.stack(logits_list, axis=0).astype(np.float32, copy=False),
        np.stack(weights_list, axis=0).astype(np.float32, copy=False),
        np.stack(output_list, axis=0).astype(np.float32, copy=False),
    )


def decode_step_with_page_logits(
    query_slice: np.ndarray,
    key_pages: Sequence[PageLike],
    value_pages: Sequence[PageLike],
    *,
    page_logits: Sequence[np.ndarray | None] | None = None,
    backend: BackendName = "auto",
    cache: PreparedPageCache | None = None,
    trace: ExecutionTrace | None = None,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
    if len(key_pages) != len(value_pages):
        raise ValueError("key_pages and value_pages must contain the same number of pages")
    if not key_pages:
        raise ValueError("decode_step requires at least one page")
    if page_logits is not None and len(page_logits) != len(key_pages):
        raise ValueError("page_logits must align with key_pages")

    prepared_key_pages = prepare_pages(key_pages, backend=backend, cache=cache, trace=trace)
    prepared_value_pages = prepare_pages(value_pages, backend=backend, cache=cache, trace=trace)

    prepared_backend = _prepared_pages_backend(prepared_key_pages)
    if prepared_backend is not None and prepared_backend == _prepared_pages_backend(prepared_value_pages):
        if prepared_backend == "torch_cuda":
            return decode_step_cuda(
                query_slice,
                prepared_key_pages,
                prepared_value_pages,
                precomputed_page_logits=page_logits,
                trace=trace,
            )
        return decode_step_mps(
            query_slice,
            prepared_key_pages,
            prepared_value_pages,
            precomputed_page_logits=page_logits,
            trace=trace,
        )

    resolved_page_logits = []
    for index, page in enumerate(prepared_key_pages):
        cached_logits = None if page_logits is None else page_logits[index]
        if cached_logits is None:
            cached_logits = score_page(query_slice, page, backend=backend, trace=trace)
        resolved_page_logits.append(np.asarray(cached_logits, dtype=np.float32))
    logits = np.concatenate(resolved_page_logits).astype(np.float32, copy=False)
    weights = softmax(logits)

    output = np.zeros(prepared_key_pages[0].header.head_dim, dtype=np.float32)
    offset = 0
    for value_page in prepared_value_pages:
        token_count = value_page.header.token_count
        page_weights = weights[offset : offset + token_count]
        output = mix_page(page_weights, value_page, out_acc=output, backend=backend, trace=trace)
        offset += token_count

    return logits, weights, output