use crate::{Kind, Reduction, Result, RustTorchError, Tensor, no_grad};
#[derive(Debug)]
pub struct Normal {
location: Tensor,
scale: Tensor,
}
impl Normal {
pub fn new(location: &Tensor, scale: &Tensor) -> Result<Self> {
compatible(location, scale)?;
if !all(&scale.f_gt(0.)?)? {
return Err(invalid("scale", "must be positive"));
}
let mut parameters = Tensor::f_broadcast_tensors(&[location, scale])?;
let scale = parameters.pop().unwrap();
let location = parameters.pop().unwrap();
Ok(Self { location, scale })
}
pub fn batch_shape(&self) -> Vec<i64> {
self.location.size()
}
pub fn mean(&self) -> Tensor {
self.location.shallow_clone()
}
pub fn variance(&self) -> Result<Tensor> {
Ok(self.scale.f_square()?)
}
pub fn sample(&self, sample_shape: &[i64]) -> Result<Tensor> {
no_grad(|| self.rsample(sample_shape))
}
pub fn rsample(&self, sample_shape: &[i64]) -> Result<Tensor> {
let shape = extended(sample_shape, &self.batch_shape())?;
let noise = Tensor::f_randn(
shape.as_slice(),
(self.location.kind(), self.location.device()),
)?;
Ok(noise.f_mul(&self.scale)?.f_add(&self.location)?)
}
pub fn log_prob(&self, value: &Tensor) -> Result<Tensor> {
compatible(value, &self.location)?;
let standardized = value.f_sub(&self.location)?.f_div(&self.scale)?;
Ok(standardized
.f_square()?
.f_mul_scalar(-0.5)?
.f_sub(&self.scale.f_log()?)?
.f_sub_scalar(0.5 * (2. * std::f64::consts::PI).ln())?)
}
pub fn entropy(&self) -> Result<Tensor> {
Ok(self
.scale
.f_log()?
.f_add_scalar(0.5 * (2. * std::f64::consts::PI * std::f64::consts::E).ln())?)
}
}
#[derive(Debug)]
pub struct Bernoulli {
probabilities: Tensor,
logits: Tensor,
}
impl Bernoulli {
pub fn from_probs(probabilities: &Tensor) -> Result<Self> {
real(probabilities)?;
if !all(&probabilities
.f_ge(0.)?
.f_logical_and(&probabilities.f_le(1.)?)?)?
{
return Err(invalid("probabilities", "must be in [0, 1]"));
}
let epsilon = epsilon(probabilities.kind());
let p = probabilities.f_clamp(epsilon, 1. - epsilon)?;
let logits = p.f_log()?.f_sub(&p.f_neg()?.f_log1p()?)?;
Ok(Self {
probabilities: probabilities.shallow_clone(),
logits,
})
}
pub fn from_logits(logits: &Tensor) -> Result<Self> {
real(logits)?;
Ok(Self {
probabilities: logits.f_sigmoid()?,
logits: logits.shallow_clone(),
})
}
pub fn batch_shape(&self) -> Vec<i64> {
self.probabilities.size()
}
pub fn probabilities(&self) -> &Tensor {
&self.probabilities
}
pub fn sample(&self, sample_shape: &[i64]) -> Result<Tensor> {
let shape = extended(sample_shape, &self.batch_shape())?;
no_grad(|| {
Ok(self
.probabilities
.f_expand(shape.as_slice(), false)?
.f_bernoulli()?)
})
}
pub fn log_prob(&self, value: &Tensor) -> Result<Tensor> {
compatible(value, &self.logits)?;
if !all(&value.f_eq(0.)?.f_logical_or(&value.f_eq(1.)?)?)? {
return Err(invalid("value", "binary observations must be zero or one"));
}
let tensors = Tensor::f_broadcast_tensors(&[&self.logits, value])?;
Ok(tensors[0]
.f_binary_cross_entropy_with_logits(
&tensors[1],
None::<&Tensor>,
None::<&Tensor>,
Reduction::None,
)?
.f_neg()?)
}
pub fn entropy(&self) -> Result<Tensor> {
Ok(self.logits.f_binary_cross_entropy_with_logits(
&self.probabilities,
None::<&Tensor>,
None::<&Tensor>,
Reduction::None,
)?)
}
}
#[derive(Debug)]
pub struct Categorical {
probabilities: Tensor,
log_probabilities: Tensor,
}
impl Categorical {
pub fn from_logits(logits: &Tensor) -> Result<Self> {
classes(logits)?;
let log_probabilities = logits.f_log_softmax(-1, logits.kind())?;
Ok(Self {
probabilities: log_probabilities.f_exp()?,
log_probabilities,
})
}
pub fn from_probs(probabilities: &Tensor) -> Result<Self> {
classes(probabilities)?;
let sums = probabilities.f_sum_dim_intlist([-1].as_slice(), true, probabilities.kind())?;
if !all(&probabilities.f_ge(0.)?)?
|| !all(&sums.f_gt(0.)?.f_logical_and(&sums.f_isfinite()?)?)?
{
return Err(invalid(
"probabilities",
"each row needs nonnegative weights with a finite positive sum",
));
}
let probabilities = probabilities.f_div(&sums)?;
let epsilon = epsilon(probabilities.kind());
let log_probabilities = probabilities.f_clamp(epsilon, 1. - epsilon)?.f_log()?;
Ok(Self {
probabilities,
log_probabilities,
})
}
pub fn batch_shape(&self) -> Vec<i64> {
let mut shape = self.probabilities.size();
shape.pop();
shape
}
pub fn probabilities(&self) -> &Tensor {
&self.probabilities
}
pub fn sample(&self, sample_shape: &[i64]) -> Result<Tensor> {
let shape = extended(sample_shape, &self.batch_shape())?;
let count = sample_shape
.iter()
.try_fold(1_i64, |a, &b| a.checked_mul(b))
.ok_or_else(|| invalid("sample_shape", "element count overflow"))?;
if count == 0 {
return Ok(Tensor::f_zeros(
shape.as_slice(),
(Kind::Int64, self.probabilities.device()),
)?);
}
let classes = *self.probabilities.size().last().unwrap();
no_grad(|| {
Ok(self
.probabilities
.f_reshape([-1, classes])?
.f_multinomial(count, true)?
.f_transpose(0, 1)?
.f_reshape(shape.as_slice())?)
})
}
pub fn log_prob(&self, value: &Tensor) -> Result<Tensor> {
if !value.defined()
|| value.kind() != Kind::Int64
|| value.device() != self.probabilities.device()
{
return Err(invalid(
"value",
"expected defined Int64 indices on the parameter device",
));
}
let classes = *self.probabilities.size().last().unwrap();
if !all(&value.f_ge(0)?.f_logical_and(&value.f_lt(classes)?)?)? {
return Err(invalid("value", "class index out of range"));
}
let values = value.f_unsqueeze(-1)?;
let tensors = Tensor::f_broadcast_tensors(&[&values, &self.log_probabilities])?;
Ok(tensors[1]
.f_gather(-1, &tensors[0].f_narrow(-1, 0, 1)?, false)?
.f_squeeze_dim(-1)?)
}
pub fn entropy(&self) -> Result<Tensor> {
Ok(self
.probabilities
.f_mul(
&self
.log_probabilities
.f_clamp_min(match self.probabilities.kind() {
Kind::Double => f64::MIN,
Kind::Half => -65504.,
Kind::BFloat16 => -3.3895313892515355e38,
_ => f32::MIN as f64,
})?,
)?
.f_sum_dim_intlist([-1].as_slice(), false, self.probabilities.kind())?
.f_neg()?)
}
}
fn classes(input: &Tensor) -> Result<()> {
real(input)?;
if input.size().last().is_none_or(|&n| n == 0) {
return Err(invalid("classes", "expected a nonempty final class axis"));
}
Ok(())
}
fn compatible(left: &Tensor, right: &Tensor) -> Result<()> {
real(left)?;
real(right)?;
if left.kind() != right.kind() || left.device() != right.device() {
return Err(invalid("parameters", "dtype and device must match"));
}
Ok(())
}
fn real(input: &Tensor) -> Result<()> {
if !input.defined() {
return Err(invalid("tensor", "must be defined"));
}
if !matches!(
input.kind(),
Kind::Float | Kind::Double | Kind::Half | Kind::BFloat16
) || input.is_sparse()
{
return Err(invalid(
"tensor",
"expected dense real floating-point values",
));
}
let _ = input.f_as_strided([0], [1], None)?;
if !all(&input.f_isfinite()?)? {
return Err(invalid("tensor", "values must be finite"));
}
Ok(())
}
fn all(input: &Tensor) -> Result<bool> {
Ok(input.f_all()?.f_int64_value(&[])? != 0)
}
fn epsilon(kind: Kind) -> f64 {
match kind {
Kind::Double => f64::EPSILON,
Kind::Half => 0.0009765625,
Kind::BFloat16 => 0.0078125,
_ => f32::EPSILON as f64,
}
}
fn extended(sample: &[i64], batch: &[i64]) -> Result<Vec<i64>> {
if sample.iter().any(|&n| n < 0) {
return Err(invalid("sample_shape", "dimensions must be nonnegative"));
}
let mut shape = sample.to_vec();
shape.extend_from_slice(batch);
Ok(shape)
}
fn invalid(field: &'static str, reason: &str) -> RustTorchError {
RustTorchError::InvalidConfiguration {
field,
reason: reason.into(),
}
}