Skip to main content

ModelConfig

Struct ModelConfig 

Source
pub struct ModelConfig {
Show 34 fields pub vocab_size: usize, pub hidden_size: usize, pub intermediate_size: usize, pub num_layers: usize, pub num_heads: usize, pub num_kv_heads: usize, pub head_dim: usize, pub max_seq_len: usize, pub norm_eps: f32, pub rope_config: RopeConfig, pub use_parallel_residual: bool, pub hidden_act: ActivationType, pub attention_bias: bool, pub mlp_bias: bool, pub tie_word_embeddings: bool, pub num_experts: usize, pub num_experts_per_token: usize, pub expert_intermediate_size: usize, pub key_length: usize, pub value_length: usize, pub ssm_d_inner: usize, pub ssm_d_state: usize, pub ssm_n_group: usize, pub ssm_dt_rank: usize, pub ssm_conv_kernel: usize, pub attn_logit_softcap: f32, pub final_logit_softcap: f32, pub sliding_window: usize, pub has_combined_qkv: bool, pub uses_layer_norm: bool, pub uses_gelu: bool, pub has_ffn_gate: bool, pub attention_layer_configs: Option<Vec<AttentionLayerConfig>>, pub kv_source_layer: Option<Vec<usize>>,
}
Expand description

Full model configuration

Fields§

§vocab_size: usize

Vocabulary size

§hidden_size: usize

Hidden dimension (embedding size)

§intermediate_size: usize

Intermediate size (FFN dimension, typically 4 * hidden_size or computed)

§num_layers: usize

Number of transformer layers

§num_heads: usize

Number of attention heads

§num_kv_heads: usize

Number of key-value heads (for GQA/MQA)

§head_dim: usize

Dimension per head

§max_seq_len: usize

Maximum sequence length

§norm_eps: f32

RMS normalization epsilon

§rope_config: RopeConfig

RoPE configuration

§use_parallel_residual: bool

Whether to use parallel attention (compute QKV in parallel)

§hidden_act: ActivationType

Activation function type

§attention_bias: bool

Whether there’s a bias in attention projections

§mlp_bias: bool

Whether there’s a bias in MLP layers

§tie_word_embeddings: bool

Tie word embeddings with output projection

§num_experts: usize

Number of MoE experts (0 = dense model)

§num_experts_per_token: usize

Number of experts activated per token

§expert_intermediate_size: usize

Expert FFN intermediate dimension (may differ from dense intermediate_size)

§key_length: usize

Per-head key dimension (defaults to head_dim if not specified)

§value_length: usize

Per-head value dimension (defaults to head_dim if not specified)

§ssm_d_inner: usize

SSM/DeltaNet inner dimension (0 = no SSM layers)

§ssm_d_state: usize

SSM state dimension (per-head key dim for delta-net)

§ssm_n_group: usize

SSM group count (number of key heads in delta-net)

§ssm_dt_rank: usize

SSM time step rank (number of value heads in delta-net)

§ssm_conv_kernel: usize

SSM convolution kernel size

§attn_logit_softcap: f32

Attention logit soft-capping value (Gemma2: 50.0, 0.0 = disabled)

§final_logit_softcap: f32

Final logit soft-capping value (Gemma2: 30.0, 0.0 = disabled)

§sliding_window: usize

Sliding window attention size (0 = disabled)

§has_combined_qkv: bool

Whether this architecture uses combined QKV tensor

§uses_layer_norm: bool

Whether this architecture uses LayerNorm instead of RMSNorm

§uses_gelu: bool

Whether this architecture uses GELU activation

§has_ffn_gate: bool

Whether this architecture has a gate projection in FFN

§attention_layer_configs: Option<Vec<AttentionLayerConfig>>

Per-layer attention configs for architectures with heterogeneous attention (e.g., Gemma 4). None means all layers use uniform config.

§kv_source_layer: Option<Vec<usize>>

Maps layer index to physical KV cache slot. Identity by default. For KV shared layers, multiple indices map to the same slot.

Implementations§

Source§

impl ModelConfig

Source

pub fn has_ssm(&self) -> bool

Whether this model has SSM/delta-net recurrent layers

Source

pub fn is_moe(&self) -> bool

Check if this is an MoE model

Source

pub fn llama_7b() -> Self

Create config for LLaMA 7B

Source

pub fn llama2_7b() -> Self

Create config for LLaMA 2 7B

Source

pub fn llama3_8b() -> Self

Create config for LLaMA 3 8B

Source

pub fn uses_gqa(&self) -> bool

Check if this model uses Grouped Query Attention

Source

pub fn num_queries_per_kv(&self) -> usize

Get the number of query heads per KV head

Source

pub fn build_attention_layer_configs( num_layers: usize, pattern_period: usize, sliding_head_dim: usize, sliding_kv_heads: usize, sliding_rope_freq_base: f32, sliding_window: usize, global_head_dim: usize, global_kv_heads: usize, global_rope_freq_base: f32, global_rope_dims: usize, ) -> Vec<AttentionLayerConfig>

Build attention layer configs for a sliding/global pattern.

pattern_period layers form one cycle, where the last layer is Global and the rest are Sliding. E.g., period=6 gives 5 sliding + 1 global.

Source

pub fn build_attention_layer_configs_from_pattern( is_swa: &[bool], sliding_head_dim: usize, sliding_kv_heads: usize, sliding_rope_freq_base: f32, sliding_rope_dims: usize, sliding_window: usize, global_head_dim: usize, global_kv_heads: usize, global_rope_freq_base: f32, global_rope_dims: usize, ) -> Vec<AttentionLayerConfig>

Build attention layer configs from a per-layer boolean SWA pattern.

is_swa[i] is true if layer i uses sliding-window attention, false for global attention. This matches the sliding_window_pattern array stored in Gemma 4 GGUF files.

Source

pub fn build_kv_source_mapping( num_layers: usize, shared_layers: usize, layer_configs: &[AttentionLayerConfig], ) -> Vec<usize>

Build KV source layer mapping for shared KV cache.

The last shared_layers layers reuse cached K/V from earlier layers instead of projecting their own. The mapping is TYPE-SPECIFIC: shared SWA layers map to the last KV-owning SWA layer, shared global layers map to the last KV-owning global layer.

Requires layer_configs to determine each layer’s type.

Trait Implementations§

Source§

impl Clone for ModelConfig

Source§

fn clone(&self) -> ModelConfig

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 ModelConfig

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for ModelConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for ModelConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for ModelConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

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

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

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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