Skip to content

Commit 723344a

Browse files
committed
Use cases section: add a Numba/JIT use case
1 parent bb772d5 commit 723344a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

spec/use_cases.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,43 @@ without having to make the above-mentioned choices.
172172
XND is another array library, written in C, that still needs a Python API.
173173
Array implementations in other languages are often in a similar situation,
174174
and could translate this array API standard 1:1 to their language.
175+
176+
177+
### Use case 4: make JIT compilation of array computations easier and more robust
178+
179+
[Numba](https://github.com/numba/numba) is a Just-In-Time (JIT) compiler for
180+
numerical functions in Python; it is NumPy-aware. [PyPy](https://pypy.org)
181+
is an implementation of Python with a JIT at its core; its NumPy support relies
182+
on running NumPy itself through a compatibility layer (`cpyext`), while a
183+
previous attempt to implement NumPy support directly was unsuccessful.
184+
185+
Other array libraries may have an internal JIT (e.g., TensorFlow, PyTorch,
186+
JAX, MXNet) or work with an external JIT like
187+
[XLA](https://www.tensorflow.org/xla) or [VTA](https://tvm.apache.org/docs/vta/index.html).
188+
189+
Numba currently has to jump through some hoops to accommodate NumPy's casting rules
190+
and may not attain full compatibility with NumPy in some cases - see, e.g.,
191+
[this](https://github.com/numba/numba/issues/4749) or
192+
[this](https://github.com/numba/numba/issues/5907) example issue regarding (array) scalar
193+
return values.
194+
195+
An [explicit suggestion from a Numba developer](https://twitter.com/esc___/status/1295389487485333505)
196+
for this array API standard was:
197+
198+
> for JIT compilers (e.g. Numba) it will be important, that the type of the
199+
returned value(s) depends only on the *types* of the input but not on the
200+
*values*.
201+
202+
A concrete goal for this use case is to have better matching between
203+
JIT-compiled and non-JIT execution. Here is an example from the Numba code
204+
base, the need for which should be avoided in the future:
205+
206+
```
207+
def check(x, y):
208+
got = cfunc(x, y)
209+
np.testing.assert_array_almost_equal(got, pyfunc(x, y))
210+
# Check the power operation conserved the input's dtype
211+
# (this is different from Numpy, whose behaviour depends on
212+
# the *values* of the arguments -- see PyArray_CanCastArrayTo).
213+
self.assertEqual(got.dtype, x.dtype)
214+
```

0 commit comments

Comments
 (0)