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 mod errors;
21pub use errors::*;
22
23/// Network utilities.
24#[cfg(feature = "network")]
25pub mod network;
26
27// Re-exported types
28pub use cubecl_common::bytes::*;
29pub use cubecl_common::*;
30pub use half::{bf16, f16};
31
32#[cfg(feature = "cubecl")]
33pub use cubecl::flex32;
34
35#[cfg(feature = "cubecl")]
36mod cube {
37    use cubecl::ir::{ElemType, FloatKind, IntKind, StorageType, UIntKind};
38    use cubecl_common::quant::scheme::QuantScheme;
39
40    use crate::tensor::DType;
41    use crate::tensor::quantization::{QuantStore, QuantValue};
42
43    impl From<DType> for cubecl::ir::ElemType {
44        fn from(dtype: DType) -> Self {
45            match dtype {
46                DType::F64 => ElemType::Float(FloatKind::F64),
47                DType::F32 => ElemType::Float(FloatKind::F32),
48                DType::Flex32 => ElemType::Float(FloatKind::Flex32),
49                DType::F16 => ElemType::Float(FloatKind::F16),
50                DType::BF16 => ElemType::Float(FloatKind::BF16),
51                DType::I64 => ElemType::Int(IntKind::I64),
52                DType::I32 => ElemType::Int(IntKind::I32),
53                DType::I16 => ElemType::Int(IntKind::I16),
54                DType::I8 => ElemType::Int(IntKind::I8),
55                DType::U64 => ElemType::UInt(UIntKind::U64),
56                DType::U32 => ElemType::UInt(UIntKind::U32),
57                DType::U16 => ElemType::UInt(UIntKind::U16),
58                DType::U8 => ElemType::UInt(UIntKind::U8),
59                DType::Bool => ElemType::Bool,
60                DType::QFloat(scheme) => match scheme.store {
61                    QuantStore::Native => match scheme.value {
62                        QuantValue::Q8F | QuantValue::Q8S => Self::Int(IntKind::I8),
63                        QuantValue::E4M3 => Self::Float(FloatKind::E4M3),
64                        QuantValue::E5M2 => Self::Float(FloatKind::E5M2),
65                        QuantValue::Q4F
66                        | QuantValue::Q4S
67                        | QuantValue::Q2F
68                        | QuantValue::Q2S
69                        | QuantValue::E2M1 => {
70                            panic!("Can't store native sub-byte values")
71                        }
72                    },
73                    QuantStore::U32 => Self::UInt(UIntKind::U32),
74                },
75            }
76        }
77    }
78
79    impl From<DType> for cubecl::ir::StorageType {
80        fn from(dtype: DType) -> cubecl::ir::StorageType {
81            match dtype {
82                DType::QFloat(QuantScheme {
83                    store: QuantStore::Native,
84                    value: QuantValue::E2M1,
85                    ..
86                }) => StorageType::Packed(ElemType::Float(FloatKind::E2M1), 2),
87                _ => {
88                    let elem: ElemType = dtype.into();
89                    elem.into()
90                }
91            }
92        }
93    }
94}