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: StringArchitecture identifier (llama, qwen2, qwen3, phi3, gemma2, gemma3, opt).
architectures: Vec<String>Hugging Face architecture class names.
vocab_size: usizeVocabulary size.
Transformer hidden dimension.
intermediate_size: usizeFeed-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.
Number of transformer decoder layers.
num_attention_heads: usizeNumber 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: usizeMaximum context length.
n_ctx: Option<usize>Legacy context length alias (n_ctx).
rms_norm_eps: f64RMSNorm / LayerNorm epsilon.
rope_theta: f64RoPE base frequency (rope_theta).
rope_local_base_freq: Option<f64>Local sliding-window RoPE base frequency for Gemma 3.
tie_word_embeddings: boolWhether lm_head shares weights with embed_tokens.
attention_bias: boolWhether Q/K/V/O attention projections carry biases.
mlp_bias: boolWhether 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 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.
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).
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
impl ModelConfig
Sourcepub fn from_file(path: &Path) -> Result<Self>
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.
Sourcepub fn resolve_effective(self) -> Self
pub fn resolve_effective(self) -> Self
Unwrap nested text_config when present (e.g., Gemma 3 / Gemma 4 multimodal checkpoints).
Sourcepub fn detect_architecture(&self) -> Result<ModelArchitecture>
pub fn detect_architecture(&self) -> Result<ModelArchitecture>
Detect the canonical ModelArchitecture family.
§Errors
Returns ModelError::UnsupportedArchitecture for unrecognized model_type.
Effective hidden activation identifier (hidden_activation when set, otherwise hidden_act).
Sourcepub fn effective_head_dim(&self) -> usize
pub fn effective_head_dim(&self) -> usize
Effective per-head dimension.
Sourcepub fn effective_global_head_dim(&self) -> usize
pub fn effective_global_head_dim(&self) -> usize
Effective global (full_attention) head dimension for Gemma 4.
Sourcepub fn effective_kv_heads(&self) -> usize
pub fn effective_kv_heads(&self) -> usize
Effective number of key/value heads.
Sourcepub fn effective_global_kv_heads(&self) -> usize
pub fn effective_global_kv_heads(&self) -> usize
Effective number of key/value heads for global (full_attention) layers.
Sourcepub fn effective_layer_types(&self) -> Vec<String>
pub fn effective_layer_types(&self) -> Vec<String>
Per-layer attention type schedule ("sliding_attention" vs. "full_attention").
Sourcepub fn gemma4_rope_params(&self, layer_type: &str) -> ResolvedRopeParams
pub fn gemma4_rope_params(&self, layer_type: &str) -> ResolvedRopeParams
Resolve Gemma 4 RoPE hyperparameters for a specific attention layer type.
Sourcepub fn effective_seqlen(&self) -> usize
pub fn effective_seqlen(&self) -> usize
Effective sequence length matching eval.py fallback (n_ctx -> max_position_embeddings -> 2048).
Sourcepub fn effective_intermediate_size(&self) -> usize
pub fn effective_intermediate_size(&self) -> usize
Effective intermediate size (ffn_dim for OPT or intermediate_size otherwise).
Sourcepub fn tiny_llama(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
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.
Sourcepub fn tiny_qwen2(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
pub fn tiny_qwen2(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
Build a compact synthetic Qwen2 config.
Sourcepub fn tiny_qwen3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
pub fn tiny_qwen3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
Build a compact synthetic Qwen3 config.
Sourcepub fn tiny_phi3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
pub fn tiny_phi3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
Build a compact synthetic Phi-3/Phi-4 config.
Sourcepub fn tiny_gemma2(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
pub fn tiny_gemma2(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
Build a compact synthetic Gemma 2 config.
Sourcepub fn tiny_gemma3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
pub fn tiny_gemma3(vocab_size: usize, hidden_size: usize, layers: usize) -> Self
Build a compact synthetic Gemma 3 config.
Sourcepub fn tiny_gemma4_dense(
vocab_size: usize,
hidden_size: usize,
layers: usize,
) -> Self
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).
Trait Implementations§
Source§impl Clone for ModelConfig
impl Clone for ModelConfig
Source§impl Debug for ModelConfig
impl Debug for ModelConfig
Source§impl Default for ModelConfig
impl Default for ModelConfig
Source§impl<'de> Deserialize<'de> for ModelConfigwhere
ModelConfig: Default,
impl<'de> Deserialize<'de> for ModelConfigwhere
ModelConfig: Default,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for ModelConfig
impl RefUnwindSafe for ModelConfig
impl Send for ModelConfig
impl Sync for ModelConfig
impl Unpin for ModelConfig
impl UnsafeUnpin for ModelConfig
impl UnwindSafe for ModelConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
impl<T, U> Imply<T> for U
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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