pub const MODEL_TYPE: &str = "qwen4_exp";
pub const TEXT_MODEL_TYPE: &str = "qwen4_exp_text";
pub const ARCHITECTURE: &str = "Qwen4ExpForConditionalGeneration";
pub const UNSUPPORTED_OP_NAMES: [&str; 6] = [
"sparse-attention indexer",
"ngram embedding",
"ple",
"hc residual",
"sigmoid output gate",
"mtp",
];
#[must_use]
pub fn is_qwen4_exp(model_type: Option<&str>, architectures: &[String]) -> bool {
matches!(model_type, Some(MODEL_TYPE | TEXT_MODEL_TYPE))
|| architectures.iter().any(|a| a == ARCHITECTURE)
}
#[must_use]
pub fn refusal_reason(op_names: &[&str]) -> String {
format!(
"architecture '{MODEL_TYPE}' is recognised but this build has no forward for its new \
ops: [{}]. This is a refusal, not a fallback: no tensor data was read (headers may \
have been parsed) and nothing was generated (paiml/aprender#7169 S2/S3).",
op_names.join(", ")
)
}
#[must_use]
pub fn refusal_for(model_type: Option<&str>, architectures: &[String]) -> Option<String> {
is_qwen4_exp(model_type, architectures).then(|| refusal_reason(&UNSUPPORTED_OP_NAMES))
}
#[must_use]
pub fn refusal_for_json(raw: &serde_json::Value) -> Option<String> {
let mt = |v: &serde_json::Value| {
v.get("model_type")
.and_then(|m| m.as_str())
.map(String::from)
};
let archs: Vec<String> = raw
.get("architectures")
.and_then(|a| a.as_array())
.map(|a| {
a.iter()
.filter_map(|s| s.as_str().map(String::from))
.collect()
})
.unwrap_or_default();
refusal_for(mt(raw).as_deref(), &archs).or_else(|| {
raw.get("text_config")
.and_then(|t| refusal_for(mt(t).as_deref(), &[]))
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn json_refusal_matches_by_type_text_config_and_arch() {
for j in [
r#"{"model_type":"qwen4_exp"}"#,
r#"{"model_type":"qwen4_exp_text"}"#,
r#"{"architectures":["Qwen4ExpForConditionalGeneration"]}"#,
r#"{"model_type":"x","text_config":{"model_type":"qwen4_exp_text"}}"#,
] {
let v: serde_json::Value = serde_json::from_str(j).expect("json");
let r = refusal_for_json(&v).unwrap_or_else(|| panic!("no refusal: {j}"));
assert!(r.contains("refusal, not a fallback") && r.contains("mtp"));
}
let v: serde_json::Value = serde_json::from_str(r#"{"model_type":"qwen3"}"#).expect("json");
assert!(refusal_for_json(&v).is_none());
}
}