[PR #550] fix: rewind file pointer and handle AttributeError on JIT load fallback #532

Open
opened 2026-10-31 21:15:47 +00:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/openai/CLIP/pull/550
Author: @reginaldalfret
Created: 10/14/2026
Status: 🔄 Open

Base: main ← Head: fix/issue-494-load-fallback


📝 Commits (1)

  • 4b5b3b4 fix: rewind file pointer and handle AttributeError on JIT load fallback

📊 Changes

2 files changed (+36 additions, -1 deletions)

View changed files

📝 clip/clip.py (+2 -1)
➕ tests/test_model_fallback.py (+34 -0)

📄 Description

Summary

In clip.load(), when attempting to load a checkpoint file, the function first tries torch.jit.load():

        try:
            # loading JIT archive
            model = torch.jit.load(opened_file, map_location=device if jit else "cpu").eval()
            state_dict = None
        except RuntimeError:
            ...
            state_dict = torch.load(opened_file, map_location="cpu")

If torch.jit.load() encounters an incompatibility (such as an AttributeError from missing script attributes in certain environments) or fails after reading initial bytes from the stream:

  1. AttributeError was uncaught, causing an unhandled crash rather than falling back to loading a plain PyTorch state_dict.
  2. The open file stream pointer was not rewound (opened_file.seek(0)), causing torch.load() to read from an offset position and fail during unpickling.

Fix

  • Catch (RuntimeError, AttributeError) in the fallback handler.
  • Rewind the file stream with opened_file.seek(0) before calling torch.load(opened_file).

Validation

  • Added unit test tests/test_model_fallback.py verifying that when torch.jit.load raises AttributeError, clip.load catches it, rewinds the file stream, and loads the model via torch.load.
  • Test passes cleanly.

Closes #494


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/openai/CLIP/pull/550 **Author:** [@reginaldalfret](https://github.com/reginaldalfret) **Created:** 10/14/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/issue-494-load-fallback` --- ### 📝 Commits (1) - [`4b5b3b4`](https://github.com/openai/CLIP/commit/4b5b3b4b953694c9f5a5d94a63055ffc1670c100) fix: rewind file pointer and handle AttributeError on JIT load fallback ### 📊 Changes **2 files changed** (+36 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `clip/clip.py` (+2 -1) ➕ `tests/test_model_fallback.py` (+34 -0) </details> ### 📄 Description ### Summary In `clip.load()`, when attempting to load a checkpoint file, the function first tries `torch.jit.load()`: ```python try: # loading JIT archive model = torch.jit.load(opened_file, map_location=device if jit else "cpu").eval() state_dict = None except RuntimeError: ... state_dict = torch.load(opened_file, map_location="cpu") ``` If `torch.jit.load()` encounters an incompatibility (such as an `AttributeError` from missing script attributes in certain environments) or fails after reading initial bytes from the stream: 1. `AttributeError` was uncaught, causing an unhandled crash rather than falling back to loading a plain PyTorch `state_dict`. 2. The open file stream pointer was not rewound (`opened_file.seek(0)`), causing `torch.load()` to read from an offset position and fail during unpickling. ### Fix - Catch `(RuntimeError, AttributeError)` in the fallback handler. - Rewind the file stream with `opened_file.seek(0)` before calling `torch.load(opened_file)`. ### Validation - Added unit test `tests/test_model_fallback.py` verifying that when `torch.jit.load` raises `AttributeError`, `clip.load` catches it, rewinds the file stream, and loads the model via `torch.load`. - Test passes cleanly. Closes #494 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#532
No description provided.