Hey all!
This is my last blog-post for GSoC’19. It’s kinda report of my whole GSoC journey.
Past 3 months were amazing! And it’s all thanks to the support of my mentors Imanishi-san and Sean Farley. It was my first opensource experience and in my experience CuPy is awesome. Not just the library, but as an organisation. I witnessed how patiently new contributions were welcomed, including mine :P
So, lets get started with “What is the Project?”.
What is the Project?
I got selected for Google Summer of Code’19 in CuPy, which is an organisation under NumFOCUS umbrella. CuPy is a NumPy-compatible matrix library accelerated by CUDA. That is, it can make use of NVIDIA GPUs to perform computations faster than NumPy.
Now, the problem with CuPy is that, it doesn’t have all NumPy methods implemented. So, porting a NumPy codebase to CuPy is not that easy. User will have to take care of such not implemented methods separately. This is where my Project CuPy automatic fallback to NumPy kicks in. This projects aims at implementing a fallback_mode
for CuPy. That is, whenever it encounters a method which is not yet implemented in CuPy, it will automatically call respective NumPy method and get the result while performing necessary data transfers under the hood.
How does it works?
There are two important things needed in order for fallback_mode
to work.
-
Catch method not yet implemented in CuPy and get respective NumPy method.
-
Perform necessary data transfer between CPU and GPU and execute NumPy method.
For the first part, I’ve implemented _RecursiveAttr
class which will work recursively storing NumPy and CuPy pair of objects till an attribute gets called. After an attribute gets called, it will call _call_cupy
or _call_numpy
depending on the values of _cupy_object
and _numpy_object
.
cupyx.fallback_mode.fallback.ndarray
, a wrapper around cupy.ndarray
is implemented to support methods of numpy.ndarray
i.e, supporting fallback of methods of the form ndarray.func(*args, **kwargs)
. And, cupyx.fallback_mode.fallback.vectorize
is implemented to support numpy.vectorize
class. Thereafter, necessary conversions, data transfer and method execution will be done in _call_cupy
or _call_numpy
.
So, how to use fallback_mode
?
Using fallback_mode
is pretty simple. It’s same as using CuPy or NumPy. You just need to make following changes in your codebase. Currently, there are some limitations which would be resolved soon.
# remove numpy import
- import numpy as np
# add fallback_mode import
+ from cupyx.fallback_mode import numpy as np
So, some example program would be like:
>>> from cupyx.fallback_mode import numpy as np
>>> np
<numpy = module numpy, cupy = module cupy>
>>> np.array
<numpy = <built-in function array>, cupy = <function array at 0x7fdc3c3f0c80>>
>>> a = np.array([1, 2, 3])
>>> a
array([1, 2, 3])
>>> type(a)
<class cupyx.fallback_mode.fallback.ndarray>
>>> # method available in CuPy
>>> a.min()
array(1)
>>> # method not available in CuPy
>>> b = np.asarray_chkfinite(a, dtype=np.float64)
>>> b
array([1., 2., 3.])
>>> x = np.random.rand(3, 4)
>>> vabs = np.vectorize(np.abs)
>>> res = vabs(x)
>>> res
array([[0.9767158 , 0.02067347, 0.4274213 , 0.90569813],
[0.98059118, 0.94584199, 0.35095123, 0.63525241],
[0.22457749, 0.62680865, 0.23070315, 0.77516518]])
>>> type(res)
<class cupyx.fallback_mode.fallback.ndarray>
>>>
Relevant Pull Requests..
Here is the issue for fallback_mode
: Issue #2066
PRs related to fallback_mode
:
-
Merged Add
fallback_mode
: PR #2229 -
Merged Add
fallback_mode.ndarray
: PR #2272 -
OpenMerged Add notification-support: PR #2279 -
Merged Add wrapper for
numpy.vectorize
: PR #2350 -
OpenMerged Addnumpy.ndarray
variants and in-place operations / views support: PR #2391
Other contributions:
What’s left?
-
Documentaion of
fallback_mode
is left. -
In-place operations / views are work in progress:PR #2391 -
Support ofPR #2391numpy.ndarray
variants is being added:
What did I learn?
I learnt so many things in past few months.
-
How an opensource organisation work?
-
Python testing using
unittest
andpytest
modules. -
My Python and git skills were tremendously improved.
-
Also, I learnt many things about Python, CuPy and NumPy internals.