Repository navigation
Expand file tree
/
Copy pathscratch.py
More file actions
416 lines (358 loc) · 18.7 KB
/
Copy pathscratch.py
File metadata and controls
416 lines (358 loc) · 18.7 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-FileCopyrightText: Copyright (c) 2026 Flare community contributors
# SPDX-License-Identifier: Apache-2.0
"""Preparation workspace and typed invocation-local scratch views."""
from __future__ import annotations
from contextlib import nullcontext
from math import prod
from weakref import finalize
import torch
def buffer_rows(values, name, rows):
"""Reuse a named buffer's row prefix while its tensor identity is unchanged."""
value = values[name]
views = values.setdefault("row_views", {})
cached = views.get(name)
if cached is None or cached[0] is not value:
cached = (value, {})
views[name] = cached
if rows not in cached[1]:
cached[1][rows] = value[:rows]
return cached[1][rows]
def scratch_views(backing, shapes):
"""Partition scratch into disjoint tensors with independent storage descriptors.
Compilers can track each partition independently. Operations using the
unpartitioned backing must execute separately from operations using these views.
"""
if backing.ndim != 1 or not backing.is_contiguous() or backing.storage_offset():
raise ValueError("scratch partitions require a contiguous one-dimensional backing")
offset, views = 0, []
for shape in shapes:
shape = tuple(shape)
if not shape or any(type(n) is not int or n <= 0 for n in shape):
raise ValueError("scratch partition dimensions must be positive integers")
offset = (offset + 255) // 256 * 256
end = offset + prod(shape) * backing.element_size()
if end > backing.nbytes:
raise ValueError("scratch partitions exceed their backing")
storage = backing.untyped_storage()[offset:end]
views.append(torch.empty(0, dtype=backing.dtype, device=backing.device).set_(storage, 0, shape))
offset = end
return tuple(views)
class ScratchArena:
"""Share tuning workspace and retire declaration storage before compilation.
With ``overlap`` false, preparation creates no auxiliary stream and accepts
no concurrent plans. Serving forwards allocate their own temporary tensors.
"""
def __init__(self, device, *, overlap=True, allocation_pool=None):
if type(overlap) is not bool:
raise TypeError("overlap must be a boolean")
self.tensor = torch.empty(0, dtype=torch.uint8, device=device)
self.allocation_pool = allocation_pool
self.overlap = overlap
self.frozen = False
self._released = False
self._finalizers = []
self._preparation_releases = []
self._parallel_plans = []
self._auxiliary_stream = None
self._recorded_views = None
self._binding_views = None
@property
def device(self):
return self.tensor.device
@property
def nbytes(self):
return self.tensor.nbytes
@property
def auxiliary_stream(self):
self._require_active()
if not self.overlap:
raise RuntimeError("stream overlap is disabled; compose this branch on the main stream")
if self._auxiliary_stream is None:
from cuda.bindings import driver as cuda
# PyTorch's round-robin stream pool can alias a later capture stream.
# Highest stream priority lets small collective kernels preempt main-stream GEMMs.
with torch.cuda.device(self.device):
pstatus, _least, greatest = cuda.cuCtxGetStreamPriorityRange()
if pstatus == cuda.CUresult.CUDA_SUCCESS and greatest < 0:
status, handle = cuda.cuStreamCreateWithPriority(
cuda.CUstream_flags.CU_STREAM_NON_BLOCKING, greatest
)
else:
status, handle = cuda.cuStreamCreate(cuda.CUstream_flags.CU_STREAM_NON_BLOCKING)
if status != cuda.CUresult.CUDA_SUCCESS:
raise RuntimeError(f"cannot create overlap stream: {status}")
stream = torch.cuda.ExternalStream(int(handle), device=self.device)
finalize(stream, cuda.cuStreamDestroy, handle).atexit = False
self._auxiliary_stream = stream
return self._auxiliary_stream
def register_parallel(self, *plans):
"""Declare plans that need disjoint workspace during preparation."""
if not self.overlap:
raise RuntimeError("stream overlap is disabled; serial plans share scratch")
if self.frozen:
raise RuntimeError("scratch declarations are frozen")
if len(plans) < 2:
raise ValueError("parallel scratch requires at least two plans")
self._parallel_plans.append(plans)
def parallel_nbytes(self, requirement):
sizes = {}
def aligned(plan):
key = id(plan)
if key not in sizes:
sizes[key] = (requirement(plan) + 255) // 256 * 256
return sizes[key]
return max((sum(aligned(plan) for plan in group)
for group in self._parallel_plans), default=0)
def reserve(self, nbytes):
self._require_active()
if type(nbytes) is not int or nbytes < 0:
raise ValueError("scratch size must be a nonnegative integer")
if nbytes > self.nbytes:
if self.frozen:
raise RuntimeError(f"frozen scratch arena has {self.nbytes} bytes, requires {nbytes}")
with (nullcontext() if self.allocation_pool is None else
torch.cuda.use_mem_pool(self.allocation_pool, device=self.device.index)):
self.tensor = torch.empty((nbytes + 255) // 256 * 256,
dtype=torch.uint8, device=self.device)
return self.tensor
def freeze(self):
self.frozen = True
def release_preparation(self):
"""Retire finalized workspace before capture when all consumers use local scratch.
The caller must finish tuning and binding finalizers, synchronize their
work, and ensure no execution depends on this arena's storage. Existing
tensor views retain their own storage ownership; this method does not
inspect consumers or infer whether release is safe.
"""
if self._released:
return
if not self.frozen or self._finalizers:
raise RuntimeError("scratch preparation release requires completed binding finalization")
for callback in self._preparation_releases:
callback()
self._preparation_releases.clear()
self.tensor = torch.empty(0, dtype=torch.uint8, device=self.device)
self._parallel_plans.clear()
self._released = True
def register_preparation_release(self, callback):
"""Release shared declaration owners after every binding has finalized."""
self._require_active()
if not callable(callback):
raise TypeError("preparation release must be callable")
self._preparation_releases.append(callback)
def _require_active(self):
if self._released:
raise RuntimeError("scratch preparation storage has been released")
def trim(self, nbytes):
self._require_active()
if type(nbytes) is not int or nbytes < 0:
raise ValueError("scratch size must be a nonnegative integer")
capacity = (nbytes + 255) // 256 * 256
if capacity > self.nbytes:
raise ValueError("selected scratch exceeds the reserved candidate capacity")
if capacity < self.nbytes:
if self.frozen:
raise RuntimeError("cannot trim a frozen scratch arena")
device = self.device
with (nullcontext() if self.allocation_pool is None else
torch.cuda.use_mem_pool(self.allocation_pool, device=device.index)):
# Tuning has drained and scratch contents are disposable. Release
# our candidate owner before allocating the selected capacity;
# external views, if any, still retain their own valid storage.
self.tensor = torch.empty(0, dtype=torch.uint8, device=device)
self.tensor = torch.empty(capacity, dtype=torch.uint8, device=device)
def views(self, *shapes_and_dtypes: tuple[tuple[int, ...], torch.dtype]) -> tuple[torch.Tensor, ...]:
"""Return disjoint views for simultaneous use within one serialized call.
Calls reuse the same offsets. Preparation must refresh retained views
after growth and freeze the arena before recording CUDA graph pointers.
"""
if self._recorded_views is not None:
self._recorded_views.append(shapes_and_dtypes)
if self._binding_views is not None:
shapes_and_dtypes = next(self._binding_views)
sizes = []
for shape, dtype in shapes_and_dtypes:
if any(type(dim) is not int or dim < 0 for dim in shape):
raise ValueError("scratch dimensions must be nonnegative integers")
if not isinstance(dtype, torch.dtype):
raise TypeError("scratch dtype must be a torch dtype")
sizes.append(prod(shape) * dtype.itemsize)
storage = self.reserve(sum((size + 255) // 256 * 256 for size in sizes))
result, offset = [], 0
for (shape, dtype), size in zip(shapes_and_dtypes, sizes, strict=True):
result.append(storage.narrow(0, offset, size).view(dtype).reshape(shape))
offset += (size + 255) // 256 * 256
return tuple(result)
def register_finalizer(self, callback):
if self.frozen:
raise RuntimeError("scratch binding declarations are frozen")
if not callable(callback):
raise TypeError("scratch binding finalizer must be callable")
self._finalizers.append(callback)
def finalize(self, *, group=None):
"""Bind scratch views and clear registered finalizers."""
self._require_active()
if group is None:
for callback in self._finalizers:
callback()
else:
import torch.distributed as dist
self._recorded_views = []
try:
for callback in self._finalizers:
callback()
layouts = self._recorded_views
finally:
self._recorded_views = None
ranks = [None] * dist.get_world_size(group)
dist.all_gather_object(ranks, (self.nbytes, layouts), group=group)
shared = _shared_views([views for _, views in ranks])
required = max((sum((prod(shape) * dtype.itemsize + 255) // 256 * 256
for shape, dtype in views) for views in shared), default=0)
before = self.tensor
self.reserve(max(required, *(size for size, _ in ranks)))
if shared != layouts or self.tensor is not before:
self._binding_views = iter(shared)
try:
for callback in self._finalizers:
callback()
finally:
self._binding_views = None
self._finalizers.clear()
def _shared_views(layouts):
"""Give equivalent rank forwards identical scratch sizes and offsets."""
result = []
for calls in zip(*layouts, strict=True):
views = []
for specs in zip(*calls, strict=True):
shape, dtype = specs[0]
if any(spec != specs[0] for spec in specs[1:]):
if any(len(other) != 1 or kind != torch.uint8 for other, kind in specs):
raise ValueError("tensor-parallel scratch layouts differ beyond byte-buffer capacity")
shape = (max(other[0] for other, _ in specs),)
views.append((shape, dtype))
result.append(tuple(views))
return result
def scratch_storage(workspace, *, nbytes, device):
if type(nbytes) is not int or nbytes < 0:
raise ValueError("scratch size must be a nonnegative integer")
device = torch.device(device)
if isinstance(workspace, ScratchArena):
if workspace.device != device:
raise ValueError("scratch arena and operator must share a device")
return workspace.reserve(nbytes)
if workspace is None:
return torch.empty(nbytes, dtype=torch.uint8, device=device)
if not isinstance(workspace, torch.Tensor) or workspace.dtype != torch.uint8:
raise TypeError("planned scratch must be a uint8 tensor or ScratchArena")
if workspace.device != device or workspace.ndim != 1 or not workspace.is_contiguous():
raise ValueError("planned scratch requires contiguous one-dimensional storage on the operator device")
if workspace.nbytes < nbytes:
raise ValueError(f"planned scratch has {workspace.nbytes} bytes, requires {nbytes}")
return workspace
def bind_scratch(layout, *, device, workspace=None):
"""Bind contiguous operand prefixes from caller-owned, typed capacity buffers."""
if isinstance(workspace, GraphBuffers):
if workspace.pool.device != torch.device(device):
raise ValueError("graph scratch and operands must share a device")
return {name: workspace.zeros(shape, dtype) for name, (shape, dtype) in layout.items()}
if workspace is None:
return {name: torch.empty(shape, dtype=dtype, device=device)
for name, (shape, dtype) in layout.items()}
result = {}
for name, (shape, dtype) in layout.items():
tensor = workspace[name]
if tensor.dtype != dtype or tensor.device != torch.device(device) or not tensor.is_contiguous():
raise ValueError(f"scratch {name} requires contiguous {dtype} storage on {device}")
if tensor.numel() < prod(shape):
raise ValueError(f"scratch {name} has {tensor.numel()} elements, requires {prod(shape)}")
result[name] = tensor.view(-1)[:prod(shape)].view(shape)
return result
def planned_scratch(specs, workspace=None):
specs = tuple(specs)
if isinstance(workspace, ScratchArena):
if not specs or any(spec.device != workspace.device for spec in specs):
raise ValueError("scratch specs must be nonempty and share the arena device")
views = workspace.views(*((spec.shape, spec.dtype) for spec in specs))
return views[0] if len(views) == 1 else views
if len(specs) != 1 or specs[0].dtype != torch.uint8 or len(specs[0].shape) != 1:
raise ValueError("operator scratch plan must expose one uint8 byte buffer")
spec, = specs
return scratch_storage(workspace, nbytes=spec.nbytes, device=spec.device)
def prime_owners(workspace, *owners):
"""Keep preparation storage without retaining disposable declaration views.
b12x retains priming owners until a plan is released. Functional execution
supplies invocation tensors; standalone calls retain their concrete owners.
"""
return (workspace,) if isinstance(workspace, ScratchArena) else owners
class GraphBufferPool:
"""Chunks shared by private execution buffers with disjoint lifetimes.
A rank replays one captured graph at a time, so a buffer that each launch writes
before reading it need not outlive the launch. Every graph allocates through its
own `cursor()`: cursors fill available space in the same chunks, so different graphs
share memory while one graph's buffers stay disjoint. Graphs constructed largest
first leave later cursors within the chunks the first one created. Each dtype has
its own chunks because compiled graphs cannot reinterpret aliased inputs across
element sizes.
Sequential layers can take separate cursors from a dedicated pool when every
layer consumes and commits its intermediates before the following layer starts.
Values retained across layers or until speculative acceptance need separate storage.
"""
def __init__(self, device, *, chunk_bytes=64 << 20, allocation_pool=None):
if type(chunk_bytes) is not int or chunk_bytes <= 0 or chunk_bytes % 256:
raise ValueError("graph buffer chunks must be a positive multiple of 256 bytes")
self.device, self.chunk_bytes = torch.device(device), chunk_bytes
self.allocation_pool = allocation_pool
self.chunks = {}
@property
def nbytes(self):
return sum(chunk.nbytes for chunks in self.chunks.values() for chunk in chunks)
def cursor(self):
return GraphBuffers(self)
class GraphBuffers:
"""One graph's disjoint views into a `GraphBufferPool`."""
def __init__(self, pool):
self.pool, self.positions = pool, {}
def fork(self):
"""Allocate a sequential stage after the caller's live buffers.
Sibling cursors reuse storage. Each stage must consume its outputs before
another sibling starts; concurrent branches belong to the same cursor.
Join all siblings before allocating values that must coexist with them.
"""
cursor = GraphBuffers(self.pool)
cursor.positions = {dtype: dict(offsets) for dtype, offsets in self.positions.items()}
return cursor
def join(self, *cursors):
"""Reserve the union of stage extents without summing sequential lifetimes."""
if any(cursor.pool is not self.pool for cursor in cursors):
raise ValueError("graph buffer cursors must share a pool")
for cursor in cursors:
for dtype, offsets in cursor.positions.items():
target = self.positions.setdefault(dtype, {})
for index, offset in offsets.items():
target[index] = max(target.get(index, 0), offset)
def zeros(self, shape, dtype):
return self._take(shape, dtype).zero_()
def full(self, shape, value, dtype):
return self._take(shape, dtype).fill_(value)
def _take(self, shape, dtype):
shape = (shape,) if isinstance(shape, int) else tuple(shape)
if any(type(dim) is not int or dim < 0 for dim in shape):
raise ValueError("graph buffer dimensions must be nonnegative integers")
count = prod(shape)
aligned = (count * dtype.itemsize + 255) // 256 * 256
chunks = self.pool.chunks.setdefault(dtype, [])
offsets = self.positions.setdefault(dtype, {})
for index, chunk in enumerate(chunks):
offset = offsets.get(index, 0)
if offset + aligned <= chunk.nbytes:
break
else:
index, offset = len(chunks), 0
with (nullcontext() if self.pool.allocation_pool is None else
torch.cuda.use_mem_pool(self.pool.allocation_pool, device=self.pool.device.index)):
chunks.append(torch.empty(max(aligned, self.pool.chunk_bytes) // dtype.itemsize, dtype=dtype,
device=self.pool.device))
offsets[index] = offset + aligned
return chunks[index].narrow(0, offset // dtype.itemsize, count).view(shape)