| 1 |
GPUs |
| 2 |
==== |
| 3 |
|
| 4 |
Dask works with GPUs in a few ways. |
| 5 |
|
| 6 |
|
| 7 |
Custom Computations |
| 8 |
------------------- |
| 9 |
|
| 10 |
Many people use Dask alongside GPU-accelerated libraries like PyTorch and |
| 11 |
TensorFlow to manage workloads across several machines. They typically use |
| 12 |
Dask's custom APIs, notably :doc:`Delayed <delayed>` and :doc:`Futures |
| 13 |
<futures>`. |
| 14 |
|
| 15 |
Dask doesn't need to know that these functions use GPUs. It just runs Python |
| 16 |
functions. Whether or not those Python functions use a GPU is orthogonal to |
| 17 |
Dask. It will work regardless. |
| 18 |
|
| •19 |
As a worked example, you may want to view :bdg-link-primary:`Batch Scoring for Computer Vision Workloads (video) <https://developer.download.nvidia.com/video/gputechconf/gtc/2019/video/S9198/s9198-dask-and-v100s-for-fast-distributed-batch-scoring-of-computer-vision-workloads.mp4>` |
| 20 |
|
| 21 |
High Level Collections |
| 22 |
---------------------- |
| 23 |
|
| 24 |
Dask can also help to scale out large array and dataframe computations by |
| 25 |
combining the Dask Array and DataFrame collections with a GPU-accelerated |
| 26 |
array or dataframe library. |
| 27 |
|
| 28 |
Recall that :doc:`Dask Array <array>` creates a large array out of many NumPy |
| 29 |
arrays and :doc:`Dask DataFrame <dataframe>` creates a large dataframe out of |
| 30 |
many Pandas dataframes. We can use these same systems with GPUs if we swap out |
| 31 |
the NumPy/Pandas components with GPU-accelerated versions of those same |
| 32 |
libraries, as long as the GPU accelerated version looks enough like |
| 33 |
NumPy/Pandas in order to interoperate with Dask. |
| 34 |
|
| 35 |
Fortunately, libraries that mimic NumPy, Pandas, and Scikit-Learn on the GPU do |
| 36 |
exist. |
| 37 |
|
| 38 |
|
| 39 |
DataFrames |
| 40 |
~~~~~~~~~~ |
| 41 |
|
| 42 |
The `RAPIDS <https://rapids.ai>`_ libraries provide a GPU accelerated |
| 43 |
Pandas-like library, |
| 44 |
`cuDF <https://github.com/rapidsai/cudf>`_, |
| 45 |
which interoperates well and is tested against Dask DataFrame. |
| 46 |
|
| 47 |
If you have cuDF installed then you should be able to convert a Pandas-backed |
| 48 |
Dask DataFrame to a cuDF-backed Dask DataFrame as follows: |
| 49 |
|
| 50 |
.. code-block:: python |
| 51 |
|
| 52 |
import cudf |
| 53 |
|
| 54 |
df = df.map_partitions(cudf.from_pandas) # convert pandas partitions into cudf partitions |
| 55 |
|
| 56 |
However, cuDF does not support the entire Pandas interface, and so a variety of |
| 57 |
Dask DataFrame operations will not function properly. Check the |
| 58 |
`cuDF API Reference <https://docs.rapids.ai/api/cudf/stable/>`_ |
| 59 |
for currently supported interface. |
| 60 |
|
| 61 |
|
| 62 |
Arrays |
| 63 |
~~~~~~ |
| 64 |
|
| 65 |
.. note:: Dask's integration with CuPy relies on features recently added to |
| 66 |
NumPy and CuPy, particularly in version ``numpy>=1.17`` and ``cupy>=6`` |
| 67 |
|
| 68 |
`Chainer's CuPy <https://cupy.chainer.org/>`_ library provides a GPU |
| 69 |
accelerated NumPy-like library that interoperates nicely with Dask Array. |
| 70 |
|
| 71 |
If you have CuPy installed then you should be able to convert a NumPy-backed |
| 72 |
Dask Array into a CuPy backed Dask Array as follows: |
| 73 |
|
| 74 |
.. code-block:: python |
| 75 |
|
| 76 |
import cupy |
| 77 |
|
| 78 |
x = x.map_blocks(cupy.asarray) |
| 79 |
|
| 80 |
CuPy is fairly mature and adheres closely to the NumPy API. However, small |
| 81 |
differences do exist and these can cause Dask Array operations to function |
| 82 |
improperly. Check the |
| 83 |
`CuPy Reference Manual <https://docs-cupy.chainer.org/en/stable/reference/index.html>`_ |
| 84 |
for API compatibility. |
| 85 |
|
| 86 |
|
| 87 |
Scikit-Learn |
| 88 |
~~~~~~~~~~~~ |
| 89 |
|
| 90 |
There are a variety of GPU accelerated machine learning libraries that follow |
| 91 |
the Scikit-Learn Estimator API of fit, transform, and predict. These can |
| 92 |
generally be used within `Dask-ML's <https://ml.dask.org>`_ meta estimators, |
| 93 |
such as `hyper parameter optimization <https://ml.dask.org/hyper-parameter-search.html>`_. |
| 94 |
|
| 95 |
Some of these include: |
| 96 |
|
| 97 |
- `Skorch <https://skorch.readthedocs.io/>`_ |
| 98 |
- `cuML <https://rapidsai.github.io/projects/cuml/en/latest/>`_ |
| 99 |
- `LightGBM <https://github.com/Microsoft/LightGBM>`_ |
| 100 |
- `XGBoost <https://xgboost.readthedocs.io/en/latest/>`_ |
| 101 |
- `Thunder SVM <https://github.com/Xtra-Computing/thundersvm>`_ |
| 102 |
- `Thunder GBM <https://github.com/Xtra-Computing/thundergbm>`_ |
| 103 |
|
| 104 |
|
| 105 |
Setup |
| 106 |
----- |
| 107 |
|
| 108 |
From the examples above we can see that the user experience of using Dask with |
| 109 |
GPU-backed libraries isn't very different from using it with CPU-backed |
| 110 |
libraries. However, there are some changes you might consider making when |
| 111 |
setting up your cluster. |
| 112 |
|
| 113 |
Restricting Work |
| 114 |
~~~~~~~~~~~~~~~~ |
| 115 |
|
| 116 |
By default Dask allows as many tasks as you have CPU cores to run concurrently. |
| 117 |
However if your tasks primarily use a GPU then you probably want far fewer |
| 118 |
tasks running at once. There are a few ways to limit parallelism here: |
| 119 |
|
| 120 |
- Limit the number of threads explicitly on your workers using the |
| 121 |
``--nthreads`` keyword in the CLI or the ``ncores=`` keyword the |
| 122 |
Cluster constructor. |
| 123 |
- Use `worker resources <https://distributed.dask.org/en/latest/resources.html>`_ and tag certain |
| 124 |
tasks as GPU tasks so that the scheduler will limit them, while leaving the |
| 125 |
rest of your CPU cores for other work |
| 126 |
|
| 127 |
Specifying GPUs per Machine |
| 128 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 129 |
|
| 130 |
Some configurations may have many GPU devices per node. Dask is often used to |
| 131 |
balance and coordinate work between these devices. |
| 132 |
|
| 133 |
In these situations it is common to start one Dask worker per device, and use |
| 134 |
the CUDA environment variable ``CUDA_VISIBLE_DEVICES`` to pin each worker to |
| 135 |
prefer one device. |
| 136 |
|
| 137 |
.. code-block:: bash |
| 138 |
|
| 139 |
# If we have four GPUs on one machine |
| 140 |
CUDA_VISIBLE_DEVICES=0 dask-worker ... |
| 141 |
CUDA_VISIBLE_DEVICES=1 dask-worker ... |
| 142 |
CUDA_VISIBLE_DEVICES=2 dask-worker ... |
| 143 |
CUDA_VISIBLE_DEVICES=3 dask-worker ... |
| 144 |
|
| 145 |
The `Dask CUDA <https://github.com/rapidsai/dask-cuda>`_ project contains some |
| 146 |
convenience CLI and Python utilities to automate this process. |
| 147 |
|
| 148 |
Work in Progress |
| 149 |
---------------- |
| 150 |
|
| 151 |
GPU computing is a quickly moving field today and as a result the information |
| 152 |
in this page is likely to go out of date quickly. We encourage interested |
| 153 |
readers to check out `Dask's Blog <https://blog.dask.org>`_ which has more |
| 154 |
timely updates on ongoing work. |