Skip to content
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
24 changes: 23 additions & 1 deletion src/huggingface_hub/cli/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
_default_job_name_from_script,
)
from huggingface_hub.errors import CLIError
from huggingface_hub.utils import logging
from huggingface_hub.utils import is_agent, logging
from huggingface_hub.utils._cache_manager import _format_size
from huggingface_hub.utils._hf_uris import _split_mount
from huggingface_hub.utils._parsing import format_duration, parse_duration
Expand Down Expand Up @@ -718,13 +718,35 @@ def _hint_or_follow_started_job(api: HfApi, job: JobInfo, *, detach: bool) -> No
f"`--network-group {group}` reach each other at `$HF_NETWORK_GROUP_HOSTNAME` (every member) "
"or `${HF_NETWORK_GROUP_PREFIX}<alias>` (members claiming an alias)."
)
if is_agent():
_hint_group_labels(job)
if detach:
out.hint(f"Use `hf jobs logs -f {job_ref}` to stream logs, or `hf jobs inspect {job_ref}` to check status.")
out.hint(f"Use `hf jobs wait {job_ref}` to block until it finishes.")
return
_stream_logs_and_check_status(api, job)


def _hint_group_labels(job: JobInfo) -> None:
"""Nudge agents to label all the Jobs of a session the same way, so they are grouped in the UI."""
labels = job.labels or {}
if group_labels := {key: value for key, value in labels.items() if key != "name"}:
flags = " ".join(
f"--label {key}={value}" if value else f"--label {key}" for key, value in group_labels.items()
)
out.hint(
f"If you launch more Jobs in this session, reuse `{flags}` (with a unique `--name` per Job) "
"so they are grouped in the UI."
)
return
group = f"group={job.id[:8]}"
rename = f" --name {name}" if (name := labels.get("name")) else ""
out.hint(
f"If you launch more Jobs in this session, tag them all with `--label {group}` (with a unique `--name` per Job) "
f"so they are grouped in the UI. Tag this one too: `hf jobs labels {job.owner.name}/{job.id}{rename} --label {group}`."
)


@jobs_cli.command(
"run",
context_settings={"ignore_unknown_options": True},
Expand Down
45 changes: 45 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3481,6 +3481,51 @@ def test_run_with_extra_args(self, runner: CliRunner) -> None:
)
api.fetch_job_logs.assert_not_called()

@pytest.mark.parametrize(
"labels, extra_args, expected",
[
(
{"name": "my-run"},
[],
"tag them all with `--label group=687f911e` (with a unique `--name` per Job) so they are grouped in the UI. "
"Tag this one too: `hf jobs labels my-user/687f911eaea852de79c4a50a --name my-run --label group=687f911e`.",
),
(
{"name": "my-run", "sweep": "lr"},
["--label", "sweep=lr"],
"reuse `--label sweep=lr` (with a unique `--name` per Job) so they are grouped in the UI.",
),
],
)
def test_run_hints_agents_to_group_jobs(
self, runner: CliRunner, labels: dict[str, str], extra_args: list[str], expected: str
) -> None:
job = Mock(id="687f911eaea852de79c4a50a", url="https://huggingface.co/jobs/my-user/687f911eaea852de79c4a50a")
job.owner.name = "my-user"
job.labels = labels
with (
patch("huggingface_hub.cli.jobs.get_hf_api") as api_cls,
patch("huggingface_hub.cli._cli_utils._get_extended_environ", return_value={}),
patch("huggingface_hub.cli.jobs.is_agent", return_value=True),
):
api_cls.return_value.run_job.return_value = job
result = runner.invoke(app, ["jobs", "run", "--detach", "--name", "my-run", *extra_args, "ubuntu", "ls"])
assert result.exit_code == 0
assert f"If you launch more Jobs in this session, {expected}" in result.stderr

def test_run_no_group_hint_for_humans(self, runner: CliRunner) -> None:
job = Mock(id="687f911eaea852de79c4a50a", url="https://huggingface.co/jobs/my-user/687f911eaea852de79c4a50a")
job.labels = {"name": "my-run"}
with (
patch("huggingface_hub.cli.jobs.get_hf_api") as api_cls,
patch("huggingface_hub.cli._cli_utils._get_extended_environ", return_value={}),
patch("huggingface_hub.cli.jobs.is_agent", return_value=False),
):
api_cls.return_value.run_job.return_value = job
result = runner.invoke(app, ["jobs", "run", "--detach", "--name", "my-run", "ubuntu", "ls"])
assert result.exit_code == 0
assert "If you launch more Jobs" not in result.stderr

def test_create_scheduled_job(self, runner: CliRunner) -> None:
scheduled_job = Mock(id="my-job-id")
with (
Expand Down
Loading