incin-core 0.1.0

Core tensor operations and static shape checking for incin
Documentation
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
//! Backend-neutral, owned model state artifacts.
//!
//! A state snapshot is deliberately separate from live tensors and backend
//! variable handles.  It is therefore safe to serialize, inspect, validate,
//! and stage before touching a module's live state.

use alloc::{
    collections::BTreeMap,
    string::{String, ToString},
    vec::Vec,
};
use core::fmt;

use crate::{
    err::{Error, ErrorMessage, Result},
    shapes::ShapeBuf,
    tensor::dtype::DTypeDescriptor,
};

/// Durable, hierarchical state name.  This is a serialization path, not a
/// parameter/runtime-variable identity or alias identifier.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
pub struct StatePath(String);

impl<'de> serde::Deserialize<'de> for StatePath {
    fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let raw = <String as serde::Deserialize>::deserialize(deserializer)?;
        if raw.is_empty() {
            Ok(Self::root())
        } else {
            Self::new(raw).map_err(|error| serde::de::Error::custom(error.to_string()))
        }
    }
}

impl StatePath {
    /// The root path.
    #[must_use]
    pub fn root() -> Self {
        Self(String::new())
    }

    /// Creates a path from its canonical dotted representation.
    pub fn new(path: impl Into<String>) -> Result<Self> {
        let path = path.into();
        if path.is_empty()
            || path.starts_with('.')
            || path.ends_with('.')
            || path
                .split('.')
                .any(|part| part.is_empty() || part == "." || part == "..")
        {
            return Err(Error::InvalidModuleState {
                operation: "state path",
                reason: ErrorMessage::new("path must contain non-empty dotted components"),
            });
        }
        Ok(Self(path))
    }

    /// Fallibly adds exactly one named child component.
    ///
    /// Components are non-empty strings that do not contain `.`.  Dotted
    /// paths must be built one component at a time so every durable path
    /// produced by traversal has the same grammar as [`Self::new`].
    pub fn try_child(&self, name: &str) -> Result<Self> {
        if name.is_empty() || name.contains('.') {
            return Err(Error::InvalidModuleState {
                operation: "state path child",
                reason: ErrorMessage::new("component must be non-empty and must not contain `.`"),
            });
        }
        if self.0.is_empty() {
            Ok(Self(name.into()))
        } else {
            Ok(Self(format!("{}.{}", self.0, name)))
        }
    }

    /// Adds a flat positional child component used by `Sequential`.
    #[must_use]
    pub fn index(&self, index: usize) -> Self {
        let component = index.to_string();
        if self.0.is_empty() {
            Self(component)
        } else {
            Self(format!("{}.{}", self.0, component))
        }
    }

    /// Returns the canonical dotted representation.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for StatePath {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

/// Whether a state value is trainable or a persistent non-trainable buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum StateRole {
    /// Role marker: learnable parameter.
    Parameter,
    /// Role marker: non-trainable buffer.
    Buffer,
}

/// Receives typed state leaves during module traversal.
pub trait StateVisitor<B: crate::tensor::backend::VariableBackend> {
    /// Visits one parameter leaf.
    fn visit_param<S, K, Train>(
        &mut self,
        path: &StatePath,
        param: &crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState;

    /// Visits one buffer leaf.
    fn visit_buffer<S, K>(
        &mut self,
        path: &StatePath,
        buffer: &crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop;
}

/// Structural traversal of typed parameter and buffer leaves.
pub trait VisitState<B: crate::tensor::backend::VariableBackend> {
    /// Number of flat module slots occupied by this subtree.
    fn flat_width() -> usize
    where
        Self: Sized,
    {
        1
    }

    /// Walks this module's leaves through a visitor.
    fn visit_state<V: StateVisitor<B>>(&self, path: &StatePath, visitor: &mut V) -> Result<()>;

    /// Visits this subtree at a flat positional index under `parent`.
    fn visit_state_flat<V: StateVisitor<B>>(
        &self,
        parent: &StatePath,
        base_index: usize,
        visitor: &mut V,
    ) -> Result<()>
    where
        Self: Sized,
    {
        self.visit_state(&parent.index(base_index), visitor)
    }
}

/// Receives mutable typed state leaves while restoring a snapshot.
pub trait StateMutVisitor<B: crate::tensor::backend::VariableBackend> {
    /// Visits one parameter leaf.
    fn visit_param<S, K, Train>(
        &mut self,
        path: &StatePath,
        param: &mut crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState;

    /// Visits one buffer leaf.
    fn visit_buffer<S, K>(
        &mut self,
        path: &StatePath,
        buffer: &mut crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop;
}

/// Structural mutable traversal used by snapshot restoration.
pub trait VisitStateMut<B: crate::tensor::backend::VariableBackend> {
    /// Number of flat module slots occupied by this subtree.
    fn flat_width() -> usize
    where
        Self: Sized,
    {
        1
    }

    /// Walks leaves mutably through a visitor.
    fn visit_state_mut<V: StateMutVisitor<B>>(
        &mut self,
        path: &StatePath,
        visitor: &mut V,
    ) -> Result<()>;

    /// Visits this subtree at a flat positional index under `parent`.
    fn visit_state_mut_flat<V: StateMutVisitor<B>>(
        &mut self,
        parent: &StatePath,
        base_index: usize,
        visitor: &mut V,
    ) -> Result<()>
    where
        Self: Sized,
    {
        self.visit_state_mut(&parent.index(base_index), visitor)
    }
}

/// Collects typed leaves into the durable, backend-neutral snapshot format.
#[derive(Debug, Default)]
pub struct StateSnapshotVisitor {
    snapshot: StateSnapshot,
}

impl StateSnapshotVisitor {
    #[must_use]
    /// Creates an empty collector.
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    /// Converts collected leaves into a snapshot.
    pub fn into_snapshot(self) -> StateSnapshot {
        self.snapshot
    }
}

impl<B: crate::tensor::backend::VariableBackend> StateVisitor<B> for StateSnapshotVisitor {
    fn visit_param<S, K, Train>(
        &mut self,
        path: &StatePath,
        param: &crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        self.snapshot
            .insert(path.clone(), param.snapshot_state_value(path)?)
    }

    fn visit_buffer<S, K>(
        &mut self,
        path: &StatePath,
        buffer: &crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        self.snapshot
            .insert(path.clone(), buffer.snapshot_state_value(path)?)
    }
}

/// Runs typed state traversal and returns an owned snapshot.
pub fn collect_state<B, M>(module: &M) -> Result<StateSnapshot>
where
    B: crate::tensor::backend::VariableBackend,
    M: VisitState<B>,
{
    let mut visitor = StateSnapshotVisitor::new();
    module.visit_state(&StatePath::root(), &mut visitor)?;
    Ok(visitor.into_snapshot())
}

struct StatePreparation<'a> {
    snapshot: &'a StateSnapshot,
    alias_sources: &'a BTreeMap<usize, StatePath>,
}

impl<'a, B: crate::tensor::backend::VariableBackend> StateMutVisitor<B> for StatePreparation<'a> {
    fn visit_param<S, K, Train>(
        &mut self,
        path: &StatePath,
        param: &mut crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        let source = param
            .state_slot_identity()
            .and_then(|slot| self.alias_sources.get(&slot))
            .unwrap_or(path);
        param.prepare_state_value(source, self.snapshot)
    }

    fn visit_buffer<S, K>(
        &mut self,
        path: &StatePath,
        buffer: &mut crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        buffer.prepare_state_value(path, self.snapshot)
    }
}

struct StateCommit;

impl<B: crate::tensor::backend::VariableBackend> StateMutVisitor<B> for StateCommit {
    fn visit_param<S, K, Train>(
        &mut self,
        _path: &StatePath,
        param: &mut crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        param.commit_prepared_state()
    }

    fn visit_buffer<S, K>(
        &mut self,
        _path: &StatePath,
        buffer: &mut crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        buffer.commit_prepared_state()
    }
}

/// Drops transaction staging after a fully successful load.
struct StateFinalize;

impl<B: crate::tensor::backend::VariableBackend> StateMutVisitor<B> for StateFinalize {
    fn visit_param<S, K, Train>(
        &mut self,
        _path: &StatePath,
        param: &mut crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        param.clear_prepared_state();
        Ok(())
    }

    fn visit_buffer<S, K>(
        &mut self,
        _path: &StatePath,
        buffer: &mut crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        buffer.clear_prepared_state();
        Ok(())
    }
}

/// Drops staging for leaves that were never committed, or that were restored
/// successfully.  A leaf whose restore failed deliberately retains its
/// original storage and committed marker: clearing either would hide that the
/// backend left the transaction only partially rolled back.
struct StateAbortClear;

impl<B: crate::tensor::backend::VariableBackend> StateMutVisitor<B> for StateAbortClear {
    fn visit_param<S, K, Train>(
        &mut self,
        _path: &StatePath,
        param: &mut crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        param.clear_prepared_state_if_rolled_back();
        Ok(())
    }

    fn visit_buffer<S, K>(
        &mut self,
        _path: &StatePath,
        buffer: &mut crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        buffer.clear_prepared_state_if_rolled_back();
        Ok(())
    }
}

/// Restores every previously committed leaf.  Mutable state traversal stops
/// at the first visitor error, so rollback records its first error and keeps
/// visiting; traversal order is structural and therefore deterministic.
struct StateRollback {
    first_error: Option<Error>,
}

impl<B: crate::tensor::backend::VariableBackend> StateMutVisitor<B> for StateRollback {
    fn visit_param<S, K, Train>(
        &mut self,
        _path: &StatePath,
        param: &mut crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        let result = param.rollback_prepared_state();
        if self.first_error.is_none() {
            self.first_error = result.err();
        }
        Ok(())
    }

    fn visit_buffer<S, K>(
        &mut self,
        _path: &StatePath,
        buffer: &mut crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        let result = buffer.rollback_prepared_state();
        if self.first_error.is_none() {
            self.first_error = result.err();
        }
        Ok(())
    }
}

struct StateAliasAudit<'a> {
    snapshot: &'a StateSnapshot,
    sources: BTreeMap<usize, StatePath>,
}

impl<'a, B: crate::tensor::backend::VariableBackend> StateVisitor<B> for StateAliasAudit<'a> {
    fn visit_param<S, K, Train>(
        &mut self,
        path: &StatePath,
        param: &crate::nn::param::Param<S, B, K, Train>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
        Train: crate::nn::param::TrainState,
    {
        let Some(slot) = param.state_slot_identity() else {
            return Ok(());
        };
        let value = self
            .snapshot
            .get(path)
            .ok_or_else(|| Error::InvalidModuleState {
                operation: "load state",
                reason: ErrorMessage::new(format!("missing state path {path}")),
            })?;
        if let Some(canonical) = self.sources.get(&slot) {
            let canonical_value = self
                .snapshot
                .get(canonical)
                .expect("canonical state path was validated");
            if canonical_value != value {
                return Err(Error::InvalidModuleState {
                    operation: "load state",
                    reason: ErrorMessage::new(format!(
                        "conflicting payloads for tied parameters at {canonical} and {path}"
                    )),
                });
            }
            if path < canonical {
                self.sources.insert(slot, path.clone());
            }
        } else {
            self.sources.insert(slot, path.clone());
        }
        Ok(())
    }

    fn visit_buffer<S, K>(
        &mut self,
        _path: &StatePath,
        _buffer: &crate::nn::param::Buffer<S, B, K>,
    ) -> Result<()>
    where
        S: crate::shapes::Shape,
        K: crate::tensor::dtype::DType<Arg = ()>,
        B: crate::tensor::backend::SupportsDType<K>
            + crate::exec::Capabilities
            + crate::tensor::backend::HostInterop,
    {
        Ok(())
    }
}

/// Restores a complete snapshot through typed mutable leaf visits.
pub fn load_state<B, M>(module: &mut M, snapshot: &StateSnapshot) -> Result<()>
where
    B: crate::tensor::backend::VariableBackend,
    M: VisitState<B> + VisitStateMut<B>,
{
    let current = collect_state::<B, _>(&*module)?;
    let expected: alloc::collections::BTreeSet<_> = current.iter().map(|(path, _)| path).collect();
    let provided: alloc::collections::BTreeSet<_> = snapshot.iter().map(|(path, _)| path).collect();
    if expected != provided {
        let missing = expected
            .difference(&provided)
            .map(ToString::to_string)
            .collect::<alloc::vec::Vec<_>>();
        let unexpected = provided
            .difference(&expected)
            .map(ToString::to_string)
            .collect::<alloc::vec::Vec<_>>();
        return Err(Error::InvalidModuleState {
            operation: "load state",
            reason: ErrorMessage::new(format!(
                "state paths differ: missing {:?}, unexpected {:?}",
                missing, unexpected
            )),
        });
    }
    let mut aliases = StateAliasAudit {
        snapshot,
        sources: BTreeMap::new(),
    };
    module.visit_state(&StatePath::root(), &mut aliases)?;
    let mut visitor = StatePreparation {
        snapshot,
        alias_sources: &aliases.sources,
    };
    if let Err(error) = module.visit_state_mut(&StatePath::root(), &mut visitor) {
        let mut clear = StateFinalize;
        let _ = module.visit_state_mut(&StatePath::root(), &mut clear);
        return Err(error);
    }

    let mut commit = StateCommit;
    if let Err(error) = module.visit_state_mut(&StatePath::root(), &mut commit) {
        let mut rollback = StateRollback { first_error: None };
        // StateRollback consumes leaf errors so every committed leaf receives
        // a restoration attempt even if an earlier one fails.
        module.visit_state_mut(&StatePath::root(), &mut rollback)?;
        let mut clear = StateAbortClear;
        let _ = module.visit_state_mut(&StatePath::root(), &mut clear);
        if let Some(rollback_error) = rollback.first_error {
            return Err(Error::InvalidModuleState {
                operation: "load state rollback",
                reason: ErrorMessage::new(format!(
                    "commit failed ({error}); backend also rejected rollback ({rollback_error})"
                )),
            });
        }
        return Err(error);
    }
    let mut clear = StateFinalize;
    module.visit_state_mut(&StatePath::root(), &mut clear)
}

/// One owned, exact-dtype state value.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct StateValue {
    shape: ShapeBuf,
    dtype: DTypeDescriptor,
    bytes: Vec<u8>,
    role: StateRole,
}

#[derive(serde::Deserialize)]
struct StateValueWire {
    shape: ShapeBuf,
    dtype: DTypeDescriptor,
    bytes: Vec<u8>,
    role: StateRole,
}

impl<'de> serde::Deserialize<'de> for StateValue {
    fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let wire = <StateValueWire as serde::Deserialize>::deserialize(deserializer)?;
        Self::new(wire.shape, wire.dtype, wire.bytes, wire.role)
            .map_err(|error| serde::de::Error::custom(error.to_string()))
    }
}

impl StateValue {
    /// Constructs and validates an owned value.
    pub fn new(
        shape: ShapeBuf,
        dtype: DTypeDescriptor,
        bytes: Vec<u8>,
        role: StateRole,
    ) -> Result<Self> {
        let elements = shape.numel().ok_or_else(|| Error::InvalidModuleState {
            operation: "state value",
            reason: ErrorMessage::new("shape element count overflow"),
        })?;
        let expected = dtype
            .size_bytes(elements, crate::shapes::error::OperationKind::Storage)
            .map_err(|error| Error::InvalidModuleState {
                operation: "state value",
                reason: ErrorMessage::new(error.to_string()),
            })?;
        if bytes.len() != expected {
            return Err(Error::MalformedArtifact {
                operation: "state value",
                artifact: "model state",
                reason: ErrorMessage::new(format!(
                    "byte length {} does not match shape {:?} and dtype {} (expected {})",
                    bytes.len(),
                    shape.dims(),
                    dtype.name(),
                    expected
                )),
            });
        }
        Ok(Self {
            shape,
            dtype,
            bytes,
            role,
        })
    }

    /// Runtime shape.
    #[must_use]
    pub fn shape(&self) -> &ShapeBuf {
        &self.shape
    }
    /// Exact logical and physical dtype descriptor.
    #[must_use]
    pub fn dtype(&self) -> DTypeDescriptor {
        self.dtype
    }
    /// Native bytes, owned by this value.
    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }
    /// State role.
    #[must_use]
    pub fn role(&self) -> StateRole {
        self.role
    }
}

/// An owned collection of heterogeneous model state.
#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Serialize)]
pub struct StateSnapshot(BTreeMap<StatePath, StateValue>);

impl<'de> serde::Deserialize<'de> for StateSnapshot {
    fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct SnapshotVisitor;

        impl<'de> serde::de::Visitor<'de> for SnapshotVisitor {
            type Value = StateSnapshot;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("a map of unique state paths to state values")
            }

            fn visit_map<A>(self, mut map: A) -> core::result::Result<Self::Value, A::Error>
            where
                A: serde::de::MapAccess<'de>,
            {
                let mut snapshot = StateSnapshot::new();
                while let Some((path, value)) = map.next_entry()? {
                    snapshot
                        .insert(path, value)
                        .map_err(|error| serde::de::Error::custom(error.to_string()))?;
                }
                Ok(snapshot)
            }
        }

        deserializer.deserialize_map(SnapshotVisitor)
    }
}

impl StateSnapshot {
    /// Creates an empty snapshot.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
    /// Inserts a value, rejecting duplicate paths.
    pub fn insert(&mut self, path: StatePath, value: StateValue) -> Result<()> {
        if self.0.contains_key(&path) {
            return Err(Error::InvalidModuleState {
                operation: "state snapshot",
                reason: ErrorMessage::new("duplicate state path"),
            });
        }
        self.0.insert(path, value);
        Ok(())
    }
    /// Looks up a path.
    pub fn get(&self, path: &StatePath) -> Option<&StateValue> {
        self.0.get(path)
    }
    /// Number of values.
    #[must_use]
    pub fn len(&self) -> usize {
        self.0.len()
    }
    /// Whether the snapshot is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
    /// Deterministically ordered entries.
    pub fn iter(&self) -> impl Iterator<Item = (&StatePath, &StateValue)> {
        self.0.iter()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{shapes::ShapeBuf, tensor::dtype::DTypeId};

    #[test]
    fn validates_exact_native_byte_length() {
        let value = StateValue::new(
            ShapeBuf::from_slice(&[2, 3]),
            DTypeId::F16.descriptor(),
            vec![0; 12],
            StateRole::Parameter,
        )
        .expect("valid f16 state");
        assert_eq!(value.dtype(), DTypeId::F16.descriptor());
        assert!(
            StateValue::new(
                ShapeBuf::from_slice(&[2, 3]),
                DTypeId::F16.descriptor(),
                vec![0; 24],
                StateRole::Parameter,
            )
            .is_err()
        );
    }

    #[test]
    fn duplicate_insert_rejects_without_replacing_the_first_value() {
        let path = StatePath::new("weight").expect("valid path");
        let first = StateValue::new(
            ShapeBuf::from_slice(&[1]),
            DTypeId::F32.descriptor(),
            vec![0, 0, 128, 63],
            StateRole::Parameter,
        )
        .expect("valid first value");
        let replacement = StateValue::new(
            ShapeBuf::from_slice(&[1]),
            DTypeId::F32.descriptor(),
            vec![0, 0, 0, 64],
            StateRole::Parameter,
        )
        .expect("valid replacement value");
        let mut snapshot = StateSnapshot::new();

        snapshot.insert(path.clone(), first.clone()).unwrap();
        assert!(matches!(
            snapshot.insert(path.clone(), replacement),
            Err(Error::InvalidModuleState { .. })
        ));
        assert_eq!(snapshot.len(), 1);
        assert_eq!(snapshot.get(&path), Some(&first));
    }

    #[test]
    fn paths_are_ordered_and_distinct_from_runtime_identity() {
        let root = StatePath::root();
        assert_eq!(
            root.try_child("q_proj")
                .unwrap()
                .try_child("weight")
                .unwrap()
                .as_str(),
            "q_proj.weight"
        );
        assert_eq!(root.index(3).as_str(), "3");
        assert!(StatePath::new("q_proj.weight").is_ok());
        assert!(StatePath::new("q_proj..weight").is_err());
    }

    #[test]
    fn child_accepts_only_one_non_empty_component() {
        let root = StatePath::root();
        for invalid in ["", ".", "..", "a.b", "a..b", ".a", "a."] {
            assert!(root.try_child(invalid).is_err(), "accepted `{invalid}`");
        }
        for valid in ["weight", "bias", "running_mean", "layer_0"] {
            let path = root.try_child(valid).expect("valid component");
            assert_eq!(path.as_str(), valid);
            assert_eq!(
                postcard::from_bytes::<StatePath>(&postcard::to_allocvec(&path).unwrap()).unwrap(),
                path
            );
        }
    }

    #[test]
    fn root_and_nested_paths_roundtrip_through_postcard() {
        for path in [
            StatePath::root(),
            StatePath::new("weight").unwrap(),
            StatePath::new("layer_0.attention.q_proj.weight").unwrap(),
        ] {
            let encoded = postcard::to_allocvec(&path).unwrap();
            let decoded = postcard::from_bytes::<StatePath>(&encoded).unwrap();
            assert_eq!(decoded, path);
        }
    }

    #[test]
    fn preserves_supported_native_dtype_payloads() {
        for dtype in [
            DTypeId::F32,
            DTypeId::F16,
            DTypeId::BF16,
            DTypeId::I64,
            DTypeId::U32,
            DTypeId::U8,
            DTypeId::Bool,
            DTypeId::Q8_0,
        ] {
            let descriptor = dtype.descriptor();
            let bytes = descriptor
                .size_bytes(32, crate::shapes::error::OperationKind::Storage)
                .expect("test dtype has a storage size");
            let value = StateValue::new(
                ShapeBuf::from_slice(&[32]),
                descriptor,
                (0..bytes).map(|index| index as u8).collect(),
                StateRole::Buffer,
            )
            .expect("native payload should validate");
            assert_eq!(value.dtype(), descriptor);
            assert_eq!(value.bytes().len(), bytes);
        }
    }

    #[test]
    fn rejects_overflow_and_malformed_payloads() {
        assert!(
            StateValue::new(
                ShapeBuf::from_slice(&[usize::MAX, 2]),
                DTypeId::F32.descriptor(),
                Vec::new(),
                StateRole::Parameter,
            )
            .is_err()
        );
        assert!(
            StateValue::new(
                ShapeBuf::from_slice(&[2]),
                DTypeId::F32.descriptor(),
                vec![0; 3],
                StateRole::Parameter,
            )
            .is_err()
        );
    }

    #[test]
    fn wire_deserialization_revalidates_paths_and_payload_lengths() {
        let invalid_path = postcard::to_allocvec(&"layer..weight".to_string()).unwrap();
        assert!(postcard::from_bytes::<StatePath>(&invalid_path).is_err());

        #[derive(serde::Serialize)]
        struct Wire {
            shape: ShapeBuf,
            dtype: crate::tensor::dtype::DTypeDescriptor,
            bytes: Vec<u8>,
            role: StateRole,
        }

        let invalid_value = postcard::to_allocvec(&Wire {
            shape: ShapeBuf::from_slice(&[2]),
            dtype: DTypeId::F32.descriptor(),
            bytes: vec![0; 3],
            role: StateRole::Parameter,
        })
        .unwrap();
        assert!(postcard::from_bytes::<StateValue>(&invalid_value).is_err());
    }
}