Repository navigation
fix(load): take the global-state writer inside the construction helper (#5301) - #5303
Merged
Merged
Conversation
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.
Fixes #5301. Regression from #5291 (which fixed #5268).
Problem
#5291 replaced the load
RLockthat compiled calls took with a reader/writerlock, so construction coverage became per-site. The quantize-path load with
offload_to_disk=False(loader.py, "loading model directly to CPU") called_hf_loader_from_pretrained_with_dynamic_module_retrywithout the writer, whileits neighbours did take it.
transformers runs construction inside
local_torch_dtype(a process-globaldefault-dtype window), and dynamo's
preserve_global_staterestores the dtypearound every trace. A compiled forward re-tracing inside that window left bf16
installed after the load restored fp32, or tripped
assert_loaded_model_state-- the #5127/#5209 failure mode, back on this path.Change
The writer now lives inside the helper, around each
loader.from_pretrainedattempt, so every construction site is covered and anew one cannot be added unguarded. The two call sites that already wrapped the
helper are de-indented (the lock is writer-reentrant, so keeping them would be
harmless but redundant). The file-repair step of the retry stays outside the
writer, so a
force_downloadretry does not hold it across the copy.Tests
New
tests/test_quantize_load_global_state_5301.py:test_helper_holds_the_writer_for_every_attempt-- a fake loader recordsmodel_global_state_lock()._writerand the calling thread id during both thefirst construction and the
force_download=Trueretry; both must be thecurrent thread, and the writer must be released afterwards.
test_helper_releases_the_writer_when_the_load_fails-- a non-retryable errorpropagates and the writer is not left held.
test_every_model_construction_site_is_guarded-- AST guard overgptqmodel/models/loader.py: everyloader.from_pretrained/from_configcall must sit inside a
with model_global_state_lock():block (which is howthe helper holds it), and the helper must still be used by >= 3 construction
sites. This is the "a new site cannot bypass it" check the issue asks for.
The first and third nodes fail on
main; the second is a release guard.Existing lock/race suites still pass:
test_compile_lock_scope_5268,test_compile_load_race_5209(12 passed), and the quantize-load path itself(
test_weight_only,test_weight_only_looper: 96 passed, 9 skipped).