Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(python): Add documentation for beta gpu support #18762

Merged
merged 8 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/_build/API_REFERENCE_LINKS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ python:
LazyFrame: https://docs.pola.rs/api/python/stable/reference/lazyframe/index.html
Series: https://docs.pola.rs/api/python/stable/reference/series/index.html
Categorical: https://docs.pola.rs/api/python/stable/reference/api/polars.Categorical.html
GPUEngine: https://docs.pola.rs/api/python/stable/lazyframe/api/polars.lazyframe.engine_config.GPUEngine.html
Config: https://docs.pola.rs/api/python/stable/reference/config.html
select: https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.select.html
filter: https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.filter.html
with_columns: https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.with_columns.html
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Polars is a blazingly fast DataFrame library for manipulating structured data. T
- **Out of Core**: The streaming API allows you to process your results without requiring all your data to be in memory at the same time
- **Parallel**: Utilises the power of your machine by dividing the workload among the available CPU cores without any additional configuration.
- **Vectorized Query Engine**: Using [Apache Arrow](https://arrow.apache.org/), a columnar data format, to process your queries in a vectorized manner and SIMD to optimize CPU usage.
- **GPU Support**: Optionally run queries on NVIDIA GPUs for maximum performance for in-memory workloads.

<!-- dprint-ignore-start -->

Expand Down
66 changes: 66 additions & 0 deletions docs/src/python/user-guide/lazy/gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# --8<-- [start:setup]
# --8<-- [start:simple]
import polars as pl

df = pl.LazyFrame({"a": [1.242, 1.535]})

q = df.select(pl.col("a").round(1))
# --8<-- [end:setup]

result = q.collect(engine="gpu")
print(result)
# --8<-- [end:simple]

# Avoiding requiring the GPU engine for doc build output
# --8<-- [start:simple-result]
result = q.collect()
print(result)
# --8<-- [end:simple-result]


# --8<-- [start:engine]
# --8<-- [start:engine-setup]
q = df.select((pl.col("a") ** 4))

# --8<-- [end:engine-setup]
result = q.collect(engine=pl.GPUEngine(device=1))
print(result)
# --8<-- [end:engine]

# --8<-- [start:engine-result]
result = q.collect()
print(result)
# --8<-- [end:engine-result]

# --8<-- [start:fallback-warning]
# --8<-- [start:fallback-setup]
df = pl.LazyFrame(
{
"key": [1, 1, 1, 2, 3, 3, 2, 2],
"value": [1, 2, 3, 4, 5, 6, 7, 8],
}
)

q = df.select(pl.col("value").sum().over("key"))

# --8<-- [end:fallback-setup]

with pl.Config() as cfg:
cfg.set_verbose(True)
result = q.collect(engine="gpu")

print(result)
# --8<-- [end:fallback-warning]
# --8<-- [start:fallback-result]
print(
"PerformanceWarning: Query execution with GPU not supported, reason: \n"
"<class 'NotImplementedError'>: Grouped rolling window not implemented"
)
print("# some details elided")
print()
print(q.collect())
# --8<- [end:fallback-result]

# --8<-- [start:fallback-raise]
q.collect(engine=pl.GPUEngine(raise_on_fail=True))
coastalwhite marked this conversation as resolved.
Show resolved Hide resolved
# --8<-- [end:fallback-raise]
133 changes: 133 additions & 0 deletions docs/user-guide/gpu-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# GPU Support [Open Beta]

Polars provides an in-memory, GPU-accelerated execution engine for Python users of the Lazy API on NVIDIA GPUs using [RAPIDS cuDF](https://docs.rapids.ai/api/cudf/stable/). This functionality is available in Open Beta and is undergoing rapid development.

### System Requirements

- NVIDIA Volta™ or higher GPU with [compute capability](https://developer.nvidia.com/cuda-gpus) 7.0+
- CUDA 11 or CUDA 12
- Linux or Windows Subsystem for Linux 2 (WSL2)

See the [RAPIDS installation guide](https://docs.rapids.ai/install#system-req) for full details.

### Installation

You can install the GPU backend for Polars with a feature flag as part of a normal [installation](installation.md).

=== ":fontawesome-brands-python: Python"
`bash pip install --extra-index-url=https://pypi.nvidia.com polars[gpu]`

!!! note Installation on a CUDA 11 system

If you have CUDA 11, the installation line is slightly more complicated: the relevant GPU package must be requested by hand.

=== ":fontawesome-brands-python: Python"
```bash
pip install --extra-index-url=https://pypi.nvidia.com polars cudf-polars-cu11
```

### Usage

Having built a query using the lazy API [as normal](lazy/index.md), GPU-enabled execution is requested by running `.collect(engine="gpu")` instead of `.collect()`.

{{code_block('user-guide/lazy/gpu', 'simple', ['collect'])}}

```python exec="on" result="text" session="user-guide/lazy"
--8<-- "python/user-guide/lazy/gpu.py:setup"
--8<-- "python/user-guide/lazy/gpu.py:simple-result"
```

For more detailed control over the execution, for example to specify which GPU to use on a multi-GPU node, we can provide a `GPUEngine` object. By default, the GPU engine will use a configuration applicable to most use cases.

{{code_block('user-guide/lazy/gpu', 'engine', ['GPUEngine'])}}

```python exec="on" result="text" session="user-guide/lazy"
--8<-- "python/user-guide/lazy/gpu.py:engine-setup"
--8<-- "python/user-guide/lazy/gpu.py:engine-result"
```

### How It Works

When you use the GPU-accelerated engine, Polars creates and optimizes a query plan and dispatches to a [RAPIDS](https://rapids.ai/) cuDF-based physical execution engine to compute the results on NVIDIA GPUs. The final result is returned as a normal CPU-backed Polars dataframe.

### What's Supported on the GPU?

GPU support is currently in Open Beta and the engine is undergoing rapid development. The engine currently supports many, but not all, of the core expressions and data types.

Since expressions are composable, it's not feasible to list a full matrix of expressions supported on the GPU. Instead, we provide a list of the high-level categories of expressions and interfaces that are currently supported and not supported.

#### Supported

- LazyFrame API
- SQL API
- I/O from CSV, Parquet, ndjson, and in-memory CPU DataFrames.
- Operations on numeric, logical, string, and datetime types
- String processing
- Aggregations and grouped aggregations
- Joins
- Filters
- Missing data
- Concatenation

#### Not Supported

- Eager DataFrame API
- Streaming API
- Operations on categorical, struct, and list data types
- Rolling aggregations
- Time series resampling
- Timezones
- Folds
- User-defined functions
- JSON, Excel, and Database file formats

#### Did my query use the GPU?

The release of the GPU engine in Open Beta implies that we expect things to work well, but there are still some rough edges we're working on. In particular the full breadth of the Polars expression API is not yet supported. With fallback to the CPU, your query _should_ complete, but you might not observe any change in the time it takes to execute. There are two ways to get more information on whether the query ran on the GPU.

When running in verbose mode, any queries that cannot execute on the GPU will issue a `PerformanceWarning`:

{{code_block('user-guide/lazy/gpu', 'fallback-warning', ['Config'])}}

```python exec="on" result="text" session="user-guide/lazy"
--8<-- "python/user-guide/lazy/gpu.py:fallback-setup"
--8<-- "python/user-guide/lazy/gpu.py:fallback-result"
```

To disable fallback, and have the GPU engine raise an exception if a query is unsupported, we can pass an appropriately configured `GPUEngine` object:

{{code_block('user-guide/lazy/gpu', 'fallback-raise', ['GPUEngine'])}}

```pytb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/coder/third-party/polars/py-polars/polars/lazyframe/frame.py", line 2035, in collect
return wrap_df(ldf.collect(callback))
polars.exceptions.ComputeError: 'cuda' conversion failed: NotImplementedError: Grouped rolling window not implemented
```

Currently, only the proximal cause of failure to execute on the GPU is reported, we plan to extend this functionality to report all unsupported operations for a query.

### Testing

The Polars and NVIDIA RAPIDS teams run comprehensive unit and integration tests to ensure that the GPU-accelerated Polars backend works smoothly.

The **full** Polars test suite is run on every commit made to the GPU engine, ensuring consistency of results.

The GPU engine currently passes 99.2% of the Polars unit tests with CPU fallback enabled. Without CPU fallback, the GPU engine passes 88.8% of the Polars unit tests. With fallback, there are approximately 100 failing tests: around 40 of these fail due to mismatching debug output; there are some cases where the GPU engine produces the a correct result but uses a different data type; the remainder are cases where we do not correctly determine that a query is unsupported and therefore fail at runtime, instead of falling back.

### When Should I Use a GPU?

Based on our benchmarking, you're most likely to observe speedups using the GPU engine when your workflow's profile is dominated by grouped aggregations and joins. In contrast I/O bound queries typically show similar performance on GPU and CPU. GPUs typically have less RAM than CPU systems, therefore very large datasets will fail due to out of memory errors. Based on our testing, raw datasets of 50-100 GiB fit (depending on the workflow) well with a GPU with 80GiB of memory.

### CPU-GPU Interoperability

Both the CPU and GPU engine use the Apache Arrow columnar memory specification, making it possible to quickly move data between the CPU and GPU. Additionally, files written by one engine can be read by the other engine.

When using GPU mode, your workflow won't fail if something isn't supported. When you run `collect(engine="gpu")`, the optimized query plan is inspected to see whether it can be executed on the GPU. If it can't, it will transparently fall back to the standard Polars engine and run on the CPU.

GPU execution is only available in the Lazy API, so materialized DataFrames will reside in CPU memory when the query execution finishes.

### Providing feedback

Please report issues, and missing features, on the Polars [issue tracker](../development/contributing/index.md).
13 changes: 13 additions & 0 deletions docs/user-guide/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ pip install 'polars[numpy,fsspec]'
| --- | --------------------------------- |
| all | Install all optional dependencies |

#### GPU

| Tag | Description |
| --- | -------------------------- |
| gpu | Run queries on NVIDIA GPUs |

!!! note

To install the GPU engine, you need to pass
`--extra-index-url=https://pypi.nvidia.com` to `pip`. See [GPU
support](gpu-support.md) for more detailed instructions and
prerequisites.

#### Interop

| Tag | Description |
Expand Down
14 changes: 14 additions & 0 deletions docs/user-guide/lazy/gpu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# GPU Support

Polars provides an in-memory, GPU-accelerated execution engine for the Lazy API in Python using [RAPIDS cuDF](https://docs.rapids.ai/api/cudf/stable/) on NVIDIA GPUs. This functionality is available in Open Beta and is undergoing rapid development.

If you install Polars with the [GPU feature flag](../installation.md), you can trigger GPU-based execution by running `.collect(engine="gpu")` instead of `.collect()`.

{{code_block('user-guide/lazy/gpu', 'simple', [])}}

```python exec="on" result="text" session="user-guide/lazy"
--8<-- "python/user-guide/lazy/gpu.py:setup"
--8<-- "python/user-guide/lazy/gpu.py:simple-result"
```

Learn more in the [GPU Support guide](../gpu-support.md).
1 change: 1 addition & 0 deletions docs/user-guide/lazy/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ The Lazy chapter is a guide for working with `LazyFrames`. It covers the functio
- [Query plan](query-plan.md)
- [Execution](execution.md)
- [Streaming](streaming.md)
- [GPU Support](gpu.md)
2 changes: 2 additions & 0 deletions docs/user-guide/misc/multiprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ It does this by executing computations which can be done in parallel in separate
For example, requesting two expressions in a `select` statement can be done in parallel, with the results only being combined at the end.
Another example is aggregating a value within groups using `group_by().agg(<expr>)`, each group can be evaluated separately.
It is very unlikely that the `multiprocessing` module can improve your code performance in these cases.
If you're using the GPU Engine with Polars you should also avoid manual multiprocessing. When used simultaneously, they can compete
for system memory and processing power, leading to reduced performance.

See [the optimizations section](../lazy/optimizations.md) for more optimizations.

Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ nav:
- user-guide/lazy/query-plan.md
- user-guide/lazy/execution.md
- user-guide/lazy/streaming.md
- user-guide/lazy/gpu.md
- IO:
- user-guide/io/index.md
- user-guide/io/csv.md
Expand Down Expand Up @@ -86,6 +87,7 @@ nav:
- user-guide/misc/styling.md
- user-guide/misc/comparison.md
- user-guide/misc/arrow.md
- user-guide/gpu-support.md

- API reference: api/index.md

Expand Down
14 changes: 14 additions & 0 deletions py-polars/docs/source/reference/lazyframe/gpu_engine.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=========
GPUEngine
=========

This object provides fine-grained control over the behavior of the
GPU engine when calling `LazyFrame.collect()` with an `engine`
argument.

.. currentmodule:: polars.lazyframe.engine_config

.. autosummary::
:toctree: api/

GPUEngine
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/lazyframe/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This page gives an overview of all public LazyFrame methods.
modify_select
miscellaneous
in_process
gpu_engine

.. _lazyframe:

Expand Down
29 changes: 25 additions & 4 deletions py-polars/polars/lazyframe/engine_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,35 @@ class GPUEngine:

Use this if you want control over details of the execution.

Supported options
Parameters
----------
device : int, default None
Select the GPU used to run the query. If not provided, the
query uses the current CUDA device.
memory_resource : rmm.mr.DeviceMemoryResource, default None
Provide a memory resource for GPU memory allocations.

.. warning::
If passing a `memory_resource`, you must ensure that it is valid
for the selected `device`. See the `RMM documentation
<https://github.com/rapidsai/rmm?tab=readme-ov-file#multiple-devices>`_
for more details.

raise_on_fail : bool, default False
If True, do not fall back to the Polars CPU engine if the GPU
engine cannot execute the query, but instead raise an error.

- `device`: Select the device to run the query on.
- `memory_resource`: Set an RMM memory resource for
device-side allocations.
"""

device: int | None
"""Device on which to run query."""
memory_resource: DeviceMemoryResource | None
"""Memory resource to use for device allocations."""
raise_on_fail: bool
"""
Whether unsupported queries should raise an error, rather than falling
back to the CPU engine.
"""
config: Mapping[str, Any]
"""Additional configuration options for the engine."""

Expand All @@ -33,8 +51,11 @@ def __init__(
*,
device: int | None = None,
memory_resource: Any | None = None,
raise_on_fail: bool = False,
**kwargs: Any,
) -> None:
self.device = device
self.memory_resource = memory_resource
# Avoids need for changes in cudf-polars
kwargs["raise_on_fail"] = raise_on_fail
self.config = kwargs
9 changes: 5 additions & 4 deletions py-polars/polars/lazyframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,7 +1865,7 @@ def collect(
polars CPU engine. If set to `"gpu"`, the GPU engine is
used. Fine-grained control over the GPU engine, for
example which device to use on a system with multiple
devices, is possible by providing a :class:`GPUEngine` object
devices, is possible by providing a :class:`~.GPUEngine` object
with configuration options.

.. note::
Expand Down Expand Up @@ -2019,9 +2019,10 @@ def collect(
"cudf_polars",
err_prefix="GPU engine requested, but required package",
install_message=(
"Please install using the command `pip install cudf-polars-cu12` "
"(or `pip install cudf-polars-cu11` if your system has a "
"CUDA 11 driver)."
"Please install using the command "
"`pip install --extra-index-url=https://pypi.nvidia.com cudf-polars-cu12` "
"(or `pip install --extra-index-url=https://pypi.nvidia.com cudf-polars-cu11` "
"if your system has a CUDA 11 driver)."
),
)
if not is_config_obj:
Expand Down
Loading