Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用rapids.ai在GPU中进行矩阵点积_Python_Nvidia_Rapids_Cudf - Fatal编程技术网

Python 如何使用rapids.ai在GPU中进行矩阵点积

Python 如何使用rapids.ai在GPU中进行矩阵点积,python,nvidia,rapids,cudf,Python,Nvidia,Rapids,Cudf,我用的是Nvidia的rapids ML套件的一部分 使用此套件,我将如何制作点产品 df = cudf.DataFrame([('a', list(range(20))), ('b', list(reversed(range(20)))), ('c', list(range(20)))]) e、 g.如何使用相同的cudf对象在上述数据帧上执行点积?cudf数据帧提供了一个apply_rows方法,该方法能够将方法编译到内核中并在GPU上执行。此功能已实现 普鲁士 [0.0, 18.0,

我用的是Nvidia的rapids ML套件的一部分

使用此套件,我将如何制作点产品

df = cudf.DataFrame([('a', list(range(20))),
('b', list(reversed(range(20)))),
('c', list(range(20)))])

e、 g.如何使用相同的cudf对象在上述数据帧上执行点积?

cudf数据帧提供了一个
apply_rows
方法,该方法能够将方法编译到内核中并在GPU上执行。此功能已实现

普鲁士

[0.0,
 18.0,
 68.0,
 144.0,
 240.0,
 350.0,
 468.0,
 588.0,
 704.0,
 810.0,
 900.0,
 968.0,
 1008.0,
 1014.0,
 980.0,
 900.0,
 768.0,
 578.0,
 324.0,
 0.0]
至于计算点积

import cudf
import numpy
import pandas

rows = 20000000

values_a = [float(x) for x in list(range(rows))]
values_b = [float(x) for x in list(reversed(range(rows)))]
values_c = [float(x) for x in list(range(rows))]

def create_cudf_dataframe():
    return cudf.DataFrame([
        ('a_in', values_a),
        ('b_in', values_b),
        ('c_in', values_c)
    ])

def create_pandas_dataframe():
    return pandas.DataFrame(
        data = {
            'a_in': values_a,
            'b_in': values_b,
            'c_in': values_c
        }
    )


def test_cudf(df = None):

    print('\ncomputing dot product using cudf')

    def kernel(a_in, b_in, c_in, dot):
         for i, (a, b, c) in enumerate(zip(a_in, b_in, c_in)):
             dot[i] = a * b * c

    if df is None:
        print(' - creating dataframe using cudf')
        df = create_cudf_dataframe()

    df = df.apply_rows(
        kernel,
        incols=['a_in', 'b_in', 'c_in'],
        outcols=dict(dot=numpy.float64),
        kwargs=dict(),
        cache_key='dot_product_3'
    )

    dp = df['dot'].sum()

    print(dp);


def test_pandas(df = None):
    print('\ncomputing dot product using pandas')
    if df is None:
        print(' - creating dataframe using pandas')
        df = create_pandas_dataframe()

    a = df['a_in']
    b = df['b_in']
    c = df['c_in']

    dp = a.mul(b).mul(c).sum()

    print(dp)

cudf_df = create_cudf_dataframe()
pandas_df = create_pandas_dataframe()

%time test_cudf()
%time test_cudf(cudf_df)
%time test_pandas()
%time test_pandas(pandas_df)
以及ubuntu@18.04在jupyter上运行的i7 6700-k具有32GB ram和GTX 1080 ti

computing dot product using cudf
 - creating dataframe using cudf
1.333333066666688e+28
CPU times: user 1.78 s, sys: 273 ms, total: 2.06 s
Wall time: 2.05 s

computing dot product using cudf
1.333333066666689e+28
CPU times: user 19.4 ms, sys: 24 ms, total: 43.4 ms
Wall time: 43.1 ms

computing dot product using pandas
 - creating dataframe using pandas
1.3333330666666836e+28
CPU times: user 7.81 s, sys: 781 ms, total: 8.59 s
Wall time: 8.57 s

computing dot product using pandas
1.3333330666666836e+28
CPU times: user 125 ms, sys: 120 ms, total: 245 ms
Wall time: 245 ms

我也一直在寻找这个问题的解决方案,我的
cuDF/cupy/numba
技能现在可以胜任这项任务了:

import cudf
import cupy as cp
x = cudf.DataFrame({'a': np.arange(10), 'b': np.arange(10), 'c': np.arange(10)})
X = cp.asarray(x.as_gpu_matrix())
y = cudf.DataFrame.from_gpu_matrix((X.T).dot(X))
y.columns = x.columns
print(y)
输出:


cudf
cupy
和后面的传输都是零拷贝

cudf还很年轻,尚未实现与大熊猫达到功能对等的目标。如果这在您的限制范围内,我将转而考虑执行此任务。@wbadart cudf已经提供了行内核执行功能。谢谢@cwharris,这是个好消息!
import cudf
import cupy as cp
x = cudf.DataFrame({'a': np.arange(10), 'b': np.arange(10), 'c': np.arange(10)})
X = cp.asarray(x.as_gpu_matrix())
y = cudf.DataFrame.from_gpu_matrix((X.T).dot(X))
y.columns = x.columns
print(y)
     a    b    c
0  285  285  285
1  285  285  285
2  285  285  285