burn-dispatch 0.22.0-pre.1

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
use crate::{DispatchDevice, backends::*};

#[cfg(feature = "autodiff")]
use burn_autodiff::checkpoint::strategy::{
    BalancedCheckpointing, CheckpointStrategy, NoCheckpointing,
};
use burn_backend::{Backend, BackendTypes, DType, Shape, TensorMetadata};

use crate::CheckpointingStrategy;
#[cfg(feature = "autodiff")]
use alloc::boxed::Box;
#[cfg(feature = "autodiff")]
use burn_backend::tensor::FloatTensor;

use alloc::{format, string::String};

// TODO: if we reduce the different associated types for float/int/bool/quantized tensor primitives down to a single
// `B::TensorPrimitive` we can simplify this.

/// Tensor which points to a backend tensor primitive kind.
#[derive(Clone, Debug)]
pub enum BackendTensor<B: BackendTypes> {
    /// Float tensor handle.
    Float(B::FloatTensorPrimitive),
    /// Int tensor handle.
    Int(B::IntTensorPrimitive),
    /// Bool tensor handle.
    Bool(B::BoolTensorPrimitive),
    /// Quantized tensor handle.
    Quantized(B::QuantizedTensorPrimitive),
    #[cfg(feature = "autodiff")]
    /// Autodiff float tensor handle.
    Autodiff(FloatTensor<Autodiff<B>>),
}

impl<B: Backend> BackendTensor<B> {
    /// Returns the inner float tensor primitive.
    pub fn float(self) -> B::FloatTensorPrimitive {
        match self {
            BackendTensor::Float(tensor) => tensor,
            BackendTensor::Int(_) => panic!("Should be float, got int"),
            BackendTensor::Bool(_) => panic!("Should be float, got bool"),
            BackendTensor::Quantized(_) => panic!("Should be float, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be float, got autodiff"),
        }
    }
    /// Returns the inner float tensor primitive.
    pub fn as_float(&self) -> &B::FloatTensorPrimitive {
        match self {
            BackendTensor::Float(tensor) => tensor,
            BackendTensor::Int(_) => panic!("Should be float, got int"),
            BackendTensor::Bool(_) => panic!("Should be float, got bool"),
            BackendTensor::Quantized(_) => panic!("Should be float, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be float, got autodiff"),
        }
    }

    /// Returns the inner int tensor primitive.
    pub fn int(self) -> B::IntTensorPrimitive {
        match self {
            BackendTensor::Int(tensor) => tensor,
            BackendTensor::Float(_) => panic!("Should be int, got float"),
            BackendTensor::Bool(_) => panic!("Should be int, got bool"),
            BackendTensor::Quantized(_) => panic!("Should be int, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be int, got autodiff"),
        }
    }

    /// Returns the inner bool tensor primitive.
    pub fn bool(self) -> B::BoolTensorPrimitive {
        match self {
            BackendTensor::Bool(tensor) => tensor,
            BackendTensor::Float(_) => panic!("Should be bool, got float"),
            BackendTensor::Int(_) => panic!("Should be bool, got int"),
            BackendTensor::Quantized(_) => panic!("Should be bool, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be bool, got autodiff"),
        }
    }

    /// Returns the inner quantized tensor primitive.
    pub fn quantized(self) -> B::QuantizedTensorPrimitive {
        match self {
            BackendTensor::Quantized(tensor) => tensor,
            _ => unreachable!(),
        }
    }

    #[cfg(feature = "autodiff")]
    /// Returns the inner autodiff tensor primitive.
    pub fn autodiff(self) -> FloatTensor<Autodiff<B>> {
        match self {
            BackendTensor::Autodiff(tensor) => tensor,
            // NOTE: this is the panicking code reached in tensor.rs:74:18:
            _ => unreachable!(),
        }
    }

    #[cfg(feature = "autodiff")]
    /// Returns the inner autodiff tensor primitive.
    pub fn as_autodiff(&self) -> &FloatTensor<Autodiff<B>> {
        match self {
            BackendTensor::Autodiff(tensor) => tensor,
            _ => unreachable!(),
        }
    }

    #[cfg(feature = "autodiff")]
    /// Returns the inner autodiff tensor primitive.
    pub fn autodiff_inner(self) -> B::FloatTensorPrimitive {
        match self {
            BackendTensor::Autodiff(tensor) => tensor.primitive,
            _ => unreachable!(),
        }
    }

    /// Lift a handle for backend `B` into the equivalent handle for `Autodiff<B>`.
    ///
    /// An already-tracked float (`Autodiff`) becomes the `Float` handle of `Autodiff<B>`; int/bool/
    /// quantized handles are re-tagged unchanged (those primitives are shared between `B` and
    /// `Autodiff<B>`). An untracked `Float` handle is invalid here (under autodiff, float tensors
    /// arrive tracked), so it panics.
    #[cfg(feature = "autodiff")]
    pub fn into_autodiff(self) -> BackendTensor<Autodiff<B>> {
        match self {
            BackendTensor::Autodiff(tensor) => BackendTensor::Float(tensor),
            BackendTensor::Int(tensor) => BackendTensor::Int(tensor),
            BackendTensor::Bool(tensor) => BackendTensor::Bool(tensor),
            BackendTensor::Quantized(tensor) => BackendTensor::Quantized(tensor),
            BackendTensor::Float(_) => {
                unreachable!("an untracked float handle can't be lifted to Autodiff<B>")
            }
        }
    }

    /// Returns the tensor primitive kind name.
    pub fn name(&self) -> &'static str {
        match self {
            BackendTensor::Float(_) => "Float",
            BackendTensor::Int(_) => "Int",
            BackendTensor::Bool(_) => "Bool",
            BackendTensor::Quantized(_) => "Quantized",
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => "Autodiff",
        }
    }
}

impl<B: BackendTypes> TensorMetadata for BackendTensor<B> {
    type Device = B::Device;
    fn device(&self) -> Self::Device {
        match self {
            BackendTensor::Float(tensor) => tensor.device(),
            BackendTensor::Int(tensor) => tensor.device(),
            BackendTensor::Bool(tensor) => tensor.device(),
            BackendTensor::Quantized(tensor) => tensor.device(),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(tensor) => tensor.device(),
        }
    }
    fn dtype(&self) -> DType {
        match self {
            BackendTensor::Float(tensor) => tensor.dtype(),
            BackendTensor::Int(tensor) => tensor.dtype(),
            BackendTensor::Bool(tensor) => tensor.dtype(),
            BackendTensor::Quantized(tensor) => tensor.dtype(),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(tensor) => tensor.dtype(),
        }
    }

    fn shape(&self) -> Shape {
        match self {
            BackendTensor::Float(tensor) => tensor.shape(),
            BackendTensor::Int(tensor) => tensor.shape(),
            BackendTensor::Bool(tensor) => tensor.shape(),
            BackendTensor::Quantized(tensor) => tensor.shape(),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(tensor) => tensor.shape(),
        }
    }

    fn can_mut(&self) -> bool {
        match self {
            BackendTensor::Float(tensor) => tensor.can_mut(),
            BackendTensor::Int(tensor) => tensor.can_mut(),
            BackendTensor::Bool(tensor) => tensor.can_mut(),
            BackendTensor::Quantized(tensor) => tensor.can_mut(),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(tensor) => tensor.can_mut(),
        }
    }
}

/// A tensor that can dispatch operations to any enabled backend at runtime.
///
/// When the `autodiff` feature is enabled, tensors may carry a checkpointing
/// strategy used to control gradient computation. This is derived from the
/// device used to create the tensor.
#[derive(Clone, Debug)]
pub struct DispatchTensor {
    /// Tensor kind primitive.
    pub kind: DispatchTensorKind,
    // Technically more of a device property, but device is not a dispatch tensor field.
    // Right now this is the easiest way to preserve the checkpointing strategy because primitives are not consolidated.
    // Once float/int/bool primitives are consolidated into a single associative type, we could hold that
    // property for all autodiff tensors.
    /// Holds the autodiff checkpointing strategy.
    /// - `None`: tensor is not tracked by autodiff
    /// - `Some(strategy)`: tensor is tracked by autodiff, and uses the checkpointing `strategy`
    pub checkpointing: Option<CheckpointingStrategy>,
}

/// Internal representation of a [`DispatchTensor`].
///
/// This enum contains the concrete backend tensor for each enabled backend.
/// It is not intended to be used directly; instead, it is manipulated by
/// the dispatch system to route operations to the correct backend.
///
/// Each variant corresponds to a specific backend implementation.
#[derive(Clone, Debug)]
pub enum DispatchTensorKind {
    /// The [CPU backend](Cpu) tensor.
    #[cfg(feature = "cpu")]
    Cpu(BackendTensor<Cpu>),

    /// The [CUDA backend](Cuda) tensor.
    #[cfg(feature = "cuda")]
    Cuda(BackendTensor<Cuda>),

    /// The [Metal backend](Metal) tensor.
    #[cfg(feature = "metal")]
    Metal(BackendTensor<Metal>),

    /// The [ROCm backend](Rocm) tensor.
    #[cfg(feature = "rocm")]
    Rocm(BackendTensor<Rocm>),

    /// The [Vulkan backend](Vulkan) tensor.
    #[cfg(feature = "vulkan")]
    Vulkan(BackendTensor<Vulkan>),

    /// The [Wgpu backend](Wgpu) tensor.
    #[cfg(feature = "wgpu")]
    Wgpu(BackendTensor<Wgpu>),

    /// The [WebGPU backend](Wgpu) tensor.
    #[cfg(feature = "webgpu")]
    WebGpu(BackendTensor<WebGpu>),

    /// The [Flex backend](Flex) tensor.
    #[cfg(any(feature = "flex", default_backend))]
    Flex(BackendTensor<Flex>),

    /// The [NdArray backend](NdArray) tensor.
    #[cfg(feature = "ndarray")]
    NdArray(BackendTensor<NdArray>),

    /// The [LibTorch backend](LibTorch) tensor.
    #[cfg(feature = "tch")]
    LibTorch(BackendTensor<LibTorch>),

    /// The [Remote backend](Remote) tensor (lives on a remote server).
    #[cfg(feature = "remote")]
    Remote(BackendTensor<Remote>),

    /// The [autodiff enabled backend](Autodiff) tensor.
    #[cfg(feature = "autodiff")]
    Autodiff(Box<DispatchTensorKind>),
}

impl TensorMetadata for DispatchTensorKind {
    type Device = DispatchDevice;

    fn dtype(&self) -> DType {
        match self {
            #[cfg(feature = "cpu")]
            Self::Cpu(tensor) => tensor.dtype(),
            #[cfg(feature = "cuda")]
            Self::Cuda(tensor) => tensor.dtype(),
            #[cfg(feature = "metal")]
            Self::Metal(tensor) => tensor.dtype(),
            #[cfg(feature = "rocm")]
            Self::Rocm(tensor) => tensor.dtype(),
            #[cfg(feature = "vulkan")]
            Self::Vulkan(tensor) => tensor.dtype(),
            #[cfg(feature = "wgpu")]
            Self::Wgpu(tensor) => tensor.dtype(),
            #[cfg(feature = "webgpu")]
            Self::WebGpu(tensor) => tensor.dtype(),
            #[cfg(any(feature = "flex", default_backend))]
            Self::Flex(tensor) => tensor.dtype(),
            #[cfg(feature = "ndarray")]
            Self::NdArray(tensor) => tensor.dtype(),
            #[cfg(feature = "tch")]
            Self::LibTorch(tensor) => tensor.dtype(),
            #[cfg(feature = "remote")]
            Self::Remote(tensor) => tensor.dtype(),
            #[cfg(feature = "autodiff")]
            Self::Autodiff(tensor) => tensor.dtype(),
        }
    }

    fn shape(&self) -> Shape {
        match self {
            #[cfg(feature = "cpu")]
            Self::Cpu(tensor) => tensor.shape(),
            #[cfg(feature = "cuda")]
            Self::Cuda(tensor) => tensor.shape(),
            #[cfg(feature = "metal")]
            Self::Metal(tensor) => tensor.shape(),
            #[cfg(feature = "rocm")]
            Self::Rocm(tensor) => tensor.shape(),
            #[cfg(feature = "vulkan")]
            Self::Vulkan(tensor) => tensor.shape(),
            #[cfg(feature = "wgpu")]
            Self::Wgpu(tensor) => tensor.shape(),
            #[cfg(feature = "webgpu")]
            Self::WebGpu(tensor) => tensor.shape(),
            #[cfg(any(feature = "flex", default_backend))]
            Self::Flex(tensor) => tensor.shape(),
            #[cfg(feature = "ndarray")]
            Self::NdArray(tensor) => tensor.shape(),
            #[cfg(feature = "tch")]
            Self::LibTorch(tensor) => tensor.shape(),
            #[cfg(feature = "remote")]
            Self::Remote(tensor) => tensor.shape(),
            #[cfg(feature = "autodiff")]
            Self::Autodiff(tensor) => tensor.shape(),
        }
    }

    fn device(&self) -> DispatchDevice {
        match self {
            #[cfg(feature = "cpu")]
            DispatchTensorKind::Cpu(tensor) => DispatchDevice::Cpu(tensor.device()),
            #[cfg(feature = "cuda")]
            DispatchTensorKind::Cuda(tensor) => DispatchDevice::Cuda(tensor.device()),
            #[cfg(feature = "metal")]
            DispatchTensorKind::Metal(tensor) => DispatchDevice::Metal(tensor.device()),
            #[cfg(feature = "rocm")]
            DispatchTensorKind::Rocm(tensor) => DispatchDevice::Rocm(tensor.device()),
            #[cfg(feature = "vulkan")]
            DispatchTensorKind::Vulkan(tensor) => DispatchDevice::Vulkan(tensor.device()),
            #[cfg(feature = "wgpu")]
            DispatchTensorKind::Wgpu(tensor) => DispatchDevice::Wgpu(tensor.device()),
            #[cfg(feature = "webgpu")]
            DispatchTensorKind::WebGpu(tensor) => DispatchDevice::WebGpu(tensor.device()),
            #[cfg(any(feature = "flex", default_backend))]
            DispatchTensorKind::Flex(tensor) => DispatchDevice::Flex(tensor.device()),
            #[cfg(feature = "ndarray")]
            DispatchTensorKind::NdArray(tensor) => DispatchDevice::NdArray(tensor.device()),
            #[cfg(feature = "tch")]
            DispatchTensorKind::LibTorch(tensor) => DispatchDevice::LibTorch(tensor.device()),
            #[cfg(feature = "remote")]
            DispatchTensorKind::Remote(tensor) => DispatchDevice::Remote(tensor.device()),
            #[cfg(feature = "autodiff")]
            DispatchTensorKind::Autodiff(tensor) => DispatchDevice::autodiff(tensor.device()),
        }
    }

    fn can_mut(&self) -> bool {
        match self {
            #[cfg(feature = "cpu")]
            Self::Cpu(tensor) => tensor.can_mut(),
            #[cfg(feature = "cuda")]
            Self::Cuda(tensor) => tensor.can_mut(),
            #[cfg(feature = "metal")]
            Self::Metal(tensor) => tensor.can_mut(),
            #[cfg(feature = "rocm")]
            Self::Rocm(tensor) => tensor.can_mut(),
            #[cfg(feature = "vulkan")]
            Self::Vulkan(tensor) => tensor.can_mut(),
            #[cfg(feature = "wgpu")]
            Self::Wgpu(tensor) => tensor.can_mut(),
            #[cfg(feature = "webgpu")]
            Self::WebGpu(tensor) => tensor.can_mut(),
            #[cfg(any(feature = "flex", default_backend))]
            Self::Flex(tensor) => tensor.can_mut(),
            #[cfg(feature = "ndarray")]
            Self::NdArray(tensor) => tensor.can_mut(),
            #[cfg(feature = "tch")]
            Self::LibTorch(tensor) => tensor.can_mut(),
            #[cfg(feature = "remote")]
            Self::Remote(tensor) => tensor.can_mut(),
            #[cfg(feature = "autodiff")]
            Self::Autodiff(tensor) => tensor.can_mut(),
        }
    }
}

impl TensorMetadata for DispatchTensor {
    fn dtype(&self) -> DType {
        self.kind.dtype()
    }

    fn shape(&self) -> Shape {
        self.kind.shape()
    }

    fn can_mut(&self) -> bool {
        self.kind.can_mut()
    }

    type Device = DispatchDevice;

    fn device(&self) -> Self::Device {
        #[allow(unused_mut)]
        let mut device = self.kind.device();

        // TODO: should int and bool kinds return an autodiff device?
        // It would be much easier once there is a single underlying primitive type, which
        // we can wrap with Autodiff in all cases.

        #[cfg(feature = "autodiff")]
        if let DispatchDevice::Autodiff(device) = &mut device
            && let Some(checkpointing) = &self.checkpointing
        {
            device.checkpointing = *checkpointing;
        }

        device
    }
}

impl DispatchTensorKind {
    /// Returns the backend tensor kind name.
    pub(crate) fn name(&self) -> &'static str {
        match self {
            #[cfg(feature = "cpu")]
            DispatchTensorKind::Cpu(_) => "Cpu",
            #[cfg(feature = "cuda")]
            DispatchTensorKind::Cuda(_) => "Cuda",
            #[cfg(feature = "metal")]
            DispatchTensorKind::Metal(_) => "Metal",
            #[cfg(feature = "rocm")]
            DispatchTensorKind::Rocm(_) => "Rocm",
            #[cfg(feature = "vulkan")]
            DispatchTensorKind::Vulkan(_) => "Vulkan",
            #[cfg(feature = "wgpu")]
            DispatchTensorKind::Wgpu(_) => "Wgpu",
            #[cfg(feature = "webgpu")]
            DispatchTensorKind::WebGpu(_) => "WebGpu",
            #[cfg(any(feature = "flex", default_backend))]
            DispatchTensorKind::Flex(_) => "Flex",
            #[cfg(feature = "ndarray")]
            DispatchTensorKind::NdArray(_) => "NdArray",
            #[cfg(feature = "tch")]
            DispatchTensorKind::LibTorch(_) => "LibTorch",
            #[cfg(feature = "remote")]
            DispatchTensorKind::Remote(_) => "Remote",
            #[cfg(feature = "autodiff")]
            DispatchTensorKind::Autodiff(_) => "Autodiff",
        }
    }
}

#[cfg(feature = "autodiff")]
trait IntoCheckpointingStrategy {
    const STRATEGY: CheckpointingStrategy;
}

#[cfg(feature = "autodiff")]
impl IntoCheckpointingStrategy for NoCheckpointing {
    const STRATEGY: CheckpointingStrategy = CheckpointingStrategy::None;
}

#[cfg(feature = "autodiff")]
impl IntoCheckpointingStrategy for BalancedCheckpointing {
    const STRATEGY: CheckpointingStrategy = CheckpointingStrategy::Balanced;
}

/// Trait to execute runtime routing conversions between the dynamic dispatch layer and specific backends.
pub trait DispatchKindConversion<B: Backend> {
    /// Attempts to extract a backend-specific [`BackendTensor`] wrapper from a generic, dynamically-routed [`DispatchTensor`].
    ///
    /// # Errors
    ///
    /// Returns an error if the dynamic routing state does not match the requested backend `B`.
    fn try_into_backend(tensor: DispatchTensor) -> Result<BackendTensor<B>, String>;

    /// Encapsulates a backend-specific tensor variant back into a globally routing [`DispatchTensor`].
    fn from_backend(tensor: BackendTensor<B>) -> DispatchTensor;
}

macro_rules! impl_dispatch_conversion {
    ($backend:ident, $cfg:meta) => {
        #[cfg($cfg)]
        impl DispatchKindConversion<$backend> for DispatchTensor {
            fn try_into_backend(tensor: DispatchTensor) -> Result<BackendTensor<$backend>, String> {
                // The catch-all is unreachable in single-backend builds (the enum then has one
                // variant), but required when several backend features are enabled.
                #[allow(unreachable_patterns)]
                match tensor.kind {
                    DispatchTensorKind::$backend(t) => Ok(t),
                    other => Err(format!(
                        "Expected {} tensor, got variant: {}",
                        stringify!($backend),
                        other.name()
                    )),
                }
            }

            fn from_backend(tensor: BackendTensor<$backend>) -> DispatchTensor {
                DispatchTensor {
                    kind: DispatchTensorKind::$backend(tensor),
                    checkpointing: None,
                }
            }
        }

        #[cfg(all($cfg, feature = "autodiff"))]
        impl<C: CheckpointStrategy + IntoCheckpointingStrategy>
            DispatchKindConversion<Autodiff<$backend, C>> for DispatchTensor
        {
            fn try_into_backend(
                tensor: DispatchTensor,
            ) -> Result<BackendTensor<Autodiff<$backend, C>>, String> {
                match tensor.kind {
                    DispatchTensorKind::Autodiff(t) => match *t {
                        DispatchTensorKind::$backend(t) => match t {
                            // Encode as `BackendTensor::Float` for `Autodiff<B, C>`
                            BackendTensor::Autodiff(t) => Ok(BackendTensor::Float(t)),
                            other => Err(format!(
                                "Expected Autodiff {} float tensor, got Autodiff variant: {}",
                                stringify!($backend),
                                other.name()
                            )),
                        },
                        other => Err(format!(
                            "Expected Autodiff {} tensor, got Autodiff variant: {}",
                            stringify!($backend),
                            other.name()
                        )),
                    },
                    other => Err(format!(
                        "Expected Autodiff tensor, got backend: {}",
                        other.name()
                    )),
                }
            }

            fn from_backend(tensor: BackendTensor<Autodiff<$backend, C>>) -> DispatchTensor {
                // Unwrap the Autodiff backend representation back into the inner hardware representation
                let kind = match tensor {
                    // Inverse: Wrap the `Float` variant back into the backend's `Autodiff` primitive variant
                    BackendTensor::Float(t) => {
                        let ad_tensor = BackendTensor::Autodiff(t);
                        // Wrap in the concrete backend's dispatch container
                        let inner_dispatch = DispatchTensorKind::$backend(ad_tensor);
                        // Re-apply the outer Autodiff dispatch wrapper
                        DispatchTensorKind::Autodiff(Box::new(inner_dispatch))
                    }

                    // Pass-throughs for non-differentiable types
                    BackendTensor::Int(t) => DispatchTensorKind::$backend(BackendTensor::Int(t)),
                    BackendTensor::Bool(t) => DispatchTensorKind::$backend(BackendTensor::Bool(t)),
                    BackendTensor::Quantized(t) => {
                        DispatchTensorKind::$backend(BackendTensor::Quantized(t))
                    }

                    BackendTensor::Autodiff(_) => {
                        panic!("Unexpected Autodiff variant provided to `from_backend`",)
                    }
                };

                DispatchTensor {
                    kind,
                    checkpointing: Some(C::STRATEGY),
                }
            }
        }
    };
}

impl_dispatch_conversion!(Flex, any(feature = "flex", default_backend));
impl_dispatch_conversion!(Cpu, feature = "cpu");
impl_dispatch_conversion!(Cuda, feature = "cuda");
impl_dispatch_conversion!(Rocm, feature = "rocm");
impl_dispatch_conversion!(Remote, feature = "remote");
impl_dispatch_conversion!(Metal, feature = "metal");
impl_dispatch_conversion!(Vulkan, feature = "vulkan");
impl_dispatch_conversion!(Wgpu, feature = "wgpu");
impl_dispatch_conversion!(WebGpu, feature = "webgpu");
impl_dispatch_conversion!(NdArray, feature = "ndarray");
impl_dispatch_conversion!(LibTorch, feature = "tch");