Skip to main content

cubecl_core/
lib.rs

1#![no_std]
2
3#[cfg(feature = "std")]
4extern crate std;
5
6extern crate alloc;
7
8#[macro_use]
9extern crate derive_new;
10
11pub use cubecl_zspace as zspace;
12use cubecl_zspace::Shape;
13use cubecl_zspace::Strides;
14
15/// Cube Frontend Types.
16pub mod frontend;
17/// Input Output utilities.
18pub mod io;
19
20pub mod post_processing;
21
22/// Some future utilities that work across environments.
23pub use cubecl_environment as environment;
24pub use cubecl_environment::future;
25
26use cubecl_ir::VectorSize;
27use cubecl_runtime::client::Client;
28pub use cubecl_runtime::memory_management::MemoryConfiguration;
29use cubecl_runtime::server::CubeCountSelection;
30pub use frontend::cmma;
31
32/// Cube Language Internal Representation.
33pub use cubecl_ir as ir;
34
35pub mod codegen;
36pub mod compute;
37pub mod prelude;
38
39mod pod;
40
41pub use codegen::*;
42pub use pod::*;
43
44pub use cubecl_macros::*;
45pub use cubecl_runtime::benchmark;
46pub use cubecl_runtime::client;
47pub use cubecl_runtime::compiler::{CompilationError, Compiler};
48pub use cubecl_runtime::kernel::{CubeKernel, PrecompiledSource};
49pub use cubecl_runtime::memory_management::MemoryUsage;
50pub use cubecl_runtime::memory_management::{
51    InstallMemoryPoolsError, MemoryPoolKind, MemoryPoolReport, MemoryReport,
52};
53pub use cubecl_runtime::server;
54pub use cubecl_runtime::throughput;
55pub use cubecl_runtime::tune;
56
57use frontend::LaunchArg;
58
59pub use cubecl_common::*;
60
61pub use prelude::CubeCount;
62pub use prelude::{CubeDim, ExecutionMode};
63
64pub use num_traits;
65
66mod id;
67pub use id::*;
68
69// Private utils for macros
70#[doc(hidden)]
71pub mod __private {
72    pub use alloc::sync::Arc;
73    pub use alloc::{format, vec};
74    pub use cubecl_runtime::runtime::Runtime;
75    pub use paste::paste;
76}
77
78pub use prelude::{Assign, IntoRuntime};
79
80/// Calculate the number of cubes required to execute an operation where one cube unit is
81/// assigned to one element.
82pub fn calculate_cube_count_elemwise(
83    client: &Client,
84    num_elems: usize,
85    cube_dim: CubeDim,
86) -> CubeCount {
87    if num_elems == 0 {
88        return CubeCount::Static(0, 0, 0);
89    }
90    let num_cubes = num_elems.div_ceil(cube_dim.num_elems() as usize);
91    CubeCountSelection::new(client, num_cubes as u32).cube_count()
92}
93
94pub fn tensor_vectorization_factor(
95    factors: &[VectorSize],
96    shape: &Shape,
97    strides: &Strides,
98    dim: usize,
99) -> VectorSize {
100    tensor_vector_size_parallel(factors.iter().cloned(), shape, strides, dim)
101}
102pub fn tensor_vectorization(
103    factors: &[VectorSize],
104    shape: &Shape,
105    strides: &Strides,
106    dim: usize,
107) -> VectorSize {
108    tensor_vector_size_parallel(factors.iter().cloned(), shape, strides, dim)
109}
110
111#[derive(Debug, Clone)]
112pub enum VectorizationError {
113    AxisOutOfBounds,
114    StrideMismatch,
115    NoValidVectorization,
116}
117
118/// Find the maximum vector size usable for parallel vectorization along the given axis
119/// from the supported vector sizes or return 1 if vectorization is impossible.
120///
121/// This function is designed to never return a vector size above 1 by error,
122/// but doesn't guarantee to always return the actual maximum possible vector size.
123/// That is, it may be overly strict.
124///
125/// Currently, this checks that the stride of the axis is 1, that its shape is
126/// divisible by a candidate vector size and that every non-broadcast stride outside
127/// the axis is divisible by the vector size.
128/// The last condition ensures a vectorized read on `axis` stays contiguous in the
129/// source buffer as coordinates in other dimensions change.
130pub fn tensor_vector_size_parallel(
131    optimized_vector_sizes: impl Iterator<Item = VectorSize>,
132    shape: &Shape,
133    strides: &Strides,
134    axis: usize,
135) -> VectorSize {
136    try_tensor_vector_size_parallel(optimized_vector_sizes, shape, strides, axis).unwrap_or(1)
137}
138
139/// Like `try_tensor_vector_size_parallel` but does not assume 1 is supported
140pub fn try_tensor_vector_size_parallel(
141    supported_vector_sizes: impl Iterator<Item = VectorSize>,
142    shape: &Shape,
143    strides: &Strides,
144    axis: usize,
145) -> Result<VectorSize, VectorizationError> {
146    let stride = strides
147        .get(axis)
148        .ok_or(VectorizationError::AxisOutOfBounds)?;
149    if *stride != 1 {
150        return Err(VectorizationError::StrideMismatch);
151    }
152
153    let axis_shape = shape.get(axis).ok_or(VectorizationError::AxisOutOfBounds)?;
154
155    // Smallest non-zero stride among non-axis dims. Stride 0 is a broadcast and
156    // never contributes to the source offset, so it can be ignored. Every other
157    // dim can shift the source offset when its coord changes, so its stride must
158    // be a multiple of the vector size for vectorized reads to stay aligned.
159    // Unit-size dims are included for simplicity; they only cause false negatives
160    // (vectorization disabled) rather than incorrect output.
161    let next_stride = strides
162        .iter()
163        .enumerate()
164        .filter_map(|(i, &s)| (i != axis && s != 0).then_some(s))
165        .min()
166        .unwrap_or(0);
167
168    supported_vector_sizes
169        .filter(|&vector_size| axis_shape % vector_size == 0 && next_stride % vector_size == 0)
170        .max()
171        .ok_or(VectorizationError::NoValidVectorization)
172}
173
174/// Find the maximum vector size usable for perpendicular vectorization along the given axis
175/// from the supported vector sizes or return 1 if vectorization is impossible.
176///
177/// This function is designed to never return a vector size above 1 by error,
178/// but doesn't guarantee to always return the actual maximum possible vector size.
179/// That is, it may be overly strict.
180///
181/// Currently, this checks that the stride of the axis is divisible by a candidate vector size
182/// and that the product of all shapes of axes with smaller strides is equal to the stride of the axis.
183/// The second condition ensure that elements within the stride are contiguous.
184pub fn tensor_vector_size_perpendicular(
185    supported_vector_sizes: impl Iterator<Item = VectorSize>,
186    shape: &[usize],
187    strides: &[usize],
188    axis: usize,
189) -> VectorSize {
190    try_tensor_vector_sizes_perpendicular(supported_vector_sizes, shape, strides, axis).unwrap_or(1)
191}
192
193/// Like `tensor_vector_sizes_perpendicular` but does not assume 1 is supported
194pub fn try_tensor_vector_sizes_perpendicular(
195    supported_vector_sizes: impl Iterator<Item = VectorSize>,
196    shape: &[usize],
197    strides: &[usize],
198    axis: usize,
199) -> Result<VectorSize, VectorizationError> {
200    let axis_stride = strides
201        .get(axis)
202        .ok_or(VectorizationError::AxisOutOfBounds)?;
203
204    let prod_shape_axes_smaller_strides = strides
205        .iter()
206        .zip(shape.iter())
207        .filter(|(stride, _)| **stride < *axis_stride)
208        .map(|(_, shape)| shape)
209        .product::<usize>();
210
211    if *axis_stride != prod_shape_axes_smaller_strides {
212        return Err(VectorizationError::StrideMismatch);
213    }
214
215    supported_vector_sizes
216        .filter(|&vector_size| *axis_stride % vector_size == 0)
217        .max()
218        .ok_or(VectorizationError::NoValidVectorization)
219}
220
221/// Runtime arguments to launch a kernel.
222pub type RuntimeArg<T> = <T as LaunchArg>::RuntimeArg;
223pub type ExpandType<T> = <T as crate::prelude::CubeType>::ExpandType;
224
225#[cfg(feature = "export_tests")]
226/// Tests only useful for runtimes.
227pub mod runtime_tests;
228
229#[cfg(test)]
230mod tests {
231    use super::*;
232
233    fn try_parallel(
234        sizes: &[VectorSize],
235        shape: &[usize],
236        strides: &[usize],
237        axis: usize,
238    ) -> Result<VectorSize, VectorizationError> {
239        try_tensor_vector_size_parallel(
240            sizes.iter().copied(),
241            &Shape::from(shape.iter().copied()),
242            &Strides::new(strides),
243            axis,
244        )
245    }
246
247    #[test]
248    fn parallel_contiguous_picks_max_vector_size() {
249        // Contiguous [1, 9, 4], vectorize along last dim (stride 1).
250        // Outer stride 4 is a multiple of 4, so vec_size = 4 is safe.
251        let v = try_parallel(&[1, 2, 4], &[1, 9, 4], &[36, 4, 1], 2).unwrap();
252        assert_eq!(v, 4);
253    }
254
255    #[test]
256    fn parallel_unfold_step_one_rejects_vectorization() {
257        // Unfold view produced by `unfold(1, 4, 1)` on a [1, 12] contiguous tensor:
258        // shape [1, 9, 4], strides [12, 1, 1]. The frame dim has stride 1, so each
259        // step in the frame coord shifts the source offset by 1 - not a multiple
260        // of any vec_size > 1, so vectorized reads would be unaligned and return
261        // the wrong data. Must fall back to vec_size = 1.
262        let v = try_parallel(&[1, 2, 4], &[1, 9, 4], &[12, 1, 1], 2).unwrap();
263        assert_eq!(v, 1);
264    }
265
266    #[test]
267    fn parallel_unfold_step_two_allows_vectorization() {
268        // Same unfold pattern but with step=2: strides [12, 2, 1]. Frame coord
269        // shifts source by 2 (still not a multiple of 4), so vec_size = 4 must
270        // be rejected - but vec_size = 2 is fine.
271        let v = try_parallel(&[1, 2, 4], &[1, 9, 4], &[12, 2, 1], 2).unwrap();
272        assert_eq!(v, 2);
273    }
274
275    #[test]
276    fn parallel_broadcast_dim_ignored() {
277        // Broadcast dim has stride 0; it never shifts the source offset, so
278        // it should not disqualify vectorization.
279        let v = try_parallel(&[1, 2, 4], &[1, 9, 4], &[0, 4, 1], 2).unwrap();
280        assert_eq!(v, 4);
281    }
282
283    #[test]
284    fn parallel_axis_stride_not_one_is_error() {
285        let err = try_parallel(&[1, 2, 4], &[1, 9, 4], &[36, 1, 4], 2).unwrap_err();
286        assert!(matches!(err, VectorizationError::StrideMismatch));
287    }
288}