Skip to main content

burn_std/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![warn(missing_docs)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5//! # Burn Standard Library
6//!
7//! This library contains core types and utilities shared across Burn, including shapes, indexing,
8//! and data types.
9
10extern crate alloc;
11
12/// Id module contains types for unique identifiers.
13pub mod id;
14
15/// Tensor utilities.
16pub mod tensor;
17pub use tensor::*;
18
19/// Common Errors.
20pub use cubecl_zspace::errors::{self, *};
21
22/// Network utilities.
23#[cfg(feature = "network")]
24pub mod network;
25
26// Re-exported types
27pub use cubecl_common::bytes::*;
28pub use cubecl_common::*;
29pub use half::{bf16, f16};
30
31#[cfg(feature = "cubecl")]
32pub use cubecl::flex32;
33
34#[cfg(feature = "cubecl")]
35mod cube {
36    use cubecl::ir::{ElemType, FloatKind, IntKind, StorageType, UIntKind};
37    use cubecl_common::quant::scheme::QuantScheme;
38
39    use crate::tensor::DType;
40    use crate::tensor::quantization::{QuantStore, QuantValue};
41
42    impl From<DType> for cubecl::ir::ElemType {
43        fn from(dtype: DType) -> Self {
44            match dtype {
45                DType::F64 => ElemType::Float(FloatKind::F64),
46                DType::F32 => ElemType::Float(FloatKind::F32),
47                DType::Flex32 => ElemType::Float(FloatKind::Flex32),
48                DType::F16 => ElemType::Float(FloatKind::F16),
49                DType::BF16 => ElemType::Float(FloatKind::BF16),
50                DType::I64 => ElemType::Int(IntKind::I64),
51                DType::I32 => ElemType::Int(IntKind::I32),
52                DType::I16 => ElemType::Int(IntKind::I16),
53                DType::I8 => ElemType::Int(IntKind::I8),
54                DType::U64 => ElemType::UInt(UIntKind::U64),
55                DType::U32 => ElemType::UInt(UIntKind::U32),
56                DType::U16 => ElemType::UInt(UIntKind::U16),
57                DType::U8 => ElemType::UInt(UIntKind::U8),
58                DType::Bool => ElemType::Bool,
59                DType::QFloat(scheme) => match scheme.store {
60                    QuantStore::Native => match scheme.value {
61                        QuantValue::Q8F | QuantValue::Q8S => Self::Int(IntKind::I8),
62                        QuantValue::E4M3 => Self::Float(FloatKind::E4M3),
63                        QuantValue::E5M2 => Self::Float(FloatKind::E5M2),
64                        QuantValue::Q4F
65                        | QuantValue::Q4S
66                        | QuantValue::Q2F
67                        | QuantValue::Q2S
68                        | QuantValue::E2M1 => {
69                            panic!("Can't store native sub-byte values")
70                        }
71                    },
72                    QuantStore::PackedU32(_) => Self::UInt(UIntKind::U32),
73                    QuantStore::PackedNative(_) => match scheme.value {
74                        QuantValue::E2M1 => panic!("Can't store native sub-byte values"),
75                        other => panic!("{other:?} doesn't support native packing"),
76                    },
77                },
78            }
79        }
80    }
81
82    impl From<DType> for cubecl::ir::StorageType {
83        fn from(dtype: DType) -> cubecl::ir::StorageType {
84            match dtype {
85                DType::QFloat(QuantScheme {
86                    store: QuantStore::PackedNative(_),
87                    value: QuantValue::E2M1,
88                    ..
89                }) => StorageType::Packed(ElemType::Float(FloatKind::E2M1), 2),
90                _ => {
91                    let elem: ElemType = dtype.into();
92                    elem.into()
93                }
94            }
95        }
96    }
97}