ort-transformers-gpu-pytest
Skill22k starsChanged 3 months agoMITView on GitHub
Run the ONNX Runtime transformers Python tests (onnxruntime/test/python/transformers) against a GPU wheel, and confirm real cuDNN/flash SDPA dispatch. Use when a transformers pytest fails with ModuleNotFoundError onnxruntime.capi, when torch's bundled CUDA/cuDNN libs shadow the ORT-built ones (wrong-version dispatch or load errors), or when you must PROVE a test exercised the cuDNN SDPA decode tier instead of silently skipping/falling back to MATH.
Trending
Less talk lately.about the whole repository
Who's talking about microsoft/onnxruntime
- A Hacker News story“ONNX Runtime merges WebGPU backend”Hacker News · Apr 24, 2023 · 178 points
- immich-app/immich“feat(ml): introduce support of onnxruntime-rocm for AMD GPU”GitHub · Jul 12, 2024 · 28 reactions
- Unstructured-IO/unstructured“feat/numpy_2”GitHub · Oct 1, 2024 · 17 reactions
- microsoft/vcpkg“[onnxruntime] new ports for microsoft/onnxruntime v1.23.2”GitHub · Feb 19, 2024 · 17 reactions
- huggingface/transformers.js“Can GPU acceleration be used when using this library in a node.js environment?”GitHub · Feb 7, 2024 · 17 reactions
The biggest 5 of 39 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 39
Talked about on GitHub, Hacker News, Bluesky and the web (39 times).
On GitHub · 32
- Jul 12, 2024feat(ml): introduce support of onnxruntime-rocm for AMD GPU — immich-app/immich28 reactions · 58 comments
- Oct 1, 2024feat/numpy_2 — Unstructured-IO/unstructured17 reactions · 10 comments
- Feb 19, 2024[onnxruntime] new ports for microsoft/onnxruntime v1.23.2 — microsoft/vcpkg17 reactions · 80 comments
- Feb 7, 2024Can GPU acceleration be used when using this library in a node.js environment? — huggingface/transformers.js17 reactions · 6 comments
- Jan 21, 2024FAQ/GUIDE: I've figured out why CUDA isn't available in Insightface! — cubiq/ComfyUI_IPAdapter_plus17 reactions · 14 comments
27 more on GitHub
- Jan 8, 2020[request] some of most relevant c++ libraries in github (by number of stars) — conan-io/conan-center-index16 reactions · 132 comments
- Jun 18, 2024feat: add support for custom ai models | Pipeline API RFC — supabase/edge-runtime14 reactions · 18 comments
- Mar 16, 2021Add support for anchor tags — lycheeverse/lychee13 reactions · 39 comments
- May 14, 2024wanted: microsoft/onnxruntime — bazelbuild/bazel-central-registry11 reactions · 3 comments
- Mar 6, 2021[request] onnxruntime/1.7.1 — conan-io/conan-center-index11 reactions · 11 comments
- Nov 13, 2025Getting wrong .NET version — actions/setup-dotnet10 reactions · 18 comments
- May 8, 2025Enable Android 16 KB page size support — microsoft/onnxruntime-extensions9 reactions · 14 comments
- Apr 11, 2024Expose onnxruntime session options (intra_op_num_threads) — deepinsight/insightface9 reactions
- Sep 16, 2020Exporting the pytorch grid_sample operator to ONNX opset version 9 is not supported — pytorch/pytorch7 reactions · 1 comment
- Dec 29, 2023wasm does not work on node right now with multiple threads — huggingface/transformers.js6 reactions · 3 comments
- Mar 22, 2022Define layer normalization as a function — onnx/onnx6 reactions
- Dec 2, 2020[onnxruntime] Add new port — microsoft/vcpkg6 reactions · 28 comments
- Jul 28, 2026(partially) fix #23462: configure onnxruntime to stop VRAM usage from growing — immich-app/immich5 reactions · 4 comments
- Jul 23, 2024windows 10 installation tutorial (win10 安装教程) — warmshao/FasterLivePortrait5 reactions · 1 comment
- Nov 30, 2023[ROCm] TunableOp — pytorch/pytorch5 reactions · 31 comments
- Apr 15, 2025[DO NOT SUBMIT] Model cache POC — shiyi9801/chromium4 reactions · 2 comments
- Nov 19, 2024GafferML — GafferHQ/gaffer4 reactions · 3 comments
- Apr 15, 2026Add support for q1, q1f16, q2, q2f16 data types — huggingface/transformers.js3 reactions · 1 comment
- Dec 26, 2025onnx and onnxruntime bump versions (fixed onnx dependency with shared=True) — conan-io/conan-center-index3 reactions
- Nov 4, 2025python3Packages.rapidocr-onnxruntime: fix aarch64-linux build — NixOS/nixpkgs3 reactions · 7 comments
- Feb 26, 2025introduce "--webnn-ort-ov-gpu-precision" to specify gpu precision — shiyi9801/chromium3 reactions · 1 comment
- Feb 4, 2025Add: WASM Emscripten example — pykeio/ort3 reactions · 33 comments
- Feb 1, 2025Object mask — darktable-org/darktable3 reactions · 5 comments
- Sep 23, 2024C, C++, and Python API for Adapters (Multi-LoRA and others) — microsoft/onnxruntime-genai3 reactions
- Dec 12, 2023relax protobuf restriction — microsoft/onnxconverter-common3 reactions · 12 comments
- Apr 1, 2023onnxruntime: add 1.14.1 — conan-io/conan-center-index3 reactions · 36 comments
- Jan 14, 2022Add Col2Im operator — onnx/onnx3 reactions · 21 comments
On Hacker News · 5
- Apr 24, 2023ONNX Runtime merges WebGPU backend178 points · 35 comments
- Jan 19, 2020ONNX Runtime: cross-platform, high performance scoring engine for ML models8 points · 1 comment
- Sep 26, 2025ONNX Runtime v1.23.0 Released4 points
- Dec 11, 2020ONNX Runtime v1.6.0 Release1 point
- Oct 31, 2019Microsoft collects telemetry in onnxruntime Python module1 point
On Bluesky · 1
- Aug 22, 2026📦 microsoft / onnxruntime ⭐ 21,420 (+5) 🗒 C++ ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator — @github-trending.bsky.social1 likes and repost
On the web · 1
- Oct 21, 2026microsoft/onnxruntime - GitHub Explorer — ghe.derekchia.com
In this repository15 other files it gives its agents.
- AGENTS.mdAGENTS.md
- .github/copilot-instructions.mdCopilot instructions
- code-reviewSkill
- contrib-op-shape-inference-memory-safetySkill
- cuda-attention-kernel-patternsSkill
- cuda-cutlass-fmha-incremental-rebuildSkill
- onnx-opset-bump-checklistSkill
- ort-buildSkill
- ort-ciSkill
- ort-lintSkill
- ort-release-notesSkill
- ort-testSkill
- python-kwargs-setattr-securitySkill
- webgpu-local-testingSkill
- workspace-estimation-shared-headerSkill
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 1 is distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 29
Fix: run pytest from a neutral, private working directory — a fresh mktemp -d, not the repo root and not a bare shared /tmp — so the …
- Very specific: line 46
Do not cd into the repo and run pytest onnxruntime/test/...
- Very specific: line 85
Keep the two exports and the preload together.
- Very specific: line 136
Prefer this over reading torch.backends.cudnn.version() or any library-version check: a version probe reads torch's cuDNN, not the cuDNN …
- Distinctive: line 169
Run both ways to prove it works AND bites:
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Running ONNX Runtime transformers GPU pytest (dispatch-verified)
Reusable, hard-won knowledge for running the Python transformers tests under
onnxruntime/test/python/transformers/ against a GPU-built wheel, on a box where
torch is also installed. Three gotchas below will each cost you an hour if
rediscovered. See the ort-test skill for the general test taxonomy and the
false-green modes; this skill is the GPU + transformers + SDPA-dispatch specialization.
1. NEUTRAL-CWD gotcha — never run pytest from the repo root
Symptom:
ModuleNotFoundError: No module named 'onnxruntime.capi'
# or an AttributeError deep inside onnxruntime importCause: the repo root contains a source directory ./onnxruntime/ (the C++/Python
source tree). When pytest runs with the repo root on sys.path[0], import onnxruntime
resolves to that source package — which has no compiled capi extension — instead
of the installed wheel in your venv's site-packages. The source dir shadows the
wheel.
Fix: run pytest from a neutral, private working directory — a fresh
mktemp -d, not the repo root and not a bare shared /tmp — so the shadowing source
dir is not on the path, and point at the test file by absolute path. Put the
transformers test-helper dir on PYTHONPATH so shared helpers still import.
WORKDIR=$(mktemp -d); cd "$WORKDIR" # NEUTRAL + PRIVATE cwd — NOT repo root, NOT /tmp
export PYTHONPATH=/abs/repo/onnxruntime/test/python/transformers
python -m pytest /abs/repo/onnxruntime/test/python/transformers/<file>.py -vWhy mktemp -d and not a bare cd /tmp: pytest prepends the cwd to sys.path, and
Python auto-imports sitecustomize.py/usercustomize.py from it at startup — so on a
shared box a co-tenant's planted /tmp/sitecustomize.py would execute as arbitrary code
in your test process. A fresh per-run mktemp -d (private, 0700) keeps the neutral-cwd
source-shadow protection while removing that injection vector.
Do not cd into the repo and run pytest onnxruntime/test/... — that reintroduces
the shadowing. (This is the Python analogue of the C++ "run from the build output dir"
rule in ort-test.)
2. LD_PRELOAD lib-pinning gotcha — torch's CUDA/cuDNN shadow ORT's
Symptom: any of —
- the SDPA decode tier silently routes to
MATHinstead ofCUDNN_FLASH_ATTENTION(wrong-version cuDNN loaded), or libcudnn.so.9: cannot open shared object file/undefined symbol/ cuDNN version mismatch errors at first CUDA op, or- ORT loads a different CUDA runtime than it was built against.
Cause: a pip-installed torch ships its own bundled CUDA runtime + cuDNN
(e.g. cu124 → CUDA 12.4 / cuDNN 9.1) under site-packages/nvidia/*/lib. If ORT was
built against a different CUDA/cuDNN (e.g. CUDA 12.9 / cuDNN 9.8), whichever set the
dynamic loader resolves first wins. With torch imported (or its libs on the path),
torch's older libs can shadow the ones ORT dlopens → wrong-version dispatch or load
failure.
Fix: LD_PRELOAD the system CUDA runtime + cuDNN that ORT was built against so
they are loaded first, and add their dirs to LD_LIBRARY_PATH. Activate the venv that
has the ORT wheel. Concrete form used successfully (CUDA 12.9 + cuDNN 9.8; substitute
your absolute lib paths):
WORKDIR=$(mktemp -d); cd "$WORKDIR" # neutral + private (see §1)
source /abs/repo/.venv/bin/activate
export LD_PRELOAD=/abs/cuda12.9/lib64/libcudart.so.12:/abs/cudnn9.8/lib/libcudnn.so.9
export LD_LIBRARY_PATH=/abs/cuda12.9/lib64:/abs/cudnn9.8/lib
export PYTHONPATH=/abs/repo/onnxruntime/test/python/transformers
python -m pytest /abs/repo/onnxruntime/test/python/transformers/<file>.py -vNotes:
libcudart.so.12is correct for both CUDA 12.4 and 12.9 (SONAME is major-only) — pinning the 12.9 file forces the right minor.torch.cudastill works fine under this preload — the bf16 IO-binding path that usestorchtensors +.data_ptr()runs correctly.- Keep the two exports and the preload together; dropping
LD_LIBRARY_PATHcan still let a transitive dependency resolve against torch's copy.
3. Confirm REAL cuDNN SDPA dispatch (don't trust value-equality)
A numerically-correct result does not prove the cuDNN SDPA path ran — the kernel has
a MATH fallback that produces the same answer (false-green mode 4 in ort-test). To
prove the tier dispatched, observe ORT's routing rather than probing a version.
Observe-dispatch (the correct probe)
ORT's ONNX-domain Attention kernel emits a debug line when
ORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO=1 is set before the InferenceSession is
created (the option is read once at session creation). AttentionKernelDebugInfo::Print
emits a token of the form:
SdpaKernel=CUDNN_FLASH_ATTENTION # or =MATH, =FLASH_ATTENTION, =EFFICIENT_ATTENTION (non-exhaustive)Capture stdout across a single run() and parse it. Capture at the file-descriptor
level, not contextlib.redirect_stdout: the SdpaKernel= line is written to native
fd-1 from C++, which Python-level stdout redirection never intercepts — you would get
dispatched=None, a silent false-negative. Mirror ORT's own _CaptureStdout
(os.dup2 fd-1 to a temp file, run, restore, read it back):
os.environ["ORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO"] = "1" # BEFORE InferenceSession()
# FD-level capture (see onnxruntime's _CaptureStdout for the exact idiom):
saved_fd = os.dup(1)
tmp = tempfile.TemporaryFile()
os.dup2(tmp.fileno(), 1) # redirect native fd-1
try:
# ... create session, run once ...
finally:
os.dup2(saved_fd, 1) # restore fd-1
os.close(saved_fd)
tmp.seek(0)
captured_text = tmp.read().decode()
m = re.search(r"SdpaKernel=(?P<kernel>[A-Z_]+)", captured_text)
dispatched = m.group("kernel") if m else None
assert dispatched == "CUDNN_FLASH_ATTENTION"Caveat: re.search returns only the first SdpaKernel= token — correct for the
single-node decode probe here. For a graph with multiple attention nodes use
re.findall and check every token, or a later node's MATH fallback is masked by an
earlier cuDNN hit.
Prefer this over reading torch.backends.cudnn.version() or any library-version check:
a version probe reads torch's cuDNN, not the cuDNN ORT actually loaded/dispatched —
that mismatch is a real trustworthiness bug. Observe-dispatch reads ORT's own routing
decision, so it is correct across cuDNN versions with no hard-coded version table.
Non-skippable canary — ORT_TEST_REQUIRE_CUDNN_SDPA
Gating decode tests by observed dispatch has a failure mode: if the tier silently regresses (stops selecting cuDNN), the observation returns "not dispatched" and every decode test skips green, hiding the regression as all-green skips.
Close the hole with an env-gated canary: ORT_TEST_REQUIRE_CUDNN_SDPA=1. When set, the
dispatch assertion becomes non-skippable — a MATH fallback / non-dispatch on the
minimal known-good config FAILS LOUD instead of skipping. When unset (dev boxes,
unsupported cuDNN) it falls back to the normal skip guard so it never false-alarms.
The variable is intended for an operator to export on a known-good GPU CI leg once one exists. Note that today no ONNX Runtime pipeline definition exports it (there is no Hopper+ GPU CI leg), so it has no effect in this project's CI and only matters for manual/local runs where a developer sets it explicitly — don't describe it in test docstrings as an enforcement that CI already applies.
def require_cudnn_sdpa():
return os.environ.get("ORT_TEST_REQUIRE_CUDNN_SDPA") == "1"
# in the test:
enforce = require_cudnn_sdpa()
if not enforce and not cudnn_decode_supported(head_size): # illustrative: your suite's own support predicate
self.skipTest("cuDNN SDPA decode tier not dispatched; set ORT_TEST_REQUIRE_CUDNN_SDPA=1 to enforce")
# then assert dispatch == CUDNN_FLASH_ATTENTION unconditionallyRun both ways to prove it works AND bites:
python -m pytest <file>.py -v # normal: skips where unsupported
ORT_TEST_REQUIRE_CUDNN_SDPA=1 python -m pytest <file>.py -v # enforced: fails if not cuDNNProve the teeth. A canary you never watched fail is not verified. Force MATH-only
by setting the CUDA provider's sdpa_kernel provider option to the MATH bitmask
(16) — a monkeypatch of the C++ selector is not reachable from Python — under
ORT_TEST_REQUIRE_CUDNN_SDPA=1, and confirm it fails with, verbatim:
AssertionError: 'CUDNN_FLASH_ATTENTION' != 'MATH'A run that never demonstrates this failure has not proven the canary has teeth (grounding rule: negative/teeth evidence must actually be observed, not asserted).
4. Putting it together — one clean run block
WORKDIR=$(mktemp -d); cd "$WORKDIR" # neutral + private (see §1)
source /abs/repo/.venv/bin/activate
export LD_PRELOAD=/abs/cuda12.9/lib64/libcudart.so.12:/abs/cudnn9.8/lib/libcudnn.so.9
export LD_LIBRARY_PATH=/abs/cuda12.9/lib64:/abs/cudnn9.8/lib
export PYTHONPATH=/abs/repo/onnxruntime/test/python/transformers
F=/abs/repo/onnxruntime/test/python/transformers/<file>.py
python -m pytest "$F" -v # A: normal
ORT_TEST_REQUIRE_CUDNN_SDPA=1 python -m pytest "$F" -v # B: canary active, non-skippable
# C: teeth — force MATH under the env var, expect the AssertionError aboveCheck the passed count, not just the exit code. pytest -v exits 0 even if every
test skipped (no CUDA, or an unmet @skipUnless(ml_dtypes) guard) — the saved log then
looks like passing evidence but proves nothing. Require a non-zero passed count and
zero unexpected skips, and note pytest exit code 5 = "no tests collected" (usually a
wrong path or -k filter, not success). RUN B's canary only converts dispatch-related
skips into failures — it does not rescue collection or environment skips, so still
read the summary line.
Redirect to a log (... 2>&1 | tee "$WORKDIR/gpu_run.log") — the debug-info stdout and
pytest output are large, and a saved log is the evidence that the run happened and
dispatched to cuDNN. Write it inside $WORKDIR (the mktemp -d above), not a
predictable /tmp/gpu_run.log a co-tenant could pre-create as a symlink to clobber.
Gotcha quick-reference
| Symptom | Root cause | Fix |
ModuleNotFoundError: onnxruntime.capi | repo-root ./onnxruntime/ source shadows the wheel | run pytest from a private mktemp -d (not repo root, not bare /tmp); abs path + PYTHONPATH |
routes to MATH / cuDNN load error | torch's bundled CUDA/cuDNN shadow ORT's | LD_PRELOAD system libcudart.so.12 + libcudnn.so.9, set LD_LIBRARY_PATH |
| test passes but path unproven | MATH fallback gives same numbers | observe SdpaKernel= via ORT_ENABLE_ATTENTION_KERNEL_DEBUG_INFO=1 |
| all tests skip green, regression hidden | dispatch-gated skip | ORT_TEST_REQUIRE_CUDNN_SDPA=1 makes assertions non-skippable |
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

