Skip to content

Commit 6f37a53

Browse files
committed
Added C++ API test
1 parent aa99eba commit 6f37a53

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

aten/src/ATen/test/basic.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <ATen/ATen.h>
44
#include <ATen/core/Reduction.h>
5+
#include <torch/cuda.h>
56

67
// for TH compat test only...
78
struct THFloatTensor;
@@ -345,3 +346,69 @@ TEST(BasicTest, BasicTestCUDA) {
345346
test(CUDA(kFloat));
346347
}
347348
}
349+
350+
TEST(BasicTest, FactoryMethodsTest) {
351+
// Test default values
352+
at::Tensor tensor0 = at::empty({4});
353+
ASSERT_EQ(tensor0.dtype(), at::kFloat);
354+
ASSERT_EQ(tensor0.layout(), at::kStrided);
355+
ASSERT_EQ(tensor0.device(), at::kCPU);
356+
ASSERT_FALSE(tensor0.requires_grad());
357+
ASSERT_FALSE(tensor0.is_pinned());
358+
359+
// Test setting grad to FALSE
360+
tensor0 = at::empty({4}, at::TensorOptions().requires_grad(false));
361+
ASSERT_EQ(tensor0.dtype(), at::kFloat);
362+
ASSERT_EQ(tensor0.layout(), at::kStrided);
363+
ASSERT_EQ(tensor0.device(), at::kCPU);
364+
ASSERT_FALSE(tensor0.requires_grad());
365+
ASSERT_FALSE(tensor0.is_pinned());
366+
367+
// Test setting grad to TRUE
368+
tensor0 = at::empty({4}, at::TensorOptions().requires_grad(true));
369+
ASSERT_EQ(tensor0.dtype(), at::kFloat);
370+
ASSERT_EQ(tensor0.layout(), at::kStrided);
371+
ASSERT_EQ(tensor0.device(), at::kCPU);
372+
// This is a bug. Requires_grad was set to TRUE but this is being ignored.
373+
// Issue #30405
374+
ASSERT_FALSE(tensor0.requires_grad());
375+
ASSERT_FALSE(tensor0.is_pinned());
376+
377+
// Test setting dtype
378+
at::Tensor tensor1 = at::empty({4}, at::TensorOptions().dtype(at::kHalf));
379+
ASSERT_EQ(tensor1.dtype(), at::kHalf);
380+
ASSERT_EQ(tensor1.layout(), at::kStrided);
381+
ASSERT_EQ(tensor1.device(), at::kCPU);
382+
ASSERT_FALSE(tensor1.requires_grad());
383+
ASSERT_FALSE(tensor1.is_pinned());
384+
385+
if (torch::cuda::is_available()) {
386+
// Test setting pin memory
387+
tensor1 = at::empty({4}, at::TensorOptions().pinned_memory(true));
388+
ASSERT_EQ(tensor1.dtype(), at::kFloat);
389+
ASSERT_EQ(tensor1.layout(), at::kStrided);
390+
ASSERT_EQ(tensor1.device(), at::kCPU);
391+
ASSERT_EQ(tensor1.requires_grad(), false);
392+
ASSERT_FALSE(tensor1.device().is_cuda());
393+
ASSERT_TRUE(tensor1.is_pinned());
394+
395+
// Test setting device
396+
tensor1 = at::empty({4}, at::TensorOptions().device(at::kCUDA));
397+
ASSERT_EQ(tensor1.dtype(), at::kFloat);
398+
ASSERT_EQ(tensor1.layout(), at::kStrided);
399+
ASSERT_TRUE(tensor1.device().is_cuda());
400+
ASSERT_FALSE(tensor1.requires_grad());
401+
ASSERT_FALSE(tensor1.is_pinned());
402+
403+
// Test set everything
404+
tensor1 = at::empty({4}, at::TensorOptions().dtype(at::kHalf).device(at::kCUDA).layout(at::kSparse).requires_grad(true));
405+
ASSERT_EQ(tensor1.dtype(), at::kHalf);
406+
ASSERT_EQ(tensor1.layout(), at::kSparse);
407+
ASSERT_TRUE(tensor1.device().is_cuda());
408+
ASSERT_FALSE(tensor1.requires_grad());
409+
410+
// This will cause an exception.
411+
// Issue #30405
412+
ASSERT_ANY_THROW(tensor1.is_pinned());
413+
}
414+
}

0 commit comments

Comments
 (0)