use alloc::vec::Vec;
use burn::{
store::burn_pack::Tensor as PackTensor,
tensor::FloatDType,
};
use super::{
CHECKPOINT,
QWEN3_KIT,
};
use crate::{
data::{
catalog::presets::ResolvedPresetSet,
formats::NamedTensorsApplied,
resources::LoadedResourceSet,
},
errors::BunsenResult,
kits::{
language::qwen3::{
GENERATION_CONFIG,
Qwen3Config,
Qwen3Model,
TOKENIZER_CONFIG,
weights::{
load_transformers_tensors_as,
qwen3_config_from_transformers,
},
},
support::{
construct::TransformersLmKit,
model_config::{
ProbeSpec,
probe_loaded,
},
},
},
};
#[derive(Clone, Debug, Default)]
pub struct Qwen3ConstructConfig;
impl TransformersLmKit for Qwen3ConstructConfig {
type Config = Qwen3Config;
type Model = Qwen3Model;
const CHECKPOINT: &'static str = CHECKPOINT;
const KIT: &'static str = QWEN3_KIT;
const NAME: &'static str = "Qwen3";
fn config_from_transformers(json: &str) -> BunsenResult<Qwen3Config> {
qwen3_config_from_transformers(json)
}
fn load_tensors(
model: &mut Qwen3Model,
tensors: Vec<PackTensor>,
dtype: FloatDType,
) -> BunsenResult<NamedTensorsApplied> {
load_transformers_tensors_as(model, tensors, dtype)
}
fn probed(loaded: &LoadedResourceSet) -> BunsenResult<ResolvedPresetSet> {
probe_loaded(
loaded,
&[
ProbeSpec::into_base("generation_config", GENERATION_CONFIG),
ProbeSpec::into_base("chat_template", TOKENIZER_CONFIG),
],
&ResolvedPresetSet::default(),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
data::builds::ConstructInit,
kits::language::qwen3::Qwen3Policy,
};
#[test]
fn test_the_hook_follows_the_kind() {
Qwen3ConstructConfig::for_resource_set(
&crate::data::catalog::BuiltinCatalog::get()
.unwrap()
.build("qwen/qwen3-0.6b")
.unwrap()
.resource_set(),
)
.unwrap();
Qwen3ConstructConfig::for_resource_set(
&crate::data::catalog::BuiltinCatalog::get()
.unwrap()
.build("qwen/qwen3-4b")
.unwrap()
.resource_set(),
)
.unwrap();
let mut gguf = crate::data::catalog::BuiltinCatalog::get()
.unwrap()
.build("qwen/qwen3-0.6b")
.unwrap()
.resource_set();
gguf.resources.get_mut(CHECKPOINT).unwrap().format = Some("gguf".to_string());
crate::errors::testing::ErrorMatcher::kind(crate::errors::BunsenErrorKind::Unsupported)
.message_contains("gguf")
.assert_err(&Qwen3ConstructConfig::for_resource_set(&gguf));
}
#[test]
fn test_the_generation_presets() {
use crate::kits::{
language::qwen3::pretrained::default_qwen3_factory,
support::model_config::SamplingPolicy,
};
let factory = default_qwen3_factory().unwrap();
let policy = |spec: &str| factory.resolve_presets(spec, &Qwen3Policy::default());
let thinking = policy("qwen/qwen3-0.6b").unwrap();
assert_eq!(thinking.presets, ["thinking"]);
let g = &thinking.config.generation;
assert_eq!(g.stop_ids, [151645, 151643]);
assert_eq!(g.template_kwargs["enable_thinking"], true);
assert_eq!(
g.sampling,
Some(SamplingPolicy {
temperature: 0.6,
top_k: Some(20),
top_p: Some(0.95),
min_p: None,
})
);
let fast = policy("qwen/qwen3-8b[no-thinking]").unwrap();
let g = &fast.config.generation;
assert_eq!(g.template_kwargs["enable_thinking"], false);
assert_eq!(
g.sampling,
Some(SamplingPolicy {
temperature: 0.7,
top_k: Some(20),
top_p: Some(0.8),
min_p: Some(0.0),
})
);
for row in [
"qwen/qwen3-0.6b-base",
"qwen/qwen3-4b-instruct-2507",
"qwen/qwen3guard-gen-4b",
] {
let r = policy(row).unwrap();
assert_eq!(r.presets, ["no-thinking"], "{row}");
assert!(!r.config.generation.stop_ids.is_empty(), "{row}");
assert!(r.config.generation.template_kwargs.is_empty(), "{row}");
crate::errors::testing::ErrorMatcher::kind(crate::errors::BunsenErrorKind::Policy)
.message_contains("fixed at `no-thinking`")
.assert_err(&policy(&format!("{row}[thinking]")));
}
}
}