Repository navigation
perf(ckpt): use set lookups for HF state dict keys during import - #6489
Merged
Merged
Conversation
StateDict membership checks (__getitem__, __contains__, get, exact pattern match) scanned the full key list on every call, so a per-key import loop was quadratic in the number of checkpoint keys. Cache a key set, rebuilt only when the source returns a different key list, and cache each shard's key set next to its reused safe_open handle. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Eugenio "Jay" Zuccarelli <[email protected]>
cuichenx
approved these changes
Oct 16, 2026
cuichenx
left a comment
Contributor
There was a problem hiding this comment.
great optimization, thanks for contributing
Contributor
|
/ok to test f644649 |
Contributor
|
Automatic Claude reviews have been retired. To request a pull-request review, post a comment containing: Add |
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
StateDictmembership checks scanned the full key list on every call, so per-key loops during HF import (state[k],k in state) were quadratic in the number of checkpoint keys. This switches them to set lookups.Changelog
StateDict._get_key_set(): a set oversource.get_all_keys(), used by__getitem__,__contains__,get, list access and exact-key_match_keys. Both in-tree sources return a cached list, so the set is built once; it is rebuilt only when the source hands back a different list (the Kimi K2.5-VL / K3 bridges temporarily wrapget_all_keyswith virtual keys duringbuild_conversion_tasks).SafeTensorsStateSource.load_tensors: cache each shard's key set next to its reusedsafe_openhandle instead of rebuildingset(f.keys())per call. Dropped together with the handle in_invalidate_handle, so the retry path still re-reads a replaced shard (covered by the existingtest_retry_refreshes_incomplete_shard).test_membership_follows_wrapped_get_all_keyschecks membership before, during and after wrappingget_all_keys. Passes on main, fails on a build-once cache, passes here.Benchmark
Synthetic checkpoints with real key names and one-element tensors, so this measures lookup overhead only, not end-to-end import time. CPU,
StateDict(SafeTensorsStateSource(path)), keys warmed first.state[k]loopk in stateloopTests
uv run python -m pytest tests/unit_tests/models/hf_pretrained/: 193 passed, on this branch and with main'sstate.py(same tests, including the new one). Alsotests/unit_tests/models/kimi_vl/test_kimi_k25_vl_bridge.py: 24 passed. Run on a CPU-only box: CPU torch wheel, Megatron-LM submodule installed editable, andtransformer_enginereplaced by a local import stub somegatron.bridgeimports without CUDA. pre-commit passes on the changed files.Additional Information
AI disclosure: I used Claude Code to profile this and write the change; I reviewed the diff, tests and benchmark.
🤖 Generated with Claude Code