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
use crate::{codegen::Compiler, tensor::JitTensor, PrecisionBridge, Runtime};
use burn_tensor::backend::Backend;
use rand::{rngs::StdRng, SeedableRng};
use std::{marker::PhantomData, sync::Mutex};

pub(crate) static SEED: Mutex<Option<StdRng>> = Mutex::new(None);

/// Generic tensor backend that can be compiled just-in-time to any shader runtime
#[derive(new)]
pub struct JitBackend<R: Runtime> {
    _runtime: PhantomData<R>,
}

impl<R: Runtime> Backend for JitBackend<R> {
    type Device = R::Device;

    type FullPrecisionBridge = PrecisionBridge<R::FullPrecisionRuntime>;
    type FloatElem = <R::Compiler as Compiler>::Float;
    type IntElem = <R::Compiler as Compiler>::Int;

    type FloatTensorPrimitive<const D: usize> = JitTensor<R, Self::FloatElem, D>;
    type IntTensorPrimitive<const D: usize> = JitTensor<R, Self::IntElem, D>;
    type BoolTensorPrimitive<const D: usize> = JitTensor<R, u32, D>;

    fn name() -> String {
        format!("jit<{}>", R::name())
    }

    fn seed(seed: u64) {
        let rng = StdRng::seed_from_u64(seed);
        let mut seed = SEED.lock().unwrap();
        *seed = Some(rng);
    }

    fn ad_enabled() -> bool {
        false
    }

    fn sync(device: &Self::Device) {
        let client = R::client(device);
        client.sync();
    }
}

impl<R: Runtime> core::fmt::Debug for JitBackend<R> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("JitBackend {{ runtime: {}}}", R::name()))
    }
}

impl<R: Runtime> Clone for JitBackend<R> {
    fn clone(&self) -> Self {
        Self::new()
    }
}

impl<R: Runtime> Default for JitBackend<R> {
    fn default() -> Self {
        Self::new()
    }
}