davanstrien HF Staff commited on
Commit
83e0ded
·
verified ·
1 Parent(s): 3688d57

Sync from GitHub via hub-sync

Browse files
Files changed (1) hide show
  1. falcon-perception.py +71 -2
falcon-perception.py CHANGED
@@ -230,14 +230,83 @@ def batched(it, n):
230
  SCRIPT_URL = "https://huggingface.co/datasets/uv-scripts/object-detection/raw/main/falcon-perception.py"
231
 
232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  def push_card(repo_id, query, counters):
234
  """Dataset card with the repo's canonical provenance stamp (see AGENTS.md)."""
235
  from huggingface_hub import DatasetCard
236
 
237
  on_jobs = os.environ.get("JOB_ID") is not None # set by HF Jobs in-container
238
- hw = os.environ.get("ACCELERATOR") or "" # e.g. "a10g-large"; empty on CPU
239
  origin = (
240
- f"Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
241
  + (f" (`{hw}`)" if hw else "")
242
  ) if on_jobs else "Generated"
243
  tags = "\n".join(f"- {t}" for t in (["uv-script", "hf-jobs"] if on_jobs else ["uv-script"]))
 
230
  SCRIPT_URL = "https://huggingface.co/datasets/uv-scripts/object-detection/raw/main/falcon-perception.py"
231
 
232
 
233
+ def jobs_flavor() -> str:
234
+ """Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
235
+
236
+ HF Jobs sets ACCELERATOR to "cpu" or "gpu", not the flavor name, so the flavor is looked up
237
+ in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
238
+ MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
239
+ cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
240
+ """
241
+ hardware = os.environ.get("ACCELERATOR") or ""
242
+ if "-" in hardware or any(character.isdigit() for character in hardware):
243
+ return hardware # already a flavor name
244
+ try:
245
+ from huggingface_hub import HfApi
246
+
247
+ gpu_name, gpu_count = "", 0
248
+ try:
249
+ import torch
250
+
251
+ if torch.cuda.is_available():
252
+ gpu_name = torch.cuda.get_device_name(0)
253
+ gpu_count = torch.cuda.device_count()
254
+ except ImportError:
255
+ # Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
256
+ import subprocess
257
+
258
+ try:
259
+ listed = subprocess.run(
260
+ ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
261
+ capture_output=True, text=True, timeout=10, check=True,
262
+ ).stdout
263
+ names = [line.strip() for line in listed.splitlines() if line.strip()]
264
+ if names:
265
+ gpu_name, gpu_count = names[0], len(names)
266
+ except (OSError, subprocess.SubprocessError):
267
+ pass
268
+ if gpu_count == 0 and hardware == "gpu":
269
+ return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
270
+
271
+ # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
272
+ gpu_words = gpu_name.upper().replace("-", " ").split()
273
+ candidates = []
274
+ for flavor in HfApi(token=False).list_jobs_hardware():
275
+ accelerator = flavor.accelerator
276
+ if gpu_count == 0 and accelerator is None:
277
+ candidates.append(flavor)
278
+ elif (
279
+ gpu_count > 0
280
+ and accelerator is not None
281
+ and accelerator.model.upper() in gpu_words
282
+ and int(accelerator.quantity) == gpu_count
283
+ ):
284
+ candidates.append(flavor)
285
+ if not candidates:
286
+ return ""
287
+
288
+ cores = float(os.environ.get("CPU_CORES") or 0)
289
+ memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
290
+
291
+ def distance(flavor) -> float:
292
+ # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
293
+ listed_cores = float(flavor.cpu.split()[0])
294
+ listed_memory = float(flavor.ram.split()[0])
295
+ return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
296
+
297
+ return min(candidates, key=distance).name
298
+ except Exception:
299
+ return ""
300
+
301
+
302
  def push_card(repo_id, query, counters):
303
  """Dataset card with the repo's canonical provenance stamp (see AGENTS.md)."""
304
  from huggingface_hub import DatasetCard
305
 
306
  on_jobs = os.environ.get("JOB_ID") is not None # set by HF Jobs in-container
307
+ hw = jobs_flavor() # flavor name, e.g. "a10g-small"; "" on CPU
308
  origin = (
309
+ "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
310
  + (f" (`{hw}`)" if hw else "")
311
  ) if on_jobs else "Generated"
312
  tags = "\n".join(f"- {t}" for t in (["uv-script", "hf-jobs"] if on_jobs else ["uv-script"]))