use crate::get_cache_dir;
use ort::{environment::Environment, ep::ExecutionProviderDispatch};
use std::{fmt, path::PathBuf};
pub(crate) struct ProviderNames<'a>(pub(crate) &'a [ExecutionProviderDispatch]);
impl fmt::Debug for ProviderNames<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.0.iter().map(|ep| ep.name()))
.finish()
}
}
pub trait HasMaxLength {
const MAX_LENGTH: usize;
}
#[derive(Clone)]
#[non_exhaustive]
pub struct InitOptionsWithLength<M> {
pub model_name: M,
pub environment: Option<Environment>,
pub execution_providers: Vec<ExecutionProviderDispatch>,
pub cache_dir: PathBuf,
pub show_download_progress: bool,
pub max_length: usize,
pub intra_threads: Option<usize>,
pub session_config: Vec<(String, String)>,
}
#[derive(Clone)]
#[non_exhaustive]
pub struct InitOptions<M> {
pub model_name: M,
pub environment: Option<Environment>,
pub execution_providers: Vec<ExecutionProviderDispatch>,
pub cache_dir: PathBuf,
pub show_download_progress: bool,
pub intra_threads: Option<usize>,
pub session_config: Vec<(String, String)>,
}
impl<M: fmt::Debug> fmt::Debug for InitOptionsWithLength<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InitOptionsWithLength")
.field("model_name", &self.model_name)
.field("environment", &self.environment)
.field(
"execution_providers",
&ProviderNames(&self.execution_providers),
)
.field("cache_dir", &self.cache_dir)
.field("show_download_progress", &self.show_download_progress)
.field("max_length", &self.max_length)
.field("intra_threads", &self.intra_threads)
.field("session_config", &self.session_config)
.finish()
}
}
impl<M: fmt::Debug> fmt::Debug for InitOptions<M> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InitOptions")
.field("model_name", &self.model_name)
.field("environment", &self.environment)
.field(
"execution_providers",
&ProviderNames(&self.execution_providers),
)
.field("cache_dir", &self.cache_dir)
.field("show_download_progress", &self.show_download_progress)
.field("intra_threads", &self.intra_threads)
.field("session_config", &self.session_config)
.finish()
}
}
impl<M: Default + HasMaxLength> Default for InitOptionsWithLength<M> {
fn default() -> Self {
Self {
model_name: M::default(),
environment: None,
execution_providers: Default::default(),
cache_dir: get_cache_dir().into(),
show_download_progress: true,
max_length: M::MAX_LENGTH,
intra_threads: None,
session_config: Vec::new(),
}
}
}
impl<M: Default> Default for InitOptions<M> {
fn default() -> Self {
Self {
model_name: M::default(),
environment: None,
execution_providers: Default::default(),
cache_dir: get_cache_dir().into(),
show_download_progress: true,
intra_threads: None,
session_config: Vec::new(),
}
}
}
impl<M: Default + HasMaxLength> InitOptionsWithLength<M> {
pub fn new(model_name: M) -> Self {
Self {
model_name,
..Default::default()
}
}
pub fn with_max_length(mut self, max_length: usize) -> Self {
self.max_length = max_length;
self
}
pub fn with_cache_dir(mut self, cache_dir: PathBuf) -> Self {
self.cache_dir = cache_dir;
self
}
pub fn with_environment(mut self, environment: Environment) -> Self {
self.environment = Some(environment);
self
}
pub fn with_execution_providers(
mut self,
execution_providers: Vec<ExecutionProviderDispatch>,
) -> Self {
self.execution_providers = execution_providers;
self
}
pub fn with_intra_threads(mut self, intra_threads: usize) -> Self {
self.intra_threads = Some(intra_threads);
self
}
pub fn with_session_config(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.session_config.push((key.into(), value.into()));
self
}
pub fn with_show_download_progress(mut self, show_download_progress: bool) -> Self {
self.show_download_progress = show_download_progress;
self
}
}
impl<M: Default> InitOptions<M> {
pub fn new(model_name: M) -> Self {
Self {
model_name,
..Default::default()
}
}
pub fn with_cache_dir(mut self, cache_dir: PathBuf) -> Self {
self.cache_dir = cache_dir;
self
}
pub fn with_environment(mut self, environment: Environment) -> Self {
self.environment = Some(environment);
self
}
pub fn with_execution_providers(
mut self,
execution_providers: Vec<ExecutionProviderDispatch>,
) -> Self {
self.execution_providers = execution_providers;
self
}
pub fn with_intra_threads(mut self, intra_threads: usize) -> Self {
self.intra_threads = Some(intra_threads);
self
}
pub fn with_session_config(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.session_config.push((key.into(), value.into()));
self
}
pub fn with_show_download_progress(mut self, show_download_progress: bool) -> Self {
self.show_download_progress = show_download_progress;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn intra_threads_defaults_none_and_builder_sets() {
let o = InitOptions::<crate::ImageEmbeddingModel>::default();
assert_eq!(o.intra_threads, None);
let o = o.with_intra_threads(4);
assert_eq!(o.intra_threads, Some(4));
}
}