burn_jit/
lib.rs

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
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

//! Burn JIT Backend

#[macro_use]
extern crate derive_new;
extern crate alloc;

mod ops;

/// Kernel module
pub mod kernel;
/// Tensor module.
pub mod tensor;

pub(crate) mod tune;

/// Elements for JIT backend
pub mod element;

use burn_tensor::backend::{DeviceId, DeviceOps};
use cubecl::{compute::CubeTask, Feature, Runtime};
pub use element::{FloatElement, IntElement, JitElement};

mod backend;
mod bridge;

pub use backend::*;
pub use bridge::*;

// Re-export cubecl.
pub use cubecl;

mod tune_key;
pub use tune_key::JitAutotuneKey;

#[cfg(any(feature = "fusion", test))]
mod fusion;

#[cfg(feature = "template")]
/// Module for compiling custom non-jit kernels
pub mod template;

#[cfg(feature = "export_tests")]
pub mod tests;

/// Just-in-Time runtime extending the [cube runtime](Runtime).
pub trait JitRuntime: Runtime<Device = Self::JitDevice, Server = Self::JitServer> {
    /// The device that should also implement [burn_tensor::backend::DeviceOps].
    type JitDevice: burn_tensor::backend::DeviceOps;
    /// The cube server with the [JitAutotuneKey].
    type JitServer: cubecl::server::ComputeServer<
        Kernel = Box<dyn CubeTask<Self::Compiler>>,
        Feature = Feature,
    >;
}

/// ID used to identify a Just-in-Time environment.
#[derive(Hash, PartialEq, Eq, Debug, Clone)]
pub struct JitTuneId {
    device: DeviceId,
    name: &'static str,
}

impl JitTuneId {
    /// Create a new ID.
    pub fn new<R: JitRuntime>(device: &R::Device) -> Self {
        Self {
            device: DeviceOps::id(device),
            name: R::name(),
        }
    }
}

impl core::fmt::Display for JitTuneId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "device-{}-{}-{}",
            self.device.type_id, self.device.index_id, self.name
        ))
    }
}