|
| 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, AutocastLongInputs) { |
| 10 | + std::string source_graph = R"IR( |
| 11 | + graph(%long_0 : Tensor, %long_1 : Tensor): |
| 12 | + %res : Tensor = aten::add(%long_0, %long_1) |
| 13 | + return (%res))IR"; |
| 14 | + std::string target_graph = R"IR( |
| 15 | + graph(%long_0 : Tensor, %long_1 : Tensor): |
| 16 | + %3 : bool = prim::Constant[value=0]() |
| 17 | + %4 : Device = prim::Constant[value="cuda:0"]() |
| 18 | + %5 : NoneType = prim::Constant() |
| 19 | + %6 : int = prim::Constant[value=4]() |
| 20 | + %7 : Tensor = aten::to[to_compile=0](%long_0, %4, %6, %3, %3, %5) |
| 21 | + %8 : int = prim::Constant[value=4]() |
| 22 | + %9 : Tensor = aten::to[to_compile=0](%long_1, %4, %8, %3, %3, %5) |
| 23 | + %2 : Tensor = aten::add(%7, %9) |
| 24 | + return (%2))IR"; |
| 25 | + |
| 26 | + auto sg = std::make_shared<torch::jit::Graph>(); |
| 27 | + torch::jit::parseIR(source_graph, &*sg); |
| 28 | + std::unordered_map<const torch::jit::Value*, c10::optional<at::ScalarType>> type_map; |
| 29 | + type_map[sg->inputs()[0]] = at::kLong; |
| 30 | + type_map[sg->inputs()[1]] = at::kLong; |
| 31 | + torch_tensorrt::core::lowering::AutocastLongInputs(sg, type_map, "cuda:0"); |
| 32 | + auto tg = std::make_shared<torch::jit::Graph>(); |
| 33 | + torch::jit::parseIR(target_graph, &*tg); |
| 34 | + ASSERT_TRUE(sg->nodes().front()->kind() == torch::jit::prim::Constant); // confirm constants are added before casts |
| 35 | + ASSERT_TRUE(!torch::jit::findPatternMatches(*tg, *sg).empty()); |
| 36 | +} |
0 commit comments