Caffe2 - C++ API
A deep learning, cross platform ML framework
undefined_tensor_test.cpp
1 #include <gtest/gtest.h>
2 
3 #include <ATen/ATen.h>
4 #include <c10/core/UndefinedTensorImpl.h>
5 #include <string>
6 
7 using namespace at;
8 
9 TEST(TestUndefined, UndefinedTest) {
10  manual_seed(123);
11 
12  // mainly test ops on undefined tensors don't segfault and give a reasonable errror message.
13  Tensor und;
14  Tensor ft = ones({1}, CPU(kFloat));
15 
16  std::stringstream ss;
17  ss << und << std::endl;
18  ASSERT_FALSE(und.defined());
19  ASSERT_EQ(std::string("UndefinedType"), und.toString());
20 
21  ASSERT_ANY_THROW(und.strides());
22  ASSERT_ANY_THROW(und.dim());
23  ASSERT_ANY_THROW([]() { return Tensor(); }() = Scalar(5));
24  ASSERT_ANY_THROW(und.add(und));
25  ASSERT_ANY_THROW(und.add(ft));
26  ASSERT_ANY_THROW(ft.add(und));
27  ASSERT_ANY_THROW(und.add(5));
28  ASSERT_ANY_THROW(und.mm(und));
29 
30  und.toType(und.type());
31  ASSERT_ANY_THROW(und.toType(ft.type()));
32  ASSERT_ANY_THROW(ft.toType(und.type()));
33  und.toType(ScalarType::Undefined);
34  ASSERT_ANY_THROW(und.toType(ScalarType::Float));
35  ASSERT_ANY_THROW(ft.toType(ScalarType::Undefined));
36 
37  // copy_
38  ASSERT_ANY_THROW(und.copy_(und));
39  ASSERT_ANY_THROW(und.copy_(ft));
40  ASSERT_ANY_THROW(ft.copy_(und));
41 
42  und.toBackend(Backend::Undefined);
43  ASSERT_ANY_THROW(und.toBackend(Backend::CPU));
44  ASSERT_ANY_THROW(ft.toBackend(Backend::Undefined));
45 
46  Tensor to_move = ones({1}, CPU(kFloat));
47  Tensor m(std::move(to_move));
48  ASSERT_FALSE(to_move.defined());
49  ASSERT_EQ(to_move.unsafeGetTensorImpl(), UndefinedTensorImpl::singleton());
50 }
Scalar represents a 0-dimensional tensor which contains a single element.
Definition: Scalar.h:22
Flush-To-Zero and Denormals-Are-Zero mode.