|
| 1 | +#include <torch/torch.h> |
| 2 | +#include <string> |
| 3 | +#include "core/compiler.h" |
| 4 | +#include "gtest/gtest.h" |
| 5 | +#include "tests/util/util.h" |
| 6 | +#include "torch/csrc/jit/ir/irparser.h" |
| 7 | + |
| 8 | +TEST(Converters, ATenExpandSameDimConvertsCorrectly) { |
| 9 | + const auto graph = R"IR( |
| 10 | + graph(%x.1 : Tensor): |
| 11 | + %2 : int[] = prim::Constant[value=[3, 4]]() |
| 12 | + %3 : bool = prim::Constant[value=0]() |
| 13 | + %4 : Tensor = aten::expand(%x.1, %2, %3) |
| 14 | + return (%4))IR"; |
| 15 | + |
| 16 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 17 | + |
| 18 | + torch::jit::parseIR(graph, &*g); |
| 19 | + |
| 20 | + auto in = at::randint(1, 10, {3, 1}, {at::kCUDA}); |
| 21 | + |
| 22 | + auto jit_in = at::clone(in); |
| 23 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 24 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in}); |
| 25 | + |
| 26 | + auto trt_in = at::clone(in); |
| 27 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 28 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in}); |
| 29 | + |
| 30 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 31 | + |
| 32 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(Converters, ATenExpandTileConvertsCorrectly) { |
| 36 | + const auto graph = R"IR( |
| 37 | + graph(%x.1 : Tensor): |
| 38 | + %2 : int[] = prim::Constant[value=[2, 3, 1]]() |
| 39 | + %3 : bool = prim::Constant[value=0]() |
| 40 | + %4 : Tensor = aten::expand(%x.1, %2, %3) |
| 41 | + return (%4))IR"; |
| 42 | + |
| 43 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 44 | + |
| 45 | + torch::jit::parseIR(graph, &*g); |
| 46 | + |
| 47 | + auto in = at::randint(1, 10, {3, 1}, {at::kCUDA}); |
| 48 | + |
| 49 | + auto jit_in = at::clone(in); |
| 50 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 51 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in}); |
| 52 | + |
| 53 | + auto trt_in = at::clone(in); |
| 54 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 55 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in}); |
| 56 | + |
| 57 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 58 | + |
| 59 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(Converters, ATenExpandTileLastConvertsCorrectly) { |
| 63 | + const auto graph = R"IR( |
| 64 | + graph(%x.1 : Tensor): |
| 65 | + %2 : int[] = prim::Constant[value=[1, 3, 4]]() |
| 66 | + %3 : bool = prim::Constant[value=0]() |
| 67 | + %4 : Tensor = aten::expand(%x.1, %2, %3) |
| 68 | + return (%4))IR"; |
| 69 | + |
| 70 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 71 | + |
| 72 | + torch::jit::parseIR(graph, &*g); |
| 73 | + |
| 74 | + auto in = at::randint(1, 10, {3, 1}, {at::kCUDA}); |
| 75 | + |
| 76 | + auto jit_in = at::clone(in); |
| 77 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 78 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in}); |
| 79 | + |
| 80 | + auto trt_in = at::clone(in); |
| 81 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 82 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in}); |
| 83 | + |
| 84 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 85 | + |
| 86 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 87 | +} |
| 88 | + |
| 89 | +/* Expand_as layer takes two inputs and only dimensions of second input are |
| 90 | + actually used. TRT prunes away the second input. This will result in internal |
| 91 | + failure from TRT. To avoid unrelated issues, we add a dummy operation which |
| 92 | + outputs second_input+2 as a second output. The second input is preserved. |
| 93 | +*/ |
| 94 | +TEST(Converters, ATenExpandASConvertsCorrectly) { |
| 95 | + const auto graph = R"IR( |
| 96 | + graph(%x.1 : Tensor, |
| 97 | + %y.1 : Tensor): |
| 98 | + %3 : int = prim::Constant[value=1]() |
| 99 | + %4 : int = prim::Constant[value=2]() |
| 100 | + %5 : Tensor = aten::expand_as(%x.1, %y.1) |
| 101 | + %6 : Tensor = aten::add(%y.1, %4, %3) |
| 102 | + return (%5, %6))IR"; |
| 103 | + |
| 104 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 105 | + |
| 106 | + torch::jit::parseIR(graph, &*g); |
| 107 | + |
| 108 | + auto in = at::randint(1, 10, {3, 1}, {at::kCUDA}); |
| 109 | + auto target_in = at::randint(1, 10, {2, 3, 1}, {at::kCUDA}); |
| 110 | + |
| 111 | + auto jit_in = at::clone(in); |
| 112 | + auto jit_target_in = at::clone(target_in); |
| 113 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 114 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in, jit_target_in}); |
| 115 | + |
| 116 | + auto trt_in = at::clone(jit_in); |
| 117 | + auto trt_target_in = at::clone(jit_target_in); |
| 118 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 119 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in, trt_target_in}); |
| 120 | + |
| 121 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 122 | + |
| 123 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 124 | +} |
| 125 | + |
| 126 | +TEST(Converters, ATenRepeatConvertsCorrectly) { |
| 127 | + const auto graph = R"IR( |
| 128 | + graph(%x.1 : Tensor): |
| 129 | + %2 : int[] = prim::Constant[value=[4, 2]]() |
| 130 | + %3 : Tensor = aten::repeat(%x.1, %2) |
| 131 | + return (%3))IR"; |
| 132 | + |
| 133 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 134 | + |
| 135 | + torch::jit::parseIR(graph, &*g); |
| 136 | + |
| 137 | + auto in = at::randint(1, 10, {1, 3}, {at::kCUDA}); |
| 138 | + |
| 139 | + auto jit_in = at::clone(in); |
| 140 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 141 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in}); |
| 142 | + |
| 143 | + auto trt_in = at::clone(jit_in); |
| 144 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 145 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in}); |
| 146 | + |
| 147 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 148 | + |
| 149 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 150 | +} |
| 151 | + |
| 152 | +TEST(Converters, ATenRepeat3dConvertsCorrectly) { |
| 153 | + const auto graph = R"IR( |
| 154 | + graph(%x.1 : Tensor): |
| 155 | + %2 : int[] = prim::Constant[value=[2, 2, 2]]() |
| 156 | + %3 : Tensor = aten::repeat(%x.1, %2) |
| 157 | + return (%3))IR"; |
| 158 | + |
| 159 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 160 | + |
| 161 | + torch::jit::parseIR(graph, &*g); |
| 162 | + |
| 163 | + auto in = at::randint(1, 10, {2, 3, 2}, {at::kCUDA}); |
| 164 | + |
| 165 | + auto jit_in = at::clone(in); |
| 166 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 167 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in}); |
| 168 | + |
| 169 | + auto trt_in = at::clone(jit_in); |
| 170 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 171 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in}); |
| 172 | + |
| 173 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 174 | + |
| 175 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 176 | +} |
| 177 | + |
| 178 | +TEST(Converters, ATenRepeatExtraDimsConvertsCorrectly) { |
| 179 | + const auto graph = R"IR( |
| 180 | + graph(%x.1 : Tensor): |
| 181 | + %2 : int[] = prim::Constant[value=[1, 3, 2]]() |
| 182 | + %3 : Tensor = aten::repeat(%x.1, %2) |
| 183 | + return (%3))IR"; |
| 184 | + |
| 185 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 186 | + |
| 187 | + torch::jit::parseIR(graph, &*g); |
| 188 | + |
| 189 | + auto in = at::randint(1, 10, {1, 3}, {at::kCUDA}); |
| 190 | + |
| 191 | + auto jit_in = at::clone(in); |
| 192 | + auto params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 193 | + auto jit_results = trtorch::tests::util::RunGraph(g, params, {jit_in}); |
| 194 | + |
| 195 | + auto trt_in = at::clone(jit_in); |
| 196 | + params = trtorch::core::conversion::get_named_params(g->inputs(), {}); |
| 197 | + auto trt_results = trtorch::tests::util::RunGraphEngine(g, params, {trt_in}); |
| 198 | + |
| 199 | + auto trt = trt_results[0].reshape(jit_results[0].sizes()); |
| 200 | + |
| 201 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt, 2e-6)); |
| 202 | +} |
0 commit comments