davanstrien HF Staff commited on
Commit
1b2e556
·
verified ·
1 Parent(s): def192a

Sync from davanstrien/uv-scripts via hub-sync

Browse files
Files changed (1) hide show
  1. wevisone-detect.py +1 -64
wevisone-detect.py CHANGED
@@ -237,69 +237,6 @@ def merge_cross_class_duplicates(
237
  return kept, merged
238
 
239
 
240
- def jobs_flavor() -> str:
241
- """Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
242
-
243
- HF Jobs sets HF_JOB_FLAVOR to the flavor name. Without it (ACCELERATOR holds only
244
- "cpu" or "gpu", not the flavor name), the flavor is looked up
245
- in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
246
- MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
247
- cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
248
- """
249
- if os.environ.get("HF_JOB_FLAVOR"):
250
- return os.environ["HF_JOB_FLAVOR"]
251
- hardware = os.environ.get("ACCELERATOR") or ""
252
- if "-" in hardware or any(character.isdigit() for character in hardware):
253
- return hardware # already a flavor name
254
- try:
255
- from huggingface_hub import HfApi
256
-
257
- gpu_name, gpu_count = "", 0
258
- try:
259
- import torch
260
-
261
- if torch.cuda.is_available():
262
- gpu_name = torch.cuda.get_device_name(0)
263
- gpu_count = torch.cuda.device_count()
264
- except ImportError:
265
- pass
266
- if gpu_count == 0 and hardware == "gpu":
267
- return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
268
-
269
- # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
270
- gpu_words = gpu_name.upper().replace("-", " ").split()
271
- candidates = []
272
- for flavor in HfApi(token=False).list_jobs_hardware():
273
- accelerator = flavor.accelerator
274
- if gpu_count == 0 and accelerator is None:
275
- candidates.append(flavor)
276
- elif (
277
- gpu_count > 0
278
- and accelerator is not None
279
- and accelerator.model.upper() in gpu_words
280
- and int(accelerator.quantity) == gpu_count
281
- ):
282
- candidates.append(flavor)
283
- if not candidates:
284
- return ""
285
-
286
- cores = float(os.environ.get("CPU_CORES") or 0)
287
- memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
288
-
289
- def distance(flavor) -> float:
290
- # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
291
- listed_cores = float(flavor.cpu.split()[0])
292
- listed_memory = float(flavor.ram.split()[0])
293
- return (
294
- abs(listed_cores - cores) / listed_cores
295
- + abs(listed_memory - memory_gb) / listed_memory
296
- )
297
-
298
- return min(candidates, key=distance).name
299
- except Exception:
300
- return ""
301
-
302
-
303
  def push_card(
304
  args: argparse.Namespace, classes: list[str], stats: dict[str, Any]
305
  ) -> None:
@@ -309,7 +246,7 @@ def push_card(
309
  is kept; only the tags are extended and the body replaced.
310
  """
311
  on_jobs = os.environ.get("HF_JOB_ID") is not None # set by HF Jobs in-container
312
- hw = jobs_flavor() # flavor name, e.g. "l4x1"; "" on CPU
313
  origin = (
314
  (
315
  "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
 
237
  return kept, merged
238
 
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  def push_card(
241
  args: argparse.Namespace, classes: list[str], stats: dict[str, Any]
242
  ) -> None:
 
246
  is kept; only the tags are extended and the body replaced.
247
  """
248
  on_jobs = os.environ.get("HF_JOB_ID") is not None # set by HF Jobs in-container
249
+ hw = os.environ.get("HF_JOB_FLAVOR", "") # set by HF Jobs, e.g. "a10g-small"; "" outside Jobs
250
  origin = (
251
  (
252
  "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"