RSTSR v0.10: A Native Rust n-Dimensional Tensor Library for Scientific Computing, Fully Compatible with the Python Array API Standard

After two years of development, RSTSR has reached a milestone: an efficient-on-CPU, native Rust, n-dimensional tensor scientific-computing library that is highly compatible with the NumPy interface.

We believe NumPy's interface design for n-dimensional tensors is excellent. With RSTSR we show that native Rust scientific computing can be as easy to write and read as NumPy, while being more efficient, safer, and better suited to parallel computing.

For details, see the RSTSR v0.10 blog post: RSTSR 3rd Report | RSTSR: An n-Dimensional Rust Tensor Toolkit

For NumPy users, RSTSR provides a NumPy-RSTSR Cheatsheet to get you up to speed quickly.

Fully Compatible with the Python Array API 2025 Standard

As a native Rust library, RSTSR implements almost every interface of the Python Array API 2025 standard. RSTSR is no longer merely "inspired by" the Array API โ€” it is a non-Python library that genuinely implements the Array API standard.

The Python Array API 2025 standard covers nearly all of NumPy's commonly used and important functionality, including:

  • Creation: zeros, ones, full, arange, linspace, eye, meshgrid, etc.;
  • Indexing: basic indexing/slicing, advanced/fancy indexing, element indexing;
  • Shape manipulation: reshape, transpose, swapaxes, moveaxis, expand_dims, squeeze, etc.;
  • Broadcasting: broadcast_to, broadcast_arrays, etc.; broadcasting also applies to tensor arithmetic and other operations;
  • Arithmetic: addition, subtraction, multiplication, division, trigonometric functions, etc.;
  • Binary operations: such as logical and comparison operations;
  • Linear algebra: matmul, vecdot, eigh, svd, etc.;
  • Statistical operations: sum, mean, var, std, min, max, etc.;
  • Other tensor operations: concatenate, stack, split, repeat, tile, etc.

rstsr-faer-py, a thin Python wrapper built on RSTSR v0.10.0 with the Faer backend, is available on PyPI. Against the Array API 2025 test suite it scores 1336 Pass / 17 Fail / 33 Skip (NumPy 2.5, for comparison, scores 1339 Pass / 42 Fail / 5 Skip). Of these, 14 failures / 33 skips come from the test suite itself and from the RSTSR Faer backend not implementing FFT; the remaining 3 failures are a floating-point overflow in SVD that is rare in practical situations. This shows that, apart from FFT, RSTSR correctly implements every Array API 2025 interface.

Although we do not literally implement every Array API 2025 interface, that is because a small number are difficult or impossible to implement due to the nature of the Rust language. So even where a solution exists in Python, we do not provide a one-to-one native Rust implementation. The excluded features are:

  • The operator overloads __ipow__, __imatmul__ that Python permits but Rust lacks (though they can be achieved with functions);
  • Dynamic-typed lists, which Python allows but are very hard to express in statically typed Rust. The affected functions are concat and stack (so long as the tensors share the same dtype, RSTSR can still use rt::concat and rt::stack).

Key Features of RSTSR

  • Use % as the matrix-multiplication operator: a distinctive feature of RSTSR. You can write &a % &b instead of a.matmul(&b), making matrix multiplication more concise.
  • Excellent parallelism management: for multithreaded BLAS backends, our rayon-based thread control guarantees that whether RSTSR's general computation or matrix linear algebra is called inside or outside rayon parallel threads, the parallel device is fully utilized without ever exceeding the thread count specified by the user.
  • Support for both row-major and column-major: RSTSR defaults to row-major memory layout, but also allows column-major layout, which is already used extensively in the real-world REST electronic-structure program.
  • Multiple BLAS backends: RSTSR can use Faer's default multithreaded backend, or OpenBLAS, Intel MKL, BLIS, KML, and other BLAS backends; CPU performance is on par with NumPy. Matrix linear algebra operations such as solve, svd, and eigh are supported.
  • Extensible backends: generics support multiple backends. Users need not modify RSTSR's source code, and the data need not reside in memory (GPU, disk, or network storage can hold it) โ€” implementing the new backend trait interfaces is enough to perform mathematical computation with a custom backend.
  • Multiple data types: RSTSR supports ordinary floating-point types as well as arbitrary data structures (including types such as strings that require alloc). In electronic-structure programs, f64 and c64 are the most common dtypes; we support them and provide special optimizations for them.
  • Zero-overhead reshape: under a reasonable tensor layout (not necessarily fully contiguous in memory), RSTSR can implement reshape as a view, with no extra memory overhead.
  • Einstein summation support: with the external TBLIS tensor-contraction library, RSTSR can perform tensor contractions using the same Einstein-summation syntax as NumPy. See rt::tblis::einsum.
  • Efficient Vecdot support: in electronic structure โ€” particularly some dense density-functional matrix computations โ€” tensor contractions of the form "pi, pi -> p" are very common and are indeed a significant memory-bandwidth bottleneck. Our implementation of rt::vecdot is not naive: its multi-core parallel performance is excellent, it is much faster than a plain elementwise multiply followed by a sum, and it uses less memory.

This is an interesting project for scientific computation.

The main question that comes to mind is, how does your library integrate with the existing tensor or linear algebra rust ecosystem? How does it compare with it in case there is overlap in what the libraries do? In short, why choose your library instead of existing ones?

Minor point, &a % &b for matrix multiplication does not look like an improvement to me, but rather a new confusing notation.

Good point.

For the minor point, I think that it is convenient for python to have a @ b as matrix multiplication. Rust does not have equilvant operator like this; and as operator % to be of the same precedence to *, and tensor-elementwise remainder is seldomly used as I guess, so we choose %. We still provide the choice of using a.matmul(&b) to be matrix multiplication, and rt::rem(&a, &b) for remainder, if % seems to be confusing. This is a matter of choice for users.

For the major point. More details refer user document (also responds to "overlap in what the libraries do", written at version v0.3.5 last year but most discussion still applies now)

For the rust community, it is commonly accepted that ndarray is a good n-dimensional tensor library. However, there are some points that short of, to our aim of developing electronic structure program:

  • The greatest motivation is how to call BLAS. In many cases, we need both multi-threaded BLAS at main thread, while single-threaded BLAS in spawned parallel threads.

    // parallel matmul or BLAS outside rayon
    let c = &a % &b;
    
    // parallel matmul or BLAS inside rayon
    (0..100).into_par_iter().for_each(|i| {
        let d = &a % &b;
    });
    

    If not properly configured, either let c = &a % &b may done in single-threaded (not properly paralleled), or let d = &a % &b to be thread-oversubscribted (exceeding CPU thread usage demand).

    Also, we need in-place matrix triangular solve (like scipy's scipy.linalg.solve_triangular(a, b, overwrite_b=True)). For RSTSR, this can be resolvable as rt::linalg::solve_triangular((&a, b.view_mut())). And if we do not need overwrite_b=True, we can also call rt::linalg::solve_triangular((&a, &b)). We use rust's trait-based overload trick, so one function can do different jobs.

    We need both some more innovation beyond crate ndarray, in how BLAS (or other backends) should be called, and how user interface can be adapted with such kind of advanced features.

    We also need some schemes that backend can be configured by user, so that if one backend fails, the main RSTSR code does not need to be modified. A backend crate can do the whole thing. And anyway, it's our wish to make the backend to be more easily configurable, that cause our team previously developed crate rest_tensors and I then developed crate rstsr. Note that rstsr not going to integrate MPI at this current time, but will probably support GPU (CUDA/HIP/DCU, maybe others) in very near future.

    I believe this is also not fully covered by some other fameous frameworks like burn and candle. They focus on machine learning not linear algebra. We focus on different aims. For python alike, we may develop something more like numpy/scipy/cupy, instead of pytorch/jax.

  • Electronic structure programs really need very complicated basic indexing and reshape. We may usually allocate hundreds of GB, seldomly up to 1 TB, resources on a single node. Truely zero-copy indexing / reshape / broadcasting can be important. Crate ndarray has done good job, but not full support. We need full support on basic indexing and reshape.

  • Our team leader comes from Fortran community, and he insists to use column-major. So we now have both row-major and column-major supported, with row-major to be default for common users.

  • For the point on existing rust linear algebra ecosystem, we use crate faer as default backend, to enable fast linear algebra and fast parallel elementwise operations. In our real electronic structure program, we use OpenBLAS backend.