burn-capture 0.22.1

Graph capture backend for the Burn framework
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
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
//! Non-executing router channel used to capture Burn operation graphs.

use alloc::{boxed::Box, collections::BTreeMap, format, string::String, sync::Arc, vec::Vec};
use burn_backend::{
    BoolStore, DType, DTypeUsage, DTypeUsageSet, DeviceId, DeviceOps, DeviceSettings,
    ExecutionError, RouterDeviceType, Shape, TensorData,
};
use burn_ir::{
    GraphBindings, GraphId, GraphIr, IrVisitorMut, OperationIr, TensorId, TensorIr, TensorStatus,
};
use burn_std::{device::Device, future::DynFut};
use hashbrown::{HashMap, HashSet};
use portable_atomic::{AtomicU64, Ordering};
use spin::Mutex;

use burn_router::{
    Graph, MultiBackendBridge, RouterChannel, RouterClient, RouterClientRegistration, RouterTensor,
    register_scoped_client,
};

static DEVICE_COUNTER: AtomicU64 = AtomicU64::new(0);
static TENSOR_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Backend type that records operations instead of executing them.
pub type CaptureBackend = burn_router::BackendRouter<CaptureChannel>;

/// Reusable logical device for isolated graph-capture scopes.
///
/// Device identifiers are deliberately monotonic and never recycled. This keeps stale tensor
/// handles from aliasing a newer capture device without requiring an identity registry. Since a
/// [`DeviceId`] has a `u16` backend-local index, creating more than 65,536 capture devices in one
/// process will panic. A device can be reused for any number of sequential capture scopes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CaptureDevice {
    id: u16,
}

impl CaptureDevice {
    /// Capture one operation scope on this reusable device.
    ///
    /// The closure receives a [`CaptureScope`] and must return the token produced by
    /// [`CaptureScope::complete`], declaring both the runtime input and requested output tensor
    /// IDs. A fresh session is installed before the closure runs and is always unregistered
    /// afterward, including while unwinding from a panic. Only one scope may be active on a capture
    /// device at a time. Calling `complete` closes the session immediately, so the closure must not
    /// perform more tensor operations before returning the completion token.
    pub fn capture_scope(
        &self,
        capture: impl FnOnce(CaptureScope) -> CompletedCaptureScope,
    ) -> Result<CapturedGraph, CaptureError> {
        let session = Arc::new(CaptureSession::default());
        let client = CaptureClient::new(*self, session.clone());
        // Bind this reusable device to the new session client for exactly this scope. BackendRouter
        // operations call `get_client`, which now finds this client instead of trying the channel's
        // intentionally unsupported unscoped initialization path.
        let registration = register_scoped_client::<CaptureChannel>(self, client.clone())
            .ok_or(CaptureError::AlreadyActive)?;
        let guard = CaptureScopeGuard {
            client,
            _registration: registration,
        };
        guard.complete(capture(CaptureScope { session }))
    }
}

impl Default for CaptureDevice {
    fn default() -> Self {
        let id = DEVICE_COUNTER.fetch_add(1, Ordering::Relaxed);
        assert!(
            id <= u16::MAX as u64,
            "capture device identifier space exhausted"
        );
        Self { id: id as u16 }
    }
}

impl Device for CaptureDevice {
    fn from_id(device_id: DeviceId) -> Self {
        assert_eq!(
            device_id.type_id,
            u16::from(RouterDeviceType::Capture),
            "invalid capture device type"
        );
        Self {
            id: device_id.index_id,
        }
    }

    fn to_id(&self) -> DeviceId {
        DeviceId {
            type_id: RouterDeviceType::Capture.into(),
            index_id: self.id,
        }
    }
}

impl DeviceOps for CaptureDevice {
    fn defaults(&self) -> DeviceSettings {
        DeviceSettings::new(
            DType::F32,
            DType::I64,
            DType::Bool(BoolStore::U8),
            Default::default(),
        )
    }
}

/// Final result of a capture scope.
#[derive(Clone, Debug)]
pub struct CapturedGraph {
    /// Ordered operation graph with caller-declared boundaries.
    pub graph: GraphIr,
    /// Concrete values supplied through tensor initialization, keyed by tensor ID.
    pub values: BTreeMap<TensorId, TensorData>,
}

/// Token passed to a [`CaptureDevice::capture_scope`] closure.
///
/// Call [`complete`](Self::complete) after recording the operations in the scope. The closure's
/// return type requires the resulting completion token, so a successful capture cannot omit its
/// explicit graph boundaries.
#[derive(Debug)]
pub struct CaptureScope {
    // The scope owns the authority to close its session. Keeping this private prevents callers
    // from manufacturing completion tokens detached from the active router registration.
    session: Arc<CaptureSession>,
}

impl CaptureScope {
    /// Complete the scope with ordered runtime input and graph output tensor IDs.
    ///
    /// Module parameters and other initialized constants should not be listed as runtime inputs;
    /// their retained tensor values remain available in [`CapturedGraph::values`].
    /// The active session is closed before this returns; subsequent operations through tensors or
    /// the capture device are rejected even if the closure has not returned yet.
    pub fn complete(
        self,
        inputs: impl IntoIterator<Item = TensorId>,
        outputs: impl IntoIterator<Item = TensorId>,
    ) -> CompletedCaptureScope {
        let inputs = inputs.into_iter().collect();
        let outputs = outputs.into_iter().collect();
        self.session.state.lock().close();
        CompletedCaptureScope { inputs, outputs }
    }
}

/// Completion token required from a [`CaptureDevice::capture_scope`] closure.
///
/// This value can only be created by [`CaptureScope::complete`]. It carries the explicit graph
/// boundaries back to the capture device for validation and finalization.
#[derive(Debug)]
pub struct CompletedCaptureScope {
    inputs: Vec<TensorId>,
    outputs: Vec<TensorId>,
}

#[derive(Debug, Default)]
struct CaptureSession {
    state: Mutex<CaptureState>,
}

/// Owns the session client and its router binding for one capture scope.
///
/// The registration makes every router lookup for the device resolve to `client`. Dropping this
/// guard removes that lookup entry so the reusable device can be bound to a fresh session later.
struct CaptureScopeGuard {
    client: CaptureClient,
    // Owns the global router entry until completion, error, or panic unwinding.
    _registration: RouterClientRegistration<CaptureChannel>,
}

impl CaptureScopeGuard {
    fn complete(self, scope: CompletedCaptureScope) -> Result<CapturedGraph, CaptureError> {
        let mut state = self.client.state().lock();
        let inputs: Vec<_> = scope
            .inputs
            .into_iter()
            .map(|id| state.resolve_alias(id))
            .collect();
        let outputs: Vec<_> = scope
            .outputs
            .into_iter()
            .map(|id| state.resolve_alias(id))
            .collect();
        let known: HashSet<_> = state
            .operations
            .iter()
            .flat_map(OperationIr::nodes)
            .map(|tensor| tensor.id)
            .chain(state.values.keys().copied())
            .collect();
        for (boundary, ids) in [("input", &inputs), ("output", &outputs)] {
            let mut unique = HashSet::new();
            if let Some(id) = ids.iter().find(|id| !unique.insert(**id)) {
                return Err(CaptureError::DuplicateBoundary {
                    boundary,
                    tensor: *id,
                });
            }
            if let Some(id) = ids.iter().find(|id| !known.contains(*id)) {
                return Err(CaptureError::UnknownBoundary {
                    boundary,
                    tensor: *id,
                });
            }
        }

        let inferred = GraphIr::classify(&state.operations);
        for &id in &inputs {
            if !inferred.inputs.contains(&id) && !state.values.contains_key(&id) {
                return Err(CaptureError::InvalidInput { tensor: id });
            }
        }
        for &id in &inferred.inputs {
            if !inputs.contains(&id) && !state.values.contains_key(&id) {
                return Err(CaptureError::UndeclaredInput { tensor: id });
            }
        }
        for &id in &outputs {
            // Returning a graph input directly is valid. A computed output, however, must not have
            // been consumed in place or explicitly dropped according to boundary classification.
            let consumed = state.operations.iter().any(|operation| {
                matches!(operation, OperationIr::Drop(tensor) if tensor.id == id)
                    || operation
                        .nodes()
                        .into_iter()
                        .any(|tensor| tensor.id == id && tensor.status == TensorStatus::ReadWrite)
            });
            if consumed || (!inferred.inputs.contains(&id) && !inferred.outputs.contains(&id)) {
                return Err(CaptureError::InvalidOutput { tensor: id });
            }
        }
        let operations = core::mem::take(&mut state.operations);
        // Tensor handles can outlive the scope (for example, module running states shared across
        // clones). Keep initialized data in the closed session so those handles can still be
        // materialized onto another device without allowing any further graph operations.
        let values = state.values.clone();
        let captured = CapturedGraph {
            graph: GraphIr {
                operations,
                inputs,
                outputs,
            },
            values,
        };
        drop(state);
        Ok(captured)
    }
}

impl Drop for CaptureScopeGuard {
    fn drop(&mut self) {
        // Also close scopes that unwind before returning a completion token. Tensor handles can
        // retain a clone of the session client after the router registration itself is removed.
        self.client.state().lock().close();
    }
}

/// Error returned while finalizing a capture.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CaptureError {
    /// Another capture scope is already active on the device.
    AlreadyActive,
    /// Capture was requested through a user-facing device using another backend.
    InvalidDevice,
    /// A declared boundary tensor was never initialized or referenced.
    UnknownBoundary {
        /// Whether this is an input or output boundary.
        boundary: &'static str,
        /// Unknown tensor ID.
        tensor: TensorId,
    },
    /// A tensor was declared more than once at the same boundary.
    DuplicateBoundary {
        /// Whether this is an input or output boundary.
        boundary: &'static str,
        /// Repeated tensor ID.
        tensor: TensorId,
    },
    /// A tensor dependency was neither initialized nor declared as a runtime input.
    UndeclaredInput {
        /// Tensor read by the graph without an available source.
        tensor: TensorId,
    },
    /// A declared input is produced within the graph rather than supplied externally.
    InvalidInput {
        /// Invalid input tensor ID.
        tensor: TensorId,
    },
    /// A declared output is consumed in place or otherwise cannot survive the graph.
    InvalidOutput {
        /// Invalid output tensor ID.
        tensor: TensorId,
    },
}

impl core::fmt::Display for CaptureError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::AlreadyActive => write!(f, "a capture scope is already active on this device"),
            Self::InvalidDevice => write!(f, "capture scope requires a capture device"),
            Self::UnknownBoundary { boundary, tensor } => {
                write!(f, "unknown graph {boundary} tensor {tensor}")
            }
            Self::DuplicateBoundary { boundary, tensor } => {
                write!(f, "duplicate graph {boundary} tensor {tensor}")
            }
            Self::UndeclaredInput { tensor } => {
                write!(f, "graph reads undeclared input tensor {tensor}")
            }
            Self::InvalidInput { tensor } => {
                write!(
                    f,
                    "graph input tensor {tensor} is produced within the graph"
                )
            }
            Self::InvalidOutput { tensor } => {
                write!(f, "graph output tensor {tensor} does not survive the graph")
            }
        }
    }
}

/// Router channel selecting the non-executing capture client.
///
/// Capture clients are scope-specific: [`CaptureDevice::capture_scope`] creates the client and
/// installs it with `register_scoped_client` before any tensor operations run. An unscoped client
/// would have no lifecycle owner or completed graph boundary, so it is intentionally unsupported.
#[derive(Clone)]
pub struct CaptureChannel;

impl RouterChannel for CaptureChannel {
    type Device = CaptureDevice;
    type Bridge = CaptureBridge;
    type Client = CaptureClient;

    fn name(_device: &Self::Device) -> String {
        "capture".into()
    }

    fn init_client(_device: &Self::Device) -> Self::Client {
        // `get_client` reaches this only when no capture scope registered its client first.
        panic!("capture tensor operations must run inside CaptureDevice::capture_scope")
    }

    fn get_tensor_handle(tensor: &TensorIr, client: &Self::Client) -> TensorData {
        client
            .state()
            .lock()
            .value(tensor.id)
            .unwrap_or_else(|| panic!("capture tensor {} has no initialized value", tensor.id))
    }

    fn register_tensor(
        client: &Self::Client,
        handle: TensorData,
        _shape: Shape,
        _dtype: DType,
    ) -> RouterTensor<Self::Client> {
        client.register_tensor_data(handle)
    }
}

/// Bridge for materialized values between independent capture devices.
///
/// Only initialized tensors have handles in the capture backend, so values reaching this bridge
/// are concrete and can safely initialize a tensor in another capture scope. Computed tensors have
/// no materialized handle and are rejected by [`CaptureChannel::get_tensor_handle`] first.
pub struct CaptureBridge;

impl MultiBackendBridge for CaptureBridge {
    type TensorHandle = TensorData;
    type Device = CaptureDevice;

    fn change_backend_float(
        tensor: TensorData,
        _shape: Shape,
        _target: &Self::Device,
    ) -> TensorData {
        tensor
    }
    fn change_backend_int(tensor: TensorData, _shape: Shape, _target: &Self::Device) -> TensorData {
        tensor
    }
    fn change_backend_bool(
        tensor: TensorData,
        _shape: Shape,
        _target: &Self::Device,
    ) -> TensorData {
        tensor
    }
}

#[derive(Debug)]
struct CaptureState {
    operations: Vec<OperationIr>,
    values: BTreeMap<TensorId, TensorData>,
    graphs: HashMap<GraphId, Graph>,
    aliases: HashMap<TensorId, TensorId>,
    closed: bool,
}

impl Default for CaptureState {
    fn default() -> Self {
        Self {
            operations: Vec::new(),
            values: BTreeMap::new(),
            graphs: HashMap::new(),
            aliases: HashMap::new(),
            closed: false,
        }
    }
}

impl CaptureState {
    fn close(&mut self) {
        self.closed = true;
    }

    fn assert_open(&self) {
        assert!(!self.closed, "capture scope is already complete");
    }

    /// Resolve a fusion alias to the tensor ID that owns the computation or initializer.
    fn resolve_alias(&self, mut id: TensorId) -> TensorId {
        while let Some(source) = self.aliases.get(&id) {
            id = *source;
        }
        id
    }

    fn value(&self, id: TensorId) -> Option<TensorData> {
        self.values.get(&self.resolve_alias(id)).cloned()
    }

    /// Record a computation after replacing runtime-only fusion aliases with graph dependencies.
    ///
    /// Router `Drop` operations only manage handle lifetimes. Excluding them keeps the captured
    /// computation independent of incidental Rust cloning and ensures closure-local boundary
    /// tensors may be dropped before the scope is finalized.
    fn register_op(&mut self, mut op: OperationIr) {
        if matches!(op, OperationIr::Drop(_)) {
            return;
        }
        self.assert_open();
        op.visit_mut(&mut AliasVisitor {
            aliases: &self.aliases,
        });
        self.operations.push(op);
    }
}

struct AliasVisitor<'a> {
    aliases: &'a HashMap<TensorId, TensorId>,
}

impl IrVisitorMut for AliasVisitor<'_> {
    fn visit_tensor_mut(&mut self, tensor: &mut TensorIr) {
        while let Some(source) = self.aliases.get(&tensor.id) {
            tensor.id = *source;
        }
    }
}

/// Client that records router operations and locally retains initialized values.
#[derive(Clone)]
pub struct CaptureClient {
    device: CaptureDevice,
    session: Arc<CaptureSession>,
}

impl CaptureClient {
    fn new(device: CaptureDevice, session: Arc<CaptureSession>) -> Self {
        Self { device, session }
    }

    fn state(&self) -> &Mutex<CaptureState> {
        &self.session.state
    }
}

impl RouterClient for CaptureClient {
    type Device = CaptureDevice;

    fn register_op(&self, op: OperationIr) {
        self.state().lock().register_op(op);
    }

    fn read_tensor_async(&self, tensor: TensorIr) -> DynFut<Result<TensorData, ExecutionError>> {
        let value = self.state().lock().value(tensor.id);
        Box::pin(async move {
            value.ok_or_else(|| ExecutionError::WithContext {
                reason: format!("captured tensor {} has no concrete value", tensor.id),
            })
        })
    }

    fn sync(&self) -> Result<(), ExecutionError> {
        Ok(())
    }
    fn flush(&self) {}

    fn create_empty_handle(&self) -> TensorId {
        self.state().lock().assert_open();
        TensorId::new(TENSOR_COUNTER.fetch_add(1, Ordering::Relaxed))
    }

    fn register_tensor_data(&self, data: TensorData) -> RouterTensor<Self> {
        let mut state = self.state().lock();
        state.assert_open();
        let id = TensorId::new(TENSOR_COUNTER.fetch_add(1, Ordering::Relaxed));
        let shape = data.shape().clone();
        let dtype = data.dtype();
        state.values.insert(id, data);
        drop(state);
        RouterTensor::new(id, shape, dtype, self.clone())
    }

    fn device(&self) -> Self::Device {
        self.device
    }

    fn seed(&self, _seed: u64) {
        panic!("seeding is not supported during graph capture")
    }

    fn dtype_usage(&self, dtype: DType) -> DTypeUsageSet {
        match dtype {
            // Capture records these operations without executing dtype-specific kernels. The
            // router's quantized operations are not implemented yet, so quantized tensors remain
            // the only dtype family that capture cannot represent through the backend API.
            DType::QFloat(_) => DTypeUsageSet::empty(),
            _ => DTypeUsage::general(),
        }
    }

    fn register_and_execute_graph(
        &self,
        graph_id: GraphId,
        relative_graph: Vec<OperationIr>,
        bindings: GraphBindings,
    ) {
        let graph = Graph::new(relative_graph);
        let bound = graph.bind(bindings);
        let mut state = self.state().lock();
        state.graphs.insert(graph_id, graph);
        for operation in bound.operations {
            state.register_op(operation);
        }
    }

    fn execute_graph(&self, graph_id: GraphId, bindings: GraphBindings) {
        let graph = self
            .state()
            .lock()
            .graphs
            .get(&graph_id)
            .cloned()
            .unwrap_or_else(|| panic!("capture graph {graph_id:?} was not registered"));
        let bound = graph.bind(bindings);
        let mut state = self.state().lock();
        for operation in bound.operations {
            state.register_op(operation);
        }
    }

    fn register_alias(&self, new_id: TensorId, src_id: TensorId) {
        let mut state = self.state().lock();
        state.assert_open();
        let source = state.resolve_alias(src_id);
        if source != new_id {
            state.aliases.insert(new_id, source);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;
    #[cfg(feature = "std")]
    use burn_backend::Backend;
    use burn_backend::ops::FloatTensorOps;
    use burn_ir::{CustomOpIr, ScalarIr};
    use burn_router::get_client;

    fn tensor(id: u64, shape: impl Into<Shape>) -> TensorIr {
        TensorIr::uninit(TensorId::new(id), shape.into(), DType::F32)
    }

    #[test]
    fn backend_router_operations_are_captured() {
        let device = CaptureDevice::default();
        let captured = device
            .capture_scope(|scope| {
                let lhs = CaptureBackend::float_from_data(TensorData::from([1.0f32, 2.0]), &device);
                let rhs = CaptureBackend::float_from_data(TensorData::from([3.0f32, 4.0]), &device);
                let lhs_id = lhs.id();
                let rhs_id = rhs.id();
                let output = CaptureBackend::float_add(lhs, rhs);
                let output_id = output.id();

                scope.complete([lhs_id, rhs_id], [output_id])
            })
            .unwrap();
        assert_eq!(captured.values.len(), 2);
        assert!(
            captured.graph.operations.iter().any(|operation| matches!(
                operation,
                OperationIr::NumericFloat(_, burn_ir::NumericOperationIr::Add(_))
            )),
            "captured operations: {:?}",
            captured.graph.operations
        );
        assert!(
            captured
                .graph
                .operations
                .iter()
                .all(|operation| !matches!(operation, OperationIr::Drop(_))),
            "tensor lifetime operations must not be part of a computation graph"
        );
    }

    #[test]
    fn captures_operations_values_and_explicit_boundaries() {
        let device = CaptureDevice::default();
        let captured = device
            .capture_scope(|scope| {
                let client = get_client::<CaptureChannel>(&device);
                let input = client.register_tensor_data(TensorData::from([1.0f32, 2.0]));
                let input_id = input.id();
                let input_ir = input.into_ir();
                let output_id = client.create_empty_handle();
                let output_ir = TensorIr::uninit(output_id, Shape::new([2]), DType::F32);
                client.register_op(OperationIr::Custom(CustomOpIr::new(
                    "identity",
                    &[input_ir],
                    &[output_ir],
                )));

                scope.complete([input_id], [output_id])
            })
            .unwrap();
        let input_id = captured.graph.inputs[0];
        let output_id = captured.graph.outputs[0];
        assert_eq!(captured.graph.inputs, [input_id]);
        assert_eq!(captured.graph.outputs, [output_id]);
        assert_eq!(captured.graph.operations.len(), 1);
        assert_eq!(
            captured.values.get(&input_id),
            Some(&TensorData::from([1.0f32, 2.0]))
        );
    }

    #[cfg(feature = "std")]
    #[test]
    fn escaped_tensors_cannot_record_after_their_scope_completes() {
        let device = CaptureDevice::default();
        let mut escaped_output = None;
        let first = device
            .capture_scope(|scope| {
                let lhs = CaptureBackend::float_from_data(TensorData::from([1.0f32]), &device);
                let rhs = CaptureBackend::float_from_data(TensorData::from([2.0f32]), &device);
                let lhs_id = lhs.id();
                let rhs_id = rhs.id();
                let output = CaptureBackend::float_add(lhs, rhs);
                let output_id = output.id();
                escaped_output = Some(output);

                scope.complete([lhs_id, rhs_id], [output_id])
            })
            .unwrap();
        assert!(!first.graph.operations.is_empty());

        let next = device
            .capture_scope(|scope| {
                let fresh = CaptureBackend::float_from_data(TensorData::from([3.0f32]), &device);
                let fresh_id = fresh.id();
                let stale = escaped_output.take().unwrap();
                let operation = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    CaptureBackend::float_add(stale, fresh)
                }));

                assert!(operation.is_err());
                scope.complete([fresh_id], [])
            })
            .unwrap();
        assert_eq!(next.graph.operations.len(), 1);
        assert!(matches!(next.graph.operations[0], OperationIr::Init(_)));
    }

    #[test]
    fn initialized_tensor_can_move_from_completed_scope_to_new_device() {
        let first_device = CaptureDevice::default();
        let second_device = CaptureDevice::default();
        let mut escaped = None;

        first_device
            .capture_scope(|scope| {
                let tensor =
                    CaptureBackend::float_from_data(TensorData::from([1.0f32, 2.0]), &first_device);
                let tensor_id = tensor.id();
                escaped = Some(tensor);
                scope.complete([tensor_id], [])
            })
            .unwrap();

        let captured = second_device
            .capture_scope(|scope| {
                let tensor =
                    CaptureBackend::float_to_device(escaped.take().unwrap(), &second_device);
                let output = CaptureBackend::float_neg(tensor);
                let output_id = output.id();
                scope.complete([], [output_id])
            })
            .unwrap();

        assert_eq!(captured.values.len(), 1);
        assert!(matches!(
            captured.graph.operations.last(),
            Some(OperationIr::NumericFloat(
                _,
                burn_ir::NumericOperationIr::Neg(_)
            ))
        ));
    }

    #[cfg(feature = "std")]
    #[test]
    fn completing_scope_immediately_rejects_more_operations() {
        let device = CaptureDevice::default();
        let captured = device
            .capture_scope(|scope| {
                let client = get_client::<CaptureChannel>(&device);
                let completed = scope.complete([], []);
                let tensor_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    CaptureBackend::float_from_data(TensorData::from([1.0f32]), &device)
                }));
                let handle = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    client.create_empty_handle()
                }));
                let operation = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                    client.register_op(OperationIr::Custom(CustomOpIr::new(
                        "late",
                        &[],
                        &[tensor(99_000, [1])],
                    )))
                }));

                assert!(tensor_result.is_err());
                assert!(handle.is_err());
                assert!(operation.is_err());
                completed
            })
            .unwrap();

        assert!(captured.graph.operations.is_empty());
    }

    #[test]
    fn capture_device_can_be_reused_for_sequential_scopes() {
        let device = CaptureDevice::default();
        let first = device
            .capture_scope(|scope| scope.complete([], []))
            .unwrap();
        let second = device
            .capture_scope(|scope| scope.complete([], []))
            .unwrap();

        assert!(first.graph.operations.is_empty());
        assert!(second.graph.operations.is_empty());
    }

    #[cfg(feature = "std")]
    #[test]
    fn tensor_operations_require_an_active_scope() {
        let device = CaptureDevice::default();
        let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _ = CaptureBackend::float_from_data(TensorData::from([1.0f32]), &device);
        }));

        assert!(panic.is_err());
        assert!(device.capture_scope(|scope| scope.complete([], [])).is_ok());
    }

    #[test]
    fn overlapping_scopes_on_one_device_are_rejected() {
        let device = CaptureDevice::default();
        let outer = device.capture_scope(|scope| {
            let inner = device.capture_scope(|scope| scope.complete([], []));
            assert!(matches!(inner, Err(CaptureError::AlreadyActive)));
            scope.complete([], [])
        });

        assert!(outer.is_ok());
    }

    #[test]
    fn separate_devices_support_overlapping_scopes() {
        let first = CaptureDevice::default();
        let second = CaptureDevice::default();

        let outer = first.capture_scope(|outer| {
            let inner = second
                .capture_scope(|inner| inner.complete([], []))
                .unwrap();
            assert!(inner.graph.operations.is_empty());
            outer.complete([], [])
        });

        assert!(outer.is_ok());
    }

    #[cfg(feature = "std")]
    #[test]
    fn panicking_scope_releases_its_router_registration() {
        let device = CaptureDevice::default();
        let mut escaped = None;
        let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            let _ = device.capture_scope(|_| -> CompletedCaptureScope {
                escaped = Some(CaptureBackend::float_from_data(
                    TensorData::from([1.0f32]),
                    &device,
                ));
                panic!("capture failed")
            });
        }));

        assert!(panic.is_err());
        let stale = escaped.unwrap();
        let operation = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            CaptureBackend::float_neg(stale)
        }));
        assert!(operation.is_err());
        assert!(device.capture_scope(|scope| scope.complete([], [])).is_ok());
    }

    #[test]
    fn capture_devices_have_distinct_monotonic_ids() {
        let first = CaptureDevice::default();
        let second = CaptureDevice::default();

        assert_ne!(first, second);
        assert!(first.to_id().index_id < second.to_id().index_id);
    }

    #[test]
    fn capture_device_id_round_trips() {
        let device = CaptureDevice::default();
        let restored = CaptureDevice::from_id(device.to_id());
        assert_eq!(restored, device);
        assert_eq!(
            restored.to_id().type_id,
            u16::from(RouterDeviceType::Capture)
        );
    }

    #[test]
    fn invalid_boundary_is_reported() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            let _tensor = client.register_tensor_data(TensorData::from([3i64]));
            scope.complete([TensorId::new(u64::MAX)], [])
        });

        assert!(matches!(
            captured,
            Err(CaptureError::UnknownBoundary { .. })
        ));
    }

    #[test]
    fn duplicate_boundary_is_reported() {
        let device = CaptureDevice::default();
        let mut duplicate = None;
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            let tensor = client.register_tensor_data(TensorData::from([3i64]));
            let id = tensor.id();
            duplicate = Some(id);
            scope.complete([id, id], [])
        });

        assert!(matches!(
            captured,
            Err(CaptureError::DuplicateBoundary { tensor, .. }) if Some(tensor) == duplicate
        ));
    }

    #[test]
    fn dangling_graph_input_is_reported() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            let missing = tensor(90_000, [1]);
            let output_id = client.create_empty_handle();
            client.register_op(OperationIr::Custom(CustomOpIr::new(
                "dangling",
                &[missing],
                &[tensor(output_id.value(), [1])],
            )));
            scope.complete([], [output_id])
        });

        assert!(matches!(
            captured,
            Err(CaptureError::UndeclaredInput { .. })
        ));
    }

    #[test]
    fn graph_produced_tensor_cannot_be_declared_as_input() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            let input = client.register_tensor_data(TensorData::from([1.0f32]));
            let output_id = client.create_empty_handle();
            client.register_op(OperationIr::Custom(CustomOpIr::new(
                "produce",
                &[input.into_ir()],
                &[tensor(output_id.value(), [1])],
            )));
            scope.complete([output_id], [output_id])
        });

        assert!(matches!(captured, Err(CaptureError::InvalidInput { .. })));
    }

    #[test]
    fn read_write_tensor_cannot_be_declared_as_output() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            let input = client.register_tensor_data(TensorData::from([1.0f32]));
            let input_id = input.id();
            let mut input = input.into_ir();
            input.status = TensorStatus::ReadWrite;
            let output_id = client.create_empty_handle();
            client.register_op(OperationIr::Custom(CustomOpIr::new(
                "consume_in_place",
                &[input],
                &[tensor(output_id.value(), [1])],
            )));
            scope.complete([input_id], [input_id])
        });

        assert!(matches!(captured, Err(CaptureError::InvalidOutput { .. })));
    }

    #[test]
    fn capture_supports_every_non_quantized_dtype() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            for dtype in [
                DType::F64,
                DType::F32,
                DType::Flex32,
                DType::F16,
                DType::BF16,
                DType::I64,
                DType::I32,
                DType::I16,
                DType::I8,
                DType::U64,
                DType::U32,
                DType::U16,
                DType::U8,
                DType::Bool(BoolStore::Native),
                DType::Bool(BoolStore::U8),
                DType::Bool(BoolStore::U32),
            ] {
                assert_eq!(
                    client.dtype_usage(dtype),
                    DTypeUsage::general(),
                    "{dtype:?}"
                );
            }
            assert!(
                client
                    .dtype_usage(DType::QFloat(
                        burn_backend::quantization::QuantScheme::default()
                    ))
                    .is_empty()
            );
            scope.complete([], [])
        });

        assert!(captured.is_ok());
    }

    #[cfg(feature = "std")]
    #[test]
    fn seeding_capture_device_is_rejected() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let seeded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                CaptureBackend::seed(&device, 42)
            }));
            assert!(seeded.is_err());
            scope.complete([], [])
        });

        assert!(captured.is_ok());
    }

    #[test]
    fn self_alias_is_ignored() {
        let device = CaptureDevice::default();
        let captured = device.capture_scope(|scope| {
            let client = get_client::<CaptureChannel>(&device);
            let id = client.create_empty_handle();
            client.register_alias(id, id);
            assert_eq!(client.state().lock().resolve_alias(id), id);
            scope.complete([], [])
        });

        assert!(captured.is_ok());
    }

    #[test]
    fn cached_graph_invocations_are_bound_and_recorded() {
        let device = CaptureDevice::default();
        let input_1 = TensorId::new(10_000);
        let output_1 = TensorId::new(10_001);
        let input_2 = TensorId::new(20_000);
        let output_2 = TensorId::new(20_001);
        let captured = device
            .capture_scope(|scope| {
                let client = get_client::<CaptureChannel>(&device);
                let relative_input = tensor(0, Shape::new([0]));
                let relative_intermediate = tensor(1, Shape::new([0]));
                let relative_output = tensor(2, Shape::new([0]));
                let relative_graph = vec![
                    OperationIr::Custom(CustomOpIr::new(
                        "first",
                        core::slice::from_ref(&relative_input),
                        core::slice::from_ref(&relative_intermediate),
                    )),
                    OperationIr::Custom(CustomOpIr::with_scalars(
                        "second",
                        core::slice::from_ref(&relative_intermediate),
                        core::slice::from_ref(&relative_output),
                        vec![ScalarIr::UInt(0)],
                    )),
                ];
                let graph_id = GraphId(7);

                client.register_and_execute_graph(
                    graph_id,
                    relative_graph,
                    GraphBindings {
                        tensors: vec![(relative_input.id, input_1), (relative_output.id, output_1)],
                        shapes: vec![3],
                        scalars: vec![ScalarIr::Float(1.5)],
                        ranges: vec![],
                    },
                );
                client.execute_graph(
                    graph_id,
                    GraphBindings {
                        tensors: vec![(relative_input.id, input_2), (relative_output.id, output_2)],
                        shapes: vec![5],
                        scalars: vec![ScalarIr::Float(2.5)],
                        ranges: vec![],
                    },
                );

                scope.complete([input_1, input_2], [output_1, output_2])
            })
            .unwrap();
        assert_eq!(captured.graph.operations.len(), 4);

        let OperationIr::Custom(first_1) = &captured.graph.operations[0] else {
            panic!("expected first custom operation")
        };
        let OperationIr::Custom(second_1) = &captured.graph.operations[1] else {
            panic!("expected second custom operation")
        };
        let OperationIr::Custom(first_2) = &captured.graph.operations[2] else {
            panic!("expected first custom operation")
        };
        let OperationIr::Custom(second_2) = &captured.graph.operations[3] else {
            panic!("expected second custom operation")
        };
        assert_eq!(first_1.inputs[0].id, input_1);
        assert_eq!(second_1.outputs[0].id, output_1);
        assert_eq!(first_1.outputs[0].id, second_1.inputs[0].id);
        assert_eq!(first_1.inputs[0].shape, Shape::new([3]));
        assert_eq!(second_1.scalars, [ScalarIr::Float(1.5)]);
        assert_eq!(first_2.inputs[0].id, input_2);
        assert_eq!(second_2.outputs[0].id, output_2);
        assert_eq!(first_2.outputs[0].id, second_2.inputs[0].id);
        assert_ne!(first_1.outputs[0].id, first_2.outputs[0].id);
        assert_eq!(first_2.inputs[0].shape, Shape::new([5]));
        assert_eq!(second_2.scalars, [ScalarIr::Float(2.5)]);
    }

    #[test]
    fn computed_fusion_aliases_resolve_to_their_source() {
        let device = CaptureDevice::default();
        let mut source = None;
        let mut alias = None;
        let captured = device
            .capture_scope(|scope| {
                let client = get_client::<CaptureChannel>(&device);
                let input = client.register_tensor_data(TensorData::from([1.0f32, 2.0]));
                let input_id = input.id();
                let source_id = client.create_empty_handle();
                let alias_id = client.create_empty_handle();
                let output_id = client.create_empty_handle();

                client.register_op(OperationIr::Custom(CustomOpIr::new(
                    "produce",
                    &[input.into_ir()],
                    &[TensorIr::uninit(source_id, Shape::new([2]), DType::F32)],
                )));
                client.register_alias(alias_id, source_id);
                client.register_op(OperationIr::Custom(CustomOpIr::new(
                    "consume",
                    &[TensorIr::uninit(alias_id, Shape::new([2]), DType::F32)],
                    &[TensorIr::uninit(output_id, Shape::new([2]), DType::F32)],
                )));
                source = Some(source_id);
                alias = Some(alias_id);

                scope.complete([input_id], [output_id])
            })
            .unwrap();
        let source_id = source.unwrap();
        let alias_id = alias.unwrap();
        let OperationIr::Custom(consume) = &captured.graph.operations[1] else {
            panic!("expected alias consumer")
        };
        assert_eq!(consume.inputs[0].id, source_id);
        assert!(!captured.graph.inputs.contains(&alias_id));
    }

    #[test]
    fn initialized_aliases_share_the_source_value_and_boundary() {
        let device = CaptureDevice::default();
        let mut source = None;
        let captured = device
            .capture_scope(|scope| {
                let client = get_client::<CaptureChannel>(&device);
                let tensor = client.register_tensor_data(TensorData::from([4i64]));
                let source_id = tensor.id();
                let alias_id = client.create_empty_handle();
                client.register_alias(alias_id, source_id);

                assert_eq!(
                    client.state().lock().value(alias_id),
                    Some(TensorData::from([4i64]))
                );
                source = Some(source_id);
                scope.complete([alias_id], [])
            })
            .unwrap();
        let source_id = source.unwrap();
        assert_eq!(captured.graph.inputs, [source_id]);
        assert!(captured.values.contains_key(&source_id));
    }
}