kodamaembed_cpu/
precision.rs1#[cfg(target_arch = "x86_64")]
2use crate::int8;
3use crate::{EncoderError, Kernel};
4use kodamaembed_model::{Output, Pooling, Positions, Shape};
5
6#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
9pub enum CpuPrecision {
10 #[default]
12 Fp32,
13 E5CenteredInt8,
16}
17
18impl CpuPrecision {
19 pub(crate) fn validate(
20 self,
21 kernel: Kernel,
22 shape: Shape,
23 output: Output,
24 ) -> Result<(), EncoderError> {
25 if self == Self::Fp32 {
26 return Ok(());
27 }
28 if output != Output::Embedding(Pooling::Mean)
29 || shape.layers != 24
30 || shape.hidden != 1024
31 || shape.heads != 16
32 || shape.intermediate != 4096
33 || shape.vocab != 30_522
34 || shape.max_positions != 512
35 || shape.type_vocab != 2
36 || shape.eps.to_bits() != 1e-12_f32.to_bits()
37 || shape.positions != Positions::Sequential
38 {
39 return Err(EncoderError::Unsupported(
40 "the E5 INT8 preset requires the evaluated E5-large-v1 shape and mean-pooled output"
41 .into(),
42 ));
43 }
44 #[cfg(target_arch = "x86_64")]
45 let supported_kernel = kernel == Kernel::Avx2 && int8::is_supported();
46 #[cfg(not(target_arch = "x86_64"))]
47 let supported_kernel = false;
48 if !supported_kernel || !kernel.is_supported() {
49 return Err(EncoderError::Unsupported(format!(
50 "{self:?} requires AVX2/FMA, AVX-VNNI and F16C with Kernel::Avx2"
51 )));
52 }
53 Ok(())
54 }
55
56 pub(crate) fn centered(self) -> bool {
57 self == Self::E5CenteredInt8
58 }
59
60 pub(crate) fn integer_attention(self) -> bool {
61 self == Self::E5CenteredInt8
62 }
63
64 pub(crate) fn identity(self) -> &'static str {
65 match self {
66 Self::Fp32 => "cpu-fp32-v1",
67 Self::E5CenteredInt8 => {
68 "cpu-e5-centered-int8-v1:half-rne:h128:w8-gptq-damp10:a8-rne:sha256-signs-k:static3-b128-chain-h-shared-exact-f64-deferred-ln:calibration-fp32-text:attention-qk8-w14-v8-sinks-static-keys:half-residual:avx2-f16c-avxvnni:row-major6x16:fused-epilogue-v1"
69 }
70 }
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use kodamaembed_model::{Head, Positions};
78 fn e5() -> Shape {
79 Shape {
80 layers: 24,
81 hidden: 1024,
82 heads: 16,
83 intermediate: 4096,
84 vocab: 30522,
85 max_positions: 512,
86 type_vocab: 2,
87 eps: 1e-12,
88 positions: Positions::Sequential,
89 }
90 }
91 #[test]
92 fn e5_precision_is_distinct_and_scope_is_strict() {
93 let p = CpuPrecision::E5CenteredInt8;
94 assert_ne!(p.identity(), CpuPrecision::Fp32.identity());
95 assert!(
96 p.validate(Kernel::Portable, e5(), Output::Embedding(Pooling::Mean))
97 .is_err()
98 );
99 assert!(
100 p.validate(Kernel::detect(), e5(), Output::Relevance(Head::Pooler))
101 .is_err()
102 );
103 let mut other = e5();
104 other.layers = 6;
105 assert!(
106 p.validate(Kernel::detect(), other, Output::Embedding(Pooling::Mean))
107 .is_err()
108 );
109 let mut other = e5();
110 other.hidden = 384;
111 assert!(
112 p.validate(Kernel::detect(), other, Output::Embedding(Pooling::Mean))
113 .is_err()
114 );
115 #[cfg(target_arch = "x86_64")]
116 if int8::is_supported() && Kernel::Avx2.is_supported() {
117 eprintln!(
118 "native CPU: kernel={:?}, int8_supported={}, workers={}",
119 Kernel::detect(),
120 int8::is_supported(),
121 crate::workers::POOL.current_num_threads()
122 );
123 assert!(
124 p.validate(Kernel::Avx2, e5(), Output::Embedding(Pooling::Mean))
125 .is_ok()
126 );
127 }
128 }
129
130 #[test]
131 fn every_preset_has_its_own_identity_and_the_centered_ones_center() {
132 use CpuPrecision::*;
133 assert_ne!(Fp32.identity(), E5CenteredInt8.identity());
134 for (precision, centered) in [(Fp32, false), (E5CenteredInt8, true)] {
135 assert_eq!(precision.centered(), centered, "{precision:?}");
136 assert_eq!(precision.integer_attention(), centered, "{precision:?}");
137 }
138 }
139
140 #[test]
141 fn default_stays_fp32() {
142 assert_eq!(CpuPrecision::default(), CpuPrecision::Fp32);
143 assert_eq!(CpuPrecision::Fp32.identity(), "cpu-fp32-v1");
144 }
145}