Repository navigation
fix: rewind file pointer and handle AttributeError on JIT load fallback - #550
Open
reginaldalfret wants to merge 1 commit into
Open
reginaldalfret wants to merge 1 commit into
reginaldalfret wants to merge 1 commit into
Conversation
When torch.jit.load fails to load a model checkpoint (e.g. non-JIT archive checkpoints or PyTorch environments where torch.jit raises an AttributeError such as RecursiveScriptModule._construct), the exception handler attempts to fall back to loading the file as a plain state dict using torch.load. However, two issues occurred: 1. Only RuntimeError was caught, leaving AttributeErrors unhandled. 2. If torch.jit.load partially read bytes from the file stream before failing, the file pointer was not rewound, causing subsequent unpickling in torch.load to read corrupted/offset bytes or fail with EOF. Add AttributeError to the caught exceptions and seek back to 0 before calling torch.load. Fixes openai#494 Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
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.
Summary
In
clip.load(), when attempting to load a checkpoint file, the function first triestorch.jit.load():If
torch.jit.load()encounters an incompatibility (such as anAttributeErrorfrom missing script attributes in certain environments) or fails after reading initial bytes from the stream:AttributeErrorwas uncaught, causing an unhandled crash rather than falling back to loading a plain PyTorchstate_dict.opened_file.seek(0)), causingtorch.load()to read from an offset position and fail during unpickling.Fix
(RuntimeError, AttributeError)in the fallback handler.opened_file.seek(0)before callingtorch.load(opened_file).Validation
tests/test_model_fallback.pyverifying that whentorch.jit.loadraisesAttributeError,clip.loadcatches it, rewinds the file stream, and loads the model viatorch.load.Closes #494