eredu-core 0.1.0

Backend-neutral contracts and orchestration for eredu
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
//! Portable decoded-media requests and chat-placeholder composition.

/// Failure while validating portable media or composing a chat request.
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum MediaRequestError {
    /// An input request must contain at least one segment.
    #[error("multimodal input request is empty")]
    EmptyRequest,
    /// Multimodal preparation is meaningful only when media is present.
    #[error("multimodal input request contains no media")]
    MissingMedia,
    /// RGB dimensions or payload length are invalid.
    #[error("RGB8 image shape {width}x{height} requires {expected} bytes, got {actual}")]
    InvalidRgbImage {
        /// Image width.
        width: u32,
        /// Image height.
        height: u32,
        /// Required packed RGB byte count.
        expected: usize,
        /// Supplied byte count.
        actual: usize,
    },
    /// Audio must have a positive sample rate and finite samples.
    #[error("decoded audio is invalid: {0}")]
    InvalidAudio(String),
    /// Video frame or timing metadata is invalid.
    #[error("decoded video is invalid: {0}")]
    InvalidVideo(String),
    /// A chat media placeholder cannot be empty.
    #[error("chat media binding {index} has an empty placeholder")]
    EmptyPlaceholder {
        /// Binding index.
        index: usize,
    },
    /// Placeholder occurrences do not match supplied bindings.
    #[error(
        "rendered chat contains {actual} occurrence(s) of media placeholder {placeholder:?}, but {expected} binding(s) were supplied"
    )]
    PlaceholderCount {
        /// Complete placeholder spelling.
        placeholder: String,
        /// Number of supplied bindings with that spelling.
        expected: usize,
        /// Number of occurrences in rendered text.
        actual: usize,
    },
    /// Bindings were not ordered like their occurrences in the prompt.
    #[error(
        "chat media binding {index} placeholder {placeholder:?} does not occur after the preceding binding"
    )]
    PlaceholderOrder {
        /// Binding index.
        index: usize,
        /// Complete placeholder spelling.
        placeholder: String,
    },
}

/// Packed decoded RGB8 image owned by a portable request.
#[derive(Debug, Clone, PartialEq)]
pub struct RgbImage {
    pixels: Vec<u8>,
    width: u32,
    height: u32,
}

impl RgbImage {
    /// Validates and owns one packed RGB8 image.
    pub fn new(pixels: Vec<u8>, width: u32, height: u32) -> Result<Self, MediaRequestError> {
        let expected = usize::try_from(width)
            .ok()
            .zip(usize::try_from(height).ok())
            .and_then(|(width, height)| width.checked_mul(height))
            .and_then(|pixels| pixels.checked_mul(3))
            .unwrap_or(usize::MAX);
        if width == 0 || height == 0 || pixels.len() != expected {
            return Err(MediaRequestError::InvalidRgbImage {
                width,
                height,
                expected,
                actual: pixels.len(),
            });
        }
        Ok(Self {
            pixels,
            width,
            height,
        })
    }

    /// Packed RGB channel bytes in row-major order.
    pub fn pixels(&self) -> &[u8] {
        &self.pixels
    }

    /// Image width in pixels.
    pub const fn width(&self) -> u32 {
        self.width
    }

    /// Image height in pixels.
    pub const fn height(&self) -> u32 {
        self.height
    }
}

/// Decoded mono floating-point PCM audio.
#[derive(Debug, Clone, PartialEq)]
pub struct Audio {
    samples: Vec<f32>,
    sample_rate: u32,
}

impl Audio {
    /// Validates and owns decoded mono PCM samples.
    pub fn new(samples: Vec<f32>, sample_rate: u32) -> Result<Self, MediaRequestError> {
        if sample_rate == 0 {
            return Err(MediaRequestError::InvalidAudio(
                "sample rate must be positive".into(),
            ));
        }
        if samples.is_empty() {
            return Err(MediaRequestError::InvalidAudio(
                "waveform must contain at least one sample".into(),
            ));
        }
        if samples.iter().any(|sample| !sample.is_finite()) {
            return Err(MediaRequestError::InvalidAudio(
                "waveform samples must be finite".into(),
            ));
        }
        Ok(Self {
            samples,
            sample_rate,
        })
    }

    /// Mono PCM samples.
    pub fn samples(&self) -> &[f32] {
        &self.samples
    }

    /// Sampling rate in hertz.
    pub const fn sample_rate(&self) -> u32 {
        self.sample_rate
    }
}

/// Frame-selection policy for decoded video.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub enum VideoSampling {
    /// Uses the selected backend processor's default policy.
    #[default]
    ProcessorDefault,
    /// Uniformly samples approximately this many frames per second.
    Fps(f64),
    /// Uniformly samples exactly this many frames, capped by source length.
    FrameCount(usize),
    /// Uses every decoded source frame.
    All,
}

/// Decoded RGB8 video and portable sampling policy.
#[derive(Debug, Clone, PartialEq)]
pub struct Video {
    frames: Vec<RgbImage>,
    source_fps: Option<f64>,
    sampling: VideoSampling,
}

impl Video {
    /// Validates and owns decoded video frames.
    pub fn new(
        frames: Vec<RgbImage>,
        source_fps: Option<f64>,
        sampling: VideoSampling,
    ) -> Result<Self, MediaRequestError> {
        if frames.is_empty() {
            return Err(MediaRequestError::InvalidVideo(
                "video must contain at least one frame".into(),
            ));
        }
        if source_fps.is_some_and(|fps| !fps.is_finite() || fps <= 0.0) {
            return Err(MediaRequestError::InvalidVideo(
                "source frame rate must be finite and positive".into(),
            ));
        }
        match sampling {
            VideoSampling::Fps(fps) if !fps.is_finite() || fps <= 0.0 => {
                return Err(MediaRequestError::InvalidVideo(
                    "sampling frame rate must be finite and positive".into(),
                ));
            }
            VideoSampling::FrameCount(0) => {
                return Err(MediaRequestError::InvalidVideo(
                    "sampling frame count must be positive".into(),
                ));
            }
            _ => {}
        }
        Ok(Self {
            frames,
            source_fps,
            sampling,
        })
    }

    /// Decoded frames in source order.
    pub fn frames(&self) -> &[RgbImage] {
        &self.frames
    }

    /// Source frame rate when known.
    pub const fn source_fps(&self) -> Option<f64> {
        self.source_fps
    }

    /// Requested frame-selection policy.
    pub const fn sampling(&self) -> VideoSampling {
        self.sampling
    }
}

/// One decoded media item in a portable request.
#[derive(Debug, Clone, PartialEq)]
pub enum Media {
    /// Decoded packed RGB8 image.
    Image(RgbImage),
    /// Decoded RGB8 video.
    Video(Video),
    /// Decoded mono PCM audio.
    Audio(Audio),
}

/// One ordered portable model-input segment.
#[derive(Debug, Clone, PartialEq)]
pub enum MultimodalSegment {
    /// Text to encode with [`MultimodalRequest::tokenize`].
    Text(String),
    /// Text already represented as tokenizer vocabulary IDs.
    TokenIds(Vec<u32>),
    /// Decoded media preprocessed by the selected backend.
    Media(Media),
}

/// Validated ordered text and decoded-media input.
#[derive(Debug, Clone, PartialEq)]
pub struct MultimodalRequest {
    segments: Vec<MultimodalSegment>,
}

impl MultimodalRequest {
    /// Validates an ordered multimodal request.
    pub fn new(segments: Vec<MultimodalSegment>) -> Result<Self, MediaRequestError> {
        if segments.is_empty() {
            return Err(MediaRequestError::EmptyRequest);
        }
        if !segments
            .iter()
            .any(|segment| matches!(segment, MultimodalSegment::Media(_)))
        {
            return Err(MediaRequestError::MissingMedia);
        }
        Ok(Self { segments })
    }

    /// Composes a rendered prompt with media bound to exact placeholder occurrences.
    pub fn from_chat(
        rendered_prompt: &str,
        bindings: &[MediaBinding],
    ) -> Result<Self, MediaRequestError> {
        validate_bindings(rendered_prompt, bindings)?;
        let mut segments = Vec::with_capacity(bindings.len().saturating_mul(2) + 1);
        let mut cursor = 0;
        for (index, binding) in bindings.iter().enumerate() {
            let remainder = &rendered_prompt[cursor..];
            let relative = remainder.find(binding.placeholder()).ok_or_else(|| {
                MediaRequestError::PlaceholderOrder {
                    index,
                    placeholder: binding.placeholder().into(),
                }
            })?;
            let start = cursor + relative;
            if start > cursor {
                segments.push(MultimodalSegment::Text(
                    rendered_prompt[cursor..start].into(),
                ));
            }
            segments.push(MultimodalSegment::Media(binding.media().clone()));
            cursor = start + binding.placeholder().len();
        }
        if cursor < rendered_prompt.len() {
            segments.push(MultimodalSegment::Text(rendered_prompt[cursor..].into()));
        }
        Self::new(segments)
    }

    /// Ordered request segments.
    pub fn segments(&self) -> &[MultimodalSegment] {
        &self.segments
    }

    /// Encodes text while preserving media order for backend preparation.
    pub fn tokenize<E>(
        &self,
        mut encode: impl FnMut(&str) -> Result<Vec<u32>, E>,
    ) -> Result<TokenizedMultimodalRequest, E> {
        let mut segments = Vec::with_capacity(self.segments.len());
        for segment in &self.segments {
            segments.push(match segment {
                MultimodalSegment::Text(text) => {
                    TokenizedMultimodalSegment::TokenIds(encode(text)?)
                }
                MultimodalSegment::TokenIds(ids) => {
                    TokenizedMultimodalSegment::TokenIds(ids.clone())
                }
                MultimodalSegment::Media(media) => TokenizedMultimodalSegment::Media(media.clone()),
            });
        }
        Ok(TokenizedMultimodalRequest { segments })
    }
}

/// One exact rendered-prompt placeholder bound to decoded media.
#[derive(Debug, Clone, PartialEq)]
pub struct MediaBinding {
    placeholder: String,
    media: Media,
}

impl MediaBinding {
    /// Binds decoded media to one complete placeholder spelling.
    pub fn new(placeholder: impl Into<String>, media: Media) -> Self {
        Self {
            placeholder: placeholder.into(),
            media,
        }
    }

    /// Complete placeholder spelling.
    pub fn placeholder(&self) -> &str {
        &self.placeholder
    }

    /// Bound decoded media.
    pub const fn media(&self) -> &Media {
        &self.media
    }
}

/// One ordered segment after facade tokenization and before backend preprocessing.
#[derive(Debug, Clone, PartialEq)]
pub enum TokenizedMultimodalSegment {
    /// Tokenizer vocabulary IDs.
    TokenIds(Vec<u32>),
    /// Decoded media.
    Media(Media),
}

/// Backend preparation request with tokenizer work already complete.
#[derive(Debug, Clone, PartialEq)]
pub struct TokenizedMultimodalRequest {
    segments: Vec<TokenizedMultimodalSegment>,
}

impl TokenizedMultimodalRequest {
    /// Ordered token and media segments.
    pub fn segments(&self) -> &[TokenizedMultimodalSegment] {
        &self.segments
    }
}

fn validate_bindings(
    rendered_prompt: &str,
    bindings: &[MediaBinding],
) -> Result<(), MediaRequestError> {
    for (index, binding) in bindings.iter().enumerate() {
        if binding.placeholder.is_empty() {
            return Err(MediaRequestError::EmptyPlaceholder { index });
        }
        if bindings[..index]
            .iter()
            .any(|earlier| earlier.placeholder == binding.placeholder)
        {
            continue;
        }
        let expected = bindings
            .iter()
            .filter(|candidate| candidate.placeholder == binding.placeholder)
            .count();
        let actual = rendered_prompt.matches(&binding.placeholder).count();
        if actual != expected {
            return Err(MediaRequestError::PlaceholderCount {
                placeholder: binding.placeholder.clone(),
                expected,
                actual,
            });
        }
    }
    Ok(())
}

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

    fn image(value: u8) -> Media {
        Media::Image(RgbImage::new(vec![value; 3], 1, 1).unwrap())
    }

    #[test]
    fn chat_composition_and_tokenization_preserve_exact_order() {
        let request = MultimodalRequest::from_chat(
            "before<image>middle<image>after",
            &[
                MediaBinding::new("<image>", image(1)),
                MediaBinding::new("<image>", image(2)),
            ],
        )
        .unwrap();
        let tokenized = request
            .tokenize::<std::convert::Infallible>(|text| {
                Ok(text
                    .as_bytes()
                    .iter()
                    .map(|byte| u32::from(*byte))
                    .collect())
            })
            .unwrap();
        assert_eq!(tokenized.segments().len(), 5);
        assert!(matches!(
            &tokenized.segments()[0],
            TokenizedMultimodalSegment::TokenIds(ids)
                if ids == &[98, 101, 102, 111, 114, 101]
        ));
        assert!(matches!(
            &tokenized.segments()[1],
            TokenizedMultimodalSegment::Media(Media::Image(image)) if image.pixels() == [1, 1, 1]
        ));
        assert!(matches!(
            &tokenized.segments()[3],
            TokenizedMultimodalSegment::Media(Media::Image(image)) if image.pixels() == [2, 2, 2]
        ));
    }

    #[test]
    fn validation_rejects_bad_media_and_placeholder_contracts() {
        assert!(matches!(
            RgbImage::new(vec![0; 2], 1, 1),
            Err(MediaRequestError::InvalidRgbImage { .. })
        ));
        assert!(Audio::new(vec![f32::NAN], 16_000).is_err());
        assert!(Video::new(Vec::new(), None, VideoSampling::All).is_err());
        assert!(matches!(
            MultimodalRequest::from_chat(
                "<image>",
                &[
                    MediaBinding::new("<image>", image(1)),
                    MediaBinding::new("<image>", image(2)),
                ],
            ),
            Err(MediaRequestError::PlaceholderCount { .. })
        ));
    }
}