trustformers 0.2.1

TrustformeRS - Rust port of Hugging Face Transformers
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
//! Unit tests for [`super`] -- split out of `config_management.rs` to keep
//! it under the workspace's 2000-line-per-file policy (see the `#[path =
//! "config_management_tests.rs"] mod tests;` declaration at the bottom of
//! that file; the same convention is used by
//! `pipeline/adaptive_inference.rs` and `auto/optimizers/mod.rs`).

use super::*;

#[test]
fn test_configuration_manager_creation() {
    let manager = ConfigurationManager::new();
    assert!(manager.schema_registry.contains_key("training"));
    assert!(manager.schema_registry.contains_key("model"));
    assert!(manager.schema_registry.contains_key("conversational"));
}

#[test]
fn test_validation_success() {
    let manager = ConfigurationManager::new();
    let config = serde_json::json!({
        "num_epochs": 5,
        "batch_size": 32,
        "learning_rate": 2e-5
    });

    let result = manager.validate_config("training", &config);
    assert!(result.is_valid);
    assert!(result.errors.is_empty());
}

#[test]
fn test_validation_missing_required_field() {
    let manager = ConfigurationManager::new();
    let config = serde_json::json!({
        "num_epochs": 5,
        "batch_size": 32
        // missing learning_rate
    });

    let result = manager.validate_config("training", &config);
    assert!(!result.is_valid);
    assert_eq!(result.errors.len(), 1);
    assert!(matches!(
        result.errors[0].error_type,
        ValidationErrorType::MissingRequiredField
    ));
}

#[test]
fn test_validation_type_mismatch() {
    let manager = ConfigurationManager::new();
    let config = serde_json::json!({
        "num_epochs": "not_a_number",
        "batch_size": 32,
        "learning_rate": 2e-5
    });

    let result = manager.validate_config("training", &config);
    assert!(!result.is_valid);
    assert!(result
        .errors
        .iter()
        .any(|e| matches!(e.error_type, ValidationErrorType::TypeMismatch)));
}

#[test]
fn test_migration() {
    let manager = ConfigurationManager::new();
    let old_config = serde_json::json!({
        "num_epochs": 5,
        "batch_size": 32,
        "learning_rate": 2e-5
    });

    let migrated = manager
        .migrate_config("training", &old_config, "1.0.0", "2.0.0")
        .expect("operation failed in test");

    assert!(migrated.get("gradient_accumulation_steps").is_some());
    assert!(migrated.get("warmup_steps").is_some());
}

// -------------------------------------------------------------------
// Multi-hop migration paths: regression coverage for the bug where
// `migrate_config` found a multi-hop path only when the migrations
// `Vec` happened to already be ordered to match the path being walked.
// A real graph search (`find_migration_path`, BFS) must find a valid
// path regardless of registration order.
// -------------------------------------------------------------------

fn passthrough_migration(from: &str, to: &str, marker: &'static str) -> Migration {
    Migration::new(from, to, "test migration", move |config| {
        let mut new_config = config.clone();
        if let serde_json::Value::Object(map) = &mut new_config {
            map.insert(marker.to_string(), serde_json::Value::Bool(true));
        }
        Ok(new_config)
    })
}

#[test]
fn test_multi_hop_migration_applies_every_migration_in_path_order() {
    // 1.0.0 -> 2.0.0 -> 3.0.0, with only the two direct one-hop
    // migrations registered -- no direct 1.0.0 -> 3.0.0 migration
    // exists. The old single-pass code could resolve this only when
    // `migrations` happened to already be in the right order; this
    // must succeed unconditionally.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "multihop".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "step_one_applied"),
    );
    manager.register_migration(
        "multihop".to_string(),
        passthrough_migration("2.0.0", "3.0.0", "step_two_applied"),
    );

    let config = serde_json::json!({});
    let migrated = manager
        .migrate_config("multihop", &config, "1.0.0", "3.0.0")
        .expect("a valid two-hop path must be found");

    assert_eq!(
        migrated.get("step_one_applied"),
        Some(&serde_json::Value::Bool(true)),
        "the first hop's migration must have run"
    );
    assert_eq!(
        migrated.get("step_two_applied"),
        Some(&serde_json::Value::Bool(true)),
        "the second hop's migration must have run"
    );
}

#[test]
fn test_multi_hop_migration_succeeds_regardless_of_registration_order() {
    // Same two-hop chain as above, but registered in *reverse* order
    // (the second hop registered before the first). Regression: the
    // old `is_version_on_path`/linear-pass code depended on `migrations`
    // already being ordered to match the path, so this exact
    // registration order would previously fail to find a path that
    // plainly exists.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "reverse_order".to_string(),
        passthrough_migration("2.0.0", "3.0.0", "step_two_applied"),
    );
    manager.register_migration(
        "reverse_order".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "step_one_applied"),
    );

    let config = serde_json::json!({});
    let migrated = manager.migrate_config("reverse_order", &config, "1.0.0", "3.0.0").expect(
        "a valid two-hop path must be found regardless of the order migrations were \
             registered in",
    );

    assert_eq!(
        migrated.get("step_one_applied"),
        Some(&serde_json::Value::Bool(true))
    );
    assert_eq!(
        migrated.get("step_two_applied"),
        Some(&serde_json::Value::Bool(true))
    );
}

#[test]
fn test_three_hop_migration_chain() {
    // 1.0.0 -> 2.0.0 -> 3.0.0 -> 4.0.0: verifies the search is not
    // hardcoded to two hops.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "threehop".to_string(),
        passthrough_migration("3.0.0", "4.0.0", "hop_three"),
    );
    manager.register_migration(
        "threehop".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "hop_one"),
    );
    manager.register_migration(
        "threehop".to_string(),
        passthrough_migration("2.0.0", "3.0.0", "hop_two"),
    );

    let config = serde_json::json!({});
    let migrated = manager
        .migrate_config("threehop", &config, "1.0.0", "4.0.0")
        .expect("a valid three-hop path must be found");

    for marker in ["hop_one", "hop_two", "hop_three"] {
        assert_eq!(
            migrated.get(marker),
            Some(&serde_json::Value::Bool(true)),
            "migration `{marker}` must have been applied"
        );
    }
}

#[test]
fn test_migration_prefers_direct_path_over_longer_detour() {
    // Both a direct 1.0.0 -> 3.0.0 migration and a longer detour
    // (1.0.0 -> 2.0.0 -> 3.0.0) are registered. BFS finds the
    // shortest path, so only the direct migration's marker must be
    // present -- proving the detour's migrations were not also
    // (redundantly, or wrongly-ordered) applied.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "shortest".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "detour_hop_one"),
    );
    manager.register_migration(
        "shortest".to_string(),
        passthrough_migration("2.0.0", "3.0.0", "detour_hop_two"),
    );
    manager.register_migration(
        "shortest".to_string(),
        passthrough_migration("1.0.0", "3.0.0", "direct_hop"),
    );

    let config = serde_json::json!({});
    let migrated = manager
        .migrate_config("shortest", &config, "1.0.0", "3.0.0")
        .expect("a path must be found");

    assert_eq!(
        migrated.get("direct_hop"),
        Some(&serde_json::Value::Bool(true)),
        "the shortest (direct) path must be used"
    );
    assert_eq!(
        migrated.get("detour_hop_one"),
        None,
        "the longer detour must not be taken when a direct path exists"
    );
}

#[test]
fn test_migration_no_path_is_structured_error_not_silent_passthrough() {
    // 1.0.0 -> 2.0.0 is registered, but nothing reaches 9.9.9: this
    // must be a real error, not a silent no-op that returns the
    // original config unmigrated.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "nopath".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "applied"),
    );

    let config = serde_json::json!({ "original": true });
    let result = manager.migrate_config("nopath", &config, "1.0.0", "9.9.9");
    assert!(
        result.is_err(),
        "an unreachable target version must be a structured error"
    );
}

#[test]
fn test_migration_same_version_is_identity_without_registered_migrations() {
    // from_version == to_version must succeed trivially (nothing to
    // migrate), even though no migration whose `from_version` equals
    // `to_version` exists in the registry.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "identity".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "applied"),
    );

    let config = serde_json::json!({ "unchanged": true });
    let migrated = manager
        .migrate_config("identity", &config, "1.0.0", "1.0.0")
        .expect("migrating a version to itself must succeed");
    assert_eq!(
        migrated, config,
        "identity migration must not alter the config"
    );
}

#[test]
fn test_migration_graph_with_cycle_does_not_infinite_loop() {
    // A migration graph containing a cycle (2.0.0 -> 1.0.0, back to the
    // start) must not cause the BFS to loop forever; it must still find
    // the real forward path.
    let mut manager = ConfigurationManager::new();
    manager.register_migration(
        "cyclic".to_string(),
        passthrough_migration("1.0.0", "2.0.0", "forward"),
    );
    manager.register_migration(
        "cyclic".to_string(),
        passthrough_migration("2.0.0", "1.0.0", "backward"),
    );
    manager.register_migration(
        "cyclic".to_string(),
        passthrough_migration("2.0.0", "3.0.0", "onward"),
    );

    let config = serde_json::json!({});
    let migrated = manager
        .migrate_config("cyclic", &config, "1.0.0", "3.0.0")
        .expect("a cycle elsewhere in the graph must not prevent finding the real path");
    assert_eq!(
        migrated.get("forward"),
        Some(&serde_json::Value::Bool(true))
    );
    assert_eq!(migrated.get("onward"), Some(&serde_json::Value::Bool(true)));
}

#[test]
fn test_template_generation() {
    let manager = ConfigurationManager::new();
    let template = manager.generate_template("training").expect("temp file creation failed");

    assert!(template.get("num_epochs").is_some());
    assert!(template.get("batch_size").is_some());
    assert!(template.get("learning_rate").is_some());
}

#[test]
fn test_config_comparison() {
    let manager = ConfigurationManager::new();

    let config1 = serde_json::json!({
        "num_epochs": 5,
        "batch_size": 32
    });

    let config2 = serde_json::json!({
        "num_epochs": 10,
        "learning_rate": 2e-5
    });

    let comparison = manager.compare_configs(&config1, &config2);

    assert_eq!(comparison.modified_fields.len(), 1); // num_epochs changed
    assert_eq!(comparison.added_fields.len(), 1); // learning_rate added
    assert_eq!(comparison.removed_fields.len(), 1); // batch_size removed
}

#[test]
fn test_preset_creation() {
    let manager = ConfigurationManager::new();

    let config = manager
        .create_from_preset(
            "training",
            "fast_development",
            Some(HashMap::from([(
                "batch_size".to_string(),
                serde_json::Value::Number(serde_json::Number::from(16)),
            )])),
        )
        .expect("operation failed in test");

    assert_eq!(
        config.get("num_epochs").expect("expected value not found"),
        &serde_json::Value::Number(serde_json::Number::from(3))
    );
    assert_eq!(
        config.get("batch_size").expect("expected value not found"),
        &serde_json::Value::Number(serde_json::Number::from(16))
    ); // overridden
}

#[test]
fn test_recommendations() {
    let manager = ConfigurationManager::new();

    let config = serde_json::json!({
        "batch_size": 8,
        "learning_rate": 1e-2 // Very high learning rate
    });

    let context = RecommendationContext {
        hardware_info: HashMap::from([(
            "gpu_memory_gb".to_string(),
            serde_json::Value::Number(
                serde_json::Number::from_f64(16.0).expect("operation failed in test"),
            ),
        )]),
        use_case: "production".to_string(),
        performance_requirements: PerformanceRequirements {
            max_latency_ms: None,
            min_throughput: None,
            memory_budget_gb: None,
            power_budget_watts: None,
        },
        constraints: vec![],
    };

    let recommendations = manager.get_recommendations("training", &config, &context);

    assert!(!recommendations.is_empty());
    assert!(recommendations.iter().any(|r| r.field == "batch_size"));
    assert!(recommendations.iter().any(|r| r.field == "learning_rate"));
}

#[test]
fn test_unknown_config_type() {
    let manager = ConfigurationManager::new();
    let config = serde_json::json!({"test": "value"});

    let result = manager.validate_config("unknown_type", &config);
    assert!(!result.is_valid);
    assert!(matches!(
        result.errors[0].error_type,
        ValidationErrorType::UnknownConfigType
    ));
}

#[test]
fn test_constraint_validation() {
    let manager = ConfigurationManager::new();
    let config = serde_json::json!({
        "num_epochs": -1, // Violates minimum value constraint
        "batch_size": 32,
        "learning_rate": 2e-5
    });

    let result = manager.validate_config("training", &config);
    assert!(!result.is_valid);
    assert!(result
        .errors
        .iter()
        .any(|e| matches!(e.error_type, ValidationErrorType::ConstraintViolation)));
}

// -------------------------------------------------------------------
// Conditional requirements driven by the real expression evaluator
// (`crate::config_condition`). Regression coverage for the bug where
// `evaluate_condition` only understood `==` on string fields and
// silently returned `false` for everything else, so conditional
// requirements built on `!=`/`<`/`&&`/etc. could never fire.
// -------------------------------------------------------------------

fn schema_with_condition(condition: &str) -> ConfigSchema {
    ConfigSchema {
        name: "conditional-test".to_string(),
        version: "1.0.0".to_string(),
        description: "schema exercising conditional_requirements".to_string(),
        fields: HashMap::new(),
        required_fields: HashSet::new(),
        conditional_requirements: vec![ConditionalRequirement {
            condition: condition.to_string(),
            required_fields: vec!["api_key".to_string()],
        }],
    }
}

#[test]
fn test_conditional_requirement_fires_on_not_equal() {
    // `!=` was entirely unsupported by the old evaluator.
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("environment != \"local\"");
    let config = serde_json::json!({ "environment": "production" });
    let result = validator.validate(&config, &schema);
    assert!(
        !result.is_valid,
        "missing api_key must be flagged when environment != local"
    );
    assert!(result.errors.iter().any(|e| matches!(
        e.error_type,
        ValidationErrorType::ConditionalRequirementNotMet
    )));
}

#[test]
fn test_conditional_requirement_does_not_fire_when_condition_false() {
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("environment != \"local\"");
    let config = serde_json::json!({ "environment": "local" });
    let result = validator.validate(&config, &schema);
    assert!(
        result.is_valid,
        "condition is false, so the missing api_key must not be flagged"
    );
}

#[test]
fn test_conditional_requirement_fires_on_numeric_comparison() {
    // `<`/`>`/etc. were entirely unsupported by the old evaluator.
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("batch_size > 64");
    let config = serde_json::json!({ "batch_size": 128 });
    let result = validator.validate(&config, &schema);
    assert!(!result.is_valid);
}

#[test]
fn test_conditional_requirement_fires_on_and_with_precedence() {
    // Exercises `&&` binding tighter than `||`, end to end through
    // `ConfigValidator::validate`, not just the standalone evaluator.
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("region == \"eu\" || tier == \"pro\" && strict == true");
    let config = serde_json::json!({ "region": "us", "tier": "pro", "strict": true });
    let result = validator.validate(&config, &schema);
    assert!(
        !result.is_valid,
        "`tier == pro && strict == true` must fire even though `region == eu` is false"
    );
}

#[test]
fn test_conditional_requirement_satisfied_field_present_is_valid() {
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("environment != \"local\"");
    let config = serde_json::json!({ "environment": "production", "api_key": "secret" });
    let result = validator.validate(&config, &schema);
    assert!(
        result.is_valid,
        "api_key is present, so the condition being true is fine"
    );
}

#[test]
fn test_malformed_condition_is_structured_error_not_silent_pass() {
    // Regression test for the exact bug: the old evaluator would treat
    // any operator it didn't recognize as "condition not met" and
    // silently return `false`, so a schema author's typo (`<>` instead
    // of `!=`) would validate cleanly forever. The new evaluator must
    // surface this as a validation error instead.
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("environment <> \"local\"");
    let config = serde_json::json!({ "environment": "production" });
    let result = validator.validate(&config, &schema);
    assert!(
        !result.is_valid,
        "a malformed condition must fail validation, not pass silently"
    );
    assert!(
        result
            .errors
            .iter()
            .any(|e| matches!(e.error_type, ValidationErrorType::InvalidCondition)),
        "the malformed condition must be reported as InvalidCondition"
    );
}

#[test]
fn test_malformed_condition_error_message_names_the_condition() {
    let validator = ConfigValidator::new();
    let schema = schema_with_condition("a <> b");
    let config = serde_json::json!({});
    let result = validator.validate(&config, &schema);
    let invalid = result
        .errors
        .iter()
        .find(|e| matches!(e.error_type, ValidationErrorType::InvalidCondition))
        .expect("must report InvalidCondition");
    assert!(
        invalid.message.contains("a <> b"),
        "error message should include the offending condition text: {}",
        invalid.message
    );
}