Skip to main content

cubecl_runtime/
validation.rs

1use cubecl_common::backtrace::BackTrace;
2use cubecl_ir::DeviceProperties;
3
4use crate::{
5    id::KernelId,
6    server::{CubeDim, LaunchError, ResourceLimitError},
7};
8
9/// Validate the cube dim of a kernel fits within the hardware limits
10pub fn validate_cube_dim(
11    properties: &DeviceProperties,
12    kernel_id: &KernelId,
13) -> Result<(), LaunchError> {
14    let requested = kernel_id.cube_dim;
15    let max: CubeDim = properties.hardware.max_cube_dim.into();
16    if !max.can_contain(requested) {
17        Err(ResourceLimitError::CubeDim {
18            requested: requested.into(),
19            max: max.into(),
20            backtrace: BackTrace::capture(),
21        }
22        .into())
23    } else {
24        Ok(())
25    }
26}
27
28/// Validate the total units of a kernel fits within the hardware limits
29pub fn validate_units(
30    properties: &DeviceProperties,
31    kernel_id: &KernelId,
32) -> Result<(), LaunchError> {
33    let requested = kernel_id.cube_dim.num_elems();
34    let max = properties.hardware.max_units_per_cube;
35    if requested > max {
36        Err(ResourceLimitError::Units {
37            requested,
38            max,
39            backtrace: BackTrace::capture(),
40        }
41        .into())
42    } else {
43        Ok(())
44    }
45}