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
use burn_backend::{DeviceId, DeviceOps, DeviceSettings};

#[allow(unused_imports)] // Empty in backend-free and capture-only builds.
use crate::devices::*;

#[cfg(feature = "capture")]
use burn_capture::CaptureDevice;

#[cfg(feature = "autodiff")]
use alloc::boxed::Box;

// Throughput types come from `burn-backend` (which re-exports them from cubecl),
// so `burn-dispatch` needs no direct `cubecl` dependency.
#[cfg(feature = "cubecl")]
use alloc::vec::Vec;
#[cfg(feature = "cubecl")]
use burn_backend::cubecl::{DeviceIdentity, ThroughputError, ThroughputKey, ThroughputValue};
// `cubecl` without a runtime feature gives the throughput *types* but no `Cube` device to
// measure, so the measurement itself follows `cube_backend` rather than the feature.
#[cfg(cube_backend)]
use burn_backend::cubecl::measure_peak_throughput;

/// Represents a device for the [`Dispatch`](crate::Dispatch).
///
/// Each variant corresponds to a backend that the [`Dispatch`](crate::Dispatch) can dispatch operations to.
///
/// # Example
///
/// ```ignore
/// use burn::DispatchDevice;
///
/// // One variant covers every cubecl runtime; the device inside says which.
/// #[cfg(feature = "cuda")]
/// let cuda_device = DispatchDevice::Cube(cubecl::Device::Cuda(Default::default()));
///
/// #[cfg(feature = "ndarray")]
/// let ndarray_device = DispatchDevice::NdArray(Default::default());
/// ```
#[derive(Clone, Eq)]
pub enum DispatchDevice {
    #[cfg(not(backend_enabled))]
    #[doc(hidden)]
    Unavailable(crate::NoBackend),
    /// A device of the [cubecl backend](crate::backends::Cube): CUDA, ROCm, Metal, Vulkan,
    /// WebGPU, wgpu or the CPU runtime.
    #[cfg(cube_backend)]
    Cube(CubeDevice),

    /// The [Flex backend](crate::backends::Flex) device (CPU-only).
    #[cfg(feature = "flex")]
    Flex(FlexDevice),

    /// The [NdArray backend](crate::backends::NdArray) device (CPU-only).
    #[cfg(feature = "ndarray")]
    NdArray(NdArrayDevice),

    /// The [LibTorch backend](crate::backends::LibTorch) device.
    #[cfg(feature = "tch")]
    LibTorch(LibTorchDevice),

    /// The [remote backend](crate::backends::Remote) device, identified by a network address.
    #[cfg(feature = "remote")]
    Remote(RemoteDevice),

    /// A non-executing graph capture device.
    #[cfg(feature = "capture")]
    Capture(CaptureDevice),

    /// The [autodiff enabled backend](crate::backends::Autodiff) device.
    #[cfg(feature = "autodiff")]
    Autodiff(AutodiffDevice),
}

#[cfg(feature = "cubecl")]
impl DispatchDevice {
    /// Who this device is, `None` for a backend that does not report one. An autodiff device
    /// answers for the device it wraps. Opens the device.
    pub fn identity(&self) -> Option<DeviceIdentity> {
        match self {
            #[cfg(cube_backend)]
            DispatchDevice::Cube(device) => Some(device.client().properties().identity.clone()),
            #[cfg(feature = "autodiff")]
            DispatchDevice::Autodiff(device) => device.inner.identity(),
            #[allow(unreachable_patterns)]
            _ => None,
        }
    }

    /// Measure peak throughput for this device against the given `keys`.
    ///
    /// Only cubecl-backed devices can measure throughput; other backends
    /// (ndarray, libtorch, remote, ...) return an empty vector. An autodiff
    /// device reports the peaks of the device it wraps. Each returned result
    /// corresponds positionally to the key at the same index, and carries a
    /// [`ThroughputError`] where the device has no peak for that key.
    // With `cubecl` on but no runtime compiled in, every arm below ignores `keys`.
    #[cfg_attr(not(cube_backend), allow(unused_variables))]
    pub fn performance_stats(
        &self,
        keys: &[ThroughputKey],
    ) -> Vec<Result<ThroughputValue, ThroughputError>> {
        // No catch-all arm: a new backend must fail to compile here rather
        // than silently report no peaks.
        match self {
            #[cfg(not(backend_enabled))]
            Self::Unavailable(never) => never.unreachable(),
            #[cfg(cube_backend)]
            DispatchDevice::Cube(device) => {
                let client = device.client();
                keys.iter()
                    .map(|key| measure_peak_throughput(&client, *key))
                    .collect()
            }
            // Autodiff does not change the hardware, so measure the wrapped device.
            #[cfg(feature = "autodiff")]
            DispatchDevice::Autodiff(device) => device.performance_stats(keys),

            // Not cubecl-backed, so there are no kernels to measure.
            #[cfg(feature = "flex")]
            DispatchDevice::Flex(_) => Vec::new(),
            #[cfg(feature = "ndarray")]
            DispatchDevice::NdArray(_) => Vec::new(),
            #[cfg(feature = "tch")]
            DispatchDevice::LibTorch(_) => Vec::new(),

            // The kernels run on the server, which this local API cannot reach.
            #[cfg(feature = "remote")]
            DispatchDevice::Remote(_) => Vec::new(),
            #[cfg(feature = "capture")]
            DispatchDevice::Capture(_) => Vec::new(),
        }
    }
}

#[cfg(feature = "autodiff")]
// This tuple struct mainly restricts users from creating Autodiff(Autodiff) devices.
/// A wrapper that enables automatic differentiation for a [`DispatchDevice`].
///
/// Use [`DispatchDevice::autodiff`] to construct this type.
#[derive(Debug, Clone)]
pub struct AutodiffDevice {
    pub(crate) inner: Box<DispatchDevice>,
    pub(crate) checkpointing: GradientCheckpointingStrategy,
}

/// Compares on hardware identity only, ignoring the checkpointing strategy, so that this agrees
/// with [`DispatchDevice`]'s own [`PartialEq`] — which has to ignore it, since comparing an
/// `Autodiff` device against a raw one has no strategy to compare against. A derived impl would
/// make `Autodiff(a) == Autodiff(b)` disagree with `DispatchDevice::Autodiff(a) ==
/// DispatchDevice::Autodiff(b)`.
///
/// Use [`gradient_checkpointing_strategy`](Self::gradient_checkpointing_strategy) when the
/// strategy is what you actually need to compare.
#[cfg(feature = "autodiff")]
impl PartialEq for AutodiffDevice {
    fn eq(&self, other: &Self) -> bool {
        self.inner == other.inner
    }
}

#[cfg(feature = "autodiff")]
impl Eq for AutodiffDevice {}

#[cfg(feature = "autodiff")]
impl AutodiffDevice {
    pub(crate) fn new(
        device: DispatchDevice,
        checkpointing: GradientCheckpointingStrategy,
    ) -> Self {
        Self {
            inner: Box::new(device),
            checkpointing,
        }
    }

    /// Returns the underlying device, removing the autodiff capability.
    pub fn inner(self) -> DispatchDevice {
        *self.inner
    }

    /// Returns the gradient checkpointing strategy.
    pub fn gradient_checkpointing_strategy(&self) -> GradientCheckpointingStrategy {
        self.checkpointing
    }
}

#[cfg(feature = "autodiff")]
// Useful for match in dispatch macros
impl core::ops::Deref for AutodiffDevice {
    type Target = DispatchDevice;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
/// Gradient checkpointing strategy for autodiff.
#[repr(u8)]
pub enum GradientCheckpointingStrategy {
    /// Recompute selected activations during backpropagation to reduce peak memory usage.
    Balanced,
    /// Disable gradient checkpointing while retaining autodiff tracking.
    #[default]
    Disabled,
}

impl core::fmt::Debug for DispatchDevice {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            #[cfg(not(backend_enabled))]
            Self::Unavailable(never) => never.unreachable(),
            #[cfg(cube_backend)]
            Self::Cube(device) => f.debug_tuple("Cube").field(device).finish(),
            #[cfg(feature = "flex")]
            Self::Flex(device) => f.debug_tuple("Flex").field(device).finish(),
            #[cfg(feature = "ndarray")]
            Self::NdArray(device) => f.debug_tuple("NdArray").field(device).finish(),
            #[cfg(feature = "tch")]
            Self::LibTorch(device) => f.debug_tuple("LibTorch").field(device).finish(),
            #[cfg(feature = "remote")]
            Self::Remote(device) => f.debug_tuple("Remote").field(device).finish(),
            #[cfg(feature = "capture")]
            Self::Capture(device) => f.debug_tuple("Capture").field(device).finish(),
            #[cfg(feature = "autodiff")]
            // Format without `AutodiffDevice` wrapper
            Self::Autodiff(device) => f
                .debug_struct("Autodiff")
                .field("device", &device.inner)
                .field("checkpointing", &device.checkpointing)
                .finish(),
        }
    }
}

impl Default for DispatchDevice {
    /// Select an enabled backend in this order: CUDA, Metal, ROCm, Vulkan, WebGPU,
    /// wgpu, CPU, LibTorch, Flex, Remote, NdArray. `BURN_DEVICE` overrides this in
    /// std builds. Capture devices must be constructed explicitly.
    ///
    /// Panics when no execution backend is enabled.
    #[allow(unreachable_code)]
    fn default() -> Self {
        // BURN_DEVICE selects one compiled backend or reports a configuration error.

        #[cfg(feature = "std")]
        {
            if let Ok(device_str) = std::env::var("BURN_DEVICE") {
                match device_str.to_lowercase().as_str() {
                    // Every cubecl runtime is the one `Cube` backend; the name here
                    // picks the runtime the device names, and the wgpu spellings all
                    // reach wgpu, whose compiler is chosen for it at runtime.
                    "cuda" => {
                        #[cfg(feature = "cuda")]
                        return Self::Cube(CubeDevice::Cuda(Default::default()));
                        panic!(
                            "BURN_DEVICE=cuda requested, but the 'cuda' feature is not enabled."
                        );
                    }
                    "rocm" => {
                        #[cfg(feature = "rocm")]
                        return Self::Cube(CubeDevice::Hip(Default::default()));
                        panic!(
                            "BURN_DEVICE=rocm requested, but the 'rocm' feature is not enabled."
                        );
                    }
                    "metal" | "vulkan" | "webgpu" | "wgpu" => {
                        #[cfg(any(
                            feature = "metal",
                            feature = "vulkan",
                            feature = "webgpu",
                            feature = "wgpu"
                        ))]
                        return Self::Cube(CubeDevice::Wgpu(Default::default()));
                        panic!(
                            "BURN_DEVICE={device_str} requested, but no wgpu feature is enabled."
                        );
                    }
                    "cpu" => {
                        #[cfg(feature = "cpu")]
                        return Self::Cube(CubeDevice::Cpu(Default::default()));
                        panic!("BURN_DEVICE=cpu requested, but the 'cpu' feature is not enabled.");
                    }
                    "tch" => {
                        #[cfg(feature = "tch")]
                        return Self::LibTorch(LibTorchDevice::default());
                        panic!("BURN_DEVICE=tch requested, but the 'tch' feature is not enabled.");
                    }
                    "remote" => {
                        #[cfg(feature = "remote")]
                        return Self::Remote(RemoteDevice::default());
                        panic!(
                            "BURN_DEVICE=remote requested, but the 'remote' feature is not enabled."
                        );
                    }
                    "flex" => {
                        #[cfg(feature = "flex")]
                        return Self::Flex(FlexDevice);
                        panic!(
                            "BURN_DEVICE=flex requested, but the 'flex' feature is not enabled."
                        );
                    }
                    "ndarray" => {
                        #[cfg(feature = "ndarray")]
                        return Self::NdArray(NdArrayDevice::default());
                        panic!(
                            "BURN_DEVICE=ndarray requested, but the 'ndarray' feature is not enabled."
                        );
                    }
                    _ => panic!("Unknown BURN_DEVICE override: '{}'.", device_str),
                }
            }
        }

        // Spelled out per feature rather than left to `CubeDevice::default()`: that answers for
        // the runtimes *cubecl* compiled in, and cargo unifies features across a build, so a
        // workspace that also builds `burn-cuda` would hand this crate a CUDA default even when
        // it was built with only `wgpu`. The order is the one a caller who did not choose would
        // want — a discrete accelerator, then the portable path, then the CPU.
        #[cfg(feature = "cuda")]
        return Self::Cube(CubeDevice::Cuda(Default::default()));

        #[cfg(feature = "metal")]
        return Self::Cube(CubeDevice::Wgpu(Default::default()));

        #[cfg(feature = "rocm")]
        return Self::Cube(CubeDevice::Hip(Default::default()));

        #[cfg(feature = "vulkan")]
        return Self::Cube(CubeDevice::Wgpu(Default::default()));

        #[cfg(feature = "webgpu")]
        return Self::Cube(CubeDevice::Wgpu(Default::default()));

        #[cfg(feature = "wgpu")]
        return Self::Cube(CubeDevice::Wgpu(Default::default()));

        #[cfg(feature = "cpu")]
        return Self::Cube(CubeDevice::Cpu(Default::default()));

        #[cfg(feature = "tch")]
        return Self::LibTorch(LibTorchDevice::default());

        // Preserve the preference for Flex over the deprecated NdArray backend.
        #[cfg(feature = "flex")]
        return Self::Flex(FlexDevice);

        #[cfg(feature = "remote")]
        return Self::Remote(RemoteDevice::default());

        #[cfg(feature = "ndarray")]
        return Self::NdArray(NdArrayDevice::default());

        panic!(
            "No execution backend is enabled. Enable a Burn backend feature such as `flex`, \
             `wgpu`, or `cuda`. To record a graph without executing it, enable `capture` \
             and use Device::capture()."
        );
    }
}

impl PartialEq for DispatchDevice {
    /// Compares devices based on hardware identity.
    ///
    /// Returns `true` if both devices represent the same compute resource.
    /// Note that this comparison ignores autodiff and checkpointing settings.
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            // If both are Autodiff, compare the inner devices
            #[cfg(feature = "autodiff")]
            (DispatchDevice::Autodiff(a), DispatchDevice::Autodiff(b)) => {
                a.inner.as_ref() == b.inner.as_ref()
            }
            // If one is Autodiff, compare it to the raw device
            #[cfg(feature = "autodiff")]
            (DispatchDevice::Autodiff(a), b) => a.inner.as_ref() == b,
            #[cfg(feature = "autodiff")]
            (a, DispatchDevice::Autodiff(b)) => a == b.inner.as_ref(),
            #[cfg(cube_backend)]
            (Self::Cube(a), Self::Cube(b)) => a == b,
            #[cfg(feature = "flex")]
            (Self::Flex(a), Self::Flex(b)) => a == b,
            #[cfg(feature = "ndarray")]
            (Self::NdArray(a), Self::NdArray(b)) => a == b,
            #[cfg(feature = "tch")]
            (Self::LibTorch(a), Self::LibTorch(b)) => a == b,
            #[cfg(feature = "remote")]
            (Self::Remote(a), Self::Remote(b)) => a == b,
            #[cfg(feature = "capture")]
            (Self::Capture(a), Self::Capture(b)) => a == b,
            #[allow(unreachable_patterns)]
            (_, _) => false,
        }
    }
}

const INTERNAL_ID_MASK: u16 = 0x00FF;
const BACKEND_SHIFT: u32 = 8;

impl DispatchDevice {
    /// Create the dispatch representation used by the high-level graph-capture device.
    #[cfg(feature = "capture")]
    #[doc(hidden)]
    pub fn capture() -> Self {
        Self::Capture(CaptureDevice::default())
    }

    #[cfg(feature = "autodiff")]
    /// Creates a new [`DispatchDevice`] with
    /// [automatic differentiation](crate::backends::Autodiff) enabled.
    pub fn autodiff(device: impl Into<DispatchDevice>) -> DispatchDevice {
        Self::autodiff_with_gradient_checkpointing(device, GradientCheckpointingStrategy::Disabled)
    }
    #[cfg(feature = "autodiff")]
    /// Creates a new [`DispatchDevice`] with automatic differentiation and the provided gradient
    /// checkpointing strategy enabled.
    pub fn autodiff_with_gradient_checkpointing(
        device: impl Into<DispatchDevice>,
        checkpointing: GradientCheckpointingStrategy,
    ) -> DispatchDevice {
        let device = device.into();
        DispatchDevice::Autodiff(AutodiffDevice::new(device, checkpointing))
    }

    /// Returns the inner device, without autodiff (when enabled).
    pub fn inner(self) -> Self {
        #[cfg(feature = "autodiff")]
        if let DispatchDevice::Autodiff(device) = self {
            return *device.inner;
        }

        self
    }

    /// Returns a unique number per variant to encode into type_id.
    fn backend_id(&self) -> DispatchDeviceId {
        match self {
            #[cfg(not(backend_enabled))]
            Self::Unavailable(never) => never.unreachable(),
            #[cfg(cube_backend)]
            Self::Cube(_) => DispatchDeviceId::Cube,
            #[cfg(feature = "flex")]
            Self::Flex(_) => DispatchDeviceId::Flex,
            #[cfg(feature = "ndarray")]
            Self::NdArray(_) => DispatchDeviceId::NdArray,
            #[cfg(feature = "tch")]
            Self::LibTorch(_) => DispatchDeviceId::LibTorch,
            #[cfg(feature = "remote")]
            Self::Remote(_) => DispatchDeviceId::Remote,
            #[cfg(feature = "capture")]
            Self::Capture(_) => DispatchDeviceId::Capture,
            #[cfg(feature = "autodiff")]
            Self::Autodiff(device) => device.inner.backend_id(),
        }
    }

    /// Encode variant ID and backend type ID into a unique `type_id`.
    fn encode_type_id(&self, backend_type_id: u16) -> u16 {
        // Use the lower 8 bits for the backend's internal type ID
        let internal_type_id = backend_type_id & INTERNAL_ID_MASK;
        // Use the upper 8 bits for the DispatchDevice/DispatchDeviceId
        let backend = u16::from(self.backend_id()) << BACKEND_SHIFT;
        backend | internal_type_id
    }

    /// Decode an encoded `type_id` into variant ID and backend type ID.
    pub(crate) fn decode_type_id(type_id: u16) -> (DispatchDeviceId, u16) {
        let backend_raw = type_id >> BACKEND_SHIFT;
        let internal_type_id = type_id & INTERNAL_ID_MASK;

        let backend = DispatchDeviceId::try_from(backend_raw).expect("Unknown DispatchDevice ID");

        (backend, internal_type_id)
    }
}

#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u16)]
pub enum DispatchDeviceId {
    /// Every cubecl runtime: which one is in the device's own id.
    Cube = 0,
    Flex = 4,
    LibTorch = 5,
    NdArray = 6,
    Remote = 10,
    Capture = 11,
}

impl From<DispatchDeviceId> for u16 {
    fn from(variant: DispatchDeviceId) -> Self {
        variant as u16
    }
}

impl TryFrom<u16> for DispatchDeviceId {
    type Error = ();

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        match value {
            #[cfg(cube_backend)]
            0 => Ok(Self::Cube),
            #[cfg(feature = "flex")]
            4 => Ok(Self::Flex),
            #[cfg(feature = "tch")]
            5 => Ok(Self::LibTorch),
            #[cfg(feature = "ndarray")]
            6 => Ok(Self::NdArray),
            #[cfg(feature = "remote")]
            10 => Ok(Self::Remote),
            #[cfg(feature = "capture")]
            11 => Ok(Self::Capture),
            _ => Err(()),
        }
    }
}

impl DeviceOps for DispatchDevice {
    fn defaults(&self) -> DeviceSettings {
        match self {
            #[cfg(not(backend_enabled))]
            Self::Unavailable(never) => never.unreachable(),
            #[cfg(cube_backend)]
            Self::Cube(device) => device.defaults(),
            #[cfg(feature = "flex")]
            Self::Flex(device) => device.defaults(),
            #[cfg(feature = "ndarray")]
            Self::NdArray(device) => device.defaults(),
            #[cfg(feature = "tch")]
            Self::LibTorch(device) => device.defaults(),
            #[cfg(feature = "remote")]
            Self::Remote(device) => device.defaults(),
            #[cfg(feature = "capture")]
            Self::Capture(device) => device.defaults(),
            #[cfg(feature = "autodiff")]
            Self::Autodiff(device) => device.inner.defaults(),
        }
    }
}

impl burn_backend::Device for DispatchDevice {
    fn from_id(mut device_id: DeviceId) -> Self {
        let (dispatch_id, backend_type_id) = Self::decode_type_id(device_id.type_id);
        device_id.type_id = backend_type_id;

        match dispatch_id {
            #[cfg(cube_backend)]
            DispatchDeviceId::Cube => Self::Cube(burn_backend::Device::from_id(device_id)),
            #[cfg(feature = "flex")]
            DispatchDeviceId::Flex => Self::Flex(FlexDevice::from_id(device_id)),
            #[cfg(feature = "ndarray")]
            DispatchDeviceId::NdArray => Self::NdArray(NdArrayDevice::from_id(device_id)),
            #[cfg(feature = "tch")]
            DispatchDeviceId::LibTorch => Self::LibTorch(LibTorchDevice::from_id(device_id)),
            #[cfg(feature = "remote")]
            DispatchDeviceId::Remote => Self::Remote(RemoteDevice::from_id(device_id)),
            #[cfg(feature = "capture")]
            DispatchDeviceId::Capture => Self::Capture(CaptureDevice::from_id(device_id)),
            _ => unreachable!("No backend feature enabled."),
        }
    }

    fn to_id(&self) -> DeviceId {
        let mut device_id: DeviceId = match self {
            #[cfg(not(backend_enabled))]
            Self::Unavailable(never) => never.unreachable(),
            #[cfg(cube_backend)]
            Self::Cube(device) => device.to_id(),
            #[cfg(feature = "flex")]
            Self::Flex(device) => device.to_id(),
            #[cfg(feature = "ndarray")]
            Self::NdArray(device) => device.to_id(),
            #[cfg(feature = "tch")]
            Self::LibTorch(device) => device.to_id(),
            #[cfg(feature = "remote")]
            Self::Remote(device) => device.to_id(),
            #[cfg(feature = "capture")]
            Self::Capture(device) => device.to_id(),
            #[cfg(feature = "autodiff")]
            Self::Autodiff(device) => device.inner.to_id(),
        };
        device_id.type_id = self.encode_type_id(device_id.type_id);
        device_id
    }
}

/// Every cubecl device reaches the one cubecl variant.
#[cfg(cube_backend)]
impl From<CubeDevice> for DispatchDevice {
    fn from(device: CubeDevice) -> Self {
        DispatchDevice::Cube(device)
    }
}

// A runtime's own device type converts too, since that is what its crate hands
// out. There is one variant to reach now, so a wgpu device no longer needs a
// priority chain of gates to decide which of four it lands in.
#[cfg(feature = "cpu")]
impl From<CpuDevice> for DispatchDevice {
    fn from(device: CpuDevice) -> Self {
        DispatchDevice::Cube(CubeDevice::Cpu(device))
    }
}

#[cfg(feature = "cuda")]
impl From<CudaDevice> for DispatchDevice {
    fn from(device: CudaDevice) -> Self {
        DispatchDevice::Cube(CubeDevice::Cuda(device))
    }
}

#[cfg(feature = "rocm")]
impl From<RocmDevice> for DispatchDevice {
    fn from(device: RocmDevice) -> Self {
        DispatchDevice::Cube(CubeDevice::Hip(device))
    }
}

#[cfg(any(
    feature = "wgpu",
    feature = "metal",
    feature = "vulkan",
    feature = "webgpu"
))]
impl From<WgpuDevice> for DispatchDevice {
    fn from(device: WgpuDevice) -> Self {
        DispatchDevice::Cube(CubeDevice::Wgpu(device))
    }
}

#[cfg(feature = "flex")]
impl From<FlexDevice> for DispatchDevice {
    fn from(device: FlexDevice) -> Self {
        DispatchDevice::Flex(device)
    }
}

#[cfg(feature = "ndarray")]
impl From<NdArrayDevice> for DispatchDevice {
    fn from(device: NdArrayDevice) -> Self {
        DispatchDevice::NdArray(device)
    }
}

#[cfg(feature = "tch")]
impl From<LibTorchDevice> for DispatchDevice {
    fn from(device: LibTorchDevice) -> Self {
        DispatchDevice::LibTorch(device)
    }
}

#[cfg(feature = "remote")]
impl From<RemoteDevice> for DispatchDevice {
    fn from(device: RemoteDevice) -> Self {
        DispatchDevice::Remote(device)
    }
}

#[cfg(all(test, not(backend_enabled)))]
mod no_backend_tests {
    #[test]
    #[should_panic(expected = "No execution backend is enabled. Enable a Burn backend feature")]
    fn default_requires_backend() {
        super::DispatchDevice::default();
    }
}

#[cfg(all(test, feature = "capture"))]
mod tests {
    use super::*;
    use burn_backend::Device;

    #[test]
    fn capture_device_id_round_trips_through_dispatch() {
        let device = DispatchDevice::capture();
        let restored = DispatchDevice::from_id(device.to_id());

        assert_eq!(restored, device);
    }
}