dag-ml-data-core 0.2.8

Core data contracts, schemas and plans for dag-ml-data.
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, BinaryHeap};

use serde::{Deserialize, Serialize};

use crate::error::{DataError, Result};
use crate::ids::{RepresentationId, TypeId};
use crate::plan::{FitScope, PlanIssue};

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct InputPortSpec {
    pub name: String,
    pub accepted_representations: Vec<RepresentationId>,
    pub accepted_types: Vec<TypeId>,
    pub rank: Option<usize>,
    #[serde(default)]
    pub multi_source: bool,
    #[serde(default)]
    pub optional: bool,
}

impl InputPortSpec {
    pub fn validate(&self) -> Result<()> {
        validate_name("input port", &self.name)?;
        if self.accepted_representations.is_empty() {
            return Err(DataError::Validation(format!(
                "input port `{}` accepts no representations",
                self.name
            )));
        }
        if self.accepted_types.is_empty() {
            return Err(DataError::Validation(format!(
                "input port `{}` accepts no types",
                self.name
            )));
        }
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ModelInputSpec {
    pub ports: Vec<InputPortSpec>,
    #[serde(default)]
    pub default_fusion: Option<serde_json::Value>,
}

impl ModelInputSpec {
    pub fn validate(&self) -> Result<()> {
        if self.ports.is_empty() {
            return Err(DataError::Validation(
                "model input spec contains no ports".to_string(),
            ));
        }
        let mut names = BTreeSet::new();
        for port in &self.ports {
            port.validate()?;
            if !names.insert(port.name.as_str()) {
                return Err(DataError::Validation(format!(
                    "duplicate model input port `{}`",
                    port.name
                )));
            }
        }
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AdapterSpec {
    pub id: String,
    pub version: String,
    pub input_type: TypeId,
    pub input_representation: RepresentationId,
    pub output_type: TypeId,
    pub output_representation: RepresentationId,
    pub cost: u64,
    #[serde(default)]
    pub lossy: bool,
    #[serde(default)]
    pub supervised: bool,
    #[serde(default)]
    pub stateful: bool,
    #[serde(default = "default_true")]
    pub deterministic: bool,
    pub fit_scope: FitScope,
    #[serde(default)]
    pub params: BTreeMap<String, serde_json::Value>,
}

fn default_true() -> bool {
    true
}

impl AdapterSpec {
    pub fn validate(&self) -> Result<()> {
        validate_name("adapter", &self.id)?;
        validate_name("adapter version", &self.version)?;
        if !self.deterministic {
            return Err(DataError::Validation(format!(
                "adapter `{}` is not deterministic",
                self.id
            )));
        }
        if self.stateful && self.fit_scope == FitScope::Stateless {
            return Err(DataError::Validation(format!(
                "stateful adapter `{}` cannot use stateless fit scope",
                self.id
            )));
        }
        Ok(())
    }

    fn source(&self) -> RepresentationNode {
        RepresentationNode {
            type_id: self.input_type.clone(),
            representation_id: self.input_representation.clone(),
        }
    }

    fn target(&self) -> RepresentationNode {
        RepresentationNode {
            type_id: self.output_type.clone(),
            representation_id: self.output_representation.clone(),
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct PlanningPolicy {
    #[serde(default)]
    pub allow_lossy: bool,
    #[serde(default)]
    pub allow_stateful: bool,
    #[serde(default)]
    pub allow_supervised: bool,
    #[serde(default)]
    pub forbidden_adapters: BTreeSet<String>,
    #[serde(default)]
    pub preferred_adapters: BTreeSet<String>,
    #[serde(default = "default_true")]
    pub require_user_choice_on_ambiguity: bool,
    pub max_hops: Option<usize>,
}

impl Default for PlanningPolicy {
    fn default() -> Self {
        Self {
            allow_lossy: false,
            allow_stateful: false,
            allow_supervised: false,
            forbidden_adapters: BTreeSet::new(),
            preferred_adapters: BTreeSet::new(),
            require_user_choice_on_ambiguity: true,
            max_hops: None,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct RepresentationNode {
    pub type_id: TypeId,
    pub representation_id: RepresentationId,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AdapterPath {
    pub adapters: Vec<AdapterSpec>,
    pub total_cost: u64,
    pub effective_score: u64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct PathResolution {
    pub path: Option<AdapterPath>,
    #[serde(default)]
    pub requires_user_choice: bool,
    #[serde(default)]
    pub issues: Vec<PlanIssue>,
}

impl PathResolution {
    pub fn resolved(path: AdapterPath) -> Self {
        Self {
            path: Some(path),
            requires_user_choice: false,
            issues: Vec::new(),
        }
    }

    pub fn unresolved(code: &str, message: String, choices: Vec<String>) -> Self {
        Self {
            path: None,
            requires_user_choice: !choices.is_empty(),
            issues: vec![PlanIssue {
                code: code.to_string(),
                message,
                choices,
            }],
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct AdapterRegistry {
    adapters: BTreeMap<String, AdapterSpec>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct AdapterRegistrySpec {
    #[serde(default)]
    pub adapters: Vec<AdapterSpec>,
}

impl AdapterRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_spec(spec: AdapterRegistrySpec) -> Result<Self> {
        let mut registry = Self::new();
        for adapter in spec.adapters {
            registry.register_adapter(adapter)?;
        }
        Ok(registry)
    }

    pub fn register_adapter(&mut self, adapter: AdapterSpec) -> Result<()> {
        adapter.validate()?;
        if self.adapters.contains_key(&adapter.id) {
            return Err(DataError::Validation(format!(
                "duplicate adapter id `{}`",
                adapter.id
            )));
        }
        self.adapters.insert(adapter.id.clone(), adapter);
        Ok(())
    }

    pub fn adapters(&self) -> impl Iterator<Item = &AdapterSpec> {
        self.adapters.values()
    }

    pub fn find_path(
        &self,
        source_type: &TypeId,
        source_representation: &RepresentationId,
        target_type: &TypeId,
        target_representation: &RepresentationId,
        policy: &PlanningPolicy,
    ) -> PathResolution {
        let start = RepresentationNode {
            type_id: source_type.clone(),
            representation_id: source_representation.clone(),
        };
        let goal = RepresentationNode {
            type_id: target_type.clone(),
            representation_id: target_representation.clone(),
        };
        if start == goal {
            return PathResolution::resolved(AdapterPath {
                adapters: Vec::new(),
                total_cost: 0,
                effective_score: 0,
            });
        }

        let mut edges: BTreeMap<RepresentationNode, Vec<&AdapterSpec>> = BTreeMap::new();
        for adapter in self.adapters.values() {
            if policy.forbidden_adapters.contains(&adapter.id) {
                continue;
            }
            if adapter.lossy && !policy.allow_lossy {
                continue;
            }
            if adapter.stateful && !policy.allow_stateful {
                continue;
            }
            if adapter.supervised && !policy.allow_supervised {
                continue;
            }
            edges.entry(adapter.source()).or_default().push(adapter);
        }

        let mut heap = BinaryHeap::new();
        heap.push(SearchState {
            score: 0,
            raw_cost: 0,
            hops: 0,
            node: start.clone(),
            adapter_ids: Vec::new(),
        });

        let mut best_seen: BTreeMap<RepresentationNode, (u64, usize)> = BTreeMap::new();
        best_seen.insert(start.clone(), (0, 0));
        let mut best_goal: Option<(u64, usize, u64)> = None;
        let mut goal_paths = Vec::new();

        while let Some(state) = heap.pop() {
            if let Some((best_score, best_hops, _)) = best_goal {
                if (state.score, state.hops) > (best_score, best_hops) {
                    break;
                }
            }
            if state.node == goal {
                best_goal.get_or_insert((state.score, state.hops, state.raw_cost));
                goal_paths.push(state.adapter_ids);
                continue;
            }
            if policy
                .max_hops
                .is_some_and(|max_hops| state.hops >= max_hops)
            {
                continue;
            }
            let Some(next_edges) = edges.get(&state.node) else {
                continue;
            };
            for adapter in next_edges {
                if state.adapter_ids.iter().any(|id| id == &adapter.id) {
                    continue;
                }
                let next = adapter.target();
                let score = state.score + adapter_score(adapter, policy);
                let raw_cost = state.raw_cost + adapter.cost;
                let hops = state.hops + 1;
                if best_seen.get(&next).is_some_and(|(best_score, best_hops)| {
                    (score, hops) > (*best_score, *best_hops)
                }) {
                    continue;
                }
                best_seen.insert(next.clone(), (score, hops));
                let mut adapter_ids = state.adapter_ids.clone();
                adapter_ids.push(adapter.id.clone());
                heap.push(SearchState {
                    score,
                    raw_cost,
                    hops,
                    node: next,
                    adapter_ids,
                });
            }
        }

        if goal_paths.is_empty() {
            return PathResolution::unresolved(
                "no_path",
                format!(
                    "no adapter path from `{}/{}` to `{}/{}`",
                    source_type, source_representation, target_type, target_representation
                ),
                Vec::new(),
            );
        }

        goal_paths.sort();
        goal_paths.dedup();
        if goal_paths.len() > 1 && policy.require_user_choice_on_ambiguity {
            let choices = goal_paths
                .iter()
                .map(|path| path.join(" -> "))
                .collect::<Vec<_>>();
            return PathResolution::unresolved(
                "ambiguous_path",
                "multiple equivalent adapter paths require user choice".to_string(),
                choices,
            );
        }

        let adapter_ids = goal_paths.remove(0);
        let adapters = adapter_ids
            .iter()
            .map(|id| self.adapters.get(id).expect("path adapter exists").clone())
            .collect::<Vec<_>>();
        let total_cost = adapters.iter().map(|adapter| adapter.cost).sum();
        let effective_score = adapters
            .iter()
            .map(|adapter| adapter_score(adapter, policy))
            .sum();
        PathResolution::resolved(AdapterPath {
            adapters,
            total_cost,
            effective_score,
        })
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct SearchState {
    score: u64,
    raw_cost: u64,
    hops: usize,
    node: RepresentationNode,
    adapter_ids: Vec<String>,
}

impl Ord for SearchState {
    fn cmp(&self, other: &Self) -> Ordering {
        other
            .score
            .cmp(&self.score)
            .then_with(|| other.hops.cmp(&self.hops))
            .then_with(|| other.raw_cost.cmp(&self.raw_cost))
            .then_with(|| other.node.cmp(&self.node))
            .then_with(|| other.adapter_ids.cmp(&self.adapter_ids))
    }
}

impl PartialOrd for SearchState {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

fn adapter_score(adapter: &AdapterSpec, policy: &PlanningPolicy) -> u64 {
    let mut score = adapter.cost.max(1);
    if adapter.lossy {
        score += 1_000_000;
    }
    if adapter.stateful {
        score += 100_000;
    }
    if adapter.supervised {
        score += 100_000;
    }
    if policy.preferred_adapters.contains(&adapter.id) {
        score = score.saturating_sub(1);
    }
    score
}

fn validate_name(kind: &str, value: &str) -> Result<()> {
    if value.trim().is_empty() {
        return Err(DataError::Validation(format!("{kind} name is empty")));
    }
    if !value
        .bytes()
        .all(|b| b.is_ascii_alphanumeric() || matches!(b, b'_' | b'-' | b'.' | b':' | b'/'))
    {
        return Err(DataError::Validation(format!(
            "{kind} `{value}` contains unsupported characters"
        )));
    }
    Ok(())
}

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

    fn tid(value: &str) -> TypeId {
        TypeId::new(value).unwrap()
    }

    fn rid(value: &str) -> RepresentationId {
        RepresentationId::new(value).unwrap()
    }

    fn adapter(id: &str, input: &str, output: &str, cost: u64) -> AdapterSpec {
        AdapterSpec {
            id: id.to_string(),
            version: "0.1.0".to_string(),
            input_type: tid("dense_signal"),
            input_representation: rid(input),
            output_type: if output == "tabular_numeric" {
                tid("table")
            } else {
                tid("dense_signal")
            },
            output_representation: rid(output),
            cost,
            lossy: false,
            supervised: false,
            stateful: false,
            deterministic: true,
            fit_scope: FitScope::Stateless,
            params: BTreeMap::new(),
        }
    }

    #[test]
    fn validates_model_input_ports() {
        let spec = ModelInputSpec {
            ports: vec![InputPortSpec {
                name: "X".to_string(),
                accepted_representations: vec![rid("tabular_numeric")],
                accepted_types: vec![tid("table")],
                rank: Some(2),
                multi_source: true,
                optional: false,
            }],
            default_fusion: None,
        };

        assert!(spec.validate().is_ok());
    }

    #[test]
    fn rejects_duplicate_adapter_ids() {
        let mut registry = AdapterRegistry::new();
        registry
            .register_adapter(adapter(
                "spectra.flatten",
                "signal_1d",
                "tabular_numeric",
                1,
            ))
            .unwrap();

        assert!(registry
            .register_adapter(adapter(
                "spectra.flatten",
                "signal_1d",
                "tabular_numeric",
                1
            ))
            .is_err());
    }

    #[test]
    fn path_selection_is_registration_order_independent() {
        let mut left = AdapterRegistry::new();
        left.register_adapter(adapter("a.to_mid", "signal_1d", "signal_mid", 1))
            .unwrap();
        left.register_adapter(adapter("b.to_tabular", "signal_mid", "tabular_numeric", 1))
            .unwrap();
        left.register_adapter(adapter("c.direct", "signal_1d", "tabular_numeric", 10))
            .unwrap();

        let mut right = AdapterRegistry::new();
        right
            .register_adapter(adapter("c.direct", "signal_1d", "tabular_numeric", 10))
            .unwrap();
        right
            .register_adapter(adapter("b.to_tabular", "signal_mid", "tabular_numeric", 1))
            .unwrap();
        right
            .register_adapter(adapter("a.to_mid", "signal_1d", "signal_mid", 1))
            .unwrap();

        let policy = PlanningPolicy::default();
        let left_path = left
            .find_path(
                &tid("dense_signal"),
                &rid("signal_1d"),
                &tid("table"),
                &rid("tabular_numeric"),
                &policy,
            )
            .path
            .unwrap();
        let right_path = right
            .find_path(
                &tid("dense_signal"),
                &rid("signal_1d"),
                &tid("table"),
                &rid("tabular_numeric"),
                &policy,
            )
            .path
            .unwrap();

        assert_eq!(
            left_path
                .adapters
                .iter()
                .map(|adapter| adapter.id.as_str())
                .collect::<Vec<_>>(),
            vec!["a.to_mid", "b.to_tabular"]
        );
        assert_eq!(left_path, right_path);
    }

    #[test]
    fn lossy_paths_are_refused_unless_allowed() {
        let mut lossy = adapter("image.embedding", "signal_1d", "tabular_numeric", 1);
        lossy.lossy = true;

        let mut registry = AdapterRegistry::new();
        registry.register_adapter(lossy).unwrap();

        let refused = registry.find_path(
            &tid("dense_signal"),
            &rid("signal_1d"),
            &tid("table"),
            &rid("tabular_numeric"),
            &PlanningPolicy::default(),
        );
        assert!(refused.path.is_none());

        let allowed = registry.find_path(
            &tid("dense_signal"),
            &rid("signal_1d"),
            &tid("table"),
            &rid("tabular_numeric"),
            &PlanningPolicy {
                allow_lossy: true,
                ..PlanningPolicy::default()
            },
        );
        assert_eq!(allowed.path.unwrap().adapters[0].id, "image.embedding");
    }

    #[test]
    fn equivalent_best_paths_require_user_choice() {
        let mut registry = AdapterRegistry::new();
        registry
            .register_adapter(adapter("a.flatten", "signal_1d", "tabular_numeric", 1))
            .unwrap();
        registry
            .register_adapter(adapter("b.flatten", "signal_1d", "tabular_numeric", 1))
            .unwrap();

        let resolution = registry.find_path(
            &tid("dense_signal"),
            &rid("signal_1d"),
            &tid("table"),
            &rid("tabular_numeric"),
            &PlanningPolicy::default(),
        );

        assert!(resolution.path.is_none());
        assert!(resolution.requires_user_choice);
        assert_eq!(resolution.issues[0].code, "ambiguous_path");
        assert_eq!(resolution.issues[0].choices.len(), 2);
    }
}