|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | +from textwrap import dedent |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +pytest.importorskip("IPython") |
| 22 | + |
| 23 | + |
| 24 | +SCRIPT_HEADER = """ |
| 25 | +from IPython.core.interactiveshell import InteractiveShell |
| 26 | +from traitlets.config import Config |
| 27 | +c = Config() |
| 28 | +c.HistoryManager.hist_file = ":memory:" |
| 29 | +ip = InteractiveShell(config=c) |
| 30 | +""" |
| 31 | + |
| 32 | + |
| 33 | +def run_script(body): |
| 34 | + script = SCRIPT_HEADER + dedent(body) |
| 35 | + |
| 36 | + res = subprocess.run( |
| 37 | + [sys.executable, "-c", script], |
| 38 | + stderr=subprocess.STDOUT, |
| 39 | + stdout=subprocess.PIPE, |
| 40 | + text=True, |
| 41 | + ) |
| 42 | + # Pull out attributes before assert for nicer error reporting on failure |
| 43 | + returncode = res.returncode |
| 44 | + stdout = res.stdout |
| 45 | + assert returncode == 0, stdout |
| 46 | + |
| 47 | + |
| 48 | +def test_magic(): |
| 49 | + run_script( |
| 50 | + """ |
| 51 | + ip.run_line_magic("load_ext", "cuml.accel") |
| 52 | +
|
| 53 | + # cuml.accel proxies setup properly |
| 54 | + ip.run_cell("from sklearn.linear_model import LinearRegression") |
| 55 | + ip.run_cell("from cuml.accel import is_proxy") |
| 56 | + ip.run_cell("assert is_proxy(LinearRegression)").raise_error() |
| 57 | + """ |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +def test_magic_cudf_pandas_before(): |
| 62 | + run_script( |
| 63 | + """ |
| 64 | + ip.run_line_magic("load_ext", "cudf.pandas") |
| 65 | + ip.run_cell("import rmm; mr = rmm.mr.get_current_device_resource();") |
| 66 | +
|
| 67 | + ip.run_line_magic("load_ext", "cuml.accel") |
| 68 | + ip.run_cell("mr2 = rmm.mr.get_current_device_resource();") |
| 69 | +
|
| 70 | + # cuml doesn't change the mr setup by cudf.pandas |
| 71 | + ip.run_cell("assert mr is mr2").raise_error() |
| 72 | +
|
| 73 | + # cuml.accel proxies setup properly |
| 74 | + ip.run_cell("from sklearn.linear_model import LinearRegression") |
| 75 | + ip.run_cell("from cuml.accel import is_proxy") |
| 76 | + result = ip.run_cell("assert is_proxy(LinearRegression)").raise_error() |
| 77 | + """ |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +def test_magic_cudf_pandas_after(): |
| 82 | + run_script( |
| 83 | + """ |
| 84 | + ip.run_line_magic("load_ext", "cuml.accel") |
| 85 | + ip.run_cell("import rmm; mr = rmm.mr.get_current_device_resource();") |
| 86 | +
|
| 87 | + ip.run_line_magic("load_ext", "cudf.pandas") |
| 88 | + ip.run_cell("mr2 = rmm.mr.get_current_device_resource();") |
| 89 | +
|
| 90 | + # cudf.pandas doesn't change the mr setup by cuml.accel |
| 91 | + ip.run_cell("assert mr is mr2").raise_error() |
| 92 | +
|
| 93 | + # cuml.accel proxies setup properly |
| 94 | + ip.run_cell("from sklearn.linear_model import LinearRegression") |
| 95 | + ip.run_cell("from cuml.accel import is_proxy") |
| 96 | + result = ip.run_cell("assert is_proxy(LinearRegression)").raise_error() |
| 97 | + """ |
| 98 | + ) |
0 commit comments