Repository navigation
[Test] Keep the products in test_reduction_empty_dim finite, so the prod case cannot fail at random - #3045
Merged
peterdsharpe merged 2 commits intoOct 19, 2026
Conversation
60 values up to 9 overflow float32, and torch.prod then gives nan or 0.0 depending on the order in which it multiplies, so the prod case failed for some seeds. Map the leaves to 1 + 0.01 * x for prod.
A lazy stack multiplies per member and then across members. Over hundreds of float32 values near 1, that order differs from torch.prod's by up to about n * eps, above assert_close's default rtol of 1.3e-6.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TestTensorDicts::test_reduction_empty_dim[prod-sub_td2-*]failed once on the free-threaded 3.15 job with "Scalars are not close! Expected nan but got 0.0." It passed on rerun. It is a flaky test, not a bug inprod.What was wrong
The test compares
td.prod(dim=())withtorch.prod(leaf)over all the elements of each leaf, on random data. Insub_td2, the leafcholds 60 values fromtorch.randint(10, ...)converted to float. The exact product is 0 as soon as one value is 0. But 60 values up to 9 can reach 9**60, about 1e57, which is past float32's maximum of about 3e38. Whentorch.prodmultiplies in an order where one partial product overflows toinfbefore it meets a 0, the result isinf * 0 = nan. In another order it is 0.0. The tensordict path and the directtorch.prodcall on the strided view multiply in different orders, so with some seeds one gives nan and the other 0.0.With the fixture's data, I counted 1 such seed in 3000 (seed 2123: tensordict 0.0,
torch.prodnan).The fix
For
prodonly, the test maps every leaf to1 + 0.01 * xin place before it compares, undertorch.no_grad()fortd_params. All the products then stay finite and nonzero.The order still changes the last bits. A lazy stack multiplies per member and then across members, and over hundreds of float32 values the result can differ from
torch.prod's by up to aboutn * eps, aboveassert_close's float32 default ofrtol=1.3e-6. So theprodcomparisons usertol=1e-4, atol=1e-5. The other reductions and the rest of the test are unchanged.What I ran
test/tensordict/test_methods.py -k test_reduction_empty_dimwith CI's environment variables,-n 2: 171 passed, 9 skipped (td_with_unbatched).sub_td2setup of the test, with seeds 0 to 5999 and the new rescaling: 0 mismatches. Without it: 1 in 3000.prod, called for each of the 20 CPUtd_names with seeds 0 to 199 (4000 calls), on Python 3.12 with torch 2.14.1 and on Python 3.10 with torch 2.11.0: 0 failures. With the rescaling but the default tolerance, on torch 2.11: 16 failures, instacked_td(5) andnested_stacked_td(11), as in CI'stest-oldest-cpujob.