Vectorizer: Vectorizing NumPy Programs with Shape-Guided Rewrite
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.83x faster than the original loop-based implementations.
Create a lesson
Metadata
- Categories
- cs.CL, cs.SE
Related papers
Dynamic-Programming-Guided Hierarchical BPE and Empirical Analysis of Vocabulary Pruning
Shared title terminologycs.CLKenny Shao
Lightweight Reasoning Transfer via Chain-of-Thought Vector Projection
Shared title terminologycs.CLYulin Qi, Shuo Xu, Shuxia Lin et al.
Worth the Wait? How Response Time and Streaming Shape Judgments of Generative AI Answers and Chatbot Engagement
Shared title terminologycs.CLZihan Chen, Di Zhu, Lei Nico Zheng
Data-Efficient Language Modeling: From Frontier Advancement to Principle-Guided Model Improvement
Shared title terminologycs.CLShuxing Yang, Kaihao Zhu, Junjie Yang et al.
ActReview: Rebuttal-Guided Training Data and Rubric Rewards for Actionable Peer Review Generation
Shared title terminologycs.CLYiling Ma, Yilun Zhao, Sihong Wu et al.
FramingQA: Does the Question Shape the Answer? Measuring the Compositional Framing Effect
Shared title terminologycs.CLHazel H. Kim, Andrew M. Bean, Guilherme Affonso Ferreira de Camargo et al.