Skip to content

Run each test once in the conftest pytest_runtest_call hook - #2174

Open
CriticalRegion wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
CriticalRegion:fix/conftest-runtest-twice
Open

CriticalRegion wants to merge 1 commit into
bitsandbytes-foundation:mainfrom
CriticalRegion:fix/conftest-runtest-twice

Conversation

@CriticalRegion

Copy link
Copy Markdown

pytest_runtest_call in tests/conftest.py calls item.runtest() itself, but it is a plain hook implementation. pytest_runtest_call is not a firstresult hook, so pytest's built-in implementation runs afterwards and calls item.runtest() again. Every test body runs twice, and the second run, the one whose result counts, starts from whatever RNG state the first run left behind instead of the seed.

With a test that appends torch.randn(1) to a module-level list and prints it:

before:  RUN 1 1.5409960746765137
         RUN 2 -0.293428897857666
after:   RUN 1 1.5409960746765137

This makes the hook a wrapper (@pytest.hookimpl(wrapper=True)) that sets the seed and yields to pytest's own implementation. The skip for "Torch not compiled with CUDA enabled" / "Found no NVIDIA driver" still works, since the wrapper sees the exception from the yield (checked with a test raising that assertion: it is reported as skipped).

Full suite on a DGX Spark (GB10, sm_121, CUDA 13.0, torch 2.14.1+cu130), pytest tests -n 4:

  • before: 135 failed, 536 s. 2 of the failures were test_matmul_4bit[M1N256K320-fp16-blocksize=64-fp4-compress_statistics={True,False}-has_bias=False-cuda] (0.1154 < 0.115), which come from the unseeded second run; with the seed the error is 0.0925. The other 133 were local environment issues (an old default g++ for torch.compile on CPU, and torchrun not on PATH).
  • after (with those two environment issues fixed): 6602 passed, 2014 skipped, 62 xfailed, 0 failed, 273 s.

pre-commit run --all-files passes.

The hook called item.runtest() itself and was not a wrapper, so pytest's own pytest_runtest_call ran the test a second time, without the seed being set again. Make it a wrapper that sets the seed and lets pytest run the test.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant