dakera-inference 0.11.61

Embedded inference engine for Dakera - generates embeddings locally via ONNX Runtime
Documentation
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
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
//! Core embedding engine for generating vector embeddings from text.
//!
//! The `EmbeddingEngine` provides a high-level interface for:
//! - Loading ONNX INT8 embedding models from HuggingFace Hub
//! - Generating embeddings for single texts or batches
//! - Automatic batching and parallel processing via ONNX Runtime
//!
//! # Example
//!
//! ```no_run
//! use inference::{EmbeddingEngine, ModelConfig, EmbeddingModel};
//!
//! #[tokio::main]
//! async fn main() {
//!     let config = ModelConfig::new(EmbeddingModel::MiniLM);
//!     let engine = EmbeddingEngine::new(config).await.unwrap();
//!
//!     // Embed a single query
//!     let embedding = engine.embed_query("What is machine learning?").await.unwrap();
//!     println!("Embedding dimension: {}", embedding.len());
//!
//!     // Embed multiple documents
//!     let docs = vec![
//!         "Machine learning is a subset of AI.".to_string(),
//!         "Deep learning uses neural networks.".to_string(),
//!     ];
//!     let embeddings = engine.embed_documents(&docs).await.unwrap();
//!     println!("Generated {} embeddings", embeddings.len());
//! }
//! ```

use crate::batch::{mean_pooling, normalize_embeddings, BatchProcessor};
use crate::error::{InferenceError, Result};
use crate::models::{EmbeddingModel, ModelConfig};
use ort::inputs;
use ort::session::builder::GraphOptimizationLevel;
use ort::session::Session;
use ort::value::Tensor;
use parking_lot::Mutex;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokenizers::Tokenizer;
use tracing::{debug, info, instrument, warn};

/// The main embedding engine for generating vector embeddings.
///
/// This struct is thread-safe and can be shared across async tasks.
/// ORT sessions are mutex-guarded (run() takes &mut self) and held in a
/// pool so concurrent callers can embed without head-of-line blocking.
/// CPU-heavy inference is offloaded via `tokio::task::spawn_blocking`.
pub struct EmbeddingEngine {
    /// Pool of ONNX Runtime sessions — each guarded independently.
    /// Concurrent callers round-robin across sessions via `next_session`,
    /// eliminating Mutex head-of-line blocking for batch embedding workloads.
    sessions: Vec<Arc<Mutex<Session>>>,
    /// Round-robin counter: atomically incremented per batch, wraps via modulo.
    next_session: AtomicUsize,
    /// Batch processor for tokenization (Arc-wrapped for spawn_blocking)
    processor: Arc<BatchProcessor>,
    /// Model configuration
    config: ModelConfig,
    /// Embedding dimension
    dimension: usize,
}

impl EmbeddingEngine {
    /// Create a new embedding engine with the given configuration.
    ///
    /// Downloads the ONNX INT8 model from HuggingFace Hub if not cached.
    #[instrument(skip_all, fields(model = %config.model))]
    pub async fn new(config: ModelConfig) -> Result<Self> {
        info!(
            "Initializing ONNX embedding engine with model: {}",
            config.model
        );

        // Download tokenizer and ONNX model files
        let (tokenizer_path, onnx_path) = Self::download_model_files(&config).await?;

        // Load tokenizer
        info!("Loading tokenizer from {:?}", tokenizer_path);
        let tokenizer = Tokenizer::from_file(&tokenizer_path)
            .map_err(|e| InferenceError::TokenizationError(e.to_string()))?;

        // Build ONNX session pool — N independent sessions to serve concurrent callers.
        // Each session has its own ORT context so pool members never block each other.
        info!("Loading ONNX model from {:?}", onnx_path);
        let num_threads = config.num_threads.unwrap_or(4);
        let pool_size = config.session_pool_size.max(1);
        let onnx_path_clone = onnx_path.clone();
        let sessions: Vec<Arc<Mutex<Session>>> =
            tokio::task::spawn_blocking(move || -> Result<Vec<Arc<Mutex<Session>>>> {
                (0..pool_size)
                    .map(|_| {
                        let s = Session::builder()
                            .map_err(|e| InferenceError::ModelLoadError(e.to_string()))?
                            .with_optimization_level(GraphOptimizationLevel::Level3)
                            .map_err(|e| InferenceError::ModelLoadError(e.to_string()))?
                            .with_intra_threads(num_threads)
                            .map_err(|e| InferenceError::ModelLoadError(e.to_string()))?
                            .commit_from_file(&onnx_path_clone)
                            .map_err(|e| InferenceError::ModelLoadError(e.to_string()))?;
                        Ok(Arc::new(Mutex::new(s)))
                    })
                    .collect()
            })
            .await
            .map_err(|e| {
                InferenceError::ModelLoadError(format!("Session pool init panicked: {}", e))
            })??;

        let dimension = config.model.dimension();
        let processor = Arc::new(BatchProcessor::new(
            tokenizer,
            config.model,
            config.max_batch_size,
        ));

        info!(
            "ONNX embedding engine ready: model={}, dimension={}, threads={}, pool={}",
            config.model, dimension, num_threads, pool_size
        );

        Ok(Self {
            sessions,
            next_session: AtomicUsize::new(0),
            processor,
            config,
            dimension,
        })
    }

    /// Resolve tokenizer and ONNX model files, downloading from HuggingFace if needed.
    ///
    /// - `tokenizer.json` — from the original model repo (sentence-transformers, BAAI, intfloat)
    /// - `onnx/model_quantized.onnx` — from the Xenova ONNX repo (INT8, pre-built)
    #[instrument(skip_all, fields(model = %config.model))]
    async fn download_model_files(config: &ModelConfig) -> Result<(PathBuf, PathBuf)> {
        let model_id = config.model.model_id();
        let onnx_repo_id = config.model.onnx_repo_id();
        let onnx_filename = config.model.onnx_filename();

        info!(
            "Resolving model files: tokenizer={}, onnx={}@{}",
            model_id, onnx_filename, onnx_repo_id
        );

        let tokenizer_cache_dir = Self::model_cache_dir(model_id)?;
        let onnx_cache_dir = Self::model_cache_dir(onnx_repo_id)?;

        // ONNX sub-directory mirrors the path within the repo (e.g. "onnx/")
        let onnx_subdir = onnx_cache_dir.join("onnx");
        std::fs::create_dir_all(&onnx_subdir)?;

        let local_tokenizer = tokenizer_cache_dir.join("tokenizer.json");
        // onnx_filename is "onnx/model_quantized.onnx" — basename is the last component
        let onnx_basename = Path::new(onnx_filename)
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("model_quantized.onnx");
        let local_onnx = onnx_subdir.join(onnx_basename);

        // Download missing files in a blocking thread
        let tokenizer_needs_download = !local_tokenizer.exists();
        let onnx_needs_download = !local_onnx.exists();

        if tokenizer_needs_download || onnx_needs_download {
            let model_id_owned = model_id.to_string();
            let onnx_repo_id_owned = onnx_repo_id.to_string();
            let onnx_filename_owned = onnx_filename.to_string();
            let tokenizer_cache = tokenizer_cache_dir.clone();
            let onnx_cache = onnx_cache_dir.clone();

            tokio::task::spawn_blocking(move || {
                if !tokenizer_cache.join("tokenizer.json").exists() {
                    Self::download_hf_file(&model_id_owned, "tokenizer.json", &tokenizer_cache)
                        .map_err(|e| {
                            InferenceError::HubError(format!("Failed to download tokenizer: {}", e))
                        })?;
                }
                if !onnx_cache.join(&onnx_filename_owned).exists() {
                    Self::download_hf_file(&onnx_repo_id_owned, &onnx_filename_owned, &onnx_cache)
                        .map_err(|e| {
                            InferenceError::HubError(format!(
                                "Failed to download ONNX model: {}",
                                e
                            ))
                        })?;
                }
                Ok::<_, InferenceError>(())
            })
            .await
            .map_err(|e| InferenceError::HubError(format!("Download task panicked: {}", e)))??;
        } else {
            info!("All model files found in local cache");
        }

        // Re-derive paths (cache dir / onnx / basename)
        let final_onnx = onnx_cache_dir.join(onnx_filename);

        info!(
            "Model files ready: tokenizer={:?}, onnx={:?}",
            local_tokenizer, final_onnx
        );
        Ok((local_tokenizer, final_onnx))
    }

    /// Get or create the local model cache directory.
    fn model_cache_dir(model_id: &str) -> Result<PathBuf> {
        let base = std::env::var("HF_HOME")
            .map(PathBuf::from)
            .unwrap_or_else(|_| {
                let home = std::env::var("HOME").unwrap_or_else(|_| {
                    warn!("HOME environment variable not set, using /tmp for model cache");
                    "/tmp".to_string()
                });
                PathBuf::from(home).join(".cache").join("huggingface")
            });
        let dir = base.join("dakera").join(model_id.replace('/', "--"));
        std::fs::create_dir_all(&dir)?;
        Ok(dir)
    }

    /// Download a single file from HuggingFace using ureq (sync, for spawn_blocking).
    ///
    /// Handles relative Location headers that ureq 2.x cannot resolve automatically.
    ///
    /// Public alias for use by other inference modules (e.g. GLiNER NER engine).
    pub fn download_hf_file_pub(
        model_id: &str,
        filename: &str,
        cache_dir: &Path,
    ) -> std::result::Result<PathBuf, String> {
        Self::download_hf_file(model_id, filename, cache_dir)
    }

    fn download_hf_file(
        model_id: &str,
        filename: &str,
        cache_dir: &Path,
    ) -> std::result::Result<PathBuf, String> {
        // The file may be nested (e.g. "onnx/model_quantized.onnx")
        let file_path = cache_dir.join(filename);
        if file_path.exists() {
            info!("Cached: {}/{}", model_id, filename);
            return Ok(file_path);
        }

        // Ensure parent directory exists (for "onnx/model_quantized.onnx")
        if let Some(parent) = file_path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("Failed to create directory {:?}: {}", parent, e))?;
        }

        let url = format!(
            "https://huggingface.co/{}/resolve/main/{}",
            model_id, filename
        );
        info!("Downloading: {}", url);

        // Read HuggingFace token from env (required for gated models).
        let hf_token = std::env::var("HF_TOKEN")
            .or_else(|_| std::env::var("HUGGING_FACE_HUB_TOKEN"))
            .ok();
        if hf_token.is_some() {
            info!("Using HuggingFace auth token for download");
        }

        // Disable automatic redirects so we can resolve relative Location headers ourselves.
        let agent = ureq::AgentBuilder::new()
            .redirects(0)
            .timeout(std::time::Duration::from_secs(300))
            .build();

        let mut current_url = url.clone();
        let mut redirects = 0;
        let max_redirects = 10;

        let response = loop {
            let mut req = agent.get(&current_url);
            if let Some(ref token) = hf_token {
                req = req.set("Authorization", &format!("Bearer {}", token));
            }
            let resp = req.call();

            let r = match resp {
                Ok(r) => r,
                Err(ureq::Error::Status(_status, r)) => r,
                Err(e) => return Err(format!("{}: {}", filename, e)),
            };

            let status = r.status();
            if (200..300).contains(&status) {
                break r;
            } else if (300..400).contains(&status) {
                redirects += 1;
                if redirects > max_redirects {
                    return Err(format!("{}: too many redirects", filename));
                }
                let location = r
                    .header("location")
                    .ok_or_else(|| format!("{}: redirect without Location header", filename))?
                    .to_string();

                // Resolve relative redirects against the current URL's origin
                current_url = if location.starts_with('/') {
                    let parsed = url::Url::parse(&current_url)
                        .map_err(|e| format!("{}: bad URL {}: {}", filename, current_url, e))?;
                    let host = parsed.host_str().ok_or_else(|| {
                        format!("{}: redirect URL missing host: {}", filename, current_url)
                    })?;
                    format!("{}://{}{}", parsed.scheme(), host, location)
                } else {
                    location
                };
                info!("Redirect {} → {}", redirects, current_url);
            } else {
                return Err(format!("{}: HTTP {}", filename, status));
            }
        };

        let mut bytes = Vec::new();
        response
            .into_reader()
            .take(500_000_000) // 500 MB safety limit
            .read_to_end(&mut bytes)
            .map_err(|e| format!("Failed to read {}: {}", filename, e))?;

        std::fs::write(&file_path, &bytes)
            .map_err(|e| format!("Failed to write {}: {}", filename, e))?;

        info!("Downloaded {} ({} bytes)", filename, bytes.len());
        Ok(file_path)
    }

    /// Get the embedding dimension for the loaded model.
    pub fn dimension(&self) -> usize {
        self.dimension
    }

    /// Get the model being used.
    pub fn model(&self) -> EmbeddingModel {
        self.config.model
    }

    /// Get the number of parallel ONNX sessions in the pool.
    pub fn pool_size(&self) -> usize {
        self.sessions.len()
    }

    /// Embed a single query text.
    ///
    /// For models like E5, this automatically applies the query prefix.
    #[instrument(skip(self, text), fields(text_len = text.len()))]
    pub async fn embed_query(&self, text: &str) -> Result<Vec<f32>> {
        let texts = vec![text.to_string()];
        let prepared = self.processor.prepare_texts(&texts, true);
        let embeddings = self.embed_batch_internal(&prepared).await?;
        embeddings.into_iter().next().ok_or_else(|| {
            InferenceError::InferenceError("No embedding returned for query".to_string())
        })
    }

    /// Embed multiple query texts.
    ///
    /// For models like E5, this automatically applies the query prefix.
    #[instrument(skip(self, texts), fields(count = texts.len()))]
    pub async fn embed_queries(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        let prepared = self.processor.prepare_texts(texts, true);
        self.embed_batch_internal(&prepared).await
    }

    /// Embed a single document/passage.
    ///
    /// For models like E5, this automatically applies the document prefix.
    #[instrument(skip(self, text), fields(text_len = text.len()))]
    pub async fn embed_document(&self, text: &str) -> Result<Vec<f32>> {
        let texts = vec![text.to_string()];
        let prepared = self.processor.prepare_texts(&texts, false);
        let embeddings = self.embed_batch_internal(&prepared).await?;
        embeddings.into_iter().next().ok_or_else(|| {
            InferenceError::InferenceError("No embedding returned for document".to_string())
        })
    }

    /// Embed multiple documents/passages.
    ///
    /// For models like E5, this automatically applies the document prefix.
    #[instrument(skip(self, texts), fields(count = texts.len()))]
    pub async fn embed_documents(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        let prepared = self.processor.prepare_texts(texts, false);
        self.embed_batch_internal(&prepared).await
    }

    /// Embed texts without any prefix (raw embedding).
    #[instrument(skip(self, texts), fields(count = texts.len()))]
    pub async fn embed_raw(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        self.embed_batch_internal(texts).await
    }

    /// Internal batch embedding implementation.
    ///
    /// Splits `texts` into sub-batches (≤ max_batch_size each) and distributes
    /// them across the session pool via round-robin. All sub-batches are spawned
    /// concurrently — sessions[i % pool_len] serializes only its own sub-batches,
    /// so pool_len sub-batches run in true parallel, eliminating head-of-line
    /// blocking when multiple HTTP handlers embed concurrently (DAK-5547).
    async fn embed_batch_internal(&self, texts: &[String]) -> Result<Vec<Vec<f32>>> {
        if texts.is_empty() {
            return Ok(vec![]);
        }

        let batches: Vec<Vec<String>> = self
            .processor
            .split_into_batches(texts)
            .into_iter()
            .map(|b| b.to_vec())
            .collect();

        let pool_len = self.sessions.len();
        let normalize = self.config.model.normalize_embeddings();
        // Round-robin starting index: each concurrent caller gets a different slot so
        // concurrent requests don't all contend on sessions[0].
        let start_idx = self.next_session.fetch_add(1, Ordering::Relaxed);

        // Spawn all sub-batches concurrently; preserve insertion order for reassembly.
        let mut handles = Vec::with_capacity(batches.len());
        for (i, batch_owned) in batches.into_iter().enumerate() {
            let session = Arc::clone(&self.sessions[(start_idx + i) % pool_len]);
            let processor = Arc::clone(&self.processor);
            handles.push(tokio::task::spawn_blocking(move || {
                let mut session_guard = session.lock();
                Self::process_batch_blocking(
                    &batch_owned,
                    &mut session_guard,
                    &processor,
                    normalize,
                )
            }));
        }

        let mut all_embeddings = Vec::with_capacity(texts.len());
        for handle in handles {
            let batch_embeddings = handle.await.map_err(|e| {
                InferenceError::InferenceError(format!("Inference task panicked: {}", e))
            })??;
            all_embeddings.extend(batch_embeddings);
        }

        Ok(all_embeddings)
    }

    /// Process a single batch: tokenize → ORT session → mean pool → normalize.
    ///
    /// Designed to run inside `spawn_blocking` (takes no `&self`).
    fn process_batch_blocking(
        texts: &[String],
        session: &mut Session,
        processor: &BatchProcessor,
        normalize: bool,
    ) -> Result<Vec<Vec<f32>>> {
        // Tokenize
        let prepared = processor.tokenize_batch(texts)?;
        let batch_size = prepared.batch_size;
        let seq_len = prepared.seq_len;

        // Keep a copy of attention_mask for mean_pooling (consumed by Tensor below)
        let attention_mask_flat = prepared.attention_mask.clone();

        // Build ORT tensors — from_array requires (shape, Vec<T>) in ort rc.12
        let input_ids_tensor =
            Tensor::<i64>::from_array(([batch_size, seq_len], prepared.input_ids))
                .map_err(|e| InferenceError::InferenceError(e.to_string()))?;
        let attention_mask_tensor =
            Tensor::<i64>::from_array(([batch_size, seq_len], prepared.attention_mask))
                .map_err(|e| InferenceError::InferenceError(e.to_string()))?;
        let token_type_ids_tensor =
            Tensor::<i64>::from_array(([batch_size, seq_len], prepared.token_type_ids))
                .map_err(|e| InferenceError::InferenceError(e.to_string()))?;

        // Run ONNX session
        let outputs = session
            .run(inputs![
                "input_ids" => input_ids_tensor,
                "attention_mask" => attention_mask_tensor,
                "token_type_ids" => token_type_ids_tensor
            ])
            .map_err(|e: ort::Error| InferenceError::InferenceError(e.to_string()))?;

        // Extract last_hidden_state: shape [batch, seq_len, hidden_size]
        // ort rc.12: try_extract_tensor returns (&Shape, &[T])
        // Shape derefs to [i64], so index directly.
        let (ort_shape, lhs_slice) = outputs[0]
            .try_extract_tensor::<f32>()
            .map_err(|e| InferenceError::InferenceError(e.to_string()))?;

        if ort_shape.len() != 3 {
            return Err(InferenceError::InferenceError(format!(
                "Expected 3D last_hidden_state, got {} dims",
                ort_shape.len()
            )));
        }
        let hidden_size = ort_shape[2] as usize;

        // Apply mean pooling using the saved attention mask copy
        let mut embeddings = mean_pooling(
            lhs_slice,
            batch_size,
            seq_len,
            hidden_size,
            &attention_mask_flat,
        );

        // L2 normalize if configured
        if normalize {
            normalize_embeddings(&mut embeddings);
        }

        debug!(
            "Generated {} embeddings of dimension {}",
            embeddings.len(),
            embeddings.first().map(|e| e.len()).unwrap_or(0)
        );

        Ok(embeddings)
    }

    /// Estimate the time to embed a batch of texts (in milliseconds).
    pub fn estimate_time_ms(&self, text_count: usize, avg_text_len: usize) -> f64 {
        // Rough estimation based on model speed and text length (CPU path)
        let tokens_per_text =
            (avg_text_len as f64 / 4.0).min(self.config.model.max_seq_length() as f64);
        let total_tokens = tokens_per_text * text_count as f64;
        let tokens_per_second = self.config.model.tokens_per_second_cpu() as f64;
        (total_tokens / tokens_per_second) * 1000.0
    }
}

impl std::fmt::Debug for EmbeddingEngine {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EmbeddingEngine")
            .field("model", &self.config.model)
            .field("dimension", &self.dimension)
            .field("max_batch_size", &self.config.max_batch_size)
            .field("session_pool_size", &self.sessions.len())
            .finish()
    }
}

/// Builder for creating an EmbeddingEngine with fluent API.
pub struct EmbeddingEngineBuilder {
    config: ModelConfig,
}

impl EmbeddingEngineBuilder {
    /// Create a new builder with default configuration.
    pub fn new() -> Self {
        Self {
            config: ModelConfig::default(),
        }
    }

    /// Set the embedding model to use.
    pub fn model(mut self, model: EmbeddingModel) -> Self {
        self.config.model = model;
        self
    }

    /// Set the cache directory for model files.
    pub fn cache_dir(mut self, dir: impl Into<String>) -> Self {
        self.config.cache_dir = Some(dir.into());
        self
    }

    /// Set the maximum batch size.
    pub fn max_batch_size(mut self, size: usize) -> Self {
        self.config.max_batch_size = size;
        self
    }

    /// Enable GPU acceleration (reserved for future use; ORT selects the execution provider).
    pub fn use_gpu(mut self, enable: bool) -> Self {
        self.config.use_gpu = enable;
        self
    }

    /// Set the number of intra-op CPU threads for ORT inference.
    pub fn num_threads(mut self, threads: usize) -> Self {
        self.config.num_threads = Some(threads);
        self
    }

    /// Set the number of parallel ONNX sessions in the pool.
    pub fn session_pool_size(mut self, size: usize) -> Self {
        self.config.session_pool_size = size.max(1);
        self
    }

    /// Build the embedding engine.
    pub async fn build(self) -> Result<EmbeddingEngine> {
        EmbeddingEngine::new(self.config).await
    }
}

impl Default for EmbeddingEngineBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_estimate_time() {
        let config = ModelConfig::new(EmbeddingModel::MiniLM);
        let tokens_per_second = config.model.tokens_per_second_cpu() as f64;
        assert!(tokens_per_second > 0.0);
    }

    #[test]
    fn test_builder() {
        let builder = EmbeddingEngineBuilder::new()
            .model(EmbeddingModel::BgeSmall)
            .max_batch_size(64)
            .use_gpu(false);

        assert_eq!(builder.config.model, EmbeddingModel::BgeSmall);
        assert_eq!(builder.config.max_batch_size, 64);
        assert!(!builder.config.use_gpu);
    }

    // ── model_cache_dir ──────────────────────────────────────────────────────

    /// Ensure `model_cache_dir` respects `HF_HOME` when set.
    ///
    /// Uses a process-level mutex because `std::env::set_var` is not thread-safe.
    #[test]
    fn test_model_cache_dir_with_hf_home() {
        use std::sync::Mutex;
        static ENV_LOCK: Mutex<()> = Mutex::new(());
        let _guard = ENV_LOCK.lock().unwrap();

        let tmp = std::env::temp_dir().join("dakera_test_hf_home");
        std::env::set_var("HF_HOME", &tmp);
        let result = EmbeddingEngine::model_cache_dir("org/my-model");
        std::env::remove_var("HF_HOME");

        let path = result.unwrap();
        assert!(
            path.starts_with(&tmp),
            "expected path under {tmp:?}, got {path:?}"
        );
        assert!(
            path.to_str().unwrap().contains("org--my-model"),
            "model_id separator not applied: {path:?}"
        );
    }

    #[test]
    fn test_model_cache_dir_contains_dakera_subdir() {
        let path =
            EmbeddingEngine::model_cache_dir("sentence-transformers/all-MiniLM-L6-v2").unwrap();
        let s = path.to_str().unwrap();
        assert!(s.contains("dakera"), "expected 'dakera' in path: {s}");
        assert!(
            s.contains("sentence-transformers--all-MiniLM-L6-v2"),
            "expected transformed model id in path: {s}"
        );
    }

    #[test]
    fn test_model_cache_dir_creates_directory() {
        let dir = EmbeddingEngine::model_cache_dir("test/cache-dir-creation-probe").unwrap();
        assert!(dir.exists(), "model_cache_dir should create the directory");
    }

    // ── download_hf_file (cached early-return path) ──────────────────────────

    #[test]
    fn test_download_hf_file_returns_path_when_already_cached() {
        let tmp = std::env::temp_dir().join("dakera_test_cached_file");
        std::fs::create_dir_all(&tmp).unwrap();
        let file_path = tmp.join("config.json");
        std::fs::write(&file_path, b"{}").unwrap();

        let result = EmbeddingEngine::download_hf_file("test/model", "config.json", &tmp);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), file_path);
    }

    #[test]
    fn test_download_hf_file_returns_correct_path_for_cached_onnx() {
        let tmp = std::env::temp_dir().join("dakera_test_cached_onnx");
        let onnx_dir = tmp.join("onnx");
        std::fs::create_dir_all(&onnx_dir).unwrap();
        let onnx_path = onnx_dir.join("model_quantized.onnx");
        std::fs::write(&onnx_path, b"fake_onnx_model").unwrap();

        // Filename includes the subdirectory path
        let result = EmbeddingEngine::download_hf_file(
            "Xenova/all-MiniLM-L6-v2",
            "onnx/model_quantized.onnx",
            &tmp,
        );
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), onnx_path);
    }

    // ── EmbeddingEngineBuilder ───────────────────────────────────────────────

    #[test]
    fn test_builder_default_impl() {
        let b1 = EmbeddingEngineBuilder::new();
        let b2 = EmbeddingEngineBuilder::default();
        assert_eq!(b1.config.model, b2.config.model);
        assert_eq!(b1.config.max_batch_size, b2.config.max_batch_size);
    }

    #[test]
    fn test_builder_model_field() {
        let builder = EmbeddingEngineBuilder::new().model(EmbeddingModel::E5Small);
        assert_eq!(builder.config.model, EmbeddingModel::E5Small);
    }

    #[test]
    fn test_builder_cache_dir() {
        let builder = EmbeddingEngineBuilder::new().cache_dir("/tmp/my-models");
        assert_eq!(builder.config.cache_dir, Some("/tmp/my-models".to_string()));
    }

    #[test]
    fn test_builder_max_batch_size() {
        let builder = EmbeddingEngineBuilder::new().max_batch_size(128);
        assert_eq!(builder.config.max_batch_size, 128);
    }

    #[test]
    fn test_builder_use_gpu_true() {
        let builder = EmbeddingEngineBuilder::new().use_gpu(true);
        assert!(builder.config.use_gpu);
    }

    #[test]
    fn test_builder_use_gpu_false() {
        let builder = EmbeddingEngineBuilder::new().use_gpu(false);
        assert!(!builder.config.use_gpu);
    }

    #[test]
    fn test_builder_num_threads() {
        let builder = EmbeddingEngineBuilder::new().num_threads(4);
        assert_eq!(builder.config.num_threads, Some(4));
    }

    #[test]
    fn test_builder_chain_all_fields() {
        let builder = EmbeddingEngineBuilder::new()
            .model(EmbeddingModel::BgeSmall)
            .cache_dir("/cache")
            .max_batch_size(16)
            .use_gpu(false)
            .num_threads(2);

        assert_eq!(builder.config.model, EmbeddingModel::BgeSmall);
        assert_eq!(builder.config.cache_dir, Some("/cache".to_string()));
        assert_eq!(builder.config.max_batch_size, 16);
        assert!(!builder.config.use_gpu);
        assert_eq!(builder.config.num_threads, Some(2));
    }

    // ── estimate_time_ms ─────────────────────────────────────────────────────

    #[test]
    fn test_estimate_time_zero_count() {
        let tps = EmbeddingModel::MiniLM.tokens_per_second_cpu() as f64;
        let estimate = (0.0 / tps) * 1000.0;
        assert_eq!(estimate, 0.0);
    }

    #[test]
    fn test_estimate_time_formula_cpu() {
        // texts=10, avg_len=100 → tokens_per_text = min(25, 256) = 25
        // total_tokens = 250; tps = 5000; time = (250/5000)*1000 = 50ms
        let model = EmbeddingModel::MiniLM;
        let tokens_per_text = (100.0f64 / 4.0).min(model.max_seq_length() as f64);
        let total_tokens = tokens_per_text * 10.0;
        let estimate = (total_tokens / model.tokens_per_second_cpu() as f64) * 1000.0;
        assert!(
            (estimate - 50.0).abs() < 1e-6,
            "expected 50.0ms, got {estimate}"
        );
    }

    #[test]
    fn test_estimate_time_capped_at_max_seq_length() {
        let model = EmbeddingModel::MiniLM;
        let avg_len = 100_000;
        let tokens_per_text = (avg_len as f64 / 4.0).min(model.max_seq_length() as f64);
        assert_eq!(tokens_per_text, 256.0);
    }

    // ── ModelConfig API ───────────────────────────────────────────────────────

    #[test]
    fn test_model_config_new() {
        let cfg = ModelConfig::new(EmbeddingModel::BgeSmall);
        assert_eq!(cfg.model, EmbeddingModel::BgeSmall);
        assert_eq!(cfg.max_batch_size, 8);
        assert!(!cfg.use_gpu);
        assert!(cfg.cache_dir.is_none());
        assert!(cfg.num_threads.is_none());
    }

    #[test]
    fn test_model_config_default() {
        let cfg = ModelConfig::default();
        assert_eq!(cfg.model, EmbeddingModel::BgeLarge);
        assert_eq!(cfg.max_batch_size, 8);
        assert!(!cfg.use_gpu);
    }

    #[test]
    fn test_model_config_with_cache_dir() {
        let cfg = ModelConfig::new(EmbeddingModel::MiniLM).with_cache_dir("/tmp/models");
        assert_eq!(cfg.cache_dir, Some("/tmp/models".to_string()));
    }

    #[test]
    fn test_model_config_with_max_batch_size() {
        let cfg = ModelConfig::new(EmbeddingModel::MiniLM).with_max_batch_size(64);
        assert_eq!(cfg.max_batch_size, 64);
    }

    #[test]
    fn test_model_config_with_gpu() {
        let cfg = ModelConfig::new(EmbeddingModel::MiniLM).with_gpu(true);
        assert!(cfg.use_gpu);
    }

    #[test]
    fn test_model_config_with_num_threads() {
        let cfg = ModelConfig::new(EmbeddingModel::MiniLM).with_num_threads(8);
        assert_eq!(cfg.num_threads, Some(8));
    }

    #[test]
    fn test_model_config_chained_builder() {
        let cfg = ModelConfig::new(EmbeddingModel::E5Small)
            .with_cache_dir("/cache")
            .with_max_batch_size(16)
            .with_gpu(false)
            .with_num_threads(4);
        assert_eq!(cfg.model, EmbeddingModel::E5Small);
        assert_eq!(cfg.cache_dir, Some("/cache".to_string()));
        assert_eq!(cfg.max_batch_size, 16);
        assert!(!cfg.use_gpu);
        assert_eq!(cfg.num_threads, Some(4));
    }

    // ── model_cache_dir edge cases ────────────────────────────────────────────

    /// Test `model_cache_dir` when HOME is not set — should fall back to /tmp.
    #[test]
    fn test_model_cache_dir_no_home_fallback() {
        use std::sync::Mutex;
        static ENV_LOCK: Mutex<()> = Mutex::new(());
        let _guard = ENV_LOCK.lock().unwrap();

        // Remove HOME and HF_HOME so we hit the /tmp fallback
        let saved_home = std::env::var("HOME").ok();
        let saved_hf = std::env::var("HF_HOME").ok();
        unsafe {
            std::env::remove_var("HOME");
            std::env::remove_var("HF_HOME");
        }

        let result = EmbeddingEngine::model_cache_dir("test/fallback-model");

        // Restore env
        if let Some(h) = saved_home {
            unsafe { std::env::set_var("HOME", h) };
        }
        if let Some(h) = saved_hf {
            unsafe { std::env::set_var("HF_HOME", h) };
        }

        let path = result.unwrap();
        // Should be under /tmp since HOME was unset
        assert!(
            path.starts_with("/tmp"),
            "expected path under /tmp, got {path:?}"
        );
    }

    #[test]
    fn test_model_cache_dir_deep_model_id() {
        let path = EmbeddingEngine::model_cache_dir("org/sub/model-name-with-dashes").unwrap();
        let s = path.to_str().unwrap();
        // All slashes replaced with double-dash
        assert!(
            s.contains("org--sub--model-name-with-dashes"),
            "expected transformed path, got: {s}"
        );
    }

    #[test]
    fn test_model_cache_dir_minilm_model_id() {
        let path = EmbeddingEngine::model_cache_dir(EmbeddingModel::MiniLM.model_id()).unwrap();
        let s = path.to_str().unwrap();
        assert!(s.contains("sentence-transformers--all-MiniLM-L6-v2"));
    }

    #[test]
    fn test_model_cache_dir_bge_model_id() {
        let path = EmbeddingEngine::model_cache_dir(EmbeddingModel::BgeSmall.model_id()).unwrap();
        let s = path.to_str().unwrap();
        assert!(s.contains("BAAI--bge-small-en-v1.5"));
    }

    #[test]
    fn test_model_cache_dir_e5_model_id() {
        let path = EmbeddingEngine::model_cache_dir(EmbeddingModel::E5Small.model_id()).unwrap();
        let s = path.to_str().unwrap();
        assert!(s.contains("intfloat--e5-small-v2"));
    }

    // ── download_hf_file additional cache-hit variations ─────────────────────

    #[test]
    fn test_download_hf_file_pytorch_bin_cached() {
        let tmp = std::env::temp_dir().join("dakera_test_pytorch_bin");
        std::fs::create_dir_all(&tmp).unwrap();
        let model_path = tmp.join("pytorch_model.bin");
        std::fs::write(&model_path, b"fake_pytorch_weights").unwrap();

        let result = EmbeddingEngine::download_hf_file("test/model", "pytorch_model.bin", &tmp);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), model_path);
    }

    #[test]
    fn test_download_hf_file_tokenizer_cached() {
        let tmp = std::env::temp_dir().join("dakera_test_tokenizer_cached");
        std::fs::create_dir_all(&tmp).unwrap();
        let tok_path = tmp.join("tokenizer.json");
        std::fs::write(&tok_path, br#"{"version":"1.0"}"#).unwrap();

        let result = EmbeddingEngine::download_hf_file("test/model", "tokenizer.json", &tmp);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), tok_path);
    }

    #[test]
    fn test_download_hf_file_config_json_cached() {
        let tmp = std::env::temp_dir().join("dakera_test_config_cached");
        std::fs::create_dir_all(&tmp).unwrap();
        let cfg_path = tmp.join("config.json");
        std::fs::write(&cfg_path, b"{}").unwrap();

        let result = EmbeddingEngine::download_hf_file("test/model", "config.json", &tmp);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), cfg_path);
    }

    // ── EmbeddingEngine::new() failure path via fake local cache ─────────────

    /// Tests the code path through `download_model_files` (local Dakera cache hit)
    /// and into `new()` — which then fails trying to load the tokenizer from a
    /// fake file. No network access required; fake files are pre-seeded.
    #[tokio::test]
    #[allow(clippy::await_holding_lock)]
    async fn test_new_fails_with_invalid_tokenizer_json() {
        use std::sync::Mutex;
        static ENV_LOCK: Mutex<()> = Mutex::new(());
        let _guard = ENV_LOCK.lock().unwrap();

        // Set up a fake Dakera model cache so download_model_files finds our files
        let tmp = std::env::temp_dir().join("dakera_test_engine_new_fail_tok");
        let model_dir = tmp
            .join("dakera")
            .join("sentence-transformers--all-MiniLM-L6-v2");
        std::fs::create_dir_all(&model_dir).unwrap();
        // Valid-looking model weights placeholder (candle will fail on this, which is fine)
        std::fs::write(model_dir.join("model.safetensors"), b"not_real_weights").unwrap();
        // Invalid tokenizer.json — will cause TokenizationError in new()
        std::fs::write(model_dir.join("tokenizer.json"), b"NOT_VALID_JSON").unwrap();
        std::fs::write(model_dir.join("config.json"), b"{}").unwrap();

        unsafe { std::env::set_var("HF_HOME", &tmp) };

        let config = ModelConfig::new(EmbeddingModel::MiniLM);
        let result = EmbeddingEngine::new(config).await;

        unsafe { std::env::remove_var("HF_HOME") };

        // Must fail — tokenizer.json is invalid JSON
        assert!(
            result.is_err(),
            "expected Err from new() with invalid tokenizer, got Ok"
        );
    }

    // ── EmbeddingEngineBuilder additional coverage ────────────────────────────

    #[test]
    fn test_builder_with_all_models() {
        for model in [
            EmbeddingModel::MiniLM,
            EmbeddingModel::BgeSmall,
            EmbeddingModel::E5Small,
        ] {
            let builder = EmbeddingEngineBuilder::new().model(model);
            assert_eq!(builder.config.model, model);
        }
    }

    #[test]
    fn test_builder_max_batch_size_one() {
        let builder = EmbeddingEngineBuilder::new().max_batch_size(1);
        assert_eq!(builder.config.max_batch_size, 1);
    }

    #[test]
    fn test_builder_num_threads_zero() {
        let builder = EmbeddingEngineBuilder::new().num_threads(0);
        assert_eq!(builder.config.num_threads, Some(0));
    }

    // ── EmbeddingEngine::new() / getters when model is cached (best-effort) ──

    /// If the embedding model is already cached on this machine, exercise the
    /// full `new()` path and test getters. On machines without a cached model
    /// the test passes silently — it is intentionally non-gating.
    #[tokio::test]
    async fn test_engine_getters_when_model_cached() {
        let config = ModelConfig::new(EmbeddingModel::MiniLM);
        match EmbeddingEngine::new(config).await {
            Ok(engine) => {
                assert_eq!(engine.dimension(), EmbeddingModel::MiniLM.dimension());
                assert_eq!(engine.model(), EmbeddingModel::MiniLM);
                // Device should be CPU in test environments (device() removed in CE-3 ONNX migration)
                // Debug impl should not panic
                let _ = format!("{:?}", engine);
                // estimate_time_ms should return a non-negative value
                let ms = engine.estimate_time_ms(10, 50);
                assert!(ms >= 0.0);
            }
            Err(_) => {
                // Model not in cache — skip; CI runner may or may not have it
            }
        }
    }

    /// When model is cached: embed an empty batch must return immediately with
    /// no embeddings (the `texts.is_empty()` fast-path in embed_batch_internal).
    #[tokio::test]
    async fn test_engine_embed_empty_batch_when_cached() {
        let config = ModelConfig::new(EmbeddingModel::MiniLM);
        if let Ok(engine) = EmbeddingEngine::new(config).await {
            let result = engine.embed_raw(&[]).await;
            assert!(result.is_ok());
            assert!(result.unwrap().is_empty());
        }
    }

    // ── Session pool (DAK-5547) ──────────────────────────────────────────────

    #[test]
    fn test_session_pool_default_is_4() {
        // Default pool size is 4 (DAK-5746: pool=1 regressed LME ingest ~4×; OOM causes fixed).
        // DAKERA_ONNX_POOL_SIZE env var still allows override.
        let config = ModelConfig::default();
        let expected = std::env::var("DAKERA_ONNX_POOL_SIZE")
            .ok()
            .and_then(|v| v.parse::<usize>().ok())
            .filter(|&n| n >= 1)
            .unwrap_or(4);
        assert_eq!(config.session_pool_size, expected);
    }

    #[test]
    fn test_session_pool_size_builder_roundtrip() {
        let builder = EmbeddingEngineBuilder::new().session_pool_size(8);
        assert_eq!(builder.config.session_pool_size, 8);
    }

    #[test]
    fn test_session_pool_size_min_enforced() {
        let builder = EmbeddingEngineBuilder::new().session_pool_size(0);
        assert_eq!(
            builder.config.session_pool_size, 1,
            "pool size 0 must clamp to 1"
        );
    }

    #[test]
    fn test_model_config_with_session_pool_size() {
        let cfg = ModelConfig::new(EmbeddingModel::MiniLM).with_session_pool_size(2);
        assert_eq!(cfg.session_pool_size, 2);
    }

    /// When model is cached: verify the session pool has the expected size.
    #[tokio::test]
    async fn test_engine_pool_size_matches_config_when_cached() {
        let config = ModelConfig::new(EmbeddingModel::MiniLM).with_session_pool_size(2);
        if let Ok(engine) = EmbeddingEngine::new(config).await {
            assert_eq!(
                engine.pool_size(),
                2,
                "engine should hold exactly 2 sessions"
            );
        }
    }

    // ── next_session round-robin ──────────────────────────────────────────────

    /// Round-robin counter distributes batches across pool slots without panic.
    #[test]
    fn test_round_robin_index_stays_in_bounds() {
        let pool_len = 4_usize;
        let counter = AtomicUsize::new(0);
        for expected_idx in 0..100_usize {
            let start = counter.fetch_add(1, Ordering::Relaxed);
            let slot = start % pool_len;
            assert!(slot < pool_len);
            assert_eq!(slot, expected_idx % pool_len);
        }
    }

    /// Pool size 1 degrades to single-session behavior without panicking.
    #[test]
    fn test_round_robin_pool_size_one() {
        let pool_len = 1_usize;
        let counter = AtomicUsize::new(0);
        for _ in 0..50 {
            let start = counter.fetch_add(1, Ordering::Relaxed);
            assert_eq!(start % pool_len, 0);
        }
    }

    /// When model is cached: empty batch short-circuits before touching the pool.
    #[tokio::test]
    async fn test_embed_empty_does_not_advance_pool_counter() {
        let config = ModelConfig::new(EmbeddingModel::MiniLM).with_session_pool_size(2);
        if let Ok(engine) = EmbeddingEngine::new(config).await {
            let result = engine.embed_raw(&[]).await;
            assert!(result.is_ok());
            assert!(result.unwrap().is_empty());
            // Empty batch returns before fetch_add — counter stays at 0.
            assert_eq!(engine.next_session.load(Ordering::Relaxed), 0);
        }
    }
}