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

[core] [5/N] Check uv existence before installation #48632

Merged
merged 5 commits into from
Nov 8, 2024
Merged
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
42 changes: 36 additions & 6 deletions python/ray/_private/runtime_env/uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(

logger.debug("Setting up uv for runtime_env: %s", runtime_env)
self._target_dir = target_dir
# An empty directory is created to execute cmd.
self._exec_cwd = os.path.join(self._target_dir, "exec_cwd")
self._runtime_env = runtime_env
self._logger = logger

Expand Down Expand Up @@ -62,6 +64,28 @@ async def _install_uv(
logger.info("Installing package uv to %s", virtualenv_path)
await check_output_cmd(uv_install_cmd, logger=logger, cwd=cwd, env=pip_env)

# TODO(hjiang): Add an integration test for existence check after
# PR (https://github.com/ray-project/ray/pull/48619) gets merged.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

My planned way to test:

  • In integration test with ray.remote having uv specified, making sure all packages correctly installed;
  • Call _check_uv_existence to make sure uv does exist in virtual env.

Copy link
Collaborator

Choose a reason for hiding this comment

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

48619 is merged now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just realize it's not a good way to test:

  • Runtime env agent runs in a separate process, which means at integration test, I have no access to UvProcessor
  • After task completion, the runtime env is deleted, so virtual env is expected to not found

async def _check_uv_existence(
self, path: str, cwd: str, env: dict, logger: logging.Logger
) -> bool:
"""Check and return the existence of `uv` in system executable path."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is checking the existence of uv inside the virtualenv we create for the runtime env right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

dentiny marked this conversation as resolved.
Show resolved Hide resolved
python = virtualenv_utils.get_virtualenv_python(path)

check_existence_cmd = [
python,
"-m",
"uv",
"version",
]

try:
# If `uv` doesn't exist, exception will be thrown.
await check_output_cmd(check_existence_cmd, logger=logger, cwd=cwd, env=env)
return True
except Exception:
return False

async def _install_uv_packages(
self,
path: str,
Expand All @@ -70,13 +94,18 @@ async def _install_uv_packages(
pip_env: Dict,
logger: logging.Logger,
):
"""Install required python packages via `uv`."""
virtualenv_path = virtualenv_utils.get_virtualenv_path(path)
python = virtualenv_utils.get_virtualenv_python(path)
# TODO(fyrestone): Support -i, --no-deps, --no-cache-dir, ...
requirements_file = dependency_utils.get_requirements_file(path, uv_packages)

# Check existence for `uv` and see if we could skip `uv` installation.
uv_exists = await self._check_uv_existence(python, cwd, pip_env, logger)

# Install uv, which acts as the default package manager.
await self._install_uv(path, cwd, pip_env, logger)
if not uv_exists:
await self._install_uv(path, cwd, pip_env, logger)

# Avoid blocking the event loop.
loop = get_running_loop()
Expand Down Expand Up @@ -109,17 +138,18 @@ async def _run(self):
# We create an empty directory for exec cmd so that the cmd will
# run more stable. e.g. if cwd has ray, then checking ray will
# look up ray in cwd instead of site packages.
exec_cwd = os.path.join(path, "exec_cwd")
os.makedirs(exec_cwd, exist_ok=True)
os.makedirs(self._exec_cwd, exist_ok=True)
try:
await virtualenv_utils.create_or_get_virtualenv(path, exec_cwd, logger)
await virtualenv_utils.create_or_get_virtualenv(
path, self._exec_cwd, logger
)
python = virtualenv_utils.get_virtualenv_python(path)
async with dependency_utils.check_ray(python, exec_cwd, logger):
async with dependency_utils.check_ray(python, self._exec_cwd, logger):
# Install packages with uv.
await self._install_uv_packages(
path,
uv_packages,
exec_cwd,
self._exec_cwd,
self._uv_env,
logger,
)
Expand Down