diff --git a/onnxruntime/core/graph/function_utils.cc b/onnxruntime/core/graph/function_utils.cc index 1ea90439fc..c7a02dd266 100644 --- a/onnxruntime/core/graph/function_utils.cc +++ b/onnxruntime/core/graph/function_utils.cc @@ -359,7 +359,10 @@ private: // if the call-node contains the attribute. Otherwise, this attribute must be removed. auto entry = attr_map.find(attr.ref_attr_name()); if (entry != attr_map.cend()) { + // Copy value of attribute, but retain original name: + std::string name = attr.name(); attr = entry->second; + attr.set_name(name); } else { attr_iter = attributes.erase(attr_iter); continue; diff --git a/onnxruntime/test/framework/function_test.cc b/onnxruntime/test/framework/function_test.cc index 1f79e36c70..bfdd5cc5cc 100644 --- a/onnxruntime/test/framework/function_test.cc +++ b/onnxruntime/test/framework/function_test.cc @@ -59,7 +59,7 @@ static void Check(const char* source, std::vector fetches; status = session_object.Run(run_options, feeds, AsSpan({std::string(output_name)}), &fetches); - ASSERT_TRUE(status.IsOK()) << "Session Run failed."; + ASSERT_TRUE(status.IsOK()) << "Session Run failed: " << status.ErrorMessage() << std::endl; auto& tensor = fetches[0].Get(); size_t size = static_cast(tensor.Shape().Size()); @@ -287,5 +287,34 @@ TEST(FunctionTest, CallInConditional) { Check(code, "x", {1.0, 2.0, 3.0}, "y", {6.0, 12.0, 18.0}); } +// Test use of attibute references, especially where source/target attribute +// names are not the same. In this example, the "start : int = @s" attribute-reference +// binds the attribute named "start" of the Shape op to the attribute named "s" +// of the containing function myfun. +TEST(FunctionTest, AttrName) { + const char* code = R"( + < + ir_version: 8, + opset_import: [ "" : 16, "local" : 1 ] + > + agraph (float[N] x) => (float[N] y) + { + y = local.myfun (x) + } + + < + opset_import: [ "" : 16 ], + domain: "local" + > + myfun (lx) => (ly) { + d = Shape (lx) + df = Cast (d) + ly = Mul (lx, df) + } + )"; + + Check(code, "x", {1.0, 2.0, 3.0}, "y", {3.0, 6.0, 9.0}); +} + } // namespace test } // namespace onnxruntime