Skip to content

chore: Add efficientnet b0 and VIT to testsuite #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tests/modules/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@
"model": models.detection.fasterrcnn_resnet50_fpn(pretrained=True),
"path": "script"
},
"vit": {
"efficientnet_b0": {
"model": timm.create_model('efficientnet_b0', pretrained=True),
"path": "script"
},
"vit": {
"model": timm.create_model('vit_base_patch16_224', pretrained=True),
"path": "script"
}
}

Expand Down
10 changes: 6 additions & 4 deletions tests/modules/module_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
#include "torch/script.h"
#include "trtorch/trtorch.h"

using PathAndInSize = std::pair<std::string, std::vector<std::vector<int64_t>>>;
using PathAndInSize = std::tuple<std::string, std::vector<std::vector<int64_t>>, float>;

class ModuleTests : public testing::TestWithParam<PathAndInSize> {
public:
void SetUp() override {
auto params = GetParam();
auto path = params.first;
PathAndInSize params = GetParam();
std::string path = std::get<0>(params);
try {
// Deserialize the ScriptModule from a file using torch::jit::load().
mod = torch::jit::load(path);
} catch (const c10::Error& e) {
std::cerr << "error loading the model\n";
return;
}
input_shapes = params.second;
input_shapes = std::get<1>(params);
threshold = std::get<2>(params);
}

void TearDown() {
Expand All @@ -31,4 +32,5 @@ class ModuleTests : public testing::TestWithParam<PathAndInSize> {
protected:
torch::jit::script::Module mod;
std::vector<std::vector<int64_t>> input_shapes;
float threshold;
};
17 changes: 10 additions & 7 deletions tests/modules/test_compiled_modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ TEST_P(ModuleTests, CompiledModuleIsClose) {
trt_results.push_back(trt_results_ivalues.toTensor());

for (size_t i = 0; i < trt_results.size(); i++) {
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[i], trt_results[i].reshape_as(jit_results[i]), 2e-5));
ASSERT_TRUE(
trtorch::tests::util::almostEqual(jit_results[i], trt_results[i].reshape_as(jit_results[i]), threshold));
}
}

INSTANTIATE_TEST_SUITE_P(
CompiledModuleForwardIsCloseSuite,
ModuleTests,
testing::Values(
PathAndInSize({"tests/modules/resnet18_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/resnet50_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/mobilenet_v2_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/resnet18_scripted.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/resnet50_scripted.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/mobilenet_v2_scripted.jit.pt", {{1, 3, 224, 224}}})));
PathAndInSize({"tests/modules/resnet18_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/resnet50_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/mobilenet_v2_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/resnet18_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/resnet50_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/mobilenet_v2_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/efficientnet_b0_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/vit_scripted.jit.pt", {{1, 3, 224, 224}}, 8e-3})));
18 changes: 10 additions & 8 deletions tests/modules/test_modules_as_engines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST_P(ModuleTests, ModuleAsEngineIsClose) {
jit_results.push_back(jit_results_ivalues.toTensor());
auto trt_results = trtorch::tests::util::RunModuleForwardAsEngine(mod, inputs);

ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-5));
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
}

TEST_P(ModuleTests, ModuleToEngineToModuleIsClose) {
Expand Down Expand Up @@ -41,16 +41,18 @@ TEST_P(ModuleTests, ModuleToEngineToModuleIsClose) {
std::vector<at::Tensor> trt_results;
trt_results.push_back(trt_results_ivalues.toTensor());

ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), 2e-5));
ASSERT_TRUE(trtorch::tests::util::almostEqual(jit_results[0], trt_results[0].reshape_as(jit_results[0]), threshold));
}

INSTANTIATE_TEST_SUITE_P(
ModuleAsEngineForwardIsCloseSuite,
ModuleTests,
testing::Values(
PathAndInSize({"tests/modules/resnet18_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/resnet50_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/mobilenet_v2_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/resnet18_scripted.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/resnet50_scripted.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/mobilenet_v2_scripted.jit.pt", {{1, 3, 224, 224}}})));
PathAndInSize({"tests/modules/resnet18_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/resnet50_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/mobilenet_v2_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/resnet18_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/resnet50_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/mobilenet_v2_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/efficientnet_b0_scripted.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/vit_scripted.jit.pt", {{1, 3, 224, 224}}, 8e-3})));
8 changes: 4 additions & 4 deletions tests/modules/test_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST_P(ModuleTests, SerializedModuleIsStillCorrect) {

for (size_t i = 0; i < pre_serialized_results.size(); i++) {
ASSERT_TRUE(trtorch::tests::util::almostEqual(
post_serialized_results[i], pre_serialized_results[i].reshape_as(post_serialized_results[i]), 2e-5));
post_serialized_results[i], pre_serialized_results[i].reshape_as(post_serialized_results[i]), threshold));
}
}

Expand Down Expand Up @@ -72,13 +72,13 @@ TEST_P(ModuleTests, SerializedDynamicModuleIsStillCorrect) {

for (size_t i = 0; i < pre_serialized_results.size(); i++) {
ASSERT_TRUE(trtorch::tests::util::almostEqual(
post_serialized_results[i], pre_serialized_results[i].reshape_as(post_serialized_results[i]), 2e-5));
post_serialized_results[i], pre_serialized_results[i].reshape_as(post_serialized_results[i]), threshold));
}
}

INSTANTIATE_TEST_SUITE_P(
CompiledModuleForwardIsCloseSuite,
ModuleTests,
testing::Values(
PathAndInSize({"tests/modules/resnet18_traced.jit.pt", {{1, 3, 224, 224}}}),
PathAndInSize({"tests/modules/pooling_traced.jit.pt", {{1, 3, 10, 10}}})));
PathAndInSize({"tests/modules/resnet18_traced.jit.pt", {{1, 3, 224, 224}}, 2e-5}),
PathAndInSize({"tests/modules/pooling_traced.jit.pt", {{1, 3, 10, 10}}, 2e-5})));