cera 0.5.0

Rust-native LLM inference engine
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
//! Streaming SafeTensors to GGUF quantization pipeline with HTTP retries & checkpoint resumption.

use serde::{Deserialize, Serialize};
use std::fs::{self, File, OpenOptions};
use std::io::{BufWriter, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::bundle::{DownloadProgress, HfSpec, fetch_model_info};
use crate::convert::config::HfModelConfig;
use crate::convert::quantize::{QuantStrategy, TargetQuant, quantize_tensor_data_with_strategy};
use crate::convert::safetensors::{
    SafeTensorsHeader, decode_safetensor_to_f32_into, translate_hf_to_gguf_tensor_name,
};
use crate::convert::tokenizer::HfTokenizerJson;
use crate::convert::writer::GgufWriter;
use crate::manifest::{GenerationDefaults, InferenceType, Manifest, ManifestFiles};
use crate::session::CeraError;

/// Options controlling model quantization and caching.
#[derive(Clone)]
pub struct QuantizeOptions {
    pub target_quant: TargetQuant,
    pub strategy: QuantStrategy,
    pub cache_dir: PathBuf,
    pub auth_token: Option<String>,
    pub progress: Option<Arc<dyn DownloadProgress>>,
    pub cancel: Option<Arc<std::sync::atomic::AtomicBool>>,
}

impl Default for QuantizeOptions {
    fn default() -> Self {
        Self {
            target_quant: TargetQuant::Q4_K_M,
            strategy: QuantStrategy::Auto,
            cache_dir: crate::bundle::hf::default_cache_dir(),
            auth_token: crate::bundle::hf::get_hf_auth_token(),
            progress: None,
            cancel: None,
        }
    }
}

static TEMP_FILE_SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

fn unique_temp_suffix() -> String {
    let seq = TEMP_FILE_SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    format!("tmp.{}.{}", std::process::id(), seq)
}

/// RAII guard to automatically clean up temporary files on error or drop.
struct TempFileGuard {
    path: PathBuf,
    active: bool,
}

impl TempFileGuard {
    fn new(path: PathBuf) -> Self {
        Self { path, active: true }
    }

    fn disarm(mut self) {
        self.active = false;
    }
}

impl Drop for TempFileGuard {
    fn drop(&mut self) {
        if self.active {
            let _ = fs::remove_file(&self.path);
        }
    }
}

/// Checkpoint metadata for resuming interrupted streaming quantization.
#[derive(Debug, Clone, Serialize, Deserialize)]
struct QuantCheckpoint {
    quant: String,
    strategy: String,
    completed_tensors: usize,
    total_tensors: usize,
    file_bytes: u64,
}

/// Stream and quantize a remote Hugging Face SafeTensors repository into a cached GGUF model.
pub fn stream_quantize_hf_repo(
    spec: &HfSpec,
    opts: QuantizeOptions,
) -> Result<Manifest, CeraError> {
    let quant_str = opts.target_quant.as_str();
    let strat_str = opts.strategy.as_str();
    let rev_segment = if spec.revision == "main" {
        ""
    } else {
        spec.revision.as_str()
    };
    let strat_segment = if opts.strategy == QuantStrategy::Auto {
        ""
    } else {
        strat_str
    };
    let cache_leaf = match (rev_segment.is_empty(), strat_segment.is_empty()) {
        (true, true) => quant_str.to_string(),
        (false, true) => format!("{quant_str}@{rev_segment}"),
        (true, false) => format!("{quant_str}-{strat_segment}"),
        (false, false) => format!("{quant_str}-{strat_segment}@{rev_segment}"),
    };
    let store_dir = opts
        .cache_dir
        .join("huggingface.co")
        .join(&spec.owner)
        .join(&spec.repo)
        .join("quantized")
        .join(cache_leaf);

    let manifest_path = store_dir.join(format!("{quant_str}.json"));
    let target_gguf_path = store_dir.join("model.gguf");
    let tmp_gguf_path = store_dir.join("model.gguf.tmp");
    let ckpt_path = store_dir.join("model.gguf.checkpoint.json");

    // Check if previously converted and cached
    if manifest_path.exists()
        && target_gguf_path.exists()
        && let Ok(manifest_text) = fs::read_to_string(&manifest_path)
        && let Ok(mut manifest) = Manifest::from_bytes(manifest_text.as_bytes())
    {
        manifest.files.model = target_gguf_path.to_string_lossy().to_string();
        return Ok(manifest);
    }

    fs::create_dir_all(&store_dir).map_err(|e| {
        CeraError::Backend(format!(
            "failed to create cache dir `{}`: {e}",
            store_dir.display()
        ))
    })?;

    // 1. Fetch Repo Metadata from HF API (with automatic retries)
    let info = fetch_model_info(spec)?;

    let client = reqwest::blocking::Client::builder()
        .timeout(std::time::Duration::from_secs(120))
        .build()
        .map_err(|e| CeraError::Backend(format!("failed to build reqwest client: {e}")))?;

    // 1. Fetch config.json, tokenizer.json, and optional generation_config.json
    let config_url = spec.file_download_url("config.json");
    let config_bytes = fetch_hf_file_bytes(&client, &config_url, opts.auth_token.as_deref())?;
    let config = HfModelConfig::parse_from_bytes(&config_bytes)?;

    let tokenizer_url = spec.file_download_url("tokenizer.json");
    let tokenizer_bytes = fetch_hf_file_bytes(&client, &tokenizer_url, opts.auth_token.as_deref())?;
    let tokenizer = HfTokenizerJson::parse_from_bytes(&tokenizer_bytes)?;

    // Optional chat template & generation config
    let template_url = spec.file_download_url("tokenizer_config.json");
    let chat_template = fetch_hf_file_bytes(&client, &template_url, opts.auth_token.as_deref())
        .ok()
        .and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok())
        .and_then(|v| {
            v.get("chat_template")
                .and_then(|t| t.as_str().map(str::to_string))
        });

    let gen_url = spec.file_download_url("generation_config.json");
    let gen_defaults = fetch_hf_file_bytes(&client, &gen_url, opts.auth_token.as_deref())
        .ok()
        .and_then(|b| {
            std::str::from_utf8(&b)
                .ok()
                .and_then(crate::bundle::hf::parse_generation_config_json)
        })
        .unwrap_or(GenerationDefaults::Text {
            temperature: None,
            min_p: None,
            top_p: None,
            top_k: None,
            repetition_penalty: None,
        });

    // 3. Discover SafeTensors files (single or sharded)
    let mut safetensors_files: Vec<String> = info
        .siblings
        .iter()
        .filter(|s| {
            let lower = s.rfilename.to_ascii_lowercase();
            lower.ends_with(".safetensors")
                && !lower.contains("mmproj")
                && !lower.contains("visual")
                && !lower.contains("audio_decoder")
                && !lower.contains("vocoder")
        })
        .map(|s| s.rfilename.clone())
        .collect();
    safetensors_files.sort();

    if safetensors_files.is_empty() {
        return Err(CeraError::Backend(format!(
            "repository `{}/{}` has no .safetensors files to quantize",
            spec.owner, spec.repo
        )));
    }

    // 4. Read headers of each SafeTensors shard
    let mut shard_headers = Vec::new();
    for file_name in &safetensors_files {
        let header_url = spec.file_download_url(file_name);
        // Read 8-byte header size first
        let len_bytes =
            fetch_hf_file_range(&client, &header_url, 0, 7, opts.auth_token.as_deref())?;
        let header_len_arr: [u8; 8] = len_bytes.try_into().map_err(|_| {
            CeraError::Backend(format!(
                "failed to read 8-byte header length from `{header_url}`"
            ))
        })?;
        let header_len_u64 = u64::from_le_bytes(header_len_arr);
        if header_len_u64 == 0 || header_len_u64 > 100_000_000 {
            return Err(CeraError::Backend(format!(
                "invalid SafeTensors header length {header_len_u64} in `{header_url}`"
            )));
        }
        let header_len = header_len_u64 as usize;
        let json_bytes = fetch_hf_file_range(
            &client,
            &header_url,
            8,
            8 + header_len - 1,
            opts.auth_token.as_deref(),
        )?;
        let json_str = std::str::from_utf8(&json_bytes).map_err(|e| {
            CeraError::Backend(format!(
                "invalid UTF-8 in SafeTensors header for `{header_url}`: {e}"
            ))
        })?;
        let header = SafeTensorsHeader::parse_from_json_str(json_str, 8 + header_len)?;
        shard_headers.push((file_name.clone(), header));
    }

    // 5. Initialize GgufWriter & register all tensor metadata
    let mut writer = GgufWriter::new();
    config.apply_to_gguf_writer(&mut writer, &spec.repo);
    tokenizer.apply_to_gguf_writer(&mut writer, chat_template.as_deref());

    // Register all tensors in GGUF writer
    struct PendingTensor {
        shard_idx: usize,
        name: String,
        dtype: String,
        data_start: usize,
        data_end: usize,
        ggml_type: u32,
        expected_elements: usize,
    }

    let mut pending_tensors = Vec::new();

    for (shard_idx, (_file_name, header)) in shard_headers.iter().enumerate() {
        for (tensor_name, tensor_info) in &header.tensors {
            let gguf_name = translate_hf_to_gguf_tensor_name(tensor_name);
            let num_elements: usize = tensor_info
                .shape
                .iter()
                .try_fold(1usize, |acc, &d| acc.checked_mul(d))
                .ok_or_else(|| {
                    CeraError::Backend(format!("tensor shape overflow for `{tensor_name}`"))
                })?;
            let ggml_type = opts.target_quant.select_ggml_type(
                &gguf_name,
                tensor_info.shape.len(),
                num_elements,
            );
            let out_bytes = TargetQuant::compute_tensor_bytes(ggml_type, num_elements);

            // In GGUF, dimensions are column-major (reversed shape: [cols, rows])
            let dims: Vec<u64> = tensor_info.shape.iter().rev().map(|&d| d as u64).collect();

            writer.add_tensor(&gguf_name, dims, ggml_type, out_bytes);

            let data_start = header
                .header_size_bytes
                .checked_add(tensor_info.data_offsets.0)
                .ok_or_else(|| {
                    CeraError::Backend(format!("tensor offset start overflow for `{tensor_name}`"))
                })?;
            let data_end = header
                .header_size_bytes
                .checked_add(tensor_info.data_offsets.1)
                .ok_or_else(|| {
                    CeraError::Backend(format!("tensor offset end overflow for `{tensor_name}`"))
                })?;

            pending_tensors.push(PendingTensor {
                shard_idx,
                name: gguf_name,
                dtype: tensor_info.dtype.clone(),
                data_start,
                data_end,
                ggml_type,
                expected_elements: num_elements,
            });
        }
    }

    let total_tensors = pending_tensors.len();

    // 6. Check for valid existing checkpoint to resume from
    let mut start_tensor_idx = 0;
    let mut should_write_header = true;
    let mut resume_bytes = 0u64;

    if tmp_gguf_path.exists()
        && ckpt_path.exists()
        && let Ok(ckpt_str) = fs::read_to_string(&ckpt_path)
        && let Ok(ckpt) = serde_json::from_str::<QuantCheckpoint>(&ckpt_str)
        && ckpt.quant == quant_str
        && ckpt.strategy == strat_str
        && ckpt.total_tensors == total_tensors
        && ckpt.completed_tensors < total_tensors
        && let Ok(meta) = fs::metadata(&tmp_gguf_path)
        && meta.len() >= ckpt.file_bytes
    {
        start_tensor_idx = ckpt.completed_tensors;
        should_write_header = false;
        resume_bytes = ckpt.file_bytes;
        tracing::info!(
            "Resuming quantization of `{}/{}` ({quant_str}) from tensor {}/{}…",
            spec.owner,
            spec.repo,
            start_tensor_idx,
            total_tensors
        );
    }

    // Open file (create fresh or append for resume)
    let (out_file, mut current_bytes_written) = if should_write_header {
        let _ = fs::remove_file(&ckpt_path);
        let file = File::create(&tmp_gguf_path).map_err(|e| {
            CeraError::Backend(format!(
                "failed to create `{}`: {e}",
                tmp_gguf_path.display()
            ))
        })?;
        (file, 0u64)
    } else {
        let mut file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(&tmp_gguf_path)
            .map_err(|e| {
                CeraError::Backend(format!(
                    "failed to open `{}` for resume: {e}",
                    tmp_gguf_path.display()
                ))
            })?;
        file.set_len(resume_bytes).map_err(|e| {
            CeraError::Backend(format!(
                "failed to truncate `{}` to resume boundary {resume_bytes} bytes: {e}",
                tmp_gguf_path.display()
            ))
        })?;
        let len = file.seek(SeekFrom::Start(resume_bytes)).map_err(|e| {
            CeraError::Backend(format!(
                "failed to seek `{}` to resume boundary: {e}",
                tmp_gguf_path.display()
            ))
        })?;
        (file, len)
    };

    let mut buf_writer = BufWriter::with_capacity(1024 * 1024 * 4, out_file);

    if should_write_header {
        let _data_start_offset = writer.write_header_and_tensor_info(&mut buf_writer)?;
        buf_writer.flush().map_err(|e| {
            CeraError::Backend(format!(
                "failed to flush `{}`: {e}",
                tmp_gguf_path.display()
            ))
        })?;
        current_bytes_written = fs::metadata(&tmp_gguf_path).map(|m| m.len()).unwrap_or(0);
    }

    // Estimate total output file bytes accurately
    let align = writer.alignment() as u64;
    let align_mask = align - 1;
    let total_estimated_bytes: u64 = current_bytes_written
        + pending_tensors
            .iter()
            .skip(start_tensor_idx)
            .map(|pt| {
                let raw =
                    TargetQuant::compute_tensor_bytes(pt.ggml_type, pt.expected_elements) as u64;
                (raw + align_mask) & !align_mask
            })
            .sum::<u64>();

    // 7. Stream tensors from SafeTensors shards, quantize on-the-fly, and write to GGUF
    let shard_download_urls: Vec<String> = shard_headers
        .iter()
        .map(|(name, _)| spec.file_download_url(name))
        .collect();

    let mut quant_buf = Vec::new();
    let mut f32_data = Vec::new();
    let mut raw_tensor_bytes = Vec::new();

    for (i, pt) in pending_tensors.iter().enumerate().skip(start_tensor_idx) {
        if let Some(ref cancel) = opts.cancel
            && cancel.load(std::sync::atomic::Ordering::Relaxed)
        {
            let _ = buf_writer.flush();
            return Err(CeraError::Cancelled);
        }

        let tensor_url = &shard_download_urls[pt.shard_idx];

        if let Some(p) = &opts.progress {
            p.on_progress(
                tensor_url,
                current_bytes_written,
                Some(total_estimated_bytes),
            );
        }

        // Fetch raw tensor bytes over HTTP Range with automatic retry and exponential backoff
        if pt.data_start < pt.data_end {
            fetch_hf_file_range_into(
                &client,
                tensor_url,
                pt.data_start,
                pt.data_end - 1,
                opts.auth_token.as_deref(),
                &mut raw_tensor_bytes,
            )?;
        } else {
            raw_tensor_bytes.clear();
        }

        // Decode BF16/F16/F32 to f32
        decode_safetensor_to_f32_into(&raw_tensor_bytes, &pt.dtype, &mut f32_data)?;
        let num_elements = f32_data.len();
        if num_elements != pt.expected_elements {
            return Err(CeraError::Backend(format!(
                "tensor `{}` element count mismatch: expected {}, got {num_elements}",
                pt.name, pt.expected_elements
            )));
        }

        // Quantize to target GGML type
        let target_size = TargetQuant::compute_tensor_bytes(pt.ggml_type, num_elements);
        quant_buf.resize(target_size, 0);

        quantize_tensor_data_with_strategy(&f32_data, pt.ggml_type, opts.strategy, &mut quant_buf)?;

        // Write to GGUF with alignment padding
        let written = writer.write_tensor_data(&mut buf_writer, &quant_buf)?;
        current_bytes_written += written as u64;

        // Persist checkpoint periodically
        if (i + 1) % 5 == 0 || i + 1 == total_tensors {
            buf_writer.flush().map_err(|e| {
                CeraError::Backend(format!(
                    "failed to flush `{}`: {e}",
                    tmp_gguf_path.display()
                ))
            })?;
            let ckpt = QuantCheckpoint {
                quant: quant_str.to_string(),
                strategy: strat_str.to_string(),
                completed_tensors: i + 1,
                total_tensors,
                file_bytes: current_bytes_written,
            };

            if let Ok(json) = serde_json::to_string(&ckpt) {
                let ckpt_tmp = store_dir.join(format!(
                    "model.gguf.checkpoint.json.{}",
                    unique_temp_suffix()
                ));
                if fs::write(&ckpt_tmp, json).is_ok() {
                    #[cfg(windows)]
                    let _ = fs::remove_file(&ckpt_path);
                    if fs::rename(&ckpt_tmp, &ckpt_path).is_err() {
                        let _ = fs::remove_file(&ckpt_path);
                        if fs::rename(&ckpt_tmp, &ckpt_path).is_err() {
                            let _ = fs::remove_file(&ckpt_tmp);
                        }
                    }
                }
            }
        }
    }

    if let Some(p) = &opts.progress {
        p.on_progress(
            &spec.file_download_url("model.gguf"),
            current_bytes_written,
            Some(current_bytes_written),
        );
    }

    buf_writer.flush().map_err(|e| {
        CeraError::Backend(format!(
            "failed to flush `{}`: {e}",
            tmp_gguf_path.display()
        ))
    })?;
    if let Ok(file) = buf_writer.into_inner() {
        let _ = file.sync_all();
    }

    // 8. Compute final SHA-256 digest
    let final_digest = crate::bundle::download::sha256_file(&tmp_gguf_path).map_err(|e| {
        CeraError::Backend(format!(
            "failed to compute sha256 of `{}`: {e}",
            tmp_gguf_path.display()
        ))
    })?;

    // 9. Atomic Rename and SHA-256 sidecar
    let _ = fs::remove_file(&target_gguf_path);
    fs::rename(&tmp_gguf_path, &target_gguf_path).map_err(|e| {
        CeraError::Backend(format!(
            "failed to rename to `{}`: {e}",
            target_gguf_path.display()
        ))
    })?;

    // Remove completed checkpoint
    let _ = fs::remove_file(&ckpt_path);

    let sha_path = store_dir.join("model.gguf.sha256");
    let _ = fs::write(sha_path, format!("{final_digest}\n"));

    // 10. Generate local Manifest JSON
    let files = ManifestFiles {
        model: target_gguf_path.to_string_lossy().to_string(),
        multimodal_projector: None,
        audio_decoder: None,
        audio_tokenizer: None,
        draft_model: None,
        extras: std::collections::HashMap::new(),
    };

    let mut load_time_params = serde_json::json!({
        "model": "model.gguf"
    });
    if let Some(tmpl) = &chat_template {
        load_time_params["chat_template"] = serde_json::Value::String(tmpl.clone());
    }

    let raw_val = serde_json::json!({
        "inference_type": InferenceType::LlamaCppTextToText.as_str(),
        "schema_version": "1.0.0",
        "load_time_parameters": load_time_params,
        "generation_time_parameters": gen_defaults.to_json_value(),
        "quant": quant_str,
        "strategy": strat_str,
    });

    let manifest_json = serde_json::to_string_pretty(&raw_val)
        .map_err(|e| CeraError::Backend(format!("failed to serialize manifest json: {e}")))?;

    fs::write(&manifest_path, &manifest_json).map_err(|e| {
        CeraError::Backend(format!(
            "failed to write `{}`: {e}",
            manifest_path.display()
        ))
    })?;

    let manifest = Manifest {
        inference_type: InferenceType::LlamaCppTextToText,
        schema_version: "1.0.0".into(),
        files,
        chat_template,
        generation_defaults: gen_defaults,
        raw: raw_val,
    };

    Ok(manifest)
}

/// Quantize a local SafeTensors directory or file to GGUF.
pub fn quantize_safetensors_to_gguf(
    input_path: &Path,
    output_gguf_path: &Path,
    quant: TargetQuant,
) -> Result<(), CeraError> {
    if !input_path.exists() {
        return Err(CeraError::Backend(format!(
            "input path `{}` does not exist",
            input_path.display()
        )));
    }

    let dir = if input_path.is_dir() {
        input_path.to_path_buf()
    } else {
        input_path
            .parent()
            .unwrap_or_else(|| Path::new("."))
            .to_path_buf()
    };

    let config_path = dir.join("config.json");
    let tokenizer_path = dir.join("tokenizer.json");

    if !config_path.exists() {
        return Err(CeraError::Backend(format!(
            "config.json not found in `{}`",
            dir.display()
        )));
    }

    let config_bytes = fs::read(&config_path).map_err(|e| {
        CeraError::Backend(format!("failed to read `{}`: {e}", config_path.display()))
    })?;
    let config = HfModelConfig::parse_from_bytes(&config_bytes)?;

    let mut writer = GgufWriter::new();
    let model_name = dir.file_name().and_then(|n| n.to_str()).unwrap_or("model");
    config.apply_to_gguf_writer(&mut writer, model_name);

    if tokenizer_path.exists() {
        let tok_bytes = fs::read(&tokenizer_path).map_err(|e| {
            CeraError::Backend(format!(
                "failed to read `{}`: {e}",
                tokenizer_path.display()
            ))
        })?;
        if let Ok(tokenizer) = HfTokenizerJson::parse_from_bytes(&tok_bytes) {
            let chat_template = dir
                .join("tokenizer_config.json")
                .exists()
                .then(|| fs::read(dir.join("tokenizer_config.json")).ok())
                .flatten()
                .and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok())
                .and_then(|v| {
                    v.get("chat_template")
                        .and_then(|t| t.as_str().map(str::to_string))
                });
            tokenizer.apply_to_gguf_writer(&mut writer, chat_template.as_deref());
        }
    }

    let mut safetensors_files = Vec::new();
    if input_path.is_file()
        && input_path.extension().and_then(|e| e.to_str()) == Some("safetensors")
    {
        safetensors_files.push(input_path.to_path_buf());
    } else {
        for entry in fs::read_dir(&dir)
            .map_err(|e| CeraError::Backend(format!("failed to read dir: {e}")))?
        {
            let entry =
                entry.map_err(|e| CeraError::Backend(format!("failed to read entry: {e}")))?;
            let p = entry.path();
            if p.is_file() && p.extension().and_then(|e| e.to_str()) == Some("safetensors") {
                safetensors_files.push(p);
            }
        }
        safetensors_files.sort();
    }

    if safetensors_files.is_empty() {
        return Err(CeraError::Backend(format!(
            "no .safetensors files found in `{}`",
            dir.display()
        )));
    }

    struct LocalPendingTensor {
        file_idx: usize,
        expected_elements: usize,
        dtype: String,
        data_start: usize,
        data_end: usize,
        ggml_type: u32,
    }

    let mut pending_tensors = Vec::new();

    for (file_idx, file_path) in safetensors_files.iter().enumerate() {
        let mut file = File::open(file_path).map_err(|e| {
            CeraError::Backend(format!("failed to open `{}`: {e}", file_path.display()))
        })?;
        let header = SafeTensorsHeader::parse_from_reader(&mut file)?;
        for (raw_name, tensor_info) in &header.tensors {
            let gguf_name = translate_hf_to_gguf_tensor_name(raw_name);
            let num_elements: usize = tensor_info
                .shape
                .iter()
                .try_fold(1usize, |acc, &d| acc.checked_mul(d))
                .ok_or_else(|| {
                    CeraError::Backend(format!("tensor shape overflow for `{raw_name}`"))
                })?;
            let ggml_type =
                quant.select_ggml_type(&gguf_name, tensor_info.shape.len(), num_elements);

            let dims: Vec<u64> = tensor_info.shape.iter().rev().map(|&d| d as u64).collect();
            let out_bytes = TargetQuant::compute_tensor_bytes(ggml_type, num_elements);

            writer.add_tensor(&gguf_name, dims, ggml_type, out_bytes);

            let data_start = header
                .header_size_bytes
                .checked_add(tensor_info.data_offsets.0)
                .ok_or_else(|| {
                    CeraError::Backend(format!("tensor offset start overflow for `{raw_name}`"))
                })?;
            let data_end = header
                .header_size_bytes
                .checked_add(tensor_info.data_offsets.1)
                .ok_or_else(|| {
                    CeraError::Backend(format!("tensor offset end overflow for `{raw_name}`"))
                })?;

            pending_tensors.push(LocalPendingTensor {
                file_idx,
                expected_elements: num_elements,
                dtype: tensor_info.dtype.clone(),
                data_start,
                data_end,
                ggml_type,
            });
        }
    }

    let tmp_out = output_gguf_path.with_extension(unique_temp_suffix());
    let tmp_guard = TempFileGuard::new(tmp_out.clone());

    let out_file = File::create(&tmp_out).map_err(|e| {
        CeraError::Backend(format!("failed to create `{}`: {e}", tmp_out.display()))
    })?;
    let mut buf_writer = BufWriter::with_capacity(1024 * 1024 * 4, out_file);
    writer.write_header_and_tensor_info(&mut buf_writer)?;

    let mut quant_buf = Vec::new();
    let mut f32_data = Vec::new();
    let mut raw_bytes = Vec::new();
    let mut current_file_idx = usize::MAX;
    let mut current_file: Option<File> = None;

    for pt in pending_tensors {
        if pt.file_idx != current_file_idx {
            let f = File::open(&safetensors_files[pt.file_idx]).map_err(|e| {
                CeraError::Backend(format!(
                    "failed to open `{}`: {e}",
                    safetensors_files[pt.file_idx].display()
                ))
            })?;
            current_file = Some(f);
            current_file_idx = pt.file_idx;
        }

        let file = current_file
            .as_mut()
            .ok_or_else(|| CeraError::Backend("no active safetensors shard file".into()))?;

        let byte_len = pt.data_end.saturating_sub(pt.data_start);
        file.seek(SeekFrom::Start(pt.data_start as u64))
            .map_err(|e| CeraError::Backend(format!("failed to seek in safetensors shard: {e}")))?;

        raw_bytes.resize(byte_len, 0);
        file.read_exact(&mut raw_bytes).map_err(|e| {
            CeraError::Backend(format!(
                "failed to read tensor data from safetensors shard: {e}"
            ))
        })?;

        decode_safetensor_to_f32_into(&raw_bytes, &pt.dtype, &mut f32_data)?;
        let num_elements = f32_data.len();
        if num_elements != pt.expected_elements {
            return Err(CeraError::Backend(format!(
                "tensor element count mismatch: expected {}, got {num_elements}",
                pt.expected_elements
            )));
        }

        let target_size = TargetQuant::compute_tensor_bytes(pt.ggml_type, num_elements);
        quant_buf.resize(target_size, 0);

        quantize_tensor_data_with_strategy(
            &f32_data,
            pt.ggml_type,
            QuantStrategy::Auto,
            &mut quant_buf,
        )?;
        writer.write_tensor_data(&mut buf_writer, &quant_buf)?;
    }

    buf_writer
        .flush()
        .map_err(|e| CeraError::Backend(format!("failed to flush `{}`: {e}", tmp_out.display())))?;
    if let Ok(file) = buf_writer.into_inner() {
        let _ = file.sync_all();
    }

    let _ = fs::remove_file(output_gguf_path);
    fs::rename(&tmp_out, output_gguf_path).map_err(|e| {
        CeraError::Backend(format!(
            "failed to rename to `{}`: {e}",
            output_gguf_path.display()
        ))
    })?;

    tmp_guard.disarm();
    Ok(())
}

const MAX_RETRIES: u32 = 5;
const BASE_RETRY_DELAY_MS: u64 = 1000;

fn is_retryable_status(status: reqwest::StatusCode) -> bool {
    status == reqwest::StatusCode::REQUEST_TIMEOUT
        || status == reqwest::StatusCode::TOO_MANY_REQUESTS
        || status.is_server_error()
}

fn fetch_hf_file_bytes(
    client: &reqwest::blocking::Client,
    url: &str,
    auth_token: Option<&str>,
) -> Result<Vec<u8>, CeraError> {
    let mut last_error = String::new();
    for attempt in 0..MAX_RETRIES {
        if attempt > 0 {
            let delay_ms = BASE_RETRY_DELAY_MS * (1 << (attempt - 1));
            std::thread::sleep(std::time::Duration::from_millis(delay_ms));
        }

        let mut req = client.get(url);
        if let Some(token) = auth_token {
            req = req.header(reqwest::header::AUTHORIZATION, format!("Bearer {token}"));
        }

        match req.send() {
            Ok(resp) => {
                let status = resp.status();
                if status.is_success() {
                    match resp.bytes() {
                        Ok(b) => return Ok(b.to_vec()),
                        Err(e) => last_error = format!("failed reading body from `{url}`: {e}"),
                    }
                } else if is_retryable_status(status) {
                    last_error = format!("HTTP {status} when fetching `{url}`");
                } else {
                    return Err(CeraError::Backend(format!(
                        "HTTP {status} when fetching `{url}`"
                    )));
                }
            }
            Err(e) => {
                last_error = format!("connection error for `{url}`: {e}");
            }
        }
    }

    Err(CeraError::Backend(format!(
        "failed to fetch `{url}` after {MAX_RETRIES} attempts: {last_error}"
    )))
}

fn fetch_hf_file_range_into(
    client: &reqwest::blocking::Client,
    url: &str,
    start: usize,
    end: usize,
    auth_token: Option<&str>,
    out: &mut Vec<u8>,
) -> Result<(), CeraError> {
    out.clear();
    if start > end {
        return Ok(());
    }

    let range_val = format!("bytes={start}-{end}");
    let mut last_error = String::new();

    for attempt in 0..MAX_RETRIES {
        if attempt > 0 {
            let delay_ms = BASE_RETRY_DELAY_MS * (1 << (attempt - 1));
            std::thread::sleep(std::time::Duration::from_millis(delay_ms));
        }

        let mut req = client.get(url).header(reqwest::header::RANGE, &range_val);
        if let Some(token) = auth_token {
            req = req.header(reqwest::header::AUTHORIZATION, format!("Bearer {token}"));
        }

        match req.send() {
            Ok(mut resp) => {
                let status = resp.status();
                if status == reqwest::StatusCode::PARTIAL_CONTENT {
                    let expected_len = end.saturating_sub(start) + 1;
                    out.reserve(expected_len);
                    let mut reader = std::io::Read::take(&mut resp, (expected_len + 1) as u64);
                    match std::io::Read::read_to_end(&mut reader, out) {
                        Ok(n) => {
                            if n != expected_len {
                                last_error = format!(
                                    "truncated range body from `{url}`: expected {expected_len} bytes, got {n}"
                                );
                                out.clear();
                                continue;
                            }
                            return Ok(());
                        }
                        Err(e) => {
                            last_error = format!("failed reading range body from `{url}`: {e}");
                            out.clear();
                        }
                    }
                } else if status == reqwest::StatusCode::OK {
                    // Server returned entire body instead of range; guard against unbounded memory consumption
                    let expected_range_len = end.saturating_sub(start) + 1;
                    let max_allowed = (expected_range_len * 10).max(50 * 1024 * 1024);
                    if let Some(content_len) = resp.content_length()
                        && content_len > max_allowed as u64
                    {
                        return Err(CeraError::Backend(format!(
                            "server returned full HTTP 200 body ({content_len} bytes) instead of HTTP 206 Partial Content for range {start}-{end} on `{url}`"
                        )));
                    }
                    let mut reader = std::io::Read::take(resp, max_allowed as u64 + 1);
                    let mut body_bytes = Vec::new();
                    match std::io::Read::read_to_end(&mut reader, &mut body_bytes) {
                        Ok(n) => {
                            if n > max_allowed {
                                return Err(CeraError::Backend(format!(
                                    "server returned full HTTP 200 stream exceeding {max_allowed} bytes instead of HTTP 206 Partial Content for range {start}-{end} on `{url}`"
                                )));
                            }
                            if body_bytes.len() <= end {
                                last_error = format!(
                                    "HTTP 200 OK response from `{url}` too short ({} bytes) for range {start}-{end}",
                                    body_bytes.len()
                                );
                                continue;
                            }
                            out.extend_from_slice(&body_bytes[start..=end]);
                            return Ok(());
                        }
                        Err(e) => last_error = format!("failed reading body from `{url}`: {e}"),
                    }
                } else if is_retryable_status(status) {
                    last_error = format!("HTTP {status} for range request on `{url}`");
                } else {
                    return Err(CeraError::Backend(format!(
                        "HTTP {status} for range request on `{url}`"
                    )));
                }
            }
            Err(e) => {
                last_error = format!("connection error for range request on `{url}`: {e}");
            }
        }
    }

    Err(CeraError::Backend(format!(
        "HTTP range request `{range_val}` to `{url}` failed after {MAX_RETRIES} attempts: {last_error}"
    )))
}

fn fetch_hf_file_range(
    client: &reqwest::blocking::Client,
    url: &str,
    start: usize,
    end: usize,
    auth_token: Option<&str>,
) -> Result<Vec<u8>, CeraError> {
    let mut buf = Vec::new();
    fetch_hf_file_range_into(client, url, start, end, auth_token, &mut buf)?;
    Ok(buf)
}