Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Allow linspace and logspace with steps=1 and start != end like numpy#14748

Closed
f0k wants to merge 2 commits into
pytorch:masterfrom
f0k:compatible-linspace
Closed

Allow linspace and logspace with steps=1 and start != end like numpy#14748
f0k wants to merge 2 commits into
pytorch:masterfrom
f0k:compatible-linspace

Conversation

@f0k
Copy link
Copy Markdown
Contributor

@f0k f0k commented Dec 4, 2018

torch.linspace(0, 1, 1) fails with RuntimeError: invalid argument 3: invalid number of points at ../aten/src/TH/generic/THTensorMoreMath.cpp:2119, while np.linspace(0, 1, 1) works fine.
Looking at the code, there is even a comment by @gchanan asking: "NumPy allows you to pass different points even if n <= 1 -- should we?"
I would say "yes". Currently, I would need to handle the case of steps == 1 or steps == 0 separately, making sure to change the end when calling torch.linspace. This is impractical. If we support start != end, there are two possibilities for the result: Either we ensure the first value in the resulting sequence always equals start, or we ensure the last value in the resulting sequence always equals end. Numpy chose the former, which also allows it to support a boolean endpoint flag. I'd say we should follow numpy.

This PR adapts linspace and logspace to mimic the behavior of numpy, adapts the tests accordingly, and extends the docstrings to make clear what happens when passing steps=1.

If you decide against this PR, the error message should become explicit about what I did wrong, and the documentation should be extended to mention this restriction.

@f0k
Copy link
Copy Markdown
Contributor Author

f0k commented Dec 5, 2018

The failures are not my fault, as far as I see.

The first one says:

DOCKER_IMAGE: 308535385114.dkr.ecr.us-east-1.amazonaws.com/pytorch/pytorch-linux-xenial-cuda8-cudnn6-py3:262
Error response from daemon: manifest for 308535385114.dkr.ecr.us-east-1.amazonaws.com/pytorch/pytorch-linux-xenial-cuda8-cudnn6-py3:262 not found
Exited with code 1

The second one says:

10:35:23 TensorOptions.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: struct c10::Device __cdecl c10::TensorOptions::device(void)const " (__imp_?device@TensorOptions@c10@@QEBA?AUDevice@2@XZ) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl c10::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct c10::TensorOptions const &)" (??6c10@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBUTensorOptions@0@@Z)
10:35:23 TensorOptions.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class caffe2::TypeMeta __cdecl c10::TensorOptions::dtype(void)const " (__imp_?dtype@TensorOptions@c10@@QEBA?AVTypeMeta@caffe2@@XZ) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl c10::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct c10::TensorOptions const &)" (??6c10@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBUTensorOptions@0@@Z)
10:35:23 TensorOptions.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: enum c10::Layout __cdecl c10::TensorOptions::layout(void)const " (__imp_?layout@TensorOptions@c10@@QEBA?AW4Layout@2@XZ) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl c10::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct c10::TensorOptions const &)" (??6c10@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBUTensorOptions@0@@Z)
10:35:23 TensorOptions.cpp.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __cdecl c10::TensorOptions::requires_grad(void)const " (__imp_?requires_grad@TensorOptions@c10@@QEBA_NXZ) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl c10::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,struct c10::TensorOptions const &)" (??6c10@@YAAEAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AEAV12@AEBUTensorOptions@0@@Z)
10:35:23 bin\c10.dll : fatal error LNK1120: 4 unresolved externals

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Dec 6, 2018

Yeah, not sure what happened. Push merge with master, let's see if it's clean this time.

Copy link
Copy Markdown
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezyang has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

zdevito pushed a commit to zdevito/ATen that referenced this pull request Dec 6, 2018
…(#14748)

Summary:
`torch.linspace(0, 1, 1)` fails with `RuntimeError: invalid argument 3: invalid number of points at ../aten/src/TH/generic/THTensorMoreMath.cpp:2119`, while `np.linspace(0, 1, 1)` works fine.
Looking at the code, there is even a comment by gchanan asking: "NumPy allows you to pass different points even if n <= 1 -- should we?"
I would say "yes". Currently, I would need to handle the case of `steps == 1` or `steps == 0` separately, making sure to change the `end` when calling `torch.linspace`. This is impractical. If we support `start != end`, there are two possibilities for the result: Either we ensure the first value in the resulting sequence always equals `start`, or we ensure the last value in the resulting sequence always equals `end`. Numpy chose the former, which also allows it to support a boolean `endpoint` flag. I'd say we should follow numpy.

This PR adapts `linspace` and `logspace` to mimic the behavior of numpy, adapts the tests accordingly, and extends the docstrings to make clear what happens when passing `steps=1`.

If you decide against this PR, the error message should become explicit about what I did wrong, and the documentation should be extended to mention this restriction.
Pull Request resolved: pytorch/pytorch#14748

Differential Revision: D13356136

Pulled By: ezyang

fbshipit-source-id: db85b8f0a98a5e24b3acd766132ab71c91794a82
@f0k f0k deleted the compatible-linspace branch January 5, 2019 17:32
@turian
Copy link
Copy Markdown

turian commented Jan 24, 2021

What happened with this @ezyang @f0k ? It would be nice to be able to do endpoint=False with torch.linspace?

@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Jan 25, 2021

This diff was landed but it didn't actually add endpoint. File an issue request for it please!

laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
…ytorch#14748)

Summary:
`torch.linspace(0, 1, 1)` fails with `RuntimeError: invalid argument 3: invalid number of points at ../aten/src/TH/generic/THTensorMoreMath.cpp:2119`, while `np.linspace(0, 1, 1)` works fine.
Looking at the code, there is even a comment by gchanan asking: "NumPy allows you to pass different points even if n <= 1 -- should we?"
I would say "yes". Currently, I would need to handle the case of `steps == 1` or `steps == 0` separately, making sure to change the `end` when calling `torch.linspace`. This is impractical. If we support `start != end`, there are two possibilities for the result: Either we ensure the first value in the resulting sequence always equals `start`, or we ensure the last value in the resulting sequence always equals `end`. Numpy chose the former, which also allows it to support a boolean `endpoint` flag. I'd say we should follow numpy.

This PR adapts `linspace` and `logspace` to mimic the behavior of numpy, adapts the tests accordingly, and extends the docstrings to make clear what happens when passing `steps=1`.

If you decide against this PR, the error message should become explicit about what I did wrong, and the documentation should be extended to mention this restriction.
Pull Request resolved: pytorch#14748

Differential Revision: D13356136

Pulled By: ezyang

fbshipit-source-id: db85b8f0a98a5e24b3acd766132ab71c91794a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants