use model_io::{ArchConfig, ModelFamily, ResidentIndex};
use crate::families::museglimmer::layer_tensor;
use crate::real_forward::RealForwardError;
use crate::real_forward_utils::entry;
pub(crate) const RMS_EPS: f32 = 1e-5;
pub(crate) const POST_NORM_EPS: f32 = 1e-8;
pub(crate) const QK_SCALE_FACTOR: f32 = 3.87;
pub(crate) const OUTPUT_MULTIPLIER: f32 = 0.196_116_13;
pub(crate) struct RealMuseState {
pub(crate) rotated_pairs: u32,
pub(crate) attn_gate: gpu::MetalBuffer,
}
impl RealMuseState {
pub(crate) fn build(
context: &mut gpu::MetalContext,
index: &ResidentIndex,
arch: &ArchConfig,
) -> Result<Self, RealForwardError> {
let unsupported = |detail: String| Err(RealForwardError::Unsupported(detail));
if arch.family != ModelFamily::MuseGlimmer {
return unsupported(format!(
"the muse_glimmer flow refuses family {:?}; flow selection keys on the family, \
never on tensor naming",
arch.family
));
}
if !arch.ffn_sandwich_norms {
return unsupported(
"muse_glimmer normalizes both the attention and the FFN output before adding \
them back; an install declaring ffnSandwichNorms false was built from \
something else"
.to_string(),
);
}
if arch.router_scaled
|| arch.embedding_scaled_by_sqrt_hidden
|| arch.attn_output_gate
|| arch.shared_expert_gated
|| arch.rope_neox_subdim
|| arch.attention_k_eq_v
{
return unsupported(
"the muse_glimmer flow takes none of routerScaled / \
embeddingScaledBySqrtHidden / attnOutputGate / sharedExpertGated / \
ropeNeoxSubdim / attentionKEqV. Note attnOutputGate is FALSE even though this \
family HAS an output gate: that field means `q_proj` emits packed [query; \
gate] rows, and this family's gate is its own tensor"
.to_string(),
);
}
if arch.final_logit_softcap <= 0.0 {
return unsupported(format!(
"muse_glimmer softcaps its logits (`final_logit_softcapping`, 20.0 on the \
published checkpoint); got {}",
arch.final_logit_softcap
));
}
if arch.tie_word_embeddings {
return unsupported(
"muse_glimmer ships a separate `lm_head`; an install claiming tied embeddings \
was built from something else"
.to_string(),
);
}
if arch.num_experts != 0 || arch.top_k_experts != 0 {
return unsupported(format!(
"muse_glimmer is dense; got num_experts {} and top_k_experts {}",
arch.num_experts, arch.top_k_experts
));
}
if arch.full_attention_layer_mask.iter().any(|&m| m > 1) {
return unsupported(
"muse_glimmer layers are sliding-window (0) or full (1); this architecture has \
no linear or compressed layer kinds"
.to_string(),
);
}
if !arch.full_attention_layer_mask.contains(&0) {
return unsupported(
"muse_glimmer alternates three sliding layers to one full; a mask with no \
sliding layer would size every KV buffer at max_context and decode a \
different model"
.to_string(),
);
}
if arch.sliding_window <= 0 {
return unsupported(format!(
"muse_glimmer alternates a sliding window; {} is not a usable one",
arch.sliding_window
));
}
if arch.full_rope_theta != 0.0 {
return unsupported(format!(
"muse_glimmer's full-attention layers are NoPE and this install declares \
fullRopeTheta {}; rotating them is a different model that still reads \
fluently",
arch.full_rope_theta
));
}
if arch.rope_theta <= 0.0 {
return unsupported(format!(
"muse_glimmer rotates its SLIDING layers; ropeTheta {} is not a usable base",
arch.rope_theta
));
}
let head_dim = arch.full_head_dim;
if arch.head_dim != head_dim {
return unsupported(format!(
"muse_glimmer uses one head dim for both layer kinds; got {} sliding and \
{head_dim} full",
arch.head_dim
));
}
let rotated_pairs = (head_dim as f64 * arch.partial_rotary_factor / 2.0).round() as i64;
if rotated_pairs <= 0 || rotated_pairs > head_dim / 2 {
return unsupported(format!(
"rotated pairs {rotated_pairs} (full_head_dim {head_dim} x \
partial_rotary_factor {}) must be positive and at most half the head",
arch.partial_rotary_factor
));
}
for layer in 0..arch.num_layers as usize {
for suffix in [
"input_layernorm.weight",
"post_attention_layernorm.weight",
"pre_feedforward_layernorm.weight",
"post_feedforward_layernorm.weight",
"self_attn.q_proj.weight",
"self_attn.k_proj.weight",
"self_attn.v_proj.weight",
"self_attn.gate_proj.weight",
"self_attn.o_proj.weight",
"mlp.gate_proj.weight",
"mlp.up_proj.weight",
"mlp.down_proj.weight",
] {
entry(index, &layer_tensor(layer, suffix))?;
}
for suffix in ["self_attn.q_norm.weight", "self_attn.k_norm.weight"] {
if index.entries.contains_key(&layer_tensor(layer, suffix)) {
return unsupported(format!(
"layer {layer} carries {suffix}, but muse_glimmer's q/k norms are \
no-scale and store no weights; this install is not this family"
));
}
}
}
entry(index, "language_model.lm_head.weight")?;
entry(index, "language_model.model.norm.weight")?;
entry(index, "language_model.model.embed_tokens.weight")?;
let q_dim = (arch.num_heads * head_dim) as usize;
Ok(Self {
rotated_pairs: rotated_pairs as u32,
attn_gate: context.new_output_buffer((q_dim.max(1) * 2) as u64),
})
}
}