cubecl-core 0.10.0-pre.3

CubeCL core create
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
use cubecl_ir::ManagedVariable;

use super::{CubePrimitive, Vector};
use crate::prelude::*;
use crate::{
    ir::{ElemType, Instruction, Plane, Scope, Type, UnaryOperator},
    unexpanded,
};

/// Returns true if the cube unit has the lowest `plane_unit_id` among active unit in the plane
pub fn plane_elect() -> bool {
    unexpanded!()
}

/// Module containing the expand function for [`plane_elect()`].
pub mod plane_elect {

    use super::*;

    /// Expand method of [`plane_elect()`].
    pub fn expand(scope: &mut Scope) -> NativeExpand<bool> {
        let output = scope.create_local(Type::scalar(ElemType::Bool));
        let out = *output;

        scope.register(Instruction::new(Plane::Elect, out));

        output.into()
    }
}

/// Broadcasts the value from the specified plane unit at the given index
/// to all active units within that plane. Requires a constant index. For non-constant indices,
/// use [`plane_shuffle()`].
#[allow(unused_variables)]
pub fn plane_broadcast<E: CubePrimitive>(value: E, index: u32) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_broadcast()`].
pub mod plane_broadcast {

    use super::*;

    /// Expand method of [`plane_broadcast()`].
    pub fn expand<E: CubePrimitive>(
        scope: &mut Scope,
        value: NativeExpand<E>,
        id: u32,
    ) -> NativeExpand<E> {
        let output = scope.create_local(value.expand.ty);
        let out = *output;
        let lhs = *value.expand;
        let rhs = id.into();

        scope.register(Instruction::new(
            Plane::Broadcast(crate::ir::BinaryOperator { lhs, rhs }),
            out,
        ));

        output.into()
    }
}

/// Perform an arbitrary lane shuffle operation across the plane.
/// Each unit reads the value from the specified source lane.
///
/// # Example
/// `plane_shuffle(value, 0)` - all lanes read from lane 0 (same as broadcast)
/// `plane_shuffle(value, lane_id ^ 1)` - butterfly pattern (same as `shuffle_xor`)
#[allow(unused_variables)]
pub fn plane_shuffle<E: CubePrimitive>(value: E, src_lane: u32) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_shuffle()`].
pub mod plane_shuffle {

    use super::*;

    /// Expand method of [`plane_shuffle()`].
    pub fn expand<E: CubePrimitive>(
        scope: &mut Scope,
        value: NativeExpand<E>,
        src_lane: NativeExpand<u32>,
    ) -> NativeExpand<E> {
        let output = scope.create_local(value.expand.ty);
        let out = *output;
        let lhs = *value.expand;
        let rhs = *src_lane.expand;

        scope.register(Instruction::new(
            Plane::Shuffle(crate::ir::BinaryOperator { lhs, rhs }),
            out,
        ));

        output.into()
    }
}

/// Perform a shuffle XOR operation across the plane.
/// Each unit exchanges its value with another unit at an index determined by XOR with the mask.
/// This is useful for butterfly reduction patterns.
///
/// # Example
/// For a 32-lane warp with mask=1:
/// - Lane 0 gets value from lane 1, lane 1 gets value from lane 0
/// - Lane 2 gets value from lane 3, lane 3 gets value from lane 2
/// - etc.
#[allow(unused_variables)]
pub fn plane_shuffle_xor<E: CubePrimitive>(value: E, mask: u32) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_shuffle_xor()`].
pub mod plane_shuffle_xor {

    use super::*;

    /// Expand method of [`plane_shuffle_xor()`].
    pub fn expand<E: CubePrimitive>(
        scope: &mut Scope,
        value: NativeExpand<E>,
        mask: NativeExpand<u32>,
    ) -> NativeExpand<E> {
        let output = scope.create_local(value.expand.ty);
        let out = *output;
        let lhs = *value.expand;
        let rhs = *mask.expand;

        scope.register(Instruction::new(
            Plane::ShuffleXor(crate::ir::BinaryOperator { lhs, rhs }),
            out,
        ));

        output.into()
    }
}

/// Perform a shuffle up operation across the plane.
/// Each unit reads the value from a unit with a lower lane ID (`current_id` - delta).
/// Units with `lane_id` < delta will read from themselves (no change).
///
/// # Example
/// For delta=1: `[a, b, c, d] -> [a, a, b, c]`
#[allow(unused_variables)]
pub fn plane_shuffle_up<E: CubePrimitive>(value: E, delta: u32) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_shuffle_up()`].
pub mod plane_shuffle_up {

    use super::*;

    /// Expand method of [`plane_shuffle_up()`].
    pub fn expand<E: CubePrimitive>(
        scope: &mut Scope,
        value: NativeExpand<E>,
        delta: NativeExpand<u32>,
    ) -> NativeExpand<E> {
        let output = scope.create_local(value.expand.ty);
        let out = *output;
        let lhs = *value.expand;
        let rhs = *delta.expand;

        scope.register(Instruction::new(
            Plane::ShuffleUp(crate::ir::BinaryOperator { lhs, rhs }),
            out,
        ));

        output.into()
    }
}

/// Perform a shuffle down operation across the plane.
/// Each unit reads the value from a unit with a higher lane ID (`current_id` + delta).
/// Units at the end will read from themselves if (`lane_id` + delta >= `plane_dim`).
///
/// # Example
/// For delta=1: `[a, b, c, d] -> [b, c, d, d]`
#[allow(unused_variables)]
pub fn plane_shuffle_down<E: CubePrimitive>(value: E, delta: u32) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_shuffle_down()`].
pub mod plane_shuffle_down {

    use super::*;

    /// Expand method of [`plane_shuffle_down()`].
    pub fn expand<E: CubePrimitive>(
        scope: &mut Scope,
        value: NativeExpand<E>,
        delta: NativeExpand<u32>,
    ) -> NativeExpand<E> {
        let output = scope.create_local(value.expand.ty);
        let out = *output;
        let lhs = *value.expand;
        let rhs = *delta.expand;

        scope.register(Instruction::new(
            Plane::ShuffleDown(crate::ir::BinaryOperator { lhs, rhs }),
            out,
        ));

        output.into()
    }
}

/// Perform a reduce sum operation across all units in a plane.
#[allow(unused_variables)]
pub fn plane_sum<E: CubePrimitive>(value: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_sum()`].
pub mod plane_sum {
    use super::*;

    /// Expand method of [`plane_sum()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(Plane::Sum(UnaryOperator { input }), out));

        output.into()
    }
}

/// Perform an inclusive sum operation across all units in a plane.
/// This sums all values to the "left" of the unit, including this unit's value.
/// Also known as "prefix sum" or "inclusive scan".
///
/// # Example
/// `inclusive_sum([1, 2, 3, 4, 5]) == [1, 3, 6, 10, 15]`
#[allow(unused_variables)]
pub fn plane_inclusive_sum<E: CubePrimitive>(value: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_inclusive_sum()`].
pub mod plane_inclusive_sum {
    use super::*;

    /// Expand method of [`plane_inclusive_sum()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(
            Plane::InclusiveSum(UnaryOperator { input }),
            out,
        ));

        output.into()
    }
}

/// Perform an exclusive sum operation across all units in a plane.
/// This sums all values to the "left" of the unit, excluding this unit's value. The 0th unit will
/// be set to `E::zero()`.
/// Also known as "exclusive prefix sum" or "exclusive scan".
///
/// # Example
/// `exclusive_sum([1, 2, 3, 4, 5]) == [0, 1, 3, 6, 10]`
#[allow(unused_variables)]
pub fn plane_exclusive_sum<E: CubePrimitive>(value: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_exclusive_sum()`].
pub mod plane_exclusive_sum {
    use super::*;

    /// Expand method of [`plane_exclusive_sum()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(
            Plane::ExclusiveSum(UnaryOperator { input }),
            out,
        ));

        output.into()
    }
}

/// Perform a reduce prod operation across all units in a plane.
pub fn plane_prod<E: CubePrimitive>(_elem: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_prod()`].
pub mod plane_prod {
    use super::*;

    /// Expand method of [`plane_prod()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(Plane::Prod(UnaryOperator { input }), out));

        output.into()
    }
}

/// Perform an inclusive product operation across all units in a plane.
/// This multiplies all values to the "left" of the unit, including this unit's value.
/// Also known as "prefix product" or "inclusive scan".
///
/// # Example
/// `exclusive_prod([1, 2, 3, 4, 5]) == [1, 2, 6, 24, 120]`
#[allow(unused_variables)]
pub fn plane_inclusive_prod<E: CubePrimitive>(value: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_inclusive_prod()`].
pub mod plane_inclusive_prod {
    use super::*;

    /// Expand method of [`plane_inclusive_prod()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(
            Plane::InclusiveProd(UnaryOperator { input }),
            out,
        ));

        output.into()
    }
}

/// Perform an exclusive product operation across all units in a plane.
/// This multiplies all values to the "left" of the unit, excluding this unit's value. The 0th unit
/// will be set to `E::one()`.
/// Also known as "exclusive prefix product" or "exclusive scan".
///
/// # Example
/// `exclusive_prod([1, 2, 3, 4, 5]) == [1, 1, 2, 6, 24]`
#[allow(unused_variables)]
pub fn plane_exclusive_prod<E: CubePrimitive>(value: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_exclusive_prod()`].
pub mod plane_exclusive_prod {
    use super::*;

    /// Expand method of [`plane_exclusive_prod()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(
            Plane::ExclusiveProd(UnaryOperator { input }),
            out,
        ));

        output.into()
    }
}

/// Perform a reduce max operation across all units in a plane.
pub fn plane_max<E: CubePrimitive>(_elem: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_max()`].
pub mod plane_max {
    use super::*;

    /// Expand method of [`plane_max()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(Plane::Max(UnaryOperator { input }), out));

        output.into()
    }
}

/// Perform a reduce min operation across all units in a plane.
pub fn plane_min<E: CubePrimitive>(_elem: E) -> E {
    unexpanded!()
}

/// Module containing the expand function for [`plane_min()`].
pub mod plane_min {
    use super::*;

    /// Expand method of [`plane_min()`].
    pub fn expand<E: CubePrimitive>(scope: &mut Scope, elem: NativeExpand<E>) -> NativeExpand<E> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(Plane::Min(UnaryOperator { input }), out));

        output.into()
    }
}

/// Perform a reduce all operation across all units in a plane.
pub fn plane_all(_elem: bool) -> bool {
    unexpanded!()
}

/// Module containing the expand function for [`plane_all()`].
pub mod plane_all {

    use super::*;

    /// Expand method of [`plane_all()`].
    pub fn expand(scope: &mut Scope, elem: NativeExpand<bool>) -> NativeExpand<bool> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(Plane::All(UnaryOperator { input }), out));

        output.into()
    }
}

/// Perform a reduce any operation across all units in a plane.
pub fn plane_any(_elem: bool) -> bool {
    unexpanded!()
}

/// Module containing the expand function for [`plane_any()`].
pub mod plane_any {

    use super::*;

    /// Expand method of [`plane_any()`].
    pub fn expand(scope: &mut Scope, elem: NativeExpand<bool>) -> NativeExpand<bool> {
        let elem: ManagedVariable = elem.into();
        let output = scope.create_local(elem.ty);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(Plane::Any(UnaryOperator { input }), out));

        output.into()
    }
}

/// Perform a ballot operation across all units in a plane.
/// Returns a set of 32-bit bitfields as a [`Vector`], with each element containing the value from 32
/// invocations.
/// Note that vector size will always be set to 4 even for `PLANE_DIM <= 64`, because we can't
/// retrieve the actual plane size at expand time. Use the runtime`PLANE_DIM` to index appropriately.
pub fn plane_ballot(_elem: bool) -> Vector<u32, Const<4>> {
    unexpanded!()
}

/// Module containing the expand function for [`plane_ballot()`].
pub mod plane_ballot {
    use cubecl_ir::UIntKind;

    use super::*;

    /// Expand method of [`plane_ballot()`].
    pub fn expand(
        scope: &mut Scope,
        elem: NativeExpand<bool>,
    ) -> NativeExpand<Vector<u32, Const<4>>> {
        let elem: ManagedVariable = elem.into();
        let out_item = Type::scalar(ElemType::UInt(UIntKind::U32)).with_vector_size(4);
        let output = scope.create_local(out_item);

        let out = *output;
        let input = *elem;

        scope.register(Instruction::new(
            Plane::Ballot(UnaryOperator { input }),
            out,
        ));

        output.into()
    }
}