简体   繁体   中英

How to make numpy functions workable for cupy?

Good evening everyone. I have some following code snippets as follows.

I'm trying to convert the numpy functions to cupy, as it takes a long time to process through numpy.

But whenever I run this code, the following problem occurs.

/usr/local/lib/python3.8/site-packages/cupy/_core/_kernel.pyx in cupy._core._kernel._preprocess_arg() 
TypeError: Unsupported type <class 'numpy.ndarray'>

def ps_propagate(data, d, L, beam_c, out_schema = None):
 

    #handle a list of reconstruction planes
    if isinstance(d, cp.asarray(list)) or isinstance(d, np.ndarray):

        # save time by getting portion of recontruction that doesn't change
        # when z changes
        old_Ip, npix_plane = ps_propagate_plane(
            data, d[0], L, beam_c, out_schema, old_Ip=True)

        # Loop through each value of d.
        # This saves memory because only the cropped output image is stored.
        result = [
            ps_propagate_plane(data, z, L ,beam_c, out_schema, old_Ip = old_Ip)
            for z in d]
        result = concat(result, dim='z')

    else:  # if only reconstructing at one plane
        result = ps_propagate_plane(
            data, d, L ,beam_c, out_schema, old_Ip=False)

    return result

I was assuming maybe the numpy hasn't been translated as cupy correctly, so changed it to

cp.asarray(np.ndarray) or changed the 'list' parameter as cp.asarray(list) but didn't solve the issue.

Any help and advice will be super helpful!!!

If you have a numpy.ndarray it is stored in main memory and can not be sent to the GPU. Therefore you need to explictly convert it into a cupy.ndarray, which can be sent to the GPU.

在此处输入图像描述

It is difficult from

ps_propagate(data, d, L, beam_c, out_schema = None):

alone to guess, which arguments are represented as numpy.ndarray and cupy.array , but try adding conversions as the first thing in the body.

If that strategy fails, try to narrow down the exact line that fails. Then you will know which data is in play.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM