oarust 0.8.6

GPU-first semantic computing engine written in Rust
// @generated by tool/gen/nn/generate.py; DO NOT EDIT.
// Source: tool/gen/nn/schema/ml/ml_nn_activation.json
// generator_version=8; schema_sha256=5e544cf80400a3070354390826c3019e086ae1dfce45e4eaf640fb7476e318cc

use crate::{Matrix, Result};

use super::super::{Module, ModuleRegistry, matrix};

// OA donor: oacpp/source/cpp/lib/oa/ml/nn/relu/relu.cpp
// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
/// Rectified linear unit module.
pub struct Relu {
	registry: ModuleRegistry,
}

impl Relu {
	/// Construct the parameterless activation module.
	pub const fn new() -> Self {
		Self {
			registry: ModuleRegistry::new(),
		}
	}

	/// Apply the activation without submitting or waiting.
	///
	/// # Errors
	///
	/// Returns an error from the underlying Matrix operation.
	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
		matrix::relu(input)
	}
}

impl Default for Relu {
	fn default() -> Self {
		Self::new()
	}
}

impl Module for Relu {
	fn forward(&self, input: &Matrix) -> Result<Matrix> {
		Relu::forward(self, input)
	}

	fn registry(&self) -> &ModuleRegistry {
		&self.registry
	}
}

// OA donor: oacpp/source/cpp/lib/oa/ml/nn/gelu/gelu.cpp
// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
/// Gaussian error linear unit module using OA's tanh approximation.
pub struct Gelu {
	registry: ModuleRegistry,
}

impl Gelu {
	/// Construct the parameterless activation module.
	pub const fn new() -> Self {
		Self {
			registry: ModuleRegistry::new(),
		}
	}

	/// Apply the activation without submitting or waiting.
	///
	/// # Errors
	///
	/// Returns an error from the underlying Matrix operation.
	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
		matrix::gelu(input)
	}
}

impl Default for Gelu {
	fn default() -> Self {
		Self::new()
	}
}

impl Module for Gelu {
	fn forward(&self, input: &Matrix) -> Result<Matrix> {
		Gelu::forward(self, input)
	}

	fn registry(&self) -> &ModuleRegistry {
		&self.registry
	}
}

// OA donor: oacpp/source/cpp/lib/oa/ml/nn/silu/silu.cpp
// Adaptation: Rust ownership, ModuleRegistry and checked error contracts; preserved operation behavior
/// Sigmoid linear unit module.
pub struct Silu {
	registry: ModuleRegistry,
}

impl Silu {
	/// Construct the parameterless activation module.
	pub const fn new() -> Self {
		Self {
			registry: ModuleRegistry::new(),
		}
	}

	/// Apply the activation without submitting or waiting.
	///
	/// # Errors
	///
	/// Returns an error from the underlying Matrix operation.
	pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
		matrix::silu(input)
	}
}

impl Default for Silu {
	fn default() -> Self {
		Self::new()
	}
}

impl Module for Silu {
	fn forward(&self, input: &Matrix) -> Result<Matrix> {
		Silu::forward(self, input)
	}

	fn registry(&self) -> &ModuleRegistry {
		&self.registry
	}
}