Skip to main content

t4a_desc/
lib.rs

1//! Device-neutral kernel descriptors shared by the tensor4all CPU and GPU
2//! backends (tensor4all/tcontract-rs#209).
3//!
4//! A descriptor says *what* a batched kernel computes over flat buffers,
5//! offsets, extents and leading dimensions, and nothing about *how* or *where*.
6//! It holds no buffer, pointer, device, executor, plan or scalar value. A CPU
7//! route (cpueinsum's `GroupedPlan`, tcontract's fused executor) and a GPU
8//! backend (cuBLAS grouped GEMM, CUTLASS, cuTENSOR) consume the same validated
9//! descriptor without translating it into another vocabulary.
10//!
11//! - [`gemm`]: a grouped-GEMM job list in NVIDIA's grouped-GEMM model
12//!   (`cublasGemmGroupedBatchedEx`, CUTLASS `GemmGrouped`), validated once
13//!   into a [`gemm::GemmBatch`] together with its groups and strided runs.
14//! - A block-sparse tensor descriptor in cuTENSOR's model
15//!   (tensor4all/cpueinsum-rs#81) will be a sibling module; it is not here
16//!   yet.
17//!
18//! Like `t4a-cost`, this crate depends on nothing but `std`, so libraries can
19//! share it without depending on each other.
20
21pub mod gemm;
22
23/// The element type of a descriptor's buffers: the set cuBLAS grouped GEMM
24/// and cuTENSOR's block-sparse contraction share.
25///
26/// The set is closed on purpose, without `#[non_exhaustive]`: a new kind is a
27/// breaking change, so every backend's `match` has to decide whether it serves
28/// it instead of falling through a wildcard arm.
29///
30/// # Examples
31///
32/// ```
33/// use t4a_desc::ScalarKind;
34///
35/// assert_eq!(ScalarKind::C64.size_of(), 16);
36/// assert!(!ScalarKind::F32.is_complex());
37/// ```
38#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
39pub enum ScalarKind {
40    /// `f32` (cuBLAS `CUDA_R_32F`).
41    F32,
42    /// `f64` (cuBLAS `CUDA_R_64F`).
43    F64,
44    /// Complex `f32`, real and imaginary parts interleaved (`CUDA_C_32F`).
45    C32,
46    /// Complex `f64`, real and imaginary parts interleaved (`CUDA_C_64F`).
47    C64,
48}
49
50impl ScalarKind {
51    /// Bytes per element.
52    ///
53    /// # Examples
54    ///
55    /// ```
56    /// use t4a_desc::ScalarKind;
57    ///
58    /// let sizes = [ScalarKind::F32, ScalarKind::F64, ScalarKind::C32, ScalarKind::C64]
59    ///     .map(ScalarKind::size_of);
60    /// assert_eq!(sizes, [4, 8, 8, 16]);
61    /// ```
62    pub const fn size_of(self) -> usize {
63        match self {
64            Self::F32 => 4,
65            Self::F64 | Self::C32 => 8,
66            Self::C64 => 16,
67        }
68    }
69
70    /// Whether the kind is complex. For a real kind, the conjugate-transpose
71    /// operation [`gemm::Trans::C`] means the same as [`gemm::Trans::T`].
72    ///
73    /// # Examples
74    ///
75    /// ```
76    /// use t4a_desc::ScalarKind;
77    ///
78    /// assert!(ScalarKind::C32.is_complex());
79    /// assert!(!ScalarKind::F64.is_complex());
80    /// ```
81    pub const fn is_complex(self) -> bool {
82        matches!(self, Self::C32 | Self::C64)
83    }
84}