| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use everycycle_hal::PciBusAddress; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] |
| 22 |
pub enum Vendor { |
| 23 |
Cuda, |
| 24 |
Rocm, |
| 25 |
Vulkan, |
| 26 |
Cpu, |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] |
| 33 |
pub enum Dtype { |
| 34 |
Fp32, |
| 35 |
Fp16, |
| 36 |
Bf16, |
| 37 |
Fp8E4M3, |
| 38 |
Fp8E5M2, |
| 39 |
Int8, |
| 40 |
Int4, |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)] |
| 46 |
pub struct TensorShape { |
| 47 |
pub dims: Vec<u64>, |
| 48 |
} |
| 49 |
|
| 50 |
impl TensorShape { |
| 51 |
#[must_use] |
| 52 |
pub fn element_count(&self) -> u64 { |
| 53 |
self.dims.iter().product() |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] |
| 61 |
pub struct BoundDevice { |
| 62 |
pub bus_address: PciBusAddress, |
| 63 |
pub display_name: String, |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 76 |
pub struct ActivationHandle(pub u64); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] |
| 88 |
pub struct PortableActivation { |
| 89 |
pub shape: TensorShape, |
| 90 |
pub dtype: Dtype, |
| 91 |
pub bytes: Vec<u8>, |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] |
| 101 |
pub struct SubGraphPlan { |
| 102 |
pub vendor: Vendor, |
| 103 |
pub payload: Vec<u8>, |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
#[derive(Debug)] |
| 109 |
pub enum ExecutorError { |
| 110 |
|
| 111 |
UnsupportedDtype(Dtype), |
| 112 |
|
| 113 |
VendorMismatch { plan: Vendor, executor: Vendor }, |
| 114 |
|
| 115 |
OutOfMemory, |
| 116 |
|
| 117 |
UnknownHandle(ActivationHandle), |
| 118 |
|
| 119 |
|
| 120 |
Backend(String), |
| 121 |
} |
| 122 |
|
| 123 |
impl core::fmt::Display for ExecutorError { |
| 124 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 125 |
match self { |
| 126 |
Self::UnsupportedDtype(d) => write!(f, "executor does not support dtype {d:?}"), |
| 127 |
Self::VendorMismatch { plan, executor } => { |
| 128 |
write!( |
| 129 |
f, |
| 130 |
"plan vendor {plan:?} does not match executor vendor {executor:?}" |
| 131 |
) |
| 132 |
} |
| 133 |
Self::OutOfMemory => f.write_str("executor allocation failed"), |
| 134 |
Self::UnknownHandle(h) => write!(f, "unknown activation handle {h:?}"), |
| 135 |
Self::Backend(s) => write!(f, "backend error: {s}"), |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
impl std::error::Error for ExecutorError {} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
pub trait Executor: Send + Sync { |
| 149 |
|
| 150 |
fn vendor(&self) -> Vendor; |
| 151 |
|
| 152 |
|
| 153 |
fn name(&self) -> &str; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
fn devices(&self) -> &[BoundDevice]; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
fn supported_dtypes(&self) -> &[Dtype]; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
fn alloc_activation( |
| 171 |
&self, |
| 172 |
shape: &TensorShape, |
| 173 |
dtype: Dtype, |
| 174 |
) -> Result<ActivationHandle, ExecutorError>; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
fn release_activation(&self, handle: ActivationHandle); |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
fn execute( |
| 189 |
&self, |
| 190 |
plan: &SubGraphPlan, |
| 191 |
inputs: &[ActivationHandle], |
| 192 |
) -> Result<Vec<ActivationHandle>, ExecutorError>; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
fn export_activation( |
| 212 |
&self, |
| 213 |
handle: ActivationHandle, |
| 214 |
) -> Result<PortableActivation, ExecutorError>; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
fn import_activation( |
| 229 |
&self, |
| 230 |
portable: &PortableActivation, |
| 231 |
) -> Result<ActivationHandle, ExecutorError>; |
| 232 |
} |
| 233 |
|
| 234 |
#[cfg(test)] |
| 235 |
mod tests { |
| 236 |
use super::*; |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn shape_element_count() { |
| 240 |
let s = TensorShape { |
| 241 |
dims: vec![2, 3, 4], |
| 242 |
}; |
| 243 |
assert_eq!(s.element_count(), 24); |
| 244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn error_display_renders() { |
| 248 |
let e = ExecutorError::VendorMismatch { |
| 249 |
plan: Vendor::Cuda, |
| 250 |
executor: Vendor::Rocm, |
| 251 |
}; |
| 252 |
let rendered = e.to_string(); |
| 253 |
assert!(rendered.contains("Cuda")); |
| 254 |
assert!(rendered.contains("Rocm")); |
| 255 |
} |
| 256 |
} |
| 257 |
|