accelerate icon indicating copy to clipboard operation
accelerate copied to clipboard

OffloadedWeightsLoader initialization scans CPU keys for every disk weight

Open emme1t opened this issue 2 months ago • 0 comments

System Info

  • Accelerate: 1.16.0.dev0, current main at b795b4838eb33bde60eb398649c4ab006874e3e9
  • Python: 3.13.13
  • PyTorch: 2.14.0+cpu
  • OS: Windows 11, build 26200
  • Execution: single CPU process, public utility API, editable installation

Information

  • [x] My own modified scripts

Tasks

  • [x] My own task or dataset: constructor benchmark for the mixed CPU/disk weight map used by dispatch_model.

Reproduction

OffloadedWeightsLoader constructs all_keys by checking each disk-index key against a list containing all in-memory keys. With N CPU keys and M disk-index keys, this costs O(N*M) string comparisons. dispatch_model uses this combination for mixed offloading, so many-expert checkpoints can spend seconds building metadata before loading weights.

The following reproducer needs the standard CPU dependencies:

import time

import torch
from accelerate.utils import OffloadedWeightsLoader

weight = torch.empty(1)
for size in (1_000, 5_000, 20_000):
    state_dict = {f"model.cpu.experts.{i}.weight": weight for i in range(size)}
    index = {
        f"model.disk.experts.{i}.weight": {"dtype": "float32", "shape": [1]}
        for i in range(size)
    }
    start = time.perf_counter()
    loader = OffloadedWeightsLoader(state_dict=state_dict, index=index)
    print(size, time.perf_counter() - start, len(loader))

I also benchmarked names from the public DeepSeek-V3 weight index, which contains 91,991 keys. Using the first N names for CPU weights and the next N for disk metadata produced these medians over three constructor calls:

CPU keys + disk-index keys Current main Local one-line fix
1,000 + 1,000 0.0471 s 0.000350 s
5,000 + 5,000 0.9412 s 0.002807 s
20,000 + 20,000 18.9351 s 0.014248 s

With 20,000 keys on each side and 50% overlap, the median changed from 13.9148 s to 0.015074 s. Imports, tensor allocation, index construction and output validation were outside the timed region. This measures metadata construction on this machine; full model loading and inference throughput were outside the benchmark.

Expected behavior

Key merging should have approximately linear cost. The existing state_dict already provides hash-based membership and contains exactly the CPU keys that the list is checking. Using it preserves CPU-first iteration order, deduplication, and CPU tensor precedence.

I have a one-line patch and a focused test that checks insertion order, deduplication, overlapping weights with different CPU/disk values, and real disk reads. The complete tests/test_offload.py suite passes on CPU (5 tests), and repository-wide Ruff checks pass.


Codex collaboration disclosure: Codex generated this report, the patch, regression test and benchmark. A separate Codex agent performed an independent automated review and reran the relevant tests. This work is part of an Astra Ultra-based automated PR project whose goal is to contribute to the open-source community.

emme1t avatar Sep 19 '26 10:09 emme1t