From a69aea231a2d4a170017c5e67187e8cfaa63a9a9 Mon Sep 17 00:00:00 2001 From: Beheshti Date: Tue, 21 Jul 2026 11:45:24 -0700 Subject: [PATCH 1/7] fix output shape mismtach for pre allocated buffer --- onnxruntime/core/framework/execution_frame.cc | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index 59efae597ceb2..9a6b8a45862bc 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -17,6 +17,7 @@ #include "core/framework/session_state.h" #include "core/framework/TensorSeq.h" #include "core/framework/utils.h" +#include "core/common/logging/logging.h" #if !defined(ORT_MINIMAL_BUILD) && defined(ORT_MEMORY_PROFILE) #include "core/framework/memory_info.h" #endif @@ -189,23 +190,29 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int } if (!shape_matched) { - const TensorShape& existing_shape = p_ort_value->IsTensor() - ? p_ort_value->Get().Shape() -#if !defined(DISABLE_SPARSE_TENSORS) - : p_ort_value->Get().DenseShape(); -#else - : *shape; // unreachable, but satisfies compiler -#endif - return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, - "The output OrtValue provided for output '", - node.OutputDefs()[output_index]->Name(), - "' of node '", node.Name(), - "' (", node.OpType(), ") has shape ", existing_shape, - " but the computed output shape for this run is ", *shape, - ". When calling Run() with pre-allocated output OrtValues on a model " - "with dynamic output shapes, either supply unallocated output OrtValues " - "or ensure the pre-allocated shapes match the expected output shapes " - "for each run."); + bool reused_caller_buffer = false; + LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; + if (p_ort_value->IsTensor()) { + ORT_RETURN_IF_NOT(shape != nullptr, "shape must not be null for tensor output that is already allocated"); + auto& tensor = *p_ort_value->GetMutable(); + // Keep caller-provided buffers for dynamic outputs when only the runtime + // dimensions changed and the total element count is unchanged. + LOGS_DEFAULT(VERBOSE) << "Trying in-place reshape for caller-provided output buffer."; + if (tensor.Shape().Size() == shape->Size()) { + LOGS_DEFAULT(VERBOSE) << "Reusing caller buffer by reshape. old_num_elements=" + << tensor.Shape().Size() << " new_num_elements=" << shape->Size(); + tensor.Reshape(*shape); + reused_caller_buffer = true; + } + } + + if (!reused_caller_buffer) { + // For dynamic outputs where the caller-provided buffer cannot represent + // the runtime shape, replace with a newly allocated OrtValue. + LOGS_DEFAULT(VERBOSE) << "Caller buffer cannot be reused; allocating fresh output OrtValue."; + *p_ort_value = OrtValue(); + status = CreateNodeOutputMLValueImpl(*p_ort_value, ort_value_idx, shape); + } } } else { // shape is nullptr for traditional ML output values From 8126418e0b09d365bb0c74cb5873f263079d1412 Mon Sep 17 00:00:00 2001 From: Beheshti Date: Wed, 22 Jul 2026 11:20:47 -0700 Subject: [PATCH 2/7] apply new changes and test unit --- onnxruntime/core/framework/execution_frame.cc | 49 +++++++++++------- .../test/framework/execution_frame_test.cc | 51 +++++++++++++++++++ 2 files changed, 82 insertions(+), 18 deletions(-) diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index 9a6b8a45862bc..f3180f4fcb49c 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -190,29 +190,42 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int } if (!shape_matched) { - bool reused_caller_buffer = false; - LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; - if (p_ort_value->IsTensor()) { - ORT_RETURN_IF_NOT(shape != nullptr, "shape must not be null for tensor output that is already allocated"); + const TensorShape& existing_shape = p_ort_value->IsTensor() + ? p_ort_value->Get().Shape() +#if !defined(DISABLE_SPARSE_TENSORS) + : p_ort_value->Get().DenseShape(); +#else + : *shape; // unreachable, but satisfies compiler +#endif + bool reused_caller_buffer = false; + LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; + std::cout << "Output shape mismatch for pre-allocated fetch buffer." << std::endl; + auto& tensor = *p_ort_value->GetMutable(); // Keep caller-provided buffers for dynamic outputs when only the runtime // dimensions changed and the total element count is unchanged. LOGS_DEFAULT(VERBOSE) << "Trying in-place reshape for caller-provided output buffer."; - if (tensor.Shape().Size() == shape->Size()) { - LOGS_DEFAULT(VERBOSE) << "Reusing caller buffer by reshape. old_num_elements=" - << tensor.Shape().Size() << " new_num_elements=" << shape->Size(); - tensor.Reshape(*shape); - reused_caller_buffer = true; + const bool old_shape_is_singleton_vector = shape->NumDimensions() == 1 && shape->AsShapeVector()[0] == 1; // {1} + const bool new_shape_is_scalar = tensor.Shape().NumDimensions() == 0; // {} + const bool can_reshape_in_place = (old_shape_is_singleton_vector && new_shape_is_scalar); + if (can_reshape_in_place) { + LOGS_DEFAULT(VERBOSE) << "Reusing caller buffer by reshape. old_num_elements=" + << tensor.Shape().Size() << " new_num_elements=" << shape->Size(); + tensor.Reshape(*shape); + reused_caller_buffer = true; + } + else { + return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, + "The output OrtValue provided for output '", + node.OutputDefs()[output_index]->Name(), + "' of node '", node.Name(), + "' (", node.OpType(), ") has shape ", existing_shape, + " but the computed output shape for this run is ", *shape, + ". When calling Run() with pre-allocated output OrtValues on a model " + "with dynamic output shapes, either supply unallocated output OrtValues " + "or ensure the pre-allocated shapes match the expected output shapes " + "for each run."); } - } - - if (!reused_caller_buffer) { - // For dynamic outputs where the caller-provided buffer cannot represent - // the runtime shape, replace with a newly allocated OrtValue. - LOGS_DEFAULT(VERBOSE) << "Caller buffer cannot be reused; allocating fresh output OrtValue."; - *p_ort_value = OrtValue(); - status = CreateNodeOutputMLValueImpl(*p_ort_value, ort_value_idx, shape); - } } } else { // shape is nullptr for traditional ML output values diff --git a/onnxruntime/test/framework/execution_frame_test.cc b/onnxruntime/test/framework/execution_frame_test.cc index bbfe22a2d4bc7..b0378d5916ed6 100644 --- a/onnxruntime/test/framework/execution_frame_test.cc +++ b/onnxruntime/test/framework/execution_frame_test.cc @@ -743,5 +743,56 @@ TEST(ExecutionFrameTestInit, SparseInitializerAsOutput) { } #endif // !defined(DISABLE_SPARSE_TENSORS) +TEST(ExecutionFrameTestInit, FetchReusesPreallocatedScalarOutputForSingleElementVector) { + SessionOptions so; + so.enable_mem_pattern = true; + + InferenceSession session(so, GetEnvironment()); + + onnxruntime::Model model("scalar_to_vector1_output_test", false, ModelMetaData(), PathString(), + IOnnxRuntimeOpSchemaRegistryList(), + { {kOnnxDomain, 12} }, {}, DefaultLoggingManager().DefaultLogger()); + auto& graph = model.MainGraph(); + + TypeProto float_tensor; + float_tensor.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT); + + auto& input_arg = graph.GetOrCreateNodeArg("X", &float_tensor); + auto& output_arg = graph.GetOrCreateNodeArg("Y", &float_tensor); + graph.AddNode("identity", "Identity", "identity", { &input_arg }, { &output_arg }); + graph.SetInputs({ &input_arg }); + graph.SetOutputs({ &output_arg }); + ASSERT_STATUS_OK(graph.Resolve()); + + std::string serialized; + ASSERT_TRUE(model.ToProto().SerializeToString(&serialized)); + std::istringstream model_stream(serialized); + ASSERT_STATUS_OK(session.Load(model_stream)); + ASSERT_STATUS_OK(session.Initialize()); + + auto allocator = test::AllocatorManager::Instance().GetAllocator(CPU); + + std::array input_data = { 5.0f }; + OrtValue input; + Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({ 1 }), input_data.data(), allocator->Info(), input); + + // Pre-allocate scalar output ({}). Runtime output for this run is {1}. + OrtValue preallocated_output; + Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({}), allocator, preallocated_output); + const void* preallocated_buffer = preallocated_output.Get().DataRaw(); + std::vector results = { preallocated_output }; + + RunOptions ro; + ASSERT_STATUS_OK(session.Run(ro, + AsSpan({ std::string("X") }), AsSpan({ input }), + AsSpan({ std::string("Y") }), &results, nullptr)); + + ASSERT_EQ(results.size(), 1u); + ASSERT_TRUE(results[0].IsTensor()); + EXPECT_EQ(results[0].Get().Shape(), TensorShape({ 1 })); + EXPECT_EQ(results[0].Get().DataRaw(), preallocated_buffer); + EXPECT_EQ(results[0].Get().DataAsSpan()[0], 5.0f); +} + } // namespace test } // namespace onnxruntime From 8776eaceb58d1e60a5fa29908d0c6bb9ab319ba3 Mon Sep 17 00:00:00 2001 From: Beheshti Date: Wed, 22 Jul 2026 12:56:26 -0700 Subject: [PATCH 3/7] fix format --- .../test/framework/execution_frame_test.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/onnxruntime/test/framework/execution_frame_test.cc b/onnxruntime/test/framework/execution_frame_test.cc index b0378d5916ed6..6bd14c0da9fdd 100644 --- a/onnxruntime/test/framework/execution_frame_test.cc +++ b/onnxruntime/test/framework/execution_frame_test.cc @@ -751,7 +751,7 @@ TEST(ExecutionFrameTestInit, FetchReusesPreallocatedScalarOutputForSingleElement onnxruntime::Model model("scalar_to_vector1_output_test", false, ModelMetaData(), PathString(), IOnnxRuntimeOpSchemaRegistryList(), - { {kOnnxDomain, 12} }, {}, DefaultLoggingManager().DefaultLogger()); + {{kOnnxDomain, 12}}, {}, DefaultLoggingManager().DefaultLogger()); auto& graph = model.MainGraph(); TypeProto float_tensor; @@ -759,9 +759,9 @@ TEST(ExecutionFrameTestInit, FetchReusesPreallocatedScalarOutputForSingleElement auto& input_arg = graph.GetOrCreateNodeArg("X", &float_tensor); auto& output_arg = graph.GetOrCreateNodeArg("Y", &float_tensor); - graph.AddNode("identity", "Identity", "identity", { &input_arg }, { &output_arg }); - graph.SetInputs({ &input_arg }); - graph.SetOutputs({ &output_arg }); + graph.AddNode("identity", "Identity", "identity", {&input_arg}, {&output_arg}); + graph.SetInputs({&input_arg}); + graph.SetOutputs({&output_arg}); ASSERT_STATUS_OK(graph.Resolve()); std::string serialized; @@ -772,9 +772,9 @@ TEST(ExecutionFrameTestInit, FetchReusesPreallocatedScalarOutputForSingleElement auto allocator = test::AllocatorManager::Instance().GetAllocator(CPU); - std::array input_data = { 5.0f }; + std::array input_data = {5.0f}; OrtValue input; - Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({ 1 }), input_data.data(), allocator->Info(), input); + Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({1}), input_data.data(), allocator->Info(), input); // Pre-allocate scalar output ({}). Runtime output for this run is {1}. OrtValue preallocated_output; @@ -784,12 +784,12 @@ TEST(ExecutionFrameTestInit, FetchReusesPreallocatedScalarOutputForSingleElement RunOptions ro; ASSERT_STATUS_OK(session.Run(ro, - AsSpan({ std::string("X") }), AsSpan({ input }), - AsSpan({ std::string("Y") }), &results, nullptr)); + AsSpan({std::string("X")}), AsSpan({input}), + AsSpan({std::string("Y")}), &results, nullptr)); ASSERT_EQ(results.size(), 1u); ASSERT_TRUE(results[0].IsTensor()); - EXPECT_EQ(results[0].Get().Shape(), TensorShape({ 1 })); + EXPECT_EQ(results[0].Get().Shape(), TensorShape({1})); EXPECT_EQ(results[0].Get().DataRaw(), preallocated_buffer); EXPECT_EQ(results[0].Get().DataAsSpan()[0], 5.0f); } From 39d447efcd6499d0f0cb40925e87524ee2dfea42 Mon Sep 17 00:00:00 2001 From: Beheshti Date: Wed, 22 Jul 2026 13:00:44 -0700 Subject: [PATCH 4/7] remove cout --- onnxruntime/core/framework/execution_frame.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index f3180f4fcb49c..ab5c1ca965e20 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -199,8 +199,6 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int #endif bool reused_caller_buffer = false; LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; - std::cout << "Output shape mismatch for pre-allocated fetch buffer." << std::endl; - auto& tensor = *p_ort_value->GetMutable(); // Keep caller-provided buffers for dynamic outputs when only the runtime // dimensions changed and the total element count is unchanged. From 8c225b05ffff1d36936c3094de684fe6931ebe6b Mon Sep 17 00:00:00 2001 From: Beheshti Date: Wed, 22 Jul 2026 13:03:30 -0700 Subject: [PATCH 5/7] fix format space --- onnxruntime/core/framework/execution_frame.cc | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index ab5c1ca965e20..47c698e0cb771 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -191,11 +191,11 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int if (!shape_matched) { const TensorShape& existing_shape = p_ort_value->IsTensor() - ? p_ort_value->Get().Shape() + ? p_ort_value->Get().Shape() #if !defined(DISABLE_SPARSE_TENSORS) - : p_ort_value->Get().DenseShape(); + : p_ort_value->Get().DenseShape(); #else - : *shape; // unreachable, but satisfies compiler + : *shape; // unreachable, but satisfies compiler #endif bool reused_caller_buffer = false; LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; @@ -214,15 +214,15 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int } else { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, - "The output OrtValue provided for output '", - node.OutputDefs()[output_index]->Name(), - "' of node '", node.Name(), - "' (", node.OpType(), ") has shape ", existing_shape, - " but the computed output shape for this run is ", *shape, - ". When calling Run() with pre-allocated output OrtValues on a model " - "with dynamic output shapes, either supply unallocated output OrtValues " - "or ensure the pre-allocated shapes match the expected output shapes " - "for each run."); + "The output OrtValue provided for output '", + node.OutputDefs()[output_index]->Name(), + "' of node '", node.Name(), + "' (", node.OpType(), ") has shape ", existing_shape, + " but the computed output shape for this run is ", *shape, + ". When calling Run() with pre-allocated output OrtValues on a model " + "with dynamic output shapes, either supply unallocated output OrtValues " + "or ensure the pre-allocated shapes match the expected output shapes " + "for each run."); } } } else { From 4bbe28cc69e6d2f65e6960e235e105c43f638465 Mon Sep 17 00:00:00 2001 From: Beheshti Date: Wed, 22 Jul 2026 14:58:53 -0700 Subject: [PATCH 6/7] fix lint issues after lintrunner --- onnxruntime/core/framework/execution_frame.cc | 61 +++++++------- .../test/framework/execution_frame_test.cc | 84 +++++++++---------- 2 files changed, 72 insertions(+), 73 deletions(-) diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index 47c698e0cb771..847a8ad4eba18 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -190,40 +190,39 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int } if (!shape_matched) { - const TensorShape& existing_shape = p_ort_value->IsTensor() - ? p_ort_value->Get().Shape() + const TensorShape& existing_shape = p_ort_value->IsTensor() + ? p_ort_value->Get().Shape() #if !defined(DISABLE_SPARSE_TENSORS) - : p_ort_value->Get().DenseShape(); + : p_ort_value->Get().DenseShape(); #else - : *shape; // unreachable, but satisfies compiler + : *shape; // unreachable, but satisfies compiler #endif - bool reused_caller_buffer = false; - LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; - auto& tensor = *p_ort_value->GetMutable(); - // Keep caller-provided buffers for dynamic outputs when only the runtime - // dimensions changed and the total element count is unchanged. - LOGS_DEFAULT(VERBOSE) << "Trying in-place reshape for caller-provided output buffer."; - const bool old_shape_is_singleton_vector = shape->NumDimensions() == 1 && shape->AsShapeVector()[0] == 1; // {1} - const bool new_shape_is_scalar = tensor.Shape().NumDimensions() == 0; // {} - const bool can_reshape_in_place = (old_shape_is_singleton_vector && new_shape_is_scalar); - if (can_reshape_in_place) { - LOGS_DEFAULT(VERBOSE) << "Reusing caller buffer by reshape. old_num_elements=" - << tensor.Shape().Size() << " new_num_elements=" << shape->Size(); - tensor.Reshape(*shape); - reused_caller_buffer = true; - } - else { - return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, - "The output OrtValue provided for output '", - node.OutputDefs()[output_index]->Name(), - "' of node '", node.Name(), - "' (", node.OpType(), ") has shape ", existing_shape, - " but the computed output shape for this run is ", *shape, - ". When calling Run() with pre-allocated output OrtValues on a model " - "with dynamic output shapes, either supply unallocated output OrtValues " - "or ensure the pre-allocated shapes match the expected output shapes " - "for each run."); - } + bool reused_caller_buffer = false; + LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; + auto& tensor = *p_ort_value->GetMutable(); + // Keep caller-provided buffers for dynamic outputs when only the runtime + // dimensions changed and the total element count is unchanged. + LOGS_DEFAULT(VERBOSE) << "Trying in-place reshape for caller-provided output buffer."; + const bool old_shape_is_singleton_vector = shape->NumDimensions() == 1 && shape->AsShapeVector()[0] == 1; // {1} + const bool new_shape_is_scalar = tensor.Shape().NumDimensions() == 0; // {} + const bool can_reshape_in_place = (old_shape_is_singleton_vector && new_shape_is_scalar); + if (can_reshape_in_place) { + LOGS_DEFAULT(VERBOSE) << "Reusing caller buffer by reshape. old_num_elements=" + << tensor.Shape().Size() << " new_num_elements=" << shape->Size(); + tensor.Reshape(*shape); + reused_caller_buffer = true; + } else { + return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, + "The output OrtValue provided for output '", + node.OutputDefs()[output_index]->Name(), + "' of node '", node.Name(), + "' (", node.OpType(), ") has shape ", existing_shape, + " but the computed output shape for this run is ", *shape, + ". When calling Run() with pre-allocated output OrtValues on a model " + "with dynamic output shapes, either supply unallocated output OrtValues " + "or ensure the pre-allocated shapes match the expected output shapes " + "for each run."); + } } } else { // shape is nullptr for traditional ML output values diff --git a/onnxruntime/test/framework/execution_frame_test.cc b/onnxruntime/test/framework/execution_frame_test.cc index 6bd14c0da9fdd..6f321e19971cd 100644 --- a/onnxruntime/test/framework/execution_frame_test.cc +++ b/onnxruntime/test/framework/execution_frame_test.cc @@ -744,54 +744,54 @@ TEST(ExecutionFrameTestInit, SparseInitializerAsOutput) { #endif // !defined(DISABLE_SPARSE_TENSORS) TEST(ExecutionFrameTestInit, FetchReusesPreallocatedScalarOutputForSingleElementVector) { - SessionOptions so; - so.enable_mem_pattern = true; + SessionOptions so; + so.enable_mem_pattern = true; - InferenceSession session(so, GetEnvironment()); + InferenceSession session(so, GetEnvironment()); - onnxruntime::Model model("scalar_to_vector1_output_test", false, ModelMetaData(), PathString(), - IOnnxRuntimeOpSchemaRegistryList(), - {{kOnnxDomain, 12}}, {}, DefaultLoggingManager().DefaultLogger()); - auto& graph = model.MainGraph(); - - TypeProto float_tensor; - float_tensor.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT); - - auto& input_arg = graph.GetOrCreateNodeArg("X", &float_tensor); - auto& output_arg = graph.GetOrCreateNodeArg("Y", &float_tensor); - graph.AddNode("identity", "Identity", "identity", {&input_arg}, {&output_arg}); - graph.SetInputs({&input_arg}); - graph.SetOutputs({&output_arg}); - ASSERT_STATUS_OK(graph.Resolve()); - - std::string serialized; - ASSERT_TRUE(model.ToProto().SerializeToString(&serialized)); - std::istringstream model_stream(serialized); - ASSERT_STATUS_OK(session.Load(model_stream)); - ASSERT_STATUS_OK(session.Initialize()); + onnxruntime::Model model("scalar_to_vector1_output_test", false, ModelMetaData(), PathString(), + IOnnxRuntimeOpSchemaRegistryList(), + {{kOnnxDomain, 12}}, {}, DefaultLoggingManager().DefaultLogger()); + auto& graph = model.MainGraph(); - auto allocator = test::AllocatorManager::Instance().GetAllocator(CPU); + TypeProto float_tensor; + float_tensor.mutable_tensor_type()->set_elem_type(TensorProto_DataType_FLOAT); + + auto& input_arg = graph.GetOrCreateNodeArg("X", &float_tensor); + auto& output_arg = graph.GetOrCreateNodeArg("Y", &float_tensor); + graph.AddNode("identity", "Identity", "identity", {&input_arg}, {&output_arg}); + graph.SetInputs({&input_arg}); + graph.SetOutputs({&output_arg}); + ASSERT_STATUS_OK(graph.Resolve()); + + std::string serialized; + ASSERT_TRUE(model.ToProto().SerializeToString(&serialized)); + std::istringstream model_stream(serialized); + ASSERT_STATUS_OK(session.Load(model_stream)); + ASSERT_STATUS_OK(session.Initialize()); - std::array input_data = {5.0f}; - OrtValue input; - Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({1}), input_data.data(), allocator->Info(), input); + auto allocator = test::AllocatorManager::Instance().GetAllocator(CPU); - // Pre-allocate scalar output ({}). Runtime output for this run is {1}. - OrtValue preallocated_output; - Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({}), allocator, preallocated_output); - const void* preallocated_buffer = preallocated_output.Get().DataRaw(); - std::vector results = { preallocated_output }; + std::array input_data = {5.0f}; + OrtValue input; + Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({1}), input_data.data(), allocator->Info(), input); - RunOptions ro; - ASSERT_STATUS_OK(session.Run(ro, - AsSpan({std::string("X")}), AsSpan({input}), - AsSpan({std::string("Y")}), &results, nullptr)); - - ASSERT_EQ(results.size(), 1u); - ASSERT_TRUE(results[0].IsTensor()); - EXPECT_EQ(results[0].Get().Shape(), TensorShape({1})); - EXPECT_EQ(results[0].Get().DataRaw(), preallocated_buffer); - EXPECT_EQ(results[0].Get().DataAsSpan()[0], 5.0f); + // Pre-allocate scalar output ({}). Runtime output for this run is {1}. + OrtValue preallocated_output; + Tensor::InitOrtValue(DataTypeImpl::GetType(), TensorShape({}), allocator, preallocated_output); + const void* preallocated_buffer = preallocated_output.Get().DataRaw(); + std::vector results = {preallocated_output}; + + RunOptions ro; + ASSERT_STATUS_OK(session.Run(ro, + AsSpan({std::string("X")}), AsSpan({input}), + AsSpan({std::string("Y")}), &results, nullptr)); + + ASSERT_EQ(results.size(), 1u); + ASSERT_TRUE(results[0].IsTensor()); + EXPECT_EQ(results[0].Get().Shape(), TensorShape({1})); + EXPECT_EQ(results[0].Get().DataRaw(), preallocated_buffer); + EXPECT_EQ(results[0].Get().DataAsSpan()[0], 5.0f); } } // namespace test From adac26136612a917125e5fec05a4c91838e11409 Mon Sep 17 00:00:00 2001 From: Beheshti Date: Thu, 23 Jul 2026 10:48:26 -0700 Subject: [PATCH 7/7] remove unused variable --- onnxruntime/core/framework/execution_frame.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/onnxruntime/core/framework/execution_frame.cc b/onnxruntime/core/framework/execution_frame.cc index 847a8ad4eba18..15001355c8366 100644 --- a/onnxruntime/core/framework/execution_frame.cc +++ b/onnxruntime/core/framework/execution_frame.cc @@ -197,7 +197,6 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int #else : *shape; // unreachable, but satisfies compiler #endif - bool reused_caller_buffer = false; LOGS_DEFAULT(VERBOSE) << "Output shape mismatch for pre-allocated fetch buffer."; auto& tensor = *p_ort_value->GetMutable(); // Keep caller-provided buffers for dynamic outputs when only the runtime @@ -210,7 +209,6 @@ Status IExecutionFrame::GetOrCreateNodeOutputMLValue(const int output_index, int LOGS_DEFAULT(VERBOSE) << "Reusing caller buffer by reshape. old_num_elements=" << tensor.Shape().Size() << " new_num_elements=" << shape->Size(); tensor.Reshape(*shape); - reused_caller_buffer = true; } else { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "The output OrtValue provided for output '",