Spaces:
Running on Zero
Running on Zero
Upload 80 files
Browse files- obliteratus/models/loader.py +14 -7
obliteratus/models/loader.py
CHANGED
|
@@ -6,7 +6,6 @@ import copy
|
|
| 6 |
import logging
|
| 7 |
import tempfile
|
| 8 |
from dataclasses import dataclass, field
|
| 9 |
-
from pathlib import Path
|
| 10 |
from typing import Optional
|
| 11 |
|
| 12 |
import torch
|
|
@@ -103,7 +102,7 @@ def _available_gpu_memory_gb() -> float:
|
|
| 103 |
total = 0.0
|
| 104 |
for i in range(torch.cuda.device_count()):
|
| 105 |
props = torch.cuda.get_device_properties(i)
|
| 106 |
-
total += props.
|
| 107 |
return total
|
| 108 |
|
| 109 |
|
|
@@ -116,7 +115,7 @@ def load_model(
|
|
| 116 |
num_labels: int = 2,
|
| 117 |
quantization: str | None = None,
|
| 118 |
offload_folder: str | None = None,
|
| 119 |
-
skip_snapshot: bool =
|
| 120 |
) -> ModelHandle:
|
| 121 |
"""Load a HuggingFace model and tokenizer, returning a ModelHandle.
|
| 122 |
|
|
@@ -130,7 +129,10 @@ def load_model(
|
|
| 130 |
quantization: None, "4bit", or "8bit". Requires bitsandbytes.
|
| 131 |
offload_folder: Directory for disk offloading when model exceeds GPU memory.
|
| 132 |
If None and offloading is needed, a temp directory is created automatically.
|
| 133 |
-
skip_snapshot:
|
|
|
|
|
|
|
|
|
|
| 134 |
"""
|
| 135 |
if task not in TASK_MODEL_MAP:
|
| 136 |
raise ValueError(f"Unknown task {task!r}. Choose from {list(TASK_MODEL_MAP)}")
|
|
@@ -215,10 +217,15 @@ def load_model(
|
|
| 215 |
)
|
| 216 |
|
| 217 |
# Skip snapshot for large models to avoid doubling memory usage
|
| 218 |
-
if
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
logger.warning(
|
| 221 |
-
f"
|
| 222 |
f"(model ~{est_gb:.0f} GB vs GPU {gpu_gb:.0f} GB). "
|
| 223 |
f"Use skip_snapshot=False to force."
|
| 224 |
)
|
|
|
|
| 6 |
import logging
|
| 7 |
import tempfile
|
| 8 |
from dataclasses import dataclass, field
|
|
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
import torch
|
|
|
|
| 102 |
total = 0.0
|
| 103 |
for i in range(torch.cuda.device_count()):
|
| 104 |
props = torch.cuda.get_device_properties(i)
|
| 105 |
+
total += props.total_memory / (1024 ** 3)
|
| 106 |
return total
|
| 107 |
|
| 108 |
|
|
|
|
| 115 |
num_labels: int = 2,
|
| 116 |
quantization: str | None = None,
|
| 117 |
offload_folder: str | None = None,
|
| 118 |
+
skip_snapshot: bool | None = None,
|
| 119 |
) -> ModelHandle:
|
| 120 |
"""Load a HuggingFace model and tokenizer, returning a ModelHandle.
|
| 121 |
|
|
|
|
| 129 |
quantization: None, "4bit", or "8bit". Requires bitsandbytes.
|
| 130 |
offload_folder: Directory for disk offloading when model exceeds GPU memory.
|
| 131 |
If None and offloading is needed, a temp directory is created automatically.
|
| 132 |
+
skip_snapshot: Controls initial state dict snapshot.
|
| 133 |
+
None (default): auto-decide based on GPU memory headroom.
|
| 134 |
+
True: always skip (saves memory).
|
| 135 |
+
False: always snapshot (force even for large models).
|
| 136 |
"""
|
| 137 |
if task not in TASK_MODEL_MAP:
|
| 138 |
raise ValueError(f"Unknown task {task!r}. Choose from {list(TASK_MODEL_MAP)}")
|
|
|
|
| 217 |
)
|
| 218 |
|
| 219 |
# Skip snapshot for large models to avoid doubling memory usage
|
| 220 |
+
if skip_snapshot is True:
|
| 221 |
+
pass # user explicitly opted out
|
| 222 |
+
elif skip_snapshot is False:
|
| 223 |
+
handle.snapshot() # user explicitly forced snapshot
|
| 224 |
+
else:
|
| 225 |
+
# Auto-decide: skip only when GPU is present and model is tight on memory
|
| 226 |
+
if gpu_gb > 0 and est_gb > 0 and est_gb > gpu_gb * 0.5:
|
| 227 |
logger.warning(
|
| 228 |
+
f"Auto-skipping state dict snapshot to save memory "
|
| 229 |
f"(model ~{est_gb:.0f} GB vs GPU {gpu_gb:.0f} GB). "
|
| 230 |
f"Use skip_snapshot=False to force."
|
| 231 |
)
|