• NumPy 1.20 Has been Released with Runtime SIMD Support and Type Annotations
  • Originally written by Bruno Courio
  • The Nuggets translation Project
  • Permanent link to this article: github.com/xitu/gold-m…
  • Translator: Hoarfroster
  • Proofreader: HumanBeingXenon, zenblo

NumPy 1.20 came with runtime SIMD support and type annotations

The new NumPy 1.20 release offers performance and documentation improvements. We can now use type annotations in NumPy functions. The wider use of SIMD (Single instruction stream multiple data Stream) instructions completes the common functionality (UFUNC). NumPy has also made significant improvements to its documentation.

NumPy library code is now annotated with type information, as a result of NumPy no longer supporting Python 2. One contributor explained the reasoning behind the move, as follows:

A few years ago, when we started using Numpy-STUbs, it seemed premature to put type annotations into Numpy itself. We were still supporting Python 2 at the time, which meant we needed to use awkward annotations in type annotations. Over the past few years, the use of type annotations has become increasingly popular, even within the Python science stack. For example, I know that at least part of the API types of SciPy, PANDAS, and Xarray are commented out. Even without annotations for Shape or Dtype, it is valuable for NumPy, an item at the bottom of the science stack, to have near-complete annotations.

Developers can also use new types, ArrayLike and DTypeLike. The ArrayLike type is used for objects that can be converted to arrays, while DTypeLike is used for objects that can be converted to dtypes. The data type object (numpy.dtype) is used to specify the contents of the memory block corresponding to a fixed size to the array item and contains information about the item data type (e.g., integer, floating point), data size, byte order ([little-endian](numpy.org/doc/stable/… Html# term-little-endian) or big-endian). These two new types enable the type checker to identify inefficient patterns and alert the user. The document explains:

The DTypeLike type tries to avoid creating a DType object with a field dictionary like this:

X = np.dtype({" field1 ":(float.1), "field2" :int.3)})Copy the code

Although this is valid Numpy code, the type recognizer will alert you to the problem because its usage is unclear.

The new Numpy. Typing module contains new type aliases that can be imported at run time:

from numpy.typing import ArrayLike
x: ArrayLike = [1, 2, 3, 4]
Copy the code

NumPy 1.20 also enables multi-platform SIMD compiler optimization, and NumPy is now able to detect and optimize SIMD instructions provided by the CPU. Users can configure runtime optimization behavior with several new build parameters. The –cpu-baseline parameter is used to specify the minimum set of desired optimizations, while –cpu-dispatch is used to specify other optimizations that have been scheduled. (The default max-xop-fMA4 means that all CPU functions other than AMD’s old ones will be enabled.) Users can opt out of using the new improvements by using the –disable-optimization parameter.

To use NumPy 1.20, upgrade to Python 3.7 or later. In order to improve NumPy’s online performance and friendliness to new users, the new NumPy distribution has significantly improved its documentation – the distribution mentions merging 185 related pull requests, which is an ongoing work.

NumPy 1.20 was a large project with a combined 184 people contributing 684 pull requests. The full release notes are available online and contain information about additional features and not recommended use.

Some users are embracing this new type of annotation rather than just comparing it to Julia. Julia is another dynamically typed programming language designed specifically for high-performance scientific computing. It is commonly used in machine learning, data mining, large-scale linear algebra, distributed and parallel computing. One user wrote on HackerNews:

It is true that type annotation is better with Julia, but the type annotation that NumPy brings is beneficial to many users who do not use Julia. Number crunching isn’t the main problem, it’s also important that Python has more and better libraries, and that Julia doesn’t have to call Python.

NumPy is an open source Python library that adds support for large, multidimensional, isomorphic types of arrays and matrices. NumPy contains a bunch of mathematical functions for creating and transforming these arrays, linear algebra routines, and so on. NumPy is at the heart of SciPy, a Python-based open source software ecosystem for mathematics, science and engineering. NumPy enables data scientists to perform data analysis tasks using efficient scripting languages.

If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.


The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.