Struct MultiHeadAttention
pub struct MultiHeadAttention {
pub query: Linear,
pub key: Linear,
pub value: Linear,
pub output: Linear,
pub dropout: Dropout,
pub activation: Gelu,
pub d_model: usize,
pub n_heads: usize,
pub d_k: usize,
pub min_float: f64,
pub quiet_softmax: bool,
}Expand description
The multihead attention module as describe in the paper Attention Is All You Need.
§Params
query:Linearlayer withd_modelinput and output features.key:Linearlayer withd_modelinput and output features.value:Linearlayer withd_modelinput and output features.output:Linearlayer withd_modelinput and output features.
Should be created with MultiHeadAttentionConfig.
Fields§
§query: LinearLinear layer to transform the input features into the query space.
key: LinearLinear layer to transform the input features into the key space.
value: LinearLinear layer to transform the input features into the value space.
output: LinearLinear layer to transform the output features back to the original space.
dropout: DropoutDropout layer.
activation: GeluActivation function.
d_model: usizeThe size of each linear layer.
n_heads: usizeThe number of heads.
d_k: usizeSize of the key and query vectors.
min_float: f64Minimum value a float can take.
quiet_softmax: boolUse “quiet softmax” instead of regular softmax.
Implementations§
§impl MultiHeadAttention
impl MultiHeadAttention
pub fn forward(&self, input: MhaInput) -> MhaOutput
pub fn forward(&self, input: MhaInput) -> MhaOutput
Applies the forward pass on the input tensors.
See MultiHeadAttention for more information.
§Shapes
- query:
[batch_size, seq_length_1, d_model] - key:
[batch_size, seq_length_2, d_model] - value:
[batch_size, seq_length_2, d_model] - output:
[batch_size, seq_length_1, d_model]
§Panics
Panics if the last axis of query, key or value is not d_model, if the batch size
of key or value differs from query, or if the sequence length of value differs
from key.
pub fn forward_cache(&self, input: MhaInput, cache: &mut MhaCache) -> MhaOutput
pub fn forward_cache(&self, input: MhaInput, cache: &mut MhaCache) -> MhaOutput
Applies the forward pass using a cache.
§Shapes
- query:
[batch_size, seq_length_1, d_model] - key:
[batch_size, seq_length_2, d_model] - value:
[batch_size, seq_length_2, d_model] - output:
[batch_size, seq_length_1, d_model]
§Panics
Panics if the last axis of query, key or value is not d_model, if the batch size
of key or value differs from query, or if the sequence length of value differs
from key.
Trait Implementations§
§impl Clone for MultiHeadAttention
impl Clone for MultiHeadAttention
§fn clone(&self) -> MultiHeadAttention
fn clone(&self) -> MultiHeadAttention
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for MultiHeadAttention
impl Debug for MultiHeadAttention
§impl Display for MultiHeadAttention
impl Display for MultiHeadAttention
§impl Module for MultiHeadAttention
impl Module for MultiHeadAttention
§fn num_params(&self) -> usize
fn num_params(&self) -> usize
§fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor,
fn visit<Visitor>(&self, visitor: &mut Visitor)where
Visitor: ModuleVisitor,
§fn map<Mapper>(self, mapper: &mut Mapper) -> MultiHeadAttentionwhere
Mapper: ModuleMapper,
fn map<Mapper>(self, mapper: &mut Mapper) -> MultiHeadAttentionwhere
Mapper: ModuleMapper,
§fn collect_devices(&self, devices: Vec<Device>) -> Vec<Device>
fn collect_devices(&self, devices: Vec<Device>) -> Vec<Device>
§fn to_device(self, device: &Device) -> MultiHeadAttention
fn to_device(self, device: &Device) -> MultiHeadAttention
§fn fork(self, device: &Device) -> MultiHeadAttention
fn fork(self, device: &Device) -> MultiHeadAttention
§fn valid(&self) -> MultiHeadAttention
fn valid(&self) -> MultiHeadAttention
§fn train(self) -> MultiHeadAttention
fn train(self) -> MultiHeadAttention
§fn materialize(self) -> MultiHeadAttention
fn materialize(self) -> MultiHeadAttention
§fn devices(&self) -> Vec<Device>
fn devices(&self) -> Vec<Device>
§fn set_require_grad(self, require_grad: bool) -> Self
fn set_require_grad(self, require_grad: bool) -> Self
§fn set_require_grad_group(self, group: ParamGroup, require_grad: bool) -> Self
fn set_require_grad_group(self, group: ParamGroup, require_grad: bool) -> Self
§fn no_grad(self) -> Self
fn no_grad(self) -> Self
§fn freeze_group(self, group: ParamGroup) -> Self
fn freeze_group(self, group: ParamGroup) -> Self
§fn unfreeze_group(self, group: ParamGroup) -> Self
fn unfreeze_group(self, group: ParamGroup) -> Self
§fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
fn quantize_weights(self, quantizer: &mut Quantizer) -> Self
§fn quantize_weights_group(
self,
quantizer: &mut Quantizer,
group: ParamGroup,
) -> Self
fn quantize_weights_group( self, quantizer: &mut Quantizer, group: ParamGroup, ) -> Self
§fn apply_reparameterization<R>(self, reparameterizer: R) -> Selfwhere
Self: Sized,
R: Reparameterizer,
fn apply_reparameterization<R>(self, reparameterizer: R) -> Selfwhere
Self: Sized,
R: Reparameterizer,
Reparameterizer. Read more§fn apply_lora(self, lora: Lora) -> Selfwhere
Self: Sized,
fn apply_lora(self, lora: Lora) -> Selfwhere
Self: Sized,
§fn apply_qlora(self, qlora: QLora) -> Selfwhere
Self: Sized,
fn apply_qlora(self, qlora: QLora) -> Selfwhere
Self: Sized,
§fn into_record(self) -> ModuleRecordwhere
Self: Sized,
fn into_record(self) -> ModuleRecordwhere
Self: Sized,
ModuleRecord. Read more§fn into_record_group(self, group: ParamGroup) -> ModuleRecordwhere
Self: Sized,
fn into_record_group(self, group: ParamGroup) -> ModuleRecordwhere
Self: Sized,
§fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
fn try_load_record(self, record: ModuleRecord) -> Result<Self, RecordError>where
Self: Sized,
ModuleRecord to this module, returning the loaded
module. Read more§fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
fn load_record(self, record: ModuleRecord) -> Selfwhere
Self: Sized,
ModuleRecord to this module, consuming and returning
it. Read more§fn save_file<P>(self, path: P) -> Result<(), RecordError>
fn save_file<P>(self, path: P) -> Result<(), RecordError>
§fn load_file<P>(self, path: P) -> Self
fn load_file<P>(self, path: P) -> Self
§fn try_load_file<P>(self, path: P) -> Result<Self, RecordError>
fn try_load_file<P>(self, path: P) -> Result<Self, RecordError>
§impl ModuleDisplay for MultiHeadAttention
impl ModuleDisplay for MultiHeadAttention
§fn custom_settings(&self) -> Option<DisplaySettings>
fn custom_settings(&self) -> Option<DisplaySettings>
§fn custom_content(&self, content: Content) -> Option<Content>
fn custom_content(&self, content: Content) -> Option<Content>
§fn format(&self, passed_settings: DisplaySettings) -> String
fn format(&self, passed_settings: DisplaySettings) -> String
§impl ModuleDisplayDefault for MultiHeadAttention
impl ModuleDisplayDefault for MultiHeadAttention
Auto Trait Implementations§
impl !RefUnwindSafe for MultiHeadAttention
impl !UnwindSafe for MultiHeadAttention
impl Freeze for MultiHeadAttention
impl Send for MultiHeadAttention
impl Sync for MultiHeadAttention
impl Unpin for MultiHeadAttention
impl UnsafeUnpin for MultiHeadAttention
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()] Read more§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString]. Read more