Skip to content

Fix issues in torch::tensor constructor#26890

Closed
yf225 wants to merge 24 commits intopytorch:masterfrom
yf225:fix_multidim_tensor_ctor
Closed

Fix issues in torch::tensor constructor#26890
yf225 wants to merge 24 commits intopytorch:masterfrom
yf225:fix_multidim_tensor_ctor

Conversation

@yf225
Copy link
Copy Markdown
Contributor

@yf225 yf225 commented Sep 26, 2019

This PR contains the following:

  1. Fix ambiguous overload problem when torch::tensor({{1, 2}}) is used:
../test/cpp/api/tensor.cpp: In member function ‘virtual void TensorTest_MultidimTensorCtor_Test::TestBody()’:
../test/cpp/api/tensor.cpp:202:41: error: call of overloaded ‘tensor(<brace-enclosed initializer list>)’ is ambiguous
     auto tensor = torch::tensor({{1, 2}});
                                         ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:177:644: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:177:1603: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:177:2562: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<short int>)
../torch/csrc/autograd/generated/variable_factories.h:177:3507: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<int>)
../torch/csrc/autograd/generated/variable_factories.h:177:4450: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<long int>)
../torch/csrc/autograd/generated/variable_factories.h:177:5404: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<float>)
../torch/csrc/autograd/generated/variable_factories.h:177:6354: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<double>)
../torch/csrc/autograd/generated/variable_factories.h:177:7630: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<bool>)
../torch/csrc/autograd/generated/variable_factories.h:177:9224: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:177:10838: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::BFloat16>)
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:193:19: note: candidate: at::Tensor torch::tensor(torch::detail::InitListTensor)
 inline at::Tensor tensor(detail::InitListTensor list_init_tensor) {
                   ^

After this PR, the multidim tensor constructor torch::tensor(...) should be ready for general use.

@yf225 yf225 requested a review from zou3519 September 26, 2019 14:10
@pytorchbot pytorchbot added module: cpp Related to C++ API module: internals Related to internal abstractions in c10 and ATen labels Sep 26, 2019
@yf225 yf225 added this to the 1.3 milestone Sep 26, 2019
@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch from bd28b86 to 427ea40 Compare September 26, 2019 18:58
This reverts commit 427ea40.
@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch from 641cde6 to 23401f2 Compare September 26, 2019 19:28
@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch from 23401f2 to 8cb239e Compare September 26, 2019 19:41
@zou3519
Copy link
Copy Markdown
Contributor

zou3519 commented Sep 26, 2019

Fix ambiguous overload problem when torch::tensor({{1, 2}}) is used.

What is the ambiguous overload problem?

Comment thread test/cpp/api/tensor.cpp
Comment thread tools/autograd/templates/variable_factories.h Outdated
scalar_type_(at::k##S), \
type_(InitListTensorType::Scalar) {}
// 2. A Tensor represented in `std::vector<ListInitTensor>` form, with value
// `vec()`, Tensor scalar type `scalar_type()`, and Tensor sizes `sizes()`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might be better to name vec something more descriptive, like values

sizes_(), \
scalar_type_(at::k##S), \
type_(ListInitTensorType::Scalar) {} \
ListInitTensor(std::vector<T> values) : \
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we need this constructor? std::vector<T> is implicitly convertible to at::ArrayRef<T>, so isn't it sufficient to just have that constructor?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried removing the constructor that takes in std::vector<T>, but got the following error:

../aten/src/ATen/test/pow_test.cpp:225:39:   required from here
../aten/src/ATen/test/pow_test.cpp:111:36: error: no matching function for call to ‘tensor(const std::vector<int>&)’
   const auto tensor = torch::tensor(vals);
                                    ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../aten/src/ATen/test/pow_test.cpp:5:
../torch/csrc/autograd/generated/variable_factories.h:177:19: note: candidate: at::Tensor torch::tensor(torch::detail::ListInitTensor, const c10::TensorOptions&)
 inline at::Tensor tensor(detail::ListInitTensor list_init_tensor, const at::TensorOptions& options) {
                   ^
../torch/csrc/autograd/generated/variable_factories.h:177:19: note:   candidate expects 2 arguments, 1 provided
../torch/csrc/autograd/generated/variable_factories.h:181:19: note: candidate: at::Tensor torch::tensor(torch::detail::ListInitTensor)
 inline at::Tensor tensor(detail::ListInitTensor list_init_tensor) {
                   ^
../torch/csrc/autograd/generated/variable_factories.h:181:19: note:   no known conversion for argument 1 from ‘const std::vector<int>’ to ‘torch::detail::ListInitTensor’

I suspect the issue is that there might be too many conversions going on that the compiler just refuses to do it...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yikes. Could we make it take const std::vector<T>& values so that we don't incur an additional copy?

In addition, could we just call the ArrayRef ctor here? Something like

ListInitTensor(const std::vector<T>& values) : ListInitTensor(ArrayRef<T>(values)) {}`

type_(ListInitTensorType::Vector) { \
vec_.reserve(values.size()); \
for (const auto& elem : values) { \
vec_.push_back(ListInitTensor(elem)); \
Copy link
Copy Markdown
Contributor

@zou3519 zou3519 Sep 26, 2019

Choose a reason for hiding this comment

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

Discussed offline: there might be a performance regression here. For an ArrayRef with 1000 elements, before we would call at::tensor. Now the code calls tensor.fill_ 1000 times. With 1us dispatch overhead this takes 1(!)ms, which is probably slower than what at::tensor can do (I imagine it does a memcpy).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh, interesting. Previously, torch::tensor({1, 2, 3, 4, 5, 6}) went through the ArrayRef ctor. Now, it goes through the ListInitTensor ctor. This means that there might be a performance regression for people who used to use torch::tensor({1, 2, 3, 4, 5, 6})

Copy link
Copy Markdown
Contributor

@zou3519 zou3519 left a comment

Choose a reason for hiding this comment

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

Code looks correct but we can improve it more. I'm not sure how much a perf regression this actually gives (compared to calling at::tensor), it would be good to benchmark and conclude if we should make a fast-path or not.

" with sizes: ",
elem.sizes_);
type_(ListInitTensorType::Vector) {
if (init_list.size() > 0) {
Copy link
Copy Markdown
Contributor

@zou3519 zou3519 Sep 26, 2019

Choose a reason for hiding this comment

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

Nit: you can avoid the additional indentation (and reduce the number of lines changed) by doing something like:

if (init_list.size() == 0) {
  scalar_type_ = c10::ScalarType::Undefined;
  sizes_ = {0};
  return;
}
// existing code here

@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch 2 times, most recently from d179b41 to d917f5f Compare September 26, 2019 22:33
@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch from 40527fb to d9481ad Compare September 26, 2019 23:22
This reverts commit c6d29d1.
@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch from d9481ad to d9ef7b1 Compare September 26, 2019 23:23
@yf225
Copy link
Copy Markdown
Contributor Author

yf225 commented Sep 26, 2019

@zou3519 I reduced the scope of this PR to just add support for torch::tensor({{1, 2}}), because it will require significant refactoring to the InitListTensor code to provide a fast path for 1D tensors. One aspect of the needed refactoring is that we need to change InitListTensor(std::initializer_list<InitListTensor> init_list) to InitListTensor(std::initializer_list<std::initializer_list<InitListTensor>> init_list), otherwise 1D tensors such as torch::tensor({1, 2, 3, 4, ...}) will always take InitListTensor(std::initializer_list<InitListTensor> init_list) over InitListTensor(at::ArrayRef<T> values) and defeats the purpose of the fast path.

@yf225 yf225 changed the title Fix issues and add support for empty tensor in torch::tensor constructor Fix issues in torch::tensor constructor Sep 26, 2019
Will Feng added 2 commits September 26, 2019 23:47
This reverts commit cb134ac.
Comment thread tools/autograd/templates/variable_factories.h Outdated
@yf225 yf225 force-pushed the fix_multidim_tensor_ctor branch from 710927f to 45c4339 Compare September 27, 2019 15:10
Comment thread tools/autograd/templates/variable_factories.h Outdated
Comment thread tools/autograd/templates/variable_factories.h Outdated
Comment thread tools/autograd/templates/variable_factories.h
Comment thread tools/autograd/templates/variable_factories.h
Will Feng added 3 commits September 27, 2019 13:06
Comment thread test/cpp/api/tensor.cpp
Comment thread tools/autograd/templates/variable_factories.h Outdated
TORCH_CHECK(init_list.size() > 0, "Empty init-list is not supported");
TORCH_CHECK(
init_list.size() > 0,
"Empty init-list is not yet supported. We can create tensors with zero-size dimensions in the following way:\n",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I know I asked you to add this error message, but I'm wondering: what actually happens if a user tries to call torch::tensor({})? Is it a compilation error or does this error message get triggered?

If this error message gets triggered, could we add a test for it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Compiling torch::tensor({}) gives an "ambiguous overload" compilation error:

../test/cpp/api/tensor.cpp:232:35: error: call of overloaded ‘tensor(<brace-enclosed initializer list>)’ is ambiguous
     auto tensor = torch::tensor({});
                                   ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:181:644: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:181:766: note: candidate: at::Tensor torch::tensor(std::initializer_list<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:181:887: note: candidate: at::Tensor torch::tensor(uint8_t)
../torch/csrc/autograd/generated/variable_factories.h:181:1603: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:181:1724: note: candidate: at::Tensor torch::tensor(std::initializer_list<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:181:1843: note: candidate: at::Tensor torch::tensor(int8_t)
../torch/csrc/autograd/generated/variable_factories.h:181:2562: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<short int>)
../torch/csrc/autograd/generated/variable_factories.h:181:2685: note: candidate: at::Tensor torch::tensor(std::initializer_list<short int>)
../torch/csrc/autograd/generated/variable_factories.h:181:2806: note: candidate: at::Tensor torch::tensor(int16_t)
../torch/csrc/autograd/generated/variable_factories.h:181:3507: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<int>)
../torch/csrc/autograd/generated/variable_factories.h:181:3624: note: candidate: at::Tensor torch::tensor(std::initializer_list<int>)
../torch/csrc/autograd/generated/variable_factories.h:181:3737: note: candidate: at::Tensor torch::tensor(int)
../torch/csrc/autograd/generated/variable_factories.h:181:4450: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<long int>)
../torch/csrc/autograd/generated/variable_factories.h:181:4572: note: candidate: at::Tensor torch::tensor(std::initializer_list<long int>)
../torch/csrc/autograd/generated/variable_factories.h:181:4693: note: candidate: at::Tensor torch::tensor(int64_t)
../torch/csrc/autograd/generated/variable_factories.h:181:5404: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<float>)
../torch/csrc/autograd/generated/variable_factories.h:181:5525: note: candidate: at::Tensor torch::tensor(std::initializer_list<float>)
../torch/csrc/autograd/generated/variable_factories.h:181:5642: note: candidate: at::Tensor torch::tensor(float)
../torch/csrc/autograd/generated/variable_factories.h:181:6354: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<double>)
../torch/csrc/autograd/generated/variable_factories.h:181:6477: note: candidate: at::Tensor torch::tensor(std::initializer_list<double>)
../torch/csrc/autograd/generated/variable_factories.h:181:6596: note: candidate: at::Tensor torch::tensor(double)
../torch/csrc/autograd/generated/variable_factories.h:181:7630: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<bool>)
../torch/csrc/autograd/generated/variable_factories.h:181:7815: note: candidate: at::Tensor torch::tensor(std::initializer_list<bool>)
../torch/csrc/autograd/generated/variable_factories.h:181:8062: note: candidate: at::Tensor torch::tensor(c10::impl::ScalarTypeToCPPType<(c10::ScalarType)11>::type)
../torch/csrc/autograd/generated/variable_factories.h:181:9224: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:181:9409: note: candidate: at::Tensor torch::tensor(std::initializer_list<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:181:9656: note: candidate: at::Tensor torch::tensor(c10::impl::ScalarTypeToCPPType<(c10::ScalarType)5>::type)
../torch/csrc/autograd/generated/variable_factories.h:181:10838: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::BFloat16>)
../torch/csrc/autograd/generated/variable_factories.h:181:11031: note: candidate: at::Tensor torch::tensor(std::initializer_list<c10::BFloat16>)
../torch/csrc/autograd/generated/variable_factories.h:181:11286: note: candidate: at::Tensor torch::tensor(c10::impl::ScalarTypeToCPPType<(c10::ScalarType)15>::type)
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:197:19: note: candidate: at::Tensor torch::tensor(torch::detail::InitListTensor)
 inline at::Tensor tensor(detail::InitListTensor init_list_tensor) {
                   ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:212:19: note: candidate: at::Tensor torch::tensor(std::initializer_list<torch::detail::InitListTensor>)
 inline at::Tensor tensor(std::initializer_list<detail::InitListTensor> init_list) {
                   ^

Compiling torch::tensor({{}, {}}) also gives an "ambiguous overload" compilation error:

../test/cpp/api/tensor.cpp:232:41: error: call of overloaded ‘tensor(<brace-enclosed initializer list>)’ is ambiguous
     auto tensor = torch::tensor({{}, {}});
                                         ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:181:644: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:181:766: note: candidate: at::Tensor torch::tensor(std::initializer_list<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:181:1603: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:181:1724: note: candidate: at::Tensor torch::tensor(std::initializer_list<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:181:2562: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<short int>)
../torch/csrc/autograd/generated/variable_factories.h:181:2685: note: candidate: at::Tensor torch::tensor(std::initializer_list<short int>)
../torch/csrc/autograd/generated/variable_factories.h:181:3507: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<int>)
../torch/csrc/autograd/generated/variable_factories.h:181:3624: note: candidate: at::Tensor torch::tensor(std::initializer_list<int>)
../torch/csrc/autograd/generated/variable_factories.h:181:4450: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<long int>)
../torch/csrc/autograd/generated/variable_factories.h:181:4572: note: candidate: at::Tensor torch::tensor(std::initializer_list<long int>)
../torch/csrc/autograd/generated/variable_factories.h:181:5404: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<float>)
../torch/csrc/autograd/generated/variable_factories.h:181:5525: note: candidate: at::Tensor torch::tensor(std::initializer_list<float>)
../torch/csrc/autograd/generated/variable_factories.h:181:6354: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<double>)
../torch/csrc/autograd/generated/variable_factories.h:181:6477: note: candidate: at::Tensor torch::tensor(std::initializer_list<double>)
../torch/csrc/autograd/generated/variable_factories.h:181:7630: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<bool>)
../torch/csrc/autograd/generated/variable_factories.h:181:7815: note: candidate: at::Tensor torch::tensor(std::initializer_list<bool>)
../torch/csrc/autograd/generated/variable_factories.h:181:9224: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:181:9409: note: candidate: at::Tensor torch::tensor(std::initializer_list<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:181:9656: note: candidate: at::Tensor torch::tensor(c10::impl::ScalarTypeToCPPType<(c10::ScalarType)5>::type)
../torch/csrc/autograd/generated/variable_factories.h:181:10838: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::BFloat16>)
../torch/csrc/autograd/generated/variable_factories.h:181:11031: note: candidate: at::Tensor torch::tensor(std::initializer_list<c10::BFloat16>)
../torch/csrc/autograd/generated/variable_factories.h:181:11286: note: candidate: at::Tensor torch::tensor(c10::impl::ScalarTypeToCPPType<(c10::ScalarType)15>::type)
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:197:19: note: candidate: at::Tensor torch::tensor(torch::detail::InitListTensor)
 inline at::Tensor tensor(detail::InitListTensor init_list_tensor) {
                   ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:212:19: note: candidate: at::Tensor torch::tensor(std::initializer_list<torch::detail::InitListTensor>)
 inline at::Tensor tensor(std::initializer_list<detail::InitListTensor> init_list) {
                   ^

I think improving the error message here is still a good idea, in case people try to look at the source code to figure out what they should do to create tensors with zero-size dimensions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it clearly documented somewhere that we don't support {}?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@yf225 is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

@yf225 merged this pull request in 2f1932f.

pdlive215 pushed a commit to pdlive215/pytorch that referenced this pull request Nov 27, 2019
Summary:
This PR contains the following:
1. Fix ambiguous overload problem when `torch::tensor({{1, 2}})` is used:
```
../test/cpp/api/tensor.cpp: In member function ‘virtual void TensorTest_MultidimTensorCtor_Test::TestBody()’:
../test/cpp/api/tensor.cpp:202:41: error: call of overloaded ‘tensor(<brace-enclosed initializer list>)’ is ambiguous
     auto tensor = torch::tensor({{1, 2}});
                                         ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:177:644: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:177:1603: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:177:2562: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<short int>)
../torch/csrc/autograd/generated/variable_factories.h:177:3507: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<int>)
../torch/csrc/autograd/generated/variable_factories.h:177:4450: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<long int>)
../torch/csrc/autograd/generated/variable_factories.h:177:5404: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<float>)
../torch/csrc/autograd/generated/variable_factories.h:177:6354: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<double>)
../torch/csrc/autograd/generated/variable_factories.h:177:7630: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<bool>)
../torch/csrc/autograd/generated/variable_factories.h:177:9224: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:177:10838: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::BFloat16>)
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:193:19: note: candidate: at::Tensor torch::tensor(torch::detail::InitListTensor)
 inline at::Tensor tensor(detail::InitListTensor list_init_tensor) {
                   ^
```

After this PR, the multidim tensor constructor `torch::tensor(...)` should be ready for general use.
Pull Request resolved: pytorch#26890

Differential Revision: D17632608

Pulled By: yf225

fbshipit-source-id: 2e653d4ad85729d052328a124004d64994bec782
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
This PR contains the following:
1. Fix ambiguous overload problem when `torch::tensor({{1, 2}})` is used:
```
../test/cpp/api/tensor.cpp: In member function ‘virtual void TensorTest_MultidimTensorCtor_Test::TestBody()’:
../test/cpp/api/tensor.cpp:202:41: error: call of overloaded ‘tensor(<brace-enclosed initializer list>)’ is ambiguous
     auto tensor = torch::tensor({{1, 2}});
                                         ^
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:177:644: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<unsigned char>)
../torch/csrc/autograd/generated/variable_factories.h:177:1603: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<signed char>)
../torch/csrc/autograd/generated/variable_factories.h:177:2562: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<short int>)
../torch/csrc/autograd/generated/variable_factories.h:177:3507: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<int>)
../torch/csrc/autograd/generated/variable_factories.h:177:4450: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<long int>)
../torch/csrc/autograd/generated/variable_factories.h:177:5404: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<float>)
../torch/csrc/autograd/generated/variable_factories.h:177:6354: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<double>)
../torch/csrc/autograd/generated/variable_factories.h:177:7630: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<bool>)
../torch/csrc/autograd/generated/variable_factories.h:177:9224: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::Half>)
../torch/csrc/autograd/generated/variable_factories.h:177:10838: note: candidate: at::Tensor torch::tensor(c10::ArrayRef<c10::BFloat16>)
In file included from ../caffe2/../torch/csrc/api/include/torch/types.h:7:0,
                 from ../caffe2/../torch/csrc/api/include/torch/detail/static.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/pimpl.h:4,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/module.h:3,
                 from ../caffe2/../torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from ../test/cpp/api/support.h:7,
                 from ../test/cpp/api/tensor.cpp:2:
../torch/csrc/autograd/generated/variable_factories.h:193:19: note: candidate: at::Tensor torch::tensor(torch::detail::InitListTensor)
 inline at::Tensor tensor(detail::InitListTensor list_init_tensor) {
                   ^
```

After this PR, the multidim tensor constructor `torch::tensor(...)` should be ready for general use.
Pull Request resolved: pytorch#26890

Differential Revision: D17632608

Pulled By: yf225

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

Labels

Merged module: cpp Related to C++ API module: internals Related to internal abstractions in c10 and ATen

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants