pub struct VqCodebook<B: Backend> { /* private fields */ }Expand description
VQ Semantic Codebook for dequantizing semantic token indices.
Uses EMA (Exponential Moving Average) codebook: the embedding for each
entry is embedding_sum / cluster_usage. This normalizes accumulated
embeddings by how often each codebook entry was used during training.
Pre-normalizes embeddings to CPU cache at construction to avoid GPU readback during dequantize() (required for WASM compatibility).
Implementations§
Source§impl<B: Backend> VqCodebook<B>
impl<B: Backend> VqCodebook<B>
Sourcepub fn new(
embedding_sum: Tensor<B, 2>,
cluster_usage: Tensor<B, 1>,
cpu_normalized: Vec<f32>,
) -> Self
pub fn new( embedding_sum: Tensor<B, 2>, cluster_usage: Tensor<B, 1>, cpu_normalized: Vec<f32>, ) -> Self
Create VQ codebook from loaded tensors and pre-computed CPU cache.
Use Self::precompute_normalized to build the CPU cache from raw
f32 slices (before uploading to GPU) to avoid GPU readback.
Sourcepub fn precompute_normalized(
embed_vals: &[f32],
usage_vals: &[f32],
n_entries: usize,
embed_dim: usize,
) -> Vec<f32>
pub fn precompute_normalized( embed_vals: &[f32], usage_vals: &[f32], n_entries: usize, embed_dim: usize, ) -> Vec<f32>
Pre-compute normalized embeddings from raw f32 slices.
Call this on CPU-side data BEFORE constructing tensors, to avoid any GPU readback (required for WASM compatibility).
Sourcepub fn from_safetensors(
safetensors: &SafeTensors<'_>,
device: &B::Device,
) -> Result<Self>
pub fn from_safetensors( safetensors: &SafeTensors<'_>, device: &B::Device, ) -> Result<Self>
Load VQ codebook from SafeTensors.
Expects:
audio_tokenizer.quantizer.semantic_codebook.embedding_sum[8192, 256]audio_tokenizer.quantizer.semantic_codebook.cluster_usage[8192]