Typhon.NumPy
A NumPy-compatible library written in C# for Typhon programs: what it covers, where it differs from NumPy and how it performs.
NumPy’s core is written in C and Fortran, and Typhon does not compile it. A Typhon program that imports numpy runs against Typhon.NumPy instead, a NumPy-compatible library written in C# for this project. Typhon.NumPy builds on System.Numerics.Tensors.TensorPrimitives, which uses SIMD instructions.
Coverage
Typhon.NumPy covers the commonly used API:
- array creation, slicing and boolean masks
- ufuncs with broadcasting
- reductions with an
axisargument - the
@operator for matrix multiplication, also with broadcast batch dimensions - the
randommodule linalgfor 2-D matrices:solve,inv,det,slogdet,norm,matrix_power,cholesky,qr,eigh,eigvalsh,svd,svdvals,lstsq,pinv,matrix_rank,condandLinAlgError
eig, eigvals, stacked matrices and the ord argument of linalg are not supported.
Differences from NumPy
- Arrays hold
float64values only. - Slices and
.Treturn copies.reshapeandravelreturn views. a[i]returns adynamicvalue. An annotated target, as inv: float = a[i], brings the code back to static types.==on arrays compares references.np.equalcompares elements.- Random streams are deterministic for a given seed, and they differ from NumPy’s streams.
a.shapeis not a tuple, stepped slices of anndarrayare not supported, and the sum of a boolean array is a float. The conformance casenumpy_detailsfails for these reasons.
Indexing with a[i], slicing and np.dot return dynamic values. Native AOT cannot compile code that uses them, and typhon build --aot reports the exact lines (TY2001).
Threads
Typhon.NumPy uses as many threads as the machine has processors. The environment variable TYPHON_NUMPY_THREADS sets a different number, and TYPHON_NUMPY_THREADS=1 runs single-threaded. Matrix products and factorizations give the same result for any number of threads.
Performance
Six NumPy programs were compared on Windows, each side in its faster thread setting. Over all six, Typhon reached rough parity with NumPy: a geometric mean of 1.04x with the JIT and 1.13x with Native AOT.
| Program | NumPy | Fastest Typhon build | Result |
|---|---|---|---|
Element loop for i in range(n): a[i] | 271 ms | 54.0 ms (JIT) | Typhon 5.0x faster |
| Broadcasting (pairwise distances) | 268 ms | 52.9 ms (AOT) | Typhon 5.1x faster |
ufuncs (sqrt, exp, sin) | 1,184 ms | 522 ms (AOT) | Typhon 2.3x faster |
| Reductions along axes | 414 ms | 224 ms (AOT) | Typhon 1.8x faster |
| Matrix multiplication 1000 x 1000 | 17.4 ms | 24.4 ms (JIT) | NumPy 1.4x faster |
linalg.solve and inv 300 x 300 | 31.7 ms | 159 ms (AOT) | NumPy 5.0x faster |
Typhon.NumPy is faster where NumPy pays a per-call overhead in Python, for example when a loop reads single elements. In these runs, NumPy was faster where it calls tuned native libraries: OpenBLAS for matrix multiplication and LAPACK for solve and inv.
The last two rows predate a rewrite of Typhon.NumPy’s dense linear algebra. @ is now a packed, cache-blocked and threaded matrix multiplication, and linalg uses LAPACK’s algorithms on top of it, without a native library. On Linux with an Intel Core i7-10700K, a 1000 x 1000 matrix product then ran at 74 to 86 % of OpenBLAS’s speed on one thread and about as fast on all cores. inv of a 1000 x 1000 matrix went from 1.5 s to 91 ms, against 63 ms for NumPy. Section 9 of the benchmark report, linked from the benchmark page, has the details. Large temporary arrays are a weak spot of Typhon.NumPy, with a peak memory use of up to five times NumPy’s.