|
| 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, ReduceGeluCorrectly) { |
| 10 | + std::string source_graph = R"IR( |
| 11 | + graph(%x): |
| 12 | + %out : Tensor = aten::gelu(%x) |
| 13 | + return (%out))IR"; |
| 14 | + std::string target_graph = R"IR( |
| 15 | + graph(%x.1 : Tensor): |
| 16 | + %6 : float = prim::Constant[value=0.044714999999999998]() |
| 17 | + %5 : float = prim::Constant[value=0.79788456080000003]() |
| 18 | + %4 : float = prim::Constant[value=1.]() |
| 19 | + %3 : float = prim::Constant[value=0.5]() |
| 20 | + %2 : int = prim::Constant[value=1]() |
| 21 | + %7 : Tensor = aten::mul(%x.1, %3) |
| 22 | + %8 : Tensor = aten::mul(%x.1, %5) |
| 23 | + %9 : Tensor = aten::mul(%x.1, %6) |
| 24 | + %10 : Tensor = aten::mul(%9, %x.1) |
| 25 | + %11 : Tensor = aten::add(%10, %4, %2) |
| 26 | + %12 : Tensor = aten::mul(%8, %11) |
| 27 | + %13 : Tensor = aten::tanh(%12) |
| 28 | + %14 : Tensor = aten::add(%13, %4, %2) |
| 29 | + %15 : Tensor = aten::mul(%7, %14) |
| 30 | + return (%15))IR"; |
| 31 | + |
| 32 | + torch_tensorrt::core::util::logging::get_logger().set_reportable_log_level( |
| 33 | + torch_tensorrt::core::util::logging::LogLevel::kGRAPH); |
| 34 | + auto sg = std::make_shared<torch::jit::Graph>(); |
| 35 | + torch::jit::parseIR(source_graph, &*sg); |
| 36 | + torch_tensorrt::core::lowering::passes::ReduceGelu(sg); |
| 37 | + |
| 38 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 39 | + torch::jit::parseIR(target_graph, &*tg); |
| 40 | + |
| 41 | + ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty()); |
| 42 | +} |
0 commit comments