Skip to main content

spider_optimize/
artifact.rs

1//! Version one numeric model artifacts. All integers and floats are little endian.
2use crate::features::{EDIT_DIM, EDIT_FEATURE_VERSION};
3use crate::model::ModelVersion;
4use crate::schema::SCHEMA_VERSION;
5use spider_route::features::FEATURES_USED;
6use std::fmt;
7
8/// Maximum layer width or tree node count accepted by this reader.
9pub const MAX_WIDTH: usize = 256;
10/// Pack four numeric categories without overlapping their bits.
11pub const fn cell_id(need: u8, ext: u8, memory_state: u8, edit_code: u8) -> u32 {
12    ((need as u32) << 24) | ((ext as u32) << 16) | ((memory_state as u32) << 8) | edit_code as u32
13}
14/// The inference payload family.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum ModelKind {
17    /// Three dense networks.
18    Mlp,
19    /// Three ensembles of decision trees.
20    Gbdt,
21}
22/// Why an artifact cannot be loaded.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub enum ArtifactError {
25    /// Missing header or checksum.
26    TooShort,
27    /// Incorrect signature.
28    BadMagic,
29    /// Unknown artifact version.
30    Version(u8),
31    /// Unknown payload kind.
32    Kind(u8),
33    /// Incompatible base feature version.
34    FeatureVersion(u16),
35    /// Incompatible edit feature version.
36    EditFeatureVersion(u16),
37    /// Incompatible schema version.
38    SchemaVersion(u16),
39    /// A declared table exceeds remaining bytes.
40    Truncated,
41    /// Invalid scratch bound.
42    Width(u16),
43    /// Checksum mismatch.
44    Crc,
45    /// Input exceeds 2,000,000 bytes.
46    Size(usize),
47    /// Invalid dimensions, numeric values, topology, or trailing data.
48    Shape,
49}
50impl fmt::Display for ArtifactError {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(f, "invalid optimizer artifact: {self:?}")
53    }
54}
55impl std::error::Error for ArtifactError {}
56
57pub(crate) struct Layer {
58    pub rows: usize,
59    pub cols: usize,
60    pub activation: u8,
61    pub weights: Vec<f32>,
62    pub bias: Vec<f32>,
63}
64pub(crate) struct Node {
65    pub feature: u16,
66    pub threshold: f32,
67    pub left: u16,
68    pub right: u16,
69    pub value: f32,
70}
71pub(crate) enum Head {
72    Mlp(Vec<Layer>),
73    Gbdt(f32, Vec<Vec<Node>>),
74}
75pub(crate) enum Calibration {
76    Identity,
77    Platt(f32, f32),
78    Isotonic(Vec<(f32, f32)>),
79}
80/// Owned, validated weights. Loading allocates; scoring does not.
81pub struct Compact {
82    pub(crate) kind: ModelKind,
83    pub(crate) thresholds: Vec<f32>,
84    pub(crate) support: Vec<u32>,
85    pub(crate) calibration: Vec<Calibration>,
86    pub(crate) heads: Vec<Head>,
87}
88struct Reader<'a> {
89    bytes: &'a [u8],
90}
91impl Reader<'_> {
92    fn take(&mut self, n: usize) -> Result<&[u8], ArtifactError> {
93        let out = self.bytes.get(..n).ok_or(ArtifactError::Truncated)?;
94        self.bytes = self.bytes.get(n..).ok_or(ArtifactError::Truncated)?;
95        Ok(out)
96    }
97    fn u8(&mut self) -> Result<u8, ArtifactError> {
98        Ok(*self.take(1)?.first().ok_or(ArtifactError::Truncated)?)
99    }
100    fn u16(&mut self) -> Result<u16, ArtifactError> {
101        let [low, high]: [u8; 2] = self
102            .take(2)?
103            .try_into()
104            .map_err(|_| ArtifactError::Truncated)?;
105        Ok(low as u16 | ((high as u16) << 8))
106    }
107    fn u32(&mut self) -> Result<u32, ArtifactError> {
108        Ok(u32::from_le_bytes(
109            self.take(4)?
110                .try_into()
111                .map_err(|_| ArtifactError::Truncated)?,
112        ))
113    }
114    fn float(&mut self) -> Result<f32, ArtifactError> {
115        Ok(f32::from_bits(self.u32()?))
116    }
117    fn finite(&mut self) -> Result<f32, ArtifactError> {
118        let v = self.float()?;
119        if v.is_finite() {
120            Ok(v)
121        } else {
122            Err(ArtifactError::Shape)
123        }
124    }
125    fn count(&self, n: usize, size: usize) -> Result<(), ArtifactError> {
126        if n > self.bytes.len() / size {
127            Err(ArtifactError::Truncated)
128        } else {
129            Ok(())
130        }
131    }
132    fn floats(&mut self, n: usize) -> Result<Vec<f32>, ArtifactError> {
133        self.count(n, 4)?;
134        (0..n).map(|_| self.finite()).collect()
135    }
136}
137pub(crate) fn crc32(bytes: &[u8]) -> u32 {
138    let mut crc = !0u32;
139    for byte in bytes {
140        crc ^= u32::from(*byte);
141        for _ in 0..8 {
142            crc = (crc >> 1) ^ (0xedb88320 & (0u32.wrapping_sub(crc & 1)));
143        }
144    }
145    !crc
146}
147impl Compact {
148    /// Validate lengths, versions, checksum, shapes and acyclic tree topology.
149    pub fn from_bytes(bytes: &[u8]) -> Result<Self, ArtifactError> {
150        if bytes.len() > 2_000_000 {
151            return Err(ArtifactError::Size(bytes.len()));
152        }
153        if bytes.len() < 20 {
154            return Err(ArtifactError::TooShort);
155        }
156        let mut r = Reader { bytes };
157        if r.take(5)? != b"SPOPT" {
158            return Err(ArtifactError::BadMagic);
159        }
160        let v = r.u8()?;
161        if v != 1 {
162            return Err(ArtifactError::Version(v));
163        }
164        let kind = match r.u8()? {
165            1 => ModelKind::Mlp,
166            2 => ModelKind::Gbdt,
167            k => return Err(ArtifactError::Kind(k)),
168        };
169        let v = r.u16()?;
170        if v != 1 {
171            return Err(ArtifactError::FeatureVersion(v));
172        }
173        let v = r.u16()?;
174        if v != EDIT_FEATURE_VERSION {
175            return Err(ArtifactError::EditFeatureVersion(v));
176        }
177        let v = r.u16()?;
178        if v != SCHEMA_VERSION {
179            return Err(ArtifactError::SchemaVersion(v));
180        }
181        let width = r.u16()?;
182        if width == 0 || usize::from(width) > MAX_WIDTH {
183            return Err(ArtifactError::Width(width));
184        }
185        if r.u8()? != 0 {
186            return Err(ArtifactError::Shape);
187        }
188        let end = bytes.len() - 4;
189        let data = bytes.get(..end).ok_or(ArtifactError::Truncated)?;
190        let checksum = bytes.get(end..).ok_or(ArtifactError::Truncated)?;
191        if crc32(data) != (Reader { bytes: checksum }).u32()? {
192            return Err(ArtifactError::Crc);
193        }
194        r.bytes = bytes.get(16..end).ok_or(ArtifactError::Truncated)?;
195        let n = usize::from(r.u16()?);
196        if n == 0 || n > 10 {
197            return Err(ArtifactError::Shape);
198        }
199        r.count(n, 4)?;
200        let mut thresholds = Vec::with_capacity(n);
201        for _ in 0..n {
202            let v = r.float()?;
203            if !v.is_nan() && !(0.0..=1.0).contains(&v) {
204                return Err(ArtifactError::Shape);
205            }
206            thresholds.push(v);
207        }
208        let n = r.u32()? as usize;
209        r.count(n, 4)?;
210        let mut support = Vec::with_capacity(n);
211        for _ in 0..n {
212            let cell = r.u32()?;
213            if support.last().is_some_and(|last| *last >= cell) {
214                return Err(ArtifactError::Shape);
215            }
216            support.push(cell);
217        }
218        let mut calibration = Vec::with_capacity(3);
219        for _ in 0..3 {
220            let k = r.u16()?;
221            let n = usize::from(r.u16()?);
222            calibration.push(match (k, n) {
223                (0, 0) => Calibration::Identity,
224                (1, 0) => Calibration::Platt(r.finite()?, r.finite()?),
225                (2, 2..) => {
226                    r.count(n, 8)?;
227                    let mut pairs: Vec<(f32, f32)> = Vec::with_capacity(n);
228                    for _ in 0..n {
229                        let pair = (r.finite()?, r.finite()?);
230                        if pairs.last().is_some_and(|p| p.0 >= pair.0 || p.1 > pair.1) {
231                            return Err(ArtifactError::Shape);
232                        }
233                        pairs.push(pair);
234                    }
235                    Calibration::Isotonic(pairs)
236                }
237                _ => return Err(ArtifactError::Shape),
238            });
239        }
240        let mut heads = Vec::with_capacity(3);
241        let mut largest = 0;
242        for _ in 0..3 {
243            heads.push(match kind {
244                ModelKind::Mlp => {
245                    let n = usize::from(r.u16()?);
246                    r.count(n, 5)?;
247                    if n == 0 {
248                        return Err(ArtifactError::Shape);
249                    }
250                    let mut layers = Vec::with_capacity(n);
251                    let mut previous = FEATURES_USED + EDIT_DIM;
252                    for _ in 0..n {
253                        let rows = usize::from(r.u16()?);
254                        let cols = usize::from(r.u16()?);
255                        let activation = r.u8()?;
256                        if rows == 0
257                            || rows > usize::from(width)
258                            || cols != previous
259                            || cols > MAX_WIDTH
260                            || activation > 2
261                        {
262                            return Err(ArtifactError::Shape);
263                        }
264                        largest = largest.max(rows).max(cols);
265                        layers.push(Layer {
266                            rows,
267                            cols,
268                            activation,
269                            weights: r.floats(rows * cols)?,
270                            bias: r.floats(rows)?,
271                        });
272                        previous = rows;
273                    }
274                    if previous != 1 {
275                        return Err(ArtifactError::Shape);
276                    }
277                    Head::Mlp(layers)
278                }
279                ModelKind::Gbdt => {
280                    let n = r.u32()? as usize;
281                    let base = r.finite()?;
282                    r.count(n, 2)?;
283                    let mut trees = Vec::with_capacity(n);
284                    for _ in 0..n {
285                        let n = usize::from(r.u16()?);
286                        if n == 0 || n > usize::from(width) {
287                            return Err(ArtifactError::Shape);
288                        }
289                        largest = largest.max(n);
290                        r.count(n, 14)?;
291                        let mut nodes = Vec::with_capacity(n);
292                        for _ in 0..n {
293                            nodes.push(Node {
294                                feature: r.u16()?,
295                                threshold: r.finite()?,
296                                left: r.u16()?,
297                                right: r.u16()?,
298                                value: r.finite()?,
299                            });
300                        }
301                        for node in &nodes {
302                            if node.left == 65535 && node.right == 65535 {
303                                continue;
304                            }
305                            if usize::from(node.feature & 0x7fff) >= FEATURES_USED + EDIT_DIM
306                                || usize::from(node.left) >= n
307                                || usize::from(node.right) >= n
308                            {
309                                return Err(ArtifactError::Shape);
310                            }
311                        }
312                        // DFS colours detect cycles even in unreachable subgraphs.
313                        let mut colours = vec![0u8; n];
314                        for at in 0..n {
315                            visit(&nodes, &mut colours, at)?;
316                        }
317                        trees.push(nodes);
318                    }
319                    Head::Gbdt(base, trees)
320                }
321            });
322        }
323        if largest != usize::from(width) || !r.bytes.is_empty() {
324            return Err(ArtifactError::Shape);
325        }
326        Ok(Self {
327            kind,
328            thresholds,
329            support,
330            calibration,
331            heads,
332        })
333    }
334    /// Payload family.
335    pub fn kind(&self) -> ModelKind {
336        self.kind
337    }
338    /// Artifact version.
339    pub fn version(&self) -> ModelVersion {
340        ModelVersion(1)
341    }
342    /// Success floor; missing codes and NaN entries abstain.
343    pub fn threshold(&self, edit_code: u8) -> Option<f32> {
344        self.thresholds
345            .get(usize::from(edit_code))
346            .copied()
347            .filter(|v| v.is_finite())
348    }
349    /// Whether this exact cell occurs in the support table.
350    pub fn supported(&self, cell: u32) -> bool {
351        self.support.binary_search(&cell).is_ok()
352    }
353}
354fn visit(nodes: &[Node], colours: &mut [u8], at: usize) -> Result<(), ArtifactError> {
355    match colours.get(at) {
356        Some(2) => return Ok(()),
357        Some(0) => {}
358        _ => return Err(ArtifactError::Shape),
359    }
360    *colours.get_mut(at).ok_or(ArtifactError::Shape)? = 1;
361    let node = nodes.get(at).ok_or(ArtifactError::Shape)?;
362    if node.left != 65535 || node.right != 65535 {
363        visit(nodes, colours, usize::from(node.left))?;
364        visit(nodes, colours, usize::from(node.right))?;
365    }
366    *colours.get_mut(at).ok_or(ArtifactError::Shape)? = 2;
367    Ok(())
368}
369/// Load the bundled synthetic fixture, not a trained production model.
370#[cfg(feature = "embedded-model")]
371pub fn embedded() -> Result<Compact, ArtifactError> {
372    Compact::from_bytes(include_bytes!("../assets/spider-optimize-v1.bin"))
373}
374
375#[cfg(test)]
376mod tests {
377    #![allow(
378        clippy::unwrap_used,
379        clippy::expect_used,
380        clippy::panic,
381        clippy::indexing_slicing
382    )]
383    use super::*;
384    use crate::{edit_code, Input, Key, Schema, Scorer, KEEP_CODE};
385
386    pub(crate) struct ArtifactTables {
387        model: Compact,
388        width: u16,
389    }
390    fn u16out(out: &mut Vec<u8>, x: u16) {
391        out.extend(x.to_le_bytes());
392    }
393    fn u32out(out: &mut Vec<u8>, x: u32) {
394        out.extend(x.to_le_bytes());
395    }
396    fn float(out: &mut Vec<u8>, x: f32) {
397        u32out(out, x.to_bits());
398    }
399    pub(crate) fn write_artifact(tables: &ArtifactTables) -> Vec<u8> {
400        let model = &tables.model;
401        let mut out = b"SPOPT".to_vec();
402        out.push(1);
403        out.push(if model.kind == ModelKind::Mlp { 1 } else { 2 });
404        for v in [1, EDIT_FEATURE_VERSION, SCHEMA_VERSION, tables.width] {
405            u16out(&mut out, v);
406        }
407        out.push(0);
408        u16out(&mut out, model.thresholds.len() as u16);
409        for v in &model.thresholds {
410            float(&mut out, *v);
411        }
412        u32out(&mut out, model.support.len() as u32);
413        for v in &model.support {
414            u32out(&mut out, *v);
415        }
416        for cal in &model.calibration {
417            match cal {
418                Calibration::Identity => {
419                    u16out(&mut out, 0);
420                    u16out(&mut out, 0);
421                }
422                Calibration::Platt(a, b) => {
423                    u16out(&mut out, 1);
424                    u16out(&mut out, 0);
425                    float(&mut out, *a);
426                    float(&mut out, *b);
427                }
428                Calibration::Isotonic(pairs) => {
429                    u16out(&mut out, 2);
430                    u16out(&mut out, pairs.len() as u16);
431                    for (x, y) in pairs {
432                        float(&mut out, *x);
433                        float(&mut out, *y);
434                    }
435                }
436            }
437        }
438        for head in &model.heads {
439            match head {
440                Head::Mlp(layers) => {
441                    u16out(&mut out, layers.len() as u16);
442                    for layer in layers {
443                        u16out(&mut out, layer.rows as u16);
444                        u16out(&mut out, layer.cols as u16);
445                        out.push(layer.activation);
446                        for v in layer.weights.iter().chain(&layer.bias) {
447                            float(&mut out, *v);
448                        }
449                    }
450                }
451                Head::Gbdt(base, trees) => {
452                    u32out(&mut out, trees.len() as u32);
453                    float(&mut out, *base);
454                    for tree in trees {
455                        u16out(&mut out, tree.len() as u16);
456                        for node in tree {
457                            u16out(&mut out, node.feature);
458                            float(&mut out, node.threshold);
459                            u16out(&mut out, node.left);
460                            u16out(&mut out, node.right);
461                            float(&mut out, node.value);
462                        }
463                    }
464                }
465            }
466        }
467        let crc = crc32(&out);
468        u32out(&mut out, crc);
469        out
470    }
471    fn random(seed: &mut u32) -> f32 {
472        *seed ^= *seed << 13;
473        *seed ^= *seed >> 17;
474        *seed ^= *seed << 5;
475        // Quantized xorshift weights keep each float's low byte zero, so
476        // numeric fixtures cannot accidentally encode long ASCII strings.
477        ((*seed % 2049) as f32 - 1024.0) / 1024.0
478    }
479    fn fixture(kind: ModelKind) -> ArtifactTables {
480        let mut seed = 0x17abc123;
481        let mut heads = Vec::new();
482        for h in 0..3 {
483            heads.push(match kind {
484                ModelKind::Mlp => Head::Mlp(vec![
485                    Layer {
486                        rows: 8,
487                        cols: 248,
488                        activation: 1,
489                        weights: (0..8 * 248).map(|_| random(&mut seed) / 32.0).collect(),
490                        bias: vec![0.125; 8],
491                    },
492                    Layer {
493                        rows: 1,
494                        cols: 8,
495                        activation: 0,
496                        weights: (0..8).map(|_| random(&mut seed) / 4.0).collect(),
497                        bias: vec![h as f32 + 0.5],
498                    },
499                ]),
500                ModelKind::Gbdt => Head::Gbdt(
501                    h as f32 + 0.5,
502                    (0..2)
503                        .map(|i| {
504                            vec![
505                                Node {
506                                    feature: if i == 0 { 0x8000 } else { 152 },
507                                    threshold: 0.0,
508                                    left: 1,
509                                    right: 2,
510                                    value: 0.0,
511                                },
512                                Node {
513                                    feature: 0,
514                                    threshold: 0.0,
515                                    left: 65535,
516                                    right: 65535,
517                                    value: -0.125,
518                                },
519                                Node {
520                                    feature: 0,
521                                    threshold: 0.0,
522                                    left: 65535,
523                                    right: 65535,
524                                    value: 0.25,
525                                },
526                            ]
527                        })
528                        .collect(),
529                ),
530            });
531        }
532        ArtifactTables {
533            width: if kind == ModelKind::Mlp { 248 } else { 3 },
534            model: Compact {
535                kind,
536                thresholds: vec![0.5, 0.625, 0.75, f32::NAN],
537                support: (0..4).map(|code| cell_id(1, 2, 0, code)).collect(),
538                calibration: vec![
539                    Calibration::Platt(1.0, -0.125),
540                    Calibration::Identity,
541                    Calibration::Identity,
542                ],
543                heads,
544            },
545        }
546    }
547    // The shipped artifacts and parity vectors come from the Python trainer
548    // (training/fixtures/golden/README.md); these tables only feed the guard tests.
549    #[test]
550    fn compact_codes_are_unique_and_ordered() {
551        assert_eq!(KEEP_CODE, 0);
552        let schema = Schema::v1();
553        let mut codes = Vec::new();
554        for key in Key::ALL {
555            if schema.spec(*key).learnable {
556                codes.push(edit_code(*key).unwrap());
557            } else {
558                assert_eq!(edit_code(*key), None);
559            }
560        }
561        assert_eq!(codes, (1..=9).collect::<Vec<_>>());
562        assert_eq!(cell_id(1, 2, 3, 4), 0x01020304);
563    }
564    #[test]
565    fn hostile_bytes_never_load_or_panic() {
566        for kind in [ModelKind::Mlp, ModelKind::Gbdt] {
567            let bytes = write_artifact(&fixture(kind));
568            assert!(Compact::from_bytes(&bytes).is_ok());
569            for length in 0..bytes.len() {
570                assert!(Compact::from_bytes(&bytes[..length]).is_err());
571            }
572            for i in 0..4096 {
573                let mut corrupt = bytes.clone();
574                let at = (i * 37) % bytes.len();
575                corrupt[at] ^= 1 << (i % 8);
576                assert!(Compact::from_bytes(&corrupt).is_err());
577            }
578        }
579    }
580    #[test]
581    fn checksum_and_structural_guards() {
582        assert_eq!(crc32(b"123456789"), 0xcbf43926);
583        let original = write_artifact(&fixture(ModelKind::Mlp));
584        for (at, value, expected) in [
585            (0, 0, ArtifactError::BadMagic),
586            (5, 2, ArtifactError::Version(2)),
587            (6, 3, ArtifactError::Kind(3)),
588            (7, 2, ArtifactError::FeatureVersion(2)),
589            (9, 2, ArtifactError::EditFeatureVersion(2)),
590            (11, 1, ArtifactError::SchemaVersion(1)),
591            (13, 0, ArtifactError::Width(0)),
592            (15, 1, ArtifactError::Shape),
593            (16, 0, ArtifactError::Shape),
594        ] {
595            let mut bytes = original.clone();
596            bytes[at] = value;
597            let end = bytes.len() - 4;
598            let crc = crc32(&bytes[..end]);
599            bytes[end..].copy_from_slice(&crc.to_le_bytes());
600            assert_eq!(Compact::from_bytes(&bytes).err(), Some(expected));
601        }
602        assert_eq!(
603            Compact::from_bytes(&vec![0; 2_000_001]).err(),
604            Some(ArtifactError::Size(2_000_001))
605        );
606        let mut t = fixture(ModelKind::Mlp);
607        t.model.support.reverse();
608        assert_eq!(
609            Compact::from_bytes(&write_artifact(&t)).err(),
610            Some(ArtifactError::Shape)
611        );
612        t = fixture(ModelKind::Mlp);
613        if let Head::Mlp(layers) = &mut t.model.heads[0] {
614            layers[0].weights[0] = f32::INFINITY;
615        }
616        assert_eq!(
617            Compact::from_bytes(&write_artifact(&t)).err(),
618            Some(ArtifactError::Shape)
619        );
620        let mut t = fixture(ModelKind::Gbdt);
621        if let Head::Gbdt(_, trees) = &mut t.model.heads[0] {
622            trees[0][0].left = 0;
623        }
624        assert_eq!(
625            Compact::from_bytes(&write_artifact(&t)).err(),
626            Some(ArtifactError::Shape)
627        );
628    }
629    #[test]
630    fn malformed_tables_with_valid_checksums_are_rejected() {
631        let original = write_artifact(&fixture(ModelKind::Mlp));
632        // Every prefix with a repaired checksum reaches structural parsing.
633        for end in (16..original.len() - 4).step_by(7) {
634            let mut bytes = original[..end].to_vec();
635            let crc = crc32(&bytes);
636            u32out(&mut bytes, crc);
637            assert!(Compact::from_bytes(&bytes).is_err());
638        }
639        for mutation in 0..15 {
640            let mut t = fixture(ModelKind::Mlp);
641            match mutation {
642                0 => t.width = 257,
643                1 => t.width = 249,
644                2 => t.model.thresholds[0] = f32::INFINITY,
645                3 => t.model.thresholds[0] = -0.1,
646                4 => t.model.support[1] = t.model.support[0],
647                5 => t.model.calibration[0] = Calibration::Platt(f32::NAN, 0.0),
648                6 => t.model.calibration[0] = Calibration::Isotonic(vec![(0.0, 0.0)]),
649                7 => t.model.calibration[0] = Calibration::Isotonic(vec![(0.0, 0.0), (0.0, 1.0)]),
650                8 => t.model.calibration[0] = Calibration::Isotonic(vec![(0.0, 1.0), (1.0, 0.0)]),
651                _ => {
652                    if let Head::Mlp(layers) = &mut t.model.heads[0] {
653                        match mutation {
654                            9 => layers.clear(),
655                            10 => layers[0].rows = 0,
656                            11 => layers[0].rows = 257,
657                            12 => layers[0].cols = 247,
658                            13 => layers[0].activation = 3,
659                            _ => {
660                                layers.pop();
661                            }
662                        }
663                    }
664                }
665            }
666            assert!(
667                Compact::from_bytes(&write_artifact(&t)).is_err(),
668                "mutation {mutation}"
669            );
670        }
671        for mutation in 0..6 {
672            let mut t = fixture(ModelKind::Gbdt);
673            if let Head::Gbdt(base, trees) = &mut t.model.heads[0] {
674                match mutation {
675                    0 => *base = f32::NAN,
676                    1 => trees[0].clear(),
677                    2 => trees[0][0].feature = 248,
678                    3 => trees[0][0].right = 3,
679                    4 => trees[0][0].left = 65535,
680                    _ => trees[0][2].right = 0,
681                }
682            }
683            assert!(
684                Compact::from_bytes(&write_artifact(&t)).is_err(),
685                "tree mutation {mutation}"
686            );
687        }
688        let mut extra = original[..original.len() - 4].to_vec();
689        extra.push(0);
690        let crc = crc32(&extra);
691        u32out(&mut extra, crc);
692        assert_eq!(
693            Compact::from_bytes(&extra).err(),
694            Some(ArtifactError::Shape)
695        );
696        let mut t = fixture(ModelKind::Mlp);
697        t.model.calibration[0] = Calibration::Isotonic(vec![(0.0, 0.0), (1.0, 1.0)]);
698        if let Head::Mlp(layers) = &mut t.model.heads[0] {
699            for layer in layers.iter_mut() {
700                layer.weights.fill(0.0);
701                layer.bias.fill(0.0);
702            }
703            layers[1].activation = 2;
704        }
705        let loaded = Compact::from_bytes(&write_artifact(&t)).unwrap();
706        assert_eq!(
707            loaded.score_slices(&[0.0; 152], &[0.0; 96], 0).p_success,
708            0.5
709        );
710        assert_eq!(loaded.version(), ModelVersion(1));
711    }
712
713    #[test]
714    fn support_lengths_missing_and_calibration() {
715        let mut t = fixture(ModelKind::Gbdt);
716        let m = &t.model;
717        assert_eq!(m.score(&Input::default()).support, 0.0);
718        assert_eq!(
719            m.score_in_cell(&Input::default(), cell_id(1, 2, 0, 0))
720                .support,
721            1.0
722        );
723        assert_eq!(m.threshold(0), Some(0.5));
724        assert_eq!(m.threshold(3), None);
725        assert_eq!(m.threshold(255), None);
726        for len in [0, 151, 153, 256] {
727            assert!(m
728                .score_slices(&vec![0.0; len], &[0.0; 96], 0)
729                .p_success
730                .is_nan());
731        }
732        for len in [0, 95, 97] {
733            assert!(m
734                .score_slices(&[0.0; 152], &vec![0.0; len], 0)
735                .p_success
736                .is_nan());
737        }
738        for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] {
739            let mut base = [0.0; 152];
740            base[0] = value;
741            let left = m.score_slices(&base, &[0.0; 96], 0);
742            assert!(left.p_success.is_nan());
743            assert!((left.latency_ms - 1.25f32.exp_m1()).abs() < 1e-6);
744            let mut edit = [0.0; 96];
745            edit[0] = value;
746            let right = m.score_slices(&[0.0; 152], &edit, 0);
747            assert!(right.p_success.is_nan());
748            assert!((right.latency_ms - 1.625f32.exp_m1()).abs() < 1e-6);
749        }
750        t.model.support.clear();
751        assert_eq!(t.model.score(&Input::default()).support, 1.0);
752        let cal = Calibration::Isotonic(vec![(0.0, 0.25), (1.0, 0.75)]);
753        assert_eq!(cal.apply(-1.0), 0.25);
754        assert_eq!(cal.apply(0.5), 0.5);
755        assert_eq!(cal.apply(2.0), 0.75);
756    }
757}