use std::sync::{Arc, RwLock};
use wgpu::util::DeviceExt;
use wgpu::{Buffer, BufferUsages};
use super::autograd::Node;
use crate::device::{GpuBuffer, GpuContext};
use crate::Device;
pub(crate) enum Storage {
Cpu(RwLock<Vec<f32>>),
Gpu(GpuBuffer),
}
pub(crate) type GradSlot = Arc<RwLock<Option<Tensor>>>;
#[derive(Clone)]
pub struct Tensor {
pub(crate) storage: Arc<Storage>,
pub shape: Vec<usize>,
pub(crate) device: Device,
pub(crate) grad: GradSlot,
pub requires_grad: bool,
pub(crate) node: Option<Arc<Node>>,
}
impl Tensor {
pub fn from_data(data: &[f32], shape: &[usize], device: &Device) -> Self {
let size: usize = shape.iter().product();
assert_eq!(
data.len(),
size,
"data length {} does not match shape {:?}",
data.len(),
shape
);
let storage = match device.gpu_ctx() {
Some(ctx) => Storage::Gpu(upload(ctx, data)),
None => Storage::Cpu(RwLock::new(data.to_vec())),
};
Self::from_storage(storage, shape.to_vec(), device)
}
pub fn from_vec(data: Vec<f32>, shape: &[usize], device: &Device) -> Self {
if device.is_cpu() {
let size: usize = shape.iter().product();
assert_eq!(
data.len(),
size,
"data length {} does not match shape {:?}",
data.len(),
shape
);
Self::from_storage(Storage::Cpu(RwLock::new(data)), shape.to_vec(), device)
} else {
Self::from_data(&data, shape, device)
}
}
pub fn scalar(value: f32, device: &Device) -> Self {
Self::from_data(&[value], &[], device)
}
pub(crate) fn from_storage(storage: Storage, shape: Vec<usize>, device: &Device) -> Self {
Self::from_shared_storage(Arc::new(storage), shape, device)
}
pub(crate) fn from_shared_storage(
storage: Arc<Storage>,
shape: Vec<usize>,
device: &Device,
) -> Self {
Self {
storage,
shape,
device: device.clone(),
grad: Arc::new(RwLock::new(None)),
requires_grad: false,
node: None,
}
}
pub fn zeros(shape: &[usize], device: &Device) -> Self {
Self::full(shape, 0.0, device)
}
pub fn ones(shape: &[usize], device: &Device) -> Self {
Self::full(shape, 1.0, device)
}
pub fn full(shape: &[usize], value: f32, device: &Device) -> Self {
let size: usize = shape.iter().product();
Self::from_vec(vec![value; size], shape, device)
}
pub fn zeros_like(&self) -> Self {
Self::zeros(&self.shape, &self.device)
}
pub fn ones_like(&self) -> Self {
Self::ones(&self.shape, &self.device)
}
pub fn full_like(&self, value: f32) -> Self {
Self::full(&self.shape, value, &self.device)
}
pub fn randn(shape: &[usize], device: &Device) -> Self {
crate::random::with_rng(|mut rng| Self::randn_with(shape, &mut rng, device))
}
pub fn randn_with(shape: &[usize], rng: &mut impl rand::Rng, device: &Device) -> Self {
let size: usize = shape.iter().product();
let data: Vec<f32> = (0..size)
.map(|_| {
let u1: f32 = 1.0 - rng.gen::<f32>();
let u2: f32 = rng.gen();
(-2.0 * u1.ln()).sqrt() * (2.0 * std::f32::consts::PI * u2).cos()
})
.collect();
Self::from_vec(data, shape, device)
}
pub fn rand(shape: &[usize], device: &Device) -> Self {
Self::uniform(shape, 0.0, 1.0, device)
}
pub fn uniform(shape: &[usize], low: f32, high: f32, device: &Device) -> Self {
crate::random::with_rng(|mut rng| Self::uniform_with(shape, low, high, &mut rng, device))
}
pub fn uniform_with(
shape: &[usize],
low: f32,
high: f32,
rng: &mut impl rand::Rng,
device: &Device,
) -> Self {
let size: usize = shape.iter().product();
let data: Vec<f32> = (0..size)
.map(|_| rng.gen::<f32>() * (high - low) + low)
.collect();
Self::from_vec(data, shape, device)
}
pub fn eye(n: usize, device: &Device) -> Self {
let mut data = vec![0.0f32; n * n];
for i in 0..n {
data[i * n + i] = 1.0;
}
Self::from_vec(data, &[n, n], device)
}
pub fn arange(start: f32, end: f32, step: f32, device: &Device) -> Self {
assert!(step > 0.0, "arange step must be positive");
let len = ((end - start) / step).ceil().max(0.0) as usize;
let data: Vec<f32> = (0..len).map(|i| start + i as f32 * step).collect();
Self::from_vec(data, &[len], device)
}
pub fn linspace(start: f32, end: f32, steps: usize, device: &Device) -> Self {
let data: Vec<f32> = match steps {
0 => vec![],
1 => vec![start],
_ => (0..steps)
.map(|i| start + (end - start) * i as f32 / (steps - 1) as f32)
.collect(),
};
Self::from_vec(data, &[steps], device)
}
pub fn numel(&self) -> usize {
self.shape.iter().product()
}
pub fn ndim(&self) -> usize {
self.shape.len()
}
pub fn size(&self) -> &[usize] {
&self.shape
}
pub fn device(&self) -> &Device {
&self.device
}
pub fn is_scalar(&self) -> bool {
self.numel() == 1
}
pub fn is_leaf(&self) -> bool {
self.node.is_none()
}
pub fn set_requires_grad(&mut self, requires_grad: bool) {
self.requires_grad = requires_grad;
}
pub fn requires_grad_(mut self) -> Self {
self.requires_grad = true;
self.node = None;
self
}
pub fn detach(&self) -> Self {
Self {
storage: Arc::clone(&self.storage),
shape: self.shape.clone(),
device: self.device.clone(),
grad: Arc::new(RwLock::new(None)),
requires_grad: false,
node: None,
}
}
pub(crate) fn view_raw(&self, shape: &[usize]) -> Self {
let mut t = self.detach();
t.shape = shape.to_vec();
t
}
pub(crate) fn id(&self) -> usize {
Arc::as_ptr(&self.grad) as *const () as usize
}
pub fn to_vec(&self) -> Vec<f32> {
match &*self.storage {
Storage::Cpu(data) => data.read().expect("tensor lock poisoned").clone(),
Storage::Gpu(buffer) => {
let ctx = self.device.gpu_ctx().expect("GPU storage on a CPU device");
pollster::block_on(download(ctx, buffer, self.numel()))
}
}
}
pub async fn to_vec_async(&self) -> Vec<f32> {
match &*self.storage {
Storage::Cpu(data) => data.read().expect("tensor lock poisoned").clone(),
Storage::Gpu(buffer) => {
let ctx = self.device.gpu_ctx().expect("GPU storage on a CPU device");
download(ctx, buffer, self.numel()).await
}
}
}
pub fn item(&self) -> f32 {
assert!(
self.is_scalar(),
"item() needs a one-element tensor, got shape {:?}",
self.shape
);
self.to_vec()[0]
}
pub fn copy_from_slice(&self, data: &[f32]) {
assert_eq!(
data.len(),
self.numel(),
"data length must match tensor size"
);
match &*self.storage {
Storage::Cpu(values) => values
.write()
.expect("tensor lock poisoned")
.copy_from_slice(data),
Storage::Gpu(buffer) => {
if let Some(ctx) = self.device.gpu_ctx() {
ctx.write_buffer(buffer, bytemuck::cast_slice(data));
}
}
}
}
pub fn assign(&self, src: &Tensor) {
assert_eq!(self.shape, src.shape, "assign needs matching shapes");
let src = src.to_device(&self.device);
match (&*self.storage, &*src.storage) {
(Storage::Gpu(dst), Storage::Gpu(from)) => {
let ctx = self.device.gpu_ctx().expect("GPU storage on a CPU device");
ctx.copy(from, dst, (self.numel() * 4) as u64);
}
_ => self.copy_from_slice(&src.to_vec()),
}
}
pub fn to_device(&self, device: &Device) -> Tensor {
if self.device.same_as(device) {
return self.clone();
}
let out = Tensor::from_data(&self.to_vec(), &self.shape, device);
let source = self.device.clone();
out.with_node("to_device", vec![self.clone()], move |g| {
vec![Some(g.to_device(&source))]
})
}
pub fn grad(&self) -> Option<Tensor> {
self.grad.read().expect("grad lock poisoned").clone()
}
pub fn set_grad(&self, grad: Option<Tensor>) {
if let Some(g) = &grad {
assert_eq!(g.shape, self.shape, "gradient shape must match tensor");
}
*self.grad.write().expect("grad lock poisoned") = grad;
}
pub fn zero_grad(&self) {
self.set_grad(None);
}
}
pub(crate) fn upload(ctx: &GpuContext, data: &[f32]) -> GpuBuffer {
let contents: &[u8] = if data.is_empty() {
&[0u8; 4]
} else {
bytemuck::cast_slice(data)
};
ctx.adopt(
ctx.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: Some("Tensor"),
contents,
usage: TENSOR_USAGE,
}),
)
}
pub(crate) fn alloc(ctx: &GpuContext, n: usize) -> GpuBuffer {
let size = (n.max(1) * 4) as u64;
ctx.buffer(size, || {
ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Tensor"),
size,
usage: TENSOR_USAGE,
mapped_at_creation: false,
})
})
}
const TENSOR_USAGE: BufferUsages = BufferUsages::STORAGE
.union(BufferUsages::COPY_SRC)
.union(BufferUsages::COPY_DST);
async fn download(ctx: &GpuContext, buffer: &Buffer, n: usize) -> Vec<f32> {
if n == 0 {
return Vec::new();
}
let size = (n * 4) as u64;
let staging = ctx.device.create_buffer(&wgpu::BufferDescriptor {
label: Some("Staging"),
size,
usage: BufferUsages::MAP_READ | BufferUsages::COPY_DST,
mapped_at_creation: false,
});
ctx.copy(buffer, &staging, size);
ctx.flush();
let slice = staging.slice(..);
let (tx, rx) = std::sync::mpsc::channel();
slice.map_async(wgpu::MapMode::Read, move |result| {
let _ = tx.send(result);
});
ctx.wait_for(&rx).expect("failed to map GPU buffer");
let data = slice.get_mapped_range();
let result: Vec<f32> = bytemuck::cast_slice(&data).to_vec();
drop(data);
staging.unmap();
result
}
impl std::fmt::Debug for Tensor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Tensor")
.field("shape", &self.shape)
.field("requires_grad", &self.requires_grad)
.field("device", &self.device.name())
.finish()
}
}
impl std::fmt::Display for Tensor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let data = self.to_vec();
let fmt_row = |row: &[f32]| {
row.iter()
.map(|x| format!("{x:.4}"))
.collect::<Vec<_>>()
.join(", ")
};
match self.ndim() {
0 => write!(f, "tensor({:.4})", data[0]),
1 => write!(f, "tensor([{}])", fmt_row(&data)),
2 => {
writeln!(f, "tensor([")?;
for row in data.chunks(self.shape[1].max(1)) {
writeln!(f, " [{}],", fmt_row(row))?;
}
write!(f, "])")
}
_ => write!(f, "tensor(shape={:?})", self.shape),
}
}
}