pub struct Softmax<'src, 'dst> { /* private fields */ }Expand description
Computes the softmax function over a slice of floats.
The implementation uses a three-pass approach for numerical stability. See https://ogunlao.github.io/2020/04/26/you_dont_really_know_softmax.html. and https://arxiv.org/abs/2001.04438.
Implementations§
Source§impl<'src, 'dst> Softmax<'src, 'dst>
impl<'src, 'dst> Softmax<'src, 'dst>
Sourcepub fn new(input: &'src [f32], output: &'dst mut [MaybeUninit<f32>]) -> Self
pub fn new(input: &'src [f32], output: &'dst mut [MaybeUninit<f32>]) -> Self
Construct a softmax operation which reads input and writes to to
output.
Sourcepub fn new_mut(input: &'dst mut [f32]) -> Selfwhere
'dst: 'src,
pub fn new_mut(input: &'dst mut [f32]) -> Selfwhere
'dst: 'src,
Construct a softmax operation which updates input in place.
Sourcepub fn flush_nans_to_zero(self, flush: bool) -> Self
pub fn flush_nans_to_zero(self, flush: bool) -> Self
Replace NaN values in the output with zeros.
This option exists to changing handling of the case where the input values are all negative infinity. In that case the normal output would be NaN.
In the context of attention operations which use negative infinity to represent masked token positions, it is preferable to produce zeros as the output if all input positions are masked. See https://github.com/pytorch/pytorch/issues/41508.
Trait Implementations§
Auto Trait Implementations§
impl<'src, 'dst> Freeze for Softmax<'src, 'dst>
impl<'src, 'dst> RefUnwindSafe for Softmax<'src, 'dst>
impl<'src, 'dst> Send for Softmax<'src, 'dst>
impl<'src, 'dst> Sync for Softmax<'src, 'dst>
impl<'src, 'dst> Unpin for Softmax<'src, 'dst>
impl<'src, 'dst> !UnwindSafe for Softmax<'src, 'dst>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more