Isn't there a way to dynamically select between numpy and cupy, depending on whether you want cpu or gpu code?
Just prepare the input on NumPy or CuPy, and then you can just feed it to NumPy APIs. NumPy functions will handle itself if the input is NumPy ndarray, or dispatch the execution to CuPy if the input is CuPy ndarray.
CuPy is an (almost) drop-in replacement for NumPy, so the following works surprisingly often:
if use_cpu:
import numpy as np
else:
import cupy as npThis is the problem with these kind of methods. It works, until it doesn't in an unknown way.