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

Experimental command line interface UX #6135

Draft
wants to merge 18 commits into
base: branch-24.12
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions python/cuml/cuml/cluster/dbscan.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ class DBSCAN(UniversalBase,
core_sample_indices_ = CumlArrayDescriptor(order="C")
labels_ = CumlArrayDescriptor(order="C")

_hyperparam_interop_translator = {
"metric": {
"manhattan": "dispatch",
"chebyshev": "dispatch",
"minkowski": "dispatch",
},

"algorithm": {
"auto": "brute",
"ball_tree": "dispatch",
"kd_tree": "dispatch",
},
}

@device_interop_preparation
def __init__(self, *,
eps=0.5,
Expand Down
10 changes: 10 additions & 0 deletions python/cuml/cuml/decomposition/pca.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,16 @@ class PCA(UniversalBase,
noise_variance_ = CumlArrayDescriptor(order='F')
trans_input_ = CumlArrayDescriptor(order='F')

_hyperparam_interop_translator = {
"svd_solver": {
"arpack": "full",
"randomized": "full"
},
"iterated_power": {
"auto": 15,
},
}

@device_interop_preparation
def __init__(self, *, copy=True, handle=None, iterated_power=15,
n_components=None, random_state=None, svd_solver='auto',
Expand Down
50 changes: 50 additions & 0 deletions python/cuml/cuml/experimental/accel/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


from .magics import load_ipython_extension

# from .profiler import Profiler
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the comment.


__all__ = ["load_ipython_extension", "install"]


LOADED = False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this threadsafe? My initial instinct is that this requires protection by a lock, but maybe there is some reason why this wouldn't be an issue?



def install():
"""Enable cuML Accelerator Mode."""
from .module_accelerator import ModuleAccelerator

print("Installing cuML Accelerator...")
loader = ModuleAccelerator.install("sklearn", "cuml", "sklearn")
loader_umap = ModuleAccelerator.install("umap", "cuml", "umap")
loader_hdbscan = ModuleAccelerator.install("hdbscan", "cuml", "hdbscan")
global LOADED
LOADED = all(
var is not None for var in [loader, loader_umap, loader_hdbscan]
)


def pytest_load_initial_conftests(early_config, parser, args):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the no-code change magic kicks in when running the pytest suite? Very cool.

By the way, does it affect the other pytests that are outside cuml.experimental.accel? Many of our existing tests assert that cuML algorithm matches output of sklearn's counterpart.

# https://docs.pytest.org/en/7.1.x/reference/\
# reference.html#pytest.hookspec.pytest_load_initial_conftests
try:
install()
except RuntimeError:
raise RuntimeError(
"An existing plugin has already loaded sklearn. Interposing failed."
)
88 changes: 88 additions & 0 deletions python/cuml/cuml/experimental/accel/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import click
import code
import os
import runpy
import sys

from . import install
from cuml.internals import logger


@click.command()
@click.option("-m", "module", required=False, help="Module to run")
@click.option(
"--profile",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a warning here that this is an unused parameter at the moment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about removing parameters that at the moment do nothing? If we want to add them (with functionality) it is easy enough to do. And it keeps things tidy, both for the reader of the help message and the reader of the code

is_flag=True,
default=False,
help="Perform per-function profiling of this script.",
)
@click.option(
"--line-profile",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

is_flag=True,
default=False,
help="Perform per-line profiling of this script.",
)
@click.option(
"--strict",
is_flag=True,
default=False,
help="Turn strict mode for hyperparameters on.",
)
@click.argument("args", nargs=-1)
def main(module, profile, line_profile, strict, args):
""" """

# todo (dgd): add option to lower verbosity
logger.set_level(logger.level_debug)
logger.set_pattern("%v")

if strict:
os.environ["CUML_ACCEL_STRICT_MODE"] = "ON"

install()

if module:
(module,) = module
# run the module passing the remaining arguments
# as if it were run with python -m <module> <args>
sys.argv[:] = [module] + args # not thread safe?
runpy.run_module(module, run_name="__main__")
elif len(args) >= 1:
# Remove ourself from argv and continue
sys.argv[:] = args
runpy.run_path(args[0], run_name="__main__")
else:
if sys.stdin.isatty():
banner = f"Python {sys.version} on {sys.platform}"
site_import = not sys.flags.no_site
if site_import:
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
banner += "\n" + cprt
else:
# Don't show prompts or banners if stdin is not a TTY
sys.ps1 = ""
sys.ps2 = ""
banner = ""

# Launch an interactive interpreter
code.interact(banner=banner, exitmsg="")


if __name__ == "__main__":
main()
34 changes: 34 additions & 0 deletions python/cuml/cuml/experimental/accel/_wrappers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

wrapped_estimators = {
"KMeans": ("cuml.cluster", "KMeans"),
"DBSCAN": ("cuml.cluster", "DBSCAN"),
"PCA": ("cuml.decomposition", "PCA"),
"TruncatedSVD": ("cuml.decomposition", "TruncatedSVD"),
"KernelRidge": ("cuml.kernel_ridge", "KernelRidge"),
"LinearRegression": ("cuml.linear_model", "LinearRegression"),
"LogisticRegression": ("cuml.linear_model", "LogisticRegression"),
"ElasticNet": ("cuml.linear_model", "ElasticNet"),
"Ridge": ("cuml.linear_model", "Ridge"),
"Lasso": ("cuml.linear_model", "Lasso"),
"TSNE": ("cuml.manifold", "TSNE"),
"NearestNeighbors": ("cuml.neighbors", "NearestNeighbors"),
"KNeighborsClassifier": ("cuml.neighbors", "KNeighborsClassifier"),
"KNeighborsRegressor": ("cuml.neighbors", "KNeighborsRegressor"),
"UMAP": ("cuml.manifold", "UMAP"),
"HDBSCAN": ("cuml.cluster", "HDBSCAN"),
}
24 changes: 24 additions & 0 deletions python/cuml/cuml/experimental/accel/_wrappers/hdbscan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright (c) 2024, NVIDIA CORPORATION.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from ..estimator_proxy import intercept


UMAP = intercept(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HDBSCAN

original_module="hdbscan",
accelerated_module="cuml.cluster",
original_class_name="HDBSCAN",
)
Loading
Loading