1 #include <gtest/gtest.h> 4 #include <ATen/CPUApplyUtils.h> 5 #include <ATen/test/test_assert.h> 11 void fill_tensor(int64_t scalar,
Tensor& t_) {
13 for (int64_t i = 0; i < t.numel(); i++) {
14 t[i] = (i + 1) * scalar;
27 auto zero_dim = at::empty({}, type);
30 AT_DISPATCH_FLOATING_TYPES(zero_dim.scalar_type(),
"test0", [&] {
31 ASSERT(zero_dim.data<scalar_t>()[0] == std::exp(2));
34 auto empty_t = at::empty({0}, type);
38 auto a0 = at::empty({0}, type.options());
39 auto a1 = at::empty({0}, type.options());
40 auto a2 = at::empty({0}, type.options());
41 auto a3 = at::empty({0}, type.options());
44 std::vector<Tensor> tensors({a0, a1, a2, a3, a4});
45 for (
size_t i = 0; i < tensors.size(); i++) {
46 tensors[i].resize_(shape);
47 fill_tensor(i + 1, tensors[i]);
48 if (a >= 0 && b >= 0) {
49 tensors[i].transpose_(a, b);
53 AT_DISPATCH_FLOATING_TYPES(a0.scalar_type(),
"test1", [&] {
54 CPU_tensor_apply2<scalar_t, scalar_t>(
55 a0, a1, [](scalar_t& y,
const scalar_t& x) { y = x * x; });
56 CPU_tensor_apply2<double, scalar_t>(
57 a4, a1, [](
double& y, scalar_t x) { y = (double)(x * x); });
58 for (int64_t i = 0; i < a0.numel(); i++) {
59 auto target = a1.data<scalar_t>()[i] * a1.data<scalar_t>()[i];
60 ASSERT(a0.data<scalar_t>()[i] == target);
61 ASSERT(a4.data<
double>()[i] == target);
65 AT_DISPATCH_FLOATING_TYPES(a0.scalar_type(),
"test2", [&] {
66 CPU_tensor_apply3<scalar_t, scalar_t, scalar_t>(
67 a0, a1, a2, [](scalar_t& y,
const scalar_t& x,
const scalar_t& z) {
70 CPU_tensor_apply3<double, scalar_t, scalar_t>(
71 a4, a1, a2, [](
double& y,
const scalar_t& x,
const scalar_t& z) {
72 y = (double)(x * x + z);
74 for (int64_t i = 0; i < a0.numel(); i++) {
75 auto target = a1.data<scalar_t>()[i] * a1.data<scalar_t>()[i];
76 target = target + a2.data<scalar_t>()[i];
77 ASSERT(a0.data<scalar_t>()[i] == target);
78 ASSERT(a4.data<
double>()[i] == target);
82 AT_DISPATCH_FLOATING_TYPES(a0.scalar_type(),
"test3", [&] {
83 CPU_tensor_apply4<scalar_t, scalar_t, scalar_t, scalar_t>(
91 const scalar_t& a) { y = x * x + z * a; });
92 CPU_tensor_apply4<double, scalar_t, scalar_t, scalar_t>(
97 [](
double& y,
const scalar_t& x,
const scalar_t& z,
const scalar_t& a) {
98 y = (double)(x * x + z * a);
100 for (int64_t i = 0; i < a0.numel(); i++) {
101 auto target = a1.data<scalar_t>()[i] * a1.data<scalar_t>()[i];
102 target = target + a2.data<scalar_t>()[i] * a3.data<scalar_t>()[i];
103 ASSERT(a0.data<scalar_t>()[i] == target);
104 ASSERT(a4.data<
double>()[i] == target);
110 TEST(ApplyUtilsTest, Contiguous2D) {
112 test(CPU(kDouble), {2, 1}, -1, -1);
116 TEST(ApplyUtilsTest, Small2D) {
118 test(CPU(kDouble), {2, 1});
122 TEST(ApplyUtilsTest, _2D) {
124 test(CPU(kDouble), {20, 10});
128 TEST(ApplyUtilsTest, _3D) {
130 test(CPU(kDouble), {3, 4, 2});
134 TEST(ApplyUtilsTest, Medium3D) {
136 test(CPU(kDouble), {3, 40, 2});
140 TEST(ApplyUtilsTest, _10D) {
142 test(CPU(kDouble), {3, 4, 2, 5, 2, 1, 3, 4, 2, 3});
C10_NODISCARD TensorOptions dtype(c10::optional< caffe2::TypeMeta > dtype) const noexcept
Return a copy of TensorOptions with dtype set to the given one.
Flush-To-Zero and Denormals-Are-Zero mode.