Skip to content

Commit 5a61f40

Browse files
authored
Perf(test): reuse random data across container types in quantile test (#8985)
## Description `test_many_elements_quantile` allocated a fresh `np.random.randn(elements)` array — up to 17M elements (~130 MB) — inside the innermost loop, so the same large array was regenerated once for every `(container type, element count, quantile-arg type)` combination. The random *values* are irrelevant to the test (assertions only check the return type and that CPU/GPU results agree), so the repeated allocation and RNG fill added runtime without adding coverage. This makes for the data generation to once per element count and reuses it across the container and argument types. ```diff @skip_if_quick def test_many_elements_quantile(self): # pytorch#64947 - for p in TEST_NDARRAYS: - for elements in (1000, 17_000_000): + for elements in (1000, 17_000_000): + data = np.random.randn(elements) + for p in TEST_NDARRAYS: + x = p(data) for t in [*TEST_NDARRAYS, list]: - x = p(np.random.randn(elements)) q = percentile(x, t([10, 50])) ``` ## Performance Wall-clock of `pytest tests/transforms/test_utils_pytorch_numpy_unification.py`, median of 3 runs on the same machine: | | Median wall (3 runs) | Individual runs | | --- | --- | --- | | Before | 17.16s | 18.36, 16.74, 17.16 | | After | 12.01s | 12.19, 11.99, 12.01 | **Machine:** 12th Gen Intel Core i7-12800H (20 threads), Ubuntu 22.04.5 (kernel 6.8.0-124), Python 3.10.12, torch 2.10.0+cu128, CUDA available (NVIDIA RTX A1000 Laptop GPU). ## Types of changes - [x] Non-breaking change (test-only; no functional code touched) - [x] Tests pass locally (`pytest tests/transforms/test_utils_pytorch_numpy_unification.py`) Signed-off-by: Soumya Snigdha Kundu <soumya_snigdha.kundu@kcl.ac.uk>
1 parent 20a9632 commit 5a61f40

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

‎tests/transforms/test_utils_pytorch_numpy_unification.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ def test_percentile(self):
5151

5252
@skip_if_quick
5353
def test_many_elements_quantile(self): # pytorch#64947
54-
for p in TEST_NDARRAYS:
55-
for elements in (1000, 17_000_000):
54+
for elements in (1000, 17_000_000):
55+
data = np.random.randn(elements)
56+
for p in TEST_NDARRAYS:
57+
x = p(data)
5658
for t in [*TEST_NDARRAYS, list]:
57-
x = p(np.random.randn(elements))
5859
q = percentile(x, t([10, 50]))
5960
if isinstance(x, torch.Tensor):
6061
self.assertIsInstance(q, torch.Tensor)

0 commit comments

Comments
 (0)