Skip to content

Size the ONNX Runtime thread pool to the cpuset, and re-enable the onnx job - #198915

Draft
huydhn wants to merge 1 commit into
pytorch:mainfrom
huydhn:osdc/onnx-ort-threads
Draft

huydhn wants to merge 1 commit into
pytorch:mainfrom
huydhn:osdc/onnx-ort-threads

Conversation

@huydhn

@huydhn huydhn commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

ONNX Runtime sizes its intra-op thread pool from std::thread::hardware_concurrency(), which reads _SC_NPROCESSORS_ONLN and counts every CPU on the machine. On a Kubernetes runner the job holds an exclusive cpuset a fraction that size, so ORT starts a thread per host CPU and then fails to pin each one past the cpuset edge:

[E:onnxruntime:Default, env.cc:226 ThreadMain] pthread_setaffinity_np failed for
thread: 110251, index: 46, mask: {47, }, error code: 22 error msg: Invalid
argument. Specify the number of threads explicitly so the affinity is not set.

In this run that was 18,938 lines of a 24,895-line job log, alongside a bidirectional-RNN test failing on numerics — which a thread pool in that state is a plausible cause of, though this PR does not prove that.

sched_getaffinity is cpuset-aware and reports the number ORT should have used, so torch.onnx._internal._ort_threads returns it and the five places that build a session pass it as intra_op_num_threads — which is what ORT's own error message asks for. Two of those are in torch.onnx rather than the tests, so this also covers anyone running an exported model inside a container with a CPU limit. Off a cpuset the two counts agree and nothing changes; the helper falls back to os.cpu_count() where sched_getaffinity does not exist.

#198706 commented the onnx entry out of pull.yml, naming this as the thing that had to be fixed first. That reason is now addressed, so the entry comes back, on the runner it had before.

Testing

Draft, because the real test is CI: the onnx job runs again on this PR, and the question is whether the affinity spam is gone and the RNN test passes. If the spam clears but that test still fails, it is a separate bug and this should not re-enable onnx on its own.

Locally verified:

  • PYFMT (tools/linter/adapters/pyfmt_linter.py) clean on all 6 Python files
  • ruff check / ruff format --check clean, baselined against the unmodified files from main
  • pull.yml parses; the clang21 matrix is back to 24 entries with onnx present
  • every _ort_threads.intra_op_num_threads() use has a matching import

Authored with Claude Code.

@pytorch-bot

pytorch-bot Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/198915

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Unclassified Failure

As of commit 2cdb862 with merge base 9a9b93a (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added the release notes: releng release notes category label Sep 28, 2026
…nx job

ONNX Runtime sizes its intra-op thread pool from
std::thread::hardware_concurrency(), which reads _SC_NPROCESSORS_ONLN and
counts every CPU on the machine. On a Kubernetes runner the job holds an
exclusive cpuset a fraction that size, so ORT starts a thread per host CPU and
then fails to pin each one past the cpuset edge:

    [E:onnxruntime:Default, env.cc:226 ThreadMain] pthread_setaffinity_np
    failed for thread: 110251, index: 46, mask: {47, }, error code: 22
    error msg: Invalid argument. Specify the number of threads explicitly so
    the affinity is not set.

In a recent run that was 18,938 lines of a 24,895-line job log, alongside a
bidirectional-RNN test failing on numerics -- which a thread pool in that
state is a plausible cause of, though that is not proven here.

sched_getaffinity is cpuset-aware and reports the number ORT should have used,
so torch.onnx._internal._ort_threads returns it and the five places that build
a session pass it as intra_op_num_threads. This is what ORT's own error
message asks for. Two of those places are in torch.onnx rather than the tests,
so it also covers anyone running an exported model inside a container with a
CPU limit. Off a cpuset the two counts agree and nothing changes; the helper
falls back to os.cpu_count() where sched_getaffinity does not exist.

pytorch#198706 commented the onnx entry out of pull.yml, naming this as the thing
that had to be fixed first. That reason is now addressed, so the entry comes
back, on the runner it had before.

Authored with Claude Code.
@huydhn
huydhn force-pushed the osdc/onnx-ort-threads branch from ab8267f to 2cdb862 Compare September 28, 2026 19:04
huydhn added a commit that referenced this pull request Sep 28, 2026
With every workflow naming its pod, the mapper has nothing left to translate.
Its other behaviours are gone too: the ROCm and XPU passthroughs return their
input unchanged, and the H100 `mt-` forcing is now literal in the workflows.

One behaviour does disappear with it. The script dropped `config: "onnx"` from
every ARC matrix, so that job has not run since the ARC switch. The entry is
removed outright here rather than left behind commented out, and the reason it
is gone -- onnxruntime sizes its intra-op thread pool from
`hardware_concurrency()`, which reads `_SC_NPROCESSORS_ONLN` and counts the
node rather than the pod's cpuset -- is recorded in #198915, which fixes that
and restores the entry.

An earlier version of this change did leave a commented-out entry with a prose
comment above it, and that is what got it reverted. A test-matrix is a literal
block scalar, interpolated verbatim into a double-quoted shell argument:

    --test-matrix "{ include: [ ... ]}"

Every `"` inside toggles shell quoting. The live entries survive because their
quotes wrap tokens with no spaces, so the fragments re-concatenate into one
argument; the comment contained a quoted phrase with a space in it, the shell
split there, and filter_test_configs.py got a stray argument:

    error: unrecognized arguments: failed in a 24k-line log, and a

Letting onnx run first was still worth it, because it found a second reason it
could not have passed. The OSDC pod exports `HF_HOME=/mnt/hf_cache`, the
read-only mount, and .ci/pytorch/test.sh is what redirects it to the writable
`HF_CACHE` the workflow prepares -- except it skipped onnx, a carve-out from
#178928 when the old .ci/onnx/test.sh (which never set HF_HOME) was folded in.
On EC2 that was harmless, since HF_HOME was unset and huggingface fell back to
a writable home; on a pod onnx alone writes to the read-only mount and dies on
`[Errno 30]`. That carve-out is dropped: it is wrong for any config.

arc.yaml stays: it documents the naming scheme and the EC2 correspondence,
which is still worth having as reference.

ghstack-source-id: c44c11d
Pull-Request: #198706
pytorchmergebot pushed a commit that referenced this pull request Sep 28, 2026
With every workflow naming its pod, the mapper has nothing left to translate.
Its other behaviours are gone too: the ROCm and XPU passthroughs return their
input unchanged, and the H100 `mt-` forcing is now literal in the workflows.

One behaviour does disappear with it. The script dropped `config: "onnx"` from
every ARC matrix, so that job has not run since the ARC switch. The entry is
removed outright here rather than left behind commented out, and the reason it
is gone -- onnxruntime sizes its intra-op thread pool from
`hardware_concurrency()`, which reads `_SC_NPROCESSORS_ONLN` and counts the
node rather than the pod's cpuset -- is recorded in #198915, which fixes that
and restores the entry.

An earlier version of this change did leave a commented-out entry with a prose
comment above it, and that is what got it reverted. A test-matrix is a literal
block scalar, interpolated verbatim into a double-quoted shell argument:

    --test-matrix "{ include: [ ... ]}"

Every `"` inside toggles shell quoting. The live entries survive because their
quotes wrap tokens with no spaces, so the fragments re-concatenate into one
argument; the comment contained a quoted phrase with a space in it, the shell
split there, and filter_test_configs.py got a stray argument:

    error: unrecognized arguments: failed in a 24k-line log, and a

Letting onnx run first was still worth it, because it found a second reason it
could not have passed. The OSDC pod exports `HF_HOME=/mnt/hf_cache`, the
read-only mount, and .ci/pytorch/test.sh is what redirects it to the writable
`HF_CACHE` the workflow prepares -- except it skipped onnx, a carve-out from
#178928 when the old .ci/onnx/test.sh (which never set HF_HOME) was folded in.
On EC2 that was harmless, since HF_HOME was unset and huggingface fell back to
a writable home; on a pod onnx alone writes to the read-only mount and dies on
`[Errno 30]`. That carve-out is dropped: it is wrong for any config.

arc.yaml stays: it documents the naming scheme and the EC2 correspondence,
which is still worth having as reference.
Pull Request resolved: #198706
Approved by: https://github.com/jeanschmidt
ghstack dependencies: #198704

This branch has not been deployed

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

Labels

release notes: releng release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant