xybrid-core 0.1.0-rc4

Core runtime for hybrid cloud-edge AI inference: model execution, pipeline orchestration, and routing primitives.
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
//! Tensor conversion utilities for converting between Envelope types and ONNX tensors.
//!
//! This module provides functions to convert:
//! - `Envelope` (Audio/Text/Embedding) → ONNX tensors (`ndarray::Array`)
//! - ONNX tensors → `Envelope`

use crate::ir::{Envelope, EnvelopeKind};
use crate::runtime_adapter::{AdapterError, AdapterResult};
use ndarray::{Array, ArrayD, IxDyn};

/// Converts an `Envelope` to ONNX tensor inputs.
///
/// # Arguments
///
/// * `envelope` - The input envelope containing Audio, Text, or Embedding data
/// * `input_shapes` - Expected input shapes for the model (one per input)
/// * `input_names` - Names of the model inputs
///
/// # Returns
///
/// A HashMap mapping input names to `ndarray::ArrayD<f32>` tensors ready for inference
///
/// # Errors
///
/// Returns an error if:
/// - The envelope type is not supported
/// - Tensor shape conversion fails
/// - Audio/text conversion fails
pub fn envelope_to_tensors(
    envelope: &Envelope,
    input_shapes: &[Vec<i64>],
    input_names: &[String],
) -> AdapterResult<std::collections::HashMap<String, ArrayD<f32>>> {
    if input_shapes.is_empty() || input_names.is_empty() {
        return Err(AdapterError::InvalidInput(
            "No input shapes or names provided".to_string(),
        ));
    }

    let input_name = &input_names[0]; // Use first input name

    // Check if envelope has shape metadata (from preprocessed tensor)
    let shape_from_metadata: Option<Vec<i64>> =
        envelope.metadata.get("tensor_shape").and_then(|s| {
            let parts: Result<Vec<i64>, _> = s.split(',').map(|p| p.parse::<i64>()).collect();
            parts.ok()
        });

    // Use shape from metadata if available, otherwise use model's declared shape
    let target_shape = match &shape_from_metadata {
        Some(shape) => shape.as_slice(),
        None => &input_shapes[0],
    };

    let tensor = match &envelope.kind {
        EnvelopeKind::Audio(audio_data) => audio_to_tensor(audio_data, target_shape)?,
        EnvelopeKind::Text(text) => text_to_tensor(text, target_shape)?,
        EnvelopeKind::Embedding(embedding) => embedding_to_tensor(embedding, target_shape)?,
    };

    let mut result = std::collections::HashMap::new();
    result.insert(input_name.clone(), tensor);
    Ok(result)
}

/// Converts ONNX tensor outputs to an `Envelope`.
///
/// # Arguments
///
/// * `outputs` - HashMap of output names to tensors from ONNX inference
/// * `output_names` - Names of the model outputs (for ordering/selection)
///
/// # Returns
///
/// An `Envelope` containing the inference result
///
/// # Errors
///
/// Returns an error if:
/// - No outputs provided
/// - Tensor extraction fails
/// - Output type detection fails
pub fn tensors_to_envelope(
    outputs: &std::collections::HashMap<String, ArrayD<f32>>,
    output_names: &[String],
) -> AdapterResult<Envelope> {
    if outputs.is_empty() {
        return Err(AdapterError::InvalidInput(
            "No output tensors provided".to_string(),
        ));
    }

    // Use first output for now
    let output_name = output_names.first().map(|s| s.as_str()).unwrap_or("output");
    let output = outputs
        .get(output_name)
        .ok_or_else(|| AdapterError::InvalidInput(format!("Output '{}' not found", output_name)))?;

    // Convert tensor to embedding, preserving shape in metadata
    let data = output
        .as_slice()
        .ok_or_else(|| AdapterError::InvalidInput("Output tensor not contiguous".to_string()))?;

    // Store shape in metadata so postprocessing can reconstruct the tensor
    let shape_str = output
        .shape()
        .iter()
        .map(|d| d.to_string())
        .collect::<Vec<_>>()
        .join(",");

    let mut metadata = std::collections::HashMap::new();
    metadata.insert("tensor_shape".to_string(), shape_str);

    Ok(Envelope::with_metadata(
        EnvelopeKind::Embedding(data.to_vec()),
        metadata,
    ))
}

/// Converts audio bytes to an ONNX tensor.
///
/// # Arguments
///
/// * `audio_data` - Raw audio bytes (WAV file or PCM bytes)
/// * `target_shape` - Expected tensor shape (e.g., `[1, 16000]` for wav2vec2)
///
/// # Returns
///
/// An `ndarray::ArrayD<f32>` tensor containing audio samples as `f32`
///
/// # Supported Formats
///
/// - WAV files: Auto-decoded with resampling to 16kHz mono
/// - Raw 16-bit PCM: Converted directly to f32
///
/// # Wav2Vec2 Format
///
/// Wav2Vec2 expects:
/// - 16kHz sample rate
/// - Mono (1 channel)
/// - Normalized to [-1.0, 1.0]
/// - Shape: `[batch, samples]` (2D tensor with batch dimension)
fn audio_to_tensor(audio_data: &[u8], target_shape: &[i64]) -> AdapterResult<ArrayD<f32>> {
    // Try to decode as WAV file first (most common case)
    let samples = decode_audio_to_samples(audio_data)?;

    if samples.is_empty() {
        return Err(AdapterError::InvalidInput(
            "Audio data is empty".to_string(),
        ));
    }

    // Determine output shape based on target_shape
    // Models like wav2vec2 expect [batch, samples] (2D)
    // Models like Whisper may expect [batch, channels, samples] (3D)
    let final_shape: Vec<usize> = if target_shape.len() == 2 {
        // 2D shape [batch, samples] - most common for wav2vec2
        let batch = if target_shape[0] == -1 {
            1
        } else {
            target_shape[0] as usize
        };
        let num_samples = if target_shape[1] == -1 {
            samples.len()
        } else {
            target_shape[1] as usize
        };
        vec![batch, num_samples]
    } else if target_shape.len() == 3 {
        // 3D shape [batch, channels, samples] - for Whisper-style models
        let batch = if target_shape[0] == -1 {
            1
        } else {
            target_shape[0] as usize
        };
        let channels = if target_shape[1] == -1 {
            1
        } else {
            target_shape[1] as usize
        };
        let num_samples = if target_shape[2] == -1 {
            samples.len()
        } else {
            target_shape[2] as usize
        };
        vec![batch, channels, num_samples]
    } else if target_shape.len() == 1 {
        // 1D shape - add batch dimension to make 2D for model compatibility
        let num_samples = if target_shape[0] == -1 {
            samples.len()
        } else {
            target_shape[0] as usize
        };
        vec![1, num_samples]
    } else {
        return Err(AdapterError::InvalidInput(format!(
            "Unsupported target shape dimensions: {:?}",
            target_shape
        )));
    };

    // Calculate expected size from shape
    let expected_size: usize = final_shape.iter().product();

    // Handle sample count mismatch (pad or truncate)
    let final_samples = if samples.len() < expected_size {
        // Pad with zeros (silence)
        let mut padded = samples;
        padded.resize(expected_size, 0.0);
        padded
    } else if samples.len() > expected_size {
        // Truncate to expected size
        samples[..expected_size].to_vec()
    } else {
        samples
    };

    Array::from_shape_vec(IxDyn(&final_shape), final_samples)
        .map_err(|e| AdapterError::RuntimeError(format!("Failed to create audio tensor: {}", e)))
}

/// Decodes audio bytes (WAV or raw PCM) to f32 samples.
///
/// Returns samples normalized to [-1.0, 1.0] at 16kHz mono.
fn decode_audio_to_samples(audio_data: &[u8]) -> AdapterResult<Vec<f32>> {
    use std::io::Cursor;

    // Try WAV decoding first
    let cursor = Cursor::new(audio_data);
    match hound::WavReader::new(cursor) {
        Ok(mut reader) => {
            let spec = reader.spec();
            let source_sample_rate = spec.sample_rate;
            let source_channels = spec.channels as usize;

            // Read samples as f32
            let samples: Vec<f32> = match spec.sample_format {
                hound::SampleFormat::Float => {
                    reader.samples::<f32>().filter_map(|s| s.ok()).collect()
                }
                hound::SampleFormat::Int => {
                    // Convert int samples to f32 normalized to [-1.0, 1.0]
                    let bits = spec.bits_per_sample;
                    let max_value = (1 << (bits - 1)) as f32;
                    reader
                        .samples::<i32>()
                        .filter_map(|s| s.ok())
                        .map(|s| s as f32 / max_value)
                        .collect()
                }
            };

            // Convert to mono if needed
            let mono_samples = if source_channels > 1 {
                // Average channels for mono conversion
                samples
                    .chunks(source_channels)
                    .map(|chunk| chunk.iter().sum::<f32>() / source_channels as f32)
                    .collect()
            } else {
                samples
            };

            // Resample to 16kHz if needed
            const TARGET_SAMPLE_RATE: u32 = 16000;
            let resampled = if source_sample_rate != TARGET_SAMPLE_RATE {
                let ratio = TARGET_SAMPLE_RATE as f32 / source_sample_rate as f32;
                let target_len = (mono_samples.len() as f32 * ratio) as usize;

                (0..target_len)
                    .map(|i| {
                        let source_idx = (i as f32 / ratio) as usize;
                        mono_samples.get(source_idx).copied().unwrap_or(0.0)
                    })
                    .collect()
            } else {
                mono_samples
            };

            Ok(resampled)
        }
        Err(_) => {
            // Not a WAV file, try raw PCM (16-bit little-endian)
            if !audio_data.len().is_multiple_of(2) {
                return Err(AdapterError::InvalidInput(format!(
                    "Audio data length ({}) must be even for 16-bit PCM",
                    audio_data.len()
                )));
            }

            let samples: Vec<f32> = audio_data
                .chunks_exact(2)
                .map(|chunk| {
                    let sample = i16::from_le_bytes([chunk[0], chunk[1]]);
                    sample as f32 / 32768.0
                })
                .collect();

            Ok(samples)
        }
    }
}

/// Converts text to an ONNX tensor (token IDs).
///
/// # Arguments
///
/// * `text` - Input text string
/// * `target_shape` - Expected tensor shape (e.g., `[1, 512]` for sequence length 512)
///
/// # Returns
///
/// An `ndarray::ArrayD<f32>` tensor containing token IDs as `f32` (converted from i64)
///
/// # Note
///
/// This is a simple tokenization implementation for MVP.
/// Real implementations should use proper tokenizers (BPE, SentencePiece, etc.)
fn text_to_tensor(text: &str, target_shape: &[i64]) -> AdapterResult<ArrayD<f32>> {
    // Simple tokenization: split by whitespace and map to IDs
    // This is a placeholder - real tokenizers should be integrated later
    let tokens: Vec<i64> = text
        .split_whitespace()
        .enumerate()
        .map(|(i, _)| i as i64)
        .collect();

    let actual_size = tokens.len();

    // Check if shape contains dynamic dimensions (-1)
    let has_dynamic = target_shape.iter().any(|&d| d < 0);

    if has_dynamic {
        // For dynamic shapes, use the actual token count
        let shape: Vec<usize> = if target_shape == [-1] {
            vec![actual_size]
        } else if target_shape.len() == 2 {
            let batch = if target_shape[0] > 0 {
                target_shape[0] as usize
            } else {
                1
            };
            vec![batch, actual_size]
        } else {
            vec![actual_size]
        };

        let tokens_f32: Vec<f32> = tokens.iter().map(|&t| t as f32).collect();
        return Array::from_shape_vec(IxDyn(&shape), tokens_f32).map_err(|e| {
            AdapterError::RuntimeError(format!("Failed to create text tensor: {}", e))
        });
    }

    // Calculate expected size from static shape
    let expected_size: i64 = target_shape.iter().product();

    // Pad or truncate to match shape
    let final_tokens = if (actual_size as i64) < expected_size {
        // Pad with zeros (or special token ID)
        let mut padded = tokens;
        padded.resize(expected_size as usize, 0);
        padded
    } else {
        // Truncate
        tokens[..expected_size as usize].to_vec()
    };

    // Create tensor (convert i64 to f32)
    let shape: Vec<usize> = target_shape.iter().map(|&s| s as usize).collect();
    let tokens_f32: Vec<f32> = final_tokens.iter().map(|&t| t as f32).collect();

    Array::from_shape_vec(IxDyn(&shape), tokens_f32)
        .map_err(|e| AdapterError::RuntimeError(format!("Failed to create text tensor: {}", e)))
}

/// Converts an embedding vector to an ONNX tensor.
///
/// # Arguments
///
/// * `embedding` - Embedding vector (f32 values)
/// * `target_shape` - Expected tensor shape
///
/// # Returns
///
/// An `ndarray::ArrayD<f32>` tensor containing embedding values as `f32`
fn embedding_to_tensor(embedding: &[f32], target_shape: &[i64]) -> AdapterResult<ArrayD<f32>> {
    let actual_size = embedding.len();

    // Check if shape contains dynamic dimensions (-1)
    let has_dynamic = target_shape.iter().any(|&d| d < 0);

    if has_dynamic {
        // For dynamic shapes, infer the shape from data
        // If shape is just [-1], treat the embedding as-is (1D tensor)
        // Common patterns:
        // - [-1] → [actual_size] (1D)
        // - [1, -1] → [1, actual_size] (batch of 1)
        // - [-1, -1] → [1, actual_size] (assume batch=1)

        let shape: Vec<usize> = if target_shape == [-1] {
            // Single dynamic dimension: create 1D tensor
            vec![actual_size]
        } else if target_shape.len() == 2 {
            // 2D with dynamics: [batch, features] or similar
            let batch = if target_shape[0] > 0 {
                target_shape[0] as usize
            } else {
                1
            };
            let features = if target_shape[1] > 0 {
                target_shape[1] as usize
            } else {
                actual_size / batch
            };
            vec![batch, features]
        } else {
            // Fallback: treat as 1D
            vec![actual_size]
        };

        return Array::from_shape_vec(IxDyn(&shape), embedding.to_vec()).map_err(|e| {
            AdapterError::RuntimeError(format!("Failed to create embedding tensor: {}", e))
        });
    }

    // Calculate expected size from static shape
    let expected_size: i64 = target_shape.iter().product();

    // Validate shape matches data size
    if actual_size as i64 != expected_size {
        return Err(AdapterError::InvalidInput(format!(
            "Embedding size mismatch: expected {}, got {}",
            expected_size, actual_size
        )));
    }

    // Create tensor with specified shape
    let shape: Vec<usize> = target_shape.iter().map(|&s| s as usize).collect();

    Array::from_shape_vec(IxDyn(&shape), embedding.to_vec()).map_err(|e| {
        AdapterError::RuntimeError(format!("Failed to create embedding tensor: {}", e))
    })
}

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

    #[test]
    fn test_audio_to_tensor() {
        // Create sample PCM audio (16-bit, 2 bytes per sample)
        let audio_data = vec![0u8; 32000]; // 16k samples * 2 bytes = 32k bytes
        let target_shape = vec![1, 1, 16000]; // [batch, channels, samples]

        let result = audio_to_tensor(&audio_data, &target_shape);
        assert!(result.is_ok());
    }

    #[test]
    fn test_text_to_tensor() {
        let text = "hello world test";
        let target_shape = vec![1, 512]; // [batch, sequence_length]

        let result = text_to_tensor(text, &target_shape);
        assert!(result.is_ok());
    }

    #[test]
    fn test_embedding_to_tensor() {
        let embedding = vec![0.1, 0.2, 0.3, 0.4];
        let target_shape = vec![1, 4]; // [batch, embedding_dim]

        let result = embedding_to_tensor(&embedding, &target_shape);
        assert!(result.is_ok());
    }

    #[test]
    fn test_envelope_to_tensors_audio() {
        let envelope = Envelope::new(EnvelopeKind::Audio(vec![0u8; 32000]));
        let input_shapes = vec![vec![1, 1, 16000]];
        let input_names = vec!["audio_input".to_string()];

        let result = envelope_to_tensors(&envelope, &input_shapes, &input_names);
        assert!(result.is_ok());
        let tensors = result.unwrap();
        assert!(tensors.contains_key("audio_input"));
    }

    #[test]
    fn test_envelope_to_tensors_text() {
        let envelope = Envelope::new(EnvelopeKind::Text("hello world".to_string()));
        let input_shapes = vec![vec![1, 512]];
        let input_names = vec!["text_input".to_string()];

        let result = envelope_to_tensors(&envelope, &input_shapes, &input_names);
        assert!(result.is_ok());
        let tensors = result.unwrap();
        assert!(tensors.contains_key("text_input"));
    }
}