Skip to main content

burn_router/channel/
base.rs

1use alloc::string::String;
2use burn_backend::{DType, Shape, backend::DeviceOps};
3use burn_ir::TensorIr;
4
5use crate::{MultiBackendBridge, RouterClient, RouterTensor, get_client};
6
7/// Type alias for `<Br as MultiBackendBridge>::TensorHandle`.
8pub type TensorHandle<Br> = <Br as MultiBackendBridge>::TensorHandle;
9
10/// Defines the connection channel and operations for a setup with multiple backend router clients.
11pub trait RouterChannel: Clone + Send + Sync + 'static + Sized {
12    /// Device type.
13    type Device: DeviceOps;
14    /// A bridge that can transfer tensors between multiple backends.
15    type Bridge: MultiBackendBridge<Device = Self::Device>;
16    /// Client type.
17    type Client: RouterClient<Device = Self::Device>;
18
19    /// Name of the channel.
20    fn name(device: &Self::Device) -> String;
21
22    /// Initialize a new unscoped client for the given device.
23    ///
24    /// [`get_client`] calls this on a cache miss and retains the result in the global client
25    /// locator. Channels whose client lifetime is externally scoped can instead construct their
26    /// client directly and install it with [`crate::register_scoped_client`]; that path deliberately
27    /// bypasses this method.
28    fn init_client(device: &Self::Device) -> Self::Client;
29
30    /// Get the tensor handle corresponding to the [tensor representation](TensorIr).
31    fn get_tensor_handle(tensor: &TensorIr, client: &Self::Client) -> TensorHandle<Self::Bridge>;
32
33    /// Create a tensor with the given handle and shape.
34    fn register_tensor(
35        client: &Self::Client,
36        handle: TensorHandle<Self::Bridge>,
37        shape: Shape,
38        dtype: DType,
39    ) -> RouterTensor<Self::Client>;
40
41    /// Change the tensor to a different client backend.
42    fn change_client_backend(
43        tensor: RouterTensor<Self::Client>,
44        device: &Self::Device, // target device
45    ) -> RouterTensor<Self::Client> {
46        // Get tensor handle from current client
47        let original_client = tensor.client.clone();
48        let desc = tensor.into_ir();
49        let mut handle = Self::get_tensor_handle(&desc, &original_client);
50
51        if desc.dtype.is_float() {
52            handle = Self::Bridge::change_backend_float(handle, desc.shape.clone(), device);
53        } else if desc.dtype.is_int() {
54            handle = Self::Bridge::change_backend_int(handle, desc.shape.clone(), device);
55        } else if desc.dtype.is_bool() {
56            handle = Self::Bridge::change_backend_bool(handle, desc.shape.clone(), device);
57        } else {
58            unimplemented!()
59        }
60
61        // Register tensor handle on target client
62        let target_client = get_client::<Self>(device);
63        Self::register_tensor(&target_client, handle, desc.shape, desc.dtype)
64    }
65}