davanstrien HF Staff commited on
Commit
1c5581d
·
verified ·
1 Parent(s): b6859cb

Sync from GitHub via hub-sync

Browse files
Files changed (4) hide show
  1. classify-gliner2.py +62 -19
  2. train-classifier.py +70 -1
  3. train-gliner2.py +62 -19
  4. train-setfit.py +71 -3
classify-gliner2.py CHANGED
@@ -191,30 +191,73 @@ def label_counts_table(tasks: list, counts_by_task: dict, total: int) -> str:
191
  return "\n".join(lines)
192
 
193
 
194
- # The smallest Jobs flavor for each GPU, keyed by a fragment of the GPU's name. "L40" comes
195
- # before "L4" because the first match wins.
196
- GPU_NAME_TO_FLAVOR = {"T4": "t4-small", "A10G": "a10g-small", "L40": "l40sx1", "L4": "l4x1", "A100": "a100-large"}
197
-
198
-
199
  def jobs_flavor() -> str:
200
- """Return the Jobs hardware flavor, or "" when it is not known.
201
 
202
- The docs say ACCELERATOR holds the flavor ("a10g-small"). On the t4-small and a10g-small
203
- jobs that tested this script it held a bare "gpu", which is not a valid --flavor. So use
204
- ACCELERATOR when it looks like a flavor, and otherwise name the smallest flavor that has
205
- this GPU. A larger flavor of the same GPU reproduces the same result.
206
  """
207
  hardware = os.environ.get("ACCELERATOR") or ""
208
- looks_like_flavor = "-" in hardware or any(character.isdigit() for character in hardware)
209
- if looks_like_flavor:
210
- return hardware
211
- if not torch.cuda.is_available():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  return ""
213
- gpu_name = torch.cuda.get_device_name(0)
214
- for fragment, flavor in GPU_NAME_TO_FLAVOR.items():
215
- if fragment in gpu_name:
216
- return flavor
217
- return ""
218
 
219
 
220
  def build_reproduce_command(args) -> str:
 
191
  return "\n".join(lines)
192
 
193
 
 
 
 
 
 
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 ACCELERATOR to "cpu" or "gpu", not the flavor name, so the flavor is looked up
198
+ in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
199
+ MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
200
+ cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
201
  """
202
  hardware = os.environ.get("ACCELERATOR") or ""
203
+ if "-" in hardware or any(character.isdigit() for character in hardware):
204
+ return hardware # already a flavor name
205
+ try:
206
+ from huggingface_hub import HfApi
207
+
208
+ gpu_name, gpu_count = "", 0
209
+ try:
210
+ import torch
211
+
212
+ if torch.cuda.is_available():
213
+ gpu_name = torch.cuda.get_device_name(0)
214
+ gpu_count = torch.cuda.device_count()
215
+ except ImportError:
216
+ # Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
217
+ import subprocess
218
+
219
+ try:
220
+ listed = subprocess.run(
221
+ ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
222
+ capture_output=True, text=True, timeout=10, check=True,
223
+ ).stdout
224
+ names = [line.strip() for line in listed.splitlines() if line.strip()]
225
+ if names:
226
+ gpu_name, gpu_count = names[0], len(names)
227
+ except (OSError, subprocess.SubprocessError):
228
+ pass
229
+ if gpu_count == 0 and hardware == "gpu":
230
+ return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
231
+
232
+ # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
233
+ gpu_words = gpu_name.upper().replace("-", " ").split()
234
+ candidates = []
235
+ for flavor in HfApi(token=False).list_jobs_hardware():
236
+ accelerator = flavor.accelerator
237
+ if gpu_count == 0 and accelerator is None:
238
+ candidates.append(flavor)
239
+ elif (
240
+ gpu_count > 0
241
+ and accelerator is not None
242
+ and accelerator.model.upper() in gpu_words
243
+ and int(accelerator.quantity) == gpu_count
244
+ ):
245
+ candidates.append(flavor)
246
+ if not candidates:
247
+ return ""
248
+
249
+ cores = float(os.environ.get("CPU_CORES") or 0)
250
+ memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
251
+
252
+ def distance(flavor) -> float:
253
+ # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
254
+ listed_cores = float(flavor.cpu.split()[0])
255
+ listed_memory = float(flavor.ram.split()[0])
256
+ return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
257
+
258
+ return min(candidates, key=distance).name
259
+ except Exception:
260
  return ""
 
 
 
 
 
261
 
262
 
263
  def build_reproduce_command(args) -> str:
train-classifier.py CHANGED
@@ -424,12 +424,81 @@ def verify_reload(output_repo, eval_texts, reference_preds, problem_type, max_le
424
  # ---------------------------------------------------------------------------
425
 
426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
427
  def build_card(
428
  input_dataset, output_repo, model_id, problem_type, label_names, metrics,
429
  thresholds, path_kind, args_summary,
430
  ) -> str:
431
  on_jobs = os.environ.get("JOB_ID") is not None # set by HF Jobs in-container
432
- hw = os.environ.get("ACCELERATOR") or "" # e.g. "l4x1"; empty on CPU
433
  origin = (
434
  "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
435
  + (f" (`{hw}`)" if hw else "")
 
424
  # ---------------------------------------------------------------------------
425
 
426
 
427
+ def jobs_flavor() -> str:
428
+ """Return the HF Jobs hardware flavor this run is on, or "" when it cannot be told.
429
+
430
+ HF Jobs sets ACCELERATOR to "cpu" or "gpu", not the flavor name, so the flavor is looked up
431
+ in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
432
+ MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
433
+ cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
434
+ """
435
+ hardware = os.environ.get("ACCELERATOR") or ""
436
+ if "-" in hardware or any(character.isdigit() for character in hardware):
437
+ return hardware # already a flavor name
438
+ try:
439
+ from huggingface_hub import HfApi
440
+
441
+ gpu_name, gpu_count = "", 0
442
+ try:
443
+ import torch
444
+
445
+ if torch.cuda.is_available():
446
+ gpu_name = torch.cuda.get_device_name(0)
447
+ gpu_count = torch.cuda.device_count()
448
+ except ImportError:
449
+ # Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
450
+ import subprocess
451
+
452
+ try:
453
+ listed = subprocess.run(
454
+ ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
455
+ capture_output=True, text=True, timeout=10, check=True,
456
+ ).stdout
457
+ names = [line.strip() for line in listed.splitlines() if line.strip()]
458
+ if names:
459
+ gpu_name, gpu_count = names[0], len(names)
460
+ except (OSError, subprocess.SubprocessError):
461
+ pass
462
+ if gpu_count == 0 and hardware == "gpu":
463
+ return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
464
+
465
+ # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
466
+ gpu_words = gpu_name.upper().replace("-", " ").split()
467
+ candidates = []
468
+ for flavor in HfApi(token=False).list_jobs_hardware():
469
+ accelerator = flavor.accelerator
470
+ if gpu_count == 0 and accelerator is None:
471
+ candidates.append(flavor)
472
+ elif (
473
+ gpu_count > 0
474
+ and accelerator is not None
475
+ and accelerator.model.upper() in gpu_words
476
+ and int(accelerator.quantity) == gpu_count
477
+ ):
478
+ candidates.append(flavor)
479
+ if not candidates:
480
+ return ""
481
+
482
+ cores = float(os.environ.get("CPU_CORES") or 0)
483
+ memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
484
+
485
+ def distance(flavor) -> float:
486
+ # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
487
+ listed_cores = float(flavor.cpu.split()[0])
488
+ listed_memory = float(flavor.ram.split()[0])
489
+ return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
490
+
491
+ return min(candidates, key=distance).name
492
+ except Exception:
493
+ return ""
494
+
495
+
496
  def build_card(
497
  input_dataset, output_repo, model_id, problem_type, label_names, metrics,
498
  thresholds, path_kind, args_summary,
499
  ) -> str:
500
  on_jobs = os.environ.get("JOB_ID") is not None # set by HF Jobs in-container
501
+ hw = jobs_flavor() # flavor name, e.g. "a10g-small"; "" on CPU
502
  origin = (
503
  "Produced on [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs)"
504
  + (f" (`{hw}`)" if hw else "")
train-gliner2.py CHANGED
@@ -748,30 +748,73 @@ def evaluate(model_path: str, eval_sets: dict, tasks: list, batch_size: int, exp
748
  return metrics_by_split
749
 
750
 
751
- # The smallest Jobs flavor for each GPU, keyed by a fragment of the GPU's name. "L40" comes
752
- # before "L4" because the first match wins.
753
- GPU_NAME_TO_FLAVOR = {"T4": "t4-small", "A10G": "a10g-small", "L40": "l40sx1", "L4": "l4x1", "A100": "a100-large"}
754
-
755
-
756
  def jobs_flavor() -> str:
757
- """Return the Jobs hardware flavor, or "" when it is not known.
758
 
759
- The docs say ACCELERATOR holds the flavor ("a10g-small"). On the t4-small and a10g-small
760
- jobs that tested this script it held a bare "gpu", which is not a valid --flavor. So use
761
- ACCELERATOR when it looks like a flavor, and otherwise name the smallest flavor that has
762
- this GPU. A larger flavor of the same GPU reproduces the same result.
763
  """
764
  hardware = os.environ.get("ACCELERATOR") or ""
765
- looks_like_flavor = "-" in hardware or any(character.isdigit() for character in hardware)
766
- if looks_like_flavor:
767
- return hardware
768
- if not torch.cuda.is_available():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
769
  return ""
770
- gpu_name = torch.cuda.get_device_name(0)
771
- for fragment, flavor in GPU_NAME_TO_FLAVOR.items():
772
- if fragment in gpu_name:
773
- return flavor
774
- return ""
775
 
776
 
777
  def build_reproduce_command(args) -> str:
 
748
  return metrics_by_split
749
 
750
 
 
 
 
 
 
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 ACCELERATOR to "cpu" or "gpu", not the flavor name, so the flavor is looked up
755
+ in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
756
+ MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
757
+ cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
758
  """
759
  hardware = os.environ.get("ACCELERATOR") or ""
760
+ if "-" in hardware or any(character.isdigit() for character in hardware):
761
+ return hardware # already a flavor name
762
+ try:
763
+ from huggingface_hub import HfApi
764
+
765
+ gpu_name, gpu_count = "", 0
766
+ try:
767
+ import torch
768
+
769
+ if torch.cuda.is_available():
770
+ gpu_name = torch.cuda.get_device_name(0)
771
+ gpu_count = torch.cuda.device_count()
772
+ except ImportError:
773
+ # Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
774
+ import subprocess
775
+
776
+ try:
777
+ listed = subprocess.run(
778
+ ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
779
+ capture_output=True, text=True, timeout=10, check=True,
780
+ ).stdout
781
+ names = [line.strip() for line in listed.splitlines() if line.strip()]
782
+ if names:
783
+ gpu_name, gpu_count = names[0], len(names)
784
+ except (OSError, subprocess.SubprocessError):
785
+ pass
786
+ if gpu_count == 0 and hardware == "gpu":
787
+ return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
788
+
789
+ # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
790
+ gpu_words = gpu_name.upper().replace("-", " ").split()
791
+ candidates = []
792
+ for flavor in HfApi(token=False).list_jobs_hardware():
793
+ accelerator = flavor.accelerator
794
+ if gpu_count == 0 and accelerator is None:
795
+ candidates.append(flavor)
796
+ elif (
797
+ gpu_count > 0
798
+ and accelerator is not None
799
+ and accelerator.model.upper() in gpu_words
800
+ and int(accelerator.quantity) == gpu_count
801
+ ):
802
+ candidates.append(flavor)
803
+ if not candidates:
804
+ return ""
805
+
806
+ cores = float(os.environ.get("CPU_CORES") or 0)
807
+ memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
808
+
809
+ def distance(flavor) -> float:
810
+ # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
811
+ listed_cores = float(flavor.cpu.split()[0])
812
+ listed_memory = float(flavor.ram.split()[0])
813
+ return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
814
+
815
+ return min(candidates, key=distance).name
816
+ except Exception:
817
  return ""
 
 
 
 
 
818
 
819
 
820
  def build_reproduce_command(args) -> str:
train-setfit.py CHANGED
@@ -480,6 +480,75 @@ def estimate_training_steps(per_class, batch_size, num_epochs, strategy) -> tupl
480
  return pairs, steps
481
 
482
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483
  def build_reproduce_command(args) -> str:
484
  """Rebuild recipe options for Jobs, preserving the recorded accelerator when available.
485
 
@@ -487,9 +556,8 @@ def build_reproduce_command(args) -> str:
487
  Outside Jobs, the hardware flavor is a suggested default rather than an exact record.
488
  """
489
  flavor = "t4-small" if torch.cuda.is_available() else "cpu-basic"
490
- accelerator = os.environ.get("ACCELERATOR", "").strip()
491
- if os.environ.get("JOB_ID") and accelerator.lower() not in ("", "none"):
492
- flavor = accelerator
493
  parts = [
494
  f"hf jobs uv run --flavor {flavor} --secrets HF_TOKEN \\",
495
  f" {SCRIPT_URL} \\",
 
480
  return pairs, steps
481
 
482
 
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 ACCELERATOR to "cpu" or "gpu", not the flavor name, so the flavor is looked up
487
+ in the public Jobs hardware list: first by GPU model and count, then by the CPU_CORES and
488
+ MEMORY values that Jobs sets in the container (to tell a10g-small from a10g-large, or
489
+ cpu-basic from cpu-upgrade). Any failure (no network, older huggingface_hub) returns "".
490
+ """
491
+ hardware = os.environ.get("ACCELERATOR") or ""
492
+ if "-" in hardware or any(character.isdigit() for character in hardware):
493
+ return hardware # already a flavor name
494
+ try:
495
+ from huggingface_hub import HfApi
496
+
497
+ gpu_name, gpu_count = "", 0
498
+ try:
499
+ import torch
500
+
501
+ if torch.cuda.is_available():
502
+ gpu_name = torch.cuda.get_device_name(0)
503
+ gpu_count = torch.cuda.device_count()
504
+ except ImportError:
505
+ # Torch-free drivers (e.g. the -server recipes): ask nvidia-smi instead.
506
+ import subprocess
507
+
508
+ try:
509
+ listed = subprocess.run(
510
+ ["nvidia-smi", "--query-gpu=name", "--format=csv,noheader"],
511
+ capture_output=True, text=True, timeout=10, check=True,
512
+ ).stdout
513
+ names = [line.strip() for line in listed.splitlines() if line.strip()]
514
+ if names:
515
+ gpu_name, gpu_count = names[0], len(names)
516
+ except (OSError, subprocess.SubprocessError):
517
+ pass
518
+ if gpu_count == 0 and hardware == "gpu":
519
+ return "" # a GPU run whose GPU cannot be identified: do not guess a CPU flavor
520
+
521
+ # Whole-word match, so "L4" does not match "NVIDIA L40S" ("NVIDIA A100-SXM4-80GB" -> A100).
522
+ gpu_words = gpu_name.upper().replace("-", " ").split()
523
+ candidates = []
524
+ for flavor in HfApi(token=False).list_jobs_hardware():
525
+ accelerator = flavor.accelerator
526
+ if gpu_count == 0 and accelerator is None:
527
+ candidates.append(flavor)
528
+ elif (
529
+ gpu_count > 0
530
+ and accelerator is not None
531
+ and accelerator.model.upper() in gpu_words
532
+ and int(accelerator.quantity) == gpu_count
533
+ ):
534
+ candidates.append(flavor)
535
+ if not candidates:
536
+ return ""
537
+
538
+ cores = float(os.environ.get("CPU_CORES") or 0)
539
+ memory_gb = float((os.environ.get("MEMORY") or "0").upper().rstrip("GIB ") or 0)
540
+
541
+ def distance(flavor) -> float:
542
+ # Relative gap to the listed vCPUs and RAM; Jobs reports slightly different numbers.
543
+ listed_cores = float(flavor.cpu.split()[0])
544
+ listed_memory = float(flavor.ram.split()[0])
545
+ return abs(listed_cores - cores) / listed_cores + abs(listed_memory - memory_gb) / listed_memory
546
+
547
+ return min(candidates, key=distance).name
548
+ except Exception:
549
+ return ""
550
+
551
+
552
  def build_reproduce_command(args) -> str:
553
  """Rebuild recipe options for Jobs, preserving the recorded accelerator when available.
554
 
 
556
  Outside Jobs, the hardware flavor is a suggested default rather than an exact record.
557
  """
558
  flavor = "t4-small" if torch.cuda.is_available() else "cpu-basic"
559
+ if os.environ.get("JOB_ID"):
560
+ flavor = jobs_flavor() or flavor
 
561
  parts = [
562
  f"hf jobs uv run --flavor {flavor} --secrets HF_TOKEN \\",
563
  f" {SCRIPT_URL} \\",