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

[runtime_env] Remote whl support #39407

Merged
merged 15 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor install_wheel_package
Signed-off-by: Jonathan Nitisastro <[email protected]>
  • Loading branch information
jonathan-anyscale committed Sep 13, 2023
commit 01361a366e0b5b6efacfc0f28712c6e1ac5f6628
46 changes: 43 additions & 3 deletions python/ray/_private/runtime_env/packaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
RAY_RUNTIME_ENV_URI_PIN_EXPIRATION_S_ENV_VAR,
RAY_RUNTIME_ENV_IGNORE_GITIGNORE,
)
from ray._private.runtime_env.conda_utils import exec_cmd_stream_to_logger
from ray._private.thirdparty.pathspec import PathSpec
from ray.experimental.internal_kv import (
_internal_kv_exists,
Expand Down Expand Up @@ -196,7 +197,9 @@ def parse_uri(pkg_uri: str) -> Tuple[Protocol, str]:

if protocol in Protocol.remote_protocols():
if pkg_uri.endswith(".whl"):
# .whl file name can't be modified
# .whl file name has a format as explained in
jonathan-anyscale marked this conversation as resolved.
Show resolved Hide resolved
# https://peps.python.org/pep-0427/#file-name-convention
# so don't modify
jonathan-anyscale marked this conversation as resolved.
Show resolved Hide resolved
package_name = pkg_uri.split("/")[-1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this cause conflict since we only use the filename of whl instead of the entire uri? Like if I have both s3:https://A/package.whl and s3:https://B/package.whl.

Copy link
Contributor Author

@jonathan-anyscale jonathan-anyscale Sep 22, 2023

Choose a reason for hiding this comment

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

Should be fine since whl filename is uniquely generated when we create it. The only conflict is when the user want to use package with same name which is unlikely.
Also, this filename will only used to save locally, which the file will be deleted after installation so shouldn't be any conflict.

else:
package_name = f"{protocol.value}_{uri.netloc}{uri.path}"
Expand Down Expand Up @@ -732,16 +735,22 @@ def open_file(uri, mode, *, transport_params=None):
with open_file(pkg_file, "wb") as fin:
fin.write(package_zip.read())

if Path(pkg_name).suffix == ".zip":
pkg_format = Path(pkg_name).suffix
if pkg_format == ".zip":
jonathan-anyscale marked this conversation as resolved.
Show resolved Hide resolved
unzip_package(
package_path=pkg_file,
target_dir=local_dir,
remove_top_level_directory=True,
unlink_zip=True,
logger=logger,
)
else:
elif pkg_format == ".whl":
return str(pkg_file)
else:
raise NotImplementedError(
f"Package format {pkg_format} is ",
"not supported for remote protocols",
)
else:
raise NotImplementedError(f"Protocol {protocol} is not supported")

Expand Down Expand Up @@ -891,3 +900,34 @@ def delete_package(pkg_uri: str, base_directory: str) -> Tuple[bool, int]:
deleted = True

return deleted


async def install_wheel_package(
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we move to here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I thought working_dir will support remote wheel as well but it isn't by our definition of working_dir.
But this function is generally used to install wheel package, so might as well move it outside from py_modules for general usage later.

wheel_uri: str,
target_dir: str,
logger: Optional[logging.Logger] = default_logger,
) -> None:
"""Install packages in the wheel URI, and then delete the local wheel file."""

pip_install_cmd = [
"pip",
"install",
wheel_uri,
f"--target={target_dir}",
]

logger.info("Running py_modules wheel install command: %s", str(pip_install_cmd))
try:
# TODO(architkulkarni): Use `await check_output_cmd` or similar.
exit_code, output = exec_cmd_stream_to_logger(pip_install_cmd, logger)
finally:
if Path(wheel_uri).exists():
Path(wheel_uri).unlink()

if exit_code != 0:
if Path(target_dir).exists():
Path(target_dir).unlink()
raise RuntimeError(
f"Failed to install py_modules wheel {wheel_uri}"
f"to {target_dir}:\n{output}"
)
43 changes: 5 additions & 38 deletions python/ray/_private/runtime_env/py_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from types import ModuleType
from typing import Any, Dict, List, Optional

from ray._private.runtime_env.conda_utils import exec_cmd_stream_to_logger
from ray._private.runtime_env.context import RuntimeEnvContext
from ray._private.runtime_env.packaging import (
Protocol,
Expand All @@ -13,6 +12,7 @@
get_local_dir_from_uri,
get_uri_for_directory,
get_uri_for_package,
install_wheel_package,
is_whl_uri,
package_exists,
parse_uri,
Expand Down Expand Up @@ -172,41 +172,6 @@ def delete_uri(
def get_uris(self, runtime_env: dict) -> List[str]:
return runtime_env.py_modules()

async def _unpack_wheel(
self,
wheel_uri: str,
target_uri: str,
logger: Optional[logging.Logger] = default_logger,
):
"""Download and install a wheel URI, and then delete the local wheel file."""
module_dir = self._get_local_dir_from_uri(target_uri)

pip_install_cmd = [
"pip",
"install",
wheel_uri,
f"--target={module_dir}",
]

logger.info(
"Running py_modules wheel install command: %s", str(pip_install_cmd)
)
try:
# TODO(architkulkarni): Use `await check_output_cmd` or similar.
exit_code, output = exec_cmd_stream_to_logger(pip_install_cmd, logger)
finally:
if Path(wheel_uri).exists():
Path(wheel_uri).unlink()

if exit_code != 0:
if Path(module_dir).exists():
Path(module_dir).unlink()
raise RuntimeError(
f"Failed to install py_modules wheel {wheel_uri}"
f"to {module_dir}:\n{output}"
)
return module_dir

async def create(
self,
uri: str,
Expand All @@ -220,8 +185,10 @@ async def create(
)

if is_whl_uri(uri):
module_dir = await self._unpack_wheel(
wheel_uri=module_dir, target_uri=uri, logger=logger
wheel_uri = module_dir
module_dir = self._get_local_dir_from_uri(uri)
await install_wheel_package(
wheel_uri=wheel_uri, target_dir=module_dir, logger=logger
)

return get_directory_size_bytes(module_dir)
Expand Down
44 changes: 6 additions & 38 deletions python/ray/_private/runtime_env/working_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
from typing import Any, Dict, List, Optional

from ray._private.runtime_env.conda_utils import exec_cmd_stream_to_logger
from ray._private.runtime_env.context import RuntimeEnvContext
from ray._private.runtime_env.packaging import (
Protocol,
Expand All @@ -12,6 +11,7 @@
get_local_dir_from_uri,
get_uri_for_directory,
get_uri_for_package,
install_wheel_package,
is_whl_uri,
parse_uri,
upload_package_if_needed,
Expand Down Expand Up @@ -153,41 +153,6 @@ def get_uris(self, runtime_env: "RuntimeEnv") -> List[str]: # noqa: F821
return [working_dir_uri]
return []

async def _unpack_wheel(
self,
wheel_uri: str,
target_uri: str,
logger: Optional[logging.Logger] = default_logger,
):
"""Download and install a wheel URI, and then delete the local wheel file."""
module_dir = get_local_dir_from_uri(target_uri, self._resources_dir)

pip_install_cmd = [
"pip",
"install",
wheel_uri,
f"--target={module_dir}",
]

logger.info(
"Running py_modules wheel install command: %s", str(pip_install_cmd)
)
try:
# TODO(architkulkarni): Use `await check_output_cmd` or similar.
exit_code, output = exec_cmd_stream_to_logger(pip_install_cmd, logger)
finally:
if Path(wheel_uri).exists():
Path(wheel_uri).unlink()

if exit_code != 0:
if Path(module_dir).exists():
Path(module_dir).unlink()
raise RuntimeError(
f"Failed to install py_modules wheel {wheel_uri}"
f"to {module_dir}:\n{output}"
)
return module_dir

async def create(
self,
uri: Optional[str],
Expand All @@ -199,9 +164,12 @@ async def create(
uri, self._resources_dir, self._gcs_aio_client, logger=logger
)
if is_whl_uri(uri):
local_dir = await self._unpack_wheel(
wheel_uri=local_dir, target_uri=uri, logger=logger
wheel_uri = local_dir
local_dir = get_local_dir_from_uri(uri, self._resources_dir)
await install_wheel_package(
wheel_uri=wheel_uri, target_dir=local_dir, logger=logger
)

return get_directory_size_bytes(local_dir)

def modify_context(
Expand Down
Loading