Skip to content

[fix] unmangle_cls_name for variable length mangled tags #1454

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
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
5 changes: 3 additions & 2 deletions core/lowering/passes/module_fallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ std::string unmangle_cls_name(const std::string& name) {

std::size_t mangle_pos = unmangled.find("___torch_mangle_");
if (mangle_pos != std::string::npos) {
unmangled.erase(mangle_pos, 21);
std::size_t dot_pos = unmangled.find(".", mangle_pos);
TORCH_CHECK(dot_pos != std::string::npos, "Expected to find '.' after '___torch_mangle_' in name: ", unmangled);
unmangled.erase(mangle_pos, dot_pos - mangle_pos + 1);
}

return unmangled;
}

Expand Down
3 changes: 3 additions & 0 deletions core/lowering/passes/passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ void UnpackAndCastNumToTensor(std::shared_ptr<torch::jit::Graph>& graph, std::st
void UnpackAndCastFull(std::shared_ptr<torch::jit::Graph>& graph, std::string target_device_name);
void ReplaceScalarImplicit(std::shared_ptr<torch::jit::Graph>& graph);

// utility functions exposed for testing
std::string unmangle_cls_name(const std::string& name);

} // namespace passes
} // namespace lowering
} // namespace core
Expand Down
14 changes: 14 additions & 0 deletions tests/core/lowering/test_module_fallback_passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,17 @@ TEST(Lowering, LowerAndPartitionSimpleModuleFallbackCorrectly) {
auto trt_results = trt_mod.forward(trt_inputs_ivalues).toTensor();
ASSERT_TRUE(torch_tensorrt::tests::util::cosineSimEqual(jit_results, trt_results, 0.99));
}

TEST(Lowering, UnmangleClsName) {
EXPECT_EQ(
"foo.Bar", torch_tensorrt::core::lowering::passes::unmangle_cls_name("__torch__.foo.___torch_mangle_605.Bar"));
EXPECT_EQ(
"torch.nn.modules.conv.Conv2d",
torch_tensorrt::core::lowering::passes::unmangle_cls_name(
"__torch__.torch.nn.modules.conv.___torch_mangle_5697.Conv2d"));
EXPECT_EQ(
"custom_models.ModuleFallbackMain",
torch_tensorrt::core::lowering::passes::unmangle_cls_name("__torch__.custom_models.ModuleFallbackMain"));
EXPECT_THROW(
torch_tensorrt::core::lowering::passes::unmangle_cls_name("__torch__.foo.___torch_mangle_605"), std::exception);
}