cubecl-std 0.11.0-pre.1

CubeCL Standard Library.
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
use std::marker::PhantomData;

use cubecl::prelude::*;
use cubecl_core::{self as cubecl, ir::VectorSize, prelude::barrier::Barrier, unexpanded};

use crate::tensor::{
    ViewOperations, ViewOperationsExpand, ViewOperationsMut, ViewOperationsMutExpand, VirtualView,
    VirtualViewMut,
    layout::{Coordinates, Layout, VirtualLayout, VirtualLayoutExpand, slice::SliceLayout},
};

/// A conceptual view of an underlying linear storage.
/// Allows abstract indexing in multiple dimensions, without having to know the data layout or
/// location.
#[derive(Clone, Copy)]
pub struct View<'a, E: CubePrimitive, C: Coordinates> {
    _layout: PhantomData<C>,
    _ty: PhantomData<E>,
    _lifetime: PhantomData<&'a ()>,
}

/// Expand type of [`View`]
#[derive(Clone, Copy)]
pub struct ViewExpand<'a, E: CubePrimitive, C: Coordinates> {
    pub(super) inner: &'a (dyn ViewOperationsExpand<E, C> + 'a),
}

/// Mutable view
/// Note: `Clone` and `Copy` should ideally not be there, but are required for good ergonomics until
/// `Reborrow` and `CoerceShared` are implemented and stabilized.
#[derive(Clone, Copy)]
pub struct ViewMut<'a, E: CubePrimitive, C: Coordinates> {
    _layout: PhantomData<C>,
    _ty: PhantomData<E>,
    _lifetime: PhantomData<&'a mut ()>,
}

/// Expand type of [`ViewMutExpand`]
#[derive(Clone, Copy)]
pub struct ViewMutExpand<'a, E: CubePrimitive, C: Coordinates> {
    pub(super) inner: &'a (dyn ViewOperationsMutExpand<E, C> + 'a),
}

macro_rules! impl_cube_type {
    ($ty: ident, $expand: ident) => {
        impl<'a, E: CubePrimitive, C: Coordinates + 'a> CubeType for $ty<'a, E, C> {
            type ExpandType = $expand<'a, E, C>;
        }

        impl<'a, E: CubePrimitive, C: Coordinates> IntoExpand for $expand<'a, E, C> {
            type Expand = $expand<'a, E, C>;

            fn into_expand(self, _: &Scope) -> Self::Expand {
                self
            }
        }

        impl<'a, E: CubePrimitive, C: Coordinates> ExpandTypeClone for $expand<'a, E, C> {
            fn clone_unchecked(&self) -> Self {
                self.clone()
            }
        }

        impl<'a, E: CubePrimitive, C: Coordinates> IntoMut for $expand<'a, E, C> {
            fn into_mut(self, _scope: &Scope) -> Self {
                self
            }
        }

        impl<'a, E: CubePrimitive, C: Coordinates> CubeDebug for $expand<'a, E, C> {}

        impl<'a, E: CubePrimitive, C: Coordinates> AsRefExpand for $expand<'a, E, C> {
            fn __expand_ref_method(&self, _: &Scope) -> &Self {
                self
            }
        }
        impl<'a, E: CubePrimitive, C: Coordinates> AsMutExpand for $expand<'a, E, C> {
            fn __expand_ref_mut_method(&mut self, _: &Scope) -> &mut Self {
                self
            }
        }
        impl<'a, E: CubePrimitive, C: Coordinates> DerefExpand for $expand<'a, E, C> {
            type Target = Self;

            fn __expand_deref_method(&self, _: &Scope) -> Self::Target {
                self.clone()
            }
        }
    };
}

impl_cube_type!(View, ViewExpand);
impl_cube_type!(ViewMut, ViewMutExpand);

impl<'a, E: CubePrimitive, C: Coordinates + 'a> View<'a, E, C> {
    /// Create a new tensor view from an underlying concrete storage and a layout to map it into
    /// the target coordinate space
    #[allow(unused_variables)]
    pub fn new<V: ViewOperations<E, S> + 'a, S: Coordinates>(
        view: V,
        layout: impl Into<VirtualLayout<C, S>>,
    ) -> Self {
        View {
            _layout: PhantomData,
            _ty: PhantomData,
            _lifetime: PhantomData,
        }
    }

    /// Expand function for [`View::new`]
    pub fn __expand_new<V: ViewOperations<E, S> + 'a, S: Coordinates + 'a>(
        scope: &Scope,
        view: V::ExpandType,
        layout: VirtualLayoutExpand<C, S>,
    ) -> ViewExpand<'a, E, C> {
        ViewExpand::new(
            scope,
            VirtualView::<E, C, S, V>::__expand_new(scope, view, layout),
        )
    }
}

impl<'a, E: CubePrimitive, C: Coordinates + 'a> ViewMut<'a, E, C> {
    /// Create a new tensor view from an underlying concrete storage and a layout to map it into
    /// the target coordinate space
    #[allow(unused_variables)]
    pub fn new<V: ViewOperationsMut<E, S> + 'a, S: Coordinates>(
        view: V,
        layout: impl Into<VirtualLayout<C, S>>,
    ) -> Self {
        ViewMut {
            _layout: PhantomData,
            _ty: PhantomData,
            _lifetime: PhantomData,
        }
    }

    /// Expand function for [`View::new`]
    pub fn __expand_new<V: ViewOperationsMut<E, S> + 'a, S: Coordinates + 'a>(
        scope: &Scope,
        view: V::ExpandType,
        layout: VirtualLayoutExpand<C, S>,
    ) -> ViewMutExpand<'a, E, C> {
        ViewMutExpand::new(
            scope,
            VirtualViewMut::<E, C, S, V>::__expand_new(scope, view, layout),
        )
    }
}

macro_rules! impl_read {
    ($ty: ident, $expand: ident) => {
        impl<'a, E: CubePrimitive, C: Coordinates + 'a> $ty<'a, E, C> {
            pub fn view<T: Coordinates + 'a>(
                self,
                _layout: impl Into<VirtualLayout<T, C>>,
            ) -> $ty<'a, E, T> {
                unexpanded!()
            }

            pub fn __expand_view<T: Coordinates + 'a>(
                scope: &Scope,
                this: $expand<'a, E, C>,
                layout: VirtualLayoutExpand<T, C>,
            ) -> $expand<'a, E, T> {
                this.__expand_view_method(scope, layout)
            }
        }

        impl<'a, E: CubePrimitive, C: Coordinates + 'a> $expand<'a, E, C> {
            pub fn __expand_view_method<T: Coordinates + 'a>(
                self,
                scope: &Scope,
                layout: VirtualLayoutExpand<T, C>,
            ) -> $expand<'a, E, T> {
                $ty::__expand_new::<$ty<'a, E, C>, C>(scope, self, layout)
            }
        }

        #[cube]
        impl<'a, E: CubePrimitive, C: Coordinates> $ty<'a, E, C> {
            /// Calls [`Layout::shape`] on the view's layout
            pub fn shape(&self) -> C {
                intrinsic!(|scope| self.inner.__expand_shape_method(scope))
            }

            /// Calls [`Layout::is_in_bounds`] on the view's layout
            #[allow(unused_variables)]
            pub fn is_in_bounds(&self, pos: C) -> bool {
                intrinsic!(|scope| self.inner.__expand_is_in_bounds_method(scope, pos))
            }
        }

        #[cube]
        impl<'a, E: CubePrimitive, C: Coordinates> $ty<'a, E, C> {
            /// Read a value at `pos`. The layout handles translation into a concrete index.
            #[allow(unused_variables)]
            pub fn read(&self, pos: C) -> E {
                intrinsic!(|scope| self.inner.__expand_read_method(scope, pos))
            }

            /// Read a value at `pos`. The layout handles translation into a concrete index.
            /// Reading is done unchecked
            #[allow(unused_variables)]
            pub fn read_unchecked(&self, pos: C) -> E {
                intrinsic!(|scope| self.inner.__expand_read_unchecked_method(scope, pos))
            }

            /// Read a value at `pos` if it's in bounds. The layout handles translation into a concrete index.
            #[allow(unused_variables)]
            pub fn read_checked(&self, pos: C) -> E {
                intrinsic!(|scope| self.inner.__expand_read_checked_method(scope, pos))
            }

            /// Read a value at `pos` if it's in bounds, returning `mask_value` otherwise. The layout handles translation into a concrete index.
            #[allow(unused_variables)]
            pub fn read_masked(&self, pos: C, mask_value: E) -> E {
                intrinsic!(|scope| self
                    .inner
                    .__expand_read_masked_method(scope, pos, mask_value))
            }

            /// Interpret this view as a linear slice encompassing the entire view.
            ///
            /// # Safety
            ///
            /// No checking is done on whether the slice is contiguous in memory.
            pub fn as_linear_slice(&self) -> &'a [E] {
                intrinsic!(|scope| {
                    let shape = self.inner.__expand_shape_method(scope);
                    let origin = C::__expand_from_int(scope, shape.clone_unchecked(), 0);
                    // Inclusive end so clamping works correctly
                    let one = C::__expand_from_int(scope, shape.clone_unchecked(), 1);
                    let shape = C::__expand_max(scope, shape, one.clone_unchecked());
                    let end = C::__expand_sub(scope, shape, one);
                    let slice = self
                        .inner
                        .__expand_as_linear_slice_method(scope, origin, end);
                    scope.create_kernel_ref(slice.expand.into())
                })
            }

            pub fn vector_size(&self) -> comptime_type!(VectorSize) {
                intrinsic!(|scope| self.inner.vector_size())
            }
        }

        impl<'a, E: CubePrimitive, C: Coordinates> $expand<'a, E, C> {
            pub(super) fn __expand_as_linear_slice_inner_method(
                &self,
                scope: &Scope,
                pos: C::ExpandType,
                end: C::ExpandType,
            ) -> &SliceExpand<E> {
                self.inner.__expand_as_linear_slice_method(scope, pos, end)
            }
        }

        #[cube]
        impl<'a, E: CubePrimitive, C: Coordinates + 'static> $ty<'a, E, C> {
            /// Create a slice starting from `pos`, with `size`.
            /// The layout handles translation into concrete indices.
            /// Size will be clamped to the current layout size.
            #[allow(unused_variables)]
            pub fn slice(self, pos: C, size: C) -> $ty<'a, E, C> {
                intrinsic!(|scope| self.slice(scope, pos, size, true))
            }

            /// Create a slice starting from `pos`, with `size`.
            /// The layout handles translation into concrete indices.
            /// Size and pos will be clamped to the current layout size.
            /// #Safety
            /// Access is always unchecked
            #[allow(unused_variables)]
            pub fn slice_unchecked(self, pos: C, size: C) -> $ty<'a, E, C> {
                intrinsic!(|scope| self.slice(scope, pos, size, false))
            }
        }

        impl<'a, E: CubePrimitive, C: Coordinates + 'static> $expand<'a, E, C> {
            fn slice(
                self,
                scope: &Scope,
                pos: C::ExpandType,
                size: C::ExpandType,
                checked: bool,
            ) -> $expand<'a, E, C> {
                let shape = self.__expand_shape_method(scope);
                let pos = C::__expand_min(scope, pos, shape.clone_unchecked());
                let max_size = C::__expand_sub(scope, shape, pos.clone_unchecked());
                let size = C::__expand_min(scope, size, max_size);
                let layout = SliceLayout::__expand_new(scope, pos, size, checked);
                $ty::__expand_new::<$ty<'a, E, C>, _>(scope, self, layout.into())
            }
        }

        #[cube]
        impl<'a, E: CubePrimitive, C: Coordinates + 'a> $ty<'a, E, C> {
            /// Execute a TMA load into shared memory, if the underlying storage supports it.
            /// Panics if it's unsupported.
            #[allow(unused_variables)]
            pub fn tensor_map_load(&self, barrier: &Barrier, shared_memory: &mut [E], pos: C) {
                intrinsic!(|scope| {
                    self.inner
                        .__expand_tensor_map_load_method(scope, barrier, shared_memory, pos)
                })
            }
        }
    };
}

impl_read!(View, ViewExpand);
impl_read!(ViewMut, ViewMutExpand);

impl<'a, E: CubePrimitive, C: Coordinates + 'a> ViewMut<'a, E, C> {
    /// Reborrow this mutable view as a read-only [`View`] over the same storage. Lets a single
    /// `ViewMut` carrier serve both read and write paths without a second handle.
    #[allow(unused_variables)]
    pub fn as_read(self) -> View<'a, E, C> {
        unexpanded!()
    }

    pub fn __expand_as_read(scope: &Scope, this: ViewMutExpand<'a, E, C>) -> ViewExpand<'a, E, C> {
        this.__expand_as_read_method(scope)
    }
}

impl<'a, E: CubePrimitive, C: Coordinates + 'a> ViewMutExpand<'a, E, C> {
    pub fn __expand_as_read_method(self, _scope: &Scope) -> ViewExpand<'a, E, C> {
        let inner: &'a (dyn ViewOperationsExpand<E, C> + 'a) = self.inner;
        ViewExpand { inner }
    }
}

impl<'a, E: CubePrimitive, C: Coordinates> ViewExpand<'a, E, C> {
    pub fn new<V: ViewOperationsExpand<E, C> + 'a>(scope: &Scope, view: V) -> Self {
        let inner: &dyn ViewOperationsExpand<E, C> = scope.create_kernel_ref(view);
        ViewExpand { inner }
    }
}

impl<'a, E: CubePrimitive, C: Coordinates> ViewMutExpand<'a, E, C> {
    pub fn new<V: ViewOperationsMutExpand<E, C> + 'a>(scope: &Scope, view: V) -> Self {
        let inner: &mut dyn ViewOperationsMutExpand<E, C> = scope.create_kernel_ref(view);
        ViewMutExpand { inner }
    }
}

impl<'a, E: CubePrimitive, C: Coordinates + 'a> ViewMut<'a, E, C> {
    pub fn view_mut<'b, T: Coordinates + 'a>(
        self,
        _layout: impl Layout<Coordinates = T, SourceCoordinates = C>,
    ) -> ViewMut<'b, E, T>
    where
        'a: 'b,
    {
        unexpanded!()
    }

    pub fn __expand_view_mut<T: Coordinates + 'a>(
        scope: &Scope,
        this: ViewMutExpand<'a, E, C>,
        layout: VirtualLayoutExpand<T, C>,
    ) -> ViewMutExpand<'a, E, T> {
        this.__expand_view_mut_method(scope, layout)
    }
}

impl<'a, E: CubePrimitive, C: Coordinates + 'a> ViewMutExpand<'a, E, C> {
    pub fn __expand_view_mut_method<'b, T: Coordinates + 'a>(
        self,
        scope: &Scope,
        layout: VirtualLayoutExpand<T, C>,
    ) -> ViewMutExpand<'b, E, T>
    where
        'a: 'b,
    {
        ViewMut::__expand_new::<ViewMut<'a, E, C>, C>(scope, self, layout)
    }
}

#[cube]
impl<'a, E: CubePrimitive, C: Coordinates> ViewMut<'a, E, C> {
    /// Write a value to `pos`. The layout handles translation into a concrete index.
    #[allow(unused_variables)]
    pub fn write(&mut self, pos: C, value: E) {
        intrinsic!(|scope| self.inner.__expand_write_method(scope, pos, value));
    }

    /// Write a value to `pos` if it's in bounds. The layout handles translation into a concrete index.
    #[allow(unused_variables)]
    pub fn write_checked(&mut self, pos: C, value: E) {
        intrinsic!(|scope| self.inner.__expand_write_checked_method(scope, pos, value));
    }

    /// Interpret this view as a mutable linear slice encompassing the entire view.
    ///
    /// # Safety
    ///
    /// No checking is done on whether the slice is contiguous in memory.
    pub fn as_linear_slice_mut(&mut self) -> &'a mut [E] {
        intrinsic!(|scope| {
            let shape = self.inner.__expand_shape_method(scope);
            let origin = C::__expand_from_int(scope, shape.clone_unchecked(), 0);
            // Inclusive end so clamping works correctly
            let one = C::__expand_from_int(scope, shape.clone_unchecked(), 1);
            let shape = C::__expand_max(scope, shape, one.clone_unchecked());
            let end = C::__expand_sub(scope, shape, one);
            let slice = self
                .inner
                .__expand_as_linear_slice_mut_method(scope, origin, end);
            scope.create_kernel_ref(slice.expand.into())
        })
    }
}

impl<'a, E: CubePrimitive, C: Coordinates> ViewMutExpand<'a, E, C> {
    pub(super) fn __expand_to_linear_slice_mut_inner_method(
        &mut self,
        scope: &Scope,
        pos: C::ExpandType,
        end: C::ExpandType,
    ) -> &mut SliceExpand<E> {
        self.inner
            .__expand_as_linear_slice_mut_method(scope, pos, end)
    }
}

#[cube]
impl<'a, E: CubePrimitive, C: Coordinates + 'static> ViewMut<'a, E, C> {
    /// Create a mutable slice starting from `pos`, with `size`.
    /// The layout handles translation into concrete indices.
    /// Size and pos will be clamped to the current layout size.
    #[allow(unused_variables)]
    pub fn slice_mut(self, pos: C, size: C) -> ViewMut<'a, E, C> {
        intrinsic!(|scope| self.slice_mut(scope, pos, size, true))
    }

    /// Create a mutable slice starting from `pos`, with `size`.
    /// The layout handles translation into concrete indices.
    /// Size and pos will be clamped to the current layout size.
    ///
    /// # Safety
    /// Access is always unchecked.
    #[allow(unused_variables)]
    pub fn slice_mut_unchecked(self, pos: C, size: C) -> ViewMut<'a, E, C> {
        intrinsic!(|scope| self.slice_mut(scope, pos, size, false))
    }
}

impl<'a, E: CubePrimitive, C: Coordinates + 'static> ViewMutExpand<'a, E, C> {
    fn slice_mut(
        &self,
        scope: &Scope,
        pos: C::ExpandType,
        size: C::ExpandType,
        checked: bool,
    ) -> ViewMutExpand<'a, E, C> {
        let shape = self.__expand_shape_method(scope);
        let pos = C::__expand_min(scope, pos, shape.clone_unchecked());
        let max_size = C::__expand_sub(scope, shape, pos.clone_unchecked());
        let size = C::__expand_min(scope, size, max_size);
        let layout = SliceLayout::__expand_new(scope, pos, size, checked);
        self.clone().__expand_view_mut_method(scope, layout.into())
    }
}

#[cube]
impl<'a, E: CubePrimitive, C: Coordinates> ViewMut<'a, E, C> {
    /// Execute a TMA store into global memory, if the underlying storage supports it.
    /// Panics if it's unsupported.
    #[allow(unused_variables)]
    pub fn tensor_map_store(&self, shared_memory: &[E], pos: C) {
        intrinsic!(|scope| {
            self.inner
                .__expand_tensor_map_store_method(scope, shared_memory, pos)
        })
    }
}