use crate::{Matrix, Result};
use std::collections::HashMap;
#[path = "loss.gen.rs"]
mod loss;
pub(in crate::ml::autograd) use loss::{
GradNodeBce, GradNodeCrossEntropy, GradNodeL1, GradNodeMaskedCrossEntropy, GradNodeMse,
GradNodePpoClippedPolicy, GradNodeSmoothL1,
};
#[path = "matrix/activation.gen.rs"]
mod matrix_activation;
pub(in crate::ml::autograd) use matrix_activation::{
GradNodeElu, GradNodeGelu, GradNodeLeakyRelu, GradNodeMish, GradNodeRelu, GradNodeSigmoid,
GradNodeSilu, GradNodeSoftplus, GradNodeTanh,
};
#[path = "matrix/pool.gen.rs"]
mod matrix_pool;
pub(in crate::ml::autograd) use matrix_pool::{
GradNodeAdaptiveAvgPool2d, GradNodeAvgPool2d, GradNodeMaxPool2d,
};
#[path = "matrix/rope.gen.rs"]
mod matrix_rope;
pub(in crate::ml::autograd) use matrix_rope::GradNodeRope;
#[path = "matrix/swiglu.gen.rs"]
mod matrix_swiglu;
pub(in crate::ml::autograd) use matrix_swiglu::{GradNodeSiluMul, GradNodeSwiglu};
#[path = "matrix/upsample.gen.rs"]
mod matrix_upsample;
pub(in crate::ml::autograd) use matrix_upsample::GradNodeUpsample2d;
pub(in crate::ml::autograd) enum BackwardStatus {
Skipped,
Recorded,
}
pub(in crate::ml::autograd) enum GradNodeOperation {
PpoClippedPolicy(GradNodePpoClippedPolicy),
CrossEntropy(GradNodeCrossEntropy),
MaskedCrossEntropy(GradNodeMaskedCrossEntropy),
SmoothL1(GradNodeSmoothL1),
Mse(GradNodeMse),
L1(GradNodeL1),
Bce(GradNodeBce),
Gelu(GradNodeGelu),
Silu(GradNodeSilu),
Relu(GradNodeRelu),
Tanh(GradNodeTanh),
Sigmoid(GradNodeSigmoid),
LeakyRelu(GradNodeLeakyRelu),
Elu(GradNodeElu),
Mish(GradNodeMish),
Softplus(GradNodeSoftplus),
Swiglu(GradNodeSwiglu),
Rope(GradNodeRope),
AvgPool2d(GradNodeAvgPool2d),
MaxPool2d(GradNodeMaxPool2d),
AdaptiveAvgPool2d(GradNodeAdaptiveAvgPool2d),
Upsample2d(GradNodeUpsample2d),
SiluMul(GradNodeSiluMul),
}
impl GradNodeOperation {
pub(in crate::ml::autograd) fn output_id(&self) -> u64 {
match self {
Self::PpoClippedPolicy(node) => node.output_id,
Self::CrossEntropy(node) => node.output_id,
Self::MaskedCrossEntropy(node) => node.output_id,
Self::SmoothL1(node) => node.output_id,
Self::Mse(node) => node.output_id,
Self::L1(node) => node.output_id,
Self::Bce(node) => node.output_id,
Self::Gelu(node) => node.output_id,
Self::Silu(node) => node.output_id,
Self::Relu(node) => node.output_id,
Self::Tanh(node) => node.output_id,
Self::Sigmoid(node) => node.output_id,
Self::LeakyRelu(node) => node.output_id,
Self::Elu(node) => node.output_id,
Self::Mish(node) => node.output_id,
Self::Softplus(node) => node.output_id,
Self::Swiglu(node) => node.output_id,
Self::Rope(node) => node.output_id,
Self::AvgPool2d(node) => node.output_id,
Self::MaxPool2d(node) => node.output_id,
Self::AdaptiveAvgPool2d(node) => node.output_id,
Self::Upsample2d(node) => node.output_id,
Self::SiluMul(node) => node.output_id,
}
}
pub(in crate::ml::autograd) fn backward(
&self,
gradients: &mut HashMap<u64, Matrix>,
) -> Result<BackwardStatus> {
match self {
Self::PpoClippedPolicy(node) => node.backward(gradients),
Self::CrossEntropy(node) => node.backward(gradients),
Self::MaskedCrossEntropy(node) => node.backward(gradients),
Self::SmoothL1(node) => node.backward(gradients),
Self::Mse(node) => node.backward(gradients),
Self::L1(node) => node.backward(gradients),
Self::Bce(node) => node.backward(gradients),
Self::Gelu(node) => node.backward(gradients),
Self::Silu(node) => node.backward(gradients),
Self::Relu(node) => node.backward(gradients),
Self::Tanh(node) => node.backward(gradients),
Self::Sigmoid(node) => node.backward(gradients),
Self::LeakyRelu(node) => node.backward(gradients),
Self::Elu(node) => node.backward(gradients),
Self::Mish(node) => node.backward(gradients),
Self::Softplus(node) => node.backward(gradients),
Self::Swiglu(node) => node.backward(gradients),
Self::Rope(node) => node.backward(gradients),
Self::AvgPool2d(node) => node.backward(gradients),
Self::MaxPool2d(node) => node.backward(gradients),
Self::AdaptiveAvgPool2d(node) => node.backward(gradients),
Self::Upsample2d(node) => node.backward(gradients),
Self::SiluMul(node) => node.backward(gradients),
}
}
}