Skip to main content

ClipTextEncoder

Struct ClipTextEncoder 

Source
pub struct ClipTextEncoder {
    pub config: ClipTextConfig,
    pub token_embedding: Vec<f32>,
    pub positional_embedding: Vec<f32>,
    /* private fields */
}
Expand description

The CLIP Transformer text tower.

Fields§

§config: ClipTextConfig

Configuration.

§token_embedding: Vec<f32>

Token embedding table: [vocab_size · width] row-major.

§positional_embedding: Vec<f32>

Learned positional embedding: [n_ctx · width] row-major.

Implementations§

Source§

impl ClipTextEncoder

Source

pub fn new(cfg: ClipTextConfig, rng: &mut LcgRng) -> VisionResult<Self>

Construct a CLIP text encoder with Gaussian-initialised weights.

§Errors

Propagates configuration / sub-component validation errors.

Source

pub fn eot_position(&self, tokens: &[usize]) -> usize

Locate the pooling position for a token sequence.

Following CLIP, the joint embedding is read at the position of the end-of-text token. We select the position of the last occurrence of eot_token; if it never appears, we fall back to CLIP’s argmax convention (the position of the highest token id), and as a final fallback the last index.

Source

pub fn hidden_states(&self, tokens: &[usize]) -> VisionResult<Vec<f32>>

Run the full encoder and return the contextual hidden states before pooling and projection: [n · width], after the final LayerNorm.

Exposed so causality tests can probe individual token hidden states.

§Errors
Source

pub fn encode(&self, tokens: &[usize]) -> VisionResult<Vec<f32>>

Encode a token sequence to a unit-norm joint-space embedding.

§Returns

[embed_dim] L2-normalised text embedding.

§Errors

Propagates errors from Self::hidden_states.

Source

pub fn encode_batch( &self, sequences: &[Vec<usize>], ) -> VisionResult<Vec<Vec<f32>>>

Encode a batch of (independent) token sequences.

§Returns

One [embed_dim] embedding per input sequence.

§Errors

Propagates the first encoding error.

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> 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, 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.