rustify-ml 0.1.2

Profile Python hotspots and auto-generate Rust + PyO3 stubs via maturin
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
/// Integration tests for rustify-ml pipeline stages.
///
/// These tests exercise the full generate() pipeline using the example
/// Python fixtures in examples/. They do NOT invoke maturin (no build
/// toolchain required) — they verify that:
///   1. The generator produces valid Rust source files on disk.
///   2. Zero fallback functions for well-formed translatable hotspots.
///   3. Generated lib.rs contains expected function names and PyO3 boilerplate.
use std::path::PathBuf;

use rustify_ml::generator::generate;
use rustify_ml::utils::{InputSource, TargetSpec};
use tempfile::tempdir;

fn load_example(name: &str) -> (PathBuf, InputSource) {
    let path = PathBuf::from(format!("examples/{}", name));
    let code = std::fs::read_to_string(&path)
        .unwrap_or_else(|e| panic!("failed to read examples/{}: {}", name, e));
    let source = InputSource::File {
        path: path.clone(),
        code,
    };
    (path, source)
}

// ── slow_tokenizer ──────────────────────────────────────────────────────────

#[test]
fn integration_slow_tokenizer_zero_fallback() {
    let (_, source) = load_example("slow_tokenizer.py");
    let targets = vec![TargetSpec {
        func: "tokenize".to_string(),
        line: 7,
        percent: 100.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1, "expected 1 function");
    assert_eq!(result.fallback_functions, 0, "tokenize should not fallback");
    assert!(
        tmp.path().join("rustify_ml_ext/src/lib.rs").exists(),
        "lib.rs not written for slow_tokenizer"
    );
}

// ── nested for fallback ───────────────────────────────────────────────────────

#[test]
fn integration_nested_for_else_fallback() {
    // for..else is unsupported; expect fallback count > 0
    let source = InputSource::Snippet(
        "def f(n):\n    total = 0\n    for i in range(n):\n        for j in range(n):\n            total += i + j\n    else:\n        total += 1\n    return total\n".to_string(),
    );
    let targets = vec![TargetSpec {
        func: "f".to_string(),
        line: 1,
        percent: 100.0,
        reason: "nested for else".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    assert!(
        result.fallback_functions > 0,
        "expected fallback for for..else construct"
    );
}

// ── euclidean ────────────────────────────────────────────────────────────────

#[test]
fn integration_euclidean_zero_fallback() {
    let (_, source) = load_example("euclidean.py");
    let targets = vec![TargetSpec {
        func: "euclidean".to_string(),
        line: 1,
        percent: 100.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1, "expected 1 function");
    assert_eq!(
        result.fallback_functions, 0,
        "euclidean should not fallback"
    );
    assert!(
        tmp.path().join("rustify_ml_ext/src/lib.rs").exists(),
        "lib.rs not written"
    );
    assert!(
        tmp.path().join("rustify_ml_ext/Cargo.toml").exists(),
        "Cargo.toml not written"
    );
}

#[test]
fn integration_euclidean_lib_rs_content() {
    let (_, source) = load_example("euclidean.py");
    let targets = vec![TargetSpec {
        func: "euclidean".to_string(),
        line: 1,
        percent: 100.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    generate(&source, &targets, tmp.path(), false).expect("generate failed");

    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");

    assert!(
        lib_rs.contains("use pyo3::prelude::*"),
        "missing pyo3 import"
    );
    assert!(lib_rs.contains("#[pyfunction]"), "missing #[pyfunction]");
    assert!(lib_rs.contains("#[pymodule]"), "missing #[pymodule]");
    assert!(lib_rs.contains("fn euclidean"), "missing fn euclidean");
    assert!(
        lib_rs.contains("for i in 0.."),
        "missing translated for loop"
    );
    assert!(lib_rs.contains("total"), "missing accumulator variable");
}

#[test]
fn integration_euclidean_len_check_emitted() {
    let (_, source) = load_example("euclidean.py");
    let targets = vec![TargetSpec {
        func: "euclidean".to_string(),
        line: 1,
        percent: 100.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    generate(&source, &targets, tmp.path(), false).expect("generate failed");

    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");

    // euclidean(p1, p2) has two Vec params → length check must be emitted
    assert!(
        lib_rs.contains("len() != ") || lib_rs.contains("length mismatch"),
        "expected length-check guard for two Vec params"
    );
}

// ── dot_product (matrix_ops) ─────────────────────────────────────────────────

#[test]
fn integration_dot_product_zero_fallback() {
    let (_, source) = load_example("matrix_ops.py");
    let targets = vec![TargetSpec {
        func: "dot_product".to_string(),
        line: 1,
        percent: 80.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    assert_eq!(
        result.fallback_functions, 0,
        "dot_product should not fallback"
    );
}

#[test]
fn integration_matmul_zero_fallback() {
    let (_, source) = load_example("matrix_ops.py");
    let targets = vec![TargetSpec {
        func: "matmul".to_string(),
        line: 7,
        percent: 95.0,
        reason: "matmul nested loops".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    assert_eq!(
        result.fallback_functions, 0,
        "matmul should fully translate without fallback"
    );
}

// ── normalize_pixels (image_preprocess) ──────────────────────────────────────

#[test]
fn integration_normalize_pixels_generates() {
    let (_, source) = load_example("image_preprocess.py");
    let targets = vec![TargetSpec {
        func: "normalize_pixels".to_string(),
        line: 1,
        percent: 90.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");
    assert!(
        lib_rs.contains("fn normalize_pixels"),
        "missing fn normalize_pixels"
    );
}

#[test]
fn integration_normalize_pixels_zero_fallback() {
    let (_, source) = load_example("image_preprocess.py");
    let targets = vec![TargetSpec {
        func: "normalize_pixels".to_string(),
        line: 1,
        percent: 90.0,
        reason: "integration fallback check".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    assert_eq!(
        result.fallback_functions, 0,
        "normalize_pixels should not fallback"
    );
}

// ── dry_run writes files ──────────────────────────────────────────────────────

#[test]
fn integration_dry_run_still_writes_files() {
    let (_, source) = load_example("euclidean.py");
    let targets = vec![TargetSpec {
        func: "euclidean".to_string(),
        line: 1,
        percent: 100.0,
        reason: "dry-run test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), true).expect("generate failed");

    // dry_run=true still writes files (for inspection), just skips maturin
    assert_eq!(result.generated_functions.len(), 1);
    assert!(
        tmp.path().join("rustify_ml_ext/src/lib.rs").exists(),
        "dry-run should still write lib.rs"
    );
}

// ── multiple targets ──────────────────────────────────────────────────────────

#[test]
fn integration_multiple_targets_same_file() {
    let (_, source) = load_example("matrix_ops.py");
    let targets = vec![
        TargetSpec {
            func: "dot_product".to_string(),
            line: 1,
            percent: 60.0,
            reason: "integration test".to_string(),
        },
        TargetSpec {
            func: "matmul".to_string(),
            line: 1,
            percent: 40.0,
            reason: "integration test".to_string(),
        },
    ];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 2, "expected 2 functions");
    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");
    assert!(lib_rs.contains("fn dot_product"), "missing dot_product");
    assert!(lib_rs.contains("fn matmul"), "missing matmul");
}

// ── BPE tokenizer (while loop) ───────────────────────────────────────────────

#[test]
fn integration_count_pairs_generates_while_loop() {
    let (_, source) = load_example("bpe_tokenizer.py");
    let targets = vec![TargetSpec {
        func: "count_pairs".to_string(),
        line: 1,
        percent: 85.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");
    assert!(lib_rs.contains("fn count_pairs"), "missing fn count_pairs");
    // count_pairs uses a for loop over range(len(tokens)-1)
    assert!(
        lib_rs.contains("for i in 0.."),
        "expected for loop in count_pairs, got:\n{}",
        lib_rs
    );
}

#[test]
fn integration_bpe_encode_generates() {
    let (_, source) = load_example("bpe_tokenizer.py");
    let targets = vec![TargetSpec {
        func: "bpe_encode".to_string(),
        line: 1,
        percent: 90.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    let result = generate(&source, &targets, tmp.path(), false).expect("generate failed");

    assert_eq!(result.generated_functions.len(), 1);
    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");
    assert!(
        lib_rs.contains("fn bpe_encode"),
        "missing fn bpe_encode, got:\n{}",
        lib_rs
    );
    // bpe_encode has while loops — check they're translated
    assert!(
        lib_rs.contains("while "),
        "expected while loop in bpe_encode, got:\n{}",
        lib_rs
    );
}

// ── golden file snapshot ──────────────────────────────────────────────────────

/// Golden file test: assert the generated lib.rs for euclidean.py contains all
/// expected structural elements. Update the snapshot by running:
///   UPDATE_SNAPSHOTS=1 cargo test integration_euclidean_golden
#[test]
fn integration_euclidean_golden() {
    let (_, source) = load_example("euclidean.py");
    let targets = vec![TargetSpec {
        func: "euclidean".to_string(),
        line: 1,
        percent: 100.0,
        reason: "integration test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    generate(&source, &targets, tmp.path(), false).expect("generate failed");

    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");

    // Structural invariants that must always hold
    let required = [
        "use pyo3::prelude::*;",
        "#[pyfunction]",
        "pub fn euclidean(",
        "py: Python",
        "Vec<f64>",
        "PyResult<f64>",
        "for i in 0..",
        "powf(",
        "#[pymodule]",
        "fn rustify_ml_ext(",
        "wrap_pyfunction!(euclidean",
    ];
    for pat in &required {
        assert!(
            lib_rs.contains(pat),
            "golden snapshot missing pattern {:?}\n\nActual lib.rs:\n{}",
            pat,
            lib_rs
        );
    }

    // Snapshot file: write if UPDATE_SNAPSHOTS=1, else compare prefix
    let snap_path = std::path::PathBuf::from("tests/snapshots/euclidean_lib_rs.snap");
    if std::env::var("UPDATE_SNAPSHOTS").is_ok() {
        std::fs::create_dir_all(snap_path.parent().unwrap()).ok();
        std::fs::write(&snap_path, &lib_rs).expect("write snapshot");
        println!("Snapshot updated: {}", snap_path.display());
    } else if snap_path.exists() {
        let snap = std::fs::read_to_string(&snap_path).expect("read snapshot");
        // Compare only the first 5 lines (header) to avoid fragile full-text matching
        let snap_head: Vec<&str> = snap.lines().take(5).collect();
        let actual_head: Vec<&str> = lib_rs.lines().take(5).collect();
        assert_eq!(
            snap_head, actual_head,
            "golden snapshot header mismatch — run UPDATE_SNAPSHOTS=1 cargo test to refresh"
        );
    }
}

// ── pow translation ───────────────────────────────────────────────────────────

#[test]
fn integration_euclidean_pow_translated() {
    let (_, source) = load_example("euclidean.py");
    let targets = vec![TargetSpec {
        func: "euclidean".to_string(),
        line: 1,
        percent: 100.0,
        reason: "pow test".to_string(),
    }];
    let tmp = tempdir().expect("tempdir");
    generate(&source, &targets, tmp.path(), false).expect("generate failed");

    let lib_rs =
        std::fs::read_to_string(tmp.path().join("rustify_ml_ext/src/lib.rs")).expect("read lib.rs");

    // total ** 0.5 should translate to (total).powf(0.5)
    assert!(
        lib_rs.contains("powf"),
        "expected powf translation for ** operator"
    );
}