arXiv:2609.08088cs.CLSep 2026

Manuscript Offprint

arXiv:2609.08088

Vectorizer: Vectorizing NumPy Programs with Shape-Guided Rewrite

Jingqian Liu, Xiaoyu Liu, Yuepeng Wang

Abstract

NumPy is a widely used Python library for numerical scientific computing, known for its declarative APIs and its optimized implementations. However, writing efficient NumPy programs, which often entails using vectorized array operations instead of explicit Python loops, may not be straightforward. This can be difficult for programmers who are accustomed to imperative array traversal, especially when vectorized API invocations require careful reasoning about shapes, broadcasting, and advanced indexing. This paper presents a rewrite-based approach for vectorizing NumPy programs with explicit loops over array data. Our approach vectorizes loops from the inside out, using array shapes and dataflow analysis to guide a source-to-source transformation that replaces loop bodies with vectorized statements. Following a set of rewrite rules that are correct by construction, our approach is consistently fast. We have implemented the approach as a tool called VECTORIZER and evaluated it on 150 benchmarks collected from prior work and Stack Overflow. The evaluation shows that VECTORIZER vectorizes 142 of the 150 benchmarks directly and 2 more after minor changes to the original benchmarks, with only 0.53 seconds on average to rewrite each one. The resulting programs are, on average, 74.83× faster than the original loop-based implementations.

1 Introduction

NumPy [23] offers high-level APIs for efficient computation with multidimensional arrays while hiding optimizations and memory management in its low-level implementation. Such APIs are designed around concepts like axes and shapes. Built upon such concepts, techniques such as broadcasting enable concise, declarative array programming. However, correctly aligning axes for broadcasting and advanced indexing can be confusing, even for experienced programmers. As a result, programmers often write nested loops to traverse arrays, which may incur substantial runtime inefficiency.

Vectorizing loop-based NumPy code is challenging. First, Python has a rich syntax. NumPy exposes many operators with flexible interfaces, which makes it difficult to lift arbitrary NumPy code. Second, an effective lifter should incorporate techniques like broadcasting and advanced indexing to improve memory and runtime efficiency. These techniques require properly aligning axes, which can be tricky for code with nested loops and many variables. Third, practical NumPy programs often contain branches, reductions, and other structures whose vectorized forms do not have a straightforward correspondence to the original code.

Existing work attempts to address similar problems. For example, TENSPILER [44] lifts C++ programs to high-level array DSLs by summarizing the input as logical formulas and searching for an equivalent target program. However, it requires program-specific configurations, and extending it with new operators requires additional axioms. Both tasks demand non-trivial user effort. TENSORIZE [15] summarizes programs via symbolic execution and searches for an equivalent program with pruning based on symbolic algebra solvers. It expects input in the Affine dialect of MLIR [29], which imposes exacting requirements on the source program.

Upon reviewing NumPy questions on Stack Overflow and NumPy code on GitHub, we observe that many loops can be replaced by vectorized statements through deductive program rewrites. The key insight is that vectorizing the innermost loop expands its loop variable to an array with an additional axis. Variables defined in outer scopes can be treated as free variables with fixed shapes. By carefully rewriting expressions in the loop based on shapes, we can propagate the newly added dimension to vectorize the entire loop.

For example, suppose we want to vectorize the code below.

1 for i in range(A.shape[0]): 2 for j in range(A.shape[1]): 3 A[i, j] = i + j

We can first vectorize the innermost loop, treating i as a free variable. Replacing j with np.arange(A.shape[1]), the 1-D array of all values taken by j, yields the following code.

1 for i in range(A.shape[0]): 2 js_ = np.arange(A.shape[1]) 3 A[i, js_] = i + js_

With broadcasting, we expand both sides of Line 3 to 1-D arrays, updating all the elements of each row of A at once.

In this paper, we present a novel method based on this insight for rewriting Python loops over array data into vectorized NumPy code. The high-level workflow of our method is shown in Figure 1. Rather than attempting to reason about the full NumPy, we introduce a domain-specific language (DSL) that captures the common operations needed for practical loop vectorization while keeping the analysis tractable.

Figure

Figure 1. An overview of the workflow.

Within this DSL, we design a set of rewrite rules for vectorizing code with explicit loops over data. The rules are correct by construction, which allows the transformation procedure to avoid enumerative search or a separate equivalence-checking phase. The rewrite process is guided by dataflow analysis and inferred type information about array shape and maskedness. Our rewrite procedure works inside out, repeatedly vectorizing the innermost loop until there is no more loop in the program. After rewriting, we apply post-processing optimizations, such as indexing simplifications, sum-after-multiplication-to-tensordot replacements, common subexpression elimination and unused variable elimination, to produce the final runnable NumPy program.

We have implemented our method in a tool called VECTORIZER and evaluated it on 150 benchmarks. The results show that our technique offers significantly better applicability compared to tools from previous work. Specifically, VECTORIZER can successfully vectorize 142 of the 150 benchmarks directly, and 2 more benchmarks after minor adaptations. On average, VECTORIZER can rewrite a NumPy function in only 0.53 seconds, and the resulting programs are, on average, 74.83× faster than the original implementations.

Contributions. We make the following main contributions:

We designed a DSL capturing the core structures and APIs of real-world NumPy programs. We formalize its semantics and type inference rules for shapes and maskedness.

We propose an inside-out loop vectorization algorithm guided by shapes, maskedness, and dataflow analysis.

We formulate the core vectorization procedure as a set of correct-by-construction rewrite rules over the DSL.

We implement the approach as a tool called VECTORIZER and evaluate it on 150 benchmarks. The results show that it is effective and efficient, and the obtained programs are on average 74.83× faster than original implementations.

References (14)