Skip to main content

ModelConfig

Struct ModelConfig 

Source
pub struct ModelConfig {
Show 46 fields pub model_type: String, pub architectures: Vec<String>, pub vocab_size: usize, pub hidden_size: usize, pub intermediate_size: usize, pub ffn_dim: Option<usize>, pub word_embed_proj_dim: Option<usize>, pub num_hidden_layers: usize, pub num_attention_heads: usize, pub num_key_value_heads: Option<usize>, pub head_dim: Option<usize>, pub max_position_embeddings: usize, pub n_ctx: Option<usize>, pub rms_norm_eps: f64, pub rope_theta: f64, pub rope_local_base_freq: Option<f64>, pub tie_word_embeddings: bool, pub attention_bias: bool, pub mlp_bias: bool, pub enable_bias: Option<bool>, pub do_layer_norm_before: Option<bool>, pub layer_norm_elementwise_affine: Option<bool>, pub hidden_act: String, pub attn_logit_softcapping: Option<f64>, pub final_logit_softcapping: Option<f64>, pub query_pre_attn_scalar: Option<f64>, pub sliding_window: Option<usize>, pub sliding_window_pattern: Option<usize>, pub layer_types: Option<Vec<String>>, pub global_head_dim: Option<usize>, pub num_global_key_value_heads: Option<usize>, pub attention_k_eq_v: Option<bool>, pub rope_parameters: Option<Value>, pub partial_rotary_factor: Option<f64>, pub hidden_activation: Option<String>, pub enable_moe_block: Option<bool>, pub num_experts: Option<usize>, pub top_k_experts: Option<usize>, pub moe_intermediate_size: Option<usize>, pub num_kv_shared_layers: Option<usize>, pub use_double_wide_mlp: Option<bool>, pub text_config: Option<Box<ModelConfig>>, pub bos_token_id: Option<TokenIdSpec>, pub eos_token_id: Option<TokenIdSpec>, pub pad_token_id: Option<TokenIdSpec>, pub extra: Map<String, Value>,
}
Expand description

Unified Hugging Face config.json model configuration.

Fields§

§model_type: String

Architecture identifier (llama, qwen2, qwen3, phi3, gemma2, gemma3, opt).

§architectures: Vec<String>

Hugging Face architecture class names.

§vocab_size: usize

Vocabulary size.

§hidden_size: usize

Transformer hidden dimension.

§intermediate_size: usize

Feed-forward intermediate dimension (ffn_dim for OPT).

§ffn_dim: Option<usize>

OPT alias for intermediate_size.

§word_embed_proj_dim: Option<usize>

OPT projection dimension between embeddings and decoder hidden states.

§num_hidden_layers: usize

Number of transformer decoder layers.

§num_attention_heads: usize

Number of query attention heads.

§num_key_value_heads: Option<usize>

Number of key/value attention heads (GQA when smaller than num_attention_heads).

§head_dim: Option<usize>

Explicit per-head dimension (defaults to hidden_size / num_attention_heads).

§max_position_embeddings: usize

Maximum context length.

§n_ctx: Option<usize>

Legacy context length alias (n_ctx).

§rms_norm_eps: f64

RMSNorm / LayerNorm epsilon.

§rope_theta: f64

RoPE base frequency (rope_theta).

§rope_local_base_freq: Option<f64>

Local sliding-window RoPE base frequency for Gemma 3.

§tie_word_embeddings: bool

Whether lm_head shares weights with embed_tokens.

§attention_bias: bool

Whether Q/K/V/O attention projections carry biases.

§mlp_bias: bool

Whether MLP projections carry biases.

§enable_bias: Option<bool>

OPT bias toggle (enable_bias).

§do_layer_norm_before: Option<bool>

OPT pre-norm vs. post-norm toggle (do_layer_norm_before).

§layer_norm_elementwise_affine: Option<bool>

OPT affine toggle on LayerNorm.

§hidden_act: String

Hidden activation function name (silu, gelu, gelu_pytorch_tanh, relu).

§attn_logit_softcapping: Option<f64>

Gemma 2/3 attention logit softcapping threshold.

§final_logit_softcapping: Option<f64>

Gemma 2/3 final logit softcapping threshold.

§query_pre_attn_scalar: Option<f64>

Gemma 2/3 attention scaling denominator (1 / sqrt(query_pre_attn_scalar)).

§sliding_window: Option<usize>

Sliding window size (Gemma 2/3, Phi).

§sliding_window_pattern: Option<usize>

Gemma 3 sliding window periodicity (every Nth layer is global).

§layer_types: Option<Vec<String>>

Per-layer attention type schedule ("sliding_attention" vs "full_attention"), used by Gemma 4.

§global_head_dim: Option<usize>

Global (full_attention) head dimension when larger than sliding head_dim (Gemma 4).

§num_global_key_value_heads: Option<usize>

Number of key/value heads for global (full_attention) layers when distinct from sliding layers (Gemma 4).

§attention_k_eq_v: Option<bool>

Whether global attention layers share keys and values (V = K, omitting v_proj), used by Gemma 4.

§rope_parameters: Option<Value>

Per-layer-type RoPE parameters (rope_type, rope_theta, partial_rotary_factor, factor) for Gemma 4.

§partial_rotary_factor: Option<f64>

Default fraction of head dims rotated when partial RoPE is enabled without nested rope_parameters.

§hidden_activation: Option<String>

Alternative activation key (hidden_activation alias for hidden_act).

§enable_moe_block: Option<bool>

Enable Mixture-of-Experts FFN block alongside the shared dense MLP (Gemma 4 MoE).

§num_experts: Option<usize>

Total number of routed experts in MoE layers (Gemma 4 MoE).

§top_k_experts: Option<usize>

Number of top-ranked experts activated per token (e.g., 8 in Gemma 4 MoE).

§moe_intermediate_size: Option<usize>

Intermediate dimension of each routed MoE expert MLP (Gemma 4 MoE).

§num_kv_shared_layers: Option<usize>

Number of trailing KV-shared edge layers (Gemma 4 E2B/E4B; 0 for Dense 31B / MoE 26B).

§use_double_wide_mlp: Option<bool>

Double-wide MLP flag for KV-shared layers (Gemma 4 edge variants).

§text_config: Option<Box<ModelConfig>>

Optional nested text_config used by Gemma 3 / Gemma 4 multimodal checkpoints.

§bos_token_id: Option<TokenIdSpec>

Beginning-of-sequence token ID.

§eos_token_id: Option<TokenIdSpec>

End-of-sequence token ID(s).

§pad_token_id: Option<TokenIdSpec>

Padding token ID.

§extra: Map<String, Value>

Preserved extra config.json keys for lossless round-trip serialization.

Implementations§

Source§

impl ModelConfig

Source

pub fn from_file(path: &Path) -> Result<Self>

Load and parse a Hugging Face config.json file.

§Errors

Returns an error if the file cannot be read or fails JSON deserialization.

Source

pub fn resolve_effective(self) -> Self

Unwrap nested text_config when present (e.g., Gemma 3 / Gemma 4 multimodal checkpoints).

Source

pub fn detect_architecture(&self) -> Result<ModelArchitecture>

Detect the canonical ModelArchitecture family.

§Errors

Returns ModelError::UnsupportedArchitecture for unrecognized model_type.

Source

pub fn effective_hidden_act(&self) -> &str

Effective hidden activation identifier (hidden_activation when set, otherwise hidden_act).

Source

pub fn effective_head_dim(&self) -> usize

Effective per-head dimension.

Source

pub fn effective_global_head_dim(&self) -> usize

Effective global (full_attention) head dimension for Gemma 4.

Source

pub fn effective_kv_heads(&self) -> usize

Effective number of key/value heads.

Source

pub fn effective_global_kv_heads(&self) -> usize

Effective number of key/value heads for global (full_attention) layers.

Source

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

Per-layer attention type schedule ("sliding_attention" vs. "full_attention").

Source

pub fn gemma4_rope_params(&self, layer_type: &str) -> ResolvedRopeParams

Resolve Gemma 4 RoPE hyperparameters for a specific attention layer type.

Source

pub fn effective_seqlen(&self) -> usize

Effective sequence length matching eval.py fallback (n_ctx -> max_position_embeddings -> 2048).

Source

pub fn effective_intermediate_size(&self) -> usize

Effective intermediate size (ffn_dim for OPT or intermediate_size otherwise).

Source

pub fn tiny_llama(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic Llama config suitable for unit tests and quick prototyping.

Source

pub fn tiny_qwen2(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic Qwen2 config.

Source

pub fn tiny_qwen3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic Qwen3 config.

Source

pub fn tiny_phi3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic Phi-3/Phi-4 config.

Source

pub fn tiny_gemma2(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic Gemma 2 config.

Source

pub fn tiny_gemma3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic Gemma 3 config.

Source

pub fn tiny_gemma4_dense( vocab_size: usize, hidden_size: usize, layers: usize, ) -> Self

Build a compact synthetic Dense Gemma 4 config (sliding_attention + full_attention, p-RoPE, k_eq_v).

Source

pub fn tiny_gemma4_moe( vocab_size: usize, hidden_size: usize, layers: usize, num_experts: usize, top_k: usize, ) -> Self

Build a compact synthetic MoE Gemma 4 config (dual shared MLP + top-k routed experts).

Source

pub fn tiny_opt(vocab_size: usize, hidden_size: usize, layers: usize) -> Self

Build a compact synthetic OPT config.

Trait Implementations§

Source§

impl Clone for ModelConfig

Source§

fn clone(&self) -> Self

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
§

impl<T> ByRef<T> for T

§

fn by_ref(&self) -> &T

§

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,

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

§

impl<T> DropFlavorWrapper<T> for T

§

type Flavor = MayDrop

The DropFlavor that [wrap]s T into Self
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

§

const WITNESS: W = W::MAKE

A constant of the type witness
§

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

§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
§

impl<T, U> Imply<T> for U
where T: ?Sized, U: ?Sized,

§

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

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

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
§

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
§

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

§

impl<E> ResultError for E
where E: Send + Debug + Sync,

§

impl<T> ResultType for T
where T: Send + Clone + Sync + Debug,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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