use serde::{Deserialize, Serialize};
use super::impl_id::ImplId;
use super::structure_key::StructureKeyToken;
pub const TELEMETRY_SCHEMA_VERSION: u32 = 2;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct HwStamp {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub compute_capability: Option<(u32, u32)>,
pub hardware_sku: String,
pub driver_version: String,
}
impl HwStamp {
pub fn from_descriptor(desc: &fuel_ir::probe::DeviceDescriptor) -> Self {
Self {
compute_capability: desc.compute_capability,
hardware_sku: desc.hardware_sku.clone(),
driver_version: desc.driver_version.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DispatchRecord {
pub schema: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub structure_key: Option<StructureKeyToken>,
pub chosen: ImplId,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub candidates: Vec<Candidate>,
pub count: u64,
pub hw: HwStamp,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Candidate {
pub impl_id: ImplId,
#[serde(skip_serializing_if = "Option::is_none")]
pub latency_ns: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MissRecord {
pub schema: u32,
pub wanted: StructureKeyToken,
pub fallback: ImplId,
pub count: u64,
pub hw: HwStamp,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::telemetry::{ImplId, StructureKeyToken};
use fuel_ir::dispatch::OpKind;
use fuel_ir::{BackendId, DType};
fn baracuda_impl() -> ImplId {
ImplId {
backend: BackendId::Cuda,
op: OpKind::MatMul,
dtypes: vec![DType::F16, DType::F16, DType::F16],
kernel_source: "baracuda".into(),
kernel_revision_hash: 0x8f3c1a,
}
}
fn cuda_stamp() -> HwStamp {
HwStamp {
compute_capability: Some((8, 9)),
hardware_sku: "NVIDIA GeForce RTX 4070".into(),
driver_version: "552.44".into(),
}
}
fn cpu_stamp() -> HwStamp {
HwStamp {
compute_capability: None,
hardware_sku: "Intel(R) Core(TM) i9-14900K".into(),
driver_version: "n/a".into(),
}
}
#[test]
fn dispatch_record_round_trips_as_one_jsonl_line() {
let rec = DispatchRecord {
schema: TELEMETRY_SCHEMA_VERSION,
structure_key: Some(StructureKeyToken("mm:innerdiv16:vec8:f16".into())),
chosen: baracuda_impl(),
candidates: vec![
Candidate {
impl_id: baracuda_impl(),
latency_ns: Some(41_230),
},
Candidate {
impl_id: ImplId {
kernel_source: "cublas".into(),
..baracuda_impl()
},
latency_ns: Some(48_800),
},
],
count: 1024,
hw: cuda_stamp(),
};
let line = serde_json::to_string(&rec).expect("serialize");
assert!(!line.contains('\n'), "JSONL record must be a single line");
let back: DispatchRecord = serde_json::from_str(&line).expect("deserialize");
assert_eq!(rec, back);
}
#[test]
fn miss_record_round_trips_and_has_no_est_speedup() {
let miss = MissRecord {
schema: TELEMETRY_SCHEMA_VERSION,
wanted: StructureKeyToken("mm:innerdiv16:vec8:flipped:f16".into()),
fallback: ImplId {
kernel_source: "baracuda-generic-strided".into(),
..baracuda_impl()
},
count: 37,
hw: cuda_stamp(),
};
let line = serde_json::to_string(&miss).expect("serialize");
assert!(!line.contains('\n'), "JSONL record must be a single line");
assert!(!line.contains("est_speedup"), "est_speedup must be omitted");
let back: MissRecord = serde_json::from_str(&line).expect("deserialize");
assert_eq!(miss, back);
}
#[test]
fn empty_candidates_are_omitted_from_the_wire() {
let rec = DispatchRecord {
schema: TELEMETRY_SCHEMA_VERSION,
structure_key: None,
chosen: baracuda_impl(),
candidates: vec![],
count: 5,
hw: cuda_stamp(),
};
let line = serde_json::to_string(&rec).expect("serialize");
assert!(
!line.contains("candidates"),
"empty candidates must be omitted"
);
assert!(
!line.contains("structure_key"),
"None structure_key must be omitted"
);
}
#[test]
fn hw_stamp_serializes_and_handles_non_cuda_none() {
let rec = DispatchRecord {
schema: TELEMETRY_SCHEMA_VERSION,
structure_key: None,
chosen: baracuda_impl(),
candidates: vec![],
count: 1,
hw: cuda_stamp(),
};
let line = serde_json::to_string(&rec).expect("serialize");
assert!(line.contains("compute_capability"), "CUDA stamp carries CC");
assert!(line.contains("hardware_sku"), "sku present");
assert!(line.contains("driver_version"), "driver present");
let back: DispatchRecord = serde_json::from_str(&line).expect("deserialize");
assert_eq!(rec, back);
assert_eq!(back.hw.compute_capability, Some((8, 9)));
let miss = MissRecord {
schema: TELEMETRY_SCHEMA_VERSION,
wanted: StructureKeyToken("mm:innerdiv16:vec8:f16".into()),
fallback: baracuda_impl(),
count: 3,
hw: cpu_stamp(),
};
let line = serde_json::to_string(&miss).expect("serialize");
assert!(
!line.contains("compute_capability"),
"None CC must be omitted from the wire"
);
assert!(
line.contains("hardware_sku") && line.contains("driver_version"),
"sku/driver stay"
);
let back: MissRecord = serde_json::from_str(&line).expect("deserialize");
assert_eq!(miss, back);
assert_eq!(back.hw.compute_capability, None);
}
#[test]
fn telemetry_schema_version_is_v2_for_hw_stamp() {
assert_eq!(TELEMETRY_SCHEMA_VERSION, 2);
}
#[test]
fn hw_stamp_from_descriptor_projects_probe_fields() {
use fuel_ir::probe::DeviceDescriptor;
use fuel_ir::{BackendId, DeviceLocation};
let cuda = DeviceDescriptor {
backend: BackendId::Cuda,
device_index: 0,
hardware_sku: "NVIDIA GeForce RTX 4070".into(),
vendor_id: 0x10DE,
device_id: 0x2786,
compute_capability: Some((8, 9)),
subgroup_width: Some(32),
driver_version: "552.44".into(),
total_memory_bytes: 8 * 1024 * 1024 * 1024,
location: DeviceLocation::Cuda { gpu_id: 0 },
};
let stamp = HwStamp::from_descriptor(&cuda);
assert_eq!(stamp.compute_capability, Some((8, 9)));
assert_eq!(stamp.hardware_sku, "NVIDIA GeForce RTX 4070");
assert_eq!(stamp.driver_version, "552.44");
let cpu = DeviceDescriptor {
backend: BackendId::Cpu,
device_index: 0,
hardware_sku: "Intel(R) Core(TM) i9-14900K".into(),
vendor_id: 0,
device_id: 0,
compute_capability: None,
subgroup_width: None,
driver_version: "n/a".into(),
total_memory_bytes: 0,
location: DeviceLocation::Cpu,
};
let stamp = HwStamp::from_descriptor(&cpu);
assert_eq!(
stamp.compute_capability, None,
"CPU path ⇒ no compute capability"
);
assert_eq!(stamp.hardware_sku, "Intel(R) Core(TM) i9-14900K");
}
}