-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python): Enable collection with gpu engine (#17550)
- Loading branch information
Showing
7 changed files
with
199 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from polars.lazyframe.engine_config import GPUEngine | ||
from polars.lazyframe.frame import LazyFrame | ||
|
||
__all__ = [ | ||
"GPUEngine", | ||
"LazyFrame", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Mapping | ||
|
||
from rmm.mr import DeviceMemoryResource # type: ignore[import-not-found] | ||
|
||
|
||
class GPUEngine: | ||
""" | ||
Configuration options for the GPU execution engine. | ||
Use this if you want control over details of the execution. | ||
Supported options | ||
- `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.""" | ||
config: Mapping[str, Any] | ||
"""Additional configuration options for the engine.""" | ||
|
||
def __init__( | ||
self, | ||
*, | ||
device: int | None = None, | ||
memory_resource: Any | None = None, | ||
**kwargs: Any, | ||
) -> None: | ||
self.device = device | ||
self.memory_resource = memory_resource | ||
self.config = kwargs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
import polars as pl | ||
from polars.testing import assert_frame_equal | ||
|
||
if TYPE_CHECKING: | ||
from polars._typing import EngineType | ||
|
||
|
||
@pytest.fixture() | ||
def df() -> pl.LazyFrame: | ||
return pl.LazyFrame({"a": [1, 2, 3]}) | ||
|
||
|
||
@pytest.fixture(params=["gpu", pl.GPUEngine()]) | ||
def engine(request: pytest.FixtureRequest) -> EngineType: | ||
value: EngineType = request.param | ||
return value | ||
|
||
|
||
def test_engine_selection_invalid_raises(df: pl.LazyFrame) -> None: | ||
with pytest.raises(ValueError): | ||
df.collect(engine="unknown") # type: ignore[call-overload] | ||
|
||
|
||
def test_engine_selection_streaming_warns(df: pl.LazyFrame, engine: EngineType) -> None: | ||
expect = df.collect() | ||
with pytest.warns( | ||
UserWarning, match="GPU engine does not support streaming or background" | ||
): | ||
got = df.collect(engine=engine, streaming=True) | ||
assert_frame_equal(expect, got) | ||
|
||
|
||
def test_engine_selection_background_warns( | ||
df: pl.LazyFrame, engine: EngineType | ||
) -> None: | ||
expect = df.collect() | ||
with pytest.warns( | ||
UserWarning, match="GPU engine does not support streaming or background" | ||
): | ||
got = df.collect(engine=engine, background=True) | ||
assert_frame_equal(expect, got.fetch_blocking()) | ||
|
||
|
||
def test_engine_selection_eager_quiet(df: pl.LazyFrame, engine: EngineType) -> None: | ||
expect = df.collect() | ||
# _eager collection turns off GPU engine quietly | ||
got = df.collect(engine=engine, _eager=True) | ||
assert_frame_equal(expect, got) | ||
|
||
|
||
def test_engine_import_error_raises(df: pl.LazyFrame, engine: EngineType) -> None: | ||
with pytest.raises(ImportError, match="GPU engine requested"): | ||
df.collect(engine=engine) |