Skip to content

Bound the hub fetch in the GPU inference smoke test - #10796

Closed
xy200303 wants to merge 6 commits into
unslothai:mainfrom
xy200303:fix/gpu-smoke-hub-timeout
Closed

xy200303 wants to merge 6 commits into
unslothai:mainfrom
xy200303:fix/gpu-smoke-hub-timeout

Conversation

@xy200303

Copy link
Copy Markdown
Contributor

What

tests/studio/test_gpu_inference_smoke.py is CUDA-gated, so it only runs where the hub may be slow,
and its fetch had no deadline. A hub connection that is slow rather than failing blocks
AutoTokenizer.from_pretrained(MODEL_ID) indefinitely, which contradicts the docstring's
"download failure is not a code defect" skip. On a self-hosted GPU runner that wedges the whole
suite, and --timeout cannot interrupt a blocked socket read.

Observed on an A100 before the change:

  • the studio suite stalled partway through with no log output for 10+ minutes
  • the test process sat in do_poll — blocked on a socket, not computing
  • ~/.cache/huggingface stayed at 0 bytes, so the download was not progressing
  • --timeout=600 fired but could not break the blocked read

Bisecting tests/studio/ in 15-file chunks put the failure in the chunk containing this file, and
the suite's last logged test was test_gpu_inference_smoke.py::test_gpu_generation_smoke.

Fix

Run the fetch on a daemon thread with a wall-clock deadline and skip when it expires. A daemon
thread is used rather than ThreadPoolExecutor, whose workers are joined at interpreter exit —
that would move the hang to teardown instead of removing it.

Verification

On A100-PCIE-40GB:

  • mechanism: _with_deadline(lambda: time.sleep(30), 2) raises TimeoutError after 2.0s, and the
    normal return path still returns its value
  • real path with the hub pointed at a blackhole (HF_ENDPOINT=http://10.255.255.1:9) and the
    deadline temporarily lowered to 20s: 1 skipped in 20.09s with timed out after 20s — before
    the change this blocked indefinitely
  • the committed deadline is 300s, and no other file in the test is touched

The rest of tests/studio/ is unaffected: the other nine 15-file chunks pass without it (the only
failing chunk was this one).

I searched issues and PRs for this and did not find an existing report.

A hub connection that is slow rather than failing blocks AutoTokenizer.from_pretrained indefinitely, so this test wedges the whole studio suite (and a self-hosted GPU runner's job budget) instead of skipping as its docstring promises. --timeout cannot interrupt a blocked socket read, and the test is gated on CUDA so it only ever runs where the hub may be slow. Run the fetch on a daemon thread with a wall-clock deadline and skip when it expires; a daemon thread is used because ThreadPoolExecutor joins its workers at interpreter exit, which would move the hang to teardown.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fd9bb15374

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +53 to +54
if thread.is_alive():
raise TimeoutError(f"timed out after {timeout}s")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Terminate the model load before reporting the timeout

Whenever this timeout branch is reached, the daemon thread is left running, so the hub fetch is not actually stopped: it can continue writing the cache and, if the connection recovers, proceed to load the model onto CUDA after pytest has already skipped the test. On a long-running self-hosted suite this can leave a cache lock held or consume GPU memory concurrently with later tests. Use a cancellable subprocess or underlying client timeouts so the timed-out load is terminated before returning.

Useful? React with 👍 / 👎.

try:
def load():
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, dtype = dtype).to("cuda")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the selected CUDA device in the loader thread

When a multi-GPU runner selects a nonzero device with torch.cuda.set_device() rather than masking devices with CUDA_VISIBLE_DEVICES, that selection is local to the main host thread; this new loader thread starts with CUDA device 0, so .to("cuda") places the model on GPU 0 while the later main-thread .to("cuda") places inputs on the selected GPU. Generation then fails with a device mismatch, and parallel workers can also pile onto GPU 0. Capture the main thread's device and use an explicit indexed device in both locations.

Useful? React with 👍 / 👎.


try:
# offline / gated / slow hub access is not a code defect
tokenizer, model = _with_deadline(load, FETCH_TIMEOUT_SECONDS)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep local GPU loading outside the hub timeout

When the model is already cached but deserialization or the CUDA transfer stalls for more than 300 seconds, the entire load callback times out here and the surrounding handler reports the smoke test as skipped for an unreachable model. That converts the model/GPU-loading regression this smoke is intended to detect into a passing CI outcome. Apply the reachability deadline only to the download stage, then perform cached model construction and GPU placement under the normal pytest failure/timeout path.

Useful? React with 👍 / 👎.

xy200303 and others added 4 commits September 11, 2026 22:40
The deadline wrapped the whole load on a daemon thread, which had three
consequences:

- A thread cannot be interrupted, so a stalled fetch kept running after the
  test had already skipped. It could still write the cache, and once the
  connection recovered it could still load onto CUDA.
- torch.cuda.set_device() is per-thread, so a runner that selected a nonzero
  device got the model on device 0 while the inputs went to the selected one.
- Bounding construction and the CUDA transfer as well turned a stalled local
  load into a skip, hiding the very regression this smoke exists to catch.

The fetch now runs in a child process that is killed at the deadline, and it
covers only populating the hub cache. Construction, placement and generation
run on the test's own thread, with no deadline, so a local failure fails.
@danielhanchen danielhanchen self-assigned this Oct 12, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Thank you for the PR and for the detailed write-up! After checking it, we will not be merging this one, for these reasons:

  1. The hub fetch is already bounded. With HF_ENDPOINT=http://10.255.255.1:9 (the same blackhole used in the PR description) and a fresh HF_HOME, AutoTokenizer.from_pretrained("unsloth/gemma-3-270m-it") on the current code raises ConnectTimeout: [Errno 110] Connection timed out after about 2m47s (huggingface_hub 1.33.0, transformers 5.17.0), so the existing except Exception already turns it into a skip. huggingface_hub also applies its own read timeout to every request, so a stalled download raises rather than waiting forever.
  2. pytest --timeout does interrupt a blocked socket read. A test that blocks on socket.socketpair()[0].recv(1) with --timeout=5 fails at 5.0s with --timeout-method=signal and is killed at 5.1s with --timeout-method=thread.
  3. This smoke test is not run by any CI workflow, so a slow hub cannot wedge a shared runner through it. Adding a subprocess prefetch, which loads the model twice, is more machinery than the risk justifies.

The stall you saw on the A100 was probably caused by something in that environment, for example a proxy, or HF_HOME pointing somewhere other than ~/.cache/huggingface, which would explain the cache staying at 0 bytes. If you can reproduce an indefinite hang on current main with a plain pytest --timeout, please open an issue with the stack trace and we will look at it again. Thanks again for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants