|
| 1 | +#include <string> |
| 2 | +#include "core/compiler.h" |
| 3 | +#include "core/lowering/passes/passes.h" |
| 4 | +#include "core/util/prelude.h" |
| 5 | +#include "gtest/gtest.h" |
| 6 | +#include "tests/util/util.h" |
| 7 | +#include "torch/csrc/jit/ir/irparser.h" |
| 8 | +#include "torch/csrc/jit/ir/subgraph_matcher.h" |
| 9 | +#include "torch/csrc/jit/passes/common_subexpression_elimination.h" |
| 10 | +#include "torch/torch.h" |
| 11 | + |
| 12 | +TEST(LoweringPasses, UnpackAndCastMaskedFillLowersCorrectly) { |
| 13 | + const auto graph = R"IR( |
| 14 | + graph(%x.1: Tensor, %x.2: Tensor, %x.3: float): |
| 15 | + %2 : Tensor = aten::masked_fill_(%x.1, %x.2, %x.3) |
| 16 | + return (%2))IR"; |
| 17 | + |
| 18 | + auto in = at::rand({2, 3, 5, 7}, {at::kCUDA}); |
| 19 | + auto in2 = at::rand({2, 3, 5, 7}, {at::kCUDA}).to(torch::kBool); |
| 20 | + auto in3 = 7.3; |
| 21 | + |
| 22 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 23 | + torch::jit::parseIR(graph, g.get()); |
| 24 | + |
| 25 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in, in2, in3}); |
| 26 | + torch_tensorrt::core::lowering::passes::UnpackAndCastMaskedFill(g); |
| 27 | + torch::jit::EliminateCommonSubexpression(g); |
| 28 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in, in2, in3}); |
| 29 | + |
| 30 | + ASSERT_TRUE( |
| 31 | + torch_tensorrt::tests::util::almostEqual(jit_pre_results[0].toTensor(), jit_post_results[0].toTensor(), 2e-6)); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(LoweringPasses, UnpackAndCastNumToTensorLowersIntCorrectly) { |
| 35 | + const auto graph = R"IR( |
| 36 | + graph(%x.1: int): |
| 37 | + %2 : Tensor = prim::NumToTensor(%x.1) |
| 38 | + return (%2))IR"; |
| 39 | + |
| 40 | + auto in = 1; |
| 41 | + |
| 42 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 43 | + torch::jit::parseIR(graph, g.get()); |
| 44 | + |
| 45 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 46 | + torch_tensorrt::core::lowering::passes::UnpackAndCastNumToTensor(g); |
| 47 | + torch::jit::EliminateCommonSubexpression(g); |
| 48 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 49 | + |
| 50 | + ASSERT_TRUE( |
| 51 | + torch_tensorrt::tests::util::almostEqual(jit_pre_results[0].toTensor(), jit_post_results[0].toTensor(), 2e-6)); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(LoweringPasses, UnpackAndCastNumToTensorLowersFloatCorrectly) { |
| 55 | + const auto graph = R"IR( |
| 56 | + graph(%x.1: float): |
| 57 | + %2 : Tensor = prim::NumToTensor(%x.1) |
| 58 | + return (%2))IR"; |
| 59 | + |
| 60 | + auto in = 78.1; |
| 61 | + |
| 62 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 63 | + torch::jit::parseIR(graph, g.get()); |
| 64 | + |
| 65 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 66 | + torch_tensorrt::core::lowering::passes::UnpackAndCastNumToTensor(g); |
| 67 | + torch::jit::EliminateCommonSubexpression(g); |
| 68 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 69 | + |
| 70 | + ASSERT_TRUE( |
| 71 | + torch_tensorrt::tests::util::almostEqual(jit_pre_results[0].toTensor(), jit_post_results[0].toTensor(), 2e-6)); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(LoweringPasses, UnpackAndCastFullIntLowersCorrectly) { |
| 75 | + const auto graph = R"IR( |
| 76 | + graph(%x.1: int): |
| 77 | + %5 : NoneType = prim::Constant() |
| 78 | + %2 : int = prim::Constant[value=3]() |
| 79 | + %10 : int[] = prim::ListConstruct(%2, %2) |
| 80 | + %out : Tensor = aten::full(%10, %x.1, %5, %5, %5, %5) |
| 81 | + return (%out))IR"; |
| 82 | + |
| 83 | + auto in = 4; |
| 84 | + |
| 85 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 86 | + torch::jit::parseIR(graph, g.get()); |
| 87 | + |
| 88 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 89 | + torch_tensorrt::core::lowering::passes::UnpackAndCastFull(g); |
| 90 | + torch::jit::EliminateCommonSubexpression(g); |
| 91 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 92 | + |
| 93 | + ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual( |
| 94 | + jit_pre_results[0].toTensor(), jit_post_results[0].toTensor().cpu(), 2e-6)); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(LoweringPasses, UnpackAndCastFullFloatLowersCorrectly) { |
| 98 | + const auto graph = R"IR( |
| 99 | + graph(%x.1: float): |
| 100 | + %5 : NoneType = prim::Constant() |
| 101 | + %2 : int = prim::Constant[value=5]() |
| 102 | + %3 : int = prim::Constant[value=4]() |
| 103 | + %10 : int[] = prim::ListConstruct(%2, %3) |
| 104 | + %out : Tensor = aten::full(%10, %x.1, %5, %5, %5, %5) |
| 105 | + return (%out))IR"; |
| 106 | + |
| 107 | + auto in = 54.1; |
| 108 | + |
| 109 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 110 | + torch::jit::parseIR(graph, g.get()); |
| 111 | + |
| 112 | + auto jit_pre_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 113 | + torch_tensorrt::core::lowering::passes::UnpackAndCastFull(g); |
| 114 | + torch::jit::EliminateCommonSubexpression(g); |
| 115 | + auto jit_post_results = torch_tensorrt::tests::util::EvaluateGraphJIT(g, {in}); |
| 116 | + |
| 117 | + ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual( |
| 118 | + jit_pre_results[0].toTensor(), jit_post_results[0].toTensor().cpu(), 2e-6)); |
| 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