|
| 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/csrc/jit/ir/subgraph_matcher.h" |
| 8 | + |
| 9 | +TEST(LoweringPasses, UnpackHardSigmoid) { |
| 10 | + std::string source_graph = R"IR( |
| 11 | + graph(%input): |
| 12 | + %result = aten::hardsigmoid(%input) |
| 13 | + return (%result))IR"; |
| 14 | + |
| 15 | + std::string target_graph = R"IR( |
| 16 | + graph(%x.1): |
| 17 | + %22 : float = prim::Constant[value=0.5]() |
| 18 | + %3 : int = prim::Constant[value=6]() |
| 19 | + %5 : int = prim::Constant[value=1]() |
| 20 | + %10 : int = prim::Constant[value=0]() |
| 21 | + %4 : Tensor = aten::div(%x.1, %3) |
| 22 | + %9 : Tensor = aten::add(%4, %22, %5) |
| 23 | + %21 : Tensor = aten::clamp(%9, %10, %5) |
| 24 | + return (%21))IR"; |
| 25 | + |
| 26 | + torch_tensorrt::core::util::logging::get_logger().set_reportable_log_level( |
| 27 | + torch_tensorrt::core::util::logging::LogLevel::kGRAPH); |
| 28 | + auto sg = std::make_shared<torch::jit::Graph>(); |
| 29 | + torch::jit::parseIR(source_graph, &*sg); |
| 30 | + |
| 31 | + auto in = at::rand({10, 100}, {at::kCUDA}); |
| 32 | + auto sg_params = torch_tensorrt::core::ir::get_static_params(sg->inputs(), {}); |
| 33 | + auto sg_results = torch_tensorrt::tests::util::RunGraph(sg, sg_params, {in}); |
| 34 | + |
| 35 | + torch_tensorrt::core::lowering::passes::UnpackHardSigmoid(sg); |
| 36 | + |
| 37 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 38 | + torch::jit::parseIR(target_graph, &*tg); |
| 39 | + |
| 40 | + ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty()); |
| 41 | + |
| 42 | + in = at::clone(in); |
| 43 | + auto tg_params = torch_tensorrt::core::ir::get_static_params(tg->inputs(), {}); |
| 44 | + auto tg_results = torch_tensorrt::tests::util::RunGraph(tg, tg_params, {in}); |
| 45 | + |
| 46 | + ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(sg_results[0], tg_results[0], 2e-6)); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(LoweringPasses, UnpackHardSigmoidInPlace) { |
| 50 | + std::string source_graph = R"IR( |
| 51 | + graph(%input): |
| 52 | + %result = aten::hardsigmoid_(%input) |
| 53 | + return (%result))IR"; |
| 54 | + |
| 55 | + std::string target_graph = R"IR( |
| 56 | + graph(%x.1): |
| 57 | + %22 : float = prim::Constant[value=0.5]() |
| 58 | + %3 : int = prim::Constant[value=6]() |
| 59 | + %5 : int = prim::Constant[value=1]() |
| 60 | + %10 : int = prim::Constant[value=0]() |
| 61 | + %4 : Tensor = aten::div(%x.1, %3) |
| 62 | + %9 : Tensor = aten::add(%4, %22, %5) |
| 63 | + %21 : Tensor = aten::clamp(%9, %10, %5) |
| 64 | + return (%21))IR"; |
| 65 | + |
| 66 | + torch_tensorrt::core::util::logging::get_logger().set_reportable_log_level( |
| 67 | + torch_tensorrt::core::util::logging::LogLevel::kGRAPH); |
| 68 | + auto sg = std::make_shared<torch::jit::Graph>(); |
| 69 | + torch::jit::parseIR(source_graph, &*sg); |
| 70 | + |
| 71 | + auto in = at::rand({10, 100}, {at::kCUDA}); |
| 72 | + auto sg_params = torch_tensorrt::core::ir::get_static_params(sg->inputs(), {}); |
| 73 | + auto sg_results = torch_tensorrt::tests::util::RunGraph(sg, sg_params, {in}); |
| 74 | + |
| 75 | + torch_tensorrt::core::lowering::passes::UnpackHardSigmoid(sg); |
| 76 | + |
| 77 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 78 | + torch::jit::parseIR(target_graph, &*tg); |
| 79 | + |
| 80 | + ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty()); |
| 81 | + |
| 82 | + in = at::clone(in); |
| 83 | + auto tg_params = torch_tensorrt::core::ir::get_static_params(tg->inputs(), {}); |
| 84 | + auto tg_results = torch_tensorrt::tests::util::RunGraph(tg, tg_params, {in}); |
| 85 | + |
| 86 | + ASSERT_TRUE(torch_tensorrt::tests::util::almostEqual(sg_results[0], tg_results[0], 2e-6)); |
| 87 | +} |
0 commit comments