rusty-stack 0.3.0

A comprehensive ML environment installer and platform toolkit optimized for AMD GPUs with ROCm support
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
//! Integration tests for installer dispatch to native Rust modules.
//!
//! Validates:
//! - VAL-INSTALL-031: installer.rs no bash subprocess for ported components
//! - VAL-INSTALL-032: installer.rs dispatches to correct Rust module per ID
//! - VAL-INSTALL-033: sudo behavior preserved for needs_sudo:true components
//! - VAL-INSTALL-034: Environment variable injection preserved
//! - VAL-INSTALL-035: InstallerEvent progress reporting preserved
//! - VAL-INSTALL-036: Error message format matches original scripts
//! - VAL-INSTALL-037: Component.script no longer holds .sh filenames
//! - VAL-INSTALL-038: state.rs supports native module routing
//! - VAL-INSTALL-039: All 35 components reference correct Rust modules
//! - VAL-INSTALL-040: Non-installer components unchanged
//! - VAL-INSTALL-041-047: Dependency declarations
//! - VAL-INSTALL-048: Dependency graph is acyclic
//! - VAL-INSTALL-049: installer.rs respects dependency ordering
//! - VAL-INSTALL-052: installer.rs integration tests exist

use rusty_stack::installers::components::{
    get_dependencies, is_native_component, topological_sort, NATIVE_COMPONENT_IDS,
};
use rusty_stack::state::{default_components, Category, Component};

// ===========================================================================
// VAL-INSTALL-031 + VAL-INSTALL-039: All 35 ported components are native
// ===========================================================================

#[test]
fn test_all_35_native_components_recognized() {
    // 24 installer + 9 benchmark + 1 fastvideo + 1 llama-cpp = 35
    assert_eq!(
        NATIVE_COMPONENT_IDS.len(),
        35,
        "Must have exactly 35 native components (24 installers + 9 benchmarks + 1 fastvideo + 1 llama-cpp)"
    );

    for id in NATIVE_COMPONENT_IDS {
        assert!(
            is_native_component(id),
            "Component '{}' should be recognized as native",
            id
        );
    }
}

#[test]
fn test_no_bash_dispatch_for_native_components() {
    // VAL-INSTALL-031: Verify that all ported components are marked native
    // so installer.rs won't spawn bash for them
    let components = default_components();
    let native_installers: Vec<&Component> = components
        .iter()
        .filter(|c| c.category != Category::Verification && c.category != Category::Performance)
        .collect();

    for comp in &native_installers {
        assert!(
            is_native_component(&comp.id),
            "Installer component '{}' must be native (no bash dispatch)",
            comp.id
        );
    }
}

// ===========================================================================
// VAL-INSTALL-037: Component.script no longer holds .sh for ported components
// ===========================================================================

#[test]
fn test_ported_components_have_empty_script() {
    let components = default_components();
    let native_installers: Vec<&Component> = components
        .iter()
        .filter(|c| is_native_component(&c.id))
        .collect();

    // 22 of 34 native components appear in default_components().
    // The other 12 (amdgpu-drivers, migraphx-python, enhanced-env, plus
    // 9 benchmark components) are chain-referenced or dispatched differently.
    assert!(
        native_installers.len() >= 22,
        "Must have at least 22 ported components in default_components(), got {}",
        native_installers.len()
    );

    for comp in &native_installers {
        assert!(
            comp.script.is_empty(),
            "Ported component '{}' should have empty script, got '{}'",
            comp.id,
            comp.script
        );
    }
}

// ===========================================================================
// VAL-INSTALL-038: state.rs supports native module routing
// ===========================================================================

#[test]
fn test_component_is_native_method() {
    let components = default_components();

    for comp in &components {
        if is_native_component(&comp.id) {
            assert!(
                comp.is_native(),
                "Component '{}' should report is_native() = true",
                comp.id
            );
        } else {
            assert!(
                !comp.is_native(),
                "Component '{}' should report is_native() = false",
                comp.id
            );
        }
    }
}

// ===========================================================================
// VAL-INSTALL-040: Non-installer components unchanged (still reference .sh)
// ===========================================================================

#[test]
fn test_verification_components_use_native_rust() {
    let components = default_components();
    let verification: Vec<&Component> = components
        .iter()
        .filter(|c| c.category == Category::Verification)
        .collect();

    assert_eq!(verification.len(), 3, "Must have 3 verification components");

    for comp in &verification {
        assert!(
            comp.script.is_empty(),
            "Verification component '{}' should use native Rust (empty script), got '{}'",
            comp.id,
            comp.script
        );
    }
}

#[test]
fn test_performance_components_use_native_rust() {
    let components = default_components();
    let performance: Vec<&Component> = components
        .iter()
        .filter(|c| c.category == Category::Performance)
        .collect();

    assert_eq!(performance.len(), 8, "Must have 8 performance components");

    for comp in &performance {
        assert!(
            comp.script.is_empty(),
            "Performance component '{}' should use native Rust (empty script), got '{}'",
            comp.id,
            comp.script
        );
        assert!(
            is_native_component(&comp.id),
            "Performance component '{}' should be native",
            comp.id
        );
    }
}

// ===========================================================================
// VAL-INSTALL-041-047: Dependency declarations
// ===========================================================================

#[test]
fn test_megatron_depends_on_pytorch_and_mpi4py() {
    let deps = get_dependencies("megatron");
    assert!(deps.contains(&"pytorch"), "Megatron must depend on pytorch");
    assert!(deps.contains(&"mpi4py"), "Megatron must depend on mpi4py");
}

#[test]
fn test_vllm_depends_on_pytorch() {
    let deps = get_dependencies("vllm");
    assert!(deps.contains(&"pytorch"), "vLLM must depend on pytorch");
}

#[test]
fn test_deepspeed_depends_on_pytorch() {
    let deps = get_dependencies("deepspeed");
    assert!(
        deps.contains(&"pytorch"),
        "DeepSpeed must depend on pytorch"
    );
}

#[test]
fn test_flash_attention_depends_on_pytorch_and_rocm() {
    let deps = get_dependencies("flash-attn");
    assert!(
        deps.contains(&"pytorch"),
        "Flash Attention must depend on pytorch"
    );
    assert!(
        deps.contains(&"rocm"),
        "Flash Attention must depend on rocm"
    );
}

#[test]
fn test_onnx_depends_on_rocm() {
    let deps = get_dependencies("onnx");
    assert!(deps.contains(&"rocm"), "ONNX Runtime must depend on rocm");
}

#[test]
fn test_aiter_depends_on_pytorch_and_rocm() {
    let deps = get_dependencies("aiter");
    assert!(deps.contains(&"pytorch"), "AITER must depend on pytorch");
    assert!(deps.contains(&"rocm"), "AITER must depend on rocm");
}

#[test]
fn test_comfyui_depends_on_pytorch() {
    let deps = get_dependencies("comfyui");
    assert!(deps.contains(&"pytorch"), "ComfyUI must depend on pytorch");
}

// ===========================================================================
// VAL-INSTALL-048: Dependency graph is acyclic
// ===========================================================================

#[test]
fn test_dependency_graph_is_acyclic() {
    let all_ids: Vec<String> = NATIVE_COMPONENT_IDS.iter().map(|s| s.to_string()).collect();
    let result = topological_sort(&all_ids);
    assert!(
        result.is_ok(),
        "Dependency graph must be acyclic: {:?}",
        result
    );
    let sorted = result.unwrap();
    assert_eq!(sorted.len(), NATIVE_COMPONENT_IDS.len());
}

// ===========================================================================
// VAL-INSTALL-049: installer.rs respects dependency ordering
// ===========================================================================

#[test]
fn test_topological_sort_orders_dependencies_before_dependents() {
    let ids = vec![
        "megatron".to_string(),
        "pytorch".to_string(),
        "mpi4py".to_string(),
        "rocm".to_string(),
    ];
    let sorted = topological_sort(&ids).unwrap();

    let pos = |id: &str| sorted.iter().position(|s| s == id).unwrap();
    assert!(pos("rocm") < pos("pytorch"), "rocm before pytorch");
    assert!(pos("pytorch") < pos("megatron"), "pytorch before megatron");
    assert!(pos("mpi4py") < pos("megatron"), "mpi4py before megatron");
}

#[test]
fn test_topological_sort_complex_dependency_chain() {
    let ids = vec![
        "megatron".to_string(),
        "flash-attn".to_string(),
        "vllm".to_string(),
        "aiter".to_string(),
        "onnx".to_string(),
        "deepspeed".to_string(),
        "comfyui".to_string(),
        "pytorch".to_string(),
        "rocm".to_string(),
        "mpi4py".to_string(),
    ];
    let sorted = topological_sort(&ids).unwrap();

    let pos = |id: &str| sorted.iter().position(|s| s == id).unwrap();

    // Verify declared dependency constraints
    // rocm -> flash-attn, onnx, aiter
    assert!(pos("rocm") < pos("flash-attn"));
    assert!(pos("rocm") < pos("onnx"));
    assert!(pos("rocm") < pos("aiter"));
    // pytorch -> megatron, vllm, deepspeed, flash-attn, aiter, comfyui
    assert!(pos("pytorch") < pos("megatron"));
    assert!(pos("mpi4py") < pos("megatron"));
    assert!(pos("pytorch") < pos("vllm"));
    assert!(pos("pytorch") < pos("deepspeed"));
    assert!(pos("pytorch") < pos("comfyui"));
}

// ===========================================================================
// VAL-INSTALL-032: Correct Rust module mapping per component ID
// ===========================================================================

#[test]
fn test_every_native_component_has_installer_module() {
    // Verify that each native component ID maps to a module that exists
    // by checking the module can be imported and an installer can be created.
    // We just verify the mapping exists by creating default configs.
    use rusty_stack::installers::components::*;

    for id in NATIVE_COMPONENT_IDS {
        // Each component should be dispatchable — we verify the mapping
        // by ensuring the module exists and can create a default installer
        match *id {
            "rocm" => {
                let _ = RocmInstaller::with_defaults();
            }
            "pytorch" => {
                let _ = PyTorchInstaller::with_defaults();
            }
            "triton" => {
                let _ = TritonInstaller::with_defaults();
            }
            "mpi4py" => {
                let _ = Mpi4PyInstaller::with_defaults();
            }
            "deepspeed" => {
                let _ = DeepSpeedInstaller::with_defaults();
            }
            "ml-stack-core" => {
                let _ = MlStackInstaller::with_defaults();
            }
            "flash-attn" => {
                let _ = FlashAttentionInstaller::with_defaults();
            }
            "megatron" => {
                let _ = MegatronInstaller::with_defaults();
            }
            "vllm" => {
                let _ = VllmInstaller::with_defaults();
            }
            "aiter" => {
                let _ = AiterInstaller::with_defaults();
            }
            "vllm-studio" => {
                let _ = VllmStudioInstaller::with_defaults();
            }
            "comfyui" => {
                let _ = ComfyuiInstaller::with_defaults();
            }
            "textgen" => {
                let _ = TextgenInstaller::with_defaults();
            }
            "onnx" => {
                let _ = OnnxRuntimeInstaller::with_defaults();
            }
            "bitsandbytes" => {
                let _ = BitsAndBytesInstaller::with_defaults();
            }
            "rocm-smi" => {
                let _ = RocmSmiInstaller::with_defaults();
            }
            "migraphx" => {
                let _ = MigraphxInstaller::with_defaults();
            }
            "pytorch-profiler" => {
                let _ = PytorchProfilerInstaller::with_defaults();
            }
            "wandb" => {
                let _ = WandbInstaller::with_defaults();
            }
            "amdgpu-drivers" => {
                let _ = AmdgpuInstaller::with_defaults();
            }
            "migraphx-python" => {
                let _ = MigraphxPythonInstaller::with_defaults();
            }
            "permanent-env" => {
                let _ = PermanentEnvInstaller::with_defaults();
            }
            "repair-stack" => {
                let _ = RepairInstaller::with_defaults();
            }
            "enhanced-env" => {} // env setup module (no dedicated installer struct yet)
            // Benchmark components — dispatched via benchmark_runners module
            "mlperf-inference"
            | "rocm-benchmarks"
            | "gpu-memory-bandwidth"
            | "rocm-smi-bench"
            | "pytorch-performance"
            | "vllm-performance"
            | "deepspeed-performance"
            | "megatron-performance"
            | "all-benchmarks" => {
                // Benchmarks are dispatched via benchmark_runners::run_benchmark()
            }
            // FastVideo — uses dedicated installer
            "fastvideo" => {
                use rusty_stack::installers::components::fastvideo::FastVideoConfig;
                let _ = rusty_stack::installers::components::fastvideo::FastVideoInstaller::new(
                    FastVideoConfig::default(),
                );
            }
            // llama-cpp — uses dedicated installer
            "llama-cpp" => {
                use rusty_stack::installers::components::llama_cpp::LlamaCppConfig;
                let _ = rusty_stack::installers::components::llama_cpp::LlamaCppInstaller::new(
                    LlamaCppConfig::default(),
                );
            }
            _ => panic!("Unknown native component ID: {}", id),
        }
    }
}

// ===========================================================================
// VAL-INSTALL-033: Sudo behavior preserved
// ===========================================================================

#[test]
fn test_native_components_preserve_needs_sudo_flag() {
    let components = default_components();
    let native_comps: Vec<&Component> = components
        .iter()
        .filter(|c| is_native_component(&c.id))
        .collect();

    // Verify sudo-requiring components still have needs_sudo=true
    let sudo_components: Vec<&&Component> = native_comps.iter().filter(|c| c.needs_sudo).collect();

    // Most installers need sudo (ROCm, PyTorch, etc.)
    assert!(
        sudo_components.len() >= 15,
        "At least 15 native components should need sudo, got {}",
        sudo_components.len()
    );

    // Some don't need sudo (app installers)
    let no_sudo: Vec<&&Component> = native_comps.iter().filter(|c| !c.needs_sudo).collect();

    assert!(
        no_sudo.iter().any(|c| c.id == "vllm-studio"),
        "vllm-studio should not need sudo"
    );
    assert!(
        no_sudo.iter().any(|c| c.id == "comfyui"),
        "comfyui should not need sudo"
    );
}

// ===========================================================================
// Cross-checks: default_components() consistency
// ===========================================================================

#[test]
fn test_default_components_total_count() {
    let components = default_components();
    // 23 native TUI installers (incl fastvideo + llama-cpp) + 3 verification + 8 performance = 34
    assert_eq!(components.len(), 34, "Expected 34 total components");
}

#[test]
fn test_no_script_field_contains_sh_for_native_components() {
    let components = default_components();
    for comp in &components {
        if is_native_component(&comp.id) {
            assert!(
                !comp.script.contains(".sh"),
                "Native component '{}' should not reference .sh in script field, got '{}'",
                comp.id,
                comp.script
            );
        }
    }
}

#[test]
fn test_all_components_are_native() {
    // All components in default_components() should now have empty script fields —
    // no component references .sh scripts anymore.
    // Verification uses a dedicated native path (run_verification), benchmarks
    // use benchmark_runners module, and installers use the native dispatch.
    let components = default_components();
    for comp in &components {
        assert!(
            comp.script.is_empty(),
            "Component '{}' should have empty script (all scripts ported to Rust), got '{}'",
            comp.id,
            comp.script
        );
    }
}