Skip to main content

Data2VecModel

Struct Data2VecModel 

Source
pub struct Data2VecModel { /* private fields */ }
Expand description

Struct-based Data2Vec model that owns student encoder layers and a teacher EMA state.

Student weights per layer are stored as:

  • student_w[l][d_model × d_model] weight matrix (row-major).
  • student_b[l][d_model] bias vector.

Teacher weights are stored flat in Data2VecState::teacher_params in the same sequential layout (w0, b0, w1, b1, …).

Implementations§

Source§

impl Data2VecModel

Source

pub fn new(config: Data2VecModelConfig, rng: &mut LcgRng) -> SslResult<Self>

Create a new Data2VecModel with Kaiming-initialised student layers and a teacher state cloned from the initial student parameters.

§Errors
Source

pub fn encode_student(&self, x: &[f32], n_patches: usize) -> SslResult<Vec<f32>>

Encode a sequence of patch embeddings with the student encoder.

Each layer applies: token ← ReLU(W · token + b) independently per token.

§Arguments
  • x — flat [n_patches × d_model] input (row-major).
  • n_patches — number of tokens in the sequence.
§Errors

SslError::DimensionMismatch when x.len() != n_patches * d_model.

Source

pub fn encode_teacher(&self, x: &[f32], n_patches: usize) -> SslResult<Vec<f32>>

Encode a sequence of patch embeddings with the teacher encoder.

Uses the teacher weight matrices stored in Data2VecState::teacher_params.

§Arguments
  • x — flat [n_patches × d_model] input (row-major).
  • n_patches — number of tokens in the sequence.
§Errors

SslError::DimensionMismatch when x.len() != n_patches * d_model.

Source

pub fn loss(&self, x: &[f32], mask: &[bool], n_patches: usize) -> SslResult<f32>

Compute the Data2Vec loss for a masked input.

  1. Encodes with the student encoder → student_repr [n_patches × d_model].
  2. Encodes with the teacher encoder → teacher_repr [n_patches × d_model].
  3. Computes Huber loss at masked positions via data2vec_loss.
§Arguments
  • x — flat [n_patches × d_model] input.
  • mask[n_patches] boolean; true = masked position.
  • n_patches — number of tokens.
§Errors

Propagates dimension and config errors.

Source

pub fn ema_update(&mut self) -> SslResult<()>

Apply the EMA update: teacher ← ema_decay · teacher + (1 − ema_decay) · student.

§Errors
Source

pub fn d_model(&self) -> usize

Return the token/patch embedding dimension.

Trait Implementations§

Source§

impl Clone for Data2VecModel

Source§

fn clone(&self) -> Data2VecModel

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Data2VecModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.