File size: 18,887 Bytes
275a757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""On-Policy Codec Distillation for Efficient All-in-One Video Generation (CodecOPSD).

Authors: Boyang Wang, Kevin Blackburn-Matzen, Oindrila Saha, Matheus Gadelha, Zezhou Cheng

Copyright (c) 2026 The CodecOPSD Authors. All rights reserved.

Pinned model offload: one model on the GPU at a time, every other one in page-locked CPU memory.

Before a model runs, the model on the GPU leaves it (nothing is copied back: inference never writes the weights, so its
tensors are pointed back at their CPU copy), then the model that runs is copied in from page-locked memory (about
50 GB/s on PCIe 5, 25 GB/s on PCIe 4). Peak GPU memory is the largest model plus its activations. The order follows
the calls of the pipeline; free memory is never looked at, so placements and outputs are the same on every run.
The scheme follows `offload.py` of hyperflow-h3-flash (Apache-2.0); diffusers' model offload moves weights from
pageable memory instead, about 10 times slower.

Layer streaming (`stream`): the layers of a model's block list (the text encoder's decoder layers) do not move with
the model; each is copied in just before it runs, the next one ahead on a side stream while this one computes, and
dropped after it. The model then needs the rest of its weights plus two layers on the GPU (the FP8 text encoder:
about 3 GB instead of 26 GB), for about 0.5 s more per call.

Parts (`parts`): a model can also move in parts, each copied in when one of its modules runs. The video VAE moves
as its encoder (with `quant_conv`) while the references are encoded and as its decoder (with `post_quant_conv`) for the
decode, so the 4.5 GB decoder is never on the GPU during the reference encoding.

Every move is timed (`MoveLog`), so that a request can show how long the moves took (`codecopsd.stats`).

File mappings (`map_safetensors`): the FP8 text encoder (VLM) is loaded as views of a read-only shared mapping of its
.safetensors files, so its weights are the page cache's pages: no copy in process memory, and no commit charge under
strict overcommit (vm.overcommit_memory=2). `pin` page-locks such weights where they are (read-only registration, no
copy) and copies only the other tensors into its page-locked buffer. A model left `pageable` keeps its mapped weights
in the page cache instead, read again at each move: about 26 GB less locked CPU memory, slower moves.
"""

import ctypes
import functools
import itertools
import json
import mmap
import os
import struct
import time
import warnings
import weakref

import torch
from accelerate.hooks import ModelHook, add_hook_to_module
from accelerate.utils import send_to_device
from torch import nn

_ALIGN = 256
_PAGE = mmap.PAGESIZE
_MADV_DONTFORK = 10  # Linux
_HOST_REGISTER_READ_ONLY = 8  # cudaHostRegisterReadOnly: page-lock memory mapped read-only
_DTYPES = {"F64": torch.float64, "F32": torch.float32, "F16": torch.float16, "BF16": torch.bfloat16,
           "I64": torch.int64, "I32": torch.int32, "I16": torch.int16, "I8": torch.int8, "U8": torch.uint8,
           "BOOL": torch.bool, "F8_E4M3": torch.float8_e4m3fn, "F8_E5M2": torch.float8_e5m2}
_MAPPINGS = {}  # base address -> bytes of each live file mapping made by `map_safetensors`
_LOCKED = {}  # first page -> end of each page range page-locked in place (`_lock_mapped`)
LOCKED_BYTES = {"copied": 0, "mapped": 0}  # page-locked so far: copies in `pin` buffers, file pages locked in place


class _Mapping(mmap.mmap):
    """A file mapping that can be watched (`weakref`), so that `_MAPPINGS` forgets it once unmapped."""


def map_safetensors(path):
    """The tensors of a .safetensors file as views of one read-only shared mapping of it (MAP_SHARED, PROT_READ):
    nothing is copied or converted, and the pages are the page cache's, so the load takes no commit charge under
    strict overcommit (safetensors maps files private and writable, which counts their whole size). The weights must
    never be written: inference only reads them (a write would fault)."""
    fd = os.open(path, os.O_RDONLY)
    try:
        size = os.fstat(fd).st_size
        mapping = _Mapping(fd, size, flags=mmap.MAP_SHARED, prot=mmap.PROT_READ)
    finally:
        os.close(fd)
    length = struct.unpack("<Q", mapping[:8])[0]
    header = json.loads(mapping[8 : 8 + length])
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")  # "the given buffer is not writable": the weights are only read
        flat = torch.frombuffer(mapping, dtype=torch.uint8)  # keeps the mapping alive while any view lives
    _MAPPINGS[flat.data_ptr()] = size
    weakref.finalize(mapping, _MAPPINGS.pop, flat.data_ptr(), None)
    start = 8 + length
    out = {}
    for name, info in header.items():
        if name != "__metadata__":
            a, b = info["data_offsets"]
            out[name] = flat[start + a : start + b].view(_DTYPES[info["dtype"]]).view(info["shape"])
    return out


def _clear_cuda_error():
    """Reset the CUDA runtime's last error after a failed registration, so that PyTorch's next launch check does not
    report it."""
    try:
        ctypes.CDLL("libcudart.so.12").cudaGetLastError()
    except (OSError, AttributeError):
        pass


def is_mapped(tensor):
    """Whether `tensor` views a file mapping of `map_safetensors`."""
    return tensor.untyped_storage().data_ptr() in _MAPPINGS


def _lock_mapped(module, tensors):
    """Page-lock the file pages that `tensors` (views of `map_safetensors` mappings) read, where they are: read-only
    registration, no copy, no commit charge. Pages locked already (a neighbour's) are skipped. Returns the tensors that
    could not be locked (`cudaHostRegisterReadOnly` unsupported): `pin` copies those."""
    ranges = sorted((t.data_ptr() // _PAGE * _PAGE, -(-(t.data_ptr() + t.numel() * t.element_size()) // _PAGE) * _PAGE)
                    for t in tensors if t.numel())
    merged = []
    for start, end in ranges:
        if merged and start <= merged[-1][1]:
            merged[-1][1] = max(merged[-1][1], end)
        else:
            merged.append([start, end])
    for first, end in _LOCKED.items():  # skip the pages locked already
        merged = [piece for start, stop in merged for piece in ([start, min(stop, first)], [max(start, end), stop])
                  if piece[0] < piece[1]]
    cudart = torch.cuda.cudart()
    done = []
    for start, end in merged:
        if int(cudart.cudaHostRegister(start, end - start, _HOST_REGISTER_READ_ONLY)):
            _clear_cuda_error()
            for first, stop in done:  # undo: the caller copies these tensors instead
                cudart.cudaHostUnregister(first)
                _LOCKED.pop(first)
            LOCKED_BYTES["mapped"] -= sum(stop - first for first, stop in done)
            return tensors
        _LOCKED[start] = end
        LOCKED_BYTES["mapped"] += end - start
        done.append((start, end))
    if done:
        storages = [t.untyped_storage() for t in tensors]  # unmapped only after the pages are unlocked

        def unlock(pages, storages):
            for first, stop in pages:
                cudart.cudaHostUnregister(first)
                _LOCKED.pop(first, None)
                LOCKED_BYTES["mapped"] -= stop - first

        weakref.finalize(module, unlock, done, storages).atexit = False
    return []


def _tensors(module):
    return itertools.chain(module.parameters(), module.buffers())


def _no_fork_copy(arena):
    """Keep page-locked memory out of forked children (`MADV_DONTFORK`). Linux copies pinned pages into a forked
    child at once instead of sharing them, so a fork (imageio-ffmpeg starts ffmpeg with `preexec_fn`, which forks)
    would need the whole arena a second time: 47 GB more CPU memory for the Flash pipeline. The child only runs
    another program, which never reads the parent's memory."""
    libc = ctypes.CDLL(None, use_errno=True)
    start = -(-arena.data_ptr() // _PAGE) * _PAGE
    end = (arena.data_ptr() + arena.numel()) // _PAGE * _PAGE
    if end > start and libc.madvise(ctypes.c_void_p(start), ctypes.c_size_t(end - start), _MADV_DONTFORK):
        warnings.warn(f"madvise(MADV_DONTFORK) failed (errno {ctypes.get_errno()}): a fork copies the pinned memory")


def pin(module, pageable=False):
    """Page-lock the CPU tensors of `module`: those that view a file mapping (`map_safetensors`) where they are
    (`_lock_mapped`; left in the page cache with `pageable`), every other one moved into one page-locked buffer of
    exactly their size (`Tensor.pin_memory` rounds every allocation up to a power of two, about a quarter more host
    memory). Returns the bytes copied into the buffer."""
    tensors, mapped, seen = [], [], set()
    for tensor in _tensors(module):
        if tensor.device.type == "cpu" and not tensor.is_pinned() and id(tensor) not in seen:
            seen.add(id(tensor))
            (mapped if is_mapped(tensor) else tensors).append(tensor)
    if mapped and not pageable:
        tensors += _lock_mapped(module, mapped)
    if not tensors:
        return 0
    sizes = [t.numel() * t.element_size() for t in tensors]
    offsets = list(itertools.accumulate((-(-size // _ALIGN) * _ALIGN for size in sizes[:-1]), initial=0))
    arena = torch.empty(offsets[-1] + sizes[-1], dtype=torch.uint8)
    for tensor, offset, size in zip(tensors, offsets, sizes, strict=True):
        view = arena[offset : offset + size].view(tensor.dtype).view(tensor.shape)
        view.copy_(tensor.data)
        tensor.data = view
    cudart = torch.cuda.cudart()
    if error := int(cudart.cudaHostRegister(arena.data_ptr(), arena.numel(), 0)):
        raise RuntimeError(f"pinning {arena.numel() / 2**30:.1f} GiB of CPU memory failed (CUDA error {error})")
    _no_fork_copy(arena)
    weakref.finalize(module, cudart.cudaHostUnregister, arena.data_ptr()).atexit = False
    module._pinned_arena = arena
    LOCKED_BYTES["copied"] += arena.numel()
    return arena.numel()


class MoveLog:
    """Seconds and bytes of every model moved to the GPU (`seconds` grows; readers take differences)."""

    def __init__(self):
        self.seconds, self.bytes, self.moves = 0.0, 0, []
        self.now = None  # (model name, start time) of the move in progress, for a live display

    def add(self, name, seconds, nbytes):
        self.seconds += seconds
        self.bytes += nbytes
        self.moves.append((name, seconds, nbytes))


class PinnedSwap:
    """A model's weights in page-locked CPU memory, with a GPU copy made on `load` and dropped on `drop`. Tensors in
    `exclude` (ids; streamed layers) stay where they are; with `pageable` its file-mapped weights stay in the page
    cache (`pin`)."""

    def __init__(self, name, module, device, log, exclude=(), pageable=False):
        pin(module, pageable)
        self.name, self.device, self.log = name, torch.device(device), log
        self.host = [(t, t.data) for t in _tensors(module) if id(t) not in exclude]
        self.nbytes = sum(h.numel() * h.element_size() for _, h in self.host)
        self.loaded = False

    def load(self):
        if self.loaded:
            return
        torch.cuda.synchronize(self.device)
        t = time.perf_counter()
        self.log.now = (self.name, t)
        for tensor, host in self.host:
            tensor.data = host.to(self.device, non_blocking=True)
        torch.cuda.synchronize(self.device)
        self.log.now = None
        self.log.add(self.name, time.perf_counter() - t, self.nbytes)
        self.loaded = True

    def drop(self):
        if self.loaded:
            for tensor, host in self.host:
                tensor.data = host
            self.loaded = False


class LayerStream:
    """The layers of a block list in page-locked CPU memory, each copied to the GPU just before it runs (the next one
    ahead on a side stream) and dropped after it. Same weights, same products: exact."""

    def __init__(self, layers, device, log=None, name="layers"):
        self.device, self.log, self.name = torch.device(device), log, name
        self.layers = [[(t, t.data) for t in _tensors(layer)] for layer in layers]
        self.stream = torch.cuda.Stream(self.device)
        self.ready = {}
        for i, layer in enumerate(layers):
            layer.register_forward_pre_hook(functools.partial(self._before, i))
            layer.register_forward_hook(functools.partial(self._after, i))

    def tensor_ids(self):
        return {id(t) for layer in self.layers for t, _ in layer}

    def _fetch(self, i):
        if i >= len(self.layers) or i in self.ready:
            return
        with torch.cuda.stream(self.stream):
            copies = [host.to(self.device, non_blocking=True) for _, host in self.layers[i]]
            event = torch.cuda.Event()
            event.record(self.stream)
        self.ready[i] = (copies, event)

    def _before(self, i, module, args):
        self._fetch(i)
        copies, event = self.ready.pop(i)
        compute = torch.cuda.current_stream(self.device)
        compute.wait_event(event)
        for (tensor, _), copy in zip(self.layers[i], copies, strict=True):
            copy.record_stream(compute)  # its memory is reused only after this layer's work on the compute stream
            tensor.data = copy
        self._fetch(i + 1)

    def _after(self, i, module, args, output):
        for tensor, host in self.layers[i]:
            tensor.data = host


class OneOnGPU(ModelHook):
    """Before its model runs: every other swapped model leaves the GPU, then this one is copied in."""

    def __init__(self, device, swap, swaps):
        self.execution_device = torch.device(device)
        self.swap, self.swaps = swap, swaps

    def pre_forward(self, module, *args, **kwargs):
        dropped = False
        for other in self.swaps:
            if other is not self.swap and other.loaded:
                other.drop()
                dropped = True
        if dropped:  # hand the dropped model's blocks back, so the cache does not keep two models' worth of memory
            torch.cuda.empty_cache()
        self.swap.load()
        return send_to_device(args, self.execution_device), send_to_device(kwargs, self.execution_device)


class InParts(ModelHook):
    """The hook of a model that moves in parts (`pinned_model_offload(parts=...)`). It moves nothing itself: each part
    has its own `OneOnGPU` hook. It tells diffusers where the model runs (`execution_device`) and lets `offload_all`
    drop every part."""

    def __init__(self, device, swaps):
        self.execution_device = torch.device(device)
        self.swaps = swaps


def pinned_model_offload(models, device="cuda", log=None, stream=None, parts=None, pageable=()):
    """Place `models` ({name: nn.Module}, in CPU memory) for pinned model offload on `device`; `stream` ({name: block
    list}) streams those layers of a model one by one (`LayerStream`) instead of moving them with it; `pageable`
    (names) leaves the file-mapped weights of those models in the page cache instead of page-locking them; `parts`
    ({name: [[module, ...], ...]}) moves a model in parts instead of whole: each part (a list of submodules, which
    together must hold every tensor of the model) is copied in when one of its modules runs, so only the part that runs
    is on the GPU (the video VAE: the encoder while the references are encoded, the decoder for the decode). Returns the
    `MoveLog`."""
    device = torch.device(device)
    if device.type == "cuda" and device.index is None:
        device = torch.device("cuda", torch.cuda.current_device())
    log = log or MoveLog()
    swaps = []
    for name, module in models.items():
        if not isinstance(module, nn.Module):
            continue
        module.to("cpu")
        if parts and name in parts:
            groups = [nn.ModuleList(part) for part in parts[name]]
            if {id(t) for g in groups for t in _tensors(g)} != {id(t) for t in _tensors(module)}:
                raise ValueError(f"the parts of {name!r} must hold every tensor of it")
            own = []
            for group in groups:
                swap = PinnedSwap(name, group, device, log, pageable=name in pageable)
                swap.group = group  # keeps the page-locked buffer registered: `pin` unregisters it with its module
                own.append(swap)
                for part in group:
                    add_hook_to_module(part, OneOnGPU(device, swap, swaps))
            swaps.extend(own)
            add_hook_to_module(module, InParts(device, own))
            continue
        exclude = ()
        if stream and name in stream:
            pin(module, name in pageable)
            exclude = LayerStream(stream[name], device, log, name).tensor_ids()
        swap = PinnedSwap(name, module, device, log, exclude, name in pageable)
        swaps.append(swap)
        add_hook_to_module(module, OneOnGPU(device, swap, swaps))
    return log


def offload_all(models):
    """Every model of a pinned model offload back to its CPU copy (frees the GPU between requests)."""
    for module in models:
        hook = getattr(module, "_hf_hook", None)
        if isinstance(hook, OneOnGPU):
            hook.swap.drop()
        elif isinstance(hook, InParts):
            for swap in hook.swaps:
                swap.drop()


def time_model_offload(pipe):
    """Time the moves of diffusers' model offload (`pipe.enable_model_cpu_offload`, accelerate `CpuOffload` hooks) into
    `pipe.moves` (a `MoveLog`), as the pinned offload does: before a model runs, the previous one goes back to CPU
    memory and this one is copied in; `maybe_free_model_hooks` (after the denoising and at the end of a call) sends
    every model back and hooks them again, so the new hooks are timed again after it. Call after
    `enable_model_cpu_offload`. Returns the `MoveLog`."""
    log = pipe.moves = getattr(pipe, "moves", None) or MoveLog()
    enable = pipe.enable_model_cpu_offload

    def timed(name, fn, /, *args, **kwargs):
        torch.cuda.synchronize()
        t = time.perf_counter()
        log.now = (name, t)
        try:
            return fn(*args, **kwargs)
        finally:
            torch.cuda.synchronize()
            log.now = None
            seconds = time.perf_counter() - t
            if seconds > 0.005:  # a model already in place moves nothing (its hook runs before every call)
                log.add(name, seconds, 0)

    def hook_all():
        names = {id(m): n for n, m in pipe.components.items()}
        for user_hook in getattr(pipe, "_all_hooks", []):
            hook = user_hook.hook
            hook.pre_forward = functools.partial(timed, names.get(id(user_hook.model), "model"), hook.pre_forward)

    def enable_again(*args, **kwargs):
        timed("every model back to the CPU", enable, *args, **kwargs)
        hook_all()

    pipe.enable_model_cpu_offload = enable_again
    hook_all()
    return log