|
| 1 | +#include <string> |
| 2 | +#include <unordered_set> |
| 3 | +#include "core/compiler.h" |
| 4 | +#include "core/lowering/lowering.h" |
| 5 | +#include "gtest/gtest.h" |
| 6 | +#include "tests/util/util.h" |
| 7 | +#include "torch/script.h" |
| 8 | + |
| 9 | +TEST(Lowering, LowerResNet18ModuleFallbackCorrectly) { |
| 10 | + torch::jit::script::Module mod; |
| 11 | + try { |
| 12 | + mod = torch::jit::load("tests/modules/resnet18_traced.jit.pt"); |
| 13 | + } catch (const c10::Error& e) { |
| 14 | + std::cerr << "error loading the model\n"; |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const std::vector<std::vector<int64_t>> input_shapes = {{1, 3, 224, 224}}; |
| 19 | + std::vector<torch::jit::IValue> jit_inputs_ivalues; |
| 20 | + std::vector<torch::jit::IValue> trt_inputs_ivalues; |
| 21 | + for (auto in_shape : input_shapes) { |
| 22 | + auto in = at::randint(5, in_shape, {at::kCUDA}); |
| 23 | + jit_inputs_ivalues.push_back(in.clone()); |
| 24 | + trt_inputs_ivalues.push_back(in.clone()); |
| 25 | + } |
| 26 | + |
| 27 | + std::vector<trtorch::core::ir::Input> input_ranges{trtorch::core::ir::Input({1, 3, 224, 224})}; |
| 28 | + trtorch::core::CompileSpec cfg(input_ranges); |
| 29 | + cfg.partition_info.enabled = true; |
| 30 | + cfg.lower_info.forced_fallback_modules.push_back("torchvision.models.resnet.BasicBlock"); |
| 31 | + |
| 32 | + auto jit_results = mod.forward(jit_inputs_ivalues).toTensor(); |
| 33 | + auto trt_mod = trtorch::core::CompileGraph(mod, cfg); |
| 34 | + |
| 35 | + auto g = trt_mod.get_method("forward").graph(); |
| 36 | + auto nodes = g->block()->nodes(); |
| 37 | + std::size_t count = 0; |
| 38 | + for (const auto n : nodes) { |
| 39 | + auto has_compile_attribute = n->hasAttribute(c10::Symbol::attr("to_compile")); |
| 40 | + if (has_compile_attribute && n->i(c10::Symbol::attr("to_compile")) == (int64_t) false) { |
| 41 | + count++; |
| 42 | + } |
| 43 | + } |
| 44 | + ASSERT_TRUE(count == 62); |
| 45 | + |
| 46 | + auto trt_results = trt_mod.forward(trt_inputs_ivalues).toTensor(); |
| 47 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results, trt_results, 2e-6)); |
| 48 | +} |
| 49 | + |
| 50 | +TEST(Lowering, LowerAndPartitionSimpleModuleFallbackCorrectly) { |
| 51 | + torch::jit::script::Module mod; |
| 52 | + try { |
| 53 | + mod = torch::jit::load("tests/modules/module_fallback_scripted.jit.pt"); |
| 54 | + } catch (const c10::Error& e) { |
| 55 | + std::cerr << "error loading the model\n"; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const std::vector<std::vector<int64_t>> input_shapes = {{1, 1, 16, 16}}; |
| 60 | + std::vector<torch::jit::IValue> jit_inputs_ivalues; |
| 61 | + std::vector<torch::jit::IValue> trt_inputs_ivalues; |
| 62 | + for (auto in_shape : input_shapes) { |
| 63 | + auto in = at::randint(5, in_shape, {at::kCUDA}); |
| 64 | + jit_inputs_ivalues.push_back(in.clone()); |
| 65 | + trt_inputs_ivalues.push_back(in.clone()); |
| 66 | + } |
| 67 | + |
| 68 | + std::vector<trtorch::core::ir::Input> input_ranges{trtorch::core::ir::Input({1, 1, 16, 16})}; |
| 69 | + trtorch::core::CompileSpec cfg(input_ranges); |
| 70 | + cfg.partition_info.enabled = true; |
| 71 | + cfg.lower_info.forced_fallback_modules.push_back("ModuleFallbackSub"); |
| 72 | + |
| 73 | + auto jit_results = mod.forward(jit_inputs_ivalues).toTensor(); |
| 74 | + auto trt_mod = trtorch::core::CompileGraph(mod, cfg); |
| 75 | + |
| 76 | + auto g = trt_mod.get_method("forward").graph(); |
| 77 | + auto nodes = g->block()->nodes(); |
| 78 | + std::size_t curr_node = 0; |
| 79 | + for (const auto n : nodes) { |
| 80 | + if (curr_node == 5) { |
| 81 | + ASSERT_TRUE(n->kind() == torch::jit::aten::conv2d); |
| 82 | + ASSERT_TRUE(n->i(c10::Symbol::attr("to_compile")) == (int64_t) false); |
| 83 | + } else if (curr_node == 6) { |
| 84 | + ASSERT_TRUE(n->kind() == torch::jit::aten::relu); |
| 85 | + ASSERT_TRUE(n->i(c10::Symbol::attr("to_compile")) == (int64_t) false); |
| 86 | + } else if (curr_node == 7) { |
| 87 | + ASSERT_TRUE(n->kind() == torch::jit::prim::GetAttr); |
| 88 | + ASSERT_TRUE(n->s(c10::Symbol::attr("name")).find("trt_engine") != std::string::npos); |
| 89 | + } |
| 90 | + curr_node++; |
| 91 | + } |
| 92 | + |
| 93 | + auto trt_results = trt_mod.forward(trt_inputs_ivalues).toTensor(); |
| 94 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results, trt_results, 2e-6)); |
| 95 | +} |
| 96 | + |
| 97 | +TEST(Lowering, LowerAndPartitionMobileNetModuleFallbackCorrectly) { |
| 98 | + torch::jit::script::Module mod; |
| 99 | + try { |
| 100 | + mod = torch::jit::load("tests/modules/mobilenet_v2_traced.jit.pt"); |
| 101 | + } catch (const c10::Error& e) { |
| 102 | + std::cerr << "error loading the model\n"; |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + const std::vector<std::vector<int64_t>> input_shapes = {{1, 3, 224, 224}}; |
| 107 | + std::vector<torch::jit::IValue> jit_inputs_ivalues; |
| 108 | + std::vector<torch::jit::IValue> trt_inputs_ivalues; |
| 109 | + for (auto in_shape : input_shapes) { |
| 110 | + auto in = at::randint(5, in_shape, {at::kCUDA}); |
| 111 | + jit_inputs_ivalues.push_back(in.clone()); |
| 112 | + trt_inputs_ivalues.push_back(in.clone()); |
| 113 | + } |
| 114 | + |
| 115 | + std::vector<trtorch::core::ir::Input> input_ranges{trtorch::core::ir::Input({1, 3, 224, 224})}; |
| 116 | + trtorch::core::CompileSpec cfg(input_ranges); |
| 117 | + cfg.partition_info.enabled = true; |
| 118 | + cfg.partition_info.min_block_size = 5; |
| 119 | + cfg.lower_info.forced_fallback_modules.push_back("torchvision.models.mobilenetv2.ConvBNActivation"); |
| 120 | + |
| 121 | + auto jit_results = mod.forward(jit_inputs_ivalues).toTensor(); |
| 122 | + auto trt_mod = trtorch::core::CompileGraph(mod, cfg); |
| 123 | + |
| 124 | + auto g = trt_mod.get_method("forward").graph(); |
| 125 | + auto nodes = g->block()->nodes(); |
| 126 | + std::size_t trt_count = 0; |
| 127 | + std::size_t fallback_count = 0; |
| 128 | + for (const auto n : nodes) { |
| 129 | + auto has_name_attribute = n->hasAttribute(c10::Symbol::attr("name")); |
| 130 | + auto has_compile_attribute = n->hasAttribute(c10::Symbol::attr("to_compile")); |
| 131 | + if (has_name_attribute && n->s(c10::Symbol::attr("name")).find("trt_engine") != std::string::npos) { |
| 132 | + trt_count++; |
| 133 | + } else if (has_compile_attribute && n->i(c10::Symbol::attr("to_compile")) == (int64_t) false) { |
| 134 | + fallback_count++; |
| 135 | + } |
| 136 | + } |
| 137 | + ASSERT_TRUE(trt_count == 1); |
| 138 | + ASSERT_TRUE(fallback_count == 105); |
| 139 | + |
| 140 | + auto trt_results = trt_mod.forward(trt_inputs_ivalues).toTensor(); |
| 141 | + ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results, trt_results, 2e-6)); |
| 142 | +} |
0 commit comments