Skip to content

Commit b3a8638

Browse files
davanstrienclaude
andauthored
Read the Jobs flavor from HF_JOB_FLAVOR; drop jobs_flavor() (#211)
Jobs now sets HF_JOB_FLAVOR to the flavor name, so the ~70-line jobs_flavor() helper (hardware-list lookup by GPU model, CPU_CORES and MEMORY) is no longer needed. Replace it in 21 scripts with os.environ.get("HF_JOB_FLAVOR", ""), update the setfit tests and the AGENTS.md provenance-stamp guidance. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent aa9bf29 commit b3a8638

23 files changed

Lines changed: 38 additions & 1668 deletions

‎AGENTS.md‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ image, add a dependency to an image-mode recipe, or retire a recipe. The procedu
6565

6666
```python
6767
on_jobs = os.environ.get("HF_JOB_ID") is not None # set by HF Jobs in-container
68-
hw = jobs_flavor() # e.g. "a10g-small"; "" if unknown
68+
hw = os.environ.get("HF_JOB_FLAVOR", "") # set by HF Jobs, e.g. "a10g-small"
6969
origin = (
7070
f"Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
7171
+ (f" (`{hw}`)" if hw else "")
@@ -82,14 +82,10 @@ image, add a dependency to an image-mode recipe, or retire a recipe. The procedu
8282
```
8383
```
8484

85-
**Do not stamp the raw `ACCELERATOR` value.** On Jobs it holds only `cpu` / `gpu` (verified on t4-small,
86-
a10g-small, l4x1, a10g-largex2 and l4x4, 2026-09/10), not the flavor the hub docs describe, so a raw stamp
87-
prints "(`gpu`)" and a reproduce command built from it says `--flavor gpu`. Copy the `jobs_flavor()`
88-
helper from `ocr/glm-ocr.py`: it returns `HF_JOB_FLAVOR` when Jobs sets it (live since 2026-11-24),
89-
and otherwise looks the flavor up in the public Jobs hardware list
90-
(`HfApi().list_jobs_hardware()`, no token needed) by GPU model and count, then by the `CPU_CORES` /
91-
`MEMORY` values Jobs sets, so it also tells `a10g-small` from `a10g-large` and names CPU flavors. It
92-
returns "" on any failure. Use `jobs_flavor() or "<default>"` for the reproduce command's `--flavor`.
85+
Jobs sets `HF_JOB_FLAVOR` to the flavor name (live since 2026-11-24). Use it for the stamp, and
86+
`os.environ.get("HF_JOB_FLAVOR") or "<default>"` for the reproduce command's `--flavor`. **Do not stamp the
87+
raw `ACCELERATOR` value:** on Jobs it holds only `cpu` / `gpu`, so a reproduce command built from it says
88+
`--flavor gpu`.
9389

9490
Rules: the reproduce command is **`hf jobs uv run`** (never bare `uv run` — the card is the Jobs
9591
advertisement); the "Produced on HF Jobs" claim is **gated on `HF_JOB_ID`** (a local run must not claim it);

‎classification/classify-gliner2.py‎

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -191,80 +191,8 @@ def label_counts_table(tasks: list, counts_by_task: dict, total: int) -> str:
191191
return "\n".join(lines)
192192

193193

194-
def jobs_flavor() -> str:
195-
"""Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
196-
197-
HF Jobs sets HF_JOB_FLAVOR to the flavor name. Without it (ACCELERATOR holds only
198-
"cpu" or "gpu", not the flavor name), the flavor is looked up
199-
in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
200-
MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
201-
cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
202-
"""
203-
if os.environ.get("HF_JOB_FLAVOR"):
204-
return os.environ["HF_JOB_FLAVOR"]
205-
hardware = os.environ.get("ACCELERATOR") or ""
206-
if "-" in hardware or any(character.isdigit() for character in hardware):
207-
return hardware # already a flavor name
208-
try:
209-
from huggingface_hub import HfApi
210-
211-
gpu_name, gpu_count = "", 0
212-
try:
213-
import torch
214-
215-
if torch.cuda.is_available():
216-
gpu_name = torch.cuda.get_device_name(0)
217-
gpu_count = torch.cuda.device_count()
218-
except ImportError:
219-
# Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
220-
import subprocess
221-
222-
try:
223-
listed = subprocess.run(
224-
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
225-
capture_output=True, text=True, timeout=10, check=True,
226-
).stdout
227-
names = [line.strip() for line in listed.splitlines() if line.strip()]
228-
if names:
229-
gpu_name, gpu_count = names[0], len(names)
230-
except (OSError, subprocess.SubprocessError):
231-
pass
232-
if gpu_count == 0 and hardware == "gpu":
233-
return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
234-
235-
# Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
236-
gpu_words = gpu_name.upper().replace("-", " ").split()
237-
candidates = []
238-
for flavor in HfApi(token=False).list_jobs_hardware():
239-
accelerator = flavor.accelerator
240-
if gpu_count == 0 and accelerator is None:
241-
candidates.append(flavor)
242-
elif (
243-
gpu_count > 0
244-
and accelerator is not None
245-
and accelerator.model.upper() in gpu_words
246-
and int(accelerator.quantity) == gpu_count
247-
):
248-
candidates.append(flavor)
249-
if not candidates:
250-
return ""
251-
252-
cores = float(os.environ.get("CPU_CORES") or 0)
253-
memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
254-
255-
def distance(flavor) -> float:
256-
# Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
257-
listed_cores = float(flavor.cpu.split()[0])
258-
listed_memory = float(flavor.ram.split()[0])
259-
return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
260-
261-
return min(candidates, key=distance).name
262-
except Exception:
263-
return ""
264-
265-
266194
def build_reproduce_command(args) -> str:
267-
flavor = jobs_flavor() or "t4-small"
195+
flavor = os.environ.get("HF_JOB_FLAVOR", "") or "t4-small"
268196
parts = [
269197
f"hf jobs uv run --flavor {flavor} --timeout 1h --secrets HF_TOKEN \\",
270198
f" {SCRIPT_URL} \\",
@@ -301,7 +229,7 @@ def build_reproduce_command(args) -> str:
301229
def build_card(args, tasks, counts_by_task, total, seconds, zero_shot: bool) -> str:
302230
"""Dataset card with the canonical uv-scripts provenance stamp."""
303231
on_jobs = os.environ.get("HF_JOB_ID") is not None
304-
hardware = jobs_flavor()
232+
hardware = os.environ.get("HF_JOB_FLAVOR", "")
305233
if on_jobs:
306234
origin = "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
307235
if hardware:

‎classification/train-classifier.py‎

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -429,84 +429,12 @@ def verify_reload(output_repo, eval_texts, reference_preds, problem_type, max_le
429429
# ---------------------------------------------------------------------------
430430

431431

432-
def jobs_flavor() -> str:
433-
"""Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
434-
435-
HF Jobs sets HF_JOB_FLAVOR to the flavor name. Without it (ACCELERATOR holds only
436-
"cpu" or "gpu", not the flavor name), the flavor is looked up
437-
in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
438-
MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
439-
cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
440-
"""
441-
if os.environ.get("HF_JOB_FLAVOR"):
442-
return os.environ["HF_JOB_FLAVOR"]
443-
hardware = os.environ.get("ACCELERATOR") or ""
444-
if "-" in hardware or any(character.isdigit() for character in hardware):
445-
return hardware # already a flavor name
446-
try:
447-
from huggingface_hub import HfApi
448-
449-
gpu_name, gpu_count = "", 0
450-
try:
451-
import torch
452-
453-
if torch.cuda.is_available():
454-
gpu_name = torch.cuda.get_device_name(0)
455-
gpu_count = torch.cuda.device_count()
456-
except ImportError:
457-
# Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
458-
import subprocess
459-
460-
try:
461-
listed = subprocess.run(
462-
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
463-
capture_output=True, text=True, timeout=10, check=True,
464-
).stdout
465-
names = [line.strip() for line in listed.splitlines() if line.strip()]
466-
if names:
467-
gpu_name, gpu_count = names[0], len(names)
468-
except (OSError, subprocess.SubprocessError):
469-
pass
470-
if gpu_count == 0 and hardware == "gpu":
471-
return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
472-
473-
# Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
474-
gpu_words = gpu_name.upper().replace("-", " ").split()
475-
candidates = []
476-
for flavor in HfApi(token=False).list_jobs_hardware():
477-
accelerator = flavor.accelerator
478-
if gpu_count == 0 and accelerator is None:
479-
candidates.append(flavor)
480-
elif (
481-
gpu_count > 0
482-
and accelerator is not None
483-
and accelerator.model.upper() in gpu_words
484-
and int(accelerator.quantity) == gpu_count
485-
):
486-
candidates.append(flavor)
487-
if not candidates:
488-
return ""
489-
490-
cores = float(os.environ.get("CPU_CORES") or 0)
491-
memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
492-
493-
def distance(flavor) -> float:
494-
# Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
495-
listed_cores = float(flavor.cpu.split()[0])
496-
listed_memory = float(flavor.ram.split()[0])
497-
return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
498-
499-
return min(candidates, key=distance).name
500-
except Exception:
501-
return ""
502-
503-
504432
def build_card(
505433
input_dataset, output_repo, model_id, problem_type, label_names, metrics,
506434
thresholds, path_kind, args_summary,
507435
) -> str:
508436
on_jobs = os.environ.get("HF_JOB_ID") is not None # set by HF Jobs in-container
509-
hw = jobs_flavor() # flavor name, e.g. "a10g-small"; "" on CPU
437+
hw = os.environ.get("HF_JOB_FLAVOR", "") # set by HF Jobs, e.g. "a10g-small"; "" outside Jobs
510438
origin = (
511439
"Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
512440
+ (f" (`{hw}`)" if hw else "")

‎classification/train-gliner2.py‎

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -748,81 +748,9 @@ def evaluate(model_path: str, eval_sets: dict, tasks: list, batch_size: int, exp
748748
return metrics_by_split
749749

750750

751-
def jobs_flavor() -> str:
752-
"""Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
753-
754-
HF Jobs sets HF_JOB_FLAVOR to the flavor name. Without it (ACCELERATOR holds only
755-
"cpu" or "gpu", not the flavor name), the flavor is looked up
756-
in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
757-
MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
758-
cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
759-
"""
760-
if os.environ.get("HF_JOB_FLAVOR"):
761-
return os.environ["HF_JOB_FLAVOR"]
762-
hardware = os.environ.get("ACCELERATOR") or ""
763-
if "-" in hardware or any(character.isdigit() for character in hardware):
764-
return hardware # already a flavor name
765-
try:
766-
from huggingface_hub import HfApi
767-
768-
gpu_name, gpu_count = "", 0
769-
try:
770-
import torch
771-
772-
if torch.cuda.is_available():
773-
gpu_name = torch.cuda.get_device_name(0)
774-
gpu_count = torch.cuda.device_count()
775-
except ImportError:
776-
# Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
777-
import subprocess
778-
779-
try:
780-
listed = subprocess.run(
781-
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
782-
capture_output=True, text=True, timeout=10, check=True,
783-
).stdout
784-
names = [line.strip() for line in listed.splitlines() if line.strip()]
785-
if names:
786-
gpu_name, gpu_count = names[0], len(names)
787-
except (OSError, subprocess.SubprocessError):
788-
pass
789-
if gpu_count == 0 and hardware == "gpu":
790-
return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
791-
792-
# Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
793-
gpu_words = gpu_name.upper().replace("-", " ").split()
794-
candidates = []
795-
for flavor in HfApi(token=False).list_jobs_hardware():
796-
accelerator = flavor.accelerator
797-
if gpu_count == 0 and accelerator is None:
798-
candidates.append(flavor)
799-
elif (
800-
gpu_count > 0
801-
and accelerator is not None
802-
and accelerator.model.upper() in gpu_words
803-
and int(accelerator.quantity) == gpu_count
804-
):
805-
candidates.append(flavor)
806-
if not candidates:
807-
return ""
808-
809-
cores = float(os.environ.get("CPU_CORES") or 0)
810-
memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
811-
812-
def distance(flavor) -> float:
813-
# Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
814-
listed_cores = float(flavor.cpu.split()[0])
815-
listed_memory = float(flavor.ram.split()[0])
816-
return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
817-
818-
return min(candidates, key=distance).name
819-
except Exception:
820-
return ""
821-
822-
823751
def build_reproduce_command(args) -> str:
824752
"""Rebuild the exact invocation, so the card's command produces the card's model."""
825-
flavor = jobs_flavor() or "t4-small"
753+
flavor = os.environ.get("HF_JOB_FLAVOR", "") or "t4-small"
826754
# The timeout is the [tool.hf-jobs] default, spelled out because older CLIs ignore the header.
827755
parts = [f"hf jobs uv run --flavor {flavor} --timeout 1h --secrets HF_TOKEN \\"]
828756
if args.train_file:
@@ -914,7 +842,7 @@ def results_table(tasks: list, zero_shot, fine_tuned) -> str:
914842
def build_card(args, tasks, zero_shot, fine_tuned, train_size, train_seconds, carved_out, oom_steps) -> str:
915843
"""Model card following the uv-scripts conventions (org credit, Jobs claim gated on HF_JOB_ID)."""
916844
on_jobs = os.environ.get("HF_JOB_ID") is not None
917-
hardware = jobs_flavor()
845+
hardware = os.environ.get("HF_JOB_FLAVOR", "")
918846
if on_jobs:
919847
provenance = "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
920848
if hardware:

‎classification/train-setfit.py‎

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -480,78 +480,6 @@ def estimate_training_steps(per_class, batch_size, num_epochs, strategy) -> tupl
480480
return pairs, steps
481481

482482

483-
def jobs_flavor() -> str:
484-
"""Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
485-
486-
HF Jobs sets HF_JOB_FLAVOR to the flavor name. Without it (ACCELERATOR holds only
487-
"cpu" or "gpu", not the flavor name), the flavor is looked up
488-
in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
489-
MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
490-
cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
491-
"""
492-
if os.environ.get("HF_JOB_FLAVOR"):
493-
return os.environ["HF_JOB_FLAVOR"]
494-
hardware = os.environ.get("ACCELERATOR") or ""
495-
if "-" in hardware or any(character.isdigit() for character in hardware):
496-
return hardware # already a flavor name
497-
try:
498-
from huggingface_hub import HfApi
499-
500-
gpu_name, gpu_count = "", 0
501-
try:
502-
import torch
503-
504-
if torch.cuda.is_available():
505-
gpu_name = torch.cuda.get_device_name(0)
506-
gpu_count = torch.cuda.device_count()
507-
except ImportError:
508-
# Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
509-
import subprocess
510-
511-
try:
512-
listed = subprocess.run(
513-
["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
514-
capture_output=True, text=True, timeout=10, check=True,
515-
).stdout
516-
names = [line.strip() for line in listed.splitlines() if line.strip()]
517-
if names:
518-
gpu_name, gpu_count = names[0], len(names)
519-
except (OSError, subprocess.SubprocessError):
520-
pass
521-
if gpu_count == 0 and hardware == "gpu":
522-
return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
523-
524-
# Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
525-
gpu_words = gpu_name.upper().replace("-", " ").split()
526-
candidates = []
527-
for flavor in HfApi(token=False).list_jobs_hardware():
528-
accelerator = flavor.accelerator
529-
if gpu_count == 0 and accelerator is None:
530-
candidates.append(flavor)
531-
elif (
532-
gpu_count > 0
533-
and accelerator is not None
534-
and accelerator.model.upper() in gpu_words
535-
and int(accelerator.quantity) == gpu_count
536-
):
537-
candidates.append(flavor)
538-
if not candidates:
539-
return ""
540-
541-
cores = float(os.environ.get("CPU_CORES") or 0)
542-
memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
543-
544-
def distance(flavor) -> float:
545-
# Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
546-
listed_cores = float(flavor.cpu.split()[0])
547-
listed_memory = float(flavor.ram.split()[0])
548-
return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
549-
550-
return min(candidates, key=distance).name
551-
except Exception:
552-
return ""
553-
554-
555483
def build_reproduce_command(args) -> str:
556484
"""Rebuild recipe options for Jobs, preserving the recorded accelerator when available.
557485
@@ -560,7 +488,7 @@ def build_reproduce_command(args) -> str:
560488
"""
561489
flavor = "t4-small" if torch.cuda.is_available() else "cpu-basic"
562490
if os.environ.get("HF_JOB_ID"):
563-
flavor = jobs_flavor() or flavor
491+
flavor = os.environ.get("HF_JOB_FLAVOR", "") or flavor
564492
parts = [
565493
f"hf jobs uv run --flavor {flavor} --secrets HF_TOKEN \\",
566494
f" {SCRIPT_URL} \\",

0 commit comments

Comments
 (0)