rumpy 0.1.1

A reimplementation of NumPy for use with rustpython in embedded interpreters
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
//! Cross-validation tests: every operation is run under both
//!
//!   * **rumpy** — our `numpy` module embedded in a RustPython VM
//!   * **numpy** — the real CPython numpy library accessed via pyo3
//!
//! and the resulting `.tolist()` outputs are compared element-wise.
//!
//! We share the exact Python source between the two implementations so the
//! only thing that varies is the runtime. If a test fails it almost
//! certainly means our implementation diverged from numpy's semantics.

use approx::assert_abs_diff_eq;
use pyo3::prelude::*;
use pyo3::types::{PyAnyMethods, PyList, PyModule};
use rustpython_vm::{AsObject, Interpreter, builtins::PyList as RpyList};

/// Run a Python snippet in a RustPython VM that has our `numpy` module
/// pre-registered. The snippet must end by assigning the value of interest
/// to the global name `result`.
fn run_in_rumpy(source: &str) -> RumpyResult {
    let interp = {
        let builder = Interpreter::builder(Default::default());
        let def = rumpy::module_def(&builder.ctx);
        builder.add_native_module(def).build()
    };
    let outcome = interp.enter(|vm| -> Result<RumpyResult, String> {
        let scope = vm.new_scope_with_builtins();
        let code = vm
            .compile(
                source,
                rustpython_vm::compiler::Mode::Exec,
                "<test>".into(),
            )
            .map_err(|e| format!("compile error: {e}"))?;
        vm.run_code_obj(code, scope.clone())
            .map_err(|e| py_err_string(vm, &e))?;
        let result = scope
            .globals
            .get_item("result", vm)
            .expect("snippet must set `result`");
        extract_rumpy_value(&result, vm).map_err(|e| py_err_string(vm, &e))
    });
    outcome.unwrap_or_else(|e| panic!("rumpy snippet failed: {e}\n--- source ---\n{source}"))
}

fn py_err_string(
    vm: &rustpython_vm::VirtualMachine,
    e: &rustpython_vm::PyRef<rustpython_vm::builtins::PyBaseException>,
) -> String {
    let mut out = String::new();
    if vm.write_exception(&mut out, e).is_err() {
        return format!("<unprintable exception of type {}>", e.class().name());
    }
    out
}

struct RumpyResult {
    shape: Option<Vec<usize>>,
    data: Vec<f64>,
}

fn extract_rumpy_value(
    obj: &rustpython_vm::PyObjectRef,
    vm: &rustpython_vm::VirtualMachine,
) -> rustpython_vm::PyResult<RumpyResult> {
    use rumpy::{ArraysD, PyNdArray};
    // Array case — collapse dtype to f64 for cross-checking against numpy.
    if let Some(arr) = obj.downcast_ref::<PyNdArray>() {
        let g = arr.view().cast(rumpy::DType::F64);
        let f = match &g {
            ArraysD::F64(x) => x,
            _ => unreachable!(),
        };
        return Ok(RumpyResult {
            shape: Some(f.shape().to_vec()),
            data: f.iter().copied().collect(),
        });
    }
    // Scalar (int/float)
    if let Ok(f) = obj.try_float(vm) {
        return Ok(RumpyResult {
            shape: None,
            data: vec![f.to_f64()],
        });
    }
    // Tuple of ints (e.g. .shape)
    if let Some(t) = obj.downcast_ref::<rustpython_vm::builtins::PyTuple>() {
        let mut data = Vec::with_capacity(t.len());
        for it in t.as_slice() {
            data.push(it.try_float(vm)?.to_f64());
        }
        return Ok(RumpyResult {
            shape: Some(vec![data.len()]),
            data,
        });
    }
    // Nested list — recursively flatten
    if let Some(l) = obj.downcast_ref::<RpyList>() {
        let mut shape = Vec::new();
        let mut data = Vec::new();
        flatten_pylist(l, &mut shape, &mut data, vm, 0)?;
        return Ok(RumpyResult {
            shape: Some(shape),
            data,
        });
    }
    Err(vm.new_type_error(format!(
        "cannot extract rumpy result of type {}",
        obj.class().name()
    )))
}

fn flatten_pylist(
    list: &RpyList,
    shape: &mut Vec<usize>,
    data: &mut Vec<f64>,
    vm: &rustpython_vm::VirtualMachine,
    depth: usize,
) -> rustpython_vm::PyResult<()> {
    let items = list.borrow_vec();
    if depth == shape.len() {
        shape.push(items.len());
    }
    for it in items.iter() {
        if let Some(sub) = it.downcast_ref::<RpyList>() {
            flatten_pylist(sub, shape, data, vm, depth + 1)?;
        } else {
            data.push(it.try_float(vm)?.to_f64());
        }
    }
    Ok(())
}

/// Run the same source in real CPython+numpy. Same convention: snippet
/// assigns to `result`. We coerce the result into `(shape, flat_data)`
/// using `numpy.asarray(result).ravel().tolist()`.
fn run_in_numpy(source: &str) -> NumpyResult {
    Python::attach(|py| -> PyResult<NumpyResult> {
        let globals = pyo3::types::PyDict::new(py);
        let numpy = PyModule::import(py, "numpy")?;
        globals.set_item("numpy", &numpy)?;
        globals.set_item("np", &numpy)?;

        py.run(&std::ffi::CString::new(source).unwrap(), Some(&globals), None)?;
        let result = globals.get_item("result")?.unwrap();
        // Use numpy itself to canonicalize: shape + flat list.
        let arr = numpy.getattr("asarray")?.call1((result,))?;
        let shape: Vec<usize> = arr.getattr("shape")?.extract()?;
        let flat = arr.call_method0("ravel")?.call_method0("tolist")?;
        let data: Vec<f64> = flat.cast::<PyList>()?.extract()?;
        Ok(NumpyResult { shape, data })
    })
    .expect("numpy snippet failed")
}

struct NumpyResult {
    shape: Vec<usize>,
    data: Vec<f64>,
}

/// Compare a rumpy run with a numpy run for the same snippet.
fn assert_same(snippet: &str) {
    let r = run_in_rumpy(snippet);
    let n = run_in_numpy(snippet);
    match r.shape {
        None => {
            // Scalar from rumpy; numpy shape should be `()`.
            assert!(
                n.shape.is_empty(),
                "rumpy returned scalar but numpy returned shape {:?} for snippet:\n{snippet}",
                n.shape
            );
        }
        Some(rs) => {
            assert_eq!(
                rs, n.shape,
                "shape mismatch (rumpy={:?}, numpy={:?}) for snippet:\n{snippet}",
                rs, n.shape
            );
        }
    }
    assert_eq!(
        r.data.len(),
        n.data.len(),
        "element count mismatch (rumpy={}, numpy={}) for snippet:\n{snippet}",
        r.data.len(),
        n.data.len()
    );
    for (i, (a, b)) in r.data.iter().zip(n.data.iter()).enumerate() {
        if a.is_nan() && b.is_nan() {
            continue;
        }
        assert_abs_diff_eq!(*a, *b, epsilon = 1e-9);
        let _ = i;
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn zeros_and_shape() {
    assert_same("import numpy as np\nresult = np.zeros((3, 4)).shape");
    assert_same("import numpy as np\nresult = np.zeros((3, 4))");
}

#[test]
fn ones_and_full() {
    assert_same("import numpy as np\nresult = np.ones((2, 3))");
    assert_same("import numpy as np\nresult = np.full((2, 2), 7.5)");
}

#[test]
fn arange() {
    assert_same("import numpy as np\nresult = np.arange(10)");
    assert_same("import numpy as np\nresult = np.arange(2, 8)");
    assert_same("import numpy as np\nresult = np.arange(0.0, 1.0, 0.25)");
}

#[test]
fn linspace() {
    assert_same("import numpy as np\nresult = np.linspace(0.0, 1.0, 5)");
    assert_same("import numpy as np\nresult = np.linspace(-1.0, 1.0, 11)");
}

#[test]
fn eye_and_identity() {
    assert_same("import numpy as np\nresult = np.eye(4)");
    assert_same("import numpy as np\nresult = np.identity(3)");
}

#[test]
fn array_from_nested_list() {
    assert_same(
        "import numpy as np\nresult = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])",
    );
}

#[test]
fn arithmetic_broadcasting() {
    assert_same(
        r#"
import numpy as np
a = np.arange(6).reshape(2, 3)
b = np.array([10.0, 20.0, 30.0])
result = a + b
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(6).reshape(2, 3)
result = a * 2.0 - 1.0
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.ones((3, 1))
b = np.arange(4.0)
result = a * b
"#,
    );
}

#[test]
fn elementwise_ufuncs() {
    assert_same(
        r#"
import numpy as np
x = np.linspace(0.1, 1.0, 5)
result = np.sqrt(x) + np.log(x) * np.sin(x)
"#,
    );
    assert_same(
        r#"
import numpy as np
x = np.array([-2.0, -1.0, 0.0, 1.0, 2.0])
result = np.exp(x) - np.abs(x)
"#,
    );
}

#[test]
fn reductions_full() {
    assert_same(
        r#"
import numpy as np
a = np.arange(12).reshape(3, 4)
result = np.array([a.sum(), a.mean(), a.min(), a.max(), a.prod()])
"#,
    );
}

#[test]
fn reductions_axis() {
    assert_same(
        r#"
import numpy as np
a = np.arange(12).reshape(3, 4)
result = a.sum(axis=0)
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(12).reshape(3, 4)
result = a.mean(axis=1)
"#,
    );
}

#[test]
fn variance_and_std() {
    assert_same(
        r#"
import numpy as np
a = np.array([1.0, 2.0, 4.0, 8.0, 16.0])
result = np.array([a.var(), a.std()])
"#,
    );
}

#[test]
fn reshape_transpose_flatten() {
    assert_same(
        r#"
import numpy as np
a = np.arange(12).reshape(3, 4)
result = a.T
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(24).reshape(2, 3, 4)
result = a.reshape(-1)
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(12).reshape(3, 4)
result = a.flatten()
"#,
    );
}

#[test]
fn dot_and_matmul() {
    assert_same(
        r#"
import numpy as np
a = np.arange(6).reshape(2, 3).astype(float) if False else np.arange(6.0).reshape(2, 3)
b = np.arange(12.0).reshape(3, 4)
result = a @ b
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(4.0)
b = np.arange(4.0) * 2.0
result = np.dot(a, b)
"#,
    );
    assert_same(
        r#"
import numpy as np
A = np.array([[1.0, 2.0], [3.0, 4.0]])
v = np.array([1.0, -1.0])
result = A.dot(v)
"#,
    );
}

#[test]
fn indexing_scalar_and_slice() {
    assert_same(
        r#"
import numpy as np
a = np.arange(12.0).reshape(3, 4)
result = float(a[1, 2])
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(12.0).reshape(3, 4)
result = a[1]
"#,
    );
    assert_same(
        r#"
import numpy as np
a = np.arange(12.0).reshape(3, 4)
result = a[1:, 1:3]
"#,
    );
}

#[test]
fn module_constants() {
    assert_same("import numpy as np\nresult = np.pi");
    assert_same("import numpy as np\nresult = np.e");
}

#[test]
fn power_and_negation() {
    assert_same(
        r#"
import numpy as np
a = np.linspace(0.5, 3.0, 6)
result = -a ** 2 + 1
"#,
    );
}

#[test]
fn concatenate_axis() {
    // axis=0 (default) — joins rows
    assert_same(
        r#"
import numpy as np
a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])
result = np.concatenate([a, b])
"#,
    );
    // axis=1 — joins columns (this is the bug that was silently wrong)
    assert_same(
        r#"
import numpy as np
a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])
result = np.concatenate([a, b], axis=1)
"#,
    );
    // axis=-1 — last axis
    assert_same(
        r#"
import numpy as np
a = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = np.array([[7.0, 8.0], [9.0, 10.0]])
result = np.concatenate([a, b], axis=-1)
"#,
    );
    // axis=None — flatten then concatenate
    assert_same(
        r#"
import numpy as np
a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])
result = np.concatenate([a, b], axis=None)
"#,
    );
}