Explicit (lo, hi) ranges containing inf/nan are still accepted where the scalar form now rejects them
Follow-up to #4621 / PR #4625.
PR #4625 makes _range_bound reject a non-finite scalar magnitude (factor.dim() == 0), so RandomSharpness(float("inf")) now raises ValueError at construction. The equivalent explicit range form takes the non-scalar branch and is still accepted:
RandomSharpness((0.0, float("inf"))) # accepted; UniformDistribution(0, inf) -> NaN at forward
ColorJitter(contrast=(0.0, float("inf"))) # accepted; NaN at forward
RandomAffine(0., scale=(0., float("inf"))) # accepted; NaN at forward
_range_bound((0.0, float("inf")), "param") # -> tensor([0., inf])So the scalar and pair forms of the same parameter now disagree — the pair form can still build an infinite draw range and produce inf * 0 -> NaN in the forward blend, exactly the defect #4621 reported for the scalar form. _range_bound's docstring also still describes the two forms as agreeing.
Possible directions (not mutually exclusive):
- extend the finiteness check to the pair branch of
_range_bound(and decide whatbounds=(0, inf)means for an explicit(0, inf)range — arguably legal as a half-open domain declaration rather than a draw range); - or check finiteness where the draw range is consumed (the
UniformDistribution/random_generatorsites), which would also catchnanendpoints; - update the
_range_bounddocstring whichever way the semantics land.
Reproduced on main at 8c71df9a0 (CPU, torch 2.14): forward on torch.rand(2, 3, 8, 8) with RandomSharpness((0.0, float("inf")), p=1.0) returns a tensor with NaN fraction 0.4375.
Source: kornia/kornia