-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport patching functionality from 24.06 (#41)
This PR backports #27, #37, and #39 to 24.04 --------- Signed-off-by: Vyas Ramasubramani <[email protected]> Co-authored-by: Richard (Rick) Zamora <[email protected]> Co-authored-by: Bradley Dice <[email protected]>
- Loading branch information
1 parent
3d6efa0
commit 99d37eb
Showing
22 changed files
with
803 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ build/ | |
wheels/ | ||
*.egg-info/ | ||
*.egg | ||
*.whl |
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,24 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
# Always exclude vendored files from linting | ||
exclude: ".*__rdd_patch_.*" | ||
|
||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.5.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- repo: https://github.com/codespell-project/codespell | ||
rev: v2.2.6 | ||
hooks: | ||
- id: codespell | ||
- repo: https://github.com/astral-sh/ruff-pre-commit | ||
rev: v0.2.2 | ||
hooks: | ||
- id: ruff | ||
args: ["--fix"] | ||
- id: ruff-format | ||
|
||
default_language_version: | ||
python: python3 |
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 @@ | ||
import rapids_dask_dependency |
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,18 +1,19 @@ | ||
#!/bin/bash | ||
# Copyright (c) 2023, NVIDIA CORPORATION. | ||
# Copyright (c) 2023-2024, NVIDIA CORPORATION. | ||
|
||
set -euo pipefail | ||
|
||
source rapids-configure-sccache | ||
source rapids-date-string | ||
|
||
package_name=rapids-dask-dependency | ||
package_dir="pip/${package_name}" | ||
version=$(rapids-generate-version) | ||
|
||
sed -i "s/^version = .*/version = \"${version}\"/g" "${package_dir}/pyproject.toml" | ||
sed -i "s/^version = .*/version = \"${version}\"/g" "pyproject.toml" | ||
|
||
cd "${package_dir}" | ||
python -m pip wheel . -w dist -vvv --no-deps --disable-pip-version-check | ||
python -m pip wheel . -w dist -vv --no-deps --disable-pip-version-check | ||
|
||
RAPIDS_PY_WHEEL_NAME="${package_name}" RAPIDS_PY_WHEEL_PURE="1" rapids-upload-wheels-to-s3 dist | ||
RAPIDS_PY_WHEEL_NAME="rapids-dask-dependency" RAPIDS_PY_WHEEL_PURE="1" rapids-upload-wheels-to-s3 dist | ||
|
||
# Run tests | ||
python -m pip install $(ls dist/*.whl)[test] | ||
python -m pytest -v tests/ |
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,4 @@ | ||
#!/bin/bash | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
python -m pytest -v tests/ |
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 @@ | ||
../../../tests/ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from .dask_loader import DaskLoader | ||
|
||
DaskLoader.install() |
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,82 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import importlib | ||
import importlib.abc | ||
import importlib.machinery | ||
import importlib.util | ||
import sys | ||
from contextlib import contextmanager | ||
|
||
from rapids_dask_dependency.utils import patch_warning_stacklevel, update_spec | ||
|
||
|
||
class DaskLoader(importlib.abc.MetaPathFinder, importlib.abc.Loader): | ||
def __init__(self): | ||
self._blocklist = set() | ||
|
||
def create_module(self, spec): | ||
if spec.name.startswith("dask") or spec.name.startswith("distributed"): | ||
with self.disable(spec.name): | ||
try: | ||
# Absolute import is important here to avoid shadowing the real dask | ||
# and distributed modules in sys.modules. Bad things will happen if | ||
# we use relative imports here. | ||
proxy = importlib.import_module( | ||
f"rapids_dask_dependency.patches.{spec.name}" | ||
) | ||
if hasattr(proxy, "load_module"): | ||
return proxy.load_module(spec) | ||
except ModuleNotFoundError: | ||
pass | ||
|
||
# Three extra stack frames: 1) DaskLoader.create_module, | ||
# 2) importlib.import_module, and 3) the patched warnings function (not | ||
# including the internal frames, which warnings ignores). | ||
with patch_warning_stacklevel(3): | ||
mod = importlib.import_module(spec.name) | ||
|
||
update_spec(spec, mod.__spec__) | ||
return mod | ||
|
||
def exec_module(self, _): | ||
pass | ||
|
||
@contextmanager | ||
def disable(self, name): | ||
# This is a context manager that prevents this finder from intercepting calls to | ||
# import a specific name. We must do this to avoid infinite recursion when | ||
# calling import_module in create_module. However, we cannot blanket disable the | ||
# finder because that causes it to be bypassed when transitive imports occur | ||
# within import_module. | ||
try: | ||
self._blocklist.add(name) | ||
yield | ||
finally: | ||
self._blocklist.remove(name) | ||
|
||
def find_spec(self, fullname: str, _, __=None): | ||
if fullname in self._blocklist: | ||
return None | ||
if ( | ||
fullname in ("dask", "distributed") | ||
or fullname.startswith("dask.") | ||
or fullname.startswith("distributed.") | ||
): | ||
return importlib.machinery.ModuleSpec( | ||
name=fullname, | ||
loader=self, | ||
# Set these parameters dynamically in create_module | ||
origin=None, | ||
loader_state=None, | ||
is_package=True, | ||
) | ||
return None | ||
|
||
@classmethod | ||
def install(cls): | ||
try: | ||
(self,) = (obj for obj in sys.meta_path if isinstance(obj, cls)) | ||
except ValueError: | ||
self = cls() | ||
sys.meta_path.insert(0, self) | ||
return self |
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,55 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
import importlib | ||
import importlib.util | ||
from abc import abstractmethod | ||
|
||
from rapids_dask_dependency.utils import patch_warning_stacklevel, update_spec | ||
|
||
|
||
class BaseImporter: | ||
@abstractmethod | ||
def load_module(self, spec): | ||
pass | ||
|
||
|
||
class MonkeyPatchImporter(BaseImporter): | ||
"""The base importer for modules that are monkey-patched.""" | ||
|
||
def __init__(self, name, patch_func): | ||
self.name = name.replace("rapids_dask_dependency.patches.", "") | ||
self.patch_func = patch_func | ||
|
||
def load_module(self, spec): | ||
# Four extra stack frames: 1) DaskLoader.create_module, 2) | ||
# MonkeyPatchImporter.load_module, 3) importlib.import_module, and 4) the | ||
# patched warnings function (not including the internal frames, which warnings | ||
# ignores). | ||
with patch_warning_stacklevel(4): | ||
mod = importlib.import_module(self.name) | ||
self.patch_func(mod) | ||
update_spec(spec, mod.__spec__) | ||
mod._rapids_patched = True | ||
return mod | ||
|
||
|
||
class VendoredImporter(BaseImporter): | ||
"""The base importer for vendored modules.""" | ||
|
||
# Vendored files use a standard prefix to avoid name collisions. | ||
default_prefix = "__rdd_patch_" | ||
|
||
def __init__(self, module): | ||
self.real_module_name = module.replace("rapids_dask_dependency.patches.", "") | ||
module_parts = module.split(".") | ||
module_parts[-1] = self.default_prefix + module_parts[-1] | ||
self.vendored_module_name = ".".join(module_parts) | ||
|
||
def load_module(self, spec): | ||
vendored_module = importlib.import_module(self.vendored_module_name) | ||
# At this stage the module loader must have been disabled for this module, so we | ||
# can access the original module. We don't want to actually import it, we just | ||
# want enough information on it to update the spec. | ||
original_spec = importlib.util.find_spec(self.real_module_name) | ||
update_spec(spec, original_spec) | ||
return vendored_module |
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 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. |
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,6 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. | ||
|
||
from rapids_dask_dependency.importer import MonkeyPatchImporter | ||
|
||
_importer = MonkeyPatchImporter(__name__, lambda _: None) | ||
load_module = _importer.load_module |
Oops, something went wrong.