use alloc::sync::Arc;
use super::Qwen3ConstructConfig;
use crate::{
data::builds::{
BuildFactory,
BuildProvider,
HfProvider,
},
errors::BunsenResult,
kits::language::qwen3::{
GENERATION_CONFIG,
TOKENIZER,
TOKENIZER_CONFIG,
},
};
pub const QWEN3_KIT: &str = "qwen3";
pub const CHECKPOINT: &str = "checkpoint";
pub const JSON: &str = "json";
pub const TOKENIZER_JSON: &str = "tokenizer.json";
pub const TOKENIZER_CONFIG_JSON: &str = "tokenizer_config.json";
pub const GENERATION_CONFIG_JSON: &str = "generation_config.json";
pub const QWEN3_CONTRACT: &str = include_str!("contract.yml");
pub const QWEN3_CATALOG_CONTRACT: &str = "qwen3-lm";
pub const DEFAULT_QWEN3_MODEL: &str = "qwen/qwen3/qwen3-0.6b";
pub fn default_qwen3_providers() -> Vec<Arc<dyn BuildProvider>> {
vec![
Arc::new(crate::data::catalog::BuiltinCatalog::expect_provider(
QWEN3_CATALOG_CONTRACT,
)),
Arc::new(
HfProvider::new()
.with_checkpoint_key(CHECKPOINT)
.with_extra_file(TOKENIZER, "tokenizer.json", TOKENIZER_JSON)
.with_extra_file(
TOKENIZER_CONFIG,
"tokenizer_config.json",
TOKENIZER_CONFIG_JSON,
)
.with_extra_file(
GENERATION_CONFIG,
"generation_config.json",
GENERATION_CONFIG_JSON,
),
),
]
}
pub fn default_qwen3_factory() -> BunsenResult<BuildFactory<Qwen3ConstructConfig>> {
BuildFactory::new().with_providers(default_qwen3_providers())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
data::{
builds::CONFIG,
pretrained::Prefabs,
},
kits::language::qwen3::{
Qwen3Config,
weights::qwen3_config_from_transformers,
},
};
#[test]
fn test_rows_are_pinned_and_match_their_prefabs() {
let factory = default_qwen3_factory().unwrap();
assert_eq!(factory.kit(), QWEN3_KIT);
assert_eq!(
factory
.providers()
.iter()
.map(|p| p.name())
.collect::<Vec<_>>(),
["catalog", "hf"]
);
let names = [
"qwen3-0.6b",
"qwen3-0.6b-base",
"qwen3-1.7b",
"qwen3-1.7b-base",
"qwen3-4b",
"qwen3-4b-base",
"qwen3-4b-instruct-2507",
"qwen3-4b-thinking-2507",
"qwen3-8b",
"qwen3-8b-base",
"qwen3-4b-saferl",
"qwen3guard-gen-0.6b",
"qwen3guard-gen-4b",
"qwen3guard-gen-8b",
];
assert_eq!(
factory.ids(),
names
.iter()
.map(|n| format!("catalog:qwen/qwen3/{n}"))
.collect::<Vec<_>>()
);
for name in names {
let (_, row) = factory.lookup(name).unwrap();
assert_eq!(row.name, format!("qwen/qwen3/{name}"));
let map = &row.resource_set();
map.validate().unwrap();
for resource in map.resources.values() {
assert!(resource.is_pinned(), "{name}: {}", resource.slot);
assert_eq!(resource.urls().len(), 1, "{name}: {}", resource.slot);
}
for key in [CONFIG, TOKENIZER, TOKENIZER_CONFIG, GENERATION_CONFIG] {
assert!(map.get(key).is_some(), "{name}: {key}");
}
assert!(
map.get(CHECKPOINT).is_some() || map.get("checkpoint.index").is_some(),
"{name}"
);
let path = format!(
"{}/testdata/qwen3/{name}.config.json",
env!("CARGO_MANIFEST_DIR")
);
assert_eq!(
Some(crate::data::cache::sha256_of(std::path::Path::new(&path)).unwrap()),
map.get(CONFIG).unwrap().sha256,
"{name}: the committed config.json is the pinned one"
);
let config =
qwen3_config_from_transformers(&std::fs::read_to_string(&path).unwrap()).unwrap();
let config = format!("{config:?}");
assert!(
Qwen3Config::PREFABS
.iter()
.any(|(_, prefab)| format!("{:?}", prefab()) == config),
"{name}: its config.json is no prefab's geometry"
);
}
assert_eq!(
factory.lookup(DEFAULT_QWEN3_MODEL).unwrap().1.name,
"qwen/qwen3/qwen3-0.6b"
);
}
#[test]
fn test_the_contract_is_the_loaders() {
use crate::{
data::{
builds::CONFIG,
resources::{
SAFETENSORS,
SAFETENSORS_INDEX,
},
},
kits::support::{
catalog_contracts::testing::contract_lines,
construct::{
GENERATION_CONFIG,
TOKENIZER,
TOKENIZER_CONFIG,
},
},
};
assert_eq!(
contract_lines(QWEN3_CONTRACT),
[
format!("qwen3-lm: {CHECKPOINT} [{SAFETENSORS}, {SAFETENSORS_INDEX}]"),
format!("qwen3-lm: {CONFIG} [{JSON}]"),
format!("qwen3-lm: {GENERATION_CONFIG} [{GENERATION_CONFIG_JSON}]"),
format!("qwen3-lm: {TOKENIZER} [{TOKENIZER_JSON}]"),
format!("qwen3-lm: {TOKENIZER_CONFIG} [{TOKENIZER_CONFIG_JSON}]"),
]
);
}
}