pub use super::loss::{
CrossEntropyOptions, binary_cross_entropy, binary_cross_entropy_with_logits,
cross_entropy_with_options, huber_loss, l1_loss, mse_loss_with_reduction, nll_loss,
smooth_l1_loss,
};
use tch::{Reduction, Tensor};
use crate::{Result, RustTorchError, device::ensure_device};
use super::GeluApproximation;
pub fn linear(input: &Tensor, weight: &Tensor, bias: Option<&Tensor>) -> Result<Tensor> {
ensure_device("linear weight", weight, input_device(input)?)?;
if weight.size().len() != 2 {
return Err(RustTorchError::InvalidDimensions {
context: "linear weight".to_owned(),
expected: "rank 2 [out_features, in_features]".to_owned(),
actual: format!("shape {:?}", weight.size()),
});
}
let input_shape = input.size();
let Some(&input_features) = input_shape.last() else {
return Err(RustTorchError::InvalidDimensions {
context: "linear input".to_owned(),
expected: "rank at least 1".to_owned(),
actual: "scalar".to_owned(),
});
};
if input_features != weight.size()[1] {
return Err(RustTorchError::InvalidDimensions {
context: "linear input".to_owned(),
expected: format!("last dimension {}", weight.size()[1]),
actual: input_features.to_string(),
});
}
if let Some(bias) = bias {
ensure_device("linear bias", bias, input_device(input)?)?;
if bias.size() != [weight.size()[0]] {
return Err(RustTorchError::ShapeMismatch {
name: "linear bias".to_owned(),
expected: vec![weight.size()[0]],
actual: bias.size(),
});
}
}
linear_backend(input, weight, bias)
}
pub(crate) fn linear_backend(
input: &Tensor,
weight: &Tensor,
bias: Option<&Tensor>,
) -> Result<Tensor> {
if let Some(bias) = bias
&& input.defined()
&& weight.defined()
&& bias.defined()
&& input.device() == tch::Device::Mps
&& weight.device() == input.device()
&& bias.device() == input.device()
&& matches!(
input.kind(),
tch::Kind::Float | tch::Kind::Half | tch::Kind::BFloat16
)
&& weight.kind() == input.kind()
&& bias.kind() == input.kind()
&& input.dim() > 0
&& weight.dim() == 2
&& bias.size() == [weight.size()[0]]
{
return Ok(input.f_matmul(&weight.f_transpose(0, 1)?)?.f_add(bias)?);
}
input.f_linear(weight, bias).map_err(Into::into)
}
pub fn relu(input: &Tensor) -> Result<Tensor> {
input.f_relu().map_err(Into::into)
}
pub fn gelu(input: &Tensor) -> Result<Tensor> {
gelu_with_approximation(input, GeluApproximation::None)
}
pub fn gelu_with_approximation(input: &Tensor, approximation: GeluApproximation) -> Result<Tensor> {
input.f_gelu(approximation.as_str()).map_err(Into::into)
}
pub fn dropout(input: &Tensor, probability: f64, training: bool) -> Result<Tensor> {
validate_dropout(probability)?;
input.f_dropout(probability, training).map_err(Into::into)
}
pub(crate) fn validate_dropout(probability: f64) -> Result<()> {
if probability.is_finite() && (0.0..=1.0).contains(&probability) {
Ok(())
} else {
Err(RustTorchError::InvalidConfiguration {
field: "dropout probability",
reason: format!("must be finite and in [0, 1], got {probability}"),
})
}
}
pub fn flatten(input: &Tensor, start_dim: i64, end_dim: i64) -> Result<Tensor> {
input.f_flatten(start_dim, end_dim).map_err(Into::into)
}
pub fn mse_loss(input: &Tensor, target: &Tensor) -> Result<Tensor> {
mse_loss_with_reduction(input, target, Reduction::Mean)
}
pub fn cross_entropy(input: &Tensor, target: &Tensor) -> Result<Tensor> {
cross_entropy_with_options(input, target, CrossEntropyOptions::default())
}
pub fn conv1d(
input: &Tensor,
weight: &Tensor,
bias: Option<&Tensor>,
stride: [i64; 1],
padding: [i64; 1],
dilation: [i64; 1],
groups: i64,
) -> Result<Tensor> {
convolution(input, weight, bias, &stride, &padding, &dilation, groups)
}
pub fn conv2d(
input: &Tensor,
weight: &Tensor,
bias: Option<&Tensor>,
stride: [i64; 2],
padding: [i64; 2],
dilation: [i64; 2],
groups: i64,
) -> Result<Tensor> {
convolution(input, weight, bias, &stride, &padding, &dilation, groups)
}
pub fn conv3d(
input: &Tensor,
weight: &Tensor,
bias: Option<&Tensor>,
stride: [i64; 3],
padding: [i64; 3],
dilation: [i64; 3],
groups: i64,
) -> Result<Tensor> {
convolution(input, weight, bias, &stride, &padding, &dilation, groups)
}
pub(crate) fn validate_conv_config(
in_channels: i64,
out_channels: i64,
kernel: &[i64],
stride: &[i64],
padding: &[i64],
dilation: &[i64],
groups: i64,
) -> Result<()> {
if !(1..=3).contains(&kernel.len()) {
return Err(invalid(
"convolution dimensions",
"must be one, two, or three",
));
}
if in_channels <= 0 || out_channels <= 0 {
return Err(invalid(
"channels",
"input and output channels must be positive",
));
}
if groups <= 0 || in_channels % groups != 0 || out_channels % groups != 0 {
return Err(invalid(
"groups",
"must be positive and divide both channel counts",
));
}
for (field, values) in [
("kernel_size", kernel),
("stride", stride),
("dilation", dilation),
] {
if values.len() != kernel.len() || values.iter().any(|&value| value <= 0) {
return Err(invalid(
field,
"must have one positive value per spatial dimension",
));
}
}
if padding.len() != kernel.len() || padding.iter().any(|&value| value < 0) {
return Err(invalid(
"padding",
"must have one nonnegative value per spatial dimension",
));
}
Ok(())
}
pub(crate) fn convolution(
input: &Tensor,
weight: &Tensor,
bias: Option<&Tensor>,
stride: &[i64],
padding: &[i64],
dilation: &[i64],
groups: i64,
) -> Result<Tensor> {
ensure_device("convolution weight", weight, input_device(input)?)?;
let shape = weight.size();
let dimensions = stride.len();
if shape.len() != dimensions + 2 {
return Err(RustTorchError::InvalidDimensions {
context: "convolution weight".to_owned(),
expected: format!("rank {}", dimensions + 2),
actual: format!("shape {shape:?}"),
});
}
let in_channels = shape[1]
.checked_mul(groups)
.ok_or_else(|| invalid("groups", "input channels exceed i64"))?;
validate_conv_config(
in_channels,
shape[0],
&shape[2..],
stride,
padding,
dilation,
groups,
)?;
let input_shape = input.size();
if ![dimensions + 1, dimensions + 2].contains(&input_shape.len()) {
return Err(RustTorchError::InvalidDimensions {
context: "convolution input".to_owned(),
expected: format!("rank {} or {}", dimensions + 1, dimensions + 2),
actual: format!("shape {input_shape:?}"),
});
}
if input_shape[input_shape.len() - dimensions - 1] != in_channels {
return Err(invalid(
"input channels",
"must match weight channels multiplied by groups",
));
}
if let Some(bias) = bias {
ensure_device("convolution bias", bias, input_device(input)?)?;
if bias.size() != [shape[0]] {
return Err(RustTorchError::ShapeMismatch {
name: "convolution bias".to_owned(),
expected: vec![shape[0]],
actual: bias.size(),
});
}
}
match dimensions {
1 => input.f_conv1d(weight, bias, stride, padding, dilation, groups),
2 => input.f_conv2d(weight, bias, stride, padding, dilation, groups),
3 => input.f_conv3d(weight, bias, stride, padding, dilation, groups),
_ => {
return Err(invalid(
"convolution dimensions",
"must be one, two, or three",
));
}
}
.map_err(Into::into)
}
pub fn layer_norm(
input: &Tensor,
normalized_shape: &[i64],
weight: Option<&Tensor>,
bias: Option<&Tensor>,
eps: f64,
) -> Result<Tensor> {
validate_layer_norm(normalized_shape, eps)?;
for (name, tensor) in [("layer norm weight", weight), ("layer norm bias", bias)] {
if let Some(tensor) = tensor {
ensure_device(name, tensor, input_device(input)?)?;
if tensor.size() != normalized_shape {
return Err(RustTorchError::ShapeMismatch {
name: name.to_owned(),
expected: normalized_shape.to_vec(),
actual: tensor.size(),
});
}
}
}
input
.f_layer_norm(normalized_shape, weight, bias, eps, true)
.map_err(Into::into)
}
pub(crate) fn validate_layer_norm(normalized_shape: &[i64], eps: f64) -> Result<()> {
if normalized_shape.is_empty() || normalized_shape.iter().any(|&dimension| dimension <= 0) {
return Err(invalid(
"normalized_shape",
"must contain at least one positive dimension",
));
}
if !eps.is_finite() || eps < 0.0 {
return Err(invalid("eps", "must be finite and nonnegative"));
}
Ok(())
}
pub fn embedding(
input: &Tensor,
weight: &Tensor,
padding_idx: Option<i64>,
scale_grad_by_freq: bool,
sparse: bool,
) -> Result<Tensor> {
ensure_device("embedding weight", weight, input_device(input)?)?;
let shape = weight.size();
if shape.len() != 2 || shape[0] <= 0 || shape[1] <= 0 {
return Err(invalid(
"embedding weight",
"must have shape [positive vocabulary size, positive vector width]",
));
}
let padding_idx =
validate_embedding_options(shape[0], padding_idx, scale_grad_by_freq, sparse)?;
Tensor::f_embedding(
weight,
input,
padding_idx.unwrap_or(-1),
scale_grad_by_freq,
sparse,
)
.map_err(Into::into)
}
pub(crate) fn validate_embedding_options(
num_embeddings: i64,
padding_idx: Option<i64>,
scale_grad_by_freq: bool,
sparse: bool,
) -> Result<Option<i64>> {
if sparse && scale_grad_by_freq {
return Err(invalid(
"scale_grad_by_freq",
"is not supported with sparse embedding gradients",
));
}
padding_idx
.map(|index| {
if index < -num_embeddings || index >= num_embeddings {
Err(invalid(
"padding_idx",
"must be within the vocabulary, allowing negative indexing",
))
} else {
Ok(if index < 0 {
index + num_embeddings
} else {
index
})
}
})
.transpose()
}
fn invalid(field: &'static str, reason: &str) -> RustTorchError {
RustTorchError::InvalidConfiguration {
field,
reason: reason.to_owned(),
}
}
pub(super) fn input_device(input: &Tensor) -> Result<tch::Device> {
if input.defined() {
Ok(input.device())
} else {
Err(RustTorchError::InvalidDimensions {
context: "functional input".to_owned(),
expected: "a defined tensor".to_owned(),
actual: "undefined tensor".to_owned(),
})
}
}