pub mod lazy_llama;
pub mod lazy_qwen2;
pub mod llama;
#[allow(dead_code)]
mod quantized_llama;
pub mod qwen2;
pub use lazy_llama::{LazyLlama, LazyLoadError, LazyStats};
pub use lazy_qwen2::LazyQwen2;
pub use llama::{Llama, LlamaConfig};
pub use qwen2::{CacheType, Qwen2, Qwen2Config};
use candle_core::{Result as CandleResult, Tensor};
pub enum ModelKind {
Llama(Llama),
LazyLlama(LazyLlama),
Qwen2(Qwen2),
LazyQwen2(LazyQwen2),
}
impl ModelKind {
pub fn forward(&mut self, input_ids: &Tensor, start_pos: usize) -> CandleResult<Tensor> {
match self {
Self::Llama(model) => model.forward(input_ids, start_pos),
Self::LazyLlama(model) => model
.forward(input_ids, start_pos)
.map_err(|e| candle_core::Error::Msg(e.to_string())),
Self::Qwen2(model) => model.forward(input_ids, start_pos),
Self::LazyQwen2(model) => model
.forward(input_ids, start_pos)
.map_err(|e| candle_core::Error::Msg(e.to_string())),
}
}
pub fn clear_cache(&mut self) {
match self {
Self::Llama(model) => model.clear_cache(),
Self::LazyLlama(model) => model.clear_cache(),
Self::Qwen2(model) => model.clear_cache(),
Self::LazyQwen2(model) => model.clear_cache(),
}
}
pub fn forward_embedding(&mut self, input_ids: &Tensor) -> CandleResult<Tensor> {
match self {
Self::Llama(model) => model.forward_embedding(input_ids),
Self::LazyLlama(_model) => {
Err(candle_core::Error::Msg(
"Embedding extraction not supported for LazyLlama".to_string(),
))
},
Self::Qwen2(model) => model.forward_embedding(input_ids),
Self::LazyQwen2(_model) => {
Err(candle_core::Error::Msg(
"Embedding extraction not supported for LazyQwen2".to_string(),
))
},
}
}
pub fn extract_embeddings(&mut self, input_ids: &Tensor) -> CandleResult<Tensor> {
match self {
Self::Llama(model) => model.extract_embeddings(input_ids),
Self::LazyLlama(_model) => {
Err(candle_core::Error::Msg(
"Embedding extraction not supported for LazyLlama".to_string(),
))
},
Self::Qwen2(model) => model.extract_embeddings(input_ids),
Self::LazyQwen2(_model) => {
Err(candle_core::Error::Msg(
"Embedding extraction not supported for LazyQwen2".to_string(),
))
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArchitectureType {
Llama,
Qwen2,
Unknown,
}
impl ArchitectureType {
pub fn detect(model_type: Option<&str>, architectures: Option<&[String]>) -> Self {
if let Some(mt) = model_type {
let mt_lower = mt.to_lowercase();
if mt_lower.contains("llama") || mt_lower.contains("mistral") {
return Self::Llama;
}
if mt_lower.contains("qwen2") || mt_lower == "qwen2" {
return Self::Qwen2;
}
}
if let Some(archs) = architectures {
for arch in archs {
let arch_lower = arch.to_lowercase();
if arch_lower.contains("llama") || arch_lower.contains("mistral") {
return Self::Llama;
}
if arch_lower.contains("qwen2") {
return Self::Qwen2;
}
}
}
Self::Unknown
}
pub fn name(&self) -> &'static str {
match self {
Self::Llama => "Llama",
Self::Qwen2 => "Qwen2",
Self::Unknown => "Unknown",
}
}
}