use super::shape::{broadcast_shapes, contiguous_strides, normalize_dim};
use super::Tensor;
use crate::backend::{binary_op, reduce_op, unary_op};
pub(crate) fn sum_to_shape(grad: &Tensor, shape: &[usize]) -> Tensor {
if grad.shape == shape {
return grad.clone();
}
let mut g = grad.clone();
while g.ndim() > shape.len() {
g = g.reduce_raw(0, reduce_op::SUM);
}
for (d, &size) in shape.iter().enumerate() {
if size == 1 && g.shape[d] != 1 {
let mut keep = g.shape.clone();
keep[d] = 1;
g = g.reduce_raw(d, reduce_op::SUM).view_raw(&keep);
}
}
g.view_raw(shape)
}
impl Tensor {
pub fn add(&self, other: &Tensor) -> Tensor {
let out = self.binary_raw(other, binary_op::ADD);
let (sa, sb) = (self.shape.clone(), other.shape.clone());
out.with_node("add", vec![self.clone(), other.clone()], move |g| {
vec![Some(sum_to_shape(g, &sa)), Some(sum_to_shape(g, &sb))]
})
}
pub fn sub(&self, other: &Tensor) -> Tensor {
let out = self.binary_raw(other, binary_op::SUB);
let (sa, sb) = (self.shape.clone(), other.shape.clone());
out.with_node("sub", vec![self.clone(), other.clone()], move |g| {
vec![
Some(sum_to_shape(g, &sa)),
Some(sum_to_shape(&g.neg(), &sb)),
]
})
}
pub fn mul(&self, other: &Tensor) -> Tensor {
let out = self.binary_raw(other, binary_op::MUL);
let (a, b) = (self.detach(), other.detach());
let need = (self.requires_grad, other.requires_grad);
out.with_node("mul", vec![self.clone(), other.clone()], move |g| {
vec![
need.0.then(|| sum_to_shape(&g.mul(&b), &a.shape)),
need.1.then(|| sum_to_shape(&g.mul(&a), &b.shape)),
]
})
}
pub fn div(&self, other: &Tensor) -> Tensor {
let out = self.binary_raw(other, binary_op::DIV);
let (a, b) = (self.detach(), other.detach());
let need = (self.requires_grad, other.requires_grad);
out.with_node("div", vec![self.clone(), other.clone()], move |g| {
vec![
need.0.then(|| sum_to_shape(&g.div(&b), &a.shape)),
need.1.then(|| {
let gb = g.mul(&a).div(&b.square()).neg();
sum_to_shape(&gb, &b.shape)
}),
]
})
}
pub fn maximum(&self, other: &Tensor) -> Tensor {
let out = self.binary_raw(other, binary_op::MAX);
let (a, b) = (self.detach(), other.detach());
out.with_node("maximum", vec![self.clone(), other.clone()], move |g| {
let mask = a.binary_raw(&b, binary_op::GE);
vec![
Some(sum_to_shape(&g.mul(&mask), &a.shape)),
Some(sum_to_shape(&g.mul(&mask.rsub_scalar(1.0)), &b.shape)),
]
})
}
pub fn minimum(&self, other: &Tensor) -> Tensor {
let out = self.binary_raw(other, binary_op::MIN);
let (a, b) = (self.detach(), other.detach());
out.with_node("minimum", vec![self.clone(), other.clone()], move |g| {
let mask = a.binary_raw(&b, binary_op::LE);
vec![
Some(sum_to_shape(&g.mul(&mask), &a.shape)),
Some(sum_to_shape(&g.mul(&mask.rsub_scalar(1.0)), &b.shape)),
]
})
}
pub fn gt(&self, other: &Tensor) -> Tensor {
self.binary_raw(other, binary_op::GT)
}
pub fn lt(&self, other: &Tensor) -> Tensor {
self.binary_raw(other, binary_op::LT)
}
pub fn ge(&self, other: &Tensor) -> Tensor {
self.binary_raw(other, binary_op::GE)
}
pub fn le(&self, other: &Tensor) -> Tensor {
self.binary_raw(other, binary_op::LE)
}
pub fn eq_elem(&self, other: &Tensor) -> Tensor {
self.binary_raw(other, binary_op::EQ)
}
pub fn less_than(&self, other: &Tensor) -> Tensor {
self.lt(other)
}
pub fn where_cond(cond: &Tensor, a: &Tensor, b: &Tensor) -> Tensor {
let zero = Tensor::zeros(&[], &cond.device);
let keep = cond
.unary_raw(unary_op::ABS, 0.0, 0.0)
.binary_raw(&zero, binary_op::GT);
a.mul(&keep).add(&b.mul(&keep.rsub_scalar(1.0)))
}
pub fn masked_fill(&self, mask: &Tensor, value: f32) -> Tensor {
assert!(value.is_finite(), "masked_fill needs a finite value");
let fill = Tensor::full(&[], value, &self.device);
Tensor::where_cond(mask, &fill, self)
}
fn unary_with_grad(
&self,
name: &'static str,
op: u32,
a: f32,
b: f32,
backward: impl Fn(&Tensor, &Tensor, &Tensor) -> Tensor + Send + Sync + 'static,
) -> Tensor {
let out = self.unary_raw(op, a, b);
let x = self.detach();
let y = out.detach();
out.with_node(name, vec![self.clone()], move |g| {
vec![Some(backward(g, &x, &y))]
})
}
pub fn add_scalar(&self, scalar: f32) -> Tensor {
self.unary_with_grad(
"add_scalar",
unary_op::ADD_SCALAR,
scalar,
0.0,
|g, _, _| g.clone(),
)
}
pub fn sub_scalar(&self, scalar: f32) -> Tensor {
self.add_scalar(-scalar)
}
pub fn rsub_scalar(&self, scalar: f32) -> Tensor {
self.unary_with_grad(
"rsub_scalar",
unary_op::RSUB_SCALAR,
scalar,
0.0,
|g, _, _| g.neg(),
)
}
pub fn mul_scalar(&self, scalar: f32) -> Tensor {
self.unary_with_grad(
"mul_scalar",
unary_op::MUL_SCALAR,
scalar,
0.0,
move |g, _, _| g.mul_scalar(scalar),
)
}
pub fn div_scalar(&self, scalar: f32) -> Tensor {
self.mul_scalar(1.0 / scalar)
}
pub fn pow_scalar(&self, exponent: f32) -> Tensor {
self.unary_with_grad(
"pow_scalar",
unary_op::POW_SCALAR,
exponent,
0.0,
move |g, x, _| g.mul(&x.pow_scalar(exponent - 1.0).mul_scalar(exponent)),
)
}
pub fn neg(&self) -> Tensor {
self.unary_with_grad("neg", unary_op::NEG, 0.0, 0.0, |g, _, _| g.neg())
}
pub fn relu(&self) -> Tensor {
self.unary_with_grad("relu", unary_op::RELU, 0.0, 0.0, |g, x, _| {
g.mul(&x.unary_raw(unary_op::STEP, 0.0, 0.0))
})
}
pub fn leaky_relu(&self, slope: f32) -> Tensor {
self.unary_with_grad(
"leaky_relu",
unary_op::LEAKY_RELU,
slope,
0.0,
move |g, x, _| g.mul(&x.unary_raw(unary_op::LEAKY_RELU_GRAD, slope, 0.0)),
)
}
pub fn silu(&self) -> Tensor {
self.unary_with_grad("silu", unary_op::SILU, 0.0, 0.0, |g, x, _| {
g.mul(&x.unary_raw(unary_op::SILU_GRAD, 0.0, 0.0))
})
}
pub fn gelu(&self) -> Tensor {
self.unary_with_grad("gelu", unary_op::GELU, 0.0, 0.0, |g, x, _| {
g.mul(&x.unary_raw(unary_op::GELU_GRAD, 0.0, 0.0))
})
}
pub fn sigmoid(&self) -> Tensor {
self.unary_with_grad("sigmoid", unary_op::SIGMOID, 0.0, 0.0, |g, _, y| {
g.mul(&y.mul(&y.rsub_scalar(1.0)))
})
}
pub fn tanh(&self) -> Tensor {
self.unary_with_grad("tanh", unary_op::TANH, 0.0, 0.0, |g, _, y| {
g.mul(&y.square().rsub_scalar(1.0))
})
}
pub fn exp(&self) -> Tensor {
self.unary_with_grad("exp", unary_op::EXP, 0.0, 0.0, |g, _, y| g.mul(y))
}
pub fn log(&self) -> Tensor {
self.unary_with_grad("log", unary_op::LOG, 0.0, 0.0, |g, x, _| g.div(x))
}
pub fn abs(&self) -> Tensor {
self.unary_with_grad("abs", unary_op::ABS, 0.0, 0.0, |g, x, _| {
g.mul(&x.unary_raw(unary_op::SIGN, 0.0, 0.0))
})
}
pub fn sqrt(&self) -> Tensor {
self.unary_with_grad("sqrt", unary_op::SQRT, 0.0, 0.0, |g, _, y| {
g.div(y).mul_scalar(0.5)
})
}
pub fn rsqrt(&self) -> Tensor {
self.sqrt().recip()
}
pub fn recip(&self) -> Tensor {
self.unary_with_grad("recip", unary_op::RECIP, 0.0, 0.0, |g, _, y| {
g.mul(&y.square()).neg()
})
}
pub fn square(&self) -> Tensor {
self.unary_with_grad("square", unary_op::SQUARE, 0.0, 0.0, |g, x, _| {
g.mul(x).mul_scalar(2.0)
})
}
pub fn sin(&self) -> Tensor {
self.unary_with_grad("sin", unary_op::SIN, 0.0, 0.0, |g, x, _| g.mul(&x.cos()))
}
pub fn cos(&self) -> Tensor {
self.unary_with_grad("cos", unary_op::COS, 0.0, 0.0, |g, x, _| {
g.mul(&x.sin()).neg()
})
}
pub fn clamp(&self, min: f32, max: f32) -> Tensor {
self.unary_with_grad("clamp", unary_op::CLAMP, min, max, move |g, x, _| {
g.mul(&x.unary_raw(unary_op::IN_RANGE, min, max))
})
}
pub fn sign(&self) -> Tensor {
self.unary_raw(unary_op::SIGN, 0.0, 0.0)
}
pub fn sum(&self) -> Tensor {
let out = self.view_raw(&[self.numel()]).reduce_raw(0, reduce_op::SUM);
let shape = self.shape.clone();
out.with_node("sum", vec![self.clone()], move |g| {
vec![Some(g.expand_raw(&shape))]
})
}
pub fn mean(&self) -> Tensor {
self.sum().div_scalar(self.numel() as f32)
}
pub fn sum_dim(&self, dim: isize, keepdim: bool) -> Tensor {
let d = normalize_dim(dim, self.ndim());
let reduced = self.reduce_raw(d, reduce_op::SUM);
let mut keep_shape = self.shape.clone();
keep_shape[d] = 1;
let out = if keepdim {
reduced.view_raw(&keep_shape)
} else {
reduced
};
let shape = self.shape.clone();
out.with_node("sum_dim", vec![self.clone()], move |g| {
vec![Some(g.view_raw(&keep_shape).expand_raw(&shape))]
})
}
pub fn mean_dim(&self, dim: isize, keepdim: bool) -> Tensor {
let d = normalize_dim(dim, self.ndim());
self.sum_dim(dim, keepdim).div_scalar(self.shape[d] as f32)
}
pub fn var_dim(&self, dim: isize, keepdim: bool) -> Tensor {
let centered = self.sub(&self.mean_dim(dim, true));
centered.square().mean_dim(dim, keepdim)
}
fn extreme(&self, op: u32, name: &'static str) -> Tensor {
assert!(self.numel() > 0, "{name} of an empty tensor");
let out = self.view_raw(&[self.numel()]).reduce_raw(0, op);
let (x, y) = (self.detach(), out.detach());
out.with_node(name, vec![self.clone()], move |g| {
let mask = x.binary_raw(&y, binary_op::EQ);
let count = mask.view_raw(&[mask.numel()]).reduce_raw(0, reduce_op::SUM);
vec![Some(mask.mul(&g.div(&count)))]
})
}
fn extreme_dim(&self, dim: isize, keepdim: bool, op: u32, name: &'static str) -> Tensor {
let d = normalize_dim(dim, self.ndim());
let mut keep_shape = self.shape.clone();
keep_shape[d] = 1;
let reduced = self.reduce_raw(d, op).view_raw(&keep_shape);
let (x, y) = (self.detach(), reduced.detach());
let out = if keepdim {
reduced
} else {
reduced.view_raw(&[&self.shape[..d], &self.shape[d + 1..]].concat())
};
out.with_node(name, vec![self.clone()], move |g| {
let mask = x.binary_raw(&y, binary_op::EQ);
let count = mask.reduce_raw(d, reduce_op::SUM).view_raw(&keep_shape);
vec![Some(mask.mul(&g.view_raw(&keep_shape).div(&count)))]
})
}
pub fn max(&self) -> Tensor {
self.extreme(reduce_op::MAX, "max")
}
pub fn min(&self) -> Tensor {
self.extreme(reduce_op::MIN, "min")
}
pub fn max_dim(&self, dim: isize, keepdim: bool) -> Tensor {
self.extreme_dim(dim, keepdim, reduce_op::MAX, "max_dim")
}
pub fn min_dim(&self, dim: isize, keepdim: bool) -> Tensor {
self.extreme_dim(dim, keepdim, reduce_op::MIN, "min_dim")
}
pub fn argmax(&self) -> usize {
arg_extreme(&self.to_vec(), |a, b| a > b || (a.is_nan() && !b.is_nan()))
}
pub fn argmin(&self) -> usize {
arg_extreme(&self.to_vec(), |a, b| a < b || (b.is_nan() && !a.is_nan()))
}
pub fn argmax_dim(&self, dim: isize) -> Tensor {
let d = normalize_dim(dim, self.ndim());
let (outer, size, inner) = super::shape::split_at_dim(&self.shape, d);
let data = self.to_vec();
let mut out = Vec::with_capacity(outer * inner);
for o in 0..outer {
for k in 0..inner {
let column: Vec<f32> = (0..size)
.map(|j| data[(o * size + j) * inner + k])
.collect();
out.push(arg_extreme(&column, |a, b| a > b || (a.is_nan() && !b.is_nan())) as f32);
}
}
let mut shape = self.shape.clone();
shape.remove(d);
self.host_output(out, &shape)
}
pub fn reshape(&self, new_shape: &[usize]) -> Tensor {
let new_size: usize = new_shape.iter().product();
assert_eq!(
new_size,
self.numel(),
"cannot reshape tensor of size {} to shape {:?}",
self.numel(),
new_shape
);
let out = self.view_raw(new_shape);
let shape = self.shape.clone();
out.with_node("reshape", vec![self.clone()], move |g| {
vec![Some(g.view_raw(&shape))]
})
}
pub fn flatten(&self) -> Tensor {
self.reshape(&[self.numel()])
}
pub fn squeeze(&self) -> Tensor {
let new_shape: Vec<usize> = self.shape.iter().copied().filter(|&d| d != 1).collect();
self.reshape(&new_shape)
}
pub fn squeeze_dim(&self, dim: isize) -> Tensor {
let d = normalize_dim(dim, self.ndim());
assert_eq!(
self.shape[d], 1,
"squeeze_dim on a dimension of size {}",
self.shape[d]
);
let mut new_shape = self.shape.clone();
new_shape.remove(d);
self.reshape(&new_shape)
}
pub fn unsqueeze(&self, dim: usize) -> Tensor {
assert!(
dim <= self.ndim(),
"dimension {} out of range for a {}-D tensor",
dim,
self.ndim()
);
let mut new_shape = self.shape.clone();
new_shape.insert(dim, 1);
self.reshape(&new_shape)
}
pub fn permute(&self, perm: &[usize]) -> Tensor {
let mut seen = vec![false; self.ndim()];
assert_eq!(
perm.len(),
self.ndim(),
"permute needs one entry per dimension"
);
for &p in perm {
assert!(p < self.ndim() && !seen[p], "invalid permutation {perm:?}");
seen[p] = true;
}
let out = self.permute_raw(perm);
let mut inverse = vec![0; perm.len()];
for (i, &p) in perm.iter().enumerate() {
inverse[p] = i;
}
out.with_node("permute", vec![self.clone()], move |g| {
vec![Some(g.permute_raw(&inverse))]
})
}
pub fn transpose_dims(&self, dim0: isize, dim1: isize) -> Tensor {
let (a, b) = (
normalize_dim(dim0, self.ndim()),
normalize_dim(dim1, self.ndim()),
);
let mut perm: Vec<usize> = (0..self.ndim()).collect();
perm.swap(a, b);
self.permute(&perm)
}
pub fn transpose(&self) -> Tensor {
assert!(self.ndim() >= 2, "transpose needs at least 2 dims");
self.transpose_dims(-2, -1)
}
pub fn expand(&self, shape: &[usize]) -> Tensor {
assert_eq!(
broadcast_shapes(&self.shape, shape).as_deref(),
Some(shape),
"cannot expand {:?} to {:?}",
self.shape,
shape
);
let out = self.expand_raw(shape);
let orig = self.shape.clone();
out.with_node("expand", vec![self.clone()], move |g| {
vec![Some(sum_to_shape(g, &orig))]
})
}
pub fn narrow(&self, dim: isize, start: usize, len: usize) -> Tensor {
let d = normalize_dim(dim, self.ndim());
assert!(
start + len <= self.shape[d],
"narrow {}..{} out of range for dimension of size {}",
start,
start + len,
self.shape[d]
);
let strides = contiguous_strides(&self.shape);
let mut out_shape = self.shape.clone();
out_shape[d] = len;
let out = self.strided_copy_raw(&out_shape, &strides, start * strides[d]);
let shape = self.shape.clone();
out.with_node("narrow", vec![self.clone()], move |g| {
let mut parts = Vec::new();
let mut before = shape.clone();
before[d] = start;
let mut after = shape.clone();
after[d] = shape[d] - start - len;
if start > 0 {
parts.push(Tensor::zeros(&before, &g.device));
}
parts.push(g.clone());
if after[d] > 0 {
parts.push(Tensor::zeros(&after, &g.device));
}
vec![Some(Tensor::cat(&parts, d as isize))]
})
}
pub fn chunk(&self, chunks: usize, dim: isize) -> Vec<Tensor> {
assert!(chunks > 0, "chunk count must be positive");
let d = normalize_dim(dim, self.ndim());
let size = self.shape[d].div_ceil(chunks).max(1);
self.split(size, dim)
}
pub fn split(&self, size: usize, dim: isize) -> Vec<Tensor> {
assert!(size > 0, "split size must be positive");
let d = normalize_dim(dim, self.ndim());
(0..self.shape[d])
.step_by(size)
.map(|start| self.narrow(dim, start, size.min(self.shape[d] - start)))
.collect()
}
pub fn cat(tensors: &[Tensor], dim: isize) -> Tensor {
assert!(!tensors.is_empty(), "cat needs at least one tensor");
let first = &tensors[0];
let d = normalize_dim(dim, first.ndim());
for t in tensors {
first.assert_same_device(t);
assert_eq!(t.ndim(), first.ndim(), "cat needs tensors of equal rank");
for (i, (&a, &b)) in t.shape.iter().zip(&first.shape).enumerate() {
assert!(
i == d || a == b,
"cat shapes differ: {:?} vs {:?}",
t.shape,
first.shape
);
}
}
let mut out_shape = first.shape.clone();
out_shape[d] = tensors.iter().map(|t| t.shape[d]).sum();
let outer: usize = first.shape[..d].iter().product();
let inner: usize = first.shape[d + 1..].iter().product();
let datas: Vec<Vec<f32>> = tensors.iter().map(Tensor::to_vec).collect();
let mut data = Vec::with_capacity(out_shape.iter().product());
for o in 0..outer {
for (t, values) in tensors.iter().zip(&datas) {
let block = t.shape[d] * inner;
data.extend_from_slice(&values[o * block..(o + 1) * block]);
}
}
let out = first.host_output(data, &out_shape);
let sizes: Vec<usize> = tensors.iter().map(|t| t.shape[d]).collect();
out.with_node("cat", tensors.to_vec(), move |g| {
let mut start = 0;
sizes
.iter()
.map(|&len| {
let piece = g.narrow(d as isize, start, len);
start += len;
Some(piece)
})
.collect()
})
}
pub fn stack(tensors: &[Tensor], dim: usize) -> Tensor {
let expanded: Vec<Tensor> = tensors.iter().map(|t| t.unsqueeze(dim)).collect();
Tensor::cat(&expanded, dim as isize)
}
pub fn index_select(&self, dim: isize, indices: &[usize]) -> Tensor {
let d = normalize_dim(dim, self.ndim());
let out = if d == 0 {
self.gather_rows_raw(indices)
} else {
let mut perm: Vec<usize> = (0..self.ndim()).collect();
perm.swap(0, d);
self.permute_raw(&perm)
.gather_rows_raw(indices)
.permute_raw(&perm)
};
let shape = self.shape.clone();
let indices = indices.to_vec();
out.with_node("index_select", vec![self.clone()], move |g| {
vec![Some(index_add(g, d, &indices, &shape))]
})
}
pub fn matmul(&self, other: &Tensor) -> Tensor {
let out = self.matmul_raw(other);
let (a, b) = (self.detach(), other.detach());
let need = (self.requires_grad, other.requires_grad);
out.with_node("matmul", vec![self.clone(), other.clone()], move |g| {
vec![
need.0
.then(|| sum_to_shape(&g.matmul_raw(&b.transpose()), &a.shape)),
need.1
.then(|| sum_to_shape(&a.transpose().matmul_raw(g), &b.shape)),
]
})
}
pub fn softmax(&self, dim: isize) -> Tensor {
let d = normalize_dim(dim, self.ndim());
let out = self.softmax_raw(d, false);
let y = out.detach();
out.with_node("softmax", vec![self.clone()], move |g| {
let dot = g.mul(&y).sum_dim(d as isize, true);
vec![Some(y.mul(&g.sub(&dot)))]
})
}
pub fn log_softmax(&self, dim: isize) -> Tensor {
let d = normalize_dim(dim, self.ndim());
let out = self.softmax_raw(d, true);
let y = out.detach();
out.with_node("log_softmax", vec![self.clone()], move |g| {
let total = g.sum_dim(d as isize, true);
vec![Some(g.sub(&y.exp().mul(&total)))]
})
}
}
fn index_add(grad: &Tensor, dim: usize, indices: &[usize], shape: &[usize]) -> Tensor {
let (outer, size, inner) = super::shape::split_at_dim(shape, dim);
let g = grad.to_vec();
let mut out = vec![0.0f32; shape.iter().product()];
let n_idx = indices.len();
for o in 0..outer {
for (j, &idx) in indices.iter().enumerate() {
let src = (o * n_idx + j) * inner;
let dst = (o * size + idx) * inner;
for k in 0..inner {
out[dst + k] += g[src + k];
}
}
}
grad.host_output(out, shape)
}
fn arg_extreme(data: &[f32], better: impl Fn(f32, f32) -> bool) -> usize {
let mut best = 0;
for (i, &v) in data.iter().enumerate().skip(1) {
if better(v, data[best]) {
best = i;
}
}
best
}
macro_rules! binary_operator {
($trait:ident, $method:ident, $scalar_fn:expr) => {
impl std::ops::$trait<&Tensor> for &Tensor {
type Output = Tensor;
fn $method(self, other: &Tensor) -> Tensor {
Tensor::$method(self, other)
}
}
impl std::ops::$trait<Tensor> for Tensor {
type Output = Tensor;
fn $method(self, other: Tensor) -> Tensor {
Tensor::$method(&self, &other)
}
}
impl std::ops::$trait<f32> for &Tensor {
type Output = Tensor;
fn $method(self, scalar: f32) -> Tensor {
let f: fn(&Tensor, f32) -> Tensor = $scalar_fn;
f(self, scalar)
}
}
};
}
binary_operator!(Add, add, Tensor::add_scalar);
binary_operator!(Sub, sub, Tensor::sub_scalar);
binary_operator!(Mul, mul, Tensor::mul_scalar);
binary_operator!(Div, div, Tensor::div_scalar);
impl std::ops::Neg for &Tensor {
type Output = Tensor;
fn neg(self) -> Tensor {
Tensor::neg(self)
}
}
impl std::ops::Neg for Tensor {
type Output = Tensor;
fn neg(self) -> Tensor {
Tensor::neg(&self)
}
}