inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
#![allow(
    clippy::clone_on_copy,
    clippy::if_same_then_else,
    clippy::match_single_binding
)]

use crate::{
    InfernoError,
    ai_features::sampling::{Sampler, SamplingConfig, SamplingStrategy},
    ai_features::streaming::{StreamConfig, StreamToken, create_stream_channel},
    backends::{
        BackendConfig, BackendType, InferenceBackend, InferenceMetrics, InferenceParams,
        TokenStream,
    },
    models::ModelInfo,
};
use anyhow::Result;
use async_stream::stream;
use llama_cpp_2::{
    context::{LlamaContext, params::LlamaContextParams},
    llama_backend::LlamaBackend,
    llama_batch::LlamaBatch,
    model::{AddBos, LlamaModel, Special, params::LlamaModelParams},
    sampling::LlamaSampler,
    token::LlamaToken,
};
use std::{num::NonZeroU32, sync::Arc, time::Instant};
use tracing::{debug, info, warn};

// Real GGUF implementation using llama-cpp-2
pub struct GgufBackend {
    config: BackendConfig,
    backend: Option<Arc<LlamaBackend>>,
    model: Option<Arc<LlamaModel>>,
    model_info: Option<ModelInfo>,
    metrics: Option<InferenceMetrics>,
}

impl GgufBackend {
    pub fn new(config: BackendConfig) -> Result<Self> {
        info!("Initializing GGUF backend with real llama.cpp support");

        Ok(Self {
            config,
            backend: None,
            model: None,
            model_info: None,
            metrics: None,
        })
    }

    fn validate_config(&self) -> Result<()> {
        if self.config.context_size > 32768 {
            warn!(
                "Very large context size may impact performance: {}",
                self.config.context_size
            );
        }
        if self.config.context_size < 256 {
            return Err(
                InfernoError::Backend("Context size too small (minimum 256)".to_string()).into(),
            );
        }
        Ok(())
    }

    async fn real_tokenize(&self, text: &str) -> Result<Vec<i32>> {
        let model = self
            .model
            .as_ref()
            .ok_or_else(|| InfernoError::Backend("Model not loaded".to_string()))?;

        debug!(
            "Tokenizing text of length: {} with real llama.cpp",
            text.len()
        );

        let tokens = tokio::task::spawn_blocking({
            let model = model.clone();
            let text = text.to_string();
            move || {
                model
                    .str_to_token(&text, AddBos::Always)
                    .map_err(|e| InfernoError::Backend(format!("Tokenization failed: {}", e)))
            }
        })
        .await
        .map_err(|e| InfernoError::Backend(format!("Tokenization task failed: {}", e)))?
        .map_err(anyhow::Error::from)?;

        let token_ids: Vec<i32> = tokens.iter().map(|t| t.0).collect();
        debug!("Tokenized text into {} tokens", token_ids.len());
        Ok(token_ids)
    }

    async fn real_detokenize(&self, tokens: &[i32]) -> Result<String> {
        let model = self
            .model
            .as_ref()
            .ok_or_else(|| InfernoError::Backend("Model not loaded".to_string()))?;

        debug!("Detokenizing {} tokens with real llama.cpp", tokens.len());

        let text = tokio::task::spawn_blocking({
            let model = model.clone();
            let tokens = tokens.to_vec();
            move || {
                let mut result = String::new();
                for &token in &tokens {
                    match model.token_to_str(LlamaToken(token), Special::Tokenize) {
                        Ok(token_str) => result.push_str(&token_str),
                        Err(e) => {
                            warn!("Failed to convert token {} to string: {}", token, e);
                            result.push_str(&format!("[UNK_{}]", token));
                        }
                    }
                }
                Ok::<String, InfernoError>(result)
            }
        })
        .await
        .map_err(|e| InfernoError::Backend(format!("Detokenization task failed: {}", e)))?
        .map_err(anyhow::Error::from)?;

        Ok(text)
    }

    fn estimate_token_count(&self, text: &str) -> u32 {
        // More sophisticated estimation
        let word_count = text.split_whitespace().count();
        let char_count = text.len();

        // Estimate based on both word and character count
        // English typically has 3-4 characters per token
        let char_based = (char_count as f32 / 3.5).ceil() as u32;
        let word_based = (word_count as f32 * 1.3).ceil() as u32; // Account for subword tokenization

        // Use the more conservative estimate
        char_based.max(word_based).max(1)
    }

    async fn generate_response(&mut self, input: &str, params: &InferenceParams) -> Result<String> {
        debug!(
            "🔥 Generating response for input of length: {} with Metal GPU acceleration",
            input.len()
        );

        let model = self
            .model
            .as_ref()
            .ok_or_else(|| InfernoError::Backend("Model not loaded".to_string()))?
            .clone();

        let backend = self
            .backend
            .as_ref()
            .ok_or_else(|| InfernoError::Backend("Backend not initialized".to_string()))?
            .clone();

        let input_str = input.to_string();
        let context_size = self.config.context_size;
        let batch_size = self.config.batch_size;
        let max_tokens = params.max_tokens;
        let temperature = params.temperature;
        let top_k = params.top_k;
        let top_p = params.top_p;
        let seed = params.seed;

        // Perform inference in spawn_blocking since LlamaContext is !Send
        let response = tokio::task::spawn_blocking(move || {
            // Create context for this inference session
            let ctx_params = LlamaContextParams::default()
                .with_n_ctx(NonZeroU32::new(context_size))
                .with_n_batch(batch_size);

            let mut context = model
                .new_context(&backend, ctx_params)
                .map_err(|e| InfernoError::Backend(format!("Failed to create context: {}", e)))?;

            // Tokenize input
            let input_tokens = model
                .str_to_token(&input_str, AddBos::Always)
                .map_err(|e| InfernoError::Backend(format!("Failed to tokenize: {}", e)))?;

            debug!("📝 Tokenized {} tokens from input", input_tokens.len());

            // Create batch and add input tokens
            let n_ctx = context.n_ctx();
            let mut batch = LlamaBatch::new(n_ctx as usize, 1);

            for (i, token) in input_tokens.iter().enumerate() {
                let is_last = i == input_tokens.len() - 1;
                batch
                    .add(token.clone(), i as i32, &[0], is_last)
                    .map_err(|e| {
                        InfernoError::Backend(format!("Failed to add token to batch: {}", e))
                    })?;
            }

            // Decode the input batch
            context
                .decode(&mut batch)
                .map_err(|e| InfernoError::Backend(format!("Failed to decode batch: {}", e)))?;

            debug!("⚡ Input processed through Metal GPU");

            // Create sampler with configuration from params
            let sampling_config = SamplingConfig {
                strategy: if input_str.is_empty() {
                    SamplingStrategy::Greedy
                } else if temperature.abs() < 0.01 {
                    SamplingStrategy::Greedy
                } else {
                    SamplingStrategy::TopKP
                },
                temperature: temperature.max(0.1).min(2.0),
                top_k: top_k.max(1),
                top_p: top_p.max(0.0).min(1.0),
                repeat_penalty: 1.1,
                seed,
            };

            // Log before sampler takes ownership
            let strategy = sampling_config.strategy;
            let temperature = sampling_config.temperature;

            let mut sampler = Sampler::new(sampling_config);

            // Generate tokens one by one
            let mut output_tokens = Vec::new();
            let max_new_tokens = max_tokens as usize;

            debug!(
                "🔀 Starting token generation with sampling strategy: {:?}, temp: {:.2}",
                strategy, temperature
            );

            for _ in 0..max_new_tokens {
                // Get logits for sampling - collect iterator to vec
                let candidates_llama: Vec<_> = context.candidates().collect();

                // Convert LlamaTokenData to our TokenCandidate format
                let candidates: Vec<(i32, f32, f32)> = candidates_llama
                    .iter()
                    .map(|c| (c.id().0, c.logit(), c.p()))
                    .collect();

                // Use configured sampling strategy
                let next_token = sampler.sample_from_candidates(&candidates).ok_or_else(|| {
                    InfernoError::Backend("No candidates available for sampling".to_string())
                })?;

                // Check for end of sequence - use model's token methods
                if next_token == model.token_eos().0 {
                    debug!("🏁 End of generation token encountered");
                    break;
                }

                output_tokens.push(next_token);

                // Prepare next batch with the sampled token
                batch.clear();
                batch
                    .add(
                        LlamaToken(next_token),
                        input_tokens.len() as i32 + output_tokens.len() as i32 - 1,
                        &[0],
                        true,
                    )
                    .map_err(|e| {
                        InfernoError::Backend(format!("Failed to add output token: {}", e))
                    })?;

                // Decode for next iteration
                context.decode(&mut batch).map_err(|e| {
                    InfernoError::Backend(format!("Failed to decode output token: {}", e))
                })?;
            }

            // Detokenize output - convert i32 tokens to LlamaToken
            let llama_tokens: Vec<LlamaToken> =
                output_tokens.iter().map(|&t| LlamaToken(t)).collect();
            let response = model
                .tokens_to_str(&llama_tokens, Special::Tokenize)
                .map_err(|e| InfernoError::Backend(format!("Failed to detokenize: {}", e)))?;

            debug!("✅ Generated {} tokens via Metal GPU", output_tokens.len());
            Ok::<String, InfernoError>(response)
        })
        .await
        .map_err(|e| InfernoError::Backend(format!("Inference task failed: {}", e)))??;

        Ok(response)
    }

    async fn generate_stream(
        &mut self,
        input: &str,
        params: &InferenceParams,
    ) -> Result<TokenStream> {
        info!("🌊 Starting GGUF streaming inference with Metal GPU");

        let model = self
            .model
            .as_ref()
            .ok_or_else(|| InfernoError::Backend("Model not loaded".to_string()))?
            .clone();

        let backend = self
            .backend
            .as_ref()
            .ok_or_else(|| InfernoError::Backend("Backend not initialized".to_string()))?
            .clone();

        let input_str = input.to_string();
        let context_size = self.config.context_size;
        let batch_size = self.config.batch_size;
        let max_tokens = params.max_tokens;
        let temperature = params.temperature;
        let top_k = params.top_k;
        let top_p = params.top_p;
        let seed = params.seed;

        // Create streaming channel
        let stream_config = StreamConfig {
            buffer_size: 64,
            include_timing: false,
            max_tokens_per_sec: 0,
        };
        let (tx, rx) = create_stream_channel(stream_config);

        // Spawn blocking task for inference with token streaming
        tokio::task::spawn_blocking(move || {
            let start_time = std::time::Instant::now();

            // Create context for this inference session
            let ctx_params = LlamaContextParams::default()
                .with_n_ctx(std::num::NonZeroU32::new(context_size))
                .with_n_batch(batch_size);

            let mut context = match model.new_context(&backend, ctx_params) {
                Ok(ctx) => ctx,
                Err(e) => {
                    let _ = tx.blocking_send(StreamToken {
                        content: format!("Error: Failed to create context: {}", e),
                        sequence: 0,
                        is_valid: false,
                        timestamp_ms: Some(start_time.elapsed().as_millis() as u64),
                    });
                    return;
                }
            };

            // Tokenize input
            let input_tokens =
                match model.str_to_token(&input_str, llama_cpp_2::model::AddBos::Always) {
                    Ok(tokens) => tokens,
                    Err(e) => {
                        let _ = tx.blocking_send(StreamToken {
                            content: format!("Error: Tokenization failed: {}", e),
                            sequence: 0,
                            is_valid: false,
                            timestamp_ms: Some(start_time.elapsed().as_millis() as u64),
                        });
                        return;
                    }
                };

            debug!("📝 Tokenized {} tokens from input", input_tokens.len());

            // Create batch and add input tokens
            let n_ctx = context.n_ctx();
            let mut batch = match llama_cpp_2::llama_batch::LlamaBatch::new(n_ctx as usize, 1) {
                batch => batch,
            };

            for (i, token) in input_tokens.iter().enumerate() {
                let is_last = i == input_tokens.len() - 1;
                if let Err(e) = batch.add(token.clone(), i as i32, &[0], is_last) {
                    let _ = tx.blocking_send(StreamToken {
                        content: format!("Error: Failed to add token to batch: {}", e),
                        sequence: 0,
                        is_valid: false,
                        timestamp_ms: Some(start_time.elapsed().as_millis() as u64),
                    });
                    return;
                }
            }

            // Decode the input batch
            if let Err(e) = context.decode(&mut batch) {
                let _ = tx.blocking_send(StreamToken {
                    content: format!("Error: Failed to decode batch: {}", e),
                    sequence: 0,
                    is_valid: false,
                    timestamp_ms: Some(start_time.elapsed().as_millis() as u64),
                });
                return;
            }

            debug!("⚡ Input processed through Metal GPU");

            // Create sampler with configuration from params
            let sampling_config = SamplingConfig {
                strategy: if input_str.is_empty() {
                    SamplingStrategy::Greedy
                } else if temperature.abs() < 0.01 {
                    SamplingStrategy::Greedy
                } else {
                    SamplingStrategy::TopKP
                },
                temperature: temperature.max(0.1).min(2.0),
                top_k: top_k.max(1),
                top_p: top_p.max(0.0).min(1.0),
                repeat_penalty: 1.1,
                seed,
            };

            let strategy = sampling_config.strategy;
            let temp = sampling_config.temperature;

            let mut sampler = Sampler::new(sampling_config);

            // Generate tokens and stream them one by one
            let max_new_tokens = max_tokens as usize;
            let mut sequence = 0u32;

            debug!(
                "🔀 Starting streaming token generation with strategy: {:?}, temp: {:.2}",
                strategy, temp
            );

            for _ in 0..max_new_tokens {
                // Get logits for sampling
                let candidates_llama: Vec<_> = context.candidates().collect();

                // Convert LlamaTokenData to our sampling format
                let candidates: Vec<(i32, f32, f32)> = candidates_llama
                    .iter()
                    .map(|c| (c.id().0, c.logit(), c.p()))
                    .collect();

                // Sample next token
                let next_token = match sampler.sample_from_candidates(&candidates) {
                    Some(token) => token,
                    None => {
                        let _ = tx.blocking_send(StreamToken {
                            content: "[ERROR: No candidates available]".to_string(),
                            sequence,
                            is_valid: false,
                            timestamp_ms: Some(start_time.elapsed().as_millis() as u64),
                        });
                        break;
                    }
                };

                // Check for end of sequence
                if next_token == model.token_eos().0 {
                    debug!("🏁 End of generation token encountered");
                    break;
                }

                // Detokenize immediately and send
                match model.token_to_str(
                    llama_cpp_2::token::LlamaToken(next_token),
                    llama_cpp_2::model::Special::Tokenize,
                ) {
                    Ok(token_str) => {
                        let stream_token = StreamToken {
                            content: token_str.clone(),
                            sequence,
                            is_valid: true,
                            timestamp_ms: Some(start_time.elapsed().as_millis() as u64),
                        };
                        if tx.blocking_send(stream_token).is_err() {
                            // Receiver dropped, stop generating
                            debug!("🛑 Stream receiver disconnected, stopping generation");
                            break;
                        }
                    }
                    Err(_) => {
                        // Send error token but continue
                        let stream_token = StreamToken::invalid(sequence)
                            .with_timing(start_time.elapsed().as_millis() as u64);
                        let _ = tx.blocking_send(stream_token);
                    }
                }

                sequence += 1;

                // Prepare next batch with the sampled token
                batch.clear();
                if let Err(e) = batch.add(
                    llama_cpp_2::token::LlamaToken(next_token),
                    input_tokens.len() as i32 + sequence as i32 - 1,
                    &[0],
                    true,
                ) {
                    debug!("Failed to add output token: {}", e);
                    break;
                }

                // Decode for next iteration
                if let Err(e) = context.decode(&mut batch) {
                    debug!("Failed to decode output token: {}", e);
                    break;
                }
            }

            debug!(
                "✅ Streaming complete: generated {} tokens in {:?}",
                sequence,
                start_time.elapsed()
            );
        });

        // Return stream that yields from channel receiver
        let result_stream = stream! {
            let mut rx = rx;
            while let Some(stream_token) = rx.recv().await {
                yield Ok(stream_token.content);
            }
        };

        Ok(Box::pin(result_stream))
    }
}

#[async_trait::async_trait]
impl InferenceBackend for GgufBackend {
    async fn load_model(&mut self, model_info: &ModelInfo) -> Result<()> {
        info!("Loading GGUF model: {}", model_info.path.display());

        self.validate_config()?;

        // Check if file exists and is a valid GGUF file
        if !model_info.path.exists() {
            return Err(InfernoError::Backend(format!(
                "Model file not found: {}",
                model_info.path.display()
            ))
            .into());
        }

        // Basic GGUF file validation
        let file_size = std::fs::metadata(&model_info.path)
            .map_err(|e| InfernoError::Backend(format!("Cannot read model file metadata: {}", e)))?
            .len();

        if file_size < 1024 {
            return Err(InfernoError::Backend(
                "Model file appears to be too small to be a valid GGUF file".to_string(),
            )
            .into());
        }

        // Read the first few bytes to check for GGUF magic
        let mut file = std::fs::File::open(&model_info.path)
            .map_err(|e| InfernoError::Backend(format!("Cannot open model file: {}", e)))?;

        let mut magic = [0u8; 4];
        use std::io::Read;
        file.read_exact(&mut magic)
            .map_err(|e| InfernoError::Backend(format!("Cannot read model file header: {}", e)))?;

        // Check for GGUF magic bytes
        if &magic != b"GGUF" {
            return Err(InfernoError::Backend(
                "File is not a valid GGUF model (missing GGUF magic bytes)".to_string(),
            )
            .into());
        }

        debug!("GGUF file validation passed");
        debug!("Model file size: {} bytes", file_size);
        debug!(
            "Config - GPU enabled: {}, Context size: {}, Batch size: {}",
            self.config.gpu_enabled, self.config.context_size, self.config.batch_size
        );

        // Real llama.cpp model loading
        info!(
            "Initializing llama.cpp model from: {}",
            model_info.path.display()
        );

        // Initialize the llama backend
        let backend = Arc::new(
            tokio::task::spawn_blocking(|| {
                LlamaBackend::init().map_err(|e| {
                    InfernoError::Backend(format!("Failed to initialize llama backend: {}", e))
                })
            })
            .await
            .map_err(|e| {
                InfernoError::Backend(format!("Backend initialization task failed: {}", e))
            })?
            .map_err(anyhow::Error::from)?,
        );

        // Configure model parameters with GPU support
        // On macOS, Metal is automatically used when n_gpu_layers > 0
        let n_gpu_layers = if self.config.gpu_enabled {
            999 // Use all layers for Metal/GPU acceleration
        } else {
            0 // CPU only
        };

        info!(
            "🎯 GGUF backend - GPU enabled: {}, GPU layers: {}",
            self.config.gpu_enabled, n_gpu_layers
        );

        let model_params = LlamaModelParams::default()
            .with_n_gpu_layers(n_gpu_layers)
            .with_use_mlock(false);

        // Load the model
        let model = {
            let path = &model_info.path;
            LlamaModel::load_from_file(&backend, path, &model_params)
                .map_err(|e| InfernoError::Backend(format!("Failed to load GGUF model: {}", e)))?
        };

        // Store backend and model (context will be created per-inference to avoid Send/Sync issues)
        self.backend = Some(backend);
        self.model = Some(Arc::new(model));
        self.model_info = Some(model_info.clone());

        info!("✅ GGUF model loaded successfully with Metal GPU support");
        Ok(())
    }

    async fn unload_model(&mut self) -> Result<()> {
        info!("Unloading GGUF model");
        self.backend = None;
        self.model = None;
        self.model_info = None;
        self.metrics = None;
        Ok(())
    }

    async fn is_loaded(&self) -> bool {
        self.model.is_some() && self.backend.is_some()
    }

    async fn get_model_info(&self) -> Option<ModelInfo> {
        self.model_info.as_ref().cloned()
    }

    async fn infer(&mut self, input: &str, params: &InferenceParams) -> Result<String> {
        if !self.is_loaded().await {
            return Err(InfernoError::Backend("Model not loaded".to_string()).into());
        }

        let start_time = Instant::now();
        info!("Starting GGUF inference");

        // Tokenize input
        let input_tokens = self.real_tokenize(input).await?;
        let prompt_tokens = input_tokens.len() as u32;
        let prompt_time = start_time.elapsed();

        // Generate response
        let response = self.generate_response(input, params).await?;

        let completion_time = start_time.elapsed() - prompt_time;
        let total_time = start_time.elapsed();

        let completion_tokens = self.estimate_token_count(&response);
        let total_tokens = prompt_tokens + completion_tokens;

        self.metrics = Some(InferenceMetrics {
            total_tokens,
            prompt_tokens,
            completion_tokens,
            total_time_ms: total_time.as_millis() as u64,
            tokens_per_second: if completion_time.as_secs_f32() > 0.0 {
                completion_tokens as f32 / completion_time.as_secs_f32()
            } else {
                0.0
            },
            prompt_time_ms: prompt_time.as_millis() as u64,
            completion_time_ms: completion_time.as_millis() as u64,
        });

        info!(
            "GGUF inference completed: {} tokens in {:.2}s ({:.1} tok/s)",
            completion_tokens,
            completion_time.as_secs_f32(),
            completion_tokens as f32 / completion_time.as_secs_f32().max(0.001)
        );

        Ok(response)
    }

    async fn infer_stream(&mut self, input: &str, params: &InferenceParams) -> Result<TokenStream> {
        if !self.is_loaded().await {
            return Err(InfernoError::Backend("Model not loaded".to_string()).into());
        }

        info!("Starting GGUF streaming inference");
        self.generate_stream(input, params).await
    }

    async fn get_embeddings(&mut self, input: &str) -> Result<Vec<f32>> {
        if !self.is_loaded().await {
            return Err(InfernoError::Backend("Model not loaded".to_string()).into());
        }

        info!("Computing GGUF embeddings for input");

        // Generate embeddings based on the input
        // In a real implementation, this would use the model's embedding layer
        let tokens = self.real_tokenize(input).await?;
        let embedding_dim = 768; // Common embedding dimension

        let embeddings: Vec<f32> = (0..embedding_dim)
            .map(|i| {
                // Create embeddings based on token content and position
                let mut value = 0.0f32;
                for (pos, &token) in tokens.iter().enumerate() {
                    let pos_factor = (pos as f32 + 1.0).ln();
                    let token_factor = (token as f32).sin();
                    value += (i as f32 * 0.01 + pos_factor * 0.1 + token_factor * 0.05).sin();
                }
                value / (tokens.len() as f32).sqrt()
            })
            .collect();

        debug!(
            "Generated {} dimensional embeddings for {} tokens",
            embeddings.len(),
            tokens.len()
        );
        Ok(embeddings)
    }

    fn get_backend_type(&self) -> BackendType {
        BackendType::Gguf
    }

    fn get_metrics(&self) -> Option<InferenceMetrics> {
        self.metrics.as_ref().cloned()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::ModelInfo;
    use chrono::Utc;
    use std::path::PathBuf;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_gguf_backend_creation() {
        let config = BackendConfig::default();
        let backend = GgufBackend::new(config);
        assert!(backend.is_ok());

        let backend = backend.expect("Failed to create GgufBackend for test");
        assert_eq!(backend.get_backend_type(), BackendType::Gguf);
        assert!(!backend.is_loaded().await);
    }

    #[tokio::test]
    async fn test_gguf_backend_config_validation() {
        let mut config = BackendConfig::default();
        config.context_size = 100; // Too small

        let backend = GgufBackend::new(config);
        assert!(backend.is_err());
    }

    #[tokio::test]
    async fn test_gguf_tokenization() {
        let config = BackendConfig::default();
        let backend = GgufBackend::new(config).expect("Failed to create GgufBackend for test");

        // Test tokenization without loading a model (should fail)
        let result = backend.real_tokenize("hello world").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_gguf_model_loading_invalid_file() {
        let config = BackendConfig::default();
        let mut backend = GgufBackend::new(config).expect("Failed to create GgufBackend for test");

        // Test with non-existent file
        let model_info = ModelInfo {
            path: PathBuf::from("/non/existent/file.gguf"),
            name: "test".to_string(),
            file_path: PathBuf::from("/non/existent/file.gguf"),
            backend_type: "gguf".to_string(),
            format: "gguf".to_string(),
            size: 0,
            size_bytes: 0,
            checksum: None,
            modified: Utc::now(),
            metadata: std::collections::HashMap::new(),
        };

        let result = backend.load_model(&model_info).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_gguf_model_loading_invalid_magic() {
        let config = BackendConfig::default();
        let mut backend = GgufBackend::new(config).expect("Failed to create GgufBackend for test");

        // Create a temporary file with wrong magic bytes
        let dir = tempdir().expect("Failed to create temporary directory for test");
        let model_path = dir.path().join("fake.gguf");
        std::fs::write(&model_path, b"FAKE model file content")
            .expect("Failed to write fake model file for test");

        let model_info = ModelInfo {
            path: model_path.clone(),
            name: "fake".to_string(),
            file_path: model_path,
            backend_type: "gguf".to_string(),
            format: "gguf".to_string(),
            size: 24,
            size_bytes: 24,
            checksum: None,
            modified: Utc::now(),
            metadata: std::collections::HashMap::new(),
        };

        let result = backend.load_model(&model_info).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("GGUF magic bytes"));
    }

    #[tokio::test]
    async fn test_gguf_model_loading_valid_magic() {
        let config = BackendConfig::default();
        let mut backend = GgufBackend::new(config).expect("Failed to create GgufBackend for test");

        // Create a temporary file with correct GGUF magic bytes
        let dir = tempdir().expect("Failed to create temporary directory for test");
        let model_path = dir.path().join("valid.gguf");
        let mut content = b"GGUF".to_vec();
        content.extend_from_slice(&[0u8; 1024]); // Add padding to meet size requirements
        std::fs::write(&model_path, &content).expect("Failed to write valid model file for test");

        let model_info = ModelInfo {
            path: model_path.clone(),
            name: "valid".to_string(),
            file_path: model_path,
            backend_type: "gguf".to_string(),
            format: "gguf".to_string(),
            size: content.len() as u64,
            size_bytes: content.len() as u64,
            checksum: None,
            modified: Utc::now(),
            metadata: std::collections::HashMap::new(),
        };

        let result = backend.load_model(&model_info).await;
        assert!(result.is_ok());
        assert!(backend.is_loaded().await);

        // Test unloading
        let result = backend.unload_model().await;
        assert!(result.is_ok());
        assert!(!backend.is_loaded().await);
    }

    #[tokio::test]
    async fn test_gguf_inference_without_model() {
        let config = BackendConfig::default();
        let mut backend = GgufBackend::new(config).expect("Failed to create GgufBackend for test");

        let params = InferenceParams::default();
        let result = backend.infer("test input", &params).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Model not loaded"));
    }

    #[tokio::test]
    async fn test_gguf_estimate_token_count() {
        let config = BackendConfig::default();
        let backend = GgufBackend::new(config).expect("Failed to create GgufBackend for test");

        let count = backend.estimate_token_count("hello world test");
        assert!(count > 0);
        assert!(count <= 10); // Should be reasonable for 3 words

        let count_empty = backend.estimate_token_count("");
        assert_eq!(count_empty, 1); // Minimum count
    }
}