use candle_core::{DType, Device, DeviceLocation, Tensor};
use crate::instrument::SpanId;
use crate::instrument::{OpRecord, TensorRecord, TraceSession};
use crate::phase::ExecutionStep;
use crate::trace::memory::{category_for_step, dense_tensor_bytes};
use crate::trace::MemoryCategory;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CandleCapture {
pub storage_id: String,
pub tensor_id: String,
pub label: Option<String>,
pub shape: Vec<usize>,
pub dtype: String,
pub device: String,
pub tensor_bytes: u64,
pub requires_grad: bool,
pub category: MemoryCategory,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CandleOpCapture {
pub op_name: String,
pub inputs: Vec<String>,
pub output: CandleCapture,
pub input_tensor_bytes: u64,
pub duration_ns: u64,
pub timestamp_ns: u64,
}
pub fn tensor_id(t: &Tensor) -> String {
format!("{id:?}", id = t.id())
}
pub fn storage_id(t: &Tensor) -> String {
let (storage, _) = t.storage_and_layout();
format!("storage:{:p}", &*storage)
}
pub fn device_label(device: &Device) -> String {
match device.location() {
DeviceLocation::Cpu => "cpu".into(),
DeviceLocation::Cuda { gpu_id } => format!("cuda:{gpu_id}"),
DeviceLocation::Metal { gpu_id } => format!("metal:{gpu_id}"),
}
}
pub fn dtype_label(dtype: DType) -> String {
format!("{dtype:?}").to_ascii_lowercase()
}
pub fn tensor_dense_bytes(t: &Tensor) -> u64 {
dense_tensor_bytes(t.dims(), &dtype_label(t.dtype()))
.expect("every Candle dtype has a known byte width")
}
impl CandleCapture {
pub fn from_tensor(t: &Tensor, step: Option<ExecutionStep>) -> Self {
let requires_grad = t.is_variable();
let category = if requires_grad {
MemoryCategory::Parameter
} else {
category_for_step(step, false)
};
Self {
storage_id: storage_id(t),
tensor_id: tensor_id(t),
label: None,
shape: t.dims().to_vec(),
dtype: dtype_label(t.dtype()),
device: device_label(t.device()),
tensor_bytes: tensor_dense_bytes(t),
requires_grad,
category,
}
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
}
impl CandleOpCapture {
pub fn new(
op_name: impl Into<String>,
inputs: Vec<String>,
output: &Tensor,
input_bytes: u64,
duration_ns: u64,
timestamp_ns: u64,
step: Option<ExecutionStep>,
) -> Self {
Self {
op_name: op_name.into(),
inputs,
output: CandleCapture::from_tensor(output, step),
input_tensor_bytes: input_bytes,
duration_ns,
timestamp_ns,
}
}
}
pub fn inputs_dense_bytes(tensors: &[&Tensor]) -> u64 {
tensors.iter().map(|t| tensor_dense_bytes(t)).sum()
}
pub fn record_tensor(
session: &TraceSession,
span_id: SpanId,
cap: &CandleCapture,
) -> anyhow::Result<()> {
session.record_tensor(
span_id,
TensorRecord {
tensor_id: &cap.tensor_id,
label: cap.label.as_deref(),
shape: &cap.shape,
dtype: &cap.dtype,
device: &cap.device,
requires_grad: cap.requires_grad,
dense_bytes: Some(cap.tensor_bytes),
category: cap.category,
},
)
}
pub fn record_op(
session: &TraceSession,
span_id: SpanId,
cap: &CandleOpCapture,
) -> anyhow::Result<()> {
session.record_op(
span_id,
OpRecord {
op_name: &cap.op_name,
inputs: &cap.inputs,
output: Some(&cap.output.tensor_id),
shape: &cap.output.shape,
dtype: &cap.output.dtype,
device: &cap.output.device,
duration_ns: cap.duration_ns,
timestamp_ns: cap.timestamp_ns,
output_dense_bytes: Some(cap.output.tensor_bytes),
input_dense_bytes: cap.input_tensor_bytes,
},
)
}
#[cfg(test)]
mod tests {
use super::*;
use candle_core::Tensor;
#[test]
fn dense_tensor_footprint_matches_shape_dtype() {
let t = Tensor::zeros((4, 8), DType::F32, &Device::Cpu).unwrap();
assert_eq!(tensor_dense_bytes(&t), 4 * 8 * 4);
}
#[test]
fn capture_from_tensor() {
let t = Tensor::zeros((2, 3), DType::F32, &Device::Cpu).unwrap();
let cap = CandleCapture::from_tensor(&t, Some(ExecutionStep::Forward));
assert_eq!(cap.shape, vec![2, 3]);
assert_eq!(cap.category, MemoryCategory::Activation);
}
}