unillm-runtime 0.1.0

Core inference runtime for UniLLM with 47 model architectures
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
//! EnCodec Model V2 - Neural Audio Codec
//!
//! CNN-based encoder/decoder for audio compression using residual vector quantization

use crate::model_config;
use super::traits::*;
use anyhow::Result;
use serde::{Serialize, Deserialize};

model_config!(EncodecConfig {
    vocab_size: usize = 1024,  // Same as codebook_size for compatibility
    hidden_size: usize = 128,
    num_hidden_layers: usize = 4,  // Number of upsampling stages
    audio_channels: usize = 1,
    sample_rate: usize = 24000,
    num_filters: usize = 32,
    num_residual_layers: usize = 1,
    upsampling_ratios: Vec<usize> = vec![8, 5, 4, 2],
    norm_type: String = "weight_norm".to_string(),
    codebook_size: usize = 1024,
    codebook_dim: usize = 128,
    num_codebooks: usize = 32,
    use_causal_conv: bool = true,
    pad_mode: String = "reflect".to_string(),
    bandwidth: f32 = 6.0,
    layer_norm_eps: f32 = 1e-5,
    pad_token_id: i64 = 0,
    bos_token_id: i64 = 1,
    eos_token_id: i64 = 2,
});

impl EncodecConfig {
    pub fn from_gguf_config(gguf: &crate::weight_loader_core::GGUFModelConfig) -> Self {
        Self {
            hidden_size: gguf.hidden_size,
            ..Default::default()
        }
    }
}

pub struct EncodecModelV2 {
    config: EncodecConfig,
    device: Device,
    encoder: EncodecEncoder,
    decoder: EncodecDecoder,
    quantizer: ResidualVectorQuantizer,
}

pub struct EncodecEncoder {
    conv_in: EncodecConv1d,
    down_blocks: Vec<EncodecDownsampleBlock>,
    conv_out: EncodecConv1d,
    lstm: Option<EncodecLSTM>,
}

pub struct EncodecDecoder {
    conv_in: EncodecConv1d,
    up_blocks: Vec<EncodecUpsampleBlock>,
    conv_out: EncodecConv1d,
    lstm: Option<EncodecLSTM>,
}

pub struct EncodecDownsampleBlock {
    conv_layers: Vec<EncodecConv1d>,
    downsample: EncodecConv1d,
    residual_layers: Vec<EncodecResidualUnit>,
}

pub struct EncodecUpsampleBlock {
    upsample: EncodecConvTranspose1d,
    conv_layers: Vec<EncodecConv1d>,
    residual_layers: Vec<EncodecResidualUnit>,
}

pub struct EncodecResidualUnit {
    conv1: EncodecConv1d,
    conv2: EncodecConv1d,
}

pub struct EncodecConv1d {
    weight: Tensor,
    bias: Option<Tensor>,
    in_channels: usize,
    out_channels: usize,
    kernel_size: usize,
    stride: usize,
    padding: usize,
}

pub struct EncodecConvTranspose1d {
    weight: Tensor,
    bias: Option<Tensor>,
    in_channels: usize,
    out_channels: usize,
    kernel_size: usize,
    stride: usize,
}

pub struct EncodecLSTM {
    weight_ih: Tensor,
    weight_hh: Tensor,
    bias_ih: Tensor,
    bias_hh: Tensor,
    hidden_size: usize,
    num_layers: usize,
}

pub struct ResidualVectorQuantizer {
    codebooks: Vec<Tensor>,  // [num_codebooks, codebook_size, codebook_dim]
    codebook_size: usize,
    codebook_dim: usize,
    num_codebooks: usize,
}

impl Model for EncodecModelV2 {
    type Config = EncodecConfig;

    fn new(config: EncodecConfig) -> Result<Self> {
        let device = Device::CPU;

        let encoder = EncodecEncoder::new(&config, &device)?;
        let decoder = EncodecDecoder::new(&config, &device)?;
        let quantizer = ResidualVectorQuantizer::new(&config, &device)?;

        Ok(Self { config, device, encoder, decoder, quantizer })
    }

    fn from_weights(config: EncodecConfig, weights: ModelWeights) -> Result<Self> {
        let model = Self::new(config)?;
        // Weight loading would be implemented here
        Ok(model)
    }

    fn forward(&self, inputs: &ModelInputs) -> Result<ModelOutputs> {
        match inputs {
            ModelInputs::Audio { input_features, .. } => {
                // Encode: audio -> latent codes
                let encoded = self.encoder.forward(input_features)?;

                // Quantize: continuous latents -> discrete codes
                let (quantized, codes) = self.quantizer.forward(&encoded)?;

                // Decode: discrete codes -> reconstructed audio
                let decoded = self.decoder.forward(&quantized)?;

                // Return encoded representation
                Ok(ModelOutputs::Logits {
                    logits: decoded,
                    hidden_states: Some(encoded)
                })
            }
            _ => Err(anyhow::anyhow!("Encodec requires audio input")),
        }
    }

    fn generate(&self, _prompt: &str, _config: &GenerationConfig) -> Result<String> {
        Err(anyhow::anyhow!("Encodec is a codec model, use encode/decode methods"))
    }

    fn config(&self) -> &Self::Config { &self.config }

    fn memory_requirements(&self) -> MemoryRequirements {
        let p = self.config.hidden_size * self.config.num_filters * 1000 * 4;
        MemoryRequirements { gpu_memory: p, cpu_memory: p / 4, kv_cache_memory: 0, peak_memory: p * 2 }
    }

    fn to_device(&mut self, device: &Device) -> Result<()> {
        self.device = device.clone();
        Ok(())
    }
}

impl EncodecEncoder {
    fn new(config: &EncodecConfig, device: &Device) -> Result<Self> {
        let mut channels = config.num_filters;

        // Initial conv: audio_channels -> num_filters
        let conv_in = EncodecConv1d::new(config.audio_channels, channels, 7, 1, 3, device)?;

        // Downsample blocks
        let mut down_blocks = Vec::new();
        for &ratio in &config.upsampling_ratios {
            let out_channels = channels * 2;
            down_blocks.push(EncodecDownsampleBlock::new(
                channels, out_channels, ratio, config.num_residual_layers, device
            )?);
            channels = out_channels;
        }

        // Output conv
        let conv_out = EncodecConv1d::new(channels, config.hidden_size, 7, 1, 3, device)?;

        // Optional LSTM for sequential modeling
        let lstm = Some(EncodecLSTM::new(config.hidden_size, 2, device)?);

        Ok(Self { conv_in, down_blocks, conv_out, lstm })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let mut x = self.conv_in.forward(input)?;
        x = elu(&x)?;

        for block in &self.down_blocks {
            x = block.forward(&x)?;
        }

        x = self.conv_out.forward(&x)?;

        if let Some(ref lstm) = self.lstm {
            x = lstm.forward(&x)?;
        }

        Ok(x)
    }
}

// ELU activation: max(0, x) + min(0, alpha * (exp(x) - 1))
fn elu(input: &Tensor) -> Result<Tensor> {
    let x = input.to_candle()?;
    let result = x.elu(1.0)?;
    Ok(Tensor::from_candle(result))
}

impl EncodecDecoder {
    fn new(config: &EncodecConfig, device: &Device) -> Result<Self> {
        let mut channels = config.hidden_size;

        // Initial conv
        let conv_in = EncodecConv1d::new(config.hidden_size, channels, 7, 1, 3, device)?;

        // Optional LSTM
        let lstm = Some(EncodecLSTM::new(channels, 2, device)?);

        // Upsample blocks (reverse order of downsampling)
        let mut up_blocks = Vec::new();
        let ratios: Vec<_> = config.upsampling_ratios.iter().rev().copied().collect();

        for (i, &ratio) in ratios.iter().enumerate() {
            let out_channels = if i == ratios.len() - 1 {
                config.num_filters
            } else {
                channels / 2
            };
            up_blocks.push(EncodecUpsampleBlock::new(
                channels, out_channels, ratio, config.num_residual_layers, device
            )?);
            channels = out_channels;
        }

        // Output conv: num_filters -> audio_channels
        let conv_out = EncodecConv1d::new(config.num_filters, config.audio_channels, 7, 1, 3, device)?;

        Ok(Self { conv_in, up_blocks, conv_out, lstm })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let mut x = self.conv_in.forward(input)?;

        if let Some(ref lstm) = self.lstm {
            x = lstm.forward(&x)?;
        }

        x = elu(&x)?;

        for block in &self.up_blocks {
            x = block.forward(&x)?;
        }

        self.conv_out.forward(&x)
    }
}

impl EncodecDownsampleBlock {
    fn new(in_channels: usize, out_channels: usize, ratio: usize, num_residual: usize, device: &Device) -> Result<Self> {
        let mut conv_layers = Vec::new();
        let mut residual_layers = Vec::new();

        // Residual units before downsampling
        for _ in 0..num_residual {
            residual_layers.push(EncodecResidualUnit::new(in_channels, device)?);
        }

        // Pre-downsample conv
        conv_layers.push(EncodecConv1d::new(in_channels, in_channels, 3, 1, 1, device)?);

        // Downsample conv with stride
        let downsample = EncodecConv1d::new(in_channels, out_channels, ratio * 2, ratio, ratio / 2, device)?;

        Ok(Self { conv_layers, downsample, residual_layers })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let mut x = input.clone();

        for layer in &self.residual_layers {
            x = layer.forward(&x)?;
        }

        for conv in &self.conv_layers {
            x = elu(&conv.forward(&x)?)?;
        }

        self.downsample.forward(&x)
    }
}

impl EncodecUpsampleBlock {
    fn new(in_channels: usize, out_channels: usize, ratio: usize, num_residual: usize, device: &Device) -> Result<Self> {
        // Upsample with transposed conv
        let upsample = EncodecConvTranspose1d::new(in_channels, out_channels, ratio * 2, ratio, device)?;

        let mut conv_layers = Vec::new();
        let mut residual_layers = Vec::new();

        // Post-upsample conv
        conv_layers.push(EncodecConv1d::new(out_channels, out_channels, 3, 1, 1, device)?);

        // Residual units after upsampling
        for _ in 0..num_residual {
            residual_layers.push(EncodecResidualUnit::new(out_channels, device)?);
        }

        Ok(Self { upsample, conv_layers, residual_layers })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let mut x = self.upsample.forward(input)?;

        for conv in &self.conv_layers {
            x = elu(&conv.forward(&x)?)?;
        }

        for layer in &self.residual_layers {
            x = layer.forward(&x)?;
        }

        Ok(x)
    }
}

impl EncodecResidualUnit {
    fn new(channels: usize, device: &Device) -> Result<Self> {
        let conv1 = EncodecConv1d::new(channels, channels, 3, 1, 1, device)?;
        let conv2 = EncodecConv1d::new(channels, channels, 1, 1, 0, device)?;
        Ok(Self { conv1, conv2 })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let x = elu(&self.conv1.forward(input)?)?;
        let x = self.conv2.forward(&x)?;
        ops_fn::add(input, &x)
    }
}

impl EncodecConv1d {
    fn new(in_channels: usize, out_channels: usize, kernel_size: usize, stride: usize, padding: usize, device: &Device) -> Result<Self> {
        let weight = ops_fn::zeros(&[out_channels, in_channels, kernel_size], DataType::Float32, device)?;
        let bias = Some(ops_fn::zeros(&[out_channels], DataType::Float32, device)?);
        Ok(Self { weight, bias, in_channels, out_channels, kernel_size, stride, padding })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        let out = ops_fn::conv1d(input, &self.weight, None, self.stride, self.padding)?;

        if let Some(ref bias) = self.bias {
            let bias_candle = bias.to_candle()?;
            let out_candle = out.to_candle()?;
            let bias_expanded = bias_candle.reshape(&[1, self.out_channels, 1])?;
            Ok(Tensor::from_candle(out_candle.broadcast_add(&bias_expanded)?))
        } else {
            Ok(out)
        }
    }
}

impl EncodecConvTranspose1d {
    fn new(in_channels: usize, out_channels: usize, kernel_size: usize, stride: usize, device: &Device) -> Result<Self> {
        let weight = ops_fn::zeros(&[in_channels, out_channels, kernel_size], DataType::Float32, device)?;
        let bias = Some(ops_fn::zeros(&[out_channels], DataType::Float32, device)?);
        Ok(Self { weight, bias, in_channels, out_channels, kernel_size, stride })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // Simplified transposed conv - would use proper conv_transpose1d in full impl
        let input_candle = input.to_candle()?;
        let weight_candle = self.weight.to_candle()?;

        // For now, use conv with modified dimensions (placeholder)
        // Real implementation would use conv_transpose1d kernel
        let shape = input_candle.shape().dims();
        let (batch, _, seq_len) = (shape[0], shape[1], shape[2]);
        let out_len = seq_len * self.stride;

        // Create output with upsampled size
        let device = input_candle.device();
        let output = candle_core::Tensor::zeros(&[batch, self.out_channels, out_len], candle_core::DType::F32, device)?;

        if let Some(ref bias) = self.bias {
            let bias_candle = bias.to_candle()?;
            let bias_expanded = bias_candle.reshape(&[1, self.out_channels, 1])?;
            Ok(Tensor::from_candle(output.broadcast_add(&bias_expanded)?))
        } else {
            Ok(Tensor::from_candle(output))
        }
    }
}

impl EncodecLSTM {
    fn new(hidden_size: usize, num_layers: usize, device: &Device) -> Result<Self> {
        let weight_ih = ops_fn::zeros(&[4 * hidden_size, hidden_size], DataType::Float32, device)?;
        let weight_hh = ops_fn::zeros(&[4 * hidden_size, hidden_size], DataType::Float32, device)?;
        let bias_ih = ops_fn::zeros(&[4 * hidden_size], DataType::Float32, device)?;
        let bias_hh = ops_fn::zeros(&[4 * hidden_size], DataType::Float32, device)?;
        Ok(Self { weight_ih, weight_hh, bias_ih, bias_hh, hidden_size, num_layers })
    }

    fn forward(&self, input: &Tensor) -> Result<Tensor> {
        // Simplified LSTM: just pass through for now
        // Real implementation would do full LSTM computation
        Ok(input.clone())
    }
}

impl ResidualVectorQuantizer {
    fn new(config: &EncodecConfig, device: &Device) -> Result<Self> {
        let mut codebooks = Vec::with_capacity(config.num_codebooks);

        for _ in 0..config.num_codebooks {
            codebooks.push(ops_fn::zeros(
                &[config.codebook_size, config.codebook_dim],
                DataType::Float32,
                device
            )?);
        }

        Ok(Self {
            codebooks,
            codebook_size: config.codebook_size,
            codebook_dim: config.codebook_dim,
            num_codebooks: config.num_codebooks,
        })
    }

    fn forward(&self, input: &Tensor) -> Result<(Tensor, Vec<Tensor>)> {
        let mut residual = input.clone();
        let input_candle = input.to_candle()?;
        let mut quantized = Tensor::from_candle(input_candle.zeros_like()?);
        let mut codes = Vec::with_capacity(self.num_codebooks);

        for codebook in &self.codebooks {
            // Find nearest codebook entry
            let (code_indices, quantized_part) = self.quantize_residual(&residual, codebook)?;

            // Accumulate quantized values
            quantized = ops_fn::add(&quantized, &quantized_part)?;

            // Update residual
            residual = ops_fn::sub(&residual, &quantized_part)?;

            codes.push(code_indices);
        }

        Ok((quantized, codes))
    }

    fn quantize_residual(&self, input: &Tensor, codebook: &Tensor) -> Result<(Tensor, Tensor)> {
        let input_candle = input.to_candle()?;
        let codebook_candle = codebook.to_candle()?;

        let shape = input_candle.shape().dims();
        let (batch, channels, seq_len) = (shape[0], shape[1], shape[2]);

        // Reshape input: [batch, channels, seq] -> [batch * seq, channels]
        let input_flat = input_candle.transpose(1, 2)?.reshape(&[batch * seq_len, channels])?;

        // Compute distances to codebook entries
        // distance = ||x - c||^2 = ||x||^2 - 2<x,c> + ||c||^2
        let input_sq = input_flat.sqr()?.sum(1)?;
        let codebook_sq = codebook_candle.sqr()?.sum(1)?;
        let inner = input_flat.matmul(&codebook_candle.t()?)?;

        let input_sq_expanded = input_sq.unsqueeze(1)?;
        let codebook_sq_expanded = codebook_sq.unsqueeze(0)?;

        let distances = (input_sq_expanded.broadcast_add(&codebook_sq_expanded)? - (inner * 2.0)?)?;

        // Find minimum distance indices
        let indices = distances.argmin(1)?;

        // Gather quantized values from codebook
        let quantized_flat = codebook_candle.index_select(&indices, 0)?;

        // Reshape back: [batch * seq, channels] -> [batch, channels, seq]
        let quantized = quantized_flat.reshape(&[batch, seq_len, channels])?.transpose(1, 2)?;

        let indices_reshaped = indices.reshape(&[batch, seq_len])?;

        Ok((Tensor::from_candle(indices_reshaped), Tensor::from_candle(quantized)))
    }
}

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

    #[test]
    fn test_encodec_config() {
        let config = EncodecConfig::default();
        assert_eq!(config.hidden_size, 128);
        assert_eq!(config.num_codebooks, 32);
        assert_eq!(config.codebook_size, 1024);
    }

    #[test]
    fn test_encodec_model_creation() {
        let config = EncodecConfig {
            hidden_size: 32,
            num_filters: 8,
            num_residual_layers: 1,
            upsampling_ratios: vec![2, 2],
            num_codebooks: 4,
            codebook_size: 64,
            codebook_dim: 32,
            ..Default::default()
        };

        let model = EncodecModelV2::new(config).unwrap();
        assert_eq!(model.config().hidden_size(), 32);
    }
}