|
| 1 | +#include <string> |
| 2 | +#include "core/partitioning/partitioning.h" |
| 3 | +#include "core/util/trt_util.h" |
| 4 | +#include "gtest/gtest.h" |
| 5 | +#include "torch/csrc/jit/ir/irparser.h" |
| 6 | +#include "torch/script.h" |
| 7 | + |
| 8 | +bool checkInsertedCastNodeNumber(torch_tensorrt::core::partitioning::SegmentedBlock& seg_block, int target_count) { |
| 9 | + int64_t cnt = 0; |
| 10 | + for (auto node : seg_block.nodes()) { |
| 11 | + if (node->kind().toQualString() == std::string("aten::to")) { |
| 12 | + cnt++; |
| 13 | + } |
| 14 | + } |
| 15 | + std::cout << "Found count of " << cnt << " inserted aten::to nodes, (looking for " << target_count |
| 16 | + << " aten::to nodes)" << std::endl; |
| 17 | + |
| 18 | + return target_count == cnt; |
| 19 | +} |
| 20 | + |
| 21 | +TEST(Partitioning, ExplicitNodeAutoConversionCorrectly) { |
| 22 | + const auto graph = R"IR( |
| 23 | + graph(%0 : Tensor, |
| 24 | + %1 : Tensor): |
| 25 | + %2 : int = prim::Constant[value=4]() |
| 26 | + %3 : bool = prim::Constant[value=0]() |
| 27 | + %4 : NoneType = prim::Constant() |
| 28 | + %5 : int = prim::Constant[value=1]() |
| 29 | + %7: Tensor = aten::to(%1, %2, %3, %3, %4) |
| 30 | + %8 : Tensor = aten::mul(%0, %0) |
| 31 | + %9 : Tensor = aten::scatter(%8, %5, %7, %5) |
| 32 | + %10 : Tensor = aten::scatter(%7, %5, %7, %5) |
| 33 | + %12 : Tensor = aten::add(%10, %10, %5) |
| 34 | + return (%9, %12))IR"; |
| 35 | + |
| 36 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 37 | + torch::jit::parseIR(graph, g.get(), true); |
| 38 | + |
| 39 | + torch_tensorrt::core::partitioning::PartitioningInfo partitioning_info; |
| 40 | + partitioning_info.enabled = true; |
| 41 | + partitioning_info.forced_fallback_operators = {"aten::scatter"}; |
| 42 | + partitioning_info.truncate_long_and_double = true; |
| 43 | + std::vector<torch_tensorrt::core::ir::Input> inputs; |
| 44 | + inputs.push_back(torch_tensorrt::core::ir::Input({5, 5})); |
| 45 | + inputs.push_back(torch_tensorrt::core::ir::Input({5, 5})); |
| 46 | + |
| 47 | + std::unordered_map<const torch::jit::Value*, std::vector<torch_tensorrt::core::ir::Input>> inputs_map; |
| 48 | + std::unordered_map<const torch::jit::Value*, std::vector<c10::optional<at::ScalarType>>> input_types; |
| 49 | + inputs_map.insert({g->inputs()[0], {inputs[0]}}); |
| 50 | + input_types.insert({g->inputs()[0], {{at::kFloat}}}); |
| 51 | + inputs_map.insert({g->inputs()[1], {inputs[1]}}); |
| 52 | + input_types.insert({g->inputs()[1], {{at::kInt}}}); |
| 53 | + |
| 54 | + auto input_ivalues_map = torch_tensorrt::core::partitioning::generateRandomInputs(inputs_map, input_types); |
| 55 | + |
| 56 | + torch_tensorrt::core::partitioning::PartitioningCtx ctx(g->block(), partitioning_info); |
| 57 | + torch_tensorrt::core::partitioning::partition(&ctx, input_ivalues_map); |
| 58 | + auto segmented_blocks = ctx.partitioned_blocks.begin()->second; |
| 59 | + |
| 60 | + for (auto& seg_block : segmented_blocks) { |
| 61 | + LOG_DEBUG(seg_block << " cur seg block"); |
| 62 | + } |
| 63 | + ASSERT_TRUE(checkInsertedCastNodeNumber(segmented_blocks[1], 2)); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(Partitioning, ImplicitAutoConversionCorrectly) { |
| 67 | + const auto graph = R"IR( |
| 68 | + graph(%0 : Tensor): |
| 69 | + %2 : int = prim::Constant[value=0]() |
| 70 | + %4 : int = aten::size(%0, %2) |
| 71 | + %6 : Tensor = prim::NumToTensor(%4) |
| 72 | + %2 : int = prim::Constant[value=5]() |
| 73 | + %7 : int[] = prim::ListConstruct(%2, %2) |
| 74 | + %8 : bool = prim::Constant[value=0]() |
| 75 | + %9 : Tensor = aten::expand(%6, %7, %8) |
| 76 | +
|
| 77 | + %10 : Tensor = aten::mul(%9, %9) |
| 78 | + return (%10))IR"; |
| 79 | + |
| 80 | + auto g = std::make_shared<torch::jit::Graph>(); |
| 81 | + torch::jit::parseIR(graph, g.get(), true); |
| 82 | + |
| 83 | + torch_tensorrt::core::partitioning::PartitioningInfo partitioning_info; |
| 84 | + partitioning_info.enabled = true; |
| 85 | + partitioning_info.forced_fallback_operators = {"aten::expand"}; |
| 86 | + partitioning_info.truncate_long_and_double = true; |
| 87 | + std::vector<torch_tensorrt::core::ir::Input> inputs; |
| 88 | + |
| 89 | + inputs.push_back(torch_tensorrt::core::ir::Input({5, 5})); |
| 90 | + |
| 91 | + std::unordered_map<const torch::jit::Value*, std::vector<torch_tensorrt::core::ir::Input>> inputs_map; |
| 92 | + std::unordered_map<const torch::jit::Value*, std::vector<c10::optional<at::ScalarType>>> input_types; |
| 93 | + inputs_map.insert({g->inputs()[0], {inputs[0]}}); |
| 94 | + input_types.insert({g->inputs()[0], {{at::kFloat}}}); |
| 95 | + |
| 96 | + auto input_ivalues_map = torch_tensorrt::core::partitioning::generateRandomInputs(inputs_map, input_types); |
| 97 | + |
| 98 | + torch_tensorrt::core::partitioning::PartitioningCtx ctx(g->block(), partitioning_info); |
| 99 | + torch_tensorrt::core::partitioning::partition(&ctx, input_ivalues_map); |
| 100 | + auto segmented_blocks = ctx.partitioned_blocks.begin()->second; |
| 101 | + |
| 102 | + for (auto& seg_block : segmented_blocks) { |
| 103 | + LOG_DEBUG(seg_block << " cur seg block"); |
| 104 | + } |
| 105 | + ASSERT_TRUE(checkInsertedCastNodeNumber(segmented_blocks[1], 2)); |
| 106 | +} |
0 commit comments