Skip to main content

Qwen2Model

Struct Qwen2Model 

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

Complete Qwen2 model for inference.

Assembles embedding, decoder layers, and LM head into a complete model.

Implementations§

§

impl Qwen2Model

pub fn new(config: &Qwen2Config) -> Self

Create a new Qwen2 model from configuration.

Weights are initialized randomly. Use load() to load pre-trained weights.

pub fn new_uninitialized(config: &Qwen2Config) -> Self

Create an uninitialized Qwen2 model with minimal memory allocation.

The model is not ready for inference until weights are loaded.

pub fn config(&self) -> &Qwen2Config

Get model configuration.

pub fn eval(&mut self)

Set model to evaluation mode (no dropout).

pub fn train(&mut self)

Set model to training mode.

pub fn enable_cache(&mut self)

Enable KV cache for efficient generation.

pub fn disable_cache(&mut self)

Disable KV cache.

pub fn clear_cache(&mut self)

Clear KV cache.

pub fn num_layers(&self) -> usize

Get number of layers.

pub fn weight_names(&self) -> Vec<String>

Get list of weight names following HuggingFace convention.

Returns names like:

  • model.embed_tokens.weight
  • model.layers.0.self_attn.q_proj.weight
  • model.norm.weight
  • lm_head.weight

pub fn weight_info(&self) -> HashMap<String, Vec<usize>>

Get weight shapes as a map from name to shape.

pub fn weights(&self) -> HashMap<String, Vec<f32>>

Extract accessible weights as a map from name to f32 data.

Returns a map suitable for serialization to SafeTensors format. Note: Currently returns weights from components with public accessors. Full weight export will be enabled when nn modules expose weight accessors.

pub fn num_parameters(&self) -> usize

Get total number of parameters in the model.

pub fn embed_tokens_mut(&mut self) -> &mut Embedding

Get mutable reference to embedding layer.

pub fn layer_mut(&mut self, idx: usize) -> Option<&mut Qwen2DecoderLayer>

Get mutable reference to decoder layer at index.

pub fn norm_mut(&mut self) -> &mut RMSNorm

Get mutable reference to final norm layer.

pub fn lm_head_mut(&mut self) -> &mut Linear

Get mutable reference to language model head.

pub fn lm_head(&self) -> &Linear

Get reference to language model head (for testing/inspection).

pub fn load_from_safetensors(&mut self, path: &Path) -> Result<usize, String>

Load weights from SafeTensors format.

§Arguments
  • path - Path to .safetensors file
§Returns

Number of weights loaded

§Errors

Returns error if file cannot be read or weights don’t match.

pub fn from_safetensors( config: &Qwen2Config, path: &Path, ) -> Result<Self, String>

Load model from SafeTensors file.

Creates a new model with the given config and loads weights from file.

pub fn load_from_apr(&mut self, path: &Path) -> Result<usize, String>

Load weights from APR v2 format file.

Per Native Library Mandate (Spec §2.4): Uses mmap via bundle::MappedFile for zero-copy tensor access. This is the REQUIRED approach for APR files.

Note: APR canonical names don’t have the “model.” prefix (it’s stripped during import per format/converter.rs). We look for names without prefix.

§Returns

Number of weights loaded

§Errors

Returns error if file cannot be read or weights don’t match.

pub fn from_apr(config: &Qwen2Config, path: &Path) -> Result<Self, String>

Load model from APR v2 format file.

Creates a new model with the given config and loads weights from file.

Trait Implementations§

Source§

impl Debug for Qwen2Model

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> 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

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

Source§

fn vzip(self) -> V