use burn::{
config::Config,
module::Module,
prelude::Tensor,
};
use crate::{
ops::drop::drop_path,
support::validators,
};
pub trait DropPathMeta {
fn drop_prob(&self) -> f64;
fn keep_prob(&self) -> f64 {
1.0 - self.drop_prob()
}
fn scale_by_keep(&self) -> bool;
}
#[derive(Config, Debug)]
pub struct DropPathConfig {
#[config(default = 0.0)]
pub drop_prob: f64,
#[config(default = true)]
pub scale_by_keep: bool,
}
impl DropPathMeta for DropPathConfig {
fn drop_prob(&self) -> f64 {
self.drop_prob
}
fn scale_by_keep(&self) -> bool {
self.scale_by_keep
}
}
impl DropPathConfig {
#[inline(always)]
#[must_use]
pub fn init(&self) -> DropPath {
DropPath {
drop_prob: validators::expect_probability(self.drop_prob),
scale_by_keep: self.scale_by_keep,
}
}
}
#[derive(Module, Debug)]
pub struct DropPath {
pub drop_prob: f64,
pub scale_by_keep: bool,
}
impl DropPathMeta for DropPath {
fn drop_prob(&self) -> f64 {
self.drop_prob
}
fn scale_by_keep(&self) -> bool {
self.scale_by_keep
}
}
impl DropPath {
#[must_use]
pub fn forward<const D: usize>(
&self,
input: Tensor<D>,
) -> Tensor<D> {
let training = input.device().is_autodiff();
drop_path(input, self.drop_prob, training, self.scale_by_keep)
}
#[inline]
#[must_use]
pub fn with_skip<const D: usize, F>(
&self,
x: Tensor<D>,
f: F,
) -> Tensor<D>
where
F: FnOnce(Tensor<D>) -> Tensor<D>,
{
x.clone() + self.forward(f(x))
}
}
#[cfg(test)]
mod tests {
use burn::{
prelude::Tensor,
tensor::Distribution,
};
use super::*;
use crate::support::testing::cpu_device;
#[test]
fn test_drop_path() {
let device = cpu_device();
let drop_prob = 0.5;
let scale_by_keep = true;
let config = DropPathConfig {
drop_prob,
scale_by_keep,
};
let module = config.init();
let input = Tensor::<4>::random([2, 3, 4, 5], Distribution::Uniform(0.0, 1.0), &device);
let output = module.forward(input.clone());
assert_eq!(input.dims(), output.dims());
}
#[test]
fn test_droppath_module() {
let drop_prob = 0.2;
let config = DropPathConfig::new().with_drop_prob(drop_prob);
assert_eq!(config.drop_prob(), 0.2);
assert_eq!(config.keep_prob(), 1.0 - drop_prob);
assert!(config.scale_by_keep());
let module = config.init();
assert_eq!(module.drop_prob(), 0.2);
assert_eq!(module.keep_prob(), 1.0 - drop_prob);
assert!(module.scale_by_keep());
let device = cpu_device();
let shape = [2, 3, 4];
let x = Tensor::<3>::random(shape, Distribution::Uniform(0.0, 1.0), &device);
let output = module.forward(x.clone());
assert_eq!(x.dims(), output.dims());
}
}