diff --git a/onnxruntime/core/graph/function_utils.cc b/onnxruntime/core/graph/function_utils.cc index c16d10851fe13..a320d7b454f54 100644 --- a/onnxruntime/core/graph/function_utils.cc +++ b/onnxruntime/core/graph/function_utils.cc @@ -348,7 +348,7 @@ class Inliner { name = new_name; } - void rename(std::string& name) { + void rename(std::string& name, bool is_new_def) { if (name.empty()) return; for (auto i = rename_scopes.size(); i > 0; --i) { const auto& map = rename_scopes[i - 1]; @@ -358,7 +358,10 @@ class Inliner { return; } } - make_unique(name); + if (is_new_def) { + make_unique(name); + } + // Otherwise, it is a reference to an outer-scope variable that should not be renamed. } template @@ -396,10 +399,10 @@ class Inliner { n.set_name(prefix + n.name()); for (auto& x : *n.mutable_input()) { - rename(x); + rename(x, false); } for (auto& y : *n.mutable_output()) { - rename(y); + rename(y, true); } auto& attributes = *n.mutable_attribute(); for (auto attr_iter = attributes.begin(); attr_iter != attributes.end();) { diff --git a/onnxruntime/test/framework/function_test.cc b/onnxruntime/test/framework/function_test.cc index bf696edb3b1a7..c74997930d5be 100644 --- a/onnxruntime/test/framework/function_test.cc +++ b/onnxruntime/test/framework/function_test.cc @@ -352,5 +352,25 @@ TEST(FunctionTest, Variadics) { ASSERT_STATUS_OK(session_object.Load(model_uri)); ASSERT_STATUS_OK(session_object.Initialize()); } + +// Test use of outer-scope names inside sub-graphs in functions that are inlined. +TEST(FunctionTest, OuterScopeName) { + const char* code = R"( + + agraph (float[N] x) => (float[N] y) + { + xseq = SequenceConstruct (x) + zeros = Constant () + yseq = SequenceMap (xseq) (float[6] ly) { + ly = Concat (lx, zeros) + }> + zero = Constant () + y = SequenceAt (yseq, zero) + } + )"; + + Check(code, "x", {1.0, 2.0, 3.0}, "y", {1.0, 2.0, 3.0, 0.0, 0.0, 0.0}); +} } // namespace test } // namespace onnxruntime