1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! Runtime view of the embeddings section dtype, carved out of
//! `mmap_file.rs`, which opens and validates the file.
use urna_format::UrnaError;
use crate::error::RuntimeError;
/// Runtime view of the embeddings section dtype.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DType {
Float32,
Float16,
Int8,
Int4,
}
impl DType {
pub(crate) fn from_str(s: &str) -> Result<Self, RuntimeError> {
match s {
"float32" => Ok(Self::Float32),
"float16" => Ok(Self::Float16),
"int8" => Ok(Self::Int8),
"int4" => Ok(Self::Int4),
other => Err(RuntimeError::Format(UrnaError::UnsupportedDType(
other.into(),
))),
}
}
/// Nominal on-disk bytes per stored embedding value. int4 packs two
/// codes per byte (rounds to 0 here); the exact section size, with the
/// f16 group scales, is `expected_embeddings_size`.
pub fn bytes_per_value(self) -> usize {
match self {
Self::Float32 => 4,
Self::Float16 => 2,
Self::Int8 => 1,
Self::Int4 => 0,
}
}
pub fn name(self) -> &'static str {
match self {
Self::Float32 => "float32",
Self::Float16 => "float16",
Self::Int8 => "int8",
Self::Int4 => "int4",
}
}
}