• <xmp id="om0om">
  • <table id="om0om"><noscript id="om0om"></noscript></table>
  • Data Science

    ??? ????? ?? GPU ?? ????

    ??? ????? ?? GPU ?? ????
    Reading Time: 5 minutes

    ??? ???? ???? ?? ???? ?? ? ????, ????? ?? ??? ??? ???? ? ?? ???? ?????. GPU ???? ?? ?????? ???? ??? ????, ??? ?? ??? ???? ????.

    RAPIDS? ???? ?? ???? GPU? ???? ??? ???? ?????? ?????? ??? NVIDIA?? ??? ?? ?? ??????? ????? ??????. ??? ??? ?? cuDF, ?? ??? ?? cuML, ??? ??? ?? cuGraph? ?? ??? ???? RAPIDS? ?? Python ?????? ???? ??? ? ?? ??? ??????? ? ??? ????? ??? ? ??? ??????.

    ? ?????? ?? ??? ??? ???????? ?? CPU ??? ???? ??????? GPU ?? ?????? ???? ?? ?? ????? ???.

    ???? ?? ???? ????? RAPIDS ????

    RAPIDS? ???? ??? ????? ? ?? ????? ????. ?? ??? ?? ??? ???? ???? ?? RAPIDS ?? ???? ??? ?? ????, pip ??, Docker ??? ?? Conda? ?? ??? ?? ?????? ??? ? ?? ?? ??? ????. ?? ???? ???? RAPIDS? ????? RAPIDS ???? ?? ???? ????? ????. ???? ?? ?? ????? ?? ?? CUDA ??? ???? RAPIDS ??? ???? ???? ????? ???.

    pandas? ?? cuDF ? GPU ??

    RAPIDS? ??? ???? GPU ?? ?????? ?? ??? ?? ?????? ??? ? ?? ??? ????? ????. ? ? cuDF? ?? pandas ?? ??????? GPU? ???? ??? ??? ???? ??? ? ?? ??? ???, ?? ??? ?? ??? ????.

    ????? ??? ???? ? ??? ??? GPU?? ???? ?? pandas? ???? ?? cuDF ??? ????? ????, load_ext cudf.pandas? RAPIDS ?? ????? ????, ??? ??? ??? ????? ?? ????? ?? cuDF DataFrame? ??? ???? pandas? ??? ??? ??? ??? ? ????.

    ?? pandas? ?????, cuDF pandas? .csv, .json, .pickle, .paraquet ? ??? ?? ??? ????? GPU ?? ??? ??? ???? ????.

    ??? ??? cudf.pandas ???? ????? ? ?? .csv ??? ???? ??? ?????:

    %load_ext cudf.pandas
    import pandas as pd
    import cupy as cp
       
    train = pd.read_csv('./Titanic/train.csv')
    test = pd.read_csv('./Titanic/test.csv')
    concat = pd.concat([train, test], axis = 0)

    cudf.pandas ???? ???? ?? ???? ??? ??? ???, ???, ??? ?? ??? pandas ??? GPU?? ??? ? ????. cuDF ???? pandas API? ???? CPU?? GPU? ???? ???? ??? ??? ?? ?? ??? ??????.

    target_rows = 1_000_000
    repeats = -(-target_rows // len(train))  # Ceiling division
    train_df = pd.concat([train] * repeats, ignore_index=True).head(target_rows)
    print(train_df.shape)  # (1000000, 2)
      
    repeats = -(-target_rows // len(test))  # Ceiling division
    test_df = pd.concat([test] * repeats, ignore_index=True).head(target_rows)
    print(test_df.shape)  # (1000000, 2)
      
    combine = [train_df, test_df]
      
      
    (1000000, 12)
    (1000000, 11)
    filtered_df = train_df[(train_df['Age'] > 30) & (train_df['Fare'] > 50)]
    grouped_df = train_df.groupby('Embarked')[['Fare', 'Age']].mean()
    additional_info = pd.DataFrame({
        'PassengerId': [1, 2, 3],
        'VIP_Status': ['No', 'Yes', 'No']
      })
    merged_df = train_df.merge(additional_info, on='PassengerId',
    how='left')

    ??? ??: CPU ? GPU ??? ??? ?? ??

    ??? ???? ???? ?? ???? ?? ?? ?? ??? ??? ???? ??? ????? ???? ??? ?????. ???? ??? CPU? GPU ????? ???? ??? ????, ????? ??? ????, ?????? ???? ????? ?? ??? ???? ?? ??? ????.

    cudf.pandas.profile? ?? ?? ????? ??? ?? ??? ?? ??? ??? ???? ???? ??? ???. ?? ?? ??? ? ??? ????? CPU?? ??? ??? GPU?? ???? ??? ?????:

    %%cudf.pandas.profile
    train_df[['Pclass', 'Survived']].groupby(['Pclass'],
    as_index=False).mean().sort_values(by='Survived', ascending=False)
            Pclass    Survived
    0         1        0.629592
    1         2        0.472810
    2         3        0.242378
      
                             Total time elapsed: 5.131 seconds
                             5 GPU function calls in 5.020 seconds
                             0 CPU function calls in 0.000 seconds
      
                                           Stats
      
    +------------------------+------------+-------------+------------+------------+-------------+------------+
    | Function           | GPU ncalls  | GPU cumtime | GPU percall | CPU ncalls | CPU cumtime | CPU percall |
    +------------------------+------------+-------------+------------+------------+-------------+------------+
    | DataFrame.__getitem__ | 1          | 5.000       | 5.000      | 0          | 0.000       | 0.000      |
    | DataFrame.groupby     | 1          | 0.000       | 0.000      | 0          | 0.000       | 0.000      |
    | GroupBy.mean          | 1          | 0.007       | 0.007      | 0          | 0.000       | 0.000      |
    | DataFrame.sort_values | 1          | 0.002       | 0.002      | 0          | 0.000       | 0.000      |
    | DataFrame.__repr__    | 1          | 0.011       | 0.011      | 0          | 0.000       | 0.000      |
    +------------------------+------------+-------------+------------+------------+-------------+------------+

    ??? ???? ???? ?? cuDF ??, ???? ?? ??? ?? ?? ??? ?? ??? ??? ?? ??? CPU ???? ????? ??? ??? ???? ? ??? ???. ??? ??? ?? ??? ? ??? ?? ? ???? ??? ??? ???? ?? ?????. ? ??(Loader)? ?? ??? ????? GPU ??? ?? cudf.pandas ????? ?????? ?????.

    ?? %%time ? %%timeit? ?? Python ?? ???(Magic commands)? ???? ?? ?? ??? ????? ????? pandas (CPU)? pandas? cuDF ???(GPU) ?? ???? ?? ??? ? ????. ??? ??? GPU ??? ?? ?? ? ?? ??? ??? ?? ????? ?????. ?? %%time? ??? ????? CPU? GPU ?? ?? ?? ??? ???? ???? ?? ??? ?? ??? ? ?? ??? ??? ?????.

    %%time
       
    print("Before", train_df.shape, test_df.shape, combine[0].shape, combine[1].shape)
       
    train_df = train_df.drop(['Ticket', 'Cabin'], axis=1)
    test_df = test_df.drop(['Ticket', 'Cabin'], axis=1)
    combine = [train_df, test_df]
       
    print("After", train_df.shape, test_df.shape, combine[0].shape, combine[1].shape)
    CPU output:
    Before (999702, 12) (999856, 11) (999702, 12) (999856, 11)
    After  (999702, 10) (999856, 9)  (999702, 10) (999856, 9)
      
    CPU times: user 56.6 ms, sys: 8.08 ms, total: 64.7 ms
      
    Wall time: 63.3 ms
    GPU output:
    Before (999702, 12) (999856, 11) (999702, 12) (999856, 11)
    After  (999702, 10) (999856, 9)  (999702, 10) (999856, 9)
      
    CPU times: user 6.65 ms, sys: 0 ns, total: 6.65 ms
      
    Wall time: 5.46 ms

    %%time ??? ?? ??? 10? ??? CPU?? 63.3???(ms)?? ? ??? GPU?? 5.46ms? ???????. ?? ??? ??? ???? cuDF pandas? ?? GPU ??? ???? ??????, ?? ???? ???? ???? ???? ?? ?? ??? ???? %%timeit? ???? ? ?? ????? ?? ? ????.

    %%timeit 
       
    for dataset in combine:
        dataset['Title'] = dataset.Name.str.extract(' ([A-Za-z]+)\\.', expand=False)
       
    pd.crosstab(train_df['Title'], train_df['Sex'])
    CPU output:
    1.11 s ± 7.49 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    GPU output:
    89.6 ms ± 959 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

    %%timeit ??? GPU ??? ?? 10?? ?? ??? ?? ??, CPU?? ??? 1.11??? ?? ??? GPU?? ??? 89.6ms? ???????, ?? ???? ??? ??? ?? cuDF pandas? ???? ?????.

    GPU ??? ??

    ?? ?? ??? ???? ??? ?? ???? GPU? ????? ???? ??? ???? ?? ?????. ??? ?? ???(Type command)? ???? ??? CPU?? ???? ??? GPU?? ???? ??? ???? NumPy ??? CuPy ??? ??? ? ????.

    type(guess_ages)
    <module 'pandas' (ModuleAccelerator(fast=cudf, slow=pandas))>

    ??? np.array?? ???? CPU?? ???? ?? ????. ??? cupy.ndarray?? ???? GPU?? ???? ?? ????. ? ?? ??? ?? ?????? ??? ??? GPU ???? ???? ??? ??? ? ????.

    ??, print ??? ???? ??? GPU? ???? ??? ???? cuDF ??????? ???? ??? ??? ? ????. ???? ?? ??(cuDF) ?? ?? ??(pandas)? ?? ??? ??? ???? ????. ? ??? ??? ?? GPU? ??? ?? ???? ?? ????? ??? ?? ??? ? ????.

    print(pd)
    <module 'pandas' (ModuleAccelerator(fast=cudf, slow=pandas))>

    ?????, df.info? ?? ??? ???? cuDF ??????? ??? ???? ??? GPU ???? ?????? ??? ? ????. ?? ?? ??? GPU?? ????? ??? CPU? ?????? ??? ? ????.

    train_df.info()
    <class 'cudf.core.dataframe.DataFrame'>
      
    RangeIndex: 1000000 entries, 0 to 999999
    Data columns (total 9 columns):
     #   Column   Non-Null Count   Dtype  
    ---  ------   --------------   ----- 
     0   Survived 1000000 non-null  int64  
     1   Pclass   1000000 non-null  int64  
     2   Sex      1000000 non-null  int64  
     3   Age      1000000 non-null  float64
     4   SibSp    1000000 non-null  int64  
     5   Parch    1000000 non-null  int64  
     6   Fare     1000000 non-null  float64
     7   Embarked 997755 non-null   object
     8   Title    1000000 non-null  int64  
    dtypes: float64(2), int64(6), object(1)
    memory usage: 65.9+ MB

    ??

    RAPIDS? cuDF pandas? ?? ??? ?? ?? CPU ?? ??? ??????? GPU ?? ??? ???? ???? ??? ?? ??? ??????, %%time, %%timeit, %%cudf.pandas.profile? ?? ????? ??? ?? ??? ???? ??? ???? ???? ???? ? ????. ?? type, print(pd), df.info? ?? ??? ??? ?? GPU ???? ??? ? ?? ?????? GPU ???? ????? ???? ??? ??? ? ????.

    ???? ??? ??? ??? ??? ?? ???? ?? ???? Jupyter ???? ?????.

    GPU ?? ??? ???? ?? ??? ????? ??? ????? ???? 10?: RAPIDS cuDF? CuPy ????? ?? ??? Google Colab?? ??? ?? 50??? ????? RAPIDS cuDF ???? ?????.

    ?? ???

    Discuss (0)
    0

    Tags

    人人超碰97caoporen国产