use super::{
EmbeddingRole, EmbeddingService, EmbeddingsWithReport, MAX_TEXT_BYTES, TokenCount,
ValidatedTextBatch,
};
use crate::error::{EmbedError, Result};
use crate::model::{EmbeddingModel, ModelConfig};
use async_trait::async_trait;
use lattice_model_bert::BertModel;
use lattice_model_embeddinggemma2::model::EmbeddingGemma2Model;
use lattice_model_qwen3_embedding::QwenModel;
use lattice_tokenizer::EmbeddingTokenCounts;
use sha2::{Digest, Sha256};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use tracing::{info, warn};
mod identity;
pub use identity::{EmbeddingBackend, EmbeddingSpaceIdentity, IdentifiedEmbeddings, IdentityMode};
enum LoadedModel {
Bert {
model: Arc<BertModel>,
identity: std::result::Result<EmbeddingSpaceIdentity, String>,
},
Qwen {
model: Arc<QwenModel>,
identity: std::result::Result<EmbeddingSpaceIdentity, String>,
},
EmbeddingGemma2 {
model: Arc<EmbeddingGemma2Model>,
output_dim: Option<usize>,
identity: std::result::Result<EmbeddingSpaceIdentity, String>,
},
}
impl LoadedModel {
fn encode_batch(&self, texts: &[&str]) -> std::result::Result<Vec<Vec<f32>>, String> {
match self {
LoadedModel::Bert { model, .. } => model.encode_batch(texts).map_err(|e| e.to_string()),
LoadedModel::Qwen { model: m, .. } => {
let mut results = Vec::with_capacity(texts.len());
for text in texts {
results.push(m.encode(text).map_err(|e| e.to_string())?);
}
Ok(results)
}
LoadedModel::EmbeddingGemma2 {
model, output_dim, ..
} => texts
.iter()
.map(|text| model.encode(text, *output_dim).map_err(|e| e.to_string()))
.collect(),
}
}
fn count_batch(&self, texts: &[&str]) -> std::result::Result<Vec<TokenCount>, String> {
match self {
LoadedModel::Bert { model, .. } => Ok(texts
.iter()
.map(|text| token_count(model.embedding_token_counts(text)))
.collect()),
LoadedModel::Qwen { model: m, .. } => Ok(texts
.iter()
.map(|text| token_count(m.embedding_token_counts(text)))
.collect()),
LoadedModel::EmbeddingGemma2 { model, .. } => texts
.iter()
.map(|text| {
model
.tokenize_with_counts(text)
.map(|(_, counts)| token_count(counts))
.map_err(|e| e.to_string())
})
.collect(),
}
}
fn encode_batch_with_report(
&self,
texts: &[&str],
) -> std::result::Result<(Vec<Vec<f32>>, Vec<TokenCount>), String> {
match self {
LoadedModel::Bert { .. } | LoadedModel::Qwen { .. } => {
let counts = self.count_batch(texts)?;
Ok((self.encode_batch(texts)?, counts))
}
LoadedModel::EmbeddingGemma2 {
model, output_dim, ..
} => {
let mut embeddings = Vec::with_capacity(texts.len());
let mut counts = Vec::with_capacity(texts.len());
for text in texts {
let (ids, text_counts) = model
.tokenize_with_counts(text)
.map_err(|e| e.to_string())?;
embeddings.push(
model
.encode_ids(&ids, *output_dim)
.map_err(|e| e.to_string())?,
);
counts.push(token_count(text_counts));
}
Ok((embeddings, counts))
}
}
}
fn cache_size(&self) -> usize {
match self {
LoadedModel::Qwen { model: m, .. } => m.cache_size(),
_ => 0,
}
}
}
fn token_count(counts: EmbeddingTokenCounts) -> TokenCount {
TokenCount::new(counts.before_truncation, counts.embedded)
}
fn prepared_views<'a>(
texts: &'a ValidatedTextBatch<'_>,
prefixed: Option<&'a [String]>,
) -> Vec<&'a str> {
match prefixed {
Some(prefixed) => prefixed.iter().map(String::as_str).collect(),
None => (0..texts.len()).map(|index| texts.get(index)).collect(),
}
}
pub struct NativeEmbeddingService {
model: Arc<OnceLock<std::result::Result<LoadedModel, String>>>,
model_config: ModelConfig,
identity_mode: IdentityMode,
initialization_started: AtomicBool,
model_directory: Option<std::path::PathBuf>,
}
impl Default for NativeEmbeddingService {
fn default() -> Self {
Self::new()
}
}
const LATTICE_EMBED_DIM: &str = "LATTICE_EMBED_DIM";
fn model_config_from_env(model: EmbeddingModel) -> Result<ModelConfig> {
let output_dim = match std::env::var(LATTICE_EMBED_DIM) {
Ok(raw) if raw.trim().is_empty() => None,
Ok(raw) => {
let dim = raw.trim().parse::<usize>().map_err(|e| {
EmbedError::InvalidInput(format!("invalid {LATTICE_EMBED_DIM}={raw:?}: {e}"))
})?;
Some(dim)
}
Err(std::env::VarError::NotPresent) => None,
Err(e) => {
return Err(EmbedError::InvalidInput(format!(
"invalid {LATTICE_EMBED_DIM}: {e}"
)));
}
};
ModelConfig::try_new(model, output_dim)
}
impl NativeEmbeddingService {
pub fn new() -> Self {
Self {
model: Arc::new(OnceLock::new()),
model_config: ModelConfig::new(EmbeddingModel::default()),
identity_mode: IdentityMode::Full,
initialization_started: AtomicBool::new(false),
model_directory: None,
}
}
pub fn with_model(model_type: EmbeddingModel) -> Self {
Self {
model: Arc::new(OnceLock::new()),
model_config: ModelConfig::new(model_type),
identity_mode: IdentityMode::Full,
initialization_started: AtomicBool::new(false),
model_directory: None,
}
}
pub fn with_model_config(model_config: ModelConfig) -> Result<Self> {
model_config.validate()?;
Ok(Self {
model: Arc::new(OnceLock::new()),
model_config,
identity_mode: IdentityMode::Full,
initialization_started: AtomicBool::new(false),
model_directory: None,
})
}
pub fn with_model_from_env(model_type: EmbeddingModel) -> Result<Self> {
let config = model_config_from_env(model_type)?;
Ok(Self {
model: Arc::new(OnceLock::new()),
model_config: config,
identity_mode: IdentityMode::Full,
initialization_started: AtomicBool::new(false),
model_directory: None,
})
}
pub fn save_cache(&self) -> Result<usize> {
let Some(Ok(model)) = self.model.get() else {
return Ok(0);
};
match model {
LoadedModel::Qwen { model: m, .. } => {
let model_name = self.model_config.model.to_string();
let config = m.effective_inference_config();
if !cache_revisions_known(&config.base_model_rev, &config.tokenizer_rev) {
warn!(
"persistent Qwen cache not saved: model or tokenizer revision is unknown"
);
return Ok(0);
}
let path =
embedding_cache_path(&model_name, m.dimensions(), &config.base_model_rev);
m.cache_save(&path)
.map_err(|e| EmbedError::InferenceFailed(e.to_string()))
}
_ => Ok(0),
}
}
pub fn cache_size(&self) -> usize {
self.model
.get()
.and_then(|r| r.as_ref().ok())
.map(LoadedModel::cache_size)
.unwrap_or(0)
}
pub async fn ensure_loaded(&self) -> Result<()> {
self.ensure_model().await.map(|_| ())
}
#[expect(
clippy::expect_used,
reason = "the OnceCell is initialized by the spawn_blocking call that precedes this read"
)]
async fn ensure_model(&self) -> Result<&LoadedModel> {
if let Some(result) = self.model.get() {
return result
.as_ref()
.map_err(|e| EmbedError::ModelInitialization(e.clone()));
}
self.initialization_started.store(true, Ordering::Release);
let model_lock = self.model.clone();
let model_config = self.model_config;
let identity_mode = self.identity_mode;
let model_directory = self.model_directory.clone();
tokio::task::spawn_blocking(move || {
model_lock.get_or_init(|| {
load_model_sync(model_config, identity_mode, model_directory.as_deref())
});
})
.await
.map_err(|e| EmbedError::ModelInitialization(e.to_string()))?;
self.model
.get()
.expect("set by spawn_blocking")
.as_ref()
.map_err(|e| EmbedError::ModelInitialization(e.clone()))
}
async fn encode_prepared(
&self,
texts: &[&str],
model: EmbeddingModel,
) -> Result<Vec<Vec<f32>>> {
if model != self.model_config.model {
return Err(EmbedError::InvalidInput(format!(
"requested model {:?} but this service is loaded with {:?}",
model, self.model_config.model
)));
}
super::validate_texts_bounded(
texts,
MAX_TEXT_BYTES.saturating_add(model.max_instruction_bytes()),
)?;
let loaded = self.ensure_model().await?;
loaded
.encode_batch(texts)
.map_err(EmbedError::InferenceFailed)
}
fn check_prepared(&self, texts: &[&str], model: EmbeddingModel) -> Result<()> {
if model != self.model_config.model {
return Err(EmbedError::InvalidInput(format!(
"requested model {:?} but this service is loaded with {:?}",
model, self.model_config.model
)));
}
super::validate_texts_bounded(
texts,
MAX_TEXT_BYTES.saturating_add(model.max_instruction_bytes()),
)
}
async fn count_prevalidated_with_role(
&self,
texts: ValidatedTextBatch<'_>,
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<Vec<TokenCount>> {
let prefixed = role
.instruction(model)
.map(|prefix| texts.to_owned_with_prefix(Some(prefix)));
let views = prepared_views(&texts, prefixed.as_deref());
self.check_prepared(&views, model)?;
let loaded = self.ensure_model().await?;
loaded
.count_batch(&views)
.map_err(EmbedError::InferenceFailed)
}
async fn report_prevalidated_with_role(
&self,
texts: ValidatedTextBatch<'_>,
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<EmbeddingsWithReport> {
let prefixed = role
.instruction(model)
.map(|prefix| texts.to_owned_with_prefix(Some(prefix)));
let views = prepared_views(&texts, prefixed.as_deref());
self.check_prepared(&views, model)?;
let loaded = self.ensure_model().await?;
let (embeddings, token_counts) = loaded
.encode_batch_with_report(&views)
.map_err(EmbedError::InferenceFailed)?;
Ok(EmbeddingsWithReport::new(embeddings, token_counts))
}
async fn encode_prevalidated_with_role(
&self,
texts: ValidatedTextBatch<'_>,
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<Vec<Vec<f32>>> {
let prefix = role.instruction(model);
if prefix.is_none()
&& let Some(borrowed) = texts.borrowed()
{
return self.encode_prepared(borrowed, model).await;
}
if prefix.is_none() {
let borrowed = (0..texts.len())
.map(|index| texts.get(index))
.collect::<Vec<_>>();
return self.encode_prepared(&borrowed, model).await;
}
let prepared = texts.to_owned_with_prefix(prefix);
let borrowed = prepared.iter().map(String::as_str).collect::<Vec<_>>();
self.encode_prepared(&borrowed, model).await
}
}
fn load_model_sync(
model_config: ModelConfig,
mode: IdentityMode,
directory: Option<&std::path::Path>,
) -> std::result::Result<LoadedModel, String> {
load_model_sync_with_bert_acquisition(
model_config,
mode,
directory,
|model_name| {
if identity::identity_family(model_config.model) == identity::IdentityFamily::BertSmall
{
let mode = identity::weight_mode(mode);
match directory {
Some(dir) => BertModel::from_directory_with_digest_mode(dir, mode, &mut |_| {}),
None => BertModel::from_pretrained_with_digest_mode(model_name, mode),
}
.map_err(|e| e.to_string())
} else if identity::identity_family(model_config.model)
== identity::IdentityFamily::BertFull
{
match (mode, directory) {
(IdentityMode::Full, Some(dir)) => BertModel::from_directory_with_digest_mode(
dir,
identity::weight_mode(mode),
&mut |_| {},
),
(IdentityMode::Full, None) => BertModel::from_pretrained_with_digest_mode(
model_name,
identity::weight_mode(mode),
),
(IdentityMode::Header, Some(dir)) => BertModel::from_directory(dir),
(IdentityMode::Header, None) => BertModel::from_pretrained(model_name),
}
.map_err(|e| e.to_string())
} else {
BertModel::from_pretrained(model_name).map_err(|e| e.to_string())
}
},
identity::identify,
)
}
fn load_model_sync_with_bert_acquisition(
model_config: ModelConfig,
mode: IdentityMode,
directory: Option<&std::path::Path>,
acquire_bert: impl FnOnce(&str) -> std::result::Result<BertModel, String>,
identify: impl FnOnce(
&BertModel,
IdentityMode,
) -> std::result::Result<EmbeddingSpaceIdentity, String>,
) -> std::result::Result<LoadedModel, String> {
match model_config.model {
EmbeddingModel::BgeSmallEnV15
| EmbeddingModel::BgeBaseEnV15
| EmbeddingModel::BgeLargeEnV15
| EmbeddingModel::MultilingualE5Small
| EmbeddingModel::MultilingualE5Base
| EmbeddingModel::AllMiniLmL6V2
| EmbeddingModel::ParaphraseMultilingualMiniLmL12V2 => {
let model_name = match model_config.model {
EmbeddingModel::BgeSmallEnV15 => "bge-small-en-v1.5",
EmbeddingModel::BgeBaseEnV15 => "bge-base-en-v1.5",
EmbeddingModel::BgeLargeEnV15 => "bge-large-en-v1.5",
EmbeddingModel::MultilingualE5Small => "multilingual-e5-small",
EmbeddingModel::MultilingualE5Base => "multilingual-e5-base",
EmbeddingModel::AllMiniLmL6V2 => "all-minilm-l6-v2",
EmbeddingModel::ParaphraseMultilingualMiniLmL12V2 => {
"paraphrase-multilingual-minilm-l12-v2"
}
_ => unreachable!(),
};
info!(model = model_name, "loading native BERT embedding model");
let mut bert = acquire_bert(model_name)?;
if let Some(pooling) = model_config.model.bert_pooling() {
bert.set_pooling(pooling);
}
let identity = match model_config.model {
EmbeddingModel::BgeSmallEnV15 => identify(&bert, mode),
EmbeddingModel::BgeBaseEnV15 | EmbeddingModel::BgeLargeEnV15 => {
identity::identify_bge_variant(&bert, mode, model_config.model)
}
EmbeddingModel::MultilingualE5Small | EmbeddingModel::MultilingualE5Base => {
identity::identify_e5(&bert, mode, model_config.model)
}
EmbeddingModel::AllMiniLmL6V2
| EmbeddingModel::ParaphraseMultilingualMiniLmL12V2 => {
identity::identify_minilm(&bert, mode, model_config.model)
}
_ => Ok(EmbeddingSpaceIdentity::Unavailable),
};
Ok(LoadedModel::Bert {
model: Arc::new(bert),
identity,
})
}
EmbeddingModel::Qwen3Embedding0_6B | EmbeddingModel::Qwen3Embedding4B => {
load_qwen_model(model_config, mode, directory)
}
EmbeddingModel::EmbeddingGemma2 => {
load_embeddinggemma2_model(model_config, mode, directory)
}
other => Err(format!("unsupported model: {other:?}")),
}
}
fn load_qwen_model(
model_config: ModelConfig,
mode: IdentityMode,
directory: Option<&std::path::Path>,
) -> std::result::Result<LoadedModel, String> {
model_config.validate().map_err(|e| e.to_string())?;
let model_type = model_config.model;
let model_name = model_type.to_string();
info!(
model = %model_name,
output_dim = ?model_config.output_dim,
"loading Qwen embedding model"
);
let (model_dir, env_override) = match directory {
Some(directory) => (directory.to_path_buf(), false),
None => qwen_model_dir(model_type).map_err(|e| e.to_string())?,
};
let mut model = if mode == IdentityMode::Full {
QwenModel::from_directory_with_full_report(&model_dir)
} else {
QwenModel::from_directory(&model_dir)
}
.map_err(|e| e.to_string())?;
model.set_output_dim(model_config.output_dim);
let config = model.effective_inference_config();
if cache_revisions_known(&config.base_model_rev, &config.tokenizer_rev) {
let cache_path =
embedding_cache_path(&model_name, model.dimensions(), &config.base_model_rev);
let managed = allow_legacy_cache(directory, env_override);
let legacy = managed.then(|| legacy_embedding_cache_path(&model_name, model.dimensions()));
match restore_embedding_cache(&cache_path, legacy.as_deref(), |path| {
model.cache_load(path)
}) {
Ok((n, source)) => {
if n > 0 {
info!(entries = n, path = %source.display(), "loaded embedding cache");
}
if source != cache_path
&& let Err(e) = model.cache_save(&cache_path)
{
warn!(path = %cache_path.display(), error = %e, "embedding cache migration could not be saved");
}
}
Err((path, e)) => {
warn!(path = %path.display(), error = %e, "embedding cache failed integrity check, ignoring (will regenerate on next save)");
}
}
} else {
warn!("persistent Qwen cache not restored: model or tokenizer revision is unknown");
}
let identity = identity::identify_qwen(&model, model_type, mode);
Ok(LoadedModel::Qwen {
model: Arc::new(model),
identity,
})
}
fn load_embeddinggemma2_model(
model_config: ModelConfig,
mode: IdentityMode,
directory: Option<&std::path::Path>,
) -> std::result::Result<LoadedModel, String> {
model_config.validate().map_err(|e| e.to_string())?;
info!(
output_dim = ?model_config.output_dim,
"loading EmbeddingGemma 2 embedding model"
);
let model_dir = match directory {
Some(directory) => directory.to_path_buf(),
None => embeddinggemma2_model_dir().map_err(|e| e.to_string())?,
};
let model = if mode == IdentityMode::Full {
EmbeddingGemma2Model::from_model_dir_with_full_report(&model_dir, &mut |_| {})
} else {
EmbeddingGemma2Model::from_model_dir(&model_dir)
}
.map_err(|e| e.to_string())?;
let identity = identity::identify_gemma(&model, model_config.output_dim, mode);
Ok(LoadedModel::EmbeddingGemma2 {
model: Arc::new(model),
output_dim: model_config.output_dim,
identity,
})
}
fn embeddinggemma2_model_dir() -> Result<std::path::PathBuf> {
if let Ok(dir) = std::env::var("LATTICE_EMBEDDINGGEMMA2_MODEL_DIR") {
return Ok(std::path::PathBuf::from(dir));
}
let home = std::env::var("HOME")
.map_err(|_| EmbedError::ModelInitialization("HOME not set".into()))?;
let dir = std::path::PathBuf::from(home)
.join(".lattice")
.join("models")
.join("embeddinggemma-2");
if dir.join("model.safetensors").exists() {
Ok(dir)
} else {
Err(EmbedError::ModelInitialization(format!(
"EmbeddingGemma 2 model not found at {dir}. Download it with:\n huggingface-cli download {repo} --local-dir {dir}",
dir = dir.display(),
repo = EmbeddingModel::EmbeddingGemma2.model_id()
)))
}
}
fn allow_legacy_cache(directory: Option<&std::path::Path>, env_override: bool) -> bool {
directory.is_none() && !env_override
}
fn cache_revisions_known(base: &str, tokenizer: &str) -> bool {
[base, tokenizer]
.iter()
.all(|revision| !revision.is_empty() && *revision != "none")
}
fn legacy_embedding_cache_path(model: &str, dim: usize) -> std::path::PathBuf {
let home = std::env::var("HOME").unwrap_or_else(|_| "/tmp".into());
std::path::PathBuf::from(home)
.join(".lattice")
.join("cache")
.join(format!("embed_{model}_{dim}d.bin"))
}
fn embedding_cache_path(model: &str, dim: usize, base_revision: &str) -> std::path::PathBuf {
let digest = Sha256::digest(base_revision.as_bytes());
legacy_embedding_cache_path(model, dim)
.with_file_name(format!("embed_{model}_{dim}d_{digest:x}.bin"))
}
fn restore_embedding_cache<F>(
path: &std::path::Path,
legacy: Option<&std::path::Path>,
mut load: F,
) -> std::result::Result<
(usize, std::path::PathBuf),
(std::path::PathBuf, lattice_inference_core::InferenceError),
>
where
F: FnMut(
&std::path::Path,
) -> std::result::Result<usize, lattice_inference_core::InferenceError>,
{
let count = load(path).map_err(|error| (path.to_path_buf(), error))?;
if count == 0
&& matches!(path.try_exists(), Ok(false))
&& let Some(legacy) = legacy
&& matches!(legacy.try_exists(), Ok(true))
{
return load(legacy)
.map(|count| (count, legacy.to_path_buf()))
.map_err(|error| (legacy.to_path_buf(), error));
}
Ok((count, path.to_path_buf()))
}
fn qwen_model_dir(model_type: EmbeddingModel) -> Result<(std::path::PathBuf, bool)> {
if let Ok(dir) = std::env::var("LATTICE_QWEN_MODEL_DIR") {
return Ok((std::path::PathBuf::from(dir), true));
}
let slug = match model_type {
EmbeddingModel::Qwen3Embedding0_6B => "qwen3-embedding-0.6b",
EmbeddingModel::Qwen3Embedding4B => "qwen3-embedding-4b",
other => {
return Err(EmbedError::ModelInitialization(format!(
"not a Qwen model: {other}"
)));
}
};
let home = std::env::var("HOME")
.map_err(|_| EmbedError::ModelInitialization("HOME not set".into()))?;
let dir = std::path::PathBuf::from(home)
.join(".lattice")
.join("models")
.join(slug);
if dir.join("model.safetensors").exists() || dir.join("model.safetensors.index.json").exists() {
Ok((dir, false))
} else {
Err(EmbedError::ModelInitialization(format!(
"Qwen3 model not found at {dir}. Download it with:\n huggingface-cli download {repo} --local-dir {dir}",
dir = dir.display(),
repo = model_type.model_id()
)))
}
}
#[async_trait]
impl EmbeddingService for NativeEmbeddingService {
async fn embed(&self, texts: &[String], model: EmbeddingModel) -> Result<Vec<Vec<f32>>> {
let texts = ValidatedTextBatch::new(texts)?;
self.encode_prevalidated_with_role(texts, model, EmbeddingRole::Generic)
.await
}
async fn embed_with_role(
&self,
texts: &[String],
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<Vec<Vec<f32>>> {
let texts = ValidatedTextBatch::new(texts)?;
self.encode_prevalidated_with_role(texts, model, role).await
}
async fn embed_with_role_prevalidated(
&self,
texts: ValidatedTextBatch<'_>,
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<Vec<Vec<f32>>> {
self.encode_prevalidated_with_role(texts, model, role).await
}
async fn count_tokens(
&self,
texts: &[String],
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<Vec<TokenCount>> {
let texts = ValidatedTextBatch::new(texts)?;
self.count_prevalidated_with_role(texts, model, role).await
}
async fn embed_with_report(
&self,
texts: &[String],
model: EmbeddingModel,
role: EmbeddingRole,
) -> Result<EmbeddingsWithReport> {
let texts = ValidatedTextBatch::new(texts)?;
self.report_prevalidated_with_role(texts, model, role).await
}
fn model_config(&self, model: EmbeddingModel) -> ModelConfig {
if model == self.model_config.model {
self.model_config
} else {
ModelConfig::new(model)
}
}
fn supports_model(&self, model: EmbeddingModel) -> bool {
model == self.model_config.model
}
fn name(&self) -> &'static str {
"native-bert"
}
}
#[cfg(test)]
#[path = "native_pooling_tests.rs"]
mod pooling_tests;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_path_contains_dim_in_filename() {
let path = embedding_cache_path("qwen3-embedding-4b", 1024, "revision-4b");
let filename = path.file_name().unwrap().to_str().unwrap();
assert!(filename.starts_with("embed_qwen3-embedding-4b_1024d_"));
assert!(filename.ends_with(".bin"));
let odd_revision = embedding_cache_path("qwen3-embedding-4b", 1024, "../odd/revision");
assert_eq!(odd_revision.parent(), path.parent());
}
#[test]
fn test_cache_path_different_dims_produce_different_paths() {
let path_1024 = embedding_cache_path("qwen3-embedding-4b", 1024, "revision-4b");
let path_2560 = embedding_cache_path("qwen3-embedding-4b", 2560, "revision-4b");
assert_ne!(path_1024, path_2560);
assert!(path_1024.to_string_lossy().contains("1024d"));
assert!(path_2560.to_string_lossy().contains("2560d"));
}
#[test]
fn test_cache_path_model_slug_differentiates_variants() {
let path_4b = embedding_cache_path("qwen3-embedding-4b", 2560, "revision-4b");
let path_06b = embedding_cache_path("qwen3-embedding-0.6b", 1024, "revision-06b");
assert_ne!(path_4b, path_06b);
assert!(path_4b.to_string_lossy().contains("qwen3-embedding-4b"));
assert!(path_06b.to_string_lossy().contains("qwen3-embedding-0.6b"));
}
#[test]
fn test_cache_path_same_model_same_dim_same_path() {
let p1 = embedding_cache_path("qwen3-embedding-4b", 1024, "revision-4b");
let p2 = embedding_cache_path("qwen3-embedding-4b", 1024, "revision-4b");
assert_eq!(p1, p2);
}
#[test]
fn persistent_cache_requires_known_revisions_and_managed_legacy() {
assert!(cache_revisions_known("base", "tokenizer"));
for (base, tokenizer) in [
("none", "tokenizer"),
("", "tokenizer"),
("base", "none"),
("base", ""),
] {
assert!(!cache_revisions_known(base, tokenizer));
}
assert!(allow_legacy_cache(None, false));
assert!(!allow_legacy_cache(None, true));
assert!(!allow_legacy_cache(
Some(std::path::Path::new("/explicit")),
false
));
}
#[test]
fn legacy_cache_fallback_requires_absent_new_file_and_propagates_refusals() {
let dir = tempfile::tempdir().unwrap();
let new_path = dir.path().join("new.bin");
let legacy = dir.path().join("old.bin");
std::fs::write(&legacy, b"fixture").unwrap();
let mut calls = Vec::new();
let (count, source) = restore_embedding_cache(&new_path, Some(&legacy), |path| {
calls.push(path.to_path_buf());
Ok(if path == legacy.as_path() { 3 } else { 0 })
})
.unwrap();
assert_eq!(count, 3);
assert_eq!(source, legacy);
assert_eq!(calls, [new_path.clone(), legacy.clone()]);
std::fs::write(&new_path, b"fixture").unwrap();
calls.clear();
let (count, source) = restore_embedding_cache(&new_path, Some(&legacy), |path| {
calls.push(path.to_path_buf());
Ok(0)
})
.unwrap();
assert_eq!(count, 0);
assert_eq!(source, new_path);
assert_eq!(calls, std::slice::from_ref(&new_path));
calls.clear();
let (failed_path, _) = restore_embedding_cache(&new_path, Some(&legacy), |path| {
calls.push(path.to_path_buf());
Err(lattice_inference_core::InferenceError::Inference(
"refused".into(),
))
})
.unwrap_err();
assert_eq!(failed_path, new_path);
assert_eq!(calls, [new_path]);
}
}