davanstrien HF Staff commited on
Commit
b13b6ab
·
verified ·
1 Parent(s): ee0293b

Sync from GitHub via hub-sync

Browse files
Files changed (1) hide show
  1. generate-embeddings.py +70 -1
generate-embeddings.py CHANGED
@@ -366,6 +366,75 @@ def run_streaming_shard(ds, model, prompt_str, args):
366
  logger.info(f"✅ streaming shard {args.shard_index + 1}/{args.num_shards} of run {args.run_id} uploaded")
367
 
368
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
  def main():
370
  p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
371
  p.add_argument("input_dataset", help="Input dataset ID on the Hugging Face Hub")
@@ -615,7 +684,7 @@ def main():
615
  # Canonical provenance stamp (see AGENTS.md): Jobs claim gated on JOB_ID, set by HF Jobs in-container.
616
  script_url = "https://huggingface.co/datasets/uv-scripts/embeddings/raw/main/generate-embeddings.py"
617
  on_jobs = os.environ.get("JOB_ID") is not None
618
- hw = os.environ.get("ACCELERATOR") or ""
619
  origin = (
620
  "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
621
  + (f" (`{hw}`)" if hw else "")
 
366
  logger.info(f"✅ streaming shard {args.shard_index + 1}/{args.num_shards} of run {args.run_id} uploaded")
367
 
368
 
369
+ def jobs_flavor() -> str:
370
+ """Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
371
+
372
+ HF Jobs sets ACCELERATOR to "cpu" or "gpu", not the flavor name, so the flavor is looked up
373
+ in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
374
+ MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
375
+ cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
376
+ """
377
+ hardware = os.environ.get("ACCELERATOR") or ""
378
+ if "-" in hardware or any(character.isdigit() for character in hardware):
379
+ return hardware # already a flavor name
380
+ try:
381
+ from huggingface_hub import HfApi
382
+
383
+ gpu_name, gpu_count = "", 0
384
+ try:
385
+ import torch
386
+
387
+ if torch.cuda.is_available():
388
+ gpu_name = torch.cuda.get_device_name(0)
389
+ gpu_count = torch.cuda.device_count()
390
+ except ImportError:
391
+ # Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
392
+ import subprocess
393
+
394
+ try:
395
+ listed = subprocess.run(
396
+ ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
397
+ capture_output=True, text=True, timeout=10, check=True,
398
+ ).stdout
399
+ names = [line.strip() for line in listed.splitlines() if line.strip()]
400
+ if names:
401
+ gpu_name, gpu_count = names[0], len(names)
402
+ except (OSError, subprocess.SubprocessError):
403
+ pass
404
+ if gpu_count == 0 and hardware == "gpu":
405
+ return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
406
+
407
+ # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
408
+ gpu_words = gpu_name.upper().replace("-", " ").split()
409
+ candidates = []
410
+ for flavor in HfApi(token=False).list_jobs_hardware():
411
+ accelerator = flavor.accelerator
412
+ if gpu_count == 0 and accelerator is None:
413
+ candidates.append(flavor)
414
+ elif (
415
+ gpu_count > 0
416
+ and accelerator is not None
417
+ and accelerator.model.upper() in gpu_words
418
+ and int(accelerator.quantity) == gpu_count
419
+ ):
420
+ candidates.append(flavor)
421
+ if not candidates:
422
+ return ""
423
+
424
+ cores = float(os.environ.get("CPU_CORES") or 0)
425
+ memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
426
+
427
+ def distance(flavor) -> float:
428
+ # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
429
+ listed_cores = float(flavor.cpu.split()[0])
430
+ listed_memory = float(flavor.ram.split()[0])
431
+ return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
432
+
433
+ return min(candidates, key=distance).name
434
+ except Exception:
435
+ return ""
436
+
437
+
438
  def main():
439
  p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
440
  p.add_argument("input_dataset", help="Input dataset ID on the Hugging Face Hub")
 
684
  # Canonical provenance stamp (see AGENTS.md): Jobs claim gated on JOB_ID, set by HF Jobs in-container.
685
  script_url = "https://huggingface.co/datasets/uv-scripts/embeddings/raw/main/generate-embeddings.py"
686
  on_jobs = os.environ.get("JOB_ID") is not None
687
+ hw = jobs_flavor() # flavor name, e.g. "a10g-small"; "" on CPU
688
  origin = (
689
  "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
690
  + (f" (`{hw}`)" if hw else "")