mistralrs/
vision_model.rs

1use candle_core::Device;
2use mistralrs_core::*;
3use mistralrs_core::{SearchCallback, Tool, ToolCallback};
4use std::collections::HashMap;
5use std::{
6    ops::{Deref, DerefMut},
7    path::PathBuf,
8    sync::Arc,
9};
10
11use crate::{best_device, Model};
12
13/// A tool callback with its associated Tool definition.
14#[derive(Clone)]
15pub struct ToolCallbackWithTool {
16    pub callback: Arc<ToolCallback>,
17    pub tool: Tool,
18}
19
20#[derive(Clone)]
21/// Configure a vision model with the various parameters for loading, running, and other inference behaviors.
22pub struct VisionModelBuilder {
23    // Loading model
24    pub(crate) model_id: String,
25    pub(crate) token_source: TokenSource,
26    pub(crate) hf_revision: Option<String>,
27    pub(crate) write_uqff: Option<PathBuf>,
28    pub(crate) from_uqff: Option<Vec<PathBuf>>,
29    pub(crate) calibration_file: Option<PathBuf>,
30    pub(crate) imatrix: Option<PathBuf>,
31    pub(crate) chat_template: Option<String>,
32    pub(crate) jinja_explicit: Option<String>,
33    pub(crate) tokenizer_json: Option<String>,
34    pub(crate) device_mapping: Option<DeviceMapSetting>,
35    pub(crate) max_edge: Option<u32>,
36    pub(crate) hf_cache_path: Option<PathBuf>,
37    pub(crate) search_bert_model: Option<BertEmbeddingModel>,
38    pub(crate) search_callback: Option<Arc<SearchCallback>>,
39    pub(crate) tool_callbacks: HashMap<String, Arc<ToolCallback>>,
40    pub(crate) tool_callbacks_with_tools: HashMap<String, ToolCallbackWithTool>,
41    pub(crate) device: Option<Device>,
42    pub(crate) matformer_config_path: Option<PathBuf>,
43    pub(crate) matformer_slice_name: Option<String>,
44
45    // Model running
46    pub(crate) topology: Option<Topology>,
47    pub(crate) loader_type: Option<VisionLoaderType>,
48    pub(crate) dtype: ModelDType,
49    pub(crate) force_cpu: bool,
50    pub(crate) isq: Option<IsqType>,
51    pub(crate) throughput_logging: bool,
52
53    // Other things
54    pub(crate) paged_attn_cfg: Option<PagedAttentionConfig>,
55    pub(crate) max_num_seqs: usize,
56    pub(crate) with_logging: bool,
57}
58
59impl VisionModelBuilder {
60    /// A few defaults are applied here:
61    /// - Token source is from the cache (.cache/huggingface/token)
62    /// - Maximum number of sequences running is 32
63    /// - Automatic device mapping with model defaults according to `AutoDeviceMapParams`
64    /// - By default, web searching compatible with the OpenAI `web_search_options` setting is disabled.
65    pub fn new(model_id: impl ToString) -> Self {
66        Self {
67            model_id: model_id.to_string(),
68            topology: None,
69            write_uqff: None,
70            from_uqff: None,
71            chat_template: None,
72            tokenizer_json: None,
73            max_edge: None,
74            loader_type: None,
75            dtype: ModelDType::Auto,
76            force_cpu: false,
77            token_source: TokenSource::CacheToken,
78            hf_revision: None,
79            isq: None,
80            max_num_seqs: 32,
81            with_logging: false,
82            device_mapping: None,
83            calibration_file: None,
84            imatrix: None,
85            jinja_explicit: None,
86            throughput_logging: false,
87            paged_attn_cfg: None,
88            hf_cache_path: None,
89            search_bert_model: None,
90            search_callback: None,
91            tool_callbacks: HashMap::new(),
92            tool_callbacks_with_tools: HashMap::new(),
93            device: None,
94            matformer_config_path: None,
95            matformer_slice_name: None,
96        }
97    }
98
99    /// Enable searching compatible with the OpenAI `web_search_options` setting. This uses the BERT model specified or the default.
100    pub fn with_search(mut self, search_bert_model: BertEmbeddingModel) -> Self {
101        self.search_bert_model = Some(search_bert_model);
102        self
103    }
104
105    /// Override the search function used when `web_search_options` is enabled.
106    pub fn with_search_callback(mut self, callback: Arc<SearchCallback>) -> Self {
107        self.search_callback = Some(callback);
108        self
109    }
110
111    pub fn with_tool_callback(
112        mut self,
113        name: impl Into<String>,
114        callback: Arc<ToolCallback>,
115    ) -> Self {
116        self.tool_callbacks.insert(name.into(), callback);
117        self
118    }
119
120    /// Register a callback with an associated Tool definition that will be automatically
121    /// added to requests when tool callbacks are active.
122    pub fn with_tool_callback_and_tool(
123        mut self,
124        name: impl Into<String>,
125        callback: Arc<ToolCallback>,
126        tool: Tool,
127    ) -> Self {
128        let name = name.into();
129        self.tool_callbacks_with_tools
130            .insert(name, ToolCallbackWithTool { callback, tool });
131        self
132    }
133
134    /// Enable runner throughput logging.
135    pub fn with_throughput_logging(mut self) -> Self {
136        self.throughput_logging = true;
137        self
138    }
139
140    /// Explicit JINJA chat template file (.jinja) to be used. If specified, this overrides all other chat templates.
141    pub fn with_jinja_explicit(mut self, jinja_explicit: String) -> Self {
142        self.jinja_explicit = Some(jinja_explicit);
143        self
144    }
145
146    /// Set the model topology for use during loading. If there is an overlap, the topology type is used over the ISQ type.
147    pub fn with_topology(mut self, topology: Topology) -> Self {
148        self.topology = Some(topology);
149        self
150    }
151
152    /// Literal Jinja chat template OR Path (ending in `.json`) to one.
153    pub fn with_chat_template(mut self, chat_template: impl ToString) -> Self {
154        self.chat_template = Some(chat_template.to_string());
155        self
156    }
157
158    /// Path to a discrete `tokenizer.json` file.
159    pub fn with_tokenizer_json(mut self, tokenizer_json: impl ToString) -> Self {
160        self.tokenizer_json = Some(tokenizer_json.to_string());
161        self
162    }
163
164    /// Manually set the model loader type. Otherwise, it will attempt to automatically
165    /// determine the loader type.
166    pub fn with_loader_type(mut self, loader_type: VisionLoaderType) -> Self {
167        self.loader_type = Some(loader_type);
168        self
169    }
170
171    /// Load the model in a certain dtype.
172    pub fn with_dtype(mut self, dtype: ModelDType) -> Self {
173        self.dtype = dtype;
174        self
175    }
176
177    /// Force usage of the CPU device. Do not use PagedAttention with this.
178    pub fn with_force_cpu(mut self) -> Self {
179        self.force_cpu = true;
180        self
181    }
182
183    /// Source of the Hugging Face token.
184    pub fn with_token_source(mut self, token_source: TokenSource) -> Self {
185        self.token_source = token_source;
186        self
187    }
188
189    /// Set the revision to use for a Hugging Face remote model.
190    pub fn with_hf_revision(mut self, revision: impl ToString) -> Self {
191        self.hf_revision = Some(revision.to_string());
192        self
193    }
194
195    /// Use ISQ of a certain type. If there is an overlap, the topology type is used over the ISQ type.
196    pub fn with_isq(mut self, isq: IsqType) -> Self {
197        self.isq = Some(isq);
198        self
199    }
200
201    /// Utilise this calibration_file file during ISQ
202    pub fn with_calibration_file(mut self, path: PathBuf) -> Self {
203        self.calibration_file = Some(path);
204        self
205    }
206
207    /// Enable PagedAttention. Configure PagedAttention with a [`PagedAttentionConfig`] object, which
208    /// can be created with sensible values with a [`PagedAttentionMetaBuilder`](crate::PagedAttentionMetaBuilder).
209    ///
210    /// If PagedAttention is not supported (query with [`paged_attn_supported`]), this will do nothing.
211    pub fn with_paged_attn(
212        mut self,
213        paged_attn_cfg: impl FnOnce() -> anyhow::Result<PagedAttentionConfig>,
214    ) -> anyhow::Result<Self> {
215        if paged_attn_supported() {
216            self.paged_attn_cfg = Some(paged_attn_cfg()?);
217        } else {
218            self.paged_attn_cfg = None;
219        }
220        Ok(self)
221    }
222
223    /// Set the maximum number of sequences which can be run at once.
224    pub fn with_max_num_seqs(mut self, max_num_seqs: usize) -> Self {
225        self.max_num_seqs = max_num_seqs;
226        self
227    }
228
229    /// Enable logging.
230    pub fn with_logging(mut self) -> Self {
231        self.with_logging = true;
232        self
233    }
234
235    /// Provide metadata to initialize the device mapper.
236    pub fn with_device_mapping(mut self, device_mapping: DeviceMapSetting) -> Self {
237        self.device_mapping = Some(device_mapping);
238        self
239    }
240
241    #[deprecated(
242        note = "Use `UqffTextModelBuilder` to load a UQFF model instead of the generic `from_uqff`"
243    )]
244    /// Path to read a `.uqff` file from. Other necessary configuration files must be present at this location.
245    ///
246    /// For example, these include:
247    /// - `residual.safetensors`
248    /// - `tokenizer.json`
249    /// - `config.json`
250    /// - More depending on the model
251    pub fn from_uqff(mut self, path: Vec<PathBuf>) -> Self {
252        self.from_uqff = Some(path);
253        self
254    }
255
256    /// Automatically resize and pad images to this maximum edge length. Aspect ratio is preserved.
257    /// This is only supported on the Qwen2-VL and Idefics 2 models. Others handle this internally.
258    pub fn from_max_edge(mut self, max_edge: u32) -> Self {
259        self.max_edge = Some(max_edge);
260        self
261    }
262
263    /// Path to write a `.uqff` file to and serialize the other necessary files.
264    ///
265    /// The parent (part of the path excluding the filename) will determine where any other files
266    /// serialized are written to.
267    ///
268    /// For example, these include:
269    /// - `residual.safetensors`
270    /// - `tokenizer.json`
271    /// - `config.json`
272    /// - More depending on the model
273    pub fn write_uqff(mut self, path: PathBuf) -> Self {
274        self.write_uqff = Some(path);
275        self
276    }
277
278    /// Cache path for Hugging Face models downloaded locally
279    pub fn from_hf_cache_pathf(mut self, hf_cache_path: PathBuf) -> Self {
280        self.hf_cache_path = Some(hf_cache_path);
281        self
282    }
283
284    /// Set the main device to load this model onto. Automatic device mapping will be performed starting with this device.
285    pub fn with_device(mut self, device: Device) -> Self {
286        self.device = Some(device);
287        self
288    }
289
290    /// Path to a Matryoshka Transformer configuration CSV file.
291    pub fn with_matformer_config_path(mut self, path: PathBuf) -> Self {
292        self.matformer_config_path = Some(path);
293        self
294    }
295
296    /// Name of the slice to use from the Matryoshka Transformer configuration.
297    pub fn with_matformer_slice_name(mut self, name: String) -> Self {
298        self.matformer_slice_name = Some(name);
299        self
300    }
301
302    pub async fn build(self) -> anyhow::Result<Model> {
303        let config = VisionSpecificConfig {
304            topology: self.topology,
305            write_uqff: self.write_uqff,
306            from_uqff: self.from_uqff,
307            max_edge: self.max_edge,
308            calibration_file: self.calibration_file,
309            imatrix: self.imatrix,
310            hf_cache_path: self.hf_cache_path,
311            matformer_config_path: self.matformer_config_path,
312            matformer_slice_name: self.matformer_slice_name,
313        };
314
315        if self.with_logging {
316            initialize_logging();
317        }
318
319        let loader = VisionLoaderBuilder::new(
320            config,
321            self.chat_template,
322            self.tokenizer_json,
323            Some(self.model_id),
324            self.jinja_explicit,
325        )
326        .build(self.loader_type);
327
328        // Load, into a Pipeline
329        let pipeline = loader.load_model_from_hf(
330            self.hf_revision,
331            self.token_source,
332            &self.dtype,
333            &self.device.unwrap_or(best_device(self.force_cpu).unwrap()),
334            !self.with_logging,
335            self.device_mapping
336                .unwrap_or(DeviceMapSetting::Auto(AutoDeviceMapParams::default_vision())),
337            self.isq,
338            self.paged_attn_cfg,
339        )?;
340
341        let scheduler_method = match self.paged_attn_cfg {
342            Some(_) => {
343                let config = pipeline
344                    .lock()
345                    .await
346                    .get_metadata()
347                    .cache_config
348                    .as_ref()
349                    .cloned();
350
351                if let Some(config) = config {
352                    SchedulerConfig::PagedAttentionMeta {
353                        max_num_seqs: self.max_num_seqs,
354                        config,
355                    }
356                } else {
357                    SchedulerConfig::DefaultScheduler {
358                        method: DefaultSchedulerMethod::Fixed(self.max_num_seqs.try_into()?),
359                    }
360                }
361            }
362            None => SchedulerConfig::DefaultScheduler {
363                method: DefaultSchedulerMethod::Fixed(self.max_num_seqs.try_into()?),
364            },
365        };
366
367        let mut runner = MistralRsBuilder::new(
368            pipeline,
369            scheduler_method,
370            self.throughput_logging,
371            self.search_bert_model,
372        );
373        if let Some(cb) = self.search_callback.clone() {
374            runner = runner.with_search_callback(cb);
375        }
376        for (name, cb) in &self.tool_callbacks {
377            runner = runner.with_tool_callback(name.clone(), cb.clone());
378        }
379        for (name, callback_with_tool) in &self.tool_callbacks_with_tools {
380            runner = runner.with_tool_callback_and_tool(
381                name.clone(),
382                callback_with_tool.callback.clone(),
383                callback_with_tool.tool.clone(),
384            );
385        }
386        let runner = runner.with_no_kv_cache(false).with_no_prefix_cache(false);
387
388        Ok(Model::new(runner.build().await))
389    }
390}
391
392#[derive(Clone)]
393/// Configure a UQFF text model with the various parameters for loading, running, and other inference behaviors.
394/// This wraps and implements `DerefMut` for the VisionModelBuilder, so users should take care to not call UQFF-related methods.
395pub struct UqffVisionModelBuilder(VisionModelBuilder);
396
397impl UqffVisionModelBuilder {
398    /// A few defaults are applied here:
399    /// - Token source is from the cache (.cache/huggingface/token)
400    /// - Maximum number of sequences running is 32
401    /// - Automatic device mapping with model defaults according to `AutoDeviceMapParams`
402    pub fn new(model_id: impl ToString, uqff_file: Vec<PathBuf>) -> Self {
403        let mut inner = VisionModelBuilder::new(model_id);
404        inner.from_uqff = Some(uqff_file);
405        Self(inner)
406    }
407
408    pub async fn build(self) -> anyhow::Result<Model> {
409        self.0.build().await
410    }
411
412    /// This wraps the VisionModelBuilder, so users should take care to not call UQFF-related methods.
413    pub fn into_inner(self) -> VisionModelBuilder {
414        self.0
415    }
416}
417
418impl Deref for UqffVisionModelBuilder {
419    type Target = VisionModelBuilder;
420
421    fn deref(&self) -> &Self::Target {
422        &self.0
423    }
424}
425
426impl DerefMut for UqffVisionModelBuilder {
427    fn deref_mut(&mut self) -> &mut Self::Target {
428        &mut self.0
429    }
430}
431
432impl From<UqffVisionModelBuilder> for VisionModelBuilder {
433    fn from(value: UqffVisionModelBuilder) -> Self {
434        value.0
435    }
436}