{"record":{"id":"92e882924494a8ab","repo":"huggingface/candle","slug":"empty-codebooks-92e882","errorCode":null,"errorMessage":"empty codebooks","messagePattern":"empty codebooks","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-transformers/src/models/snac.rs","lineNumber":718,"sourceCode":"        Ok((z_q, codes))\n    }\n\n    #[allow(clippy::wrong_self_convention)]\n    fn from_codes(&self, codes: &[&Tensor]) -> Result<Tensor> {\n        let mut sum = None;\n        for (quantizer, codes) in self.quantizers.iter().zip(codes.iter()) {\n            let z_p_i = quantizer.decode_code(codes)?;\n            let z_q_i = z_p_i.apply(&quantizer.out_proj)?;\n            let z_q_i = repeat_interleave(&z_q_i, quantizer.stride, D::Minus1)?;\n            let s = match sum {\n                None => z_q_i,\n                Some(s) => (s + z_q_i)?,\n            };\n            sum = Some(s)\n        }\n        match sum {\n            Some(s) => Ok(s),\n            None => candle::bail!(\"empty codebooks\"),\n        }\n    }\n}\n\nfn gcd(mut a: usize, mut b: usize) -> usize {\n    while b != 0 {\n        let t = b;\n        b = a % b;\n        a = t;\n    }\n    a\n}\n\nfn lcm(a: usize, b: usize) -> usize {\n    a / gcd(a, b) * b\n}\n\n// https://github.com/hubertsiuzdak/snac/blob/main/snac/snac.py","sourceCodeStart":700,"sourceCodeEnd":736,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-transformers/src/models/snac.rs#L700-L736","documentation":"The quantizer/dequantizer accumulation helper sums per-codebook contributions into an Option<Tensor>; if there were zero codebooks to iterate over, sum stays None and the function bails with 'empty codebooks'. It guarantees the function never silently returns an empty result, but indicates the model config defined no codebooks.","triggerScenarios":"Calling the forward/dequantize path of the SNAC quantizer with a model whose codebook list is empty (no codebook weights loaded or codebook count 0 in config).","commonSituations":"Constructing Snac/quantizer from a config with num_quantizers=0; VarBuilder names mismatching so codebook weights are never loaded into the vector; partially initialized model used before loading weights.","solutions":["Verify the SNAC config specifies a positive number of codebooks (e.g. codebook_size/quantizer count > 0)","Check VarBuilder checkpoint keys match expected codebook naming so all codebooks load","Inspect how the codebooks vector is populated; ensure it is filled before forward is called"],"exampleFix":"// before\nlet quantizer = SnacQuantizer::new(cfg_with_zero_codebooks, vb)?;\n// after\nassert!(cfg.num_codebooks > 0);\nlet quantizer = SnacQuantizer::new(cfg, vb)?;","handlingStrategy":"validation","validationCode":"if cfg.num_codebooks == 0 {\n    return Err(anyhow::anyhow!(\"SNAC config must define at least one codebook\"));\n}","typeGuard":null,"tryCatchPattern":"match quantizer.forward(&z) {\n    Ok(out) => out,\n    Err(e) if e.to_string().contains(\"empty codebooks\") => {\n        anyhow::bail!(\"SNAC quantizer has no codebooks loaded — check checkpoint keys\")\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Validate codebook count > 0 right after config load","Log the number of codebook weights successfully loaded from VarBuilder","Use vb.pp prefixes matching the checkpoint layout and assert on missing tensors"],"tags":["rust","candle","audio","snac","empty-input"],"backgroundTag":"empty-codebook-list","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}