burn-dispatch 0.22.0-pre.4

Backend dispatch 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
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(cube_backend)]
use burn_backend::cubecl::{Device as CubeDevice, RuntimeId};

// The cubecl runtimes — `cpu` among them — enumerate through `cube_devices` rather than a
// `vec![]` literal, so only the backends that still list a fixed device need this.
#[cfg(any(feature = "ndarray", feature = "flex"))]
use alloc::vec;

#[cfg(feature = "autodiff")]
use burn_backend::distributed::{DistributedParamId, DistributedParams};
use burn_backend::{
    AutodiffBackend, Backend, BackendGraph, BackendTypes, DType, ExecutionError,
    InstallMemoryPoolsError, MemoryPoolLayout, MemoryPoolUsage, ProfileDuration, ProfileOptions,
    ProfileToken, SlicedPoolReport,
};

/// A captured graph from one of the dispatched backends (see
/// [`BackendTypes::GraphPrimitive`]).
///
/// Like [`DispatchTensorKind`], one variant per enabled backend: the graph is
/// captured by, and can only replay on, the backend it was recorded on.
#[derive(Debug, Clone)]
pub enum DispatchGraph {
    #[cfg(not(backend_enabled))]
    #[doc(hidden)]
    Unavailable(crate::NoBackend),
    /// A graph captured on the [cubecl backend](Cube).
    #[cfg(cube_backend)]
    Cube(BackendGraph<Cube>),

    /// A graph captured on the [Flex backend](Flex).
    #[cfg(feature = "flex")]
    Flex(BackendGraph<Flex>),

    /// A graph captured on the [NdArray backend](NdArray).
    #[cfg(feature = "ndarray")]
    NdArray(BackendGraph<NdArray>),

    /// A graph captured on the [LibTorch backend](LibTorch).
    #[cfg(feature = "tch")]
    LibTorch(BackendGraph<LibTorch>),

    /// A graph captured on the [Remote backend](Remote).
    #[cfg(feature = "remote")]
    Remote(BackendGraph<Remote>),
    /// A graph captured by the non-executing capture backend.
    #[cfg(feature = "capture")]
    Capture(BackendGraph<Capture>),
}

/// The error returned when a graph operation cannot be dispatched.
fn graph_dispatch_err(reason: alloc::string::String) -> ExecutionError {
    ExecutionError::WithContext { reason }
}

/// Match arm generator for [`Backend::graph_stop_capture`] on [`Dispatch`]:
/// each backend's captured graph is wrapped in its [`DispatchGraph`] variant.
macro_rules! graph_stop_capture_arms {
    ($device:expr; $([$Backend:ident, $cfg:meta]),*) => {
        match $device {
            $(
                #[cfg($cfg)]
                $crate::DispatchDevice::$Backend(device) => {
                    <$crate::backends::$Backend as Backend>::graph_stop_capture(device)
                        .map(DispatchGraph::$Backend)
                }
            )*
            #[allow(unreachable_patterns)]
            other => Err(graph_dispatch_err(format!(
                "Graph capture is not supported for device {other:?}"
            ))),
        }
    };
}

/// Match arm generator for [`Backend::graph_replay`] on [`Dispatch`]: the graph
/// variant must match the device's backend, since a graph only replays on the
/// backend that captured it.
macro_rules! graph_replay_arms {
    ($device:expr, $graph:expr; $([$Backend:ident, $cfg:meta]),*) => {
        match ($device, $graph) {
            $(
                #[cfg($cfg)]
                ($crate::DispatchDevice::$Backend(device), DispatchGraph::$Backend(graph)) => {
                    // Safety: forwarded verbatim from `Dispatch::graph_replay`'s
                    // own contract.
                    unsafe {
                        <$crate::backends::$Backend as Backend>::graph_replay(device, graph)
                    }
                }
            )*
            #[allow(unreachable_patterns)]
            (device, _) => Err(graph_dispatch_err(format!(
                "The graph was not captured on the backend of device {device:?}"
            ))),
        }
    };
}

#[cfg(feature = "autodiff")]
macro_rules! is_tracked_arms {
    ($tensor:expr; $([$Backend:ident, $cfg:meta]),*) => {
        match &$tensor.kind {
            DispatchTensorKind::Autodiff(inner) => match &**inner {
                $(
                    #[cfg($cfg)]
                    DispatchTensorKind::$Backend(tensor) => tensor.as_autodiff().is_tracked(),
                )*
                DispatchTensorKind::Autodiff(_) => {
                    unreachable!("Autodiff should not wrap an autodiff tensor")
                }
                #[allow(unreachable_patterns)]
                _ => false,
            },
            _ => false,
        }
    };
}

#[cfg(all(
    feature = "autodiff",
    any(
        cube_backend,
        feature = "flex",
        feature = "ndarray",
        feature = "tch",
        feature = "remote"
    )
))]
use alloc::boxed::Box;
#[cfg(feature = "autodiff")]
use burn_autodiff::grads::Gradients;

#[cfg(feature = "autodiff")]
use crate::DispatchAutodiffContext;
#[allow(unused)]
use crate::DispatchDeviceId;
#[allow(unused)]
use crate::DispatchTensorKind;
#[cfg(feature = "flex")]
use crate::devices::FlexDevice;
#[cfg(feature = "tch")]
use crate::devices::LibTorchDevice;
#[cfg(feature = "ndarray")]
use crate::devices::NdArrayDevice;
#[cfg(feature = "remote-websocket")]
use crate::devices::RemoteDevice;
use crate::{DispatchDevice, DispatchTensor, backends::*};

/// The main execution backend in Burn.
///
/// [`Dispatch`] acts as a global backend that can manage multiple underlying
/// backends (e.g., `Cpu`, `Cuda`, `Wgpu`, `Metal`, etc.).
/// It is responsible for:
/// - Dispatching tensor operations to the appropriate backend.
/// - Managing cross-backend tensor transfers.
///
/// Essentially, [`Dispatch`] is the single entry point for executing tensor operations
/// in a backend-agnostic way. It allows Burn to provide a unified, global backend
/// for users while still leveraging multiple specialized backends under the hood.
///
/// # Example
///
/// ```ignore
/// use burn::Dispatch;
/// use burn::DispatchDevice;
///
/// // Select the device to execute operations on
/// let device = DispatchDevice::Cube(cubecl::Device::Cuda(Default::default()));
///
/// // Create a tensor using the global backend
/// let t = Tensor::<Dispatch, 2>::zeros([128, 128], &device);
/// ```
#[derive(Debug, Default, Clone)]
pub struct Dispatch;

#[cfg(feature = "autodiff")]
impl Dispatch {
    /// Returns whether an autodiff tensor's node is marked for graph participation.
    #[doc(hidden)]
    pub fn is_tracked(tensor: &DispatchTensor) -> bool {
        backend_list!(is_tracked_arms, tensor)
    }
}

impl BackendTypes for Dispatch {
    type Device = DispatchDevice;

    type FloatTensorPrimitive = DispatchTensor;
    type IntTensorPrimitive = DispatchTensor;
    type BoolTensorPrimitive = DispatchTensor;
    type QuantizedTensorPrimitive = DispatchTensor;

    type GraphPrimitive = DispatchGraph;
}

impl Backend for Dispatch {
    fn name(device: &Self::Device) -> String {
        let inner = dispatch_device!(device, |device| B::name(device));
        format!("dispatch<{inner}>")
    }

    fn seed(device: &Self::Device, seed: u64) {
        dispatch_device!(device, |device| B::seed(device, seed))
    }

    fn sync(device: &Self::Device) -> Result<(), ExecutionError> {
        dispatch_device!(device, |device| B::sync(device))
    }

    fn profile<O: Send + 'static>(
        device: &Self::Device,
        options: ProfileOptions,
        func: impl FnOnce() -> O + Send,
    ) -> Result<(O, ProfileDuration), ExecutionError> {
        dispatch_device!(device, |device| B::profile(device, options, func))
    }

    fn profile_start(device: &Self::Device) -> Result<Option<ProfileToken>, ExecutionError> {
        dispatch_device!(device, |device| B::profile_start(device))
    }

    fn profile_end(
        device: &Self::Device,
        token: ProfileToken,
        options: ProfileOptions,
    ) -> Result<ProfileDuration, ExecutionError> {
        dispatch_device!(device, |device| B::profile_end(device, token, options))
    }

    fn profile_abandon(device: &Self::Device, token: ProfileToken) {
        dispatch_device!(device, |device| B::profile_abandon(device, token))
    }

    fn graph_prepare(device: &Self::Device) -> Result<(), ExecutionError> {
        dispatch_device!(device, |device| B::graph_prepare(device))
    }

    fn graph_start_capture(device: &Self::Device) -> Result<(), ExecutionError> {
        dispatch_device!(device, |device| B::graph_start_capture(device))
    }

    fn graph_stop_capture(device: &Self::Device) -> Result<DispatchGraph, ExecutionError> {
        backend_list!(graph_stop_capture_arms, device)
    }

    unsafe fn graph_replay(
        device: &Self::Device,
        graph: &DispatchGraph,
    ) -> Result<(), ExecutionError> {
        backend_list!(graph_replay_arms, device, graph)
    }

    fn dtype_usage(device: &Self::Device, dtype: DType) -> burn_backend::DTypeUsageSet {
        dispatch_device!(device, |device| B::dtype_usage(device, dtype))
    }

    fn ad_enabled(device: &Self::Device) -> bool {
        match device {
            #[cfg(feature = "autodiff")]
            DispatchDevice::Autodiff(_) => true,
            _ => false,
        }
    }

    fn device_count(type_id: u16) -> usize {
        let (dispatch_id, backend_type_id) = DispatchDevice::decode_type_id(type_id);
        match dispatch_id {
            #[cfg(cube_backend)]
            DispatchDeviceId::Cube => Cube::device_count(backend_type_id),
            #[cfg(feature = "flex")]
            DispatchDeviceId::Flex => Flex::device_count(backend_type_id),
            #[cfg(feature = "ndarray")]
            DispatchDeviceId::NdArray => NdArray::device_count(backend_type_id),
            #[cfg(feature = "tch")]
            DispatchDeviceId::LibTorch => LibTorch::device_count(backend_type_id),
            #[cfg(feature = "remote")]
            DispatchDeviceId::Remote => Remote::device_count(backend_type_id),
            #[cfg(feature = "capture")]
            DispatchDeviceId::Capture => Capture::device_count(backend_type_id),
            _ => unreachable!("No backend feature enabled."),
        }
    }

    fn memory_persistent_allocations<
        Output: Send,
        Input: Send,
        Func: Fn(Input) -> Output + Send,
    >(
        device: &Self::Device,
        input: Input,
        func: Func,
    ) -> Output {
        dispatch_device!(device, |device| B::memory_persistent_allocations(
            device, input, func
        ))
    }

    fn memory_cleanup(device: &Self::Device) {
        dispatch_device!(device, |device| B::memory_cleanup(device))
    }

    fn memory_install_pools(
        device: &Self::Device,
        layout: MemoryPoolLayout,
    ) -> Result<(), InstallMemoryPoolsError> {
        dispatch_device!(device, |device| B::memory_install_pools(
            device,
            layout.clone()
        ))
    }

    fn memory_pool_report(device: &Self::Device) -> Option<Vec<SlicedPoolReport>> {
        dispatch_device!(device, |device| B::memory_pool_report(device))
    }

    fn memory_pool_usage(device: &Self::Device) -> Option<MemoryPoolUsage> {
        dispatch_device!(device, |device| B::memory_pool_usage(device))
    }

    fn staging<'a, Iter>(data: Iter, device: &Self::Device)
    where
        Iter: Iterator<Item = &'a mut burn_backend::TensorData>,
    {
        dispatch_device!(device, |device| B::staging(data, device))
    }

    fn supports_dtype(device: &Self::Device, dtype: DType) -> bool {
        dispatch_device!(device, |device| B::supports_dtype(device, dtype))
    }

    fn flush(device: &Self::Device) {
        dispatch_device!(device, |device| B::flush(device))
    }
}

#[cfg(feature = "autodiff")]
fn disable_autodiff_context(context: DispatchAutodiffContext) -> DispatchAutodiffContext {
    assert!(
        matches!(context, DispatchAutodiffContext::Enabled(_)),
        "tensor is already on the inner backend"
    );
    DispatchAutodiffContext::Disabled
}

#[cfg(feature = "autodiff")]
fn enable_autodiff_context(context: DispatchAutodiffContext) -> DispatchAutodiffContext {
    assert_eq!(
        context,
        DispatchAutodiffContext::Disabled,
        "tensor is already associated with autodiff"
    );
    DispatchAutodiffContext::Enabled(crate::GradientCheckpointingStrategy::Disabled)
}

#[cfg(feature = "autodiff")]
// Capture does not support autodiff. Without an execution backend, the dispatch
// arms below only panic, leaving gradient arguments unused and return code unreachable.
#[cfg_attr(
    not(any(
        cube_backend,
        feature = "flex",
        feature = "ndarray",
        feature = "tch",
        feature = "remote"
    )),
    allow(unused_variables, unreachable_code)
)]
impl AutodiffBackend for Dispatch {
    type InnerBackend = Dispatch;

    type Gradients = Gradients;

    fn backward(tensor: DispatchTensor) -> Self::Gradients {
        let DispatchTensor { kind, .. } = tensor;

        match kind {
            DispatchTensorKind::Autodiff(tensor) => match *tensor {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => tensor.autodiff().backward(),
                #[cfg(feature = "flex")]
                DispatchTensorKind::Flex(tensor) => tensor.autodiff().backward(),
                #[cfg(feature = "ndarray")]
                DispatchTensorKind::NdArray(tensor) => tensor.autodiff().backward(),
                #[cfg(feature = "tch")]
                DispatchTensorKind::LibTorch(tensor) => tensor.autodiff().backward(),
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => tensor.autodiff().backward(),
                #[cfg(feature = "capture")]
                DispatchTensorKind::Capture(_) => {
                    panic!("Capture tensors do not support autodiff")
                }
                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
            },
            _ => panic!("Requires autodiff tensor."),
        }
    }

    fn grad(tensor: &DispatchTensor, grads: &Self::Gradients) -> Option<DispatchTensor> {
        let DispatchTensor { kind, .. } = tensor;
        let grad: Option<DispatchTensorKind> = match &kind {
            DispatchTensorKind::Autodiff(inner_kind) => match &**inner_kind {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => tensor
                    .as_autodiff()
                    .grad(grads)
                    .map(|t| DispatchTensorKind::Cube(crate::BackendTensor::Float(t))),
                #[cfg(feature = "flex")]
                DispatchTensorKind::Flex(tensor) => tensor
                    .as_autodiff()
                    .grad(grads)
                    .map(|t| DispatchTensorKind::Flex(crate::BackendTensor::Float(t))),
                #[cfg(feature = "ndarray")]
                DispatchTensorKind::NdArray(tensor) => tensor
                    .as_autodiff()
                    .grad(grads)
                    .map(|t| DispatchTensorKind::NdArray(crate::BackendTensor::Float(t))),
                #[cfg(feature = "tch")]
                DispatchTensorKind::LibTorch(tensor) => tensor
                    .as_autodiff()
                    .grad(grads)
                    .map(|t| DispatchTensorKind::LibTorch(crate::BackendTensor::Float(t))),
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => tensor
                    .as_autodiff()
                    .grad(grads)
                    .map(|t| DispatchTensorKind::Remote(crate::BackendTensor::Float(t))),
                #[cfg(feature = "capture")]
                DispatchTensorKind::Capture(_) => {
                    panic!("Capture tensors do not support autodiff")
                }
                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
            },
            _ => panic!("Requires autodiff tensor."),
        };
        grad.map(|kind| DispatchTensor {
            kind,
            autodiff: DispatchAutodiffContext::Disabled,
        })
    }

    fn grad_remove(tensor: &DispatchTensor, grads: &mut Self::Gradients) -> Option<DispatchTensor> {
        let DispatchTensor { kind, .. } = tensor;
        let grad: Option<DispatchTensorKind> = match &kind {
            DispatchTensorKind::Autodiff(inner_kind) => match &**inner_kind {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => tensor
                    .as_autodiff()
                    .grad_remove(grads)
                    .map(|t| DispatchTensorKind::Cube(crate::BackendTensor::Float(t))),
                #[cfg(feature = "flex")]
                DispatchTensorKind::Flex(tensor) => tensor
                    .as_autodiff()
                    .grad_remove(grads)
                    .map(|t| DispatchTensorKind::Flex(crate::BackendTensor::Float(t))),
                #[cfg(feature = "ndarray")]
                DispatchTensorKind::NdArray(tensor) => tensor
                    .as_autodiff()
                    .grad_remove(grads)
                    .map(|t| DispatchTensorKind::NdArray(crate::BackendTensor::Float(t))),
                #[cfg(feature = "tch")]
                DispatchTensorKind::LibTorch(tensor) => tensor
                    .as_autodiff()
                    .grad_remove(grads)
                    .map(|t| DispatchTensorKind::LibTorch(crate::BackendTensor::Float(t))),
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => tensor
                    .as_autodiff()
                    .grad_remove(grads)
                    .map(|t| DispatchTensorKind::Remote(crate::BackendTensor::Float(t))),
                #[cfg(feature = "capture")]
                DispatchTensorKind::Capture(_) => {
                    panic!("Capture tensors do not support autodiff")
                }
                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
            },
            _ => panic!("Requires autodiff tensor."),
        };
        grad.map(|kind| DispatchTensor {
            kind,
            autodiff: DispatchAutodiffContext::Disabled,
        })
    }

    fn grad_replace(tensor: &DispatchTensor, grads: &mut Self::Gradients, grad: DispatchTensor) {
        let DispatchTensor { kind, .. } = tensor;
        let DispatchTensor {
            kind: grad,
            autodiff,
        } = grad;
        assert_eq!(
            autodiff,
            DispatchAutodiffContext::Disabled,
            "replacement gradients must use the inner backend"
        );

        match &kind {
            DispatchTensorKind::Autodiff(inner_kind) => match (&**inner_kind, grad) {
                #[cfg(cube_backend)]
                (DispatchTensorKind::Cube(tensor), DispatchTensorKind::Cube(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                #[cfg(feature = "flex")]
                (DispatchTensorKind::Flex(tensor), DispatchTensorKind::Flex(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                #[cfg(feature = "ndarray")]
                (DispatchTensorKind::NdArray(tensor), DispatchTensorKind::NdArray(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                #[cfg(feature = "remote")]
                (DispatchTensorKind::Remote(tensor), DispatchTensorKind::Remote(grad)) => {
                    tensor.as_autodiff().grad_replace(grads, grad.float())
                }
                (DispatchTensorKind::Autodiff(_), _) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
                // TODO: distributed message?
                (t, g) => panic!(
                    "The provided tensors are not on the same backend. Got backends {t:?} and {g:?}."
                ),
            },
            _ => panic!("Requires autodiff tensor."),
        }
    }

    fn inner(tensor: DispatchTensor) -> DispatchTensor {
        let DispatchTensor { kind, autodiff } = tensor;
        assert!(
            matches!(autodiff, DispatchAutodiffContext::Enabled(_)),
            "Requires autodiff tensor."
        );

        let kind = match kind {
            DispatchTensorKind::Autodiff(inner_kind) => match *inner_kind {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(ref never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => DispatchTensorKind::Cube(
                    crate::BackendTensor::Float(tensor.autodiff().into_primitive()),
                ),
                #[cfg(feature = "flex")]
                DispatchTensorKind::Flex(tensor) => DispatchTensorKind::Flex(
                    crate::BackendTensor::Float(tensor.autodiff().into_primitive()),
                ),
                #[cfg(feature = "ndarray")]
                DispatchTensorKind::NdArray(tensor) => DispatchTensorKind::NdArray(
                    crate::BackendTensor::Float(tensor.autodiff().into_primitive()),
                ),
                #[cfg(feature = "tch")]
                DispatchTensorKind::LibTorch(tensor) => DispatchTensorKind::LibTorch(
                    crate::BackendTensor::Float(tensor.autodiff().into_primitive()),
                ),
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => DispatchTensorKind::Remote(
                    crate::BackendTensor::Float(tensor.autodiff().into_primitive()),
                ),
                #[cfg(feature = "capture")]
                DispatchTensorKind::Capture(_) => {
                    panic!("Capture tensors do not support autodiff")
                }
                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
            },
            _ => panic!("Requires autodiff tensor."),
        };
        DispatchTensor {
            kind,
            autodiff: DispatchAutodiffContext::Disabled,
        }
    }

    fn int_inner(mut tensor: DispatchTensor) -> DispatchTensor {
        tensor.autodiff = disable_autodiff_context(tensor.autodiff);
        tensor
    }

    fn bool_inner(mut tensor: DispatchTensor) -> DispatchTensor {
        tensor.autodiff = disable_autodiff_context(tensor.autodiff);
        tensor
    }

    fn q_inner(mut tensor: DispatchTensor) -> DispatchTensor {
        tensor.autodiff = disable_autodiff_context(tensor.autodiff);
        tensor
    }

    fn from_inner(tensor: DispatchTensor) -> DispatchTensor {
        let DispatchTensor { kind, autodiff } = tensor;
        assert_eq!(
            autodiff,
            DispatchAutodiffContext::Disabled,
            "tensor is already associated with autodiff"
        );

        let kind = match kind {
            #[cfg(cube_backend)]
            DispatchTensorKind::Cube(tensor) => {
                DispatchTensorKind::Autodiff(Box::new(DispatchTensorKind::Cube(
                    crate::BackendTensor::Autodiff(Autodiff::<Cube>::from_inner(tensor.float())),
                )))
            }
            #[cfg(feature = "flex")]
            DispatchTensorKind::Flex(tensor) => {
                DispatchTensorKind::Autodiff(Box::new(DispatchTensorKind::Flex(
                    crate::BackendTensor::Autodiff(Autodiff::<Flex>::from_inner(tensor.float())),
                )))
            }
            #[cfg(feature = "ndarray")]
            DispatchTensorKind::NdArray(tensor) => {
                DispatchTensorKind::Autodiff(Box::new(DispatchTensorKind::NdArray(
                    crate::BackendTensor::Autodiff(Autodiff::<NdArray>::from_inner(tensor.float())),
                )))
            }
            #[cfg(feature = "tch")]
            DispatchTensorKind::LibTorch(tensor) => DispatchTensorKind::Autodiff(Box::new(
                DispatchTensorKind::LibTorch(crate::BackendTensor::Autodiff(
                    Autodiff::<LibTorch>::from_inner(tensor.float()),
                )),
            )),
            #[cfg(feature = "remote")]
            DispatchTensorKind::Remote(tensor) => {
                DispatchTensorKind::Autodiff(Box::new(DispatchTensorKind::Remote(
                    crate::BackendTensor::Autodiff(Autodiff::<Remote>::from_inner(tensor.float())),
                )))
            }
            #[cfg(feature = "capture")]
            DispatchTensorKind::Capture(_) => {
                panic!("Capture tensors do not support autodiff")
            }
            DispatchTensorKind::Autodiff(_) => {
                panic!("Autodiff should not wrap an autodiff tensor.")
            }
        };

        DispatchTensor {
            kind,
            autodiff: DispatchAutodiffContext::Enabled(
                crate::GradientCheckpointingStrategy::Disabled,
            ),
        }
    }

    fn int_from_inner(mut tensor: DispatchTensor) -> DispatchTensor {
        tensor.autodiff = enable_autodiff_context(tensor.autodiff);
        tensor
    }

    fn bool_from_inner(mut tensor: DispatchTensor) -> DispatchTensor {
        tensor.autodiff = enable_autodiff_context(tensor.autodiff);
        tensor
    }

    fn q_from_inner(mut tensor: DispatchTensor) -> DispatchTensor {
        tensor.autodiff = enable_autodiff_context(tensor.autodiff);
        tensor
    }

    // Only the collective-capable backends (Cube/Remote) carry distributed params; in builds
    // without them the match arms cfg out, leaving the bindings unused and the tail unreachable.
    #[allow(unused_variables, unreachable_code)]
    fn set_distributed_params(
        tensor: DispatchTensor,
        param_id: DistributedParamId,
    ) -> DispatchTensor {
        let DispatchTensor { kind, autodiff } = tensor;
        assert!(
            matches!(autodiff, DispatchAutodiffContext::Enabled(_)),
            "Requires autodiff tensor."
        );

        let kind = match kind {
            DispatchTensorKind::Autodiff(inner_kind) => match *inner_kind {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(ref never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => {
                    DispatchTensorKind::Autodiff(Box::new(DispatchTensorKind::Cube(
                        crate::BackendTensor::Autodiff(Autodiff::<Cube>::set_distributed_params(
                            tensor.as_autodiff().clone(),
                            param_id,
                        )),
                    )))
                }
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => {
                    DispatchTensorKind::Autodiff(Box::new(DispatchTensorKind::Remote(
                        crate::BackendTensor::Autodiff(Autodiff::<Remote>::set_distributed_params(
                            tensor.as_autodiff().clone(),
                            param_id,
                        )),
                    )))
                }
                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
                #[allow(unreachable_patterns)]
                other => {
                    panic!("Distributed operations are not supported for tensor kind {other:?}")
                }
            },
            _ => panic!("Requires autodiff tensor."),
        };

        DispatchTensor { kind, autodiff }
    }

    #[allow(unused_variables)]
    fn distributed_params(tensor: &DispatchTensor) -> Option<DistributedParams> {
        let DispatchTensor { kind, autodiff: _ } = tensor;

        match &kind {
            DispatchTensorKind::Autodiff(inner_kind) => match &**inner_kind {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => {
                    Autodiff::<Cube>::distributed_params(tensor.as_autodiff())
                }
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => {
                    Autodiff::<Remote>::distributed_params(tensor.as_autodiff())
                }

                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
                // Backends without distributed support never carry distributed params.
                #[allow(unreachable_patterns)]
                _ => None,
            },
            _ => panic!("Requires autodiff tensor."),
        }
    }

    #[allow(unused_variables)]
    fn is_distributed(tensor: &DispatchTensor) -> bool {
        let DispatchTensor { kind, autodiff: _ } = tensor;

        match &kind {
            DispatchTensorKind::Autodiff(inner_kind) => match &**inner_kind {
                #[cfg(not(backend_enabled))]
                DispatchTensorKind::Unavailable(never) => never.unreachable(),
                #[cfg(cube_backend)]
                DispatchTensorKind::Cube(tensor) => {
                    Autodiff::<Cube>::is_distributed(tensor.as_autodiff())
                }
                #[cfg(feature = "remote")]
                DispatchTensorKind::Remote(tensor) => {
                    Autodiff::<Remote>::is_distributed(tensor.as_autodiff())
                }

                DispatchTensorKind::Autodiff(_) => {
                    panic!("Autodiff should not wrap an autodiff tensor.")
                }
                // Backends without distributed support are never distributed.
                #[allow(unreachable_patterns)]
                _ => false,
            },
            _ => panic!("Requires autodiff tensor."),
        }
    }
}

#[cfg(all(test, feature = "autodiff", feature = "flex"))]
mod autodiff_context_tests {
    use super::*;
    use crate::{DispatchAutodiffContext, DispatchTensorKind, GradientCheckpointingStrategy};
    use alloc::vec;
    use burn_backend::{
        TensorData, TensorMetadata, TensorPrimitive,
        ops::{BoolTensorOps, FloatTensorOps, IntTensorOps, ModuleOps, QTensorOps},
        quantization::QuantScheme,
        tensor::{FloatTensor, IntTensor},
    };
    use burn_backend_extension::backend_dispatch;

    #[backend_dispatch]
    impl Dispatch {
        fn direct_float(tensor: FloatTensor<Self>) -> FloatTensor<Self> {
            tensor
        }

        fn conditional_float_route(
            int: IntTensor<Self>,
            float: Option<FloatTensor<Self>>,
        ) -> IntTensor<Self> {
            // `optional_float_only_uses_autodiff_when_present` exercises both routes: an enabled
            // int alone uses the concrete backend, while a present float selects `Autodiff<B>`.
            assert_eq!(float.is_some(), B::ad_enabled(&int.device()));
            int
        }

        fn optional_routing_tensor(
            first: Option<FloatTensor<Self>>,
            second: Option<FloatTensor<Self>>,
        ) -> FloatTensor<Self> {
            first.or(second).expect("test requires one tensor")
        }

        fn vector_routing_tensor(
            first: Vec<IntTensor<Self>>,
            second: Vec<IntTensor<Self>>,
        ) -> IntTensor<Self> {
            first
                .into_iter()
                .chain(second)
                .next()
                .expect("test requires one tensor")
        }
    }

    fn device(strategy: GradientCheckpointingStrategy) -> DispatchDevice {
        DispatchDevice::autodiff_with_gradient_checkpointing(
            DispatchDevice::Flex(Default::default()),
            strategy,
        )
    }

    fn inner_device() -> DispatchDevice {
        DispatchDevice::Flex(Default::default())
    }

    fn float(values: [f32; 2], device: &DispatchDevice) -> DispatchTensor {
        Dispatch::float_from_data(TensorData::from(values), device)
    }

    fn float_2d(values: [[f32; 2]; 2], device: &DispatchDevice) -> DispatchTensor {
        Dispatch::float_from_data(TensorData::from(values), device)
    }

    fn assert_enabled_float(tensor: &DispatchTensor, strategy: GradientCheckpointingStrategy) {
        assert_eq!(tensor.autodiff, DispatchAutodiffContext::Enabled(strategy));
        assert!(matches!(tensor.kind, DispatchTensorKind::Autodiff(_)));
        let DispatchDevice::Autodiff(device) = tensor.device() else {
            panic!("enabled float should report an autodiff device")
        };
        assert_eq!(device.checkpointing, strategy);
    }

    #[test]
    fn enabled_float_creation_is_untracked_for_both_strategies() {
        for strategy in [
            GradientCheckpointingStrategy::Disabled,
            GradientCheckpointingStrategy::Balanced,
        ] {
            let tensor = float([1.0, 2.0], &device(strategy));
            assert_enabled_float(&tensor, strategy);
            assert!(!Dispatch::float_is_require_grad(&tensor));
        }
    }

    #[test]
    fn enabled_int_bool_and_float_conversions_preserve_association() {
        let strategy = GradientCheckpointingStrategy::Balanced;
        let device = device(strategy);
        let int = Dispatch::int_from_data(TensorData::from([1i32, 2]), &device);
        assert_eq!(int.autodiff, DispatchAutodiffContext::Enabled(strategy));
        assert!(!matches!(int.kind, DispatchTensorKind::Autodiff(_)));
        assert!(matches!(int.device(), DispatchDevice::Autodiff(_)));

        let converted = Dispatch::int_into_float(int, burn_backend::FloatDType::F32);
        assert_enabled_float(&converted, strategy);

        let boolean = Dispatch::bool_from_data(TensorData::from([true, false]), &device);
        assert_eq!(boolean.autodiff, DispatchAutodiffContext::Enabled(strategy));
        assert!(!matches!(boolean.kind, DispatchTensorKind::Autodiff(_)));
        let converted = Dispatch::bool_into_float(boolean, burn_backend::FloatDType::F32);
        assert_enabled_float(&converted, strategy);
    }

    #[test]
    fn uniform_contexts_propagate_through_fixed_arity_and_vector_ops() {
        let strategy = GradientCheckpointingStrategy::Disabled;
        let lhs = float([1.0, 2.0], &device(strategy));
        let rhs = float([3.0, 4.0], &device(strategy));

        let added = Dispatch::float_add(lhs.clone(), rhs.clone());
        assert_enabled_float(&added, strategy);

        let concatenated = Dispatch::float_cat(vec![lhs.clone(), rhs], 0);
        assert_enabled_float(&concatenated, strategy);

        let normalized = Dispatch::layer_norm(
            lhs,
            float([1.0, 1.0], &device(strategy)),
            Some(float([0.0, 0.0], &device(strategy))),
            1e-5,
        );
        assert_enabled_float(&normalized, strategy);
    }

    #[test]
    fn optional_and_vector_routing_candidates_skip_empty_inputs() {
        let strategy = GradientCheckpointingStrategy::Balanced;
        let float = float([1.0, 2.0], &device(strategy));
        let output = Dispatch::optional_routing_tensor(None, Some(float));
        assert_enabled_float(&output, strategy);

        let int = Dispatch::int_from_data(TensorData::from([1i32, 2]), &device(strategy));
        let output = Dispatch::vector_routing_tensor(vec![], vec![int]);
        assert_eq!(output.autodiff, DispatchAutodiffContext::Enabled(strategy));
    }

    #[test]
    fn optional_float_only_uses_autodiff_when_present() {
        let strategy = GradientCheckpointingStrategy::Balanced;
        let int = Dispatch::int_from_data(TensorData::from([1i32, 2]), &device(strategy));
        let output = Dispatch::conditional_float_route(int, None);
        assert_eq!(output.autodiff, DispatchAutodiffContext::Enabled(strategy));

        let int = Dispatch::int_from_data(TensorData::from([1i32, 2]), &inner_device());
        let float = float([1.0, 2.0], &device(strategy));
        let output = Dispatch::conditional_float_route(int, Some(float));
        assert_eq!(output.autodiff, DispatchAutodiffContext::Enabled(strategy));
    }

    #[test]
    #[should_panic(expected = "an enabled float tensor must use an autodiff primitive")]
    fn direct_routing_tensor_validates_its_context() {
        let mut malformed = float([1.0, 2.0], &inner_device());
        malformed.autodiff =
            DispatchAutodiffContext::Enabled(GradientCheckpointingStrategy::Disabled);
        let _ = Dispatch::direct_float(malformed);
    }

    #[test]
    #[should_panic(expected = "Gradient checkpointing strategy mismatch")]
    fn fixed_arity_inputs_reject_mismatched_checkpointing_strategies() {
        let lhs = float([1.0, 2.0], &device(GradientCheckpointingStrategy::Balanced));
        let rhs = float([3.0, 4.0], &device(GradientCheckpointingStrategy::Disabled));
        let _ = Dispatch::float_add(lhs, rhs);
    }

    #[test]
    fn disabled_and_enabled_integer_contexts_merge_in_both_orders() {
        for strategy in [
            GradientCheckpointingStrategy::Disabled,
            GradientCheckpointingStrategy::Balanced,
        ] {
            let lhs = Dispatch::int_from_data(TensorData::from([1i32, 2]), &inner_device());
            let rhs = Dispatch::int_from_data(TensorData::from([3i32, 4]), &device(strategy));
            let output = Dispatch::int_add(lhs, rhs);
            assert_eq!(output.autodiff, DispatchAutodiffContext::Enabled(strategy));

            let lhs = Dispatch::int_from_data(TensorData::from([1i32, 2]), &device(strategy));
            let rhs = Dispatch::int_from_data(TensorData::from([3i32, 4]), &inner_device());
            let output = Dispatch::int_add(lhs, rhs);
            assert_eq!(output.autodiff, DispatchAutodiffContext::Enabled(strategy));
        }
    }

    #[test]
    fn disabled_and_enabled_float_contexts_merge_in_both_orders() {
        for strategy in [
            GradientCheckpointingStrategy::Disabled,
            GradientCheckpointingStrategy::Balanced,
        ] {
            let enabled = float([1.0, 2.0], &device(strategy));
            let disabled = float([3.0, 4.0], &inner_device());

            let output = Dispatch::float_add(enabled, disabled);
            assert_enabled_float(&output, strategy);

            let enabled = float([1.0, 2.0], &device(strategy));
            let disabled = float([3.0, 4.0], &inner_device());
            let output = Dispatch::float_add(disabled, enabled);
            assert_enabled_float(&output, strategy);
        }
    }

    #[test]
    fn disabled_and_enabled_contexts_merge_across_vector_inputs() {
        let strategy = GradientCheckpointingStrategy::Balanced;
        let disabled = float([1.0, 2.0], &inner_device());
        let enabled = float([3.0, 4.0], &device(strategy));

        let output = Dispatch::float_cat(vec![disabled, enabled], 0);
        assert_enabled_float(&output, strategy);
    }

    #[test]
    #[should_panic(expected = "Gradient checkpointing strategy mismatch")]
    fn vector_inputs_reject_mismatched_checkpointing_strategies() {
        let balanced = float([1.0, 2.0], &device(GradientCheckpointingStrategy::Balanced));
        let disabled = float([3.0, 4.0], &device(GradientCheckpointingStrategy::Disabled));
        let _ = Dispatch::float_cat(vec![balanced, disabled], 0);
    }

    #[test]
    #[should_panic(expected = "Gradient checkpointing strategy mismatch")]
    fn q_matmul_rejects_mismatched_checkpointing_strategies() {
        let balanced = Dispatch::quantize_dynamic(
            float([1.0, 2.0], &device(GradientCheckpointingStrategy::Balanced)),
            &QuantScheme::default(),
        );
        let disabled = Dispatch::quantize_dynamic(
            float([3.0, 4.0], &device(GradientCheckpointingStrategy::Disabled)),
            &QuantScheme::default(),
        );
        let _ = Dispatch::q_matmul(
            TensorPrimitive::QFloat(balanced),
            TensorPrimitive::QFloat(disabled),
        );
    }

    #[test]
    fn q_matmul_merges_disabled_and_enabled_contexts() {
        let strategy = GradientCheckpointingStrategy::Balanced;
        let disabled = Dispatch::quantize_dynamic(
            float_2d([[1.0, 2.0], [3.0, 4.0]], &inner_device()),
            &QuantScheme::default(),
        );
        let enabled = Dispatch::quantize_dynamic(
            float_2d([[1.0, 0.0], [0.0, 1.0]], &device(strategy)),
            &QuantScheme::default(),
        );
        let output = Dispatch::q_matmul(
            TensorPrimitive::QFloat(disabled),
            TensorPrimitive::QFloat(enabled),
        );
        let output = match output {
            TensorPrimitive::QFloat(output) | TensorPrimitive::Float(output) => output,
        };

        assert_eq!(output.autodiff, DispatchAutodiffContext::Enabled(strategy));
    }

    #[test]
    fn inner_transitions_clear_and_restore_context() {
        let tensor = float([1.0, 2.0], &device(GradientCheckpointingStrategy::Balanced));
        let inner = <Dispatch as AutodiffBackend>::inner(tensor);
        assert_eq!(inner.autodiff, DispatchAutodiffContext::Disabled);
        assert!(!matches!(inner.kind, DispatchTensorKind::Autodiff(_)));

        let enabled = <Dispatch as AutodiffBackend>::from_inner(inner);
        assert_enabled_float(&enabled, GradientCheckpointingStrategy::Disabled);

        let int = Dispatch::int_from_data(
            TensorData::from([1i32, 2]),
            &device(GradientCheckpointingStrategy::Balanced),
        );
        let int = <Dispatch as AutodiffBackend>::int_inner(int);
        assert_eq!(int.autodiff, DispatchAutodiffContext::Disabled);
        let int = <Dispatch as AutodiffBackend>::int_from_inner(int);
        assert_eq!(
            int.autodiff,
            DispatchAutodiffContext::Enabled(GradientCheckpointingStrategy::Disabled)
        );
    }

    #[test]
    fn gradients_are_inner_backend_tensors() {
        let x = Dispatch::float_set_require_grad(
            float([2.0, 3.0], &device(GradientCheckpointingStrategy::Balanced)),
            true,
        );
        let output = Dispatch::float_mul(x.clone(), x.clone());
        let gradients = <Dispatch as AutodiffBackend>::backward(output);
        let gradient = <Dispatch as AutodiffBackend>::grad(&x, &gradients).unwrap();
        assert_eq!(gradient.autodiff, DispatchAutodiffContext::Disabled);
        assert!(!matches!(gradient.kind, DispatchTensorKind::Autodiff(_)));
    }
}

// NOTE: placeholder for autodiff module requirements
#[cfg(not(feature = "autodiff"))]
impl AutodiffBackend for Dispatch {
    type InnerBackend = Dispatch;

    type Gradients = bool;

    fn backward(_tensor: DispatchTensor) -> Self::Gradients {
        unimplemented!("Requires `autodiff` feature")
    }

    fn grad(_tensor: &DispatchTensor, _grads: &Self::Gradients) -> Option<DispatchTensor> {
        unimplemented!("Requires `autodiff` feature")
    }

    fn grad_remove(
        _tensor: &DispatchTensor,
        _grads: &mut Self::Gradients,
    ) -> Option<DispatchTensor> {
        unimplemented!("Requires `autodiff` feature")
    }

    fn grad_replace(_tensor: &DispatchTensor, _grads: &mut Self::Gradients, _grad: DispatchTensor) {
        unimplemented!("Requires `autodiff` feature")
    }

    fn inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn int_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn bool_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn q_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn from_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn int_from_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn bool_from_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }

    fn q_from_inner(_tensor: DispatchTensor) -> DispatchTensor {
        unimplemented!("Requires `autodiff` feature")
    }
}

impl Dispatch {
    /// List all available devices of the specified [type id](DispatchDeviceId).
    pub fn enumerate(type_id: DispatchDeviceId) -> Vec<DispatchDevice> {
        // TODO: right now this assumes `type_id = 0`, but WgpuDevice and LibTorchDevice have other types.
        match type_id {
            #[cfg(cube_backend)]
            DispatchDeviceId::Cube => CubeDevice::enumerate_all()
                .into_iter()
                .filter(|device| cube_runtime_enabled(device.runtime()))
                .map(DispatchDevice::Cube)
                .collect(),
            #[cfg(feature = "flex")]
            DispatchDeviceId::Flex => vec![FlexDevice.into()],
            #[cfg(feature = "ndarray")]
            DispatchDeviceId::NdArray => vec![NdArrayDevice::Cpu.into()],
            #[cfg(feature = "tch")]
            DispatchDeviceId::LibTorch => (0..LibTorch::device_count(0))
                .map(|i| LibTorchDevice::Cuda(i).into())
                .collect(),
            #[cfg(feature = "remote")]
            // Remote devices are keyed by a network address, which the type-id-only
            // `enumerate` can't carry. Use [`Dispatch::enumerate_remote_websocket`] to list the devices
            // behind a given address.
            DispatchDeviceId::Remote => Vec::new(),
            #[cfg(feature = "capture")]
            // Capture devices are created together with a lifecycle handle and therefore
            // cannot be reconstructed from a type ID alone.
            DispatchDeviceId::Capture => Vec::new(),
            _ => unreachable!("No backend feature enabled."),
        }
    }

    /// List every device of the cubecl `runtime`.
    ///
    /// The cubecl runtimes share one backend, so [`enumerate`](Self::enumerate) with
    /// [`DispatchDeviceId::Cube`] lists all of them at once. A caller that asked for one
    /// runtime — CUDA, say — wants that runtime's devices and not the wgpu and CPU ones found
    /// beside them, which is what this narrows to. Empty for a runtime this build's features
    /// did not enable.
    #[cfg(cube_backend)]
    pub fn enumerate_cube(runtime: RuntimeId) -> Vec<DispatchDevice> {
        cube_devices(runtime)
            .into_iter()
            .map(DispatchDevice::Cube)
            .collect()
    }

    /// List every device hosted by the remote server at `address`.
    ///
    /// Unlike [`enumerate`](Self::enumerate), remote devices are identified by a network
    /// address rather than enumerable local hardware, so they need a dedicated entry point.
    /// Connecting to the server (required to learn its device count) happens here; see
    /// [`RemoteDevice::enumerate_websocket`].
    ///
    /// Websocket-only: Iroh peers are addressed by endpoint identity, not a URL string.
    #[cfg(feature = "remote-websocket")]
    pub fn enumerate_remote_websocket(address: &str) -> Vec<DispatchDevice> {
        RemoteDevice::enumerate_websocket(address)
            .into_iter()
            .map(DispatchDevice::Remote)
            .collect()
    }
}

/// Whether `runtime` is one this crate's features asked for.
///
/// `CubeDevice::enumerate_all` answers for the runtimes *cubecl* compiled in, and cargo unifies
/// features across a build: a workspace that also builds `burn-cuda` gives cubecl the CUDA
/// runtime even where this crate was built with only `wgpu`. What this crate hands out has to
/// follow its own features, so enumeration is filtered through here.
#[cfg(cube_backend)]
fn cube_runtime_enabled(runtime: RuntimeId) -> bool {
    match runtime {
        RuntimeId::Cuda => cfg!(feature = "cuda"),
        RuntimeId::Hip => cfg!(feature = "rocm"),
        RuntimeId::Wgpu => cfg!(any(
            feature = "wgpu",
            feature = "vulkan",
            feature = "metal",
            feature = "webgpu"
        )),
        RuntimeId::Cpu => cfg!(feature = "cpu"),
        // burn's `metal` feature is wgpu compiling to MSL; cubecl's native Metal runtime has no
        // burn feature to enable it.
        RuntimeId::Metal => false,
    }
}

/// The cubecl devices belonging to `runtime`, in enumeration order. Empty for a runtime this
/// build's features did not ask for, even where cubecl compiled it in.
#[cfg(cube_backend)]
pub(crate) fn cube_devices(runtime: RuntimeId) -> Vec<CubeDevice> {
    if !cube_runtime_enabled(runtime) {
        return Vec::new();
    }

    CubeDevice::enumerate_all()
        .into_iter()
        .filter(|device| device.runtime() == runtime)
        .collect()
}