Skip to main content

rig_candle/
artifacts.rs

1//! Caller-supplied model artifact buffers and inexpensive buffer validation.
2
3use crate::CandleError;
4
5/// Owned model artifacts for exactly one unsharded checkpoint.
6#[derive(Debug)]
7pub struct ModelData {
8    /// Contents of `config.json`.
9    pub config: Vec<u8>,
10    /// Contents of `tokenizer.json`.
11    pub tokenizer: Vec<u8>,
12    /// Contents of one safetensors or GGUF checkpoint, as identified by [`ModelArtifacts`].
13    pub weights: Vec<u8>,
14}
15
16/// Borrowed GGUF artifacts for zero-copy loading from embedded/static bytes.
17#[derive(Debug, Clone, Copy)]
18pub struct GgufModelData<'a> {
19    /// Contents of `config.json`.
20    pub config: &'a [u8],
21    /// Contents of `tokenizer.json`.
22    pub tokenizer: &'a [u8],
23    /// Contents of one GGUF checkpoint.
24    pub weights: &'a [u8],
25}
26
27/// Byte-backed checkpoint format supplied to [`crate::CandleModel`].
28#[derive(Debug)]
29pub enum ModelArtifacts {
30    /// One unsharded Hugging Face safetensors checkpoint.
31    Safetensors(ModelData),
32    /// A validated SmolLM2 or Qwen3 Q4_K_M GGUF checkpoint.
33    Gguf(ModelData),
34}
35
36pub(crate) fn require_nonempty(bytes: &[u8], artifact: &'static str) -> Result<(), CandleError> {
37    if bytes.is_empty() {
38        Err(CandleError::EmptyBuffer { artifact })
39    } else {
40        Ok(())
41    }
42}