use burn_core as burn;
use burn::config::Config;
use burn::module::Module;
use burn::module::{Content, DisplaySettings, ModuleDisplay};
use burn::tensor::Tensor;
use burn::tensor::activation::softplus_with_threshold;
#[derive(Module, Debug)]
#[module(custom_display)]
pub struct Softplus {
pub beta: f64,
pub threshold: f64,
}
#[derive(Config, Debug)]
pub struct SoftplusConfig {
#[config(default = "1.0")]
pub beta: f64,
#[config(default = "20.0")]
pub threshold: f64,
}
impl SoftplusConfig {
pub fn init(&self) -> Softplus {
Softplus {
beta: self.beta,
threshold: self.threshold,
}
}
}
impl ModuleDisplay for Softplus {
fn custom_settings(&self) -> Option<DisplaySettings> {
DisplaySettings::new()
.with_new_line_after_attribute(false)
.optional()
}
fn custom_content(&self, content: Content) -> Option<Content> {
content
.add("beta", &self.beta)
.add("threshold", &self.threshold)
.optional()
}
}
impl Softplus {
pub fn forward<const D: usize>(&self, input: Tensor<D>) -> Tensor<D> {
softplus_with_threshold(input, self.beta, self.threshold)
}
}
#[cfg(test)]
#[allow(clippy::approx_constant)]
mod tests {
use super::*;
use burn::tensor::TensorData;
use burn::tensor::Tolerance;
type FT = f32;
#[test]
fn test_softplus_forward() {
let device = Default::default();
let model = SoftplusConfig::new().init();
let input = Tensor::<2>::from_data(TensorData::from([[0.0, 1.0, -1.0]]), &device);
let out = model.forward(input);
let expected = TensorData::from([[0.6931, 1.3133, 0.3133]]);
out.to_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
fn test_softplus_with_beta() {
let device = Default::default();
let model = SoftplusConfig::new().with_beta(2.0).init();
let input = Tensor::<2>::from_data(TensorData::from([[0.0, 1.0]]), &device);
let out = model.forward(input);
let expected = TensorData::from([[0.3466, 1.0635]]);
out.to_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
fn test_softplus_default_threshold() {
let device = Default::default();
let model = SoftplusConfig::new().init();
assert_eq!(model.threshold, 20.0);
let input = Tensor::<2>::from_data(TensorData::from([[100.0, 1000.0]]), &device);
let out = model.forward(input);
let expected = TensorData::from([[100.0, 1000.0]]);
out.to_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
fn test_softplus_with_threshold() {
let device = Default::default();
let input = Tensor::<2>::from_data(TensorData::from([[5.0]]), &device);
let out = SoftplusConfig::new()
.with_threshold(10.0)
.init()
.forward(input.clone());
out.to_data()
.assert_approx_eq::<FT>(&TensorData::from([[5.0067153]]), Tolerance::default());
let out = SoftplusConfig::new()
.with_threshold(1.0)
.init()
.forward(input);
out.to_data()
.assert_approx_eq::<FT>(&TensorData::from([[5.0]]), Tolerance::default());
}
#[test]
fn display() {
let config = SoftplusConfig::new().init();
assert_eq!(
alloc::format!("{config}"),
"Softplus {beta: 1, threshold: 20}"
);
}
}