metrale_model_engine/traits/model/
adapters.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! 2026-09-26: `ModelAdapters`, one of the supertraits `Model` is made of. Its methods, default
4//! bodies and docs are the ones `Model` declared before the split.
5//!
6//! Owner: model-engine.
7//! Invariants: none beyond the types.
8
9use anyhow::{Result, bail};
10
11/// 2026-09-26: LoRA adapters: rotation, slot refs, disk swap and promotion.
12pub trait ModelAdapters {
13    /// 2026-09-25: Make the resident LoRA adapter `name` the active one. Takes `&mut self`, so it
14    /// runs only while nothing else holds the model. Default: an error.
15    fn set_active_lora(&mut self, _name: &str) -> Result<()> {
16        bail!("this model does not support LoRA adapter rotation")
17    }
18
19    /// 2026-09-25: The prefix-cache adapter id for a LoRA slot selector: `>= 0` names a resident
20    /// slot, `-1` the active adapter. Default `0`, the base model's id.
21    fn adapter_id_for(&self, _slot: i32) -> u64 {
22        0
23    }
24
25    /// 2026-09-25: Take a ref on the LoRA slot a sequence will use (called at prefill), resolving
26    /// `-1` to the active adapter as [`Self::adapter_id_for`] does. Returns the resolved slot,
27    /// which the caller must pass to [`Self::release_adapter_slot`], or `-1` when no ref was
28    /// taken. Default `-1`.
29    fn acquire_adapter_slot(&self, _slot: i32) -> i32 {
30        -1
31    }
32
33    /// 2026-09-25: Release a ref taken by [`Self::acquire_adapter_slot`], by the slot it returned.
34    /// Default: no-op.
35    fn release_adapter_slot(&self, _resolved: i32) {}
36
37    /// 2026-09-25: Load the LoRA adapter at `dir` into pool slot `slot`. Takes `&mut self`, so it
38    /// runs only while nothing else holds the model. Default: an error.
39    fn swap_lora_from_disk(
40        &mut self,
41        _dir: &std::path::Path,
42        _name: &str,
43        _slot: usize,
44    ) -> Result<()> {
45        bail!("this model does not support LoRA disk swap")
46    }
47
48    /// 2026-09-25: Fetch the adapter `name` (staged on `peer_addr` as `adapter_id`) from the peer
49    /// into a pool slot and make it active; returns `(slot, evicted adapter name)`. `peft`
50    /// supplies the rank, alpha and scaling. Default: an error.
51    fn promote_lora_from_peer(
52        &mut self,
53        _peer_addr: &str,
54        _adapter_id: &str,
55        _name: &str,
56        _peft: metrale_config::PeftAdapterConfig,
57    ) -> Result<(usize, Option<String>)> {
58        bail!("this model does not support LoRA peer promotion")
59    }
60
61    /// 2026-09-25: Load the adapter `name` from `adapter_dir` into a pool slot and make it active;
62    /// returns `(slot, evicted adapter name)`. Default: an error.
63    fn promote_lora_from_disk(
64        &mut self,
65        _adapter_dir: &std::path::Path,
66        _name: &str,
67    ) -> Result<(usize, Option<String>)> {
68        bail!("this model does not support LoRA disk promotion")
69    }
70}