pub struct RelativePositionBias {
pub num_heads: usize,
pub num_buckets: usize,
pub max_distance: usize,
pub bidirectional: bool,
/* private fields */
}Expand description
T5-style relative position bias that adds a learned scalar bias to attention logits based on the relative distance between query and key positions.
Fields§
§num_heads: usizeNumber of attention heads.
num_buckets: usizeNumber of learned buckets for distances.
max_distance: usizeMaximum distance to consider (beyond this, distances are clamped).
bidirectional: boolIf true, use separate buckets for forward and backward directions.
Implementations§
Source§impl RelativePositionBias
impl RelativePositionBias
Sourcepub fn new(
num_heads: usize,
num_buckets: usize,
max_distance: usize,
bidirectional: bool,
) -> Self
pub fn new( num_heads: usize, num_buckets: usize, max_distance: usize, bidirectional: bool, ) -> Self
Create a new relative position bias (zero-initialized).
Sourcepub fn compute_bias(&self, query_len: usize, key_len: usize) -> Array3<f64>
pub fn compute_bias(&self, query_len: usize, key_len: usize) -> Array3<f64>
Compute the attention bias matrix of shape [num_heads, q_len, k_len].
For each (q, k) pair the relative position q - k is mapped to a bucket
and the corresponding learned bias is looked up.
Sourcepub fn update_biases(
&mut self,
new_biases: Array2<f64>,
) -> Result<(), PositionError>
pub fn update_biases( &mut self, new_biases: Array2<f64>, ) -> Result<(), PositionError>
Update the learned bias table.
new_biases must have shape [num_buckets, num_heads].
Trait Implementations§
Source§impl Clone for RelativePositionBias
impl Clone for RelativePositionBias
Source§fn clone(&self) -> RelativePositionBias
fn clone(&self) -> RelativePositionBias
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for RelativePositionBias
impl RefUnwindSafe for RelativePositionBias
impl Send for RelativePositionBias
impl Sync for RelativePositionBias
impl Unpin for RelativePositionBias
impl UnsafeUnpin for RelativePositionBias
impl UnwindSafe for RelativePositionBias
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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