use std::{
collections::HashMap,
fmt::Display,
sync::atomic::{AtomicUsize, Ordering},
};
use nove_tensor::Tensor;
use crate::{Model, ModelError};
static ID: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug, Clone)]
pub struct ReLU {
id: usize,
}
impl ReLU {
pub fn new() -> Self {
Self {
id: ID.fetch_add(1, Ordering::Relaxed),
}
}
}
impl Model for ReLU {
type Input = Tensor;
type Output = Tensor;
fn forward(&mut self, input: Self::Input) -> Result<Self::Output, crate::ModelError> {
Ok(input.relu()?)
}
fn require_grad(&mut self, _: bool) -> Result<(), crate::ModelError> {
Ok(())
}
fn to_device(&mut self, _: &nove_tensor::Device) -> Result<(), crate::ModelError> {
Ok(())
}
fn to_dtype(&mut self, _: &nove_tensor::DType) -> Result<(), crate::ModelError> {
Ok(())
}
fn parameters(&self) -> Result<Vec<nove_tensor::Tensor>, crate::ModelError> {
Ok(vec![])
}
fn named_parameters(&self) -> Result<HashMap<String, Tensor>, ModelError> {
Ok(HashMap::new())
}
}
impl Display for ReLU {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "relu.{}()", self.id)
}
}