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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (C) 2025 Arcee AI
|
| 2 |
+
# SPDX-License-Identifier: LGPL-3.0-only
|
| 3 |
+
"""
|
| 4 |
+
Module for computational graph execution.
|
| 5 |
+
|
| 6 |
+
Classes:
|
| 7 |
+
Task: Abstract base class representing a computational task.
|
| 8 |
+
Executor: Class for scheduling and executing directed acyclic task graphs.
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import sys
|
| 13 |
+
import gc
|
| 14 |
+
import logging
|
| 15 |
+
import networkx
|
| 16 |
+
import torch
|
| 17 |
+
import tqdm
|
| 18 |
+
from pydantic import BaseModel
|
| 19 |
+
from typing_extensions import Generic, TypeVar
|
| 20 |
+
from abc import ABC, abstractmethod
|
| 21 |
+
from typing import Any, Dict, Iterable, Iterator, List, Optional, Tuple, Union
|
| 22 |
+
|
| 23 |
+
from mergekit.common import get_torch_accelerator_module
|
| 24 |
+
|
| 25 |
+
# Windows/NVIDIA specific allocator tuning to reduce fragmentation
|
| 26 |
+
if sys.platform == "win32":
|
| 27 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:32"
|
| 28 |
+
|
| 29 |
+
ValueT = TypeVar("ValueT")
|
| 30 |
+
LOG = logging.getLogger(__name__)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class Task(ABC, BaseModel, Generic[ValueT], frozen=True):
|
| 34 |
+
@abstractmethod
|
| 35 |
+
def arguments(self) -> Dict[str, "Task"]:
|
| 36 |
+
...
|
| 37 |
+
|
| 38 |
+
@abstractmethod
|
| 39 |
+
def execute(self, **kwargs) -> ValueT:
|
| 40 |
+
...
|
| 41 |
+
|
| 42 |
+
def priority(self) -> int:
|
| 43 |
+
return 0
|
| 44 |
+
|
| 45 |
+
def group_label(self) -> Optional[str]:
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
def uses_accelerator(self) -> bool:
|
| 49 |
+
return False
|
| 50 |
+
|
| 51 |
+
def main_thread_only(self) -> bool:
|
| 52 |
+
return False
|
| 53 |
+
|
| 54 |
+
def duplicate_per_gpu(self) -> bool:
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class TaskUniverse:
|
| 59 |
+
tasks: List[Task]
|
| 60 |
+
task_to_index: Dict[Task, int]
|
| 61 |
+
task_arguments: Dict[int, Dict[str, int]]
|
| 62 |
+
_type_id_to_index: Dict[Tuple[type, int], int]
|
| 63 |
+
|
| 64 |
+
def __init__(self, tasks: Optional[Iterable[Task]] = None):
|
| 65 |
+
self.tasks = []
|
| 66 |
+
self.task_to_index = {}
|
| 67 |
+
self.task_arguments = {}
|
| 68 |
+
self._type_id_to_index = {}
|
| 69 |
+
if tasks is not None:
|
| 70 |
+
for task in tasks:
|
| 71 |
+
self.add_task(task)
|
| 72 |
+
|
| 73 |
+
def add_task(self, task: Task, recursive: bool = True) -> "TaskHandle":
|
| 74 |
+
_ti_key = (type(task), id(task))
|
| 75 |
+
if _ti_key in self._type_id_to_index:
|
| 76 |
+
index = self._type_id_to_index[_ti_key]
|
| 77 |
+
return TaskHandle(self, index)
|
| 78 |
+
|
| 79 |
+
index = self.task_to_index.setdefault(task, len(self.tasks))
|
| 80 |
+
if index < len(self.tasks):
|
| 81 |
+
return TaskHandle(self, index)
|
| 82 |
+
self.tasks.append(task)
|
| 83 |
+
self._type_id_to_index[_ti_key] = index
|
| 84 |
+
|
| 85 |
+
if recursive:
|
| 86 |
+
self.task_arguments[index] = {}
|
| 87 |
+
for k, v in task.arguments().items():
|
| 88 |
+
self.task_arguments[index][k] = self.add_task(v, recursive=True)._index
|
| 89 |
+
return TaskHandle(self, index)
|
| 90 |
+
|
| 91 |
+
def get_handle(self, task: Task) -> Optional["TaskHandle"]:
|
| 92 |
+
if task not in self.task_to_index:
|
| 93 |
+
return None
|
| 94 |
+
return TaskHandle(self, self.task_to_index[task])
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
class TaskHandle:
|
| 98 |
+
__slots__ = ["_universe", "_index"]
|
| 99 |
+
_universe: TaskUniverse
|
| 100 |
+
_index: int
|
| 101 |
+
|
| 102 |
+
def __init__(self, universe: TaskUniverse, index: int):
|
| 103 |
+
self._universe = universe
|
| 104 |
+
self._index = index
|
| 105 |
+
|
| 106 |
+
def task(self) -> Task:
|
| 107 |
+
return self._universe.tasks[self._index]
|
| 108 |
+
|
| 109 |
+
def arguments(self) -> Dict[str, "TaskHandle"]:
|
| 110 |
+
return {
|
| 111 |
+
k: TaskHandle(self._universe, v)
|
| 112 |
+
for k, v in self._universe.task_arguments[self._index].items()
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
def __eq__(self, other):
|
| 116 |
+
if not isinstance(other, TaskHandle):
|
| 117 |
+
return False
|
| 118 |
+
return self._index == other._index and self._universe is other._universe
|
| 119 |
+
|
| 120 |
+
def __hash__(self):
|
| 121 |
+
return self._index
|
| 122 |
+
|
| 123 |
+
def __str__(self):
|
| 124 |
+
return f"TaskHandle({type(self.task()).__name__}, {self._index})"
|
| 125 |
+
|
| 126 |
+
__repr__ = __str__
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
class ExecutionSchedule:
|
| 130 |
+
tasks: List[TaskHandle]
|
| 131 |
+
last_use_index: Dict[TaskHandle, int]
|
| 132 |
+
|
| 133 |
+
def __init__(self, tasks: List[TaskHandle], last_use_index: Dict[TaskHandle, int]):
|
| 134 |
+
self.tasks = tasks
|
| 135 |
+
self.last_use_index = last_use_index
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def build_schedule(
|
| 139 |
+
targets: List[TaskHandle], cached_values: Dict[TaskHandle, Any]
|
| 140 |
+
) -> ExecutionSchedule:
|
| 141 |
+
if not targets:
|
| 142 |
+
return ExecutionSchedule(tasks=[], last_use_index={})
|
| 143 |
+
|
| 144 |
+
universe = targets[0]._universe
|
| 145 |
+
dummy_handle = TaskHandle(universe, -1)
|
| 146 |
+
edge_tups: List[Tuple[TaskHandle, TaskHandle]] = []
|
| 147 |
+
|
| 148 |
+
explored = set()
|
| 149 |
+
to_explore = set(targets)
|
| 150 |
+
while to_explore:
|
| 151 |
+
task = to_explore.pop()
|
| 152 |
+
if task in explored:
|
| 153 |
+
continue
|
| 154 |
+
explored.add(task)
|
| 155 |
+
if task in (cached_values or {}):
|
| 156 |
+
continue
|
| 157 |
+
for dep in task.arguments().values():
|
| 158 |
+
to_explore.add(dep)
|
| 159 |
+
edge_tups.append((dep, task))
|
| 160 |
+
|
| 161 |
+
for target in targets:
|
| 162 |
+
edge_tups.append((dummy_handle, target))
|
| 163 |
+
|
| 164 |
+
def _compare_key(node: TaskHandle) -> Tuple[str, int]:
|
| 165 |
+
if node._index < 0:
|
| 166 |
+
return ("", 0)
|
| 167 |
+
task = node.task()
|
| 168 |
+
return (task.group_label() or "", -task.priority())
|
| 169 |
+
|
| 170 |
+
graph = networkx.DiGraph(edge_tups)
|
| 171 |
+
schedule: List[TaskHandle] = [
|
| 172 |
+
node
|
| 173 |
+
for node in networkx.lexicographical_topological_sort(graph, key=_compare_key)
|
| 174 |
+
if (node != dummy_handle) and node not in (cached_values or {})
|
| 175 |
+
]
|
| 176 |
+
|
| 177 |
+
last_use_index = {}
|
| 178 |
+
for idx, task in reversed(list(enumerate(schedule))):
|
| 179 |
+
for dep in task.arguments().values():
|
| 180 |
+
if dep not in last_use_index:
|
| 181 |
+
last_use_index[dep] = idx
|
| 182 |
+
if task not in last_use_index:
|
| 183 |
+
last_use_index[task] = idx
|
| 184 |
+
for task in cached_values or {}:
|
| 185 |
+
if task not in last_use_index:
|
| 186 |
+
last_use_index[task] = len(schedule) + 1
|
| 187 |
+
|
| 188 |
+
return ExecutionSchedule(tasks=schedule, last_use_index=last_use_index)
|
| 189 |
+
|
| 190 |
+
|
| 191 |
+
class Executor:
|
| 192 |
+
math_device: torch.device
|
| 193 |
+
storage_device: torch.device
|
| 194 |
+
universe: TaskUniverse
|
| 195 |
+
targets: List[TaskHandle]
|
| 196 |
+
schedule: ExecutionSchedule
|
| 197 |
+
cached_values: Optional[Dict[TaskHandle, Any]]
|
| 198 |
+
|
| 199 |
+
def __init__(
|
| 200 |
+
self,
|
| 201 |
+
targets: Union[List[Task], List[TaskHandle]],
|
| 202 |
+
math_device: torch.device = torch.device("cpu"),
|
| 203 |
+
storage_device: torch.device = torch.device("cpu"),
|
| 204 |
+
cached_values: Optional[Dict[TaskHandle, Any]] = None,
|
| 205 |
+
):
|
| 206 |
+
self.cached_values = cached_values
|
| 207 |
+
if isinstance(math_device, str):
|
| 208 |
+
math_device = torch.device(math_device)
|
| 209 |
+
if isinstance(storage_device, str):
|
| 210 |
+
storage_device = torch.device(storage_device)
|
| 211 |
+
self.math_device = math_device
|
| 212 |
+
self.storage_device = storage_device
|
| 213 |
+
|
| 214 |
+
if targets and isinstance(targets[0], Task):
|
| 215 |
+
universe = TaskUniverse(targets)
|
| 216 |
+
targets = [universe.add_task(t) for t in targets]
|
| 217 |
+
elif targets and isinstance(targets[0], TaskHandle):
|
| 218 |
+
universe = targets[0]._universe
|
| 219 |
+
elif not targets:
|
| 220 |
+
universe = TaskUniverse()
|
| 221 |
+
else:
|
| 222 |
+
raise ValueError("Targets must be a list of Task or TaskHandle instances")
|
| 223 |
+
|
| 224 |
+
self.universe = universe
|
| 225 |
+
self.targets = targets
|
| 226 |
+
self.schedule = build_schedule(targets, cached_values=cached_values)
|
| 227 |
+
|
| 228 |
+
def _slice_argument(self, arg: Any, start: int, end: int) -> Any:
|
| 229 |
+
"""Helper to slice tensors within nested structures."""
|
| 230 |
+
if isinstance(arg, torch.Tensor):
|
| 231 |
+
# Only slice if the dimension is large enough
|
| 232 |
+
if arg.shape[0] > 1:
|
| 233 |
+
return arg[start:end]
|
| 234 |
+
return arg
|
| 235 |
+
elif isinstance(arg, dict):
|
| 236 |
+
return {k: self._slice_argument(v, start, end) for k, v in arg.items()}
|
| 237 |
+
elif isinstance(arg, list):
|
| 238 |
+
return [self._slice_argument(v, start, end) for v in arg]
|
| 239 |
+
elif isinstance(arg, tuple):
|
| 240 |
+
return tuple(self._slice_argument(v, start, end) for v in arg)
|
| 241 |
+
return arg
|
| 242 |
+
|
| 243 |
+
def _execute_chunked(self, task: Task, arguments: Dict[str, Any], chunk_size: int) -> Any:
|
| 244 |
+
"""
|
| 245 |
+
Executes a task by splitting input tensors into chunks, processing on GPU,
|
| 246 |
+
and concatenating results on CPU.
|
| 247 |
+
"""
|
| 248 |
+
# Find a reference tensor to determine batch size
|
| 249 |
+
ref_tensor = None
|
| 250 |
+
for arg in arguments.values():
|
| 251 |
+
if isinstance(arg, torch.Tensor):
|
| 252 |
+
ref_tensor = arg
|
| 253 |
+
break
|
| 254 |
+
elif isinstance(arg, dict):
|
| 255 |
+
for v in arg.values():
|
| 256 |
+
if isinstance(v, torch.Tensor):
|
| 257 |
+
ref_tensor = v
|
| 258 |
+
break
|
| 259 |
+
if ref_tensor is not None: break
|
| 260 |
+
|
| 261 |
+
if ref_tensor is None:
|
| 262 |
+
raise ValueError("No tensors found to chunk")
|
| 263 |
+
|
| 264 |
+
total_rows = ref_tensor.shape[0]
|
| 265 |
+
results = []
|
| 266 |
+
|
| 267 |
+
accelerator = get_torch_accelerator_module(self.math_device.type) if self.math_device.type != "cpu" else None
|
| 268 |
+
|
| 269 |
+
# Process in chunks
|
| 270 |
+
for i in range(0, total_rows, chunk_size):
|
| 271 |
+
end = min(i + chunk_size, total_rows)
|
| 272 |
+
|
| 273 |
+
# Slice inputs
|
| 274 |
+
chunk_args = {
|
| 275 |
+
k: self._slice_argument(v, i, end)
|
| 276 |
+
for k, v in arguments.items()
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
# Move chunk inputs to GPU
|
| 280 |
+
chunk_args_gpu = {
|
| 281 |
+
k: self._move_tensors(v, self.math_device)
|
| 282 |
+
for k, v in chunk_args.items()
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
# Execute
|
| 286 |
+
chunk_res = task.execute(**chunk_args_gpu)
|
| 287 |
+
|
| 288 |
+
# Move result to CPU immediately
|
| 289 |
+
chunk_res_cpu = self._move_tensors(chunk_res, self.storage_device)
|
| 290 |
+
results.append(chunk_res_cpu)
|
| 291 |
+
|
| 292 |
+
# Cleanup
|
| 293 |
+
del chunk_args
|
| 294 |
+
del chunk_args_gpu
|
| 295 |
+
del chunk_res
|
| 296 |
+
|
| 297 |
+
# Clear cache inside loop to handle complex methods like Magic
|
| 298 |
+
if accelerator:
|
| 299 |
+
accelerator.empty_cache()
|
| 300 |
+
|
| 301 |
+
# Concatenate results
|
| 302 |
+
if isinstance(results[0], torch.Tensor):
|
| 303 |
+
return torch.cat(results, dim=0)
|
| 304 |
+
elif isinstance(results[0], dict):
|
| 305 |
+
# Reassemble dict of tensors
|
| 306 |
+
out = {}
|
| 307 |
+
for k in results[0].keys():
|
| 308 |
+
out[k] = torch.cat([r[k] for r in results], dim=0)
|
| 309 |
+
return out
|
| 310 |
+
else:
|
| 311 |
+
raise ValueError("Unsupported return type for chunking")
|
| 312 |
+
|
| 313 |
+
def _run(
|
| 314 |
+
self,
|
| 315 |
+
quiet: bool = False,
|
| 316 |
+
desc: Optional[str] = None,
|
| 317 |
+
) -> Iterator[Tuple[TaskHandle, Any]]:
|
| 318 |
+
last_use_index = self.schedule.last_use_index
|
| 319 |
+
|
| 320 |
+
values: Dict[TaskHandle, Any] = {}
|
| 321 |
+
if self.cached_values:
|
| 322 |
+
for task, value in self.cached_values.items():
|
| 323 |
+
values[task] = value
|
| 324 |
+
|
| 325 |
+
is_gpu_execution = self.math_device.type != "cpu"
|
| 326 |
+
accelerator = get_torch_accelerator_module(self.math_device.type) if is_gpu_execution else None
|
| 327 |
+
|
| 328 |
+
for idx, task_handle in (
|
| 329 |
+
pbar := tqdm.tqdm(
|
| 330 |
+
list(enumerate(self.schedule.tasks)),
|
| 331 |
+
disable=quiet,
|
| 332 |
+
desc=desc or "Executing graph",
|
| 333 |
+
)
|
| 334 |
+
):
|
| 335 |
+
task = task_handle.task()
|
| 336 |
+
task_type = type(task).__name__
|
| 337 |
+
|
| 338 |
+
# Heuristic: Don't force I/O tasks to GPU
|
| 339 |
+
# PermutedEmbeddings is essentially a gather operation, hard to chunk, better on CPU if memory is tight
|
| 340 |
+
is_io_task = task_type in ["LoadTensor", "GatherTensors", "SaveTensor", "TensorWriterTask", "FinalizeModel", "PermutedEmbeddings"]
|
| 341 |
+
|
| 342 |
+
want_gpu = is_gpu_execution and (task.uses_accelerator() or not is_io_task)
|
| 343 |
+
|
| 344 |
+
success = False
|
| 345 |
+
|
| 346 |
+
if want_gpu:
|
| 347 |
+
try:
|
| 348 |
+
# 1. Try Full GPU Execution
|
| 349 |
+
arguments = {}
|
| 350 |
+
for name, dep_handle in task_handle.arguments().items():
|
| 351 |
+
value = values[dep_handle]
|
| 352 |
+
value = self._move_tensors(value, self.math_device)
|
| 353 |
+
arguments[name] = value
|
| 354 |
+
|
| 355 |
+
res = task.execute(**arguments)
|
| 356 |
+
del arguments
|
| 357 |
+
res = self._move_tensors(res, self.storage_device)
|
| 358 |
+
values[task_handle] = res
|
| 359 |
+
success = True
|
| 360 |
+
|
| 361 |
+
except torch.OutOfMemoryError:
|
| 362 |
+
# Cleanup
|
| 363 |
+
arguments = None
|
| 364 |
+
res = None
|
| 365 |
+
gc.collect()
|
| 366 |
+
if accelerator: accelerator.empty_cache()
|
| 367 |
+
|
| 368 |
+
# 2. Try Chunked GPU Execution with Adaptive Sizing
|
| 369 |
+
chunk_sizes = [4096, 2048, 1024, 512, 256, 128, 64]
|
| 370 |
+
|
| 371 |
+
# Reload arguments on CPU
|
| 372 |
+
arguments = {}
|
| 373 |
+
for name, dep_handle in task_handle.arguments().items():
|
| 374 |
+
arguments[name] = values[dep_handle] # Already on storage device
|
| 375 |
+
|
| 376 |
+
for chunk_size in chunk_sizes:
|
| 377 |
+
try:
|
| 378 |
+
LOG.info(f"OOM on {task_type}. Attempting chunked GPU execution (size={chunk_size})...")
|
| 379 |
+
res = self._execute_chunked(task, arguments, chunk_size=chunk_size)
|
| 380 |
+
values[task_handle] = res
|
| 381 |
+
success = True
|
| 382 |
+
LOG.info(f"Chunked execution successful for {task_type} (size={chunk_size})")
|
| 383 |
+
break
|
| 384 |
+
except Exception as e:
|
| 385 |
+
LOG.warning(f"Chunked execution failed at size {chunk_size} ({str(e)}).")
|
| 386 |
+
gc.collect()
|
| 387 |
+
if accelerator: accelerator.empty_cache()
|
| 388 |
+
# If it wasn't an OOM (e.g. index error), stop trying chunking
|
| 389 |
+
if not isinstance(e, torch.OutOfMemoryError):
|
| 390 |
+
break
|
| 391 |
+
|
| 392 |
+
# 3. CPU Fallback
|
| 393 |
+
if not success:
|
| 394 |
+
if want_gpu:
|
| 395 |
+
LOG.warning(f"All GPU attempts failed for {task_type}. Falling back to CPU.")
|
| 396 |
+
|
| 397 |
+
# Ensure we clean up any GPU debris before CPU attempt
|
| 398 |
+
if is_gpu_execution:
|
| 399 |
+
gc.collect()
|
| 400 |
+
if accelerator: accelerator.empty_cache()
|
| 401 |
+
|
| 402 |
+
arguments = {}
|
| 403 |
+
for name, dep_handle in task_handle.arguments().items():
|
| 404 |
+
value = values[dep_handle]
|
| 405 |
+
value = self._move_tensors(value, torch.device("cpu"))
|
| 406 |
+
arguments[name] = value
|
| 407 |
+
|
| 408 |
+
res = task.execute(**arguments)
|
| 409 |
+
del arguments
|
| 410 |
+
res = self._move_tensors(res, self.storage_device)
|
| 411 |
+
values[task_handle] = res
|
| 412 |
+
|
| 413 |
+
del res
|
| 414 |
+
|
| 415 |
+
if task_handle in self.targets:
|
| 416 |
+
yield (task_handle, values[task_handle])
|
| 417 |
+
|
| 418 |
+
# Evict unreferenced values
|
| 419 |
+
expired = []
|
| 420 |
+
for key in values:
|
| 421 |
+
if idx >= last_use_index[key]:
|
| 422 |
+
expired.append(key)
|
| 423 |
+
for key in expired:
|
| 424 |
+
del values[key]
|
| 425 |
+
|
| 426 |
+
# Aggressive cleanup
|
| 427 |
+
if is_gpu_execution:
|
| 428 |
+
gc.collect()
|
| 429 |
+
if accelerator: accelerator.empty_cache()
|
| 430 |
+
|
| 431 |
+
del values
|
| 432 |
+
del pbar
|
| 433 |
+
|
| 434 |
+
def run(
|
| 435 |
+
self,
|
| 436 |
+
quiet: bool = False,
|
| 437 |
+
desc: Optional[str] = None,
|
| 438 |
+
) -> Iterator[Tuple[Task, Any]]:
|
| 439 |
+
for handle, value in self._run(quiet=quiet, desc=desc):
|
| 440 |
+
yield (handle.task(), value)
|
| 441 |
+
|
| 442 |
+
def execute(self, desc: Optional[str] = None) -> None:
|
| 443 |
+
for _ in self.run(desc=desc):
|
| 444 |
+
pass
|
| 445 |
+
|
| 446 |
+
def _move_tensors(
|
| 447 |
+
self, value: Any, device: torch.device, non_blocking: Optional[bool] = None
|
| 448 |
+
) -> Any:
|
| 449 |
+
if non_blocking is None:
|
| 450 |
+
non_blocking = device.type in ["cuda", "xpu"]
|
| 451 |
+
if isinstance(value, torch.Tensor):
|
| 452 |
+
if value.device == device:
|
| 453 |
+
return value
|
| 454 |
+
return value.to(device=device, non_blocking=non_blocking)
|
| 455 |
+
elif isinstance(value, dict):
|
| 456 |
+
return {k: self._move_tensors(v, device, non_blocking) for k, v in value.items()}
|
| 457 |
+
elif isinstance(value, list):
|
| 458 |
+
return [self._move_tensors(v, device, non_blocking) for v in value]
|
| 459 |
+
elif isinstance(value, tuple):
|
| 460 |
+
return tuple(self._move_tensors(v, device, non_blocking) for v in value)
|
| 461 |
+
return value
|
mergekit_low-VRAM-graph_patch.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Mergekit Low VRAM Graph Patch
|
| 2 |
+
## Merge models in minutes instead of hours on low VRAM
|
| 3 |
+
|
| 4 |
+
This is a significant and sophisticated modification to `mergekit/graph.py`. It transforms the standard `Executor` from a "optimistic" runner (assuming tensors fit in VRAM) into a **robust, adaptive execution engine** designed specifically to survive low-VRAM environments.
|
| 5 |
+
|
| 6 |
+
Here is a detailed analysis of the changes and how they achieve the goal of running on RTX 3060-class hardware.
|
| 7 |
+
|
| 8 |
+
### Core Strategy: "Fail Gracefully and Chunk"
|
| 9 |
+
|
| 10 |
+
The original `Executor` simply moved tensors to the GPU, executed, and moved them back. If VRAM ran out, the process crashed. This modified version implements a three-tier fallback strategy inside `_run`:
|
| 11 |
+
|
| 12 |
+
1. **Tier 1: Standard GPU Execution.** Try to run the task normally on the GPU.
|
| 13 |
+
2. **Tier 2: Adaptive Chunking.** If Tier 1 throws an OOM (`torch.OutOfMemoryError`), catch it, clear the cache, and attempt to split the operation into smaller batches (chunks).
|
| 14 |
+
3. **Tier 3: CPU Fallback.** If chunking fails (or isn't applicable), fall back to system RAM (CPU), which is much slower but usually has higher capacity.
|
| 15 |
+
|
| 16 |
+
### Key Code Modifications
|
| 17 |
+
|
| 18 |
+
#### 1. Windows/NVIDIA Allocator Tuning
|
| 19 |
+
```python
|
| 20 |
+
if sys.platform == "win32":
|
| 21 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:32"
|
| 22 |
+
```
|
| 23 |
+
**Analysis:** This is a crucial addition for consumer hardware, particularly on Windows. PyTorch on Windows often suffers from memory fragmentation. Setting `max_split_size_mb` helps prevent the allocator from splitting blocks too aggressively, reducing "fragmentation OOMs" where free memory exists but isn't contiguous.
|
| 24 |
+
|
| 25 |
+
#### 2. The `_execute_chunked` Method
|
| 26 |
+
This is a new helper method that implements the logic for breaking a large tensor operation into smaller pieces.
|
| 27 |
+
|
| 28 |
+
* **Logic:** It identifies a reference tensor in the arguments, determines the total number of rows (dim 0), and iterates through the data in `chunk_size` increments.
|
| 29 |
+
* **Memory Efficiency:**
|
| 30 |
+
* It slices inputs on the CPU.
|
| 31 |
+
* Moves **only the current slice** to the GPU.
|
| 32 |
+
* Executes the task.
|
| 33 |
+
* Moves the result **immediately back to the CPU**.
|
| 34 |
+
* Deletes the GPU tensors and clears the cache.
|
| 35 |
+
* **Result:** The peak VRAM usage becomes proportional to `chunk_size` rather than the full model layer size.
|
| 36 |
+
|
| 37 |
+
#### 3. The Adaptive Execution Loop (`_run`)
|
| 38 |
+
The `_run` method has been completely rewritten to handle the fallback logic.
|
| 39 |
+
|
| 40 |
+
**The Heuristic Filter:**
|
| 41 |
+
```python
|
| 42 |
+
is_io_task = task_type in ["LoadTensor", "GatherTensors", "SaveTensor", ...]
|
| 43 |
+
want_gpu = is_gpu_execution and (task.uses_accelerator() or not is_io_task)
|
| 44 |
+
```
|
| 45 |
+
**Analysis:** The code explicitly prevents I/O tasks (loading/saving) from clogging up the GPU. `PermutedEmbeddings` is also excluded, which is smart because embedding tables are massive (often 250MB+) and permuting them is memory-bandwidth bound, not compute bound.
|
| 46 |
+
|
| 47 |
+
**The OOM Handler:**
|
| 48 |
+
```python
|
| 49 |
+
except torch.OutOfMemoryError:
|
| 50 |
+
# ... cleanup ...
|
| 51 |
+
chunk_sizes = [4096, 2048, 1024, 512, 256, 128, 64]
|
| 52 |
+
for chunk_size in chunk_sizes:
|
| 53 |
+
try:
|
| 54 |
+
res = self._execute_chunked(task, arguments, chunk_size=chunk_size)
|
| 55 |
+
# ... success ...
|
| 56 |
+
break
|
| 57 |
+
```
|
| 58 |
+
**Analysis:** This is the "magic" that allows 3060s to work. If a layer is too big, it tries progressively smaller chunks until it finds a size that fits in the remaining VRAM.
|
| 59 |
+
|
| 60 |
+
**Aggressive Garbage Collection:**
|
| 61 |
+
```python
|
| 62 |
+
if is_gpu_execution:
|
| 63 |
+
gc.collect()
|
| 64 |
+
if accelerator: accelerator.empty_cache()
|
| 65 |
+
```
|
| 66 |
+
**Analysis:** This runs at the end of *every* task execution loop.
|
| 67 |
+
* **Pros:** It ensures VRAM is absolutely as clean as possible for the next task.
|
| 68 |
+
* **Cons:** `cuda.empty_cache()` forces a device synchronization and overhead. This will make the merge process significantly slower than a standard run, but it trades speed for the ability to run at all.
|
| 69 |
+
|
| 70 |
+
### Potential Risks & Limitations
|
| 71 |
+
|
| 72 |
+
1. **Assumption of Row-Independence:**
|
| 73 |
+
The `_execute_chunked` method assumes that the `task.execute` method operates independently on rows (dimension 0).
|
| 74 |
+
* **Safe:** Linear merges, SLERP (usually), and element-wise operations.
|
| 75 |
+
* **Unsafe:** Operations that require global statistics across the batch dimension (e.g., `softmax` over dim 0, though rare in weight merging) or matrix multiplications where the split dimension is the reduction dimension. However, for standard LLM weight merging (which is usually element-wise weighted averaging), this assumption holds.
|
| 76 |
+
|
| 77 |
+
2. **Performance Overhead:**
|
| 78 |
+
The constant `gc.collect()` and `empty_cache()` calls, combined with moving data back and forth between CPU and GPU for every chunk, will result in low GPU utilization. The merge will take longer, but it will complete.
|
| 79 |
+
|
| 80 |
+
### Conclusion
|
| 81 |
+
|
| 82 |
+
This is a **highly effective patch for low-VRAM users**. It trades execution speed for memory safety.
|
| 83 |
+
|
| 84 |
+
* **For a 3090/4090 user:** This script might be slower than the original due to the aggressive GC.
|
| 85 |
+
* **For a 3060/3060 Ti user:** This script enables functionality that is otherwise impossible (merging 70B models or large 7B merges with `--cuda`).
|
| 86 |
+
|
| 87 |
+
The implementation is robust because it doesn't force chunking; it only attempts it when the standard approach fails.
|