Repository navigation
fix(calibration): bound and diagnose remote image fetches - #3483
Merged
Merged
Conversation
Multimodal calibration data can reference images by URL. Three call sites
issued `requests.get(url, stream=True)` with no timeout and no size limit:
- gptqmodel/utils/image.py (shared by ~15 VL model definitions)
- gptqmodel/models/definitions/interns1.py
- gptqmodel/models/definitions/ovis2_5.py
`requests` defaults to no timeout, so a server that accepts the connection and
never answers (or trickles bytes) hangs the whole quantization run forever; a
huge or malicious payload could also exhaust memory.
Add `load_image_from_url()` in gptqmodel/utils/image.py and route all three
sites through it:
- connect/read timeout defaults (5s, 30s) plus a 120s wall-clock budget that
survives slow-drip responses (watchdog shuts the socket down);
- 32 MiB download cap enforced from Content-Length and while streaming;
- 64 Mpx decode cap checked before `Image.load()` allocates, so decompression
bombs fail instead of spiking RSS;
- retries with exponential backoff for transient faults (timeouts, 408/425/
429/5xx, truncated bodies), no retries for URL/HTTP client errors;
- `ImageFetchError(RuntimeError)` carrying the URL and reason so calibration
failures are diagnosable instead of hanging or raising opaque PIL errors;
- byte/pixel limits overridable via GPTQMODEL_IMAGE_FETCH_MAX_BYTES and
GPTQMODEL_IMAGE_FETCH_MAX_PIXELS.
interns1/ovis2_5 also stop duplicating the download logic and no longer keep
an unclosed streaming response alive via `Image.open(response.raw)`.
Tests: tests/test_image_fetch.py (21 tests) with a local HTTP server covering
hangs, slow drip, oversize declared/streamed bodies, pixel bombs, retry
classification and both model definitions.
github-code-quality flagged four `except ...: pass` blocks added by the previous commit (CodeQL `py/empty-except`): two socket-probe/teardown paths in `_abort_response` and the two expected client-disconnect handlers of the test HTTP server. Behaviour is unchanged; each silent handler now explains why the failure is intentionally ignored. Also add `test_best_effort_except_blocks_are_documented`, which re-runs the CodeQL check (AST, comment window between the `except` line and the last statement of the body) over the files this PR touches, so the pattern cannot regress silently. Verified the helper reports a bare handler and accepts a commented one.
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.
Problem
Multimodal calibration data can reference images by URL. Three call sites fetched them with
requests.get(url, stream=True)and no timeout, no size cap, no retry:gptqmodel/utils/image.py:46(fetch_image, shared by ~15 VL model definitions)gptqmodel/models/definitions/interns1.py:125gptqmodel/models/definitions/ovis2_5.py:99requestsdefaults totimeout=None, so a server that accepts the connection and never responds hangs the whole quantization run forever.Confirmed, not theoretical
Against
main(f41b953), with a local server that accepts the connection and sends nothing:Reachability:
BaseQModel.prepare_dataset→loop_processor.py:287calibration path;fetch_imageis the choke point for qwen2_vl/qwen3_vl/ovis2/minicpmv/internvl_chat/…, and interns1/ovis2_5 had their own duplicate copies.Fix
New
load_image_from_url()ingptqmodel/utils/image.py; all three sites go through it:(5s, 30s), plus a 120 s wall-clock budget enforced by a watchdog that shuts the socket down, so slow-drip responses cannot stretch the download indefinitely;Content-Lengthand while streaming;Image.load()allocates, so decompression bombs fail instead of spiking RSS;ImageFetchError(RuntimeError)carrying URL + reason, so calibration fails diagnosably instead of hanging or raising opaque PIL errors;GPTQMODEL_IMAGE_FETCH_MAX_BYTES/GPTQMODEL_IMAGE_FETCH_MAX_PIXELS.interns1/ovis2_5 no longer duplicate the download logic or keep an unclosed streaming response alive via
Image.open(response.raw).Tests
tests/test_image_fetch.py— 21 tests against a local HTTP server: hanging server, slow drip, declared/streamed oversize, pixel bomb, retry classification, env overrides, and both model definitions.Related suite:
tests/test_image_fetch.py tests/test_prepare_dataset.py tests/test_calibration_data_device.py tests/test_silent_exception_handling.py→ 89 passed.Out of scope (noted, not changed)
gptqmodel/utils/machete.py:217—urllib.request.urlopen(url)has no timeout while downloading the pinned CUTLASS archive during JIT kernel build. Same bug class, but on the kernel-build path rather than calibration; left for a separate change.