Debian Codegraph @

docs/source/how-to/selecting-the-collection-backend.rst

raw
1 Selecting the collection backend
2 ================================
3
4
5 **Warning**: Backend-library disptaching at the collection level is still an experimental feature. Both the ``DaskBackendEntrypoint`` API and the set of "dispatchable" functions are expected to change.
6
7
8 Changing the default backend library
9 ------------------------------------
10
11 The Dask-Dataframe and Dask-Array modules were originally designed with the Pandas and Numpy backend libraries in mind, respectively. However, other dataframe and array libraries can take advantage of the same collection APIs for out-of-core and parallel processing. For example, users with `cupy <https://cupy.dev/>`_ installed can change their default Dask-Array backend to ``cupy`` with the ``"array.backend"`` configuration option:
12
13 .. code-block:: python
14
15 >>> import dask
16 >>> import dask.array as da
17 >>> with dask.config.set({"array.backend": "cupy"}):
18 ... darr = da.ones(10, chunks=(5,)) # Get cupy-backed collection
19
20 This code opts out of the default (``"numpy"``) backend for dispatchable Dask-Array creation functions, and uses the creation functions registered for ``"cupy"`` instead. The current set of dispatchable creation functions for Dask-Array is:
21
22 - ``ones``
23 - ``zeros``
24 - ``empty``
25 - ``full``
26 - ``arange``
27
28 The Dask-Array API can also dispatch the backend ``RandomState`` class to be used for random-number generation. This means all creation functions in ``dask.array.random`` are also dispatchable.
29
30 The current set of dispatchable creation functions for Dask-Dataframe is:
31
32 - ``from_dict``
33 - ``read_parquet``
34 - ``read_json``
35 - ``read_orc``
36 - ``read_csv``
37 - ``read_hdf``
38
39 As the backend-library disptaching system becomes more mature, this set of dispatchable creation functions is likely to grow.
40
41 For an existing collection, the underlying data can be forcibly moved to a desired backend using the ``to_backend`` method:
42
43 .. code-block:: python
44
45 >>> import dask
46 >>> import dask.array as da
47 >>> darr = da.ones(10, chunks=(5,)) # Creates numpy-backed collection
48 >>> with dask.config.set({"array.backend": "cupy"}):
49 ... darr = darr.to_backend() # Moves numpy data to cupy
50
51
52 Defining a new collection backend
53 ---------------------------------
54
55 **Warning**: Defining a custom backend is **not** yet recommended for most users and down-stream libraries. The backend-entrypoint system should still be treated as experimental.
56
57
58 Dask currently exposes an `entrypoint <https://packaging.python.org/specifications/entry-points/>`_ under the group ``dask.array.backends`` and ``dask.dataframe.backends`` to enable users and third-party libraries to develop and maintain backend implementations for Dask-Array and Dask-Dataframe. A custom Dask-Array backend should define a subclass of ``DaskArrayBackendEntrypoint`` (defined in ``dask.array.backends``), while a custom Dask-DataFrame backend should define a subclass of ``DataFrameBackendEntrypoint`` (defined in ``dask.dataframe.backends``).
59
60 For example, a cudf-based backend definition for Dask-Dataframe would look something like the `CudfBackendEntrypoint` definition below:
61
62
63 .. code-block:: python
64
65 from dask.dataframe.backends import DataFrameBackendEntrypoint
66 from dask.dataframe.dispatch import (
67 ...
68 make_meta_dispatch,
69 ...
70 )
71 ...
72
73 def make_meta_cudf(x, index=None):
74 return x.head(0)
75 ...
76
77 class CudfBackendEntrypoint(DataFrameBackendEntrypoint):
78
79 def __init__(self):
80 # Register compute-based dispatch functions
81 # (e.g. make_meta_dispatch, sizeof_dispatch)
82 ...
83 make_meta_dispatch.register(
84 (cudf.Series, cudf.DataFrame),
85 func=make_meta_cudf,
86 )
87 # NOTE: Registration may also be outside __init__
88 # if it is in the same module as this class
89 ...
90
91 @staticmethod
92 def read_orc(*args, **kwargs):
93 from .io import read_orc
94
95 # Use dask_cudf version of read_orc
96 return read_orc(*args, **kwargs)
97 ...
98
99 In order to support pandas-to-cudf conversion with ``DataFrame.to_backend``, this class also needs to implement the proper ``to_backend`` and ``to_backend_dispatch`` methods.
100
101 To expose this class as a ``dask.dataframe.backends`` entrypoint, the necessary ``setup.cfg`` configuration in ``cudf`` (or ``dask_cudf``) would be as follows:
102
103 .. code-block:: ini
104
105 [options.entry_points]
106 dask.dataframe.backends =
107 cudf = <module-path>:CudfBackendEntrypoint
108
109
110 Compute dispatch
111 ~~~~~~~~~~~~~~~~
112
113
114 .. note::
115
116 The primary dispatching mechanism for array-like compute operations in both Dask-Array and Dask-DataFrame is the ``__array_function__`` protocol defined in `NEP-18 <https://numpy.org/neps/nep-0018-array-function-protocol.html>`_. For a custom collection backend to be functional, this protocol **must** cover many common numpy functions for the desired array backend. For example, the ``cudf`` backend for Dask-DataFrame depends on the ``__array_function__`` protocol being defined for both ``cudf`` and its complementary array backend (``cupy``). The compute-based dispatch functions discussed in this section correspond to functionality that is not already captured by NEP-18.
117
118
119 Notice that the ``CudfBackendEntrypoint`` definition must define a distinct method definition for each dispatchable creation routine, and register all non-creation (compute-based) dispatch functions within the ``__init__`` logic. These compute dispatch functions do not operate at the collection-API level, but at computation time (within a task). The list of all current "compute" dispatch functions are listed below.
120
121 Dask-Array compute-based dispatch functions (as defined in ``dask.array.dispatch``, and defined for Numpy in ``dask.array.backends``):
122
123 - concatenate_lookup
124 - divide_lookup
125 - einsum_lookup
126 - empty_lookup
127 - nannumel_lookup
128 - numel_lookup
129 - percentile_lookup
130 - tensordot_lookup
131 - take_lookup
132
133 Dask-Dataframe compute-based dispatch functions (as defined in ``dask.dataframe.dispatch``, and defined for Pandas in ``dask.dataframe.backends``):
134
135 - categorical_dtype_dispatch
136 - concat_dispatch
137 - get_collection_type
138 - group_split_dispatch
139 - grouper_dispatch
140 - hash_object_dispatch
141 - is_categorical_dtype_dispatch
142 - make_meta_dispatch
143 - make_meta_obj
144 - meta_lib_from_array
145 - meta_nonempty
146 - pyarrow_schema_dispatch
147 - tolist_dispatch
148 - union_categoricals_dispatch
149
150 Note that the compute-based dispatching system is subject to change. Implementing a complete backend is still expected to require significant effort. However, the long-term goal is to bring further simplicity to this process.