swadhinbiswas WIP save-point: production hardening, H3 removal, fmt gate, and accumulated phase work · 7/15/2026
2080d40
234 lines · 8.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//! The streaming [`Engine`]: device selection, an in-memory [`ModelRegistry`],
//! and token streaming via a tokio channel. Generation runs on a dedicated OS
//! thread so the hot path stays GIL-free.

use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex};

use candle_core::Device;
use tokio::sync::mpsc;

use crate::model::{GeneratedToken, MockModel, Model, ModelError, SamplingParams};
use crate::spec_decode::{SpeculativeConfig, SpeculativeModel};
use crate::spec_decode_tree::TreeSpeculativeModel;

/// Compute device a model runs on. Mirrors Candle's [`Device`] minus the ROCm
/// variant (candle 0.11 enables AMD GPUs via the `cuda` feature on a ROCm
/// toolchain).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EngineDevice {
    Cpu,
    Cuda(usize),
    Metal(usize),
}

impl EngineDevice {
    /// Convert to a Candle [`Device`]. GPU variants are constructed via
    /// `new_cuda`/`new_metal` (which validate the ordinal exists).
    pub fn to_candle(self) -> candle_core::Result<Device> {
        match self {
            EngineDevice::Cpu => Ok(Device::Cpu),
            EngineDevice::Cuda(i) => Device::new_cuda(i),
            EngineDevice::Metal(i) => Device::new_metal(i),
        }
    }

    /// Best-effort discovery of available devices.
    ///
    /// CPU is always present. GPUs are probed by attempting to construct the
    /// device (cheap on a properly configured host; errors are ignored).
    pub fn discover() -> Vec<EngineDevice> {
        let mut devices = vec![EngineDevice::Cpu];
        for i in 0..8 {
            if Device::new_cuda(i).is_ok() {
                devices.push(EngineDevice::Cuda(i));
            }
        }
        for i in 0..8 {
            if Device::new_metal(i).is_ok() {
                devices.push(EngineDevice::Metal(i));
            }
        }
        devices
    }
}

/// Where a registered model came from.
#[derive(Debug, Clone)]
pub enum ModelSource {
    /// Weight-free model (tests, demos).
    Mock,
    /// Loaded from a directory of weights (path recorded).
    Real(std::path::PathBuf),
}

struct RegistryEntry {
    model: Arc<dyn Model>,
    source: ModelSource,
}

/// In-memory collection of loaded models, keyed by name.
#[derive(Default)]
pub struct ModelRegistry {
    models: HashMap<String, RegistryEntry>,
}

impl ModelRegistry {
    /// Register a model under `name`, replacing any existing entry.
    pub fn register(&mut self, name: &str, model: Arc<dyn Model>, source: ModelSource) {
        self.models.insert(name.to_string(), RegistryEntry { model, source });
    }

    /// Fetch a shared handle to a registered model.
    pub fn get(&self, name: &str) -> Option<Arc<dyn Model>> {
        self.models.get(name).map(|e| e.model.clone())
    }

    /// List registered model names.
    pub fn list(&self) -> Vec<String> {
        self.models.keys().cloned().collect()
    }

    /// Number of registered models.
    pub fn len(&self) -> usize {
        self.models.len()
    }

    /// Whether the registry is empty.
    pub fn is_empty(&self) -> bool {
        self.models.is_empty()
    }
}

/// The inference engine: owns the device and the model registry, and streams
/// generation output back to callers over a channel.
pub struct Engine {
    device: Device,
    registry: Arc<Mutex<ModelRegistry>>,
}

impl Engine {
    /// Create an engine bound to `device`.
    pub fn new(device: EngineDevice) -> Result<Self, ModelError> {
        let device = device.to_candle().map_err(|e| ModelError::Generation(e.to_string()))?;
        Ok(Self { device, registry: Arc::new(Mutex::new(ModelRegistry::default())) })
    }

    /// The Candle device this engine runs models on.
    pub fn device(&self) -> &Device {
        &self.device
    }

    /// Register an arbitrary model implementation.
    pub fn register(&self, name: &str, model: Arc<dyn Model>) {
        self.registry.lock().unwrap().register(name, model, ModelSource::Mock);
    }

    /// Fetch a shared handle to a registered model by name.
    pub fn get(&self, name: &str) -> Option<Arc<dyn Model>> {
        self.registry.lock().unwrap().get(name)
    }

    /// Register a [`MockModel`] and return a handle to it.
    pub fn register_mock(&self, name: &str) -> Arc<MockModel> {
        let model = Arc::new(MockModel::new(crate::DEFAULT_MOCK_VOCAB));
        self.registry.lock().unwrap().register(name, model.clone(), ModelSource::Mock);
        model
    }

    /// Register a speculative-decode model: a `target` model served with a
    /// `draft` model proposing `gamma` candidate tokens per step. The wrapped
    /// [`SpeculativeModel`] is registered under `name` and served through the
    /// normal [`Engine::generate`] path transparently.
    pub fn register_speculative(
        &self,
        name: &str,
        target: Arc<dyn Model>,
        draft: Arc<dyn Model>,
        gamma: usize,
        seed: u64,
    ) {
        let spec = SpeculativeModel::new(target, SpeculativeConfig::new(gamma, draft, seed));
        self.registry.lock().unwrap().register(name, Arc::new(spec), ModelSource::Mock);
    }

    /// Register a tree-based speculative-decode model (Medusa/EAGLE-style): a
    /// `draft` model proposes a tree of `branch` candidates at each of `gamma`
    /// positions, and the `target` verifies the longest matching path. The
    /// wrapped [`TreeSpeculativeModel`] is registered under `name` and served
    /// through the normal [`Engine::generate`] path transparently — callers get
    /// the same token stream as plain target decode, just faster.
    pub fn register_tree_speculative(
        &self,
        name: &str,
        target: Arc<dyn Model>,
        draft: Arc<dyn Model>,
        gamma: usize,
        branch: usize,
        seed: u64,
    ) {
        let spec = TreeSpeculativeModel::new(target, draft, gamma, branch, seed);
        self.registry.lock().unwrap().register(name, Arc::new(spec), ModelSource::Mock);
    }

    /// Load a real model from `model_dir`.
    ///
    /// Behind the `real` feature (and with weights + a GPU toolkit) this will
    /// build the candle-transformers forward pass + KV cache (Phase 42). Until
    /// then it returns a clear error so callers don't silently get a no-op.
    pub fn load(&self, name: &str, model_dir: &Path) -> Result<(), ModelError> {
        #[cfg(feature = "real")]
        {
            use crate::real::model::RealModel;
            let model = RealModel::load(model_dir, self.device.clone())
                .map_err(|e| ModelError::Generation(e.to_string()))?;
            self.registry.lock().unwrap().register(
                name,
                Arc::new(model),
                ModelSource::Real(model_dir.to_path_buf()),
            );
            Ok(())
        }
        #[cfg(not(feature = "real"))]
        {
            let _ = (name, model_dir);
            Err(ModelError::FeatureRequired("real"))
        }
    }

    /// Generate tokens for `prompt` with `params`, streaming them to the
    /// returned receiver as they are produced.
    ///
    /// Generation runs on a dedicated OS thread (Candle compute is synchronous);
    /// the caller receives tokens asynchronously with zero GIL involvement.
    pub fn generate(
        &self,
        name: &str,
        prompt: &[u32],
        params: SamplingParams,
    ) -> Result<mpsc::UnboundedReceiver<GeneratedToken>, ModelError> {
        let model = {
            let reg = self.registry.lock().unwrap();
            reg.get(name).ok_or_else(|| ModelError::NotFound(name.to_string()))?
        };

        let (tx, rx) = mpsc::unbounded_channel();
        let prompt = prompt.to_vec();
        std::thread::spawn(move || {
            let _ = model.generate(&prompt, &params, &|token| tx.send(token).is_ok());
        });
        Ok(rx)
    }

    /// List registered model names.
    pub fn list_models(&self) -> Vec<String> {
        self.registry.lock().unwrap().list()
    }

    /// Return the [`ModelSource`] a registered model was loaded from, if known.
    pub fn model_source(&self, name: &str) -> Option<ModelSource> {
        self.registry.lock().unwrap().models.get(name).map(|e| e.source.clone())
    }
}