1 #include "gtest/gtest.h" 4 #include <caffe2/core/init.h> 5 #include <caffe2/core/operator.h> 7 TEST(Caffe2ToPytorch, SimpleLegacy) {
9 c2_tensor.Resize(4, 4);
10 auto data = c2_tensor.mutable_data<int64_t>();
11 for (int64_t i = 0; i < 16; i++) {
15 ASSERT_TRUE(&at_tensor.type() !=
nullptr);
17 auto it = at_tensor.data<int64_t>();
18 for (int64_t i = 0; i < 16; i++) {
23 TEST(Caffe2ToPytorch, Simple) {
25 auto data = c2_tensor.mutable_data<int64_t>();
26 for (int64_t i = 0; i < 16; i++) {
30 ASSERT_TRUE(&at_tensor.type() !=
nullptr);
32 auto it = at_tensor.data<int64_t>();
33 for (int64_t i = 0; i < 16; i++) {
38 TEST(Caffe2ToPytorch, ExternalData) {
41 for (int64_t i = 0; i < 16; i++) {
44 c2_tensor.ShareExternalPointer(buf, 16);
49 auto it = at_tensor.data<int64_t>();
50 for (int64_t i = 0; i < 16; i++) {
53 ASSERT_FALSE(at_tensor.storage().resizable());
54 ASSERT_ANY_THROW(at_tensor.resize_({7,7}));
57 TEST(Caffe2ToPytorch, Op) {
59 c2_tensor.Resize(3, 3);
60 auto data = c2_tensor.mutable_data<int64_t>();
61 for (int64_t i = 0; i < 9; i++) {
66 ASSERT_EQ(at::sum(at_tensor).item<int64_t>(), 36);
78 TEST(Caffe2ToPytorch, PartiallyInitialized) {
84 ASSERT_ANY_THROW(
at::Tensor at_tensor(c2_tensor));
89 c2_tensor.Resize(4,4);
90 ASSERT_ANY_THROW(
at::Tensor at_tensor(c2_tensor));
95 c2_tensor.Resize(4,4);
96 c2_tensor.mutable_data<
float>();
97 c2_tensor.FreeMemory();
98 ASSERT_ANY_THROW(
at::Tensor at_tensor(c2_tensor));
102 TEST(Caffe2ToPytorch, MutualResizes) {
104 auto data = c2_tensor.mutable_data<
float>();
105 for (int64_t i = 0; i < 25; i++) {
112 at_tensor[0][0] = 123;
113 ASSERT_EQ(c2_tensor.mutable_data<
float>()[0], 123);
116 at_tensor.resize_({4, 4});
117 c2_tensor.mutable_data<
float>()[1] = 234;
118 ASSERT_EQ(at_tensor[0][1].item().to<float>(), 234);
121 at_tensor.resize_({6, 6});
122 c2_tensor.mutable_data<
float>()[2] = 345;
123 ASSERT_EQ(at_tensor[0][2].item().to<float>(), 345);
124 ASSERT_EQ(c2_tensor.sizes()[0], 6);
125 ASSERT_EQ(c2_tensor.sizes()[1], 6);
129 c2_tensor.Resize(7, 7);
130 c2_tensor.mutable_data<
float>()[3] = 456;
131 ASSERT_EQ(at_tensor[0][3].item().to<float>(), 456);
132 ASSERT_EQ(at_tensor.sizes()[0], 7);
133 ASSERT_EQ(at_tensor.sizes()[1], 7);
136 TEST(PytorchToCaffe2, Op) {
140 auto at_tensor_a = at::ones({5, 5}, at::dtype(at::kFloat));
141 auto at_tensor_b = at::ones({5, 5}, at::dtype(at::kFloat));
142 auto at_tensor_c = at::ones({5, 5}, at::dtype(at::kFloat));
150 BlobSetTensor(workspace.
CreateBlob(
"c"), c2_tensor_from_aten.Alias());
154 auto op = net.add_op();
162 workspace.RunNetOnce(net);
164 auto result = XBlobGetMutableTensor(workspace.
CreateBlob(
"d"), {5, 5}, at::kCPU);
166 auto it = result.data<
float>();
167 for (int64_t i = 0; i < 25; i++) {
168 ASSERT_EQ(it[i], 3.0);
171 ASSERT_EQ(at::sum(at_result).item<float>(), 75);
174 TEST(PytorchToCaffe2, SharedStorageRead) {
178 auto at_tensor_a = at::ones({5, 5}, at::dtype(at::kFloat));
179 auto at_tensor_b = at_tensor_a.view({5, 5});
185 auto op = net.add_op();
192 workspace.RunNetOnce(net);
194 auto result = XBlobGetMutableTensor(workspace.
CreateBlob(
"c"), {5, 5}, at::kCPU);
195 auto it = result.data<
float>();
196 for (int64_t i = 0; i < 25; i++) {
197 ASSERT_EQ(it[i], 2.0);
200 ASSERT_EQ(at::sum(at_result).item<float>(), 50);
203 TEST(PytorchToCaffe2, SharedStorageWrite) {
204 auto at_tensor_a = at::ones({5, 5}, at::dtype(at::kFloat));
205 auto at_tensor_b = at_tensor_a.view({25});
211 c2_tensor_a.mutable_data<
float>()[1] = 123;
212 ASSERT_EQ(c2_tensor_b.mutable_data<
float>()[1], 123);
213 ASSERT_EQ(at_tensor_a[0][1].item().to<float>(), 123);
214 ASSERT_EQ(at_tensor_b[1].item().to<float>(), 123);
217 TEST(PytorchToCaffe2, MutualResizes) {
218 auto at_tensor = at::ones({5, 5}, at::dtype(at::kFloat));
223 c2_tensor.mutable_data<
float>()[0] = 123;
224 ASSERT_EQ(at_tensor[0][0].item().to<float>(), 123);
227 at_tensor.resize_({4, 4});
228 c2_tensor.mutable_data<
float>()[1] = 234;
229 ASSERT_EQ(at_tensor[0][1].item().to<float>(), 234);
232 at_tensor.resize_({6, 6});
233 c2_tensor.mutable_data<
float>()[2] = 345;
234 ASSERT_EQ(at_tensor[0][2].item().to<float>(), 345);
235 ASSERT_EQ(c2_tensor.sizes()[0], 6);
236 ASSERT_EQ(c2_tensor.sizes()[1], 6);
240 c2_tensor.Resize(7, 7);
241 c2_tensor.mutable_data<
float>()[3] = 456;
242 ASSERT_EQ(at_tensor[0][3].item().to<float>(), 456);
243 ASSERT_EQ(at_tensor.sizes()[0], 7);
244 ASSERT_EQ(at_tensor.sizes()[1], 7);
247 TEST(PytorchToCaffe2, Strided) {
248 auto at_tensor = at::ones({5, 5}, at::dtype(at::kFloat)).t();
252 for (int64_t i = 0; i < 25; i++) {
253 ASSERT_EQ(c2_tensor.data<
float>()[i], 1.0);
257 TEST(PytorchToCaffe2, InplaceStrided) {
258 auto at_tensor = at::zeros({2, 5}, at::dtype(at::kFloat));
260 ASSERT_EQ(c2_tensor.sizes()[0], 2);
261 ASSERT_EQ(c2_tensor.sizes()[1], 5);
263 c2_tensor.mutable_data<
float>()[1] = 234;
264 ASSERT_EQ(at_tensor[0][1].item().to<float>(), 234);
267 ASSERT_EQ(c2_tensor.sizes()[0], 5);
268 ASSERT_EQ(c2_tensor.sizes()[1], 2);
271 ASSERT_EQ(at_tensor[1][0].item().to<float>(), 234);
272 ASSERT_EQ(c2_tensor.data<
float>()[1], 234);
275 TEST(PytorchToCaffe2, NonRegularTensor) {
277 at::empty({2, 3}, at::dtype<float>().layout(at::kSparse));
291 TEST(Caffe2ToPytorch, NonPOD) {
292 caffe2::Tensor c2_tensor = caffe2::empty({1}, at::dtype<std::string>());
293 auto data = c2_tensor.mutable_data<std::string>();
295 ASSERT_ANY_THROW(
at::Tensor at_tensor(c2_tensor));
298 TEST(Caffe2ToPytorch, Nullptr) {
300 ASSERT_FALSE(c2_tensor.defined());
302 ASSERT_FALSE(at_tensor.defined());
305 TEST(PytorchToCaffe2, Nullptr) {
307 ASSERT_FALSE(at_tensor.defined());
309 ASSERT_FALSE(c2_tensor.defined());
Blob * CreateBlob(const string &name)
Creates a blob of the given name.
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
bool is_sparse() const
Returns if a Tensor has sparse backend.