torch._numpy.geomspace(..., num=1) raises ZeroDivisionError
- Dominant language
- Python
- Stars
- 103k
- Forks
- 29.5k
- PR merge metrics
- PR metrics pending
Description
I confirmed this against NumPy 2.2.6 and the current `geomspace` impl on pytorch `main`. NumPy returns `start` for `num=1`; the in-tree code divides by `num - 1`. Drafted with AI assistance; I reviewed the repro and the NumPy contract.
> ### 🐛 Describe the bug
>
> `torch._numpy.geomspace(1, 1000, num=1)` crashes. NumPy returns a one-element array of `start`.
>
> ```python
> import numpy as np
> import torch._numpy as tnp
>
> np.geomspace(1, 1000, num=1) # array([1.])
> tnp.geomspace(1, 1000, num=1) # ZeroDivisionError: float division by zero
> ```
>
> `tnp.geomspace(1, 1000, num=5)` is fine. The impl computes the ratio as `1.0 / (num - 1)` before calling `torch.logspace`:
>
> ```python
> # torch/_numpy/_funcs_impl.py
> base = torch.pow(stop / start, 1.0 / (num - 1))
> ```
>
> Same function also accepts `dtype=` but never forwards it to `torch.logspace`. The sibling `logspace` wrapper does pass `dtype`.
>
> ```python
> tnp.geomspace(1, 1000, num=5, dtype="float32") # result is not float32
> ```
>
> ### Versions
>
> pytorch `main`. Also reproduced on torch 2.7.1 / NumPy 2.2.6.
cc @mruberry @rgommers
Contributor guide
Research direction
Start in torch/_numpy/_funcs_impl.py at the geomspace implementation and reproduce the two examples from the issue. Compare its behavior with NumPy for num=1 and dtype=, then check the sibling logspace wrapper; done means the edge case returns start and dtype is preserved without regressing the existing geomspace behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100