cubecl-server 0.11.3

Toolkit for implementing a CubeCL runtime: memory pools, streams, drivers and the compilation pipeline.
Documentation
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
//! The pools a workload's allocations are carved from, one per page size it
//! grew through.

use crate::memory_management::Cleanup;
use crate::{
    config::memory::MemoryLogLevel,
    logging::ServerLogger,
    memory_management::{
        BytesFormat, ErrorGraph, ManagedMemoryHandle, MemoryPoolKind, MemoryPoolReport,
        memory_pool::{MemoryPool, PageMapping, SlicedLayout, SlicedPool, calculate_padding},
        relocation::{
            ArenaShape, ArenaState, Landed, LiveAllocation, Move, OutdatedPages, Relocation,
            StorageCopy, TargetRoom,
        },
    },
    server::IoError,
    storage::ComputeStorage,
};
use alloc::{format, string::String, vec::Vec};
use cubecl_environment::backtrace::BackTrace;
use cubecl_environment::sync::Arc;
use cubecl_ir::MemoryDeviceProperties;

/// How many page sizes the arena holds at once.
///
/// A slot frees once the pool in it drains, and a relocation drains the
/// outdated ones, so a workload reaches this only by growing through that many
/// sizes while each keeps something alive.
pub const ARENA_SLOTS: usize = 64;

/// Slack a page keeps past the largest allocation it was sized for, so that
/// allocation still fits once aligned.
const PAGE_SLACK: u64 = 1024 * 1024;

/// The unit a page size is rounded up to.
const PAGE_GRANULE: u64 = 1024 * 1024;

/// A fixed number of sliced pools, one per page size a workload's allocations
/// grew through.
///
/// The *current* pool carves every new allocation. When one outgrows its
/// pages, a pool of the new size takes over and the old one is *outdated*: it
/// serves no new reservation, gives each page back as it empties, and frees
/// its slot once it holds none.
pub struct PoolArena {
    /// Indexed by slot; a slot is empty once its pool drained.
    slots: Vec<Option<SlicedPool>>,
    /// The pool index a slice's location carries for slot 0.
    first_index: u8,
    /// The slot new allocations are carved from.
    current: usize,
    /// The slots a growth left behind, draining.
    outdated: Vec<usize>,
    sizing: PageSizing,
    logger: Arc<ServerLogger>,
    /// The memory the arena belongs to, as its logs name it.
    name: String,
}

/// How the pages of a [`PoolArena`] are sized.
#[derive(Debug, Clone, Copy)]
pub struct PageSizing {
    /// The smallest page, so a workload of small allocations still carves
    /// pages worth carving.
    min_page_size: u64,
    /// The largest page the device allocates, alignment-rounded down: an
    /// allocation no page of that size fits is not the arena's to serve.
    max_page_size: u64,
    alignment: u64,
}

/// How an allocation fits the pages the arena carves now.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Fit {
    /// A page of the current size holds it.
    Fits,
    /// It needs pages of `page_size`, which outdates the current ones.
    Grow {
        /// The size the new pages are allocated at.
        page_size: u64,
    },
    /// No page the device allocates holds it.
    TooLarge,
}

impl PageSizing {
    /// Pages from `min_page_size` up to the largest the device with
    /// `properties` allocates.
    pub fn new(min_page_size: u64, properties: &MemoryDeviceProperties) -> Self {
        let alignment = properties.alignment;
        let max_page_size = (properties.max_page_size / alignment * alignment).max(alignment);
        Self {
            min_page_size: min_page_size
                .max(alignment)
                .next_multiple_of(alignment)
                .min(max_page_size),
            max_page_size,
            alignment,
        }
    }

    /// How an allocation of `size` bytes fits pages of `page_size`.
    ///
    /// A growth sizes the new pages to the allocation, a megabyte of slack
    /// on top, rounded to the megabyte: never below the floor, never past
    /// the device's largest page.
    pub fn fit(&self, size: u64, page_size: u64) -> Fit {
        let needed = size + calculate_padding(size, self.alignment);
        if needed <= page_size {
            return Fit::Fits;
        }
        if needed > self.max_page_size {
            return Fit::TooLarge;
        }
        Fit::Grow {
            page_size: size
                .saturating_add(PAGE_SLACK)
                .next_multiple_of(PAGE_GRANULE)
                .next_multiple_of(self.alignment)
                .clamp(self.min_page_size, self.max_page_size),
        }
    }

    /// A pool carving pages of `page_size`, whose slices carry `index`.
    fn pool(&self, page_size: u64, index: u8) -> SlicedPool {
        // It takes every size it is handed, so nothing is refused for being
        // larger than a slice of a page it fits.
        SlicedPool::new(SlicedLayout {
            page_size,
            max_slice_size: page_size,
            alignment: self.alignment,
            pool: index,
        })
    }
}

impl PoolArena {
    /// An arena whose first pool carves the smallest pages `sizing` allows,
    /// its slots addressed from `first_index` on.
    pub fn new(
        sizing: PageSizing,
        first_index: u8,
        logger: Arc<ServerLogger>,
        name: String,
    ) -> Self {
        let mut slots = Vec::with_capacity(ARENA_SLOTS);
        slots.push(Some(sizing.pool(sizing.min_page_size, first_index)));
        slots.resize_with(ARENA_SLOTS, || None);
        Self {
            slots,
            first_index,
            current: 0,
            outdated: Vec::with_capacity(ARENA_SLOTS),
            sizing,
            logger,
            name,
        }
    }

    /// The pool `index` names, while one is there.
    pub fn pool(&self, index: u8) -> Option<&SlicedPool> {
        let slot = index.checked_sub(self.first_index)?;
        self.slots.get(slot as usize)?.as_ref()
    }

    /// The pool `index` names, mutably.
    pub fn pool_mut(&mut self, index: u8) -> Option<&mut SlicedPool> {
        let slot = index.checked_sub(self.first_index)?;
        self.slots.get_mut(slot as usize)?.as_mut()
    }

    /// Whether a growth left any pool behind.
    pub fn has_outdated(&self) -> bool {
        !self.outdated.is_empty()
    }

    /// What a relocation trigger reads of the arena.
    pub fn state(&self) -> ArenaState {
        ArenaState {
            page_size: self.current().page_size(),
            has_outdated: self.has_outdated(),
            // Only the current pool and the outdated ones hold a slot.
            full: self.outdated.len() + 1 == ARENA_SLOTS,
            shape: ArenaShape {
                current_pages: self.current().pages_held(),
                outdated_pools: self.outdated.len(),
                outdated_pages: self.outdated().map(SlicedPool::pages_held).sum(),
                outdated_guarded: self
                    .outdated()
                    .flat_map(SlicedPool::pages)
                    .filter(|page| page.is_guarded())
                    .count(),
            },
        }
    }

    /// The pool new allocations are carved from.
    fn current(&self) -> &SlicedPool {
        self.slots[self.current]
            .as_ref()
            .expect("the current slot always holds a pool")
    }

    /// The pool new allocations are carved from, mutably.
    fn current_mut(&mut self) -> &mut SlicedPool {
        self.slots[self.current]
            .as_mut()
            .expect("the current slot always holds a pool")
    }

    /// The pools a growth left behind.
    fn outdated(&self) -> impl Iterator<Item = &SlicedPool> {
        self.outdated
            .iter()
            .filter_map(|slot| self.slots[*slot].as_ref())
    }

    /// The pools `source` and `target` name, which are distinct.
    fn pair_mut(&mut self, source: u8, target: u8) -> Option<(&mut SlicedPool, &mut SlicedPool)> {
        let first = self.first_index;
        let [source, target] = self
            .slots
            .get_disjoint_mut([
                source.checked_sub(first)? as usize,
                target.checked_sub(first)? as usize,
            ])
            .ok()?;
        Some((source.as_mut()?, target.as_mut()?))
    }

    /// Reserve `size` bytes on the current pool, growing the pages when `size`
    /// outgrows them.
    ///
    /// # Errors
    ///
    /// [`IoError::BufferTooBig`] when no page the device allocates fits it,
    /// [`IoError::PageSizesExhausted`] when it needs a new page size and
    /// every slot is taken, and whatever the device refused.
    pub fn reserve<Storage: ComputeStorage>(
        &mut self,
        storage: &mut Storage,
        size: u64,
        mapping: PageMapping,
        failures: &mut ErrorGraph,
    ) -> Result<ManagedMemoryHandle, IoError> {
        match self.sizing.fit(size, self.current().page_size()) {
            Fit::Fits => self.current_mut().reserve(storage, size, mapping, failures),
            Fit::Grow { page_size } => self.grow(storage, size, page_size, mapping, failures),
            Fit::TooLarge => Err(IoError::BufferTooBig {
                size,
                backtrace: BackTrace::capture(),
            }),
        }
    }

    /// Reserve `size` bytes in the room the current pool holds. `None` when
    /// it has none for it, or when it would need larger pages.
    pub fn try_reserve(
        &mut self,
        size: u64,
        failures: &mut ErrorGraph,
    ) -> Option<ManagedMemoryHandle> {
        match self.sizing.fit(size, self.current().page_size()) {
            Fit::Fits => self.current_mut().try_reserve(size, failures),
            Fit::Grow { .. } | Fit::TooLarge => None,
        }
    }

    /// Release what the current pool no longer needs, and drop every outdated
    /// pool that has drained.
    pub fn cleanup<Storage: ComputeStorage>(
        &mut self,
        storage: &mut Storage,
        alloc_nr: u64,
        cleanup: Cleanup,
        failures: &mut ErrorGraph,
    ) {
        self.current_mut()
            .cleanup(storage, alloc_nr, cleanup, failures);
        self.drain(storage, failures);
    }

    /// Give every empty page of the outdated pools back, and free the slot of
    /// each pool left with none.
    ///
    /// An outdated pool returns a page the moment it empties, rather than
    /// waiting for the explicit cleanup the current pool waits for.
    fn drain<Storage: ComputeStorage>(&mut self, storage: &mut Storage, failures: &mut ErrorGraph) {
        let slots = &mut self.slots;
        self.outdated.retain(|&slot| {
            let Some(pool) = slots[slot].as_mut() else {
                return false;
            };
            pool.release_empty(storage, failures);
            let drained = pool.is_empty();
            if drained {
                slots[slot] = None;
            }
            !drained
        });
    }

    /// A report for the current pool, stating how many pages the outdated
    /// ones hold, then one per outdated pool.
    pub fn report(&self) -> impl Iterator<Item = MemoryPoolReport> + '_ {
        let current = self.current();
        let report = MemoryPoolReport {
            kind: MemoryPoolKind::Adaptive {
                page_size: current.page_size(),
                outdated_pages: self.outdated().map(SlicedPool::pages_held).sum(),
            },
            ..current.report()
        };
        core::iter::once(report).chain(self.outdated().map(SlicedPool::report))
    }

    /// Reserve a slice on the current pool for every live allocation on the
    /// outdated pages [the analysis](OutdatedPages::plan) plans to empty, so
    /// those pools drain instead of waiting on their longest-lived slice.
    ///
    /// `room` says whether a target may take a new page when the room held
    /// runs out. Nothing moves yet: the caller copies the bytes and hands the
    /// [`Landed`] relocation back to [`commit`](Self::commit).
    pub fn plan<Storage: ComputeStorage>(
        &mut self,
        room: TargetRoom,
        storage: &mut Storage,
        failures: &mut ErrorGraph,
    ) -> Relocation {
        let plan = OutdatedPages::new(self.outdated().flat_map(SlicedPool::pages)).plan();

        let mut moves = Vec::new();
        for page in plan {
            // A page whose allocations do not all find a target keeps none of
            // them: moving part of what is on it frees nothing. A page later in
            // the plan may still fit what this one did not.
            let start = moves.len();
            for allocation in page.live {
                match self.plan_move(allocation, room, storage, failures) {
                    Some(relocated) => moves.push(relocated),
                    None => {
                        moves.truncate(start);
                        break;
                    }
                }
            }
        }
        Relocation::new(moves)
    }

    /// Hand every allocation of `landed` over to the target reserved for it,
    /// now that its bytes are there, and drop the outdated pools that emptied.
    pub fn commit<Storage: ComputeStorage>(
        &mut self,
        landed: Landed,
        storage: &mut Storage,
        failures: &mut ErrorGraph,
    ) {
        for Move {
            allocation, target, ..
        } in landed.into_moves()
        {
            // Read from the handles, which the pools keep current: a move
            // stores no location of its own, so it can never hold a stale one.
            let source = allocation.descriptor().location();
            let destination = target.descriptor().location();
            // Only the slices hold the handles once these go.
            drop(target);
            drop(allocation);

            if let Some((source_pool, target_pool)) = self.pair_mut(source.pool, destination.pool) {
                source_pool
                    .slice_at(source)
                    .hand_over(target_pool.slice_at(destination), failures);
            }
        }
        self.drain(storage, failures);
    }

    /// Reserve a target for `allocation` on the current pool, and say what has
    /// to be copied into it. `None` when `room` has no place for it.
    fn plan_move<Storage: ComputeStorage>(
        &mut self,
        allocation: LiveAllocation,
        room: TargetRoom,
        storage: &mut Storage,
        failures: &mut ErrorGraph,
    ) -> Option<Move> {
        let source = allocation.handle.descriptor().location();
        let source_storage = self.pool_mut(source.pool)?.storage_at(source);

        let pool = self.current_mut();
        let target = match (pool.try_reserve(allocation.size, failures), room) {
            (Some(target), _) => target,
            (None, TargetRoom::Held) => return None,
            (None, TargetRoom::MayAllocate) => pool
                .alloc(storage, allocation.size, PageMapping::Eager, failures)
                .ok()?,
        };
        // Bytes that exist have to land somewhere real; a source never
        // resolved has none to copy.
        let copy = match source_storage {
            Some(source) => Some(StorageCopy {
                source,
                target: pool
                    .mapped_storage_at(storage, target.descriptor().location())
                    .ok()?,
            }),
            None => None,
        };

        Some(Move {
            allocation: allocation.handle,
            target,
            copy,
        })
    }

    /// Carve `size` bytes from a new pool of `page_size` pages, which takes
    /// over from the current one only once the device gave it its first page:
    /// a refused allocation leaves the page size where it was.
    fn grow<Storage: ComputeStorage>(
        &mut self,
        storage: &mut Storage,
        size: u64,
        page_size: u64,
        mapping: PageMapping,
        failures: &mut ErrorGraph,
    ) -> Result<ManagedMemoryHandle, IoError> {
        let Some(slot) = self.slots.iter().position(Option::is_none) else {
            return Err(IoError::PageSizesExhausted {
                size,
                backtrace: BackTrace::capture(),
            });
        };
        // The pages the current pool leaves empty go back with the next
        // cleanup, which drains the pool once it is outdated. Not here: a
        // reservation never releases a page, so a caller that must keep every
        // page where it is can still reserve.
        let mut pool = self.sizing.pool(page_size, self.first_index + slot as u8);
        let handle = pool.alloc(storage, size, mapping, failures)?;

        self.slots[slot] = Some(pool);
        self.outdated.push(self.current);
        self.current = slot;
        self.logger.log_memory(
            |level| !matches!(level, MemoryLogLevel::Disabled),
            || {
                format!(
                    "[{}] Carving pages of {} from here on:\n{self}",
                    self.name,
                    BytesFormat::new(page_size)
                )
            },
        );
        Ok(handle)
    }
}

impl core::fmt::Display for PoolArena {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        for pool in self.slots.iter().flatten() {
            write!(f, "{pool}")?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory_management::relocation::CopyQueue;
    use crate::server::ServerError;
    use crate::storage::{StorageHandle, StorageId, StorageUtilization};

    const MIB: u64 = 1024 * 1024;

    fn sizing() -> PageSizing {
        PageSizing::new(2 * MIB, &MemoryDeviceProperties::new(16 * MIB, 32))
    }

    /// Device memory that is only bookkeeping, refusing any allocation past
    /// `limit` bytes the way a full device would.
    struct Bookkeeping {
        limit: u64,
    }

    impl ComputeStorage for Bookkeeping {
        type Resource = ();

        fn alignment(&self) -> usize {
            32
        }

        fn get(&mut self, _handle: &StorageHandle) -> Result<(), IoError> {
            Ok(())
        }

        fn alloc(&mut self, size: u64) -> Result<StorageHandle, IoError> {
            if size > self.limit {
                return Err(IoError::OutOfMemory {
                    size,
                    backtrace: BackTrace::capture(),
                });
            }
            Ok(StorageHandle::new(
                StorageId::new(),
                StorageUtilization { offset: 0, size },
            ))
        }

        fn dealloc(&mut self, _id: StorageId) {}

        fn flush(&mut self) {}

        fn bytes_allocated(&self) -> u64 {
            0
        }
    }

    /// Copies that land the moment they are made.
    struct Landing;

    impl CopyQueue<Bookkeeping> for Landing {
        fn wait_device(&mut self) -> Result<(), ServerError> {
            Ok(())
        }

        fn copy(&mut self, _storage: &mut Bookkeeping, _copy: &StorageCopy) -> Result<(), IoError> {
            Ok(())
        }

        fn wait_copies(&mut self) -> Result<(), ServerError> {
            Ok(())
        }
    }

    fn reserve(
        arena: &mut PoolArena,
        storage: &mut Bookkeeping,
        size: u64,
    ) -> Result<ManagedMemoryHandle, IoError> {
        arena.reserve(
            storage,
            size,
            PageMapping::Eager,
            &mut ErrorGraph::default(),
        )
    }

    /// Grown to a size the device refused, every page held would be outdated
    /// and every later allocation would need a page of that size too.
    #[test]
    fn a_refused_growth_leaves_the_page_size_where_it_was() {
        let mut storage = Bookkeeping { limit: 8 * MIB };
        let mut arena = PoolArena::new(sizing(), 2, Default::default(), String::new());
        let _small = reserve(&mut arena, &mut storage, MIB).unwrap();

        let refused = reserve(&mut arena, &mut storage, 10 * MIB);
        assert!(
            matches!(refused, Err(IoError::OutOfMemory { .. })),
            "{refused:?}"
        );
        assert_eq!(arena.state().page_size, 2 * MIB);
        assert!(!arena.state().has_outdated);

        let _served = reserve(&mut arena, &mut storage, MIB).unwrap();
        assert_eq!(arena.current().pages_held(), 1, "served from the page held");
    }

    /// A growth past the last free slot fails on its own terms rather than
    /// overwriting a slot, and a relocation allowed to allocate empties the
    /// outdated pools and frees them.
    #[test]
    fn a_full_arena_refuses_to_grow_until_a_relocation_frees_it() {
        let mut storage = Bookkeeping { limit: u64::MAX };
        let sizing = PageSizing::new(2 * MIB, &MemoryDeviceProperties::new(1024 * MIB, 32));
        let mut arena = PoolArena::new(sizing, 2, Default::default(), String::new());
        let failures = &mut ErrorGraph::default();

        // Each size outgrows the page the last one grew to, so every
        // reservation takes a slot, and each pool it leaves keeps one live
        // allocation. The first pool never carved a page.
        let mut live = Vec::new();
        for step in 0..ARENA_SLOTS as u64 - 1 {
            live.push(reserve(&mut arena, &mut storage, (3 + 2 * step) * MIB).unwrap());
        }
        assert!(arena.state().full);
        let refused = reserve(&mut arena, &mut storage, 200 * MIB);
        assert!(
            matches!(refused, Err(IoError::PageSizesExhausted { .. })),
            "{refused:?}"
        );

        let relocation = arena.plan(TargetRoom::MayAllocate, &mut storage, failures);
        assert_eq!(
            relocation.len(),
            ARENA_SLOTS - 2,
            "one allocation per outdated pool that carved a page"
        );
        let landed = relocation.copy(&mut storage, &mut Landing).unwrap();
        arena.commit(landed, &mut storage, failures);

        assert!(!arena.state().has_outdated);
        reserve(&mut arena, &mut storage, 200 * MIB).expect("a slot is free again");
    }

    #[test]
    fn an_allocation_that_fits_the_pages_grows_nothing() {
        assert_eq!(sizing().fit(MIB, 2 * MIB), Fit::Fits);
    }

    #[test]
    fn a_growth_sizes_pages_to_the_allocation_with_slack() {
        assert_eq!(
            sizing().fit(10 * MIB, 2 * MIB),
            Fit::Grow {
                page_size: 11 * MIB
            }
        );
    }

    #[test]
    fn a_growth_stops_at_the_devices_largest_page() {
        assert_eq!(
            sizing().fit(16 * MIB, 2 * MIB),
            Fit::Grow {
                page_size: 16 * MIB
            }
        );
        assert_eq!(sizing().fit(16 * MIB + 1, 2 * MIB), Fit::TooLarge);
    }
}