use burn::{
config::Config,
module::Module,
prelude::Tensor,
};
pub trait PowOpModuleMeta {
fn exponent(&self) -> f64;
}
#[derive(Config, Debug)]
pub struct PowOpModuleConfig {
pub exponent: f64,
}
impl PowOpModuleMeta for PowOpModuleConfig {
fn exponent(&self) -> f64 {
self.exponent
}
}
impl PowOpModuleConfig {
#[must_use]
pub fn init(&self) -> PowOpModule {
PowOpModule {
exponent: self.exponent,
}
}
}
#[derive(Module, Debug)]
pub struct PowOpModule {
pub exponent: f64,
}
impl PowOpModuleMeta for PowOpModule {
fn exponent(&self) -> f64 {
self.exponent
}
}
impl PowOpModule {
pub fn forward<const D: usize>(
&self,
input: Tensor<D>,
) -> Tensor<D> {
input.powf_scalar(self.exponent)
}
}
#[cfg(test)]
mod tests {
use burn::tensor::{
TensorData,
Tolerance,
};
use super::*;
use crate::support::testing::cpu_device;
#[test]
fn test_meta() {
let config = PowOpModuleConfig::new(3.0);
let module = config.init();
assert_eq!(config.exponent(), 3.0);
assert_eq!(module.exponent(), config.exponent());
}
#[test]
fn test_forward() {
let device = cpu_device();
let x = Tensor::<1>::from_floats([-2.0, -0.5, 0.0, 1.0, 3.0], &device);
PowOpModuleConfig::new(2.0)
.init()
.forward(x)
.into_data()
.assert_approx_eq::<f32>(
&TensorData::from([4.0f32, 0.25, 0.0, 1.0, 9.0]),
Tolerance::absolute(1e-6),
);
}
}