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
// `burn-backend-extension` owns the authoritative backend catalog and derives the distributed list
// and transfer matrix from it. Keep only these consumer wrappers local and visible to
// rust-analyzer; having the proc macro generate `macro_rules!` definitions made IDE resolution
// unreliable.
macro_rules! backend_list {
    ($callback:ident, $($extra:tt)*) => {
        burn_backend_extension::backend_catalog!(backends, $callback; $($extra)*)
    };
}

macro_rules! distributed_backend_list {
    ($callback:ident, $($extra:tt)*) => {
        burn_backend_extension::backend_catalog!(distributed, $callback; $($extra)*)
    };
}

macro_rules! backend_matrix {
    ($callback:ident, $($extra:tt)*) => {
        burn_backend_extension::backend_catalog!(matrix, $callback; $($extra)*)
    };
}

#[cfg(feature = "autodiff")]
/// Helper to map the runtime strategy to the compile-time Autodiff generic.
macro_rules! with_autodiff_backend {
    ($Backend:ident, $checkpointing:expr, |$B:ident| $body:expr) => {
        match $checkpointing {
            $crate::GradientCheckpointingStrategy::Balanced => {
                type $B = $crate::backends::Autodiff<
                    $crate::backends::$Backend,
                    burn_autodiff::checkpoint::strategy::BalancedCheckpointing,
                >;
                $body
            }
            $crate::GradientCheckpointingStrategy::Disabled => {
                type $B = $crate::backends::Autodiff<
                    $crate::backends::$Backend,
                    burn_autodiff::checkpoint::strategy::NoCheckpointing,
                >;
                $body
            }
        }
    };
}

/// Match arm generator for `dispatch_device`.
/// Maps each backend variant to a block where the specific backend type is bound to `B`.
macro_rules! dispatch_device_arms {
    (
        $device:expr,
        |$inner:ident| $body:expr;
        $([$Backend:ident, $cfg:meta]),*
    ) => {
        match $device {
            // Autodiff arm first
            #[cfg(feature = "autodiff")]
            $crate::DispatchDevice::Autodiff(inner) => {
                // Recursively dispatch on inner
                dispatch_device_arms!(
                    @autodiff
                    &**inner,
                    |$inner| $body;
                    $([$Backend, $cfg]),*
                )
            },
            $(
                #[cfg($cfg)]
                $crate::DispatchDevice::$Backend($inner) => {
                    type B = $crate::backends::$Backend;
                    $body
                }
            )*
            #[allow(unreachable_patterns)]
            other => panic!("Distributed operations are not supported for device {other:?}"),
        }
    };
    (
        @autodiff
        $device:expr,
        |$inner:ident| $body:expr;
        $([$Backend:ident, $cfg:meta]),*
    ) => {
        match $device {
            $(
                #[cfg($cfg)]
                $crate::DispatchDevice::$Backend($inner) => {
                    type B = $crate::backends::Autodiff<$crate::backends::$Backend>;
                    $body
                }
            )*
            $crate::DispatchDevice::Autodiff(_) => unreachable!("Autodiff should not wrap an autodiff device."),
            #[allow(unreachable_patterns)]
            other => panic!("Distributed operations are not supported for device {other:?}"),
        }
    };
}

/// Dispatches an operation body based on the provided device.
macro_rules! dispatch_device {
    ($device:expr, |$inner:ident| $body:expr) => {
        dispatch_device!(@internal backend_list, $device, |$inner| $body)
    };
    (@distributed $device:expr, |$inner:ident| $body:expr) => {
        dispatch_device!(@internal distributed_backend_list, $device, |$inner| $body)
    };
    (@internal $list_macro:ident, $device:expr, |$inner:ident| $body:expr) => {
        $list_macro!(dispatch_device_arms, $device, |$inner| $body)
    };
}

/// Match arm generator for `to_device`.
/// Handles the logic for same-backend transfers (fast path) and cross-backend
/// transfers by generating a grid of all device combinations provided via `backend_matrix`.
macro_rules! to_device_arms {
    (
        $kind:ident, $inner_fn:ident, $tensor:expr, $device:expr, $to_device:ident, |$inner:ident, $device_ident:ident| $body:expr;
        $( [$B1:ident, $src_cfg:meta] => [ $( [$B2:ident, $dst_cfg:meta] ),+ ] );*
    ) => {
        #[allow(unreachable_patterns)]
        match ($tensor.kind, $device) {
            // Capture is deliberately absent from the cross-backend matrix: it accepts concrete
            // initializer values, but a captured tensor cannot be materialized on another backend.
            #[cfg(feature = "capture")]
            ($crate::DispatchTensorKind::Capture(t), $crate::DispatchDevice::Capture(d)) => {
                $crate::DispatchTensor {
                    kind: $crate::DispatchTensorKind::Capture($crate::BackendTensor::$kind(
                        $crate::backends::Capture::$to_device(t.$inner_fn(), d)
                    )),
                    autodiff: $tensor.autodiff,
                }
            }
            // --- Same backend to_device ---
            $(
                #[cfg($src_cfg)]
                ($crate::DispatchTensorKind::$B1(t), $crate::DispatchDevice::$B1(d)) => {
                    $crate::DispatchTensor {
                        kind: $crate::DispatchTensorKind::$B1($crate::BackendTensor::$kind(
                            $crate::backends::$B1::$to_device(t.$inner_fn(), d)
                        )),
                        autodiff: $tensor.autodiff,
                    }
                }
            )*

            // Any concrete backend can be materialized on the capture backend.
            // This is how ordinary module parameters and sample inputs become
            // locally retained capture initializers.
            $(
                #[cfg(all($src_cfg, feature = "capture"))]
                ($crate::DispatchTensorKind::$B1(t), $crate::DispatchDevice::Capture($device_ident)) => {
                    type B1 = $crate::backends::$B1;
                    type B2 = $crate::backends::Capture;
                    let $inner = t.$inner_fn();
                    $crate::DispatchTensor {
                        kind: $crate::DispatchTensorKind::Capture(
                            $crate::BackendTensor::$kind($body)
                        ),
                        autodiff: $tensor.autodiff,
                    }
                }
            )*

            // --- Cross backend arms ---
            // This loop generates the grid of combinations
            $(
                $(
                    #[cfg(all($src_cfg, $dst_cfg))]
                    ($crate::DispatchTensorKind::$B1(t), $crate::DispatchDevice::$B2($device_ident)) => {
                        type B1 = $crate::backends::$B1;
                        type B2 = $crate::backends::$B2;
                        let $inner = t.$inner_fn();

                        $crate::DispatchTensor {
                            kind: $crate::DispatchTensorKind::$B2(
                                $crate::BackendTensor::$kind($body)
                            ),
                            autodiff: $tensor.autodiff,
                        }
                    }
                )+
            )*
            // --- To autodiff ---
            // This can happen when moving a bool or int tensor to the device of a float autodiff tensor.
            // We move it to the inner device and preserve the checkpointing strategy.

            // --- Same backend to_device ---
            $(
                #[cfg(all($src_cfg, feature = "autodiff"))]
                ($crate::DispatchTensorKind::$B1(t), $crate::DispatchDevice::Autodiff(device_ad))
                if matches!(&*device_ad.inner, $crate::DispatchDevice::$B1(_)) => {
                    let $crate::DispatchDevice::$B1(d) = &*device_ad.inner else { unreachable!() };

                    $crate::DispatchTensor {
                        kind: $crate::DispatchTensorKind::$B1($crate::BackendTensor::$kind(
                            <$crate::backends::$B1>::$to_device(t.$inner_fn(), d)
                        )),
                        autodiff: $crate::DispatchAutodiffContext::Enabled(
                            device_ad.checkpointing,
                        ),
                    }
                }
            )*

            // --- Same backend to_device ---
            $(
                $(
                    #[cfg(all($src_cfg, $dst_cfg, feature = "autodiff"))]
                    ($crate::DispatchTensorKind::$B1(tensor), $crate::DispatchDevice::Autodiff(device_ad))
                    if matches!(&*device_ad.inner, $crate::DispatchDevice::$B2(_)) => {
                        let $crate::DispatchDevice::$B2($device_ident) = &*device_ad.inner else { unreachable!() };
                        type B1 = $crate::backends::$B1;
                        type B2 = $crate::backends::$B2;
                        let $inner = tensor.$inner_fn();

                        $crate::DispatchTensor {
                            kind: $crate::DispatchTensorKind::$B2(
                                $crate::BackendTensor::$kind($body)
                            ),
                            autodiff: $crate::DispatchAutodiffContext::Enabled(
                                device_ad.checkpointing,
                            ),
                        }

                    },
                )+
            )*
            #[cfg(all(feature = "autodiff", feature = "capture"))]
            (_, $crate::DispatchDevice::Autodiff(device))
            if matches!(&*device.inner, $crate::DispatchDevice::Capture(_)) => {
                panic!("Cannot move a tensor to a capture device with autodiff enabled; remove autodiff with without_autodiff() before the transfer.")
            }
            #[cfg(feature = "autodiff")]
            (_, $crate::DispatchDevice::Autodiff(_)) => panic!("Cannot move this tensor to the requested autodiff device: this transfer is not supported."),
            #[cfg(feature = "autodiff")]
            ($crate::DispatchTensorKind::Autodiff(..), _) => panic!("Operation not marked for autodiff."),
            // Capture is intentionally one-way: initialized values can be moved onto a
            // capture device, but captured tensors have no materialized data to move back.
            #[cfg(feature = "capture")]
            ($crate::DispatchTensorKind::Capture(_), _) => {
                panic!("Cannot move a tensor from a capture device")
            }
        }
    };
}

/// Handles tensor movement between devices, supporting both same-backend transfers
/// and cross-backend dispatches.
macro_rules! to_device {
    ($kind:ident, $inner_fn:ident, $tensor:expr, $device:expr, $to_device:ident, |$inner:ident, $device_ident:ident| $body:expr) => {
        backend_matrix!(
            to_device_arms,
            $kind,
            $inner_fn,
            $tensor,
            $device,
            $to_device,
            |$inner, $device_ident| $body
        )
    };
}

/// Match arm generator for `float_to_device`.
///
/// Similar to `to_device_arms`, but float tensors are checked for autodiff support.
macro_rules! float_to_device_arms {
    (
        $tensor:expr, $device:expr, $to_device:ident, |$inner:ident, $device_ident:ident| $body:expr;
        $( [$B1:ident, $src_cfg:meta] => [ $( [$B2:ident, $dst_cfg:meta] ),+ ] );*
    ) => {
        #[allow(unreachable_patterns)]
        match ($tensor.kind, $device) {
            #[cfg(feature = "autodiff")]
            ($crate::DispatchTensorKind::Autodiff(kind), $crate::DispatchDevice::Autodiff(device)) => {
                // No transfer arm consumes the strategy in capture-only builds.
                let $crate::DispatchAutodiffContext::Enabled(_ckp) = $tensor.autodiff else {
                    panic!("an autodiff float primitive must have an enabled autodiff context")
                };
                float_to_device_arms!(
                    @autodiff
                    *kind, &**device, _ckp, $to_device;
                    $([$B1, $src_cfg] => [ $([$B2, $dst_cfg]),+ ]);*
                )

            }
            // Capture is deliberately absent from the cross-backend matrix. Same-backend movement
            // remains available; CaptureBackend decides whether the particular device transfer is
            // valid (computed tensors can only remain in their capture session).
            #[cfg(feature = "capture")]
            ($crate::DispatchTensorKind::Capture(kind), $crate::DispatchDevice::Capture(d)) => {
                $crate::DispatchTensor {
                    kind: $crate::DispatchTensorKind::Capture($crate::BackendTensor::Float(
                        $crate::backends::Capture::$to_device(kind.float(), d)
                    )),
                    autodiff: $tensor.autodiff,
                }
            }
            // --- Same backend to_device ---
            $(
                #[cfg($src_cfg)]
                ($crate::DispatchTensorKind::$B1(kind), $crate::DispatchDevice::$B1(d)) => {
                    $crate::DispatchTensor {
                        kind: $crate::DispatchTensorKind::$B1($crate::BackendTensor::Float(
                            $crate::backends::$B1::$to_device(kind.float(), d)
                        )),
                        autodiff: $tensor.autodiff,
                    }
                }
            )*

            // Materialize float tensors from any backend on capture.
            $(
                #[cfg(all($src_cfg, feature = "capture"))]
                ($crate::DispatchTensorKind::$B1(kind), $crate::DispatchDevice::Capture($device_ident)) => {
                    type B1 = $crate::backends::$B1;
                    type B2 = $crate::backends::Capture;
                    let $inner = kind.float();
                    $crate::DispatchTensor {
                        kind: $crate::DispatchTensorKind::Capture(
                            $crate::BackendTensor::Float($body)
                        ),
                        autodiff: $tensor.autodiff,
                    }
                }
            )*

            // --- Cross backend arms ---
            // This loop generates the grid of combinations
            $(
                $(
                    #[cfg(all($src_cfg, $dst_cfg))]
                    ($crate::DispatchTensorKind::$B1(kind), $crate::DispatchDevice::$B2($device_ident)) => {
                        type B1 = $crate::backends::$B1;
                        type B2 = $crate::backends::$B2;
                        let $inner = kind.float();

                        $crate::DispatchTensor {
                            kind: $crate::DispatchTensorKind::$B2($crate::BackendTensor::Float($body)),
                            autodiff: $tensor.autodiff,
                        }
                    }
                )+
            )*
            #[cfg(feature = "autodiff")]
            ($crate::DispatchTensorKind::Autodiff(..), _) | (_, $crate::DispatchDevice::Autodiff(_)) => panic!("Cannot move between autodiff and non-autodiff instances."),
            // Capture is intentionally one-way: initialized values can be moved onto a
            // capture device, but captured tensors have no materialized data to move back.
            #[cfg(feature = "capture")]
            ($crate::DispatchTensorKind::Capture(_), _) => {
                panic!("Cannot move a tensor from a capture device")
            }
        }
    };

    // Autodiff(DispatchTensor)
    (
        @autodiff
        $tensor:expr, $device:expr, $ckp:expr, $to_device:ident;
        $( [$B1:ident, $src_cfg:meta] => [ $( [$B2:ident, $dst_cfg:meta] ),+ ] );*
    ) => {{
        match ($tensor, $device) {
            // --- Same backend to_device ---
            $(
                #[cfg($src_cfg)]
                ($crate::DispatchTensorKind::$B1(tensor), $crate::DispatchDevice::$B1(d)) => {
                    let kind = $crate::DispatchTensorKind::Autodiff(alloc::boxed::Box::new($crate::DispatchTensorKind::$B1($crate::BackendTensor::Autodiff(
                        with_autodiff_backend!($B1, $ckp, |B| {
                            B::$to_device(tensor.autodiff(), d)
                        })
                    ))));
                    $crate::DispatchTensor {
                        kind,
                        autodiff: $crate::DispatchAutodiffContext::Enabled($ckp),
                    }
                }
            )*
            $(
                $(
                    #[cfg(all($src_cfg, $dst_cfg))]
                    ($crate::DispatchTensorKind::$B1(tensor), $crate::DispatchDevice::$B2(d)) => {
                        let output = with_autodiff_backend!($B1, $ckp, |B| {
                            B::to_backend::<
                                $crate::backends::$B2,
                                $crate::ops::transfer::HostTransfer,
                            >(tensor.autodiff(), d)
                        });
                        $crate::DispatchTensor {
                            kind: $crate::DispatchTensorKind::Autodiff(alloc::boxed::Box::new(
                                $crate::DispatchTensorKind::$B2($crate::BackendTensor::Autodiff(output))
                            )),
                            autodiff: $crate::DispatchAutodiffContext::Enabled($ckp),
                        }
                    }
                )+
            )*
            #[cfg(feature = "capture")]
            (_, $crate::DispatchDevice::Capture(_)) => {
                panic!("Cannot move a tensor to a capture device with autodiff enabled; remove autodiff with without_autodiff() before the transfer.")
            }
            #[allow(unreachable_patterns)]
            (_, _) => panic!("Cannot move this autodiff tensor between the requested backends.")
        }
    }};
}

/// Handles float tensor movement between devices (that might support autodiff).
macro_rules! float_to_device {
    ($kind:ident, $inner_fn:ident, $tensor:expr, $device:expr, $to_device:ident, |$inner:ident, $device_ident:ident| $body:expr) => {
        backend_matrix!(
            float_to_device_arms,
            $tensor,
            $device,
            $to_device,
            |$inner, $device_ident| $body
        )
    };
}

/// Unwraps a `Vec<DispatchTensor>` for a known backend.
macro_rules! unwrap_vec {
    ($Backend:ident, $vec:expr, $kind:ident) => {
        $vec.into_iter()
            .map(|t| match t.kind {
                $crate::DispatchTensorKind::$Backend(inner) => inner.$kind(),
                #[allow(unreachable_patterns)]
                _ => panic!(
                    "Tensor is on the wrong backend (expected {}).",
                    stringify!($Backend)
                ),
            })
            .collect::<Vec<_>>()
    };

    // Autodiff-wrapped backend
    (@autodiff $Backend:ident, $vec:expr, $kind:ident) => {
        $vec.into_iter()
            .map(|t| match t.kind {
                $crate::DispatchTensorKind::Autodiff(inner) => match *inner {
                    $crate::DispatchTensorKind::$Backend(inner) => inner.$kind(),
                    _ => panic!(
                        "Autodiff float tensor is on the wrong backend (expected {}).",
                        stringify!($Backend)
                    ),
                },
                _ => panic!(
                    "Expected autodiff-wrapped float tensor for backend {}.",
                    stringify!($Backend)
                ),
            })
            .collect::<Vec<_>>()
    };
}

/// Match arm generator for `transaction_op`.
macro_rules! transaction_op_arms {
    ($tx:ident, $first:expr; $([$Backend:ident, $cfg:meta]),*) => {{
        match &$first.kind {
            #[cfg(not(backend_enabled))]
            $crate::DispatchTensorKind::Unavailable(never) => never.unreachable(),
            // Autodiff arm first
            #[cfg(feature = "autodiff")]
            $crate::DispatchTensorKind::Autodiff(inner) => {
                // Recursively dispatch on inner
                match **inner {
                    #[cfg(not(backend_enabled))]
                    $crate::DispatchTensorKind::Unavailable(ref never) => never.unreachable(),
                    $(
                    #[cfg($cfg)]
                    $crate::DispatchTensorKind::$Backend(_) => {
                        type B = $crate::backends::$Backend;

                        // Unwrap vec
                        let floats = unwrap_vec!(@autodiff $Backend, $tx.read_floats, autodiff_inner);
                        let ints = unwrap_vec!($Backend, $tx.read_ints, int);
                        let bools = unwrap_vec!($Backend, $tx.read_bools, bool);
                        // Not supported
                        let qfloats = $tx.read_qfloats.into_iter().map(|_t| todo!("Quantization not supported yet")).collect();

                        B::tr_execute(TransactionPrimitive::new(floats, qfloats, ints, bools)).await
                    }
                )*
                    $crate::DispatchTensorKind::Autodiff(..) => unreachable!("Autodiff should not wrap an autodiff tensor.")
                }
            },

            $(
                #[cfg($cfg)]
                $crate::DispatchTensorKind::$Backend(_) => {
                    type B = $crate::backends::$Backend;

                    // Unwrap vec
                    let floats = unwrap_vec!($Backend, $tx.read_floats, float);
                    let ints = unwrap_vec!($Backend, $tx.read_ints, int);
                    let bools = unwrap_vec!($Backend, $tx.read_bools, bool);
                    // Not supported
                    let qfloats = $tx.read_qfloats.into_iter().map(|_t| todo!("Quantization not supported yet")).collect();

                    B::tr_execute(TransactionPrimitive::new(floats, qfloats, ints, bools)).await
                }
            )*
        }
    }};
}

/// Helper to dispatch a transaction based on the first available tensor.
macro_rules! transaction_op {
    ($tx:ident, $first:expr) => {
        backend_list!(transaction_op_arms, $tx, $first)
    };
}