pub struct CrossAttention { /* private fields */ }Expand description
Cross-attention module with optional flash attention support.
When flash attention is enabled (via feature flag and configuration), uses memory-efficient O(N) block-wise attention computation instead of the standard O(N^2) attention matrix.
Implementations§
Source§impl CrossAttention
impl CrossAttention
Sourcepub fn new(
vs: VarBuilder<'_>,
query_dim: usize,
context_dim: Option<usize>,
heads: usize,
dim_head: usize,
) -> Result<Self>
pub fn new( vs: VarBuilder<'_>, query_dim: usize, context_dim: Option<usize>, heads: usize, dim_head: usize, ) -> Result<Self>
Create a new cross-attention module with standard attention.
Sourcepub fn new_with_flash(
vs: VarBuilder<'_>,
query_dim: usize,
context_dim: Option<usize>,
heads: usize,
dim_head: usize,
use_flash_attention: bool,
flash_block_size: usize,
) -> Result<Self>
pub fn new_with_flash( vs: VarBuilder<'_>, query_dim: usize, context_dim: Option<usize>, heads: usize, dim_head: usize, use_flash_attention: bool, flash_block_size: usize, ) -> Result<Self>
Create a new cross-attention module with optional flash attention.
§Arguments
vs- Variable builder for weight initializationquery_dim- Query input dimensioncontext_dim- Context dimension (None for self-attention)heads- Number of attention headsdim_head- Dimension per headuse_flash_attention- Whether to use flash attentionflash_block_size- Block size for flash attention tiling
Sourcepub fn forward(&self, xs: &Tensor, context: Option<&Tensor>) -> Result<Tensor>
pub fn forward(&self, xs: &Tensor, context: Option<&Tensor>) -> Result<Tensor>
Scaled-dot-product attention (standard or flash based on configuration).
Automatically dispatches to flash attention when enabled and the feature is available, otherwise uses standard O(N^2) attention.
Sourcepub fn is_flash_attention_enabled(&self) -> bool
pub fn is_flash_attention_enabled(&self) -> bool
Check if flash attention is enabled for this module.
Returns true only if flash attention was requested during construction
AND the flash_attention feature is enabled.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CrossAttention
impl !RefUnwindSafe for CrossAttention
impl Send for CrossAttention
impl Sync for CrossAttention
impl Unpin for CrossAttention
impl UnsafeUnpin for CrossAttention
impl !UnwindSafe for CrossAttention
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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