pub fn encode_f32(
data: &[f32],
dims: &[usize],
cfg: &Config,
) -> (Vec<u8>, EncodeStats)Expand description
Encode an f32 array of shape dims (row-major).
Examples found in repository?
examples/roundtrip.rs (line 23)
9fn main() {
10 let (h, w) = (128, 128);
11 let x: Vec<f32> = (0..h * w)
12 .map(|k| {
13 let (i, j) = ((k / w) as f32, (k % w) as f32);
14 (i * 0.05).sin() * (j * 0.03).cos() + 0.1 * (i * j * 1e-3).sin()
15 })
16 .collect();
17
18 for bound in [
19 ErrorBound::MaxAbs(1e-3),
20 ErrorBound::Rmse(1e-4),
21 ErrorBound::Psnr(60.0),
22 ] {
23 let (bytes, stats) = encode_f32(&x, &[h, w], &Config::new(bound));
24 let Decoded::F32 { data, .. } = decode(&bytes).expect("decode") else {
25 unreachable!("encoded f32")
26 };
27 let max_abs = x
28 .iter()
29 .zip(&data)
30 .map(|(a, b)| (a - b).abs() as f64)
31 .fold(0.0, f64::max);
32 let rmse = (x
33 .iter()
34 .zip(&data)
35 .map(|(a, b)| ((a - b) as f64).powi(2))
36 .sum::<f64>()
37 / x.len() as f64)
38 .sqrt();
39 println!(
40 "{bound:?}: {} bytes ({:.3} bits/value, ratio {:.1}x), max|err| {max_abs:.2e}, rmse {rmse:.2e}",
41 stats.bytes,
42 stats.bits_per_value,
43 32.0 / stats.bits_per_value
44 );
45 if let ErrorBound::MaxAbs(e) = bound {
46 assert!(max_abs <= e);
47 }
48 }
49}More examples
examples/qtt.rs (line 107)
100fn main() {
101 let args: Vec<String> = std::env::args().skip(1).collect();
102 match args.first().map(String::as_str) {
103 Some("encode") if args.len() >= 3 => {
104 let x = read_f32(&args[1]);
105 let (dims, cfg) = parse(&args[3..], &x);
106 let t = Instant::now();
107 let (bytes, st) = encode_f32(&x, &dims, &cfg);
108 let enc = t.elapsed().as_secs_f64();
109 std::fs::write(&args[2], &bytes).unwrap();
110 println!("{}", json(&st, enc, None));
111 }
112 Some("decode") if args.len() == 3 => {
113 let bytes = std::fs::read(&args[1]).unwrap();
114 let t = Instant::now();
115 let Decoded::F32 { data, .. } = decode(&bytes).expect("decode") else {
116 panic!("not an f32 stream")
117 };
118 let dec = t.elapsed().as_secs_f64();
119 let out: Vec<u8> = data.iter().flat_map(|v| v.to_le_bytes()).collect();
120 std::fs::write(&args[2], out).unwrap();
121 println!("{{\"decode_s\":{dec}}}");
122 }
123 Some("bench") if args.len() >= 2 => {
124 let x = read_f32(&args[1]);
125 let (dims, cfg) = parse(&args[2..], &x);
126 let t = Instant::now();
127 let (bytes, st) = encode_f32(&x, &dims, &cfg);
128 let enc = t.elapsed().as_secs_f64();
129 let t = Instant::now();
130 let Decoded::F32 { data, .. } = decode(&bytes).unwrap() else {
131 unreachable!()
132 };
133 let dec = t.elapsed().as_secs_f64();
134 let mx = x
135 .iter()
136 .zip(&data)
137 .map(|(&a, &b)| (a as f64 - b as f64).abs())
138 .fold(0.0, f64::max);
139 assert_eq!(
140 mx, st.max_abs,
141 "decode differs from the encoder's self-check"
142 );
143 println!("{}", json(&st, enc, Some(dec)));
144 }
145 _ => usage(),
146 }
147}