use alloc::collections::BTreeMap;
use std::path::{
Path,
PathBuf,
};
use serde::{
Deserialize,
Serialize,
};
use crate::{
data::catalog::presets::PresetSelection,
kits::support::model_config::GenerationPolicy,
};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct LmPolicy {
pub generation: GenerationPolicy,
}
pub const TOKENIZER: &str = "tokenizer";
pub const TOKENIZER_CONFIG: &str = "tokenizer_config";
pub const GENERATION_CONFIG: &str = "generation_config";
pub const CHAT_TEMPLATE: &str = "chat_template";
#[derive(Debug)]
pub struct LmConstruct<M> {
pub model: M,
pub max_positions: Option<usize>,
pub files: BTreeMap<String, PathBuf>,
pub policy: LmPolicy,
pub presets: PresetSelection,
}
impl<M> LmConstruct<M> {
pub fn new(model: M) -> Self {
Self {
model,
max_positions: None,
files: BTreeMap::new(),
policy: LmPolicy::default(),
presets: PresetSelection::default(),
}
}
pub fn with_policy(
self,
policy: LmPolicy,
presets: PresetSelection,
) -> Self {
Self {
policy,
presets,
..self
}
}
pub fn with_max_positions(
self,
max_positions: Option<usize>,
) -> Self {
Self {
max_positions,
..self
}
}
pub fn with_file(
mut self,
key: impl Into<String>,
path: impl Into<PathBuf>,
) -> Self {
self.files.insert(key.into(), path.into());
self
}
pub fn file(
&self,
key: &str,
) -> Option<&Path> {
self.files.get(key).map(PathBuf::as_path)
}
pub fn tokenizer(&self) -> Option<&Path> {
self.file(TOKENIZER)
}
pub fn tokenizer_config(&self) -> Option<&Path> {
self.file(TOKENIZER_CONFIG)
}
pub fn generation_config(&self) -> Option<&Path> {
self.file(GENERATION_CONFIG)
}
pub fn chat_template(&self) -> Option<&Path> {
self.file(CHAT_TEMPLATE)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
burner::module::ModuleInit,
kits::language::qwen3::{
Qwen3Config,
Qwen3Model,
},
support::testing::cpu_device,
};
#[test]
fn test_files_by_key() {
let model: Qwen3Model = Qwen3Config::new(40, 12, 1, 3, 1, 6, 20).init(&cpu_device());
let bundle = LmConstruct::new(model)
.with_max_positions(Some(512))
.with_file(TOKENIZER, "/x/tokenizer.json");
assert_eq!(bundle.max_positions, Some(512));
assert_eq!(bundle.tokenizer(), Some(Path::new("/x/tokenizer.json")));
assert_eq!(bundle.tokenizer_config(), None);
assert_eq!(bundle.generation_config(), None);
assert_eq!(bundle.chat_template(), None);
}
}