|
| 1 | +#include <string> |
| 2 | +#include "core/compiler.h" |
| 3 | +#include "core/lowering/passes/passes.h" |
| 4 | +#include "gtest/gtest.h" |
| 5 | +#include "tests/util/util.h" |
| 6 | +#include "torch/csrc/jit/ir/irparser.h" |
| 7 | +#include "torch/torch.h" |
| 8 | + |
| 9 | +using torch_tensorrt::tests::util::pointwise_test_helper; |
| 10 | + |
| 11 | +TEST(Converters, ATenAddConvertsCorrectly) { |
| 12 | + const auto graph = R"IR( |
| 13 | + graph(%0 : Tensor, %1 : Tensor): |
| 14 | + %2 : int = prim::Constant[value=1]() |
| 15 | + %3 : Tensor = aten::add(%0, %1, %2) |
| 16 | + return (%3))IR"; |
| 17 | + pointwise_test_helper(graph, false); |
| 18 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 19 | + pointwise_test_helper(graph, false, false, {4}, {3, 4}); |
| 20 | + pointwise_test_helper(graph, false, true, {3, 4, 3}, {4, 3}); |
| 21 | + pointwise_test_helper(graph, false, true, {4, 3}, {3, 4, 3}); |
| 22 | +} |
| 23 | + |
| 24 | +TEST(Converters, ATenAddWithAlphaConvertsCorrectly) { |
| 25 | + const auto graph = R"IR( |
| 26 | + graph(%0 : Tensor, %1 : Tensor): |
| 27 | + %2 : float = prim::Constant[value=3.2]() |
| 28 | + %3 : Tensor = aten::add(%0, %1, %2) |
| 29 | + return (%3))IR"; |
| 30 | + pointwise_test_helper(graph, false); |
| 31 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 32 | + pointwise_test_helper(graph, false, false, {4}, {3, 4}); |
| 33 | + pointwise_test_helper(graph, false, true, {3, 4, 3}, {4, 3}); |
| 34 | + pointwise_test_helper(graph, false, true, {4, 3}, {3, 4, 3}); |
| 35 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kFloat, at::kInt); |
| 36 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kInt, at::kFloat); |
| 37 | +} |
| 38 | + |
| 39 | +TEST(Converters, ATenAddInplaceWithAlphaConvertsCorrectly) { |
| 40 | + const auto graph = R"IR( |
| 41 | + graph(%0 : Tensor, %1 : Tensor): |
| 42 | + %2 : float = prim::Constant[value=7.6]() |
| 43 | + %3 : Tensor = aten::add_(%0, %1, %2) |
| 44 | + return (%3))IR"; |
| 45 | + pointwise_test_helper(graph, false); |
| 46 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 47 | + pointwise_test_helper(graph, false, true, {3, 4, 3}, {4, 3}); |
| 48 | + pointwise_test_helper(graph, false, false, {3, 4, 3}, {4, 3}, false, at::kFloat, at::kInt); |
| 49 | +} |
| 50 | + |
| 51 | +TEST(Converters, ATenAddImplicitWithIntAlphaConvertsCorrectly) { |
| 52 | + const auto graph = R"IR( |
| 53 | + graph(%0 : Tensor, %1 : Tensor): |
| 54 | + %2 : int = prim::Constant[value=42]() |
| 55 | + %3 : Tensor = aten::add_(%0, %1, %2) |
| 56 | + return (%3))IR"; |
| 57 | + pointwise_test_helper(graph, false, false, {2, 2}, {2, 2}, false, at::kInt, at::kInt); |
| 58 | + pointwise_test_helper(graph, false, false, {3, 4, 3}, {4, 3}, false, at::kInt, at::kInt); |
| 59 | +} |
| 60 | + |
| 61 | +TEST(Converters, ATenAddWithScalarConvertsCorrectly) { |
| 62 | + const auto graph = R"IR( |
| 63 | + graph(%0 : Tensor): |
| 64 | + %2 : int = prim::Constant[value=1]() |
| 65 | + %scalar : float = prim::Constant[value=2.4]() |
| 66 | + %3 : Tensor = aten::add(%0, %scalar, %2) |
| 67 | + return (%3))IR"; |
| 68 | + pointwise_test_helper(graph, true); |
| 69 | +} |
| 70 | + |
| 71 | +TEST(Converters, ATenSubConvertsCorrectly) { |
| 72 | + const auto graph = R"IR( |
| 73 | + graph(%0 : Tensor, %1 : Tensor): |
| 74 | + %2 : int = prim::Constant[value=2.3]() |
| 75 | + %3 : Tensor = aten::sub(%0, %1, %2) |
| 76 | + return (%3))IR"; |
| 77 | + pointwise_test_helper(graph, false); |
| 78 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 79 | + pointwise_test_helper(graph, false, false, {4}, {3, 4}); |
| 80 | + pointwise_test_helper(graph, false, true, {3, 4, 3}, {4, 3}); |
| 81 | + pointwise_test_helper(graph, false, true, {4, 3}, {3, 4, 3}); |
| 82 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kFloat, at::kInt); |
| 83 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kInt, at::kFloat); |
| 84 | +} |
| 85 | + |
| 86 | +TEST(Converters, ATenRsubWithTensorConvertsCorrectly) { |
| 87 | + const auto graph = R"IR( |
| 88 | + graph(%0 : Tensor, %1 : Tensor): |
| 89 | + %2 : int = prim::Constant[value=2]() |
| 90 | + %3 : Tensor = aten::rsub(%0, %1, %2) |
| 91 | + return (%3))IR"; |
| 92 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 93 | + pointwise_test_helper(graph, false, false, {4}, {3, 4}); |
| 94 | + pointwise_test_helper(graph, false, true, {4, 3, 3, 3}, {4, 3, 3, 3}); |
| 95 | + pointwise_test_helper(graph, false, false, {4, 3, 3, 3}, {4, 3, 3, 3}, false, at::kInt, at::kFloat); |
| 96 | + pointwise_test_helper(graph, false, false, {4, 3, 3, 3}, {4, 3, 3, 3}, false, at::kInt, at::kInt); |
| 97 | +} |
| 98 | + |
| 99 | +TEST(Converters, ATenRsubWithScalarConvertsCorrectly) { |
| 100 | + const auto graph = R"IR( |
| 101 | + graph(%0 : Tensor): |
| 102 | + %2 : int = prim::Constant[value=2]() |
| 103 | + %scalar : float = prim::Constant[value=2.4]() |
| 104 | + %3 : Tensor = aten::rsub(%0, %scalar, %2) |
| 105 | + return (%3))IR"; |
| 106 | + pointwise_test_helper(graph, true, false, {4, 3, 3, 3}); |
| 107 | +} |
| 108 | + |
| 109 | +TEST(Converters, ATenRsubWithIntScalarConvertsCorrectly) { |
| 110 | + const auto graph = R"IR( |
| 111 | + graph(%0 : Tensor): |
| 112 | + %2 : int = prim::Constant[value=2]() |
| 113 | + %scalar : int = prim::Constant[value=8]() |
| 114 | + %3 : Tensor = aten::rsub(%0, %scalar, %2) |
| 115 | + return (%3))IR"; |
| 116 | + pointwise_test_helper(graph, true, false, {4, 3, 3, 3}, {}, false, at::kInt); |
| 117 | +} |
| 118 | + |
| 119 | +TEST(Converters, ATenMulConvertsCorrectly) { |
| 120 | + const auto graph = R"IR( |
| 121 | + graph(%0 : Tensor, %1 : Tensor): |
| 122 | + %2 : Tensor = aten::mul(%0, %1) |
| 123 | + return (%2))IR"; |
| 124 | + pointwise_test_helper(graph, false); |
| 125 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 126 | + pointwise_test_helper(graph, false, false, {4}, {3, 4}); |
| 127 | + pointwise_test_helper(graph, false, true, {3, 4, 3}, {4, 3}); |
| 128 | + pointwise_test_helper(graph, false, true, {4, 3}, {3, 4, 3}); |
| 129 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kFloat, at::kInt); |
| 130 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kInt, at::kFloat); |
| 131 | +} |
| 132 | + |
| 133 | +TEST(Converters, ATenSquareConvertsCorrectly) { |
| 134 | + const auto graph = R"IR( |
| 135 | + graph(%0 : Tensor): |
| 136 | + %1 : Tensor = aten::square(%0) |
| 137 | + return (%1))IR"; |
| 138 | + pointwise_test_helper(graph, true); |
| 139 | +} |
| 140 | + |
| 141 | +TEST(Converters, ATenMulWithScalarConvertsCorrectly) { |
| 142 | + const auto graph = R"IR( |
| 143 | + graph(%0 : Tensor): |
| 144 | + %scalar : float = prim::Constant[value=2.4]() |
| 145 | + %1 : Tensor = aten::mul(%0, %scalar) |
| 146 | + return (%1))IR"; |
| 147 | + pointwise_test_helper(graph, true); |
| 148 | +} |
| 149 | + |
| 150 | +TEST(Converters, ATenMulWithIntScalarConvertsCorrectly) { |
| 151 | + const auto graph = R"IR( |
| 152 | + graph(%0 : Tensor): |
| 153 | + %scalar : int = prim::Constant[value=2]() |
| 154 | + %1 : Tensor = aten::mul(%0, %scalar) |
| 155 | + return (%1))IR"; |
| 156 | + pointwise_test_helper(graph, true, false, {5}, {5}, false, at::kInt); |
| 157 | +} |
| 158 | + |
| 159 | +TEST(Converters, ATenPowTensorConvertsCorrectly) { |
| 160 | + const auto graph = R"IR( |
| 161 | + graph(%x.1 : Tensor, %x2.1 : Tensor): |
| 162 | + %3 : Tensor = aten::pow(%x.1, %x2.1) |
| 163 | + return (%3))IR"; |
| 164 | + pointwise_test_helper(graph, false); |
| 165 | + pointwise_test_helper(graph, false, false, {3, 4}, {4}); |
| 166 | + pointwise_test_helper(graph, false, false, {4}, {3, 4}); |
| 167 | + pointwise_test_helper(graph, false, true, {3, 4, 3}, {4, 3}); |
| 168 | + pointwise_test_helper(graph, false, true, {4, 3}, {3, 4, 3}); |
| 169 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kFloat, at::kInt); |
| 170 | + pointwise_test_helper(graph, false, true, {5}, {5}, false, at::kInt, at::kFloat); |
| 171 | +} |
| 172 | + |
| 173 | +TEST(Converters, ATenPowScalarConvertsCorrectly) { |
| 174 | + const auto graph = R"IR( |
| 175 | + graph(%x.1 : Tensor): |
| 176 | + %2 : int = prim::Constant[value=2]() |
| 177 | + %3 : Tensor = aten::pow(%x.1, %2) |
| 178 | + return (%3))IR"; |
| 179 | + pointwise_test_helper(graph, true); |
| 180 | +} |
0 commit comments