|
5 | 5 | #include "gtest/gtest.h"
|
6 | 6 | #include "tests/util/util.h"
|
7 | 7 | #include "torch/csrc/jit/ir/irparser.h"
|
| 8 | +#include "torch/csrc/jit/ir/subgraph_matcher.h" |
8 | 9 | #include "torch/csrc/jit/passes/common_subexpression_elimination.h"
|
9 | 10 | #include "torch/torch.h"
|
10 | 11 |
|
@@ -36,8 +37,8 @@ TEST(LoweringPasses, UnpackAndCastNumToTensorLowersIntCorrectly) {
|
36 | 37 | %2 : Tensor = prim::NumToTensor(%x.1)
|
37 | 38 | return (%2))IR";
|
38 | 39 |
|
39 |
| - // Make range [0.01, 1.01] to ensure positives / avoid NaN with negative sqrt |
40 | 40 | auto in = 1;
|
| 41 | + |
41 | 42 | auto g = std::make_shared<torch::jit::Graph>();
|
42 | 43 | torch::jit::parseIR(graph, g.get());
|
43 | 44 |
|
@@ -116,3 +117,78 @@ TEST(LoweringPasses, UnpackAndCastFullFloatLowersCorrectly) {
|
116 | 117 | ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(
|
117 | 118 | jit_pre_results[0].toTensor(), jit_post_results[0].toTensor().cpu(), 2e-6));
|
118 | 119 | }
|
| 120 | + |
| 121 | +TEST(LoweringPasses, ReplaceScalarImplicitLowersCorrectly) { |
| 122 | + const auto graph = R"IR( |
| 123 | + graph(%x.1: Tensor): |
| 124 | + %5 : int = prim::Constant[value=0]() |
| 125 | + %false : bool = prim::Constant[value=0]() |
| 126 | + %none : NoneType = prim::Constant() |
| 127 | + %cuda : Device = prim::Constant[value="cuda"]() |
| 128 | + %3 : int = aten::size(%x.1, %5) |
| 129 | + %y.2 : Tensor = prim::NumToTensor(%3) |
| 130 | + %y.1 : Tensor = aten::to(%y.2, %cuda, %none, %false, %false) |
| 131 | + %19 : Tensor[] = prim::ListConstruct(%x.1, %y.1) |
| 132 | + %21 : Tensor, %22 : Tensor = prim::ListUnpack(%19) |
| 133 | + %2 : Scalar = aten::ScalarImplicit(%22) |
| 134 | + %out : Tensor = prim::NumToTensor(%2) |
| 135 | + return (%out))IR"; |
| 136 | + |
| 137 | + auto in = at::rand({2, 3, 5, 7}, {at::kCUDA}); |
| 138 | + |
| 139 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 140 | + torch::jit::parseIR(graph, g.get()); |
| 141 | + |
| 142 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 143 | + torch_tensorrt::core::lowering::passes::ReplaceScalarImplicit(g); |
| 144 | + torch::jit::EliminateCommonSubexpression(g); |
| 145 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 146 | + |
| 147 | + ASSERT_TRUE( |
| 148 | + torch_tensorrt::tests::util::almostEqual(jit_pre_results[0].toTensor(), jit_post_results[0].toTensor(), 2e-6)); |
| 149 | +} |
| 150 | + |
| 151 | +TEST(LoweringPasses, ReplaceScalarImplicitIntNumToTensorLowersCorrectly) { |
| 152 | + const auto graph = R"IR( |
| 153 | + graph(%x.1: int): |
| 154 | + %1 : Tensor = prim::NumToTensor(%x.1) |
| 155 | + %2 : Scalar = aten::ScalarImplicit(%1) |
| 156 | + %3 : Tensor = prim::NumToTensor(%2) |
| 157 | + return (%3))IR"; |
| 158 | + |
| 159 | + auto in = 25; |
| 160 | + |
| 161 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 162 | + torch::jit::parseIR(graph, g.get()); |
| 163 | + |
| 164 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 165 | + torch_tensorrt::core::lowering::passes::UnpackAndCastNumToTensor(g); |
| 166 | + torch_tensorrt::core::lowering::passes::ReplaceScalarImplicit(g); |
| 167 | + torch::jit::EliminateCommonSubexpression(g); |
| 168 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 169 | + |
| 170 | + ASSERT_TRUE( |
| 171 | + torch_tensorrt::tests::util::almostEqual(jit_pre_results[0].toTensor(), jit_post_results[0].toTensor(), 2e-6)); |
| 172 | +} |
| 173 | + |
| 174 | +TEST(LoweringPasses, ReplaceScalarImplicitFloatLowersCorrectly) { |
| 175 | + const auto graph = R"IR( |
| 176 | + graph(%x.1: float): |
| 177 | + %1 : Tensor = prim::NumToTensor(%x.1) |
| 178 | + %2 : Scalar = aten::ScalarImplicit(%1) |
| 179 | + %3 : Tensor = prim::NumToTensor(%2) |
| 180 | + return (%3))IR"; |
| 181 | + |
| 182 | + auto in = 2.5; |
| 183 | + |
| 184 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 185 | + torch::jit::parseIR(graph, g.get()); |
| 186 | + |
| 187 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 188 | + torch_tensorrt::core::lowering::passes::ReplaceScalarImplicit(g); |
| 189 | + torch::jit::EliminateCommonSubexpression(g); |
| 190 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 191 | + |
| 192 | + ASSERT_TRUE( |
| 193 | + torch_tensorrt::tests::util::almostEqual(jit_pre_results[0].toTensor(), jit_post_results[0].toTensor(), 2e-6)); |
| 194 | +} |
0 commit comments