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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
//! One unit of work against the device.
//!
//! Every operation a backend server exposes that touches memory or launches a
//! kernel goes through a [`Command`]: it pairs the context holding the
//! compiled kernels with the streams the operation was resolved against, and
//! resolving is what orders the current stream behind whichever streams own
//! the buffers it was handed.
//!
//! Everything here is the same whichever driver is underneath — the allocation
//! and reclaim policy, when the drop queue may be flushed, what a copy stages.
//! The calls that are not are [`Driver`](super::Driver)'s.
use super::stream_copies::StreamCopies;
use super::{CopyLayout, DeviceResource, DeviceStream, Driver, Staging};
use crate::id::KernelId;
use crate::memory_management::drop_queue::Fence;
use crate::memory_management::relocation::{RelocatingStreams, RelocationNeed, RelocationReason};
use crate::memory_management::{Cleanup, release_or_leak};
use crate::memory_management::{
ManagedMemoryHandle, MemoryAllocationMode, MemoryHandle, PageGuard, PageUpdate,
StreamMemoryReport,
};
use crate::server::{BufferBinding, CopyDescriptor, Handle, IoError, LaunchError, ServerError};
use crate::storage::ManagedResource;
use crate::stream::ResolvedStreams;
use crate::stream::TouchedPage;
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
use cubecl_common::{bytes::Bytes, device::ServiceId};
use cubecl_environment::backtrace::BackTrace;
use cubecl_environment::future::DynFut;
use cubecl_environment::stream::StreamId;
/// One unit of work against the device: the context that holds its compiled
/// kernels, and the streams it was resolved against.
///
/// Built per operation rather than held, because resolving is what orders the
/// current stream behind whichever streams own the buffers it was given.
pub struct Command<'a, D: Driver> {
ctx: &'a mut D::Context,
streams: ResolvedStreams<'a, D::Backend>,
/// The service issuing the command: what the handles it allocates are
/// stamped with.
service: ServiceId,
}
impl<'a, D: Driver> Command<'a, D> {
/// A command against `ctx` over the streams `streams` resolved.
pub fn new(
ctx: &'a mut D::Context,
streams: ResolvedStreams<'a, D::Backend>,
service: ServiceId,
) -> Self {
Self {
ctx,
streams,
service,
}
}
/// The stream this command is issued on.
///
/// The one part of the resolution a backend reaches for directly: the
/// driver calls take a stream, and this is the one they take.
pub fn stream(&mut self) -> &mut D::Stream {
self.streams.current()
}
/// The device allocation `binding` names, resolved on the stream that
/// created it rather than the current one.
///
/// While the current stream records a graph, the page the allocation
/// sits on is remembered: the graph replays against the address resolved
/// here, so it guards that page once it seals.
///
/// # Errors
///
/// [`IoError::StorageHandleNotFound`] when the binding names no live allocation.
pub fn resource(&mut self, binding: BufferBinding) -> Result<DeviceResource<D>, IoError> {
self.streams
.current()
.capturing()
.touch(binding.stream, binding.memory.descriptor().location());
self.streams
.get(&binding.stream)
.device_memory()
.get_resource(binding.memory, binding.offset_start, binding.offset_end)
}
/// The device allocation `binding` names, for a caller outside the server
/// that keeps its raw address: it holds the allocation and a guard on its
/// page for as long as it lives.
///
/// # Errors
///
/// [`IoError::StorageHandleNotFound`] when the binding names no live allocation.
pub fn managed_resource(
&mut self,
binding: BufferBinding,
) -> Result<ManagedResource<DeviceResource<D>>, IoError> {
self.streams
.get(&binding.stream)
.device_memory()
.managed_resource(binding.memory, binding.offset_start, binding.offset_end)
}
/// A guard on every page in `touched`, each looked up in the memory of the
/// stream that owns it.
pub fn guard_pages(&mut self, touched: &[TouchedPage]) -> Vec<PageGuard> {
touched
.iter()
.filter_map(|page| {
self.streams
.get(&page.stream)
.device_memory()
.guard(page.location)
})
.collect()
}
/// Everything the current stream's device memory holds, pool by pool.
pub fn memory_report(&mut self) -> StreamMemoryReport {
StreamMemoryReport {
stream: self.streams.current,
pools: self.streams.current().device_memory().memory_report(),
auxiliary: Vec::new(),
}
}
/// Release everything the current stream is holding that nothing still
/// needs.
///
/// # Errors
///
/// Refused while any stream records a graph: releasing memory waits on
/// the device, and a wait on a stream that records aborts its capture.
pub fn memory_cleanup(&mut self) -> Result<(), ServerError> {
self.refuse_while_recording()?;
let stream = self.streams.current();
// Deferred frees sit in the drop queue until a fenced flush, so an
// explicit cleanup must drain it first or the pools still see those
// slices as live.
let signal = stream.signal();
stream.drop_queue().drain(|| D::Stream::fence(signal));
// The info cache's buffers are live slices in the dynamic pools; an
// explicit cleanup exists to leave those pools empty, so every entry
// not pinned by a live graph goes too.
stream.info_cache().clear_unpinned();
RelocatingStreams::reclaim(self)?;
let (stream, failures) = self.streams.current_and_failures();
stream.host_memory().cleanup(Cleanup::Explicit, failures);
Ok(())
}
/// Flush the current stream's drop queue, freeing what the device is
/// known to be done with.
///
/// Deferred while the stream records a graph — the flush records a fence
/// on the capturing stream, which corrupts the recording — and the window
/// drains the queue itself when it closes. The rule lives here, on the one
/// path a server has to the queue, so no call site can rebuild the flush
/// without the guard.
pub fn flush_drops(&mut self) {
let stream = self.streams.current();
if stream.capturing().is_recording() {
return;
}
let signal = stream.signal();
stream.drop_queue().flush(|| D::Stream::fence(signal));
}
/// Set the [`MemoryAllocationMode`] for the current stream.
pub fn allocation_mode(&mut self, mode: MemoryAllocationMode) {
self.streams.current().device_memory().mode(mode)
}
/// Allocate `size` bytes of device memory on the current stream.
///
/// # Errors
///
/// [`IoError::BufferTooBig`] when no device could ever fit it, and
/// whatever the allocator reports when a reclaim-and-retry still cannot.
pub fn reserve(&mut self, size: u64) -> Result<ManagedMemoryHandle, IoError> {
let update = self.streams.current().capturing().page_update();
if update == PageUpdate::Allow {
self.relocate_when_wanted();
}
let (stream, failures) = self.streams.current_and_failures();
match stream.device_memory().reserve(size, update, failures) {
Ok(handle) => Ok(handle),
// The recording now misses whatever this memory was for.
Err(err @ IoError::PageUpdateForbidden { .. }) => {
stream.capturing().fail(err.clone().into());
Err(err)
}
Err(err) if update != PageUpdate::Allow || !err.may_succeed_after_reclaim() => Err(err),
// Reclaim and retry once; only a failure after that is reported.
// Without the retry a transient peak becomes a never-initialized
// handle whose every downstream use fails.
Err(err) => {
log::warn!("device allocation of {size} B failed ({err}); reclaiming and retrying");
self.make_room(&err);
let (stream, failures) = self.streams.current_and_failures();
stream
.device_memory()
.reserve(size, PageUpdate::Allow, failures)
}
}
}
/// Get back what the reservation that failed with `err` needs: a slot for
/// a new page size when the arena is full, memory otherwise.
fn make_room(&mut self, err: &IoError) {
match err {
// Only a relocation that may allocate empties the outdated pools
// whatever room the current pages have.
IoError::PageSizesExhausted { .. } => {
RelocatingStreams::relocate(self, RelocationReason::ArenaFull)
}
// No stream records (the caller checked), which is the only
// refusal a cleanup has.
_ => {
let _ = self.memory_cleanup();
}
}
}
/// Give `memory` `size` bytes of device memory on the current stream.
///
/// # Errors
///
/// Returns an [`IoError`] when the memory cannot be reserved or bound, e.g. when the device
/// is out of memory. The caller records it on `memory`, which has no binding to carry it.
pub fn initialize_memory(
&mut self,
memory: ManagedMemoryHandle,
size: u64,
) -> Result<(), IoError> {
let reserved = match self.reserve(size) {
Ok(reserved) => reserved,
// The recording already failed on it, and `stop_capture` reports
// that: the handle stays unbound, and whatever uses it belongs to
// a recording that will not seal.
Err(IoError::PageUpdateForbidden { .. }) => return Ok(()),
Err(err) => return Err(err),
};
self.bind(reserved, memory)
}
/// The current stream's cursor.
pub fn cursor(&self) -> u64 {
self.streams.cursor
}
/// Allocate `size` bytes of device memory and a handle naming it.
///
/// # Errors
///
/// Whatever the allocation or the bind reports.
pub fn empty(&mut self, size: u64) -> Result<Handle, IoError> {
let handle = Handle::new(self.service, self.streams.current, size);
let reserved = self.reserve(size)?;
self.bind(reserved, handle.memory.clone())?;
Ok(handle)
}
/// Give `reserved`'s storage to `new`, so handles issued against `new`
/// resolve to it.
///
/// # Errors
///
/// [`IoError`] when the reservation has no initialized storage to give.
pub fn bind(
&mut self,
reserved: ManagedMemoryHandle,
new: ManagedMemoryHandle,
) -> Result<(), IoError> {
let cursor = self.cursor();
let (stream, failures) = self.streams.current_and_failures();
stream.device_memory().bind(reserved, new, cursor, failures)
}
/// `size` bytes of host memory, pinned when the pool can serve it.
///
/// Pinned pages transfer by DMA without a bounce, but they are scarce, so
/// an exhausted pool falls back to the heap rather than failing: this
/// always answers with a buffer of the size asked for.
pub fn reserve_cpu(&mut self, size: usize, origin: Option<StreamId>) -> Bytes {
self.reserve_pinned(size, origin)
.unwrap_or_else(|| Bytes::from_bytes_vec(vec![0; size]))
}
/// `size` bytes of pinned host memory, or `None` when the pool cannot
/// serve it.
fn reserve_pinned(&mut self, size: usize, origin: Option<StreamId>) -> Option<Bytes> {
let (stream, failures) = match origin {
Some(id) => self.streams.get_and_failures(&id),
None => self.streams.current_and_failures(),
};
// A stream recording a graph stages from what the pool holds, and
// falls back to the heap rather than allocate.
let update = stream.capturing().page_update();
let handle = stream
.host_memory()
.reserve(size as u64, update, failures)
.ok()?;
let binding = MemoryHandle::binding(handle);
stream.capturing().prime(&binding);
let resource = stream
.host_memory()
.get_resource(binding.clone(), None, None)
.ok()?;
// SAFETY: the binding has initialized memory for at least `size` bytes,
// and `resource` is what the manager just resolved it to.
Some(unsafe { D::pinned_bytes(binding, resource, size) })
}
/// Copy each descriptor's device memory back to the host, resolving once
/// the copies have landed.
///
/// The copies are enqueued before the future is returned; awaiting it
/// waits on the fence that follows them.
///
/// # Errors
///
/// [`IoError::UnsupportedStrides`] for a layout the driver cannot copy,
/// and whatever the fence reports when the stream itself failed.
pub fn read_async(
&mut self,
descriptors: Vec<CopyDescriptor>,
) -> impl Future<Output = Result<Vec<Bytes>, ServerError>> + Send + use<D> {
let held = descriptors
.iter()
.map(|descriptor| descriptor.handle.clone())
.collect::<Vec<_>>();
let result = self.copies_to_bytes(descriptors);
let fence = D::Stream::fence(self.streams.current().signal());
async move {
if let Err(err) = fence.wait() {
// Both the device sources and host destinations may still be in use.
release_or_leak((held, result), &err);
return Err(err);
}
// The bindings kept the source allocations alive across the copies;
// the fence above is what says they are done being read.
core::mem::drop(held);
result.map_err(Into::into)
}
}
/// Copy each descriptor's device memory into a fresh host buffer.
fn copies_to_bytes(&mut self, descriptors: Vec<CopyDescriptor>) -> Result<Vec<Bytes>, IoError> {
let mut result = Vec::with_capacity(descriptors.len());
for descriptor in descriptors {
match self.copy_to_bytes(descriptor, None) {
Ok(bytes) => result.push(bytes),
Err(err) => {
// The buffers collected so far are the destinations of
// copies already enqueued: dropping them hands their
// pinned slices back to a pool whose reuse is gated on
// the refcount alone, while the device is still writing
// them. The fence `read_async` records to cover exactly
// this does not exist yet on the error path, so record
// one here and wait it out before the partial set drops.
if !result.is_empty()
&& !D::Stream::fence(self.streams.current().signal()).sync()
{
core::mem::forget(result);
}
return Err(err);
}
}
}
Ok(result)
}
/// Copy one descriptor's device memory into a fresh host buffer.
fn copy_to_bytes(
&mut self,
descriptor: CopyDescriptor,
stream_id: Option<StreamId>,
) -> Result<Bytes, IoError> {
let num_bytes = descriptor.shape.iter().product::<usize>() * descriptor.elem_size;
let mut bytes = self.reserve_cpu(num_bytes, stream_id);
self.write_to_cpu(descriptor, &mut bytes, stream_id)?;
Ok(bytes)
}
/// Enqueue a copy of `descriptor`'s device memory into `bytes`.
///
/// # Errors
///
/// [`IoError::UnsupportedStrides`] for a layout that is not pitched
/// row-major, [`IoError::StorageHandleNotFound`] for a binding that names no live
/// allocation, and the driver's refusal to copy.
pub fn write_to_cpu(
&mut self,
descriptor: CopyDescriptor,
bytes: &mut Bytes,
stream_id: Option<StreamId>,
) -> Result<(), IoError> {
let CopyDescriptor {
handle: binding,
shape,
strides,
elem_size,
} = descriptor;
// Nothing to copy for an empty tensor, and `bytes` has no real backing
// for the driver to write into — a dangling zero-size buffer.
// Its strides may contain zeros, so skip copy-layout validation too.
if bytes.is_empty() {
return Ok(());
}
let layout = CopyLayout::of(&shape, &strides, elem_size)?;
let resource = self.resource(binding)?;
let stream = match stream_id {
Some(id) => self.streams.get(&id),
None => self.streams.current(),
};
// SAFETY: `resource` is a live device allocation the manager just
// resolved, `bytes` was sized for this copy, and the caller awaits the
// fence `read_async` records before reading it back.
unsafe { D::copy_to_host(&resource, &layout, bytes, stream) }
}
/// Enqueue a copy of `data` into the device memory `descriptor` names.
///
/// # Errors
///
/// [`IoError::UnsupportedStrides`] for a layout that is not pitched
/// row-major, [`IoError::StorageHandleNotFound`] for a binding that names no live
/// allocation, and the driver's refusal to copy.
pub fn write_to_gpu(&mut self, descriptor: CopyDescriptor, data: Bytes) -> Result<(), IoError> {
let CopyDescriptor {
handle: binding,
shape,
strides,
elem_size,
} = descriptor;
let size = data.len();
// An empty tensor (a zero dim in its shape) has nothing to copy. Bail
// before validating its potentially zero strides or staging: the zero-size
// staging buffer has no real backing (a dangling pointer), and a 2D copy
// would still transfer `width_bytes` from it when only the leading dims are zero.
if size == 0 {
return Ok(());
}
let layout = CopyLayout::of(&shape, &strides, elem_size)?;
let resource = self.resource(binding)?;
let staging = Staging::of(size, data.property());
let data = match staging.through_pinned {
true => {
// Pinned staging is a DMA optimization, not a requirement, so
// an exhausted pinned pool falls back to a plain heap buffer
// rather than failing the write — the same answer `reserve_cpu`
// gives for the same condition. File-backed data still lands in
// real memory before the driver reads it asynchronously, which
// is the half of the staging that is mandatory.
let mut buffer = self
.reserve_pinned(size, None)
.unwrap_or_else(|| Bytes::from_bytes_vec(vec![0; size]));
data.copy_into(&mut buffer);
buffer
}
false => data,
};
let current = self.streams.current();
// SAFETY: `resource` is a live device allocation, `data` is a valid
// host buffer, and either the drop queue or the capture window below
// keeps it alive for as long as the device reads it.
unsafe { D::copy_to_device(&resource, &layout, &data, current)? };
if current.capturing().is_recording() {
// A copy recorded into a graph is not executed now but re-read on
// every replay: the node keeps the raw host pointer, so the bytes
// ride the window onto the graph rather than the drop queue —
// which frees them when the window closes, exactly when the graph
// starts needing them.
current.capturing().retain_host(data);
} else {
current.drop_queue().push(data);
if staging.flush_after || current.drop_queue().should_flush() {
let signal = current.signal();
current.drop_queue().flush(|| D::Stream::fence(signal));
}
}
Ok(())
}
/// Allocate device memory for `data` and enqueue the copy into it.
///
/// # Errors
///
/// Whatever the allocation or the copy reports.
pub fn create_with_data(&mut self, data: &[u8]) -> Result<Handle, IoError> {
let mut staging =
self.reserve_pinned(data.len(), None)
.ok_or_else(|| IoError::Unknown {
backtrace: BackTrace::capture(),
description: "Unable to reserve pinned memory".into(),
})?;
staging.copy_from_slice(data);
let handle = self.empty(staging.len() as u64)?;
self.write_to_gpu(
CopyDescriptor {
handle: handle.clone().binding(),
shape: [data.len()].into(),
strides: [1].into(),
elem_size: 1,
},
staging,
)?;
Ok(handle)
}
/// Wait for everything already enqueued on the current stream to finish.
///
/// # Errors
///
/// The fault the barrier reveals, when the stream itself failed.
pub fn sync(&mut self) -> DynFut<Result<(), ServerError>> {
let fence = D::Stream::fence(self.streams.current().signal());
Box::pin(async move { fence.wait() })
}
/// Enqueue an already-compiled kernel on the current stream.
///
/// # Errors
///
/// The driver's refusal to enqueue the launch, returned whether or not a
/// profile is open. An open profile is not a reason to hold the failure
/// here: the caller's write scope is what claims the buffers the launch
/// never wrote, and the caller invalidates every open profile on the same
/// path, so keeping it would lose the claim and duplicate the report.
pub fn kernel(
&mut self,
id: &KernelId,
kernel: &D::Loaded,
count: (u32, u32, u32),
args: &mut D::LaunchArgs,
) -> Result<(), LaunchError> {
let stream = self.streams.current();
let result = D::launch(self.ctx, stream, id, kernel, count, args);
// A fenced flush during capture would abort it; defer until the capture
// ends, when the deferred staging buffers are reclaimed.
if !stream.capturing().is_recording() && stream.drop_queue().should_flush() {
let signal = stream.signal();
stream.drop_queue().flush(|| D::Stream::fence(signal));
}
result
}
}
impl<D: Driver> RelocatingStreams for Command<'_, D> {
fn recording(&mut self) -> bool {
self.streams.current().capturing().device().any_recording()
}
fn has_outdated(&mut self) -> bool {
self.streams.current().device_memory().has_outdated()
}
fn relocation_need(&mut self) -> RelocationNeed {
self.streams.current().device_memory().relocation_need()
}
fn bytes_allocated(&mut self) -> u64 {
self.streams
.all()
.map(|stream| stream.device_memory().bytes_allocated())
.sum()
}
/// Nothing here: `StreamCopies` waits on every stream before its first
/// copy, so a relocation with nothing to move waits on none.
fn finish(&mut self) {}
fn cleanup_memory(&mut self) {
let (stream, failures) = self.streams.current_and_failures();
stream.device_memory().cleanup(Cleanup::Explicit, failures);
}
fn relocate_memory(&mut self, reason: RelocationReason) {
let current = self.streams.current;
self.relocate_stream(current, reason);
}
fn device_has_outdated(&mut self) -> bool {
self.streams
.all()
.any(|stream| stream.device_memory().has_outdated())
}
fn relocate_device_memory(&mut self, reason: RelocationReason) {
for stream_id in self.streams.stream_ids() {
if self.streams.get(&stream_id).device_memory().has_outdated() {
self.relocate_stream(stream_id, reason);
}
}
}
}
impl<D: Driver> Command<'_, D> {
/// Move what `stream_id`'s memory holds on outdated pages, with the copies
/// queued on that stream.
fn relocate_stream(&mut self, stream_id: StreamId, reason: RelocationReason) {
// Rare enough to gather the signals as it goes: it waits on the whole
// device anyway.
let signals: Vec<_> = self.streams.all().map(|stream| stream.signal()).collect();
let queue = self.streams.get(&stream_id).signal();
let mut copier = StreamCopies::<D>::new(signals, queue, self.ctx);
let (stream, failures) = self.streams.get_and_failures(&stream_id);
stream
.device_memory()
.relocate(&mut copier, reason, failures);
}
}