Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a matrix dot product in the GPU with rapids.ai

Tags:

I'm using CUDF it's part of the rapids ML suite from Nvidia.

Using this suite how would I do a dot product?

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

e.g. how would I perform a dot product on the above Dataframe using the same cudf object?

like image 785
Pablojim Avatar asked Feb 01 '19 13:02

Pablojim


2 Answers

cuDF Dataframe provides an apply_rows method, which is capable of compiling a method in to a kernel and executing on the GPU. This functionality was implemented January of last year.

import cudf
import numpy

rows = 20000000

df = cudf.DataFrame([
    ('a_in', list(range(rows))),
    ('b_in', list(reversed(range(rows)))),
    ('c_in', list(range(rows)))
])

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

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

[x for x in df['dot']]

pruduces

[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]

As for computing the dot product...

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)

And performance results on [email protected] in jupyter running on an i7 6700-k with 32GB ram and a 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
like image 125
cwharris Avatar answered Oct 06 '22 00:10

cwharris


I've been looking for a solution to this question for a while as well, and my cuDF/cupy/numba skills are now up to the task:

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)

Out:

     a    b    c
0  285  285  285
1  285  285  285
2  285  285  285

Transfers from cudf to cupy and back are all zero-copy!

like image 40
Thomson Comer Avatar answered Oct 06 '22 01:10

Thomson Comer