Skip to content

vllm.model_executor.model_loader.weight_cache.ipc_loader

IPC model loader: maps post-quantized weights from a local weight cache daemon via CUDA IPC instead of loading from disk.

Classes:

  • IpcModelLoader –

    Loads a model by mapping the weight cache daemon's tensors via CUDA IPC.

IpcModelLoader

Bases: BaseModelLoader

Loads a model by mapping the weight cache daemon's tensors via CUDA IPC.

The model is initialized on the meta device and every parameter/buffer is replaced by the daemon's post-quantized tensor, so process_weights_after_loading is skipped entirely. In "zero_copy" mode the engine shares the daemon's GPU memory; in "copy" mode the tensors are cloned into engine-owned memory and the daemon is asked to release its cache afterwards.

Extra config keys (via --model-loader-extra-config):

  • socket_path: explicit daemon socket path. Defaults to a per-GPU path derived from the physical GPU uuid and the cache role (target/draft).
  • socket_dir: directory containing the daemon sockets.
  • mode: "zero_copy" (default) or "copy".
  • fallback: fall back to disk loading when the daemon is unavailable or the fingerprints mismatch (default: True).
  • connect_timeout_s: socket connect timeout (default: 5.0).
  • state_timeout_s: timeout for the weight-transfer request (default: 300.0).

Note: in zero-copy mode the weights live in the daemon's CUDA IPC allocations, so sleep mode (CuMemAllocator weight offloading) must not be used with this loader.

Methods:

  • load_weights –

    Best-effort in-place reload for an already-initialized model.

Source code in vllm/model_executor/model_loader/weight_cache/ipc_loader.py
 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
class IpcModelLoader(BaseModelLoader):
    """Loads a model by mapping the weight cache daemon's tensors via CUDA IPC.

    The model is initialized on the meta device and every parameter/buffer is
    replaced by the daemon's post-quantized tensor, so
    process_weights_after_loading is skipped entirely. In "zero_copy" mode the
    engine shares the daemon's GPU memory; in "copy" mode the tensors are
    cloned into engine-owned memory and the daemon is asked to release its
    cache afterwards.

    Extra config keys (via --model-loader-extra-config):

    - socket_path: explicit daemon socket path. Defaults to a per-GPU path
      derived from the physical GPU uuid and the cache role (target/draft).
    - socket_dir: directory containing the daemon sockets.
    - mode: "zero_copy" (default) or "copy".
    - fallback: fall back to disk loading when the daemon is unavailable or
      the fingerprints mismatch (default: True).
    - connect_timeout_s: socket connect timeout (default: 5.0).
    - state_timeout_s: timeout for the weight-transfer request (default: 300.0).

    Note: in zero-copy mode the weights live in the daemon's CUDA IPC
    allocations, so sleep mode (CuMemAllocator weight offloading) must not be
    used with this loader.
    """

    def __init__(self, load_config: LoadConfig):
        super().__init__(load_config)
        extra_config = copy(load_config.model_loader_extra_config or {})
        self.socket_path: str | None = extra_config.pop("socket_path", None)
        self.socket_dir: str | None = extra_config.pop("socket_dir", None)
        # Internal: set by the engine when routing a speculative draft to the
        # daemon's draft group.
        self.is_draft = bool(extra_config.pop("is_draft", False))
        if self.is_draft and self.socket_path is not None:
            raise ValueError(
                "socket_path cannot be combined with the draft weight cache role; "
                "use socket_dir so the target and draft sockets are derived "
                "independently"
            )
        self.mode: str = extra_config.pop("mode", "zero_copy")
        self.fallback: bool = extra_config.pop("fallback", True)
        self.connect_timeout_s: float = float(
            extra_config.pop("connect_timeout_s", _CONNECT_TIMEOUT_S)
        )
        self.state_timeout_s: float = float(
            extra_config.pop("state_timeout_s", _STATE_TIMEOUT_S)
        )
        if self.mode not in ("zero_copy", "copy"):
            raise ValueError(
                f"Invalid weight cache mode {self.mode!r}, "
                "expected 'zero_copy' or 'copy'"
            )
        if extra_config:
            raise ValueError(
                f"Unexpected extra config keys for load format "
                f"{load_config.load_format}: {sorted(extra_config)}"
            )

    def get_external_weight_memory(self, vllm_config: VllmConfig) -> int:
        # Copy mode clones the weights into this process; nothing external.
        if self.mode != "zero_copy":
            return 0
        total = self._daemon_memory()
        if is_draft_model_cacheable(vllm_config.speculative_config):
            # The draft group is queried with the same config the engine
            # would load the draft with.
            draft_config = get_draft_load_config(vllm_config)
            if draft_config.load_format == "ipc_cache":
                draft_loader = IpcModelLoader(draft_config)
                # Only a zero-copy draft-group daemon holds external weights;
                # an explicit draft_load_config without is_draft would resolve
                # back to the target socket and double-count it.
                if draft_loader.is_draft and draft_loader.mode == "zero_copy":
                    total += draft_loader._daemon_memory()
        return total

    def _daemon_memory(self) -> int:
        if not self.fallback:
            # The loader waits for the daemon at load time, so the weights
            # will be zero-copy mapped for sure; mirror that wait here since
            # returning 0 would over-grant the memory budget.
            return self._with_startup_wait(self._query_daemon_memory)
        # An unreachable daemon means a disk load, i.e. nothing external.
        try:
            return self._query_daemon_memory()
        except (WeightCacheUnavailableError, ConnectionError, OSError) as e:
            logger.warning(
                "Cannot query weight cache daemon memory (%s); "
                "assuming the weights are not externally held",
                e,
            )
            return 0

    def _query_daemon_memory(self) -> int:
        with self._connect(self.connect_timeout_s) as conn:
            send_msg(conn, {"cmd": "get_memory"})
            response = recv_msg(conn)
        if response.get("status") != "ok":
            raise WeightCacheUnavailableError(
                "Weight cache daemon rejected the memory query: "
                f"{response.get('message')}"
            )
        return int(response.get("memory_bytes", 0))

    def download_model(self, model_config: ModelConfig) -> None:
        DefaultModelLoader(self._fallback_load_config()).download_model(model_config)

    def load_weights(self, model: nn.Module, model_config: ModelConfig) -> None:
        """Best-effort in-place reload for an already-initialized model.

        Copies daemon tensors into matching parameters/buffers. The model is
        expected to already be in the post-quantized layout (e.g. previously
        loaded through this loader).
        """
        device_index = torch.accelerator.current_device_index()
        entries = self._fetch_entries(model_config).entries
        params = dict(model.named_parameters())
        buffers = dict(model.named_buffers())
        for name, entry in entries.items():
            target = params.get(name, buffers.get(name))
            source = entry.rebuild(device_index)
            if target is None or target.shape != source.shape:
                logger.warning("Skipping mismatched cached tensor %s", name)
                continue
            target.data.copy_(source)

    @instrument(span_name="Load model")
    def load_model(
        self, vllm_config: VllmConfig, model_config: ModelConfig, prefix: str = ""
    ) -> nn.Module:
        # An unsupported platform is a permanent misconfiguration rather than
        # a transient daemon outage, so it is raised even when fallback is on.
        check_ipc_platform_support()
        state_fetched = False
        try:
            # Cross-check the routing flag against the identity of the model
            # being loaded: a draft load that lost its flag (or a target load
            # that got one) would hit the wrong daemon group and
            # fingerprint-mismatch.
            spec = vllm_config.speculative_config
            inferred = spec is not None and model_config is spec.draft_model_config
            if inferred != self.is_draft:
                raise CacheConfigMismatchError(
                    f"Weight cache role mismatch: loading "
                    f"{'draft' if inferred else 'target'} model but the loader "
                    f"was configured for the "
                    f"{'draft' if self.is_draft else 'target'} group"
                )
            state = self._fetch_entries(model_config)
            state_fetched = True
            return self._build_model(vllm_config, model_config, prefix, state)
        except (WeightCacheUnavailableError, CacheConfigMismatchError) as e:
            if not self.fallback:
                raise
            logger.warning(
                "Weight cache unusable (%s); falling back to disk loading", e
            )
        except UnsupportedQuantForIPCError:
            # Unsupported quantization is a permanent misconfiguration rather
            # than a transient daemon outage, so it is raised even when
            # fallback is on.
            raise
        except Exception:
            if not self.fallback:
                raise
            logger.exception(
                "Weight cache IPC loading failed; falling back to disk loading"
            )
            # _build_model failed after fetching state without reaching its
            # copy-mode release, so the daemon still holds the full cache;
            # release it so the disk fallback does not OOM against it.
            if state_fetched and self.mode == "copy":
                self._send_release()
            torch.accelerator.empty_cache()
        return self._fallback_load(vllm_config, model_config, prefix)

    def _build_model(
        self,
        vllm_config: VllmConfig,
        model_config: ModelConfig,
        prefix: str,
        state: WeightCacheState,
    ) -> nn.Module:
        device_config = vllm_config.device_config
        load_device = (
            device_config.device
            if self.load_config.device is None
            else self.load_config.device
        )
        target_device = torch.device(load_device)
        device_index = (
            target_device.index
            if target_device.index is not None
            else torch.accelerator.current_device_index()
        )
        with set_default_torch_dtype(model_config.dtype):
            with torch.device("meta"):
                model = initialize_model(
                    vllm_config=vllm_config,
                    model_config=model_config,
                    prefix=prefix,
                )
            check_ipc_quant_support(model)
            self._apply_entries(model, state, device_index)
            # Flags that load_weights would have set (e.g. EAGLE ownership of
            # embed_tokens / lm_head); the daemon ran it, this process did not.
            for name, value in state.attrs.items():
                setattr(model, name, value)
            # The daemon exports tensors that already went through
            # process_weights_after_loading; re-run it in pre-processed mode
            # so quant methods only rebuild Python-side state (e.g. the MoE
            # kernel). Leftovers are materialized afterwards so that
            # placeholders the daemon-side post-processing consumed are
            # dropped rather than filled with uninitialized memory.
            with weights_already_processed():
                process_weights_after_loading(model, model_config, target_device)
            _materialize_remaining_meta_tensors(
                model, torch.device(target_device.type, device_index)
            )
        if self.mode == "copy":
            self._send_release()
        logger.info(
            "Mapped %d tensors from the weight cache daemon (%s mode)",
            len(state.entries),
            self.mode,
        )
        return model.eval()

    def _apply_entries(
        self,
        model: nn.Module,
        state: WeightCacheState,
        device_index: int,
    ) -> None:
        # remove_duplicate=False keeps tied module aliases reachable by name:
        # a tied lm_head *is* the embedding module, so the deduplicated view
        # would not contain "lm_head" at all.
        modules = dict(model.named_modules(remove_duplicate=False))
        registered: dict[str, torch.Tensor] = {}

        def _register(name: str, tensor: torch.Tensor, is_param: bool) -> None:
            module_name, _, leaf = name.rpartition(".")
            module = modules.get(module_name)
            if module is None:
                raise RuntimeError(f"Cached tensor {name} has no matching module")
            # Replace via registration rather than param.data assignment,
            # which fails for meta tensors. Entries may also introduce
            # post-quantization tensors absent from the meta model.
            module._parameters.pop(leaf, None)
            module._buffers.pop(leaf, None)
            if is_param:
                obj: torch.Tensor = (
                    tensor
                    if isinstance(tensor, nn.Parameter)
                    else nn.Parameter(tensor, requires_grad=False)
                )
                module.register_parameter(leaf, obj)
            else:
                obj = tensor
                module.register_buffer(leaf, obj)
            registered[name] = obj

        for name, entry in state.entries.items():
            tensor = entry.rebuild(device_index)
            if self.mode == "copy":
                tensor = tensor.clone()
            _register(name, tensor, entry.kind == "param")

        # Re-establish tied-weight aliases by registering the *same* object the
        # canonical name resolved to, so parameter identity (and the tie) is
        # preserved instead of allocating uninitialized memory.
        for alias_name, canonical_name in state.aliases.items():
            obj = registered.get(canonical_name)
            if obj is None:
                logger.warning(
                    "Cached alias %s references missing canonical tensor %s",
                    alias_name,
                    canonical_name,
                )
                continue
            _register(alias_name, obj, isinstance(obj, nn.Parameter))

    def _fetch_entries(self, model_config: ModelConfig) -> WeightCacheState:
        dp_group = get_dp_group()
        pp_group = get_pp_group()
        cache_config = WeightCacheKey.from_model_config(
            model_config,
            tp_size=get_tensor_model_parallel_world_size(),
            tp_rank=get_tensor_model_parallel_rank(),
            pp_size=pp_group.world_size,
            pp_rank=pp_group.rank_in_group,
            dp_size=dp_group.world_size,
            dp_rank=dp_group.rank_in_group,
            is_draft=self.is_draft,
        )
        if not self.fallback:
            return self._request_state_with_startup_wait(cache_config)
        return self._request_state(cache_config)

    def _request_state_with_startup_wait(
        self, cache_config: WeightCacheKey
    ) -> WeightCacheState:
        return self._with_startup_wait(lambda: self._request_state(cache_config))

    def _with_startup_wait(self, op: Callable[[], _T]) -> _T:
        """Retry op until the daemon answers or the state timeout elapses;
        the daemon may still be loading the model when the engine starts."""
        deadline = time.monotonic() + self.state_timeout_s
        while True:
            try:
                return op()
            except (WeightCacheUnavailableError, ConnectionError, OSError) as e:
                if time.monotonic() >= deadline:
                    raise WeightCacheUnavailableError(
                        "Weight cache daemon did not become ready within "
                        f"{self.state_timeout_s:.1f}s: {e}"
                    ) from e
                logger.info_once(
                    "Waiting up to %.1fs for the weight cache daemon to start",
                    self.state_timeout_s,
                )
                time.sleep(
                    max(
                        0.0,
                        min(_STARTUP_RETRY_INTERVAL_S, deadline - time.monotonic()),
                    )
                )

    def _request_state(self, cache_config: WeightCacheKey) -> WeightCacheState:
        with self._connect(self.state_timeout_s) as conn:
            send_msg(conn, {"cmd": "get_state", "cache_config": cache_config})
            response = recv_msg(conn)
        status = response.get("status")
        if status == "mismatch":
            raise CacheConfigMismatchError(
                f"WeightCacheKey mismatch on fields: {response.get('fields')}"
            )
        if status != "ok":
            raise WeightCacheUnavailableError(
                f"Weight cache daemon error: {response.get('message')}"
            )
        self._check_gpu_uuid(response.get("gpu_uuid"))
        return WeightCacheState(
            entries=response["entries"],
            aliases=response.get("aliases", {}),
            attrs=response.get("attrs", {}),
        )

    def _connect(self, timeout: float) -> socket.socket:
        socket_path = self._resolve_socket_path()
        # The auto-derived per-user directory is locked to 0700 and checked
        # strictly. When the operator explicitly configures a path they own the
        # trust decision, so only ownership/symlink safety is enforced.
        strict_perms = self.socket_path is None and self.socket_dir is None
        try:
            verify_socket_owner(socket_path, strict_perms=strict_perms)
        except OSError as e:
            raise WeightCacheUnavailableError(
                f"Weight cache socket {socket_path} is unavailable: {e}"
            ) from e
        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        sock.settimeout(timeout)
        try:
            sock.connect(socket_path)
        except OSError as e:
            sock.close()
            raise WeightCacheUnavailableError(
                f"Cannot connect to weight cache daemon at {socket_path}: {e}"
            ) from e
        return sock

    def _resolve_socket_path(self) -> str:
        if self.socket_path is not None:
            return self.socket_path
        return get_socket_path(
            get_current_device_uuid(),
            self.socket_dir,
            is_draft=self.is_draft,
        )

    def _check_gpu_uuid(self, daemon_uuid: str | None) -> None:
        if daemon_uuid is None:
            return
        local_uuid = get_current_device_uuid()
        if daemon_uuid != local_uuid:
            raise CacheConfigMismatchError(
                f"Daemon GPU {daemon_uuid} != engine GPU {local_uuid}; "
                "check the socket path / GPU mapping"
            )

    def _send_release(self) -> None:
        try:
            with self._connect(self.connect_timeout_s) as conn:
                send_msg(conn, {"cmd": "release"})
                recv_msg(conn)
        except (WeightCacheUnavailableError, ConnectionError, OSError):
            logger.warning("Failed to ask the weight cache daemon to release")

    def _fallback_load_config(self) -> LoadConfig:
        # DefaultModelLoader must not see load_format="ipc_cache" or the ipc
        # extra config keys.
        return dataclasses.replace(
            self.load_config,
            load_format="auto",
            model_loader_extra_config={},
        )

    def _fallback_load(
        self, vllm_config: VllmConfig, model_config: ModelConfig, prefix: str
    ) -> nn.Module:
        loader = DefaultModelLoader(self._fallback_load_config())
        return loader.load_model(
            vllm_config=vllm_config, model_config=model_config, prefix=prefix
        )

_with_startup_wait(op)

Retry op until the daemon answers or the state timeout elapses; the daemon may still be loading the model when the engine starts.

Source code in vllm/model_executor/model_loader/weight_cache/ipc_loader.py
def _with_startup_wait(self, op: Callable[[], _T]) -> _T:
    """Retry op until the daemon answers or the state timeout elapses;
    the daemon may still be loading the model when the engine starts."""
    deadline = time.monotonic() + self.state_timeout_s
    while True:
        try:
            return op()
        except (WeightCacheUnavailableError, ConnectionError, OSError) as e:
            if time.monotonic() >= deadline:
                raise WeightCacheUnavailableError(
                    "Weight cache daemon did not become ready within "
                    f"{self.state_timeout_s:.1f}s: {e}"
                ) from e
            logger.info_once(
                "Waiting up to %.1fs for the weight cache daemon to start",
                self.state_timeout_s,
            )
            time.sleep(
                max(
                    0.0,
                    min(_STARTUP_RETRY_INTERVAL_S, deadline - time.monotonic()),
                )
            )

load_weights(model, model_config)

Best-effort in-place reload for an already-initialized model.

Copies daemon tensors into matching parameters/buffers. The model is expected to already be in the post-quantized layout (e.g. previously loaded through this loader).

Source code in vllm/model_executor/model_loader/weight_cache/ipc_loader.py
def load_weights(self, model: nn.Module, model_config: ModelConfig) -> None:
    """Best-effort in-place reload for an already-initialized model.

    Copies daemon tensors into matching parameters/buffers. The model is
    expected to already be in the post-quantized layout (e.g. previously
    loaded through this loader).
    """
    device_index = torch.accelerator.current_device_index()
    entries = self._fetch_entries(model_config).entries
    params = dict(model.named_parameters())
    buffers = dict(model.named_buffers())
    for name, entry in entries.items():
        target = params.get(name, buffers.get(name))
        source = entry.rebuild(device_index)
        if target is None or target.shape != source.shape:
            logger.warning("Skipping mismatched cached tensor %s", name)
            continue
        target.data.copy_(source)

_materialize_remaining_meta_tensors(model, device)

Allocate any tensors the daemon did not provide.

These are typically parameters removed on the daemon side by process_weights_after_loading; they are not expected to be read at runtime, so they are left uninitialized.

Source code in vllm/model_executor/model_loader/weight_cache/ipc_loader.py
def _materialize_remaining_meta_tensors(model: nn.Module, device: torch.device) -> None:
    """Allocate any tensors the daemon did not provide.

    These are typically parameters removed on the daemon side by
    process_weights_after_loading; they are not expected to be read at
    runtime, so they are left uninitialized.
    """
    for module_name, module in model.named_modules():
        for leaf, param in list(module._parameters.items()):
            if param is not None and param.device.type == "meta":
                logger.warning(
                    "Materializing empty parameter %s.%s missing from the weight cache",
                    module_name,
                    leaf,
                )
                module._parameters[leaf] = nn.Parameter(
                    torch.empty_like(param, device=device), requires_grad=False
                )
        for leaf, buffer in list(module._buffers.items()):
            if buffer is not None and buffer.device.type == "meta":
                module._buffers[leaf] = torch.empty_like(buffer, device=device)