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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
impl AppState {
/// Create new application state
///
/// # Arguments
///
/// * `model` - Model for inference
/// * `tokenizer` - Tokenizer for text processing
#[must_use]
pub fn new(model: Model, tokenizer: BPETokenizer) -> Self {
let (audit_logger, audit_sink) = create_audit_state();
Self {
model: Some(Arc::new(model)),
tokenizer: Some(Arc::new(tokenizer)),
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: None,
audit_logger,
audit_sink,
#[cfg(feature = "gpu")]
gpu_model: None,
quantized_model: None,
#[cfg(feature = "gpu")]
cached_model: None,
#[cfg(feature = "gpu")]
dispatch_metrics: None,
#[cfg(feature = "gpu")]
batch_request_tx: None,
#[cfg(feature = "gpu")]
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
}
}
/// Create application state with model registry for multi-model serving
///
/// # Arguments
///
/// * `registry` - Model registry with pre-registered models
/// * `default_model_id` - Default model to use when not specified
///
/// # Errors
///
/// Returns error if default model doesn't exist in registry
pub fn with_registry(
registry: ModelRegistry,
default_model_id: &str,
) -> Result<Self, RealizarError> {
// Verify default model exists
if !registry.contains(default_model_id) {
return Err(RealizarError::ModelNotFound(default_model_id.to_string()));
}
let (audit_logger, audit_sink) = create_audit_state();
Ok(Self {
model: None,
tokenizer: None,
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: Some(Arc::new(registry)),
default_model_id: Some(default_model_id.to_string()),
apr_model: None,
audit_logger,
audit_sink,
#[cfg(feature = "gpu")]
gpu_model: None,
quantized_model: None,
#[cfg(feature = "gpu")]
cached_model: None,
#[cfg(feature = "gpu")]
dispatch_metrics: None,
#[cfg(feature = "gpu")]
batch_request_tx: None,
#[cfg(feature = "gpu")]
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
})
}
/// Get model and tokenizer by ID (or default)
#[allow(clippy::type_complexity)]
fn get_model(
&self,
model_id: Option<&str>,
) -> Result<(Arc<Model>, Arc<BPETokenizer>), RealizarError> {
// Multi-model mode
if let Some(registry) = &self.registry {
let id = model_id
.or(self.default_model_id.as_deref())
.ok_or_else(|| RealizarError::RegistryError("No model ID specified".to_string()))?;
return registry.get(id);
}
// Single model mode
let model = self
.model
.clone()
.ok_or_else(|| RealizarError::RegistryError("No model available".to_string()))?;
let tokenizer = self
.tokenizer
.clone()
.ok_or_else(|| RealizarError::RegistryError("No tokenizer available".to_string()))?;
Ok((model, tokenizer))
}
/// Resolve just the tokenizer, without requiring a dense `Model`.
///
/// [`Self::get_model`] can only answer with the dense f32 [`Model`], which is
/// `None` for every `apr serve run model.gguf` — the weights live in
/// `quantized_model` instead. Handlers that need nothing but the tokenizer
/// (`/tokenize`, `/batch/tokenize`) used to call `get_model()` and were
/// therefore dead on the standard serve path, answering
/// `"Model registry error: No model available"` on a server whose `/generate`
/// was working and whose `/health` reported `model_loaded:true`
/// (aprender#2376 findings 1 and 10).
///
/// Registry mode still resolves through the registry so a `model_id` selects
/// that model's tokenizer.
///
/// # Errors
///
/// [`RealizarError::ModelNotFound`] if a registry `model_id` is unknown, or
/// [`RealizarError::RegistryError`] if no tokenizer is resident at all.
pub(crate) fn get_tokenizer(
&self,
model_id: Option<&str>,
) -> Result<Arc<BPETokenizer>, RealizarError> {
if let Some(registry) = &self.registry {
let id = model_id
.or(self.default_model_id.as_deref())
.ok_or_else(|| RealizarError::RegistryError("No model ID specified".to_string()))?;
return registry.get(id).map(|(_, tokenizer)| tokenizer);
}
self.tokenizer
.clone()
.ok_or_else(|| RealizarError::RegistryError("No tokenizer available".to_string()))
}
/// The on-disk format of the resident model, as proven by which backend loaded it.
///
/// Single source of truth for `GET /models` and `GET /realize/model`, which
/// used to hardcode two different literals — `"unknown"` and `"gguf"` — and so
/// contradicted each other about the same model (aprender#2376 finding 6).
///
/// Returns `"unknown"` for the dense f32 transformer: it is built from either
/// a `.apr` or a `.safetensors` file and does not retain which, and a
/// confident wrong answer is worse than an honest unknown.
#[must_use]
pub fn model_format(&self) -> &'static str {
// Every GGUF-derived backend: quantized K-quant weights or a live mmap.
if self.quantized_model.is_some() || self.mapped_gguf_model.is_some() {
return "gguf";
}
#[cfg(feature = "gpu")]
if self.gpu_model.is_some() || self.cached_model.is_some() {
return "gguf";
}
#[cfg(feature = "cuda")]
if self.cuda_model.is_some() {
return "gguf";
}
"unknown"
}
/// Create application state with model caching enabled
///
/// # Arguments
///
/// * `cache_capacity` - Maximum number of models to cache
///
/// # Panics
///
/// Panics if model or tokenizer creation fails (should not happen with valid config)
#[must_use]
pub fn with_cache(cache_capacity: usize) -> Self {
// Create empty state with cache
let config = ModelConfig {
vocab_size: 100,
hidden_dim: 32,
num_heads: 1,
num_layers: 1,
intermediate_dim: 64,
eps: 1e-5,
};
let model = Model::new(config).expect("Failed to create placeholder model");
let vocab: Vec<String> = (0..100)
.map(|i| {
if i == 0 {
"<unk>".to_string()
} else {
format!("token{i}")
}
})
.collect();
let tokenizer =
BPETokenizer::new(vocab, vec![], "<unk>").expect("Failed to create tokenizer");
let (audit_logger, audit_sink) = create_audit_state();
Self {
model: Some(Arc::new(model)),
tokenizer: Some(Arc::new(tokenizer)),
cache: Some(Arc::new(ModelCache::new(cache_capacity))),
cache_key: Some(CacheKey::new("default".to_string())),
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: None,
audit_logger,
audit_sink,
#[cfg(feature = "gpu")]
gpu_model: None,
quantized_model: None,
#[cfg(feature = "gpu")]
cached_model: None,
#[cfg(feature = "gpu")]
dispatch_metrics: None,
#[cfg(feature = "gpu")]
batch_request_tx: None,
#[cfg(feature = "gpu")]
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
}
}
/// Create a demo state with small model for testing
///
/// # Errors
///
/// Returns error if model or tokenizer creation fails
pub fn demo() -> Result<Self, RealizarError> {
let config = ModelConfig {
vocab_size: 100,
hidden_dim: 32,
num_heads: 1,
num_layers: 1,
intermediate_dim: 64,
eps: 1e-5,
};
let model = Model::new(config)?;
// Simple demo vocabulary
let vocab: Vec<String> = (0..100)
.map(|i| {
if i == 0 {
"<unk>".to_string()
} else {
format!("token{i}")
}
})
.collect();
let tokenizer = BPETokenizer::new(vocab, vec![], "<unk>")?;
// Create demo APR model (real inference, not mock)
// Simple model: sum of inputs with bias
let apr_model = create_demo_apr_model(4)?; // 4 input features
let (audit_logger, audit_sink) = create_audit_state();
Ok(Self {
model: Some(Arc::new(model)),
tokenizer: Some(Arc::new(tokenizer)),
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: Some(Arc::new(apr_model)),
audit_logger,
audit_sink,
#[cfg(feature = "gpu")]
gpu_model: None,
quantized_model: None,
#[cfg(feature = "gpu")]
cached_model: None,
#[cfg(feature = "gpu")]
dispatch_metrics: None,
#[cfg(feature = "gpu")]
batch_request_tx: None,
#[cfg(feature = "gpu")]
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
})
}
/// Create a MOCK demo state for fast HTTP handler testing (no inference)
///
/// This creates an AppState with NO model loaded, so all inference endpoints
/// return errors immediately. Used for testing HTTP handler code paths
/// without the ~0.5s overhead of model creation per test.
///
/// # Performance (Dr. Popper's "Tax of Setup" Fix)
/// - `demo()`: ~0.5s (creates real model)
/// - `demo_mock()`: ~0.001s (no model, instant errors)
///
/// # When to use
/// - Use `demo_mock()` for HTTP routing/parsing tests (95% of API tests)
/// - Use `demo()` only when you need actual inference output
pub fn demo_mock() -> Result<Self, RealizarError> {
let (audit_logger, audit_sink) = create_audit_state();
Ok(Self {
model: None, // No model = instant "model not loaded" errors
tokenizer: None,
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: None,
audit_logger,
audit_sink,
#[cfg(feature = "gpu")]
gpu_model: None,
quantized_model: None,
#[cfg(feature = "gpu")]
cached_model: None,
#[cfg(feature = "gpu")]
dispatch_metrics: None,
#[cfg(feature = "gpu")]
batch_request_tx: None,
#[cfg(feature = "gpu")]
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
})
}
/// Create application state with a GPU model for GGUF inference (M33: IMP-084)
///
/// # Arguments
///
/// * `gpu_model` - GPU model for inference
///
/// # Errors
///
/// Returns error if tokenizer creation fails
#[cfg(feature = "gpu")]
pub fn with_gpu_model(gpu_model: crate::gpu::GpuModel) -> Result<Self, RealizarError> {
// Create tokenizer with vocab size matching GPU model
let vocab_size = gpu_model.config().vocab_size;
let vocab: Vec<String> = (0..vocab_size)
.map(|i| {
if i == 0 {
"<unk>".to_string()
} else {
format!("token{i}")
}
})
.collect();
let tokenizer = BPETokenizer::new(vocab, vec![], "<unk>")?;
let (audit_logger, audit_sink) = create_audit_state();
Ok(Self {
model: None,
tokenizer: Some(Arc::new(tokenizer)),
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: None,
audit_logger,
audit_sink,
gpu_model: Some(Arc::new(std::sync::RwLock::new(gpu_model))),
quantized_model: None,
cached_model: None,
dispatch_metrics: None,
batch_request_tx: None,
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
})
}
/// Create application state with GPU model and real vocabulary (IMP-152)
///
/// This version uses the actual vocabulary from the GGUF file for proper text encoding/decoding.
///
/// # Arguments
///
/// * `gpu_model` - GPU model for inference
/// * `vocab` - Vocabulary tokens from GGUF metadata (tokenizer.ggml.tokens)
///
/// # Errors
///
/// Returns error if tokenizer creation fails
#[cfg(feature = "gpu")]
pub fn with_gpu_model_and_vocab(
gpu_model: crate::gpu::GpuModel,
vocab: Vec<String>,
) -> Result<Self, RealizarError> {
let tokenizer = BPETokenizer::new(vocab, vec![], "<unk>")?;
let (audit_logger, audit_sink) = create_audit_state();
Ok(Self {
model: None,
tokenizer: Some(Arc::new(tokenizer)),
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: None,
audit_logger,
audit_sink,
gpu_model: Some(Arc::new(std::sync::RwLock::new(gpu_model))),
quantized_model: None,
cached_model: None,
dispatch_metrics: None,
batch_request_tx: None,
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
})
}
/// Create application state with a quantized model for fused Q4_K inference (IMP-100)
///
/// This is 1.37x faster than dequantized GpuModel due to reduced memory bandwidth.
///
/// # Arguments
///
/// * `quantized_model` - Quantized model for fused Q4_K inference
///
/// # Errors
///
/// Returns error if tokenizer creation fails
pub fn with_quantized_model(
quantized_model: crate::gguf::OwnedQuantizedModel,
) -> Result<Self, RealizarError> {
// Create tokenizer with vocab size matching model
let vocab_size = quantized_model.config.vocab_size;
let vocab: Vec<String> = (0..vocab_size)
.map(|i| {
if i == 0 {
"<unk>".to_string()
} else {
format!("token{i}")
}
})
.collect();
let tokenizer = BPETokenizer::new(vocab, vec![], "<unk>")?;
let (audit_logger, audit_sink) = create_audit_state();
Ok(Self {
model: None,
tokenizer: Some(Arc::new(tokenizer)),
cache: None,
cache_key: None,
metrics: Arc::new(MetricsCollector::new()),
registry: None,
default_model_id: None,
apr_model: None,
audit_logger,
audit_sink,
#[cfg(feature = "gpu")]
gpu_model: None,
quantized_model: Some(Arc::new(quantized_model)),
#[cfg(feature = "gpu")]
cached_model: None,
#[cfg(feature = "gpu")]
dispatch_metrics: None,
#[cfg(feature = "gpu")]
batch_request_tx: None,
#[cfg(feature = "gpu")]
batch_config: None,
#[cfg(feature = "cuda")]
cuda_model: None,
#[cfg(feature = "cuda")]
safetensors_cuda_model: None,
#[cfg(feature = "cuda")]
cuda_batch_tx: None,
#[cfg(feature = "cuda")]
apr_q4k_tx: None,
apr_transformer: None,
cached_architecture: None,
mapped_gguf_model: None,
cached_eos_token_id: None,
verbose: false,
trace: false,
model_source: None,
})
}
}