VisionEncoder

Struct VisionEncoder 

Source
pub struct VisionEncoder {
Show 15 fields pub patch_embed_w: DevicePtr, pub patch_embed_b: DevicePtr, pub pos_embed: DevicePtr, pub blocks: Vec<ViTBlock>, pub deepstack: Vec<MergerLayer>, pub deepstack_indexes: Vec<usize>, pub merger: MergerLayer, pub hidden_size: usize, pub num_heads: usize, pub head_dim: usize, pub spatial_merge_size: usize, pub out_hidden_size: usize, pub intermediate_size: usize, pub p_max: usize, pub num_grid_per_side: usize, /* private fields */
}

Fields§

§patch_embed_w: DevicePtr

2026-09-25: [hidden_size, PATCH_DIM] BF16.

§patch_embed_b: DevicePtr

2026-09-25: [hidden_size] BF16.

§pos_embed: DevicePtr

2026-09-25: [num_position_embeddings, hidden_size] BF16. The forward path reads it directly only when METRALE_VISION_POSINTERP=0; otherwise it uses the host copy pos_embed_host_f32.

§blocks: Vec<ViTBlock>§deepstack: Vec<MergerLayer>§deepstack_indexes: Vec<usize>

2026-09-25: deepstack[i] runs after the block whose 1-based number is deepstack_indexes[i] (forward_batched compares block_idx + 1).

§merger: MergerLayer

2026-09-25: The final merger, after the last block.

§hidden_size: usize§num_heads: usize§head_dim: usize§spatial_merge_size: usize§out_hidden_size: usize§intermediate_size: usize§p_max: usize

2026-09-25: Patch-row capacity of every scratch buffer (derive_max_patches).

§num_grid_per_side: usize

2026-09-25: sqrt(num_position_embeddings); new refuses a non-square count.

Implementations§

Source§

impl VisionEncoder

Source

pub fn forward( &self, pixels: &[f32], grid_h: usize, grid_w: usize, gpu: &dyn GpuBackend, stream: u64, ) -> Result<usize>

2026-09-25: Single-image forward through forward_batched. pixels is [P, PATCH_DIM] f32. Returns (1 + deepstack_indexes.len()) * merged_p.

Source

pub fn forward_batched( &self, images: &[(&[f32], usize, usize)], gpu: &dyn GpuBackend, stream: u64, ) -> Result<Vec<(usize, usize, usize)>>

2026-09-25: Batched forward over N images. Ops that do not depend on image geometry (patch_embed and every block’s GEMMs, norms, GELU and residuals) run once over M = Σpᵢ; host pos/rope prep, attention and the mergers loop per image.

buf_out layout, rows of out_hidden_size BF16:

  • 0 .. Σmerged_p: the final merger, packed in image order (the rows the LLM reads);
  • from (k+1)*Σmerged_p: DeepStack merger k, packed the same way.

When Σp <= p_max this path writes up to (1 + deepstack count) * Σmerged_p rows, which fits the p_max-row buf_out only while 1 + deepstack count <= spatial_merge_size²; nothing checks it. When Σp > p_max it encodes image by image without DeepStack rows.

Returns per-image (post_h, post_w, merged_p) in image order.

Source§

impl VisionEncoder

Source

pub fn new( patch_embed_w: DevicePtr, patch_embed_b: DevicePtr, pos_embed: DevicePtr, num_position_embeddings: usize, blocks: Vec<ViTBlock>, deepstack: Vec<MergerLayer>, deepstack_indexes: Vec<usize>, merger: MergerLayer, hidden_size: usize, num_heads: usize, spatial_merge_size: usize, out_hidden_size: usize, intermediate_size: usize, patch_size: usize, max_pixels: Option<usize>, gpu: &dyn GpuBackend, ) -> Result<Self>

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.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,