Skip to main content

hyprstream_inference/runtime/
channel_fp8.rs

1//! Compressed-tensors FP8_DYNAMIC admission and CPU floating emulation.
2//!
3//! Scales are multipliers on stored E4M3FN values, never inverse scales or
4//! 128x128 blocks. This profile intentionally does not use GPU scaled-mm.
5use anyhow::{bail, ensure, Context, Result};
6use serde_json::Value;
7use std::{
8    collections::HashMap,
9    path::{Path, PathBuf},
10};
11use tch::{Device, Kind, Tensor};
12
13/// Recoverable FP8 execution failure exposed by the native model forward path.
14#[derive(Debug)]
15pub struct Fp8ExecutionError(pub(crate) String);
16impl std::fmt::Display for Fp8ExecutionError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        write!(f, "FP8 execution failed: {}", self.0)
19    }
20}
21impl std::error::Error for Fp8ExecutionError {}
22
23#[derive(Debug)]
24pub(crate) struct ChannelFp8Profile {
25    ignore: Vec<String>,
26}
27
28impl ChannelFp8Profile {
29    pub(crate) fn read(path: &Path) -> Result<Option<Self>> {
30        let path = path.join("config.json");
31        if !path.exists() {
32            return Ok(None);
33        }
34        let config: Value = serde_json::from_slice(&std::fs::read(path)?)?;
35        Self::parse(&config)
36    }
37
38    pub(crate) fn parse(config: &Value) -> Result<Option<Self>> {
39        let Some(q) = config.get("quantization_config").filter(|v| !v.is_null()) else {
40            return Ok(None);
41        };
42        if q["quant_method"] != "compressed-tensors" {
43            return Ok(None);
44        }
45        ensure!(
46            q["version"] == "0.19.0",
47            "unsupported compressed-tensors producer version"
48        );
49        ensure!(
50            q["format"] == "float-quantized" && q["quantization_status"] == "compressed",
51            "unsupported compressed-tensors format/status"
52        );
53        ensure!(
54            q["kv_cache_scheme"].is_null(),
55            "compressed-tensors KV quantization unsupported"
56        );
57        for field in ["sparsity_config", "transform_config"] {
58            ensure!(
59                q[field].is_null() || q[field].as_object().is_some_and(serde_json::Map::is_empty),
60                "unsupported compressed-tensors {field}"
61            );
62        }
63        let groups = q["config_groups"]
64            .as_object()
65            .context("missing compressed-tensors config_groups")?;
66        ensure!(
67            groups.len() == 1,
68            "exactly one FP8_DYNAMIC group is supported"
69        );
70        let group = groups.values().next().context("empty config_groups")?;
71        ensure!(
72            group.as_object().is_some_and(|g| g.keys().all(|k| matches!(
73                k.as_str(),
74                "format" | "targets" | "weights" | "input_activations" | "output_activations"
75            ))),
76            "unsupported compressed-tensors group field"
77        );
78        ensure!(
79            group["format"] == "float-quantized"
80                && group["targets"] == serde_json::json!(["Linear"]),
81            "unsupported compressed-tensors targets/format"
82        );
83        ensure!(
84            group["output_activations"].is_null(),
85            "output activation quantization unsupported"
86        );
87        for (field, strategy, dynamic) in [
88            ("weights", "channel", false),
89            ("input_activations", "token", true),
90        ] {
91            let a = &group[field];
92            ensure!(
93                a["num_bits"] == 8
94                    && a["type"] == "float"
95                    && a["strategy"] == strategy
96                    && a["symmetric"] == true
97                    && a["dynamic"] == dynamic,
98                "unsupported compressed-tensors {field} scheme"
99            );
100            for unsupported in [
101                "group_size",
102                "block_structure",
103                "actorder",
104                "scale_dtype",
105                "zp_dtype",
106            ] {
107                ensure!(
108                    a[unsupported].is_null(),
109                    "unsupported {field}.{unsupported}"
110                );
111            }
112        }
113        let ignore = q["ignore"]
114            .as_array()
115            .context("missing ignore list")?
116            .iter()
117            .map(|v| {
118                let s = v.as_str().context("ignore entry must be string")?;
119                // Explicit paths only: accepting an unknown regex would silently change admission.
120                ensure!(
121                    !s.starts_with("re:") && !s.contains('*'),
122                    "regex/glob ignore unsupported"
123                );
124                Ok(s.to_owned())
125            })
126            .collect::<Result<Vec<_>>>()?;
127        Ok(Some(Self { ignore }))
128    }
129
130    /// Bound header reads and reject missing/ambiguous scale geometry before
131    /// allocating tensor payloads, even when companions live in another shard.
132    #[cfg(test)]
133    fn preflight(&self, shards: &[PathBuf]) -> Result<()> {
134        let mut resolved = shards
135            .iter()
136            .map(|path| {
137                Ok((
138                    path.clone(),
139                    super::model_factory::ResolvedWeight::File(std::fs::File::open(path)?),
140                ))
141            })
142            .collect::<Result<Vec<_>>>()?;
143        self.preflight_resolved(&mut resolved)
144    }
145
146    pub(crate) fn preflight_resolved(
147        &self,
148        shards: &mut [(PathBuf, super::model_factory::ResolvedWeight)],
149    ) -> Result<()> {
150        let mut entries = HashMap::new();
151        for (_, resolved) in shards {
152            let object: serde_json::Map<String, Value> = resolved.with_reader(|reader| {
153                let mut prefix = [0u8; 8];
154                reader.read_exact(&mut prefix)?;
155                let len = u64::from_le_bytes(prefix);
156                ensure!(
157                    len > 0 && len <= 16 * 1024 * 1024,
158                    "invalid/oversized FP8 safetensors header"
159                );
160                let mut header = vec![0u8; len as usize];
161                reader.read_exact(&mut header)?;
162                Ok(serde_json::from_slice(&header)?)
163            })?;
164            for (name, tensor) in object {
165                if name == "__metadata__" {
166                    continue;
167                }
168                ensure!(
169                    entries.insert(name.clone(), tensor).is_none(),
170                    "duplicate tensor across shards: {name}"
171                );
172            }
173        }
174        let mut quantized = 0;
175        for (name, tensor) in &entries {
176            ensure!(
177                !name.ends_with("_zero_point")
178                    && !name.ends_with("_global_scale")
179                    && !name.ends_with(".input_scale"),
180                "unsupported FP8 quantization companion {name}"
181            );
182            if name.ends_with(".weight_scale") {
183                ensure!(
184                    entries.contains_key(name.trim_end_matches("_scale")),
185                    "orphan FP8 scale {name}"
186                );
187            }
188            let dtype = tensor["dtype"]
189                .as_str()
190                .context("tensor header lacks dtype")?;
191            if !dtype.starts_with("F8_") {
192                continue;
193            }
194            ensure!(
195                dtype == "F8_E4M3",
196                "FP8_DYNAMIC requires F8_E4M3, got {dtype} for {name}"
197            );
198            ensure!(name.ends_with(".weight"), "unexpected FP8 tensor {name}");
199            let module = name.trim_end_matches(".weight");
200            ensure!(!self.ignored(module), "ignored module is FP8: {name}");
201            let shape = tensor["shape"].as_array().context("missing FP8 shape")?;
202            ensure!(
203                shape.len() == 2 && shape.iter().all(|d| d.as_u64().is_some_and(|v| v > 0)),
204                "invalid FP8 matrix shape {name}"
205            );
206            let scale = entries
207                .get(&format!("{name}_scale"))
208                .context(format!("missing channel scale {name}"))?;
209            ensure!(
210                scale["shape"] == serde_json::json!([shape[0], 1]),
211                "wrong channel-scale axis/shape for {name}"
212            );
213            ensure!(
214                matches!(scale["dtype"].as_str(), Some("F32" | "BF16" | "F16")),
215                "invalid channel scale dtype {name}"
216            );
217            ensure!(
218                !entries.contains_key(&format!("{name}_scale_inv")),
219                "ambiguous channel/block scale {name}"
220            );
221            quantized += 1;
222        }
223        ensure!(quantized > 0, "FP8_DYNAMIC checkpoint has no FP8 matrices");
224        Ok(())
225    }
226
227    fn ignored(&self, module: &str) -> bool {
228        self.ignore
229            .iter()
230            .any(|s| module == s || module.starts_with(&format!("{s}.")))
231    }
232
233    pub(crate) fn validate(&self, weights: &HashMap<String, Tensor>) -> Result<()> {
234        let mut count = 0;
235        for (name, weight) in weights {
236            ensure!(
237                !name.ends_with("_zero_point")
238                    && !name.ends_with("_global_scale")
239                    && !name.ends_with(".input_scale"),
240                "unsupported FP8 quantization companion {name}"
241            );
242            if name.ends_with(".weight_scale") {
243                let key = name.strip_suffix("_scale").context("invalid scale name")?;
244                ensure!(weights.contains_key(key), "orphan FP8 scale {name}");
245            }
246            if !name.ends_with(".weight") {
247                continue;
248            }
249            let module = name.trim_end_matches(".weight");
250            let ignored = self.ignored(module);
251            let fp8 = matches!(
252                weight.kind(),
253                Kind::Float8e4m3fn | Kind::Float8e5m2 | Kind::Float8e4m3fnuz | Kind::Float8e5m2fnuz
254            );
255            let scale = weights.get(&format!("{name}_scale"));
256            if fp8 {
257                ensure!(!ignored, "ignored module has FP8 weights: {name}");
258                ensure!(
259                    weight.kind() == Kind::Float8e4m3fn,
260                    "FP8_DYNAMIC requires E4M3FN: {name}"
261                );
262                validate_channel_scale(
263                    weight,
264                    scale.context(format!("missing channel scale for {name}"))?,
265                )?;
266                ensure!(
267                    !weights.contains_key(&format!("{name}_scale_inv")),
268                    "ambiguous FP8 scales: {name}"
269                );
270                count += 1;
271            } else {
272                ensure!(scale.is_none(), "channel scale on floating weight {name}");
273                // Only known text Linear sites are enforced. Embeddings, RMSNorm,
274                // convolution kernels, vision, and the preserved head are not Linear targets.
275                let text_linear = weight.dim() == 2
276                    && module.contains(".layers.")
277                    && (module.contains(".mlp.")
278                        || module.contains(".self_attn.")
279                        || module.contains(".linear_attn.in_proj")
280                        || module.ends_with(".linear_attn.out_proj"));
281                ensure!(
282                    !text_linear || ignored,
283                    "targeted Linear is not FP8: {name}"
284                );
285            }
286        }
287        ensure!(
288            count > 0,
289            "FP8_DYNAMIC checkpoint contains no quantized weights"
290        );
291        Ok(())
292    }
293}
294
295pub(crate) fn validate_channel_scale(weight: &Tensor, scale: &Tensor) -> Result<()> {
296    ensure!(
297        weight.kind() == Kind::Float8e4m3fn,
298        "channel FP8 requires E4M3FN"
299    );
300    let shape = weight.size();
301    ensure!(
302        shape.len() == 2 && shape.iter().all(|d| *d > 0),
303        "channel FP8 weight must be nonempty [out,in]"
304    );
305    ensure!(
306        scale.size() == [shape[0], 1],
307        "channel scale must be [out,1], got {:?}",
308        scale.size()
309    );
310    ensure!(
311        matches!(scale.kind(), Kind::Float | Kind::BFloat16 | Kind::Half),
312        "channel scale must be floating"
313    );
314    ensure!(
315        scale.isfinite().all().int64_value(&[]) != 0 && scale.gt(0).all().int64_value(&[]) != 0,
316        "channel scales must be finite and positive"
317    );
318    Ok(())
319}
320
321/// Dynamic symmetric token quantize/dequantize in the compute dtype.
322///
323/// Producer pin: compressed-tensors 0.19.0, commit
324/// `525a7a7b84ebccdbc0db07956338b6f8c851d58f`:
325/// `quantization/utils/helpers.py::calculate_qparams` divides amax by448 then
326/// replaces **only zero** scales with dtype epsilon (lines80-139). No small-amax
327/// floor is applied. `lifecycle/forward_helpers.py::_quantize_dequantize`
328/// (lines168-204) divides, clamps/rounds to E4M3FN, then multiplies by the scale.
329/// Source: <https://github.com/vllm-project/compressed-tensors/blob/525a7a7b84ebccdbc0db07956338b6f8c851d58f/src/compressed_tensors/quantization/utils/helpers.py>.
330/// CPU FP32 is an explicit compute profile, not a claim of BF16 rounding parity. ATen's
331/// E4M3FN cast supplies round-to-nearest-even, including subnormals. The producer
332/// replaces *zero* scales with dtype epsilon; it does not clamp small scales.
333pub(crate) fn dynamic_token_qdq(input: &Tensor) -> Result<Tensor> {
334    ensure!(
335        input.dim() >= 2 && input.size().last().is_some_and(|d| *d > 0),
336        "FP8 input must contain nonempty token rows"
337    );
338    ensure!(
339        matches!(input.kind(), Kind::Float | Kind::BFloat16 | Kind::Half),
340        "unsupported FP8 activation compute dtype"
341    );
342    ensure!(
343        input.isfinite().all().int64_value(&[]) != 0,
344        "nonfinite FP8 activations"
345    );
346    let eps = match input.kind() {
347        Kind::BFloat16 => 0.0078125,
348        Kind::Half => 0.0009765625,
349        _ => f32::EPSILON as f64,
350    };
351    let scale = input.abs().amax(-1, true) / 448.0;
352    let scale = scale.where_self(&scale.ne(0), &scale.full_like(eps));
353    let q = (input / &scale)
354        .clamp(-448.0, 448.0)
355        .to_kind(Kind::Float8e4m3fn);
356    Ok(q.to_kind(input.kind()) * scale)
357}
358
359/// FP8 bytes stay resident. Each decoded scratch tile is at most K*256 floats;
360/// output chunks are concatenated once, and no decoded weight is cached.
361pub(crate) fn channel_matmul(input: &Tensor, weight: &Tensor, scale: &Tensor) -> Result<Tensor> {
362    ensure!(
363        weight.device() == Device::Cpu && input.device() == Device::Cpu,
364        "channel FP8 execution is CPU-only until CPU acceptance"
365    );
366    let x = dynamic_token_qdq(input)?;
367    let n = weight.size()[1];
368    ensure!(
369        input.size().last() == weight.size().first(),
370        "channel FP8 input width mismatch"
371    );
372    let mut outputs = Vec::new();
373    for offset in (0..n).step_by(256) {
374        let width = (n - offset).min(256);
375        let decoded = weight.narrow(1, offset, width).to_kind(input.kind())
376            * scale.narrow(1, offset, width).to_kind(input.kind());
377        ensure!(
378            decoded.isfinite().all().int64_value(&[]) != 0,
379            "nonfinite decoded FP8 weight tile"
380        );
381        let output = x.matmul(&decoded);
382        ensure!(
383            output.isfinite().all().int64_value(&[]) != 0,
384            "nonfinite FP8 matmul output"
385        );
386        outputs.push(output);
387    }
388    if outputs.is_empty() {
389        bail!("empty FP8 projection");
390    }
391    Ok(Tensor::cat(&outputs, -1))
392}
393
394#[cfg(test)]
395#[allow(clippy::unwrap_used, clippy::expect_used)]
396mod tests {
397    use super::*;
398    use crate::runtime::architectures::llama::LinearProjection;
399
400    fn profile() -> Value {
401        serde_json::json!({"quantization_config":{
402            "quant_method":"compressed-tensors", "version":"0.19.0",
403            "format":"float-quantized", "quantization_status":"compressed", "ignore":["lm_head"],
404            "config_groups":{"g":{"format":"float-quantized", "targets":["Linear"],
405                "weights":{"num_bits":8,"type":"float","strategy":"channel","symmetric":true,"dynamic":false},
406                "input_activations":{"num_bits":8,"type":"float","strategy":"token","symmetric":true,"dynamic":true}}}
407        }})
408    }
409    // Independent scalar E4M3FN oracle: no libtorch cast/scaling/matmul.
410    fn decode(code: u8) -> f32 {
411        let sign = if code & 128 == 0 { 1.0 } else { -1.0 };
412        let exponent = (code >> 3) & 15;
413        let mantissa = code & 7;
414        if exponent == 15 && mantissa == 7 {
415            return f32::NAN;
416        }
417        sign * if exponent == 0 {
418            mantissa as f32 / 512.0
419        } else {
420            (1.0 + mantissa as f32 / 8.0) * 2f32.powi(exponent as i32 - 7)
421        }
422    }
423    fn round_fp8(value: f32) -> f32 {
424        let magnitude = value.abs().min(448.0);
425        let mut code = 0;
426        let mut distance = f32::INFINITY;
427        for candidate in 0u8..=126 {
428            let d = (decode(candidate) - magnitude).abs();
429            if d < distance || (d == distance && candidate & 1 == 0) {
430                code = candidate;
431                distance = d;
432            }
433        }
434        decode(code).copysign(value)
435    }
436    fn scalar_qdq(row: &[f32]) -> Vec<f32> {
437        let max = row.iter().fold(0f32, |a, b| a.max(b.abs()));
438        let mut scale = max / 448.0;
439        if scale == 0.0 {
440            scale = f32::EPSILON;
441        }
442        row.iter().map(|v| round_fp8(v / scale) * scale).collect()
443    }
444    fn raw(bytes: &[u8], shape: &[i64]) -> Tensor {
445        unsafe { Tensor::from_blob(bytes.as_ptr(), shape, &[], Kind::Float8e4m3fn, Device::Cpu) }
446            .copy()
447    }
448    fn values(t: &Tensor) -> Vec<f32> {
449        Vec::<f32>::try_from(&t.to_kind(Kind::Float).reshape([-1])).unwrap()
450    }
451    fn close(actual: &[f32], expected: &[f32], tol: f32) {
452        assert_eq!(actual.len(), expected.len());
453        for (a, b) in actual.iter().zip(expected) {
454            assert!((a - b).abs() <= tol, "{a} != {b}");
455        }
456    }
457
458    #[test]
459    fn fp8_byte_patterns_match_independent_decoder() {
460        let bytes: Vec<u8> = (0..=255).collect();
461        let actual = values(&raw(&bytes, &[256]));
462        for (byte, actual) in bytes.into_iter().zip(actual) {
463            let expected = decode(byte);
464            if expected.is_nan() {
465                assert!(actual.is_nan());
466            } else {
467                assert_eq!(actual, expected, "byte {byte}");
468            }
469        }
470    }
471    #[test]
472    fn dynamic_qdq_matches_scalar_edges_and_noncontiguous_tokens() {
473        let rows = [
474            [0.0, 0.0, 0.0, 0.0, 0.0],
475            [448.0, 1.0625, 1.1875, -448.0, 0.0009765625],
476            [1e-30, -1e-30, 0.0, 1e-32, -1e-32],
477            [1e30, 1.0, -1e29, 0.0, 1e-5],
478        ];
479        let flat: Vec<f32> = rows.into_iter().flatten().collect();
480        let contiguous = Tensor::from_slice(&flat).reshape([4, 5]);
481        let noncontiguous = contiguous.transpose(0, 1).contiguous().transpose(0, 1);
482        assert!(!noncontiguous.is_contiguous());
483        let expected: Vec<f32> = rows.iter().flat_map(|r| scalar_qdq(r)).collect();
484        let actual = values(&dynamic_token_qdq(&noncontiguous).unwrap());
485        for (a, b) in actual.into_iter().zip(expected) {
486            assert!((a - b).abs() <= b.abs() * 1e-6 + 1e-35, "{a} != {b}");
487        }
488        for nonfinite in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
489            assert!(
490                dynamic_token_qdq(&Tensor::from_slice(&[nonfinite, 1.0]).reshape([1, 2])).is_err()
491            );
492        }
493    }
494    #[test]
495    fn activation_qdq_respects_compute_dtype_zero_policy_and_round_even() {
496        for kind in [Kind::Float, Kind::BFloat16, Kind::Half] {
497            let x = Tensor::from_slice(&[448f32, 1.0625, 1.1875, 0.0009765625, 0.0])
498                .reshape([1, 5])
499                .to_kind(kind);
500            let y = dynamic_token_qdq(&x).unwrap();
501            assert_eq!(y.kind(), kind);
502            close(&values(&y), &[448.0, 1.0, 1.25, 0.0, 0.0], 0.0);
503            let zeros = Tensor::zeros([2, 3], (kind, Device::Cpu));
504            close(&values(&dynamic_token_qdq(&zeros).unwrap()), &[0.0; 6], 0.0);
505        }
506        let subnormal =
507            Tensor::from_slice(&[f32::from_bits(1), -f32::from_bits(1)]).reshape([1, 2]);
508        close(
509            &values(&dynamic_token_qdq(&subnormal).unwrap()),
510            &[0.0, 0.0],
511            0.0,
512        );
513    }
514
515    #[test]
516    fn channel_projection_partial_tiles_scale_axis_and_qdq_match_scalar_math() {
517        let n = 259usize; // two tiles, with a partial tail
518        let k = 3usize;
519        let bytes: Vec<u8> = (0..n * k)
520            .map(|i| [0x38, 0xb8, 0x40, 0x30, 0x3c][i % 5])
521            .collect();
522        let scales: Vec<f32> = (0..n).map(|i| (i % 7 + 1) as f32 / 16.0).collect();
523        let input = [448f32, 1.0625, -7.0, -14.0, 0.03125, 2.0];
524        let mut weights = HashMap::from([
525            ("p.weight".to_owned(), raw(&bytes, &[n as i64, k as i64])),
526            (
527                "p.weight_scale".to_owned(),
528                Tensor::from_slice(&scales).reshape([n as i64, 1]),
529            ),
530        ]);
531        let projection = LinearProjection::take(&mut weights, "p.weight").unwrap();
532        assert_eq!(projection.weight.kind(), Kind::Float8e4m3fn);
533        assert!(projection.scale.is_none());
534        let result = values(&projection.apply(&Tensor::from_slice(&input).reshape([2, k as i64])));
535        let mut expected = Vec::new();
536        for row in input.chunks(k) {
537            let x = scalar_qdq(row);
538            for out in 0..n {
539                expected.push(
540                    (0..k)
541                        .map(|col| x[col] * decode(bytes[out * k + col]) * scales[out])
542                        .sum(),
543                );
544            }
545        }
546        close(&result, &expected, 2e-4);
547        // The first row includes a tie that activation QDQ changes.
548        assert_ne!(scalar_qdq(&input[..k]), input[..k]);
549    }
550    #[test]
551    fn channel_scale_admission_rejects_axis_nonfinite_zero_and_wrong_format() {
552        let w = raw(&[0x38; 6], &[2, 3]);
553        for s in [
554            Tensor::ones([1, 2], (Kind::Float, Device::Cpu)),
555            Tensor::ones([2], (Kind::Float, Device::Cpu)),
556            Tensor::zeros([2, 1], (Kind::Float, Device::Cpu)),
557            Tensor::from_slice(&[f32::NAN, 1.0]).reshape([2, 1]),
558            Tensor::from_slice(&[-1f32, 1.0]).reshape([2, 1]),
559        ] {
560            assert!(validate_channel_scale(&w, &s).is_err());
561        }
562        assert!(validate_channel_scale(
563            &w.to_kind(Kind::Float8e4m3fnuz),
564            &Tensor::ones([2, 1], (Kind::Float, Device::Cpu))
565        )
566        .is_err());
567        let mut missing = HashMap::from([("p.weight".to_owned(), w)]);
568        assert!(LinearProjection::take(&mut missing, "p.weight").is_err());
569    }
570    #[test]
571    fn checked_projection_returns_typed_execution_error_without_unwind() {
572        let mut weights = HashMap::from([
573            ("p.weight".to_owned(), raw(&[0x38; 6], &[2, 3])),
574            (
575                "p.weight_scale".to_owned(),
576                Tensor::ones([2, 1], (Kind::Float, Device::Cpu)),
577            ),
578        ]);
579        let p = LinearProjection::take(&mut weights, "p.weight").unwrap();
580        let nonfinite = Tensor::from_slice(&[1f32, f32::NAN, 2.0]).reshape([1, 3]);
581        let error = p.try_apply(&nonfinite).unwrap_err();
582        assert!(error.downcast_ref::<Fp8ExecutionError>().is_some());
583        let wrong_width = Tensor::ones([1, 2], (Kind::Float, Device::Cpu));
584        assert!(p
585            .try_apply(&wrong_width)
586            .unwrap_err()
587            .downcast_ref::<Fp8ExecutionError>()
588            .is_some());
589        // A rejected request does not poison or mutate the projection.
590        assert!(p
591            .try_apply(&Tensor::ones([1, 3], (Kind::Float, Device::Cpu)))
592            .is_ok());
593    }
594
595    #[test]
596    fn strict_profile_rejects_unsupported_schemes_and_missing_companions() {
597        for (path, value) in [
598            ("version", Value::from("unknown")),
599            ("quantization_status", Value::from("frozen")),
600        ] {
601            let mut c = profile();
602            c["quantization_config"][path] = value;
603            assert!(ChannelFp8Profile::parse(&c).is_err());
604        }
605        for (path, value) in [
606            ("strategy", Value::from("block")),
607            ("dynamic", Value::from(true)),
608            ("scale_dtype", Value::from("float8_e8m0fnu")),
609        ] {
610            let mut c = profile();
611            c["quantization_config"]["config_groups"]["g"]["weights"][path] = value;
612            assert!(ChannelFp8Profile::parse(&c).is_err());
613        }
614        let p = ChannelFp8Profile::parse(&profile()).unwrap().unwrap();
615        let weights = HashMap::from([("p.weight".to_owned(), raw(&[0x38; 6], &[2, 3]))]);
616        assert!(p.validate(&weights).is_err());
617    }
618    #[test]
619    fn header_preflight_checks_cross_shard_scale_before_payload_allocation() {
620        let dir = tempfile::tempdir().unwrap();
621        let p = ChannelFp8Profile::parse(&profile()).unwrap().unwrap();
622        let write = |filename: &str, entry: Value| {
623            let header = serde_json::to_vec(&entry).unwrap();
624            let mut bytes = (header.len() as u64).to_le_bytes().to_vec();
625            bytes.extend(header);
626            let path = dir.path().join(filename);
627            std::fs::write(&path, bytes).unwrap();
628            path
629        };
630        let weight = write(
631            "a.safetensors",
632            serde_json::json!({"p.weight":{"dtype":"F8_E4M3","shape":[2,3],"data_offsets":[0,6]}}),
633        );
634        let scale = write(
635            "b.safetensors",
636            serde_json::json!({"p.weight_scale":{"dtype":"F32","shape":[2,1],"data_offsets":[0,8]}}),
637        );
638        assert!(p.preflight(std::slice::from_ref(&weight)).is_err());
639        p.preflight(&[weight.clone(), scale]).unwrap();
640        let wrong = write(
641            "c.safetensors",
642            serde_json::json!({"p.weight_scale":{"dtype":"F32","shape":[1,2],"data_offsets":[0,8]}}),
643        );
644        assert!(p.preflight(&[weight, wrong]).is_err());
645    }
646}