use crate::{Error, Matrix, Result, matrix};
use super::super::{Module, ModuleRegistry, NamedRngState};
fn channel_mask(
input: &Matrix,
spatial_rank: usize,
probability: f32,
seed: u64,
rng: &NamedRngState,
) -> Result<Matrix> {
let shape = input.shape();
let mask_shape = shape[..2].to_vec();
let ones = Matrix::new_from_slice_handle(
input.runtime(),
mask_shape.clone(),
&vec![1.0_f32; mask_shape.iter().product()],
)?;
let mask = matrix::dropout_streamed(&ones, probability, seed, rng)?;
mask.reshape(
[shape[0], shape[1]]
.into_iter()
.chain(vec![1; spatial_rank])
.collect::<Vec<_>>(),
)
}
fn validate_spatial(name: &str, input: &Matrix, spatial_rank: usize) -> Result<()> {
if input.shape().len() != spatial_rank + 2 {
return Err(Error::invalid_argument(format!(
"{name} requires a rank-{} [N, C, ...] input; found {:?}",
spatial_rank + 2,
input.shape()
)));
}
Ok(())
}
macro_rules! channel_dropout {
($name:ident, $spatial:literal, $doc:literal) => {
pub struct $name {
probability: f32,
seed: u64,
draw: NamedRngState,
registry: ModuleRegistry,
}
impl $name {
pub fn new(probability: f32) -> Result<Self> {
Self::new_with_seed(probability, 0)
}
pub fn new_with_seed(probability: f32, seed: u64) -> Result<Self> {
if !probability.is_finite() || !(0.0..1.0).contains(&probability) {
return Err(Error::invalid_argument(
"dropout probability must be finite and in [0, 1)",
));
}
let mut registry = ModuleRegistry::new();
let draw = registry.register_rng_state("draw", 0)?;
Ok(Self {
probability,
seed,
draw,
registry,
})
}
pub const fn probability(&self) -> f32 {
self.probability
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
if !self.is_training() || self.probability == 0.0 {
return Ok(input.clone());
}
validate_spatial(stringify!($name), input, $spatial)?;
let mask = channel_mask(input, $spatial, self.probability, self.seed, &self.draw)?;
matrix::mul(input, &mask)
}
}
impl Module for $name {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
$name::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
};
}
channel_dropout!(
Dropout1d,
1,
"Randomly zero entire channels of a `[N, C, L]` input in training mode."
);
channel_dropout!(
Dropout2d,
2,
"Randomly zero entire channels of a `[N, C, H, W]` input in training mode."
);
channel_dropout!(
Dropout3d,
3,
"Randomly zero entire channels of a `[N, C, D, H, W]` input in training mode."
);
const ALPHA_PRIME: f32 = -1.758_099_3_f32;
fn alpha_dropout_apply(input: &Matrix, raw_mask: &Matrix, probability: f32) -> Result<Matrix> {
let scale_a = (1.0_f32 + ALPHA_PRIME * ALPHA_PRIME * probability * (1.0 - probability))
.sqrt()
.recip();
let scale_b = -scale_a * ALPHA_PRIME * probability;
let retained = matrix::mul(input, raw_mask)?;
let dropped = matrix::scale(
&matrix::add_scalar(&matrix::neg(raw_mask)?, 1.0)?,
ALPHA_PRIME,
)?;
matrix::add_scalar(
&matrix::scale(&matrix::add(&retained, &dropped)?, scale_a)?,
scale_b,
)
}
fn raw_mask(
keep_shape: &[usize],
input: &Matrix,
probability: f32,
seed: u64,
rng: &NamedRngState,
) -> Result<Matrix> {
let ones = Matrix::new_from_slice_handle(
input.runtime(),
keep_shape.to_vec(),
&vec![1.0_f32; keep_shape.iter().product()],
)?;
matrix::scale(
&matrix::dropout_streamed(&ones, probability, seed, rng)?,
1.0 - probability,
)
}
pub struct AlphaDropout {
probability: f32,
seed: u64,
draw: NamedRngState,
registry: ModuleRegistry,
}
impl AlphaDropout {
pub fn new(probability: f32) -> Result<Self> {
Self::new_with_seed(probability, 0)
}
pub fn new_with_seed(probability: f32, seed: u64) -> Result<Self> {
if !probability.is_finite() || !(0.0..1.0).contains(&probability) {
return Err(Error::invalid_argument(
"alpha dropout probability must be finite and in [0, 1)",
));
}
let mut registry = ModuleRegistry::new();
let draw = registry.register_rng_state("draw", 0)?;
Ok(Self {
probability,
seed,
draw,
registry,
})
}
pub const fn probability(&self) -> f32 {
self.probability
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
if !self.is_training() || self.probability == 0.0 {
return Ok(input.clone());
}
let mask = raw_mask(
input.shape(),
input,
self.probability,
self.seed,
&self.draw,
)?;
alpha_dropout_apply(input, &mask, self.probability)
}
}
impl Module for AlphaDropout {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
AlphaDropout::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct FeatureAlphaDropout {
probability: f32,
seed: u64,
draw: NamedRngState,
registry: ModuleRegistry,
}
impl FeatureAlphaDropout {
pub fn new(probability: f32) -> Result<Self> {
Self::new_with_seed(probability, 0)
}
pub fn new_with_seed(probability: f32, seed: u64) -> Result<Self> {
if !probability.is_finite() || !(0.0..1.0).contains(&probability) {
return Err(Error::invalid_argument(
"feature alpha dropout probability must be finite and in [0, 1)",
));
}
let mut registry = ModuleRegistry::new();
let draw = registry.register_rng_state("draw", 0)?;
Ok(Self {
probability,
seed,
draw,
registry,
})
}
pub const fn probability(&self) -> f32 {
self.probability
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
if !self.is_training() || self.probability == 0.0 {
return Ok(input.clone());
}
let shape = input.shape();
if shape.len() < 2 {
return Err(Error::invalid_argument(
"FeatureAlphaDropout requires an input of rank at least two",
));
}
let mask = raw_mask(&shape[..2], input, self.probability, self.seed, &self.draw)?.reshape(
[shape[0], shape[1]]
.into_iter()
.chain(vec![1; shape.len() - 2])
.collect::<Vec<_>>(),
)?;
alpha_dropout_apply(input, &mask, self.probability)
}
}
impl Module for FeatureAlphaDropout {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
FeatureAlphaDropout::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}