t4a-cubecl-runtime 0.11.0

The runtime API of CubeCL: clients, devices, handles and everything a kernel launch needs.
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
use super::{StatisticsCollector, StatisticsRecorder, StreamMode};
use cubecl_environment::sync::{AtomicUsize, Mutex, Ordering};

/// What the process does with the work it is asked to run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcessMode {
    /// Launches run. What the process does when no override is open.
    Execute,
    /// Launches and tune candidates queue their kernels and are discarded. The
    /// queue compiles in one batch, at the next launch that loads a kernel. A
    /// tune measures and decides nothing.
    CompileOnly,
    /// Launches compile their kernels and are discarded. A tune measures its
    /// candidates for real.
    CompileAndAutotune,
}

impl ProcessMode {
    /// The process mode now.
    pub fn current() -> Self {
        OpenState::load().mode()
    }

    /// The mode every stream has under it, unless a
    /// [`StreamModeOverride`](super::StreamModeOverride) sets its own.
    pub fn stream_mode(self) -> StreamMode {
        match self {
            Self::Execute => StreamMode::Execute,
            Self::CompileOnly | Self::CompileAndAutotune => StreamMode::Discard,
        }
    }
}

/// Applies a process mode to the whole process while it lives, counting what it
/// triggers into a collector, on every thread and every device.
///
/// A build that compiles everything and then tunes with it compiled opens
/// two, one after the other, with one collector:
///
/// ```no_run
/// # fn warm_up() {}
/// use cubecl_runtime::execution::{ProcessModeOverride, ProcessMode, StatisticsCollector};
///
/// let collector = StatisticsCollector::new();
///
/// // Gather every kernel the warm-up reaches, and compile them together...
/// let compile = ProcessModeOverride::new(ProcessMode::CompileOnly, &collector);
/// warm_up();
/// core::mem::drop(compile);
///
/// // ...then tune with them compiled.
/// let tune = ProcessModeOverride::new(ProcessMode::CompileAndAutotune, &collector);
/// warm_up();
/// core::mem::drop(tune);
///
/// let statistics = collector.statistics();
/// assert_eq!(statistics.compilation.settled(), statistics.compilation.registered);
/// ```
///
/// Overlapping overrides of one process mode and one collector compose, so one
/// opened while another is still open leaves the process mode until the last of
/// them drops. One process mode and one collector at a time: the process mode is
/// process-wide, and the work under it counts to one collector.
///
/// The process mode is read where a launch is issued, with relaxed ordering, so a
/// launch another thread had already begun issuing may still execute. What
/// is guaranteed is the launches issued by the thread that opened it, and
/// every launch issued after other threads observe it.
///
/// There is deliberately no configuration file or environment variable for
/// it. An override left open by accident turns the rest of the process into
/// launches that quietly do nothing and read back uninitialized memory, so
/// its lifetime belongs to a scope in the code that wants it.
#[derive(Debug)]
pub struct ProcessModeOverride {
    mode: ProcessMode,
}

impl ProcessModeOverride {
    /// # Panics
    ///
    /// If an override of another process mode, or of another collector, is open.
    pub fn new(mode: ProcessMode, collector: &StatisticsCollector) -> Self {
        let mut active = ACTIVE.lock();
        let open = OpenState::load();
        match Opening::new(open, active.as_ref(), mode, collector) {
            Opening::First => {
                *active = Some(collector.recorder());
                OpenState::first(mode).store();
            }
            Opening::Joins => open.joined().store(),
            Opening::OtherCollector => {
                drop(active);
                panic!("an override cannot open while one of another collector is")
            }
            Opening::OtherMode => {
                drop(active);
                panic!(
                    "an override of {mode:?} cannot open while one of {:?} is",
                    open.mode()
                )
            }
        }
        Self { mode }
    }

    /// The process mode it applies.
    pub fn mode(&self) -> ProcessMode {
        self.mode
    }

    /// Where the work the open override triggers is tallied, or `None` when
    /// none is open: what a [`Registration`](super::Registration) holds.
    ///
    /// A launch reads it where it is issued and carries it to the thread that
    /// runs it ([`IssuedRecorder`](super::IssuedRecorder)), so its kernels
    /// count to the override open then. Read without a lock first, then under
    /// [`ACTIVE`]'s: a launch issued as one override closes while another
    /// collector's opens on another thread counts to the one opening.
    pub(crate) fn active_recorder() -> Option<StatisticsRecorder> {
        if !OpenState::load().is_open() {
            return None;
        }
        ACTIVE.lock().clone()
    }
}

impl Drop for ProcessModeOverride {
    fn drop(&mut self) {
        // The last guard out closes the override, its process mode and its
        // collector with it.
        let mut active = ACTIVE.lock();
        let left = OpenState::load().left();
        if !left.is_open() {
            *active = None;
        }
        left.store();
    }
}

/// What opening an override meets.
enum Opening {
    /// Nothing open: it opens.
    First,
    /// One of its process mode and its collector: it joins.
    Joins,
    /// One of another collector.
    OtherCollector,
    /// One of its collector, of another process mode.
    OtherMode,
}

impl Opening {
    fn new(
        open: OpenState,
        active: Option<&StatisticsRecorder>,
        mode: ProcessMode,
        collector: &StatisticsCollector,
    ) -> Self {
        let Some(active) = active.filter(|_| open.is_open()) else {
            return Self::First;
        };
        if !active.tallies_into(collector) {
            Self::OtherCollector
        } else if open.mode() != mode {
            Self::OtherMode
        } else {
            Self::Joins
        }
    }
}

/// The open override as the process holds it: its process mode's level in the low
/// [`LEVEL_BITS`](Self::LEVEL_BITS), and above them how many guards hold it
/// open. Written only under [`ACTIVE`]'s lock, and read without it on every
/// launch.
#[derive(Debug, Clone, Copy)]
struct OpenState(usize);

/// [`OpenState`] as the process holds it.
static OPEN: AtomicUsize = AtomicUsize::new(0);
/// Where the open override tallies, while one is.
static ACTIVE: Mutex<Option<StatisticsRecorder>> = Mutex::new(None);

impl OpenState {
    /// The bits that hold the process mode's level.
    const LEVEL_BITS: u32 = 2;
    /// Masks the state down to the process mode's level.
    const LEVEL_MASK: usize = (1 << Self::LEVEL_BITS) - 1;
    /// One guard, in the state's count.
    const GUARD: usize = 1 << Self::LEVEL_BITS;

    fn load() -> Self {
        Self(OPEN.load(Ordering::Relaxed))
    }

    fn store(self) {
        OPEN.store(self.0, Ordering::Relaxed);
    }

    /// One guard holding `mode` open. Every process mode has a level of its own,
    /// [`Execute`](ProcessMode::Execute) included, so an override of it
    /// is told apart from none: it tallies what a real run compiles and
    /// tunes.
    fn first(mode: ProcessMode) -> Self {
        let level = match mode {
            ProcessMode::CompileOnly => 1,
            ProcessMode::CompileAndAutotune => 2,
            ProcessMode::Execute => 3,
        };
        Self(Self::GUARD | level)
    }

    fn is_open(self) -> bool {
        self.0 != 0
    }

    /// The open process mode; none open executes.
    fn mode(self) -> ProcessMode {
        match self.0 & Self::LEVEL_MASK {
            1 => ProcessMode::CompileOnly,
            2 => ProcessMode::CompileAndAutotune,
            _ => ProcessMode::Execute,
        }
    }

    /// One more guard.
    fn joined(self) -> Self {
        Self(self.0 + Self::GUARD)
    }

    /// One guard fewer, closed with the last.
    fn left(self) -> Self {
        match self.0 - Self::GUARD {
            rest if rest < Self::GUARD => Self(0),
            rest => Self(rest),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::super::statistics::{KernelHandling, KernelOutcome, TuneOutcome, TunePick};
    use super::super::{CompilationStatistics, KernelRegistration, TuneRegistration};
    use super::*;
    // `serial_test`'s macro expands to `vec!`, which a `no_std` crate has to
    // bring in itself.
    use alloc::vec;
    use alloc::vec::Vec;

    /// One process mode is open at a time: an override of another refuses to open
    /// beside it.
    #[test]
    #[serial_test::serial]
    #[should_panic(
        expected = "an override of CompileOnly cannot open while one of CompileAndAutotune is"
    )]
    fn process_modes_do_not_overlap() {
        let collector = StatisticsCollector::new();
        let opened = ProcessModeOverride::new(ProcessMode::CompileAndAutotune, &collector);
        let refused = ProcessModeOverride::new(ProcessMode::CompileOnly, &collector);
        core::mem::drop(opened);
        core::mem::drop(refused);
    }

    /// The work under an override counts to one collector: another's
    /// refuses to open beside it, whatever its process mode.
    #[test]
    #[serial_test::serial]
    #[should_panic(expected = "cannot open while one of another collector is")]
    fn two_collectors_do_not_overlap() {
        let (first, second) = (StatisticsCollector::new(), StatisticsCollector::new());
        let opened = ProcessModeOverride::new(ProcessMode::CompileOnly, &first);
        let refused = ProcessModeOverride::new(ProcessMode::CompileOnly, &second);
        core::mem::drop(opened);
        core::mem::drop(refused);
    }

    /// A refused open leaves the open override as it was.
    #[test]
    #[serial_test::serial]
    fn a_refused_open_leaves_the_open_process_mode() {
        let collector = StatisticsCollector::new();
        let tune = ProcessModeOverride::new(ProcessMode::CompileAndAutotune, &collector);
        let refused = std::panic::catch_unwind(|| {
            ProcessModeOverride::new(ProcessMode::CompileOnly, &collector)
        });
        assert!(refused.is_err());
        assert_eq!(ProcessMode::current(), ProcessMode::CompileAndAutotune);
        core::mem::drop(tune);
        assert_eq!(ProcessMode::current(), ProcessMode::Execute);
    }

    /// Overlapping overrides of one process mode and collector compose: the last
    /// out closes it.
    #[test]
    #[serial_test::serial]
    fn overrides_nest() {
        let collector = StatisticsCollector::new();
        {
            let outer = ProcessModeOverride::new(ProcessMode::CompileOnly, &collector);
            {
                let inner = ProcessModeOverride::new(ProcessMode::CompileOnly, &collector);
                core::mem::drop(inner);
            }
            assert_eq!(
                ProcessMode::current(),
                ProcessMode::CompileOnly,
                "the outer is in force"
            );
            core::mem::drop(outer);
        }
        assert_eq!(ProcessMode::current(), ProcessMode::Execute);
        assert!(ProcessModeOverride::active_recorder().is_none());
    }

    /// What an override triggers counts to its collector, over every
    /// override of it — an `Execute` one included — and a registration made
    /// under one settles there after it closes; what happens to settled work
    /// counts once; a reader reads the same counts.
    #[test]
    #[serial_test::serial]
    fn work_counts_to_the_collector_of_the_open_override() {
        assert!(
            KernelRegistration::register().is_none(),
            "nothing counts outside an override"
        );
        let collector = StatisticsCollector::new();
        let reader = collector.reader();

        let compile = ProcessModeOverride::new(ProcessMode::CompileOnly, &collector);
        let queued: Vec<KernelRegistration> = (0..4)
            .filter_map(|_| KernelRegistration::register())
            .collect();
        let gathered = TuneRegistration::register().expect("an override is open");
        core::mem::drop(compile);

        // Kernels queued in one override compile in the next.
        let tune = ProcessModeOverride::new(ProcessMode::CompileAndAutotune, &collector);
        let mut queued = queued.into_iter();
        let mut compiled = queued.next().unwrap().settle(KernelOutcome::Compiled);
        compiled.record(KernelHandling::Refused);
        compiled.record(KernelHandling::Refused);
        let mut loaded = queued.next().unwrap().settle(KernelOutcome::Loaded);
        loaded.record(KernelHandling::Stored);
        let _failed = queued.next().unwrap().settle(KernelOutcome::Failed);
        let mut measured = gathered.settle(TuneOutcome::Measured);
        measured.record(TunePick::Persisted);

        let statistics = collector.statistics();
        assert_eq!(
            statistics.compilation,
            CompilationStatistics {
                registered: 4,
                compiled: 1,
                loaded: 1,
                failed: 1,
                abandoned: 0,
                refused: 1,
                stored: 1,
            },
            "a refusal told twice counts once"
        );
        assert_eq!(
            statistics.autotune.settled(),
            statistics.autotune.registered
        );
        assert_eq!(statistics.autotune.persisted, 1);
        assert_eq!(reader.statistics(), statistics);
        core::mem::drop(tune);

        let execute = ProcessModeOverride::new(ProcessMode::Execute, &collector);
        let _measured = TuneRegistration::register()
            .expect("an execute override counts too")
            .settle(TuneOutcome::Measured);
        core::mem::drop(execute);
        assert_eq!(collector.statistics().autotune.measured, 2);
        // The fourth kernel, never settled, is abandoned as it drops.
        drop(queued);
        assert_eq!(collector.statistics().compilation.abandoned, 1);
    }

    /// A registration dropped before it settled — its batch or its tune
    /// panicked — settles as abandoned.
    #[test]
    #[serial_test::serial]
    fn a_dropped_registration_settles_as_abandoned() {
        let collector = StatisticsCollector::new();
        let compile = ProcessModeOverride::new(ProcessMode::CompileOnly, &collector);
        drop(KernelRegistration::register());
        drop(TuneRegistration::register());
        let statistics = collector.statistics();
        assert_eq!(
            (
                statistics.compilation.abandoned,
                statistics.compilation.settled()
            ),
            (1, 1)
        );
        assert_eq!(
            (statistics.autotune.abandoned, statistics.autotune.settled()),
            (1, 1)
        );
        core::mem::drop(compile);
    }
}