diff --git a/onnxruntime/core/optimizer/constant_folding.cc b/onnxruntime/core/optimizer/constant_folding.cc index 0dcae0be3e9ac..c315e121bb9d6 100644 --- a/onnxruntime/core/optimizer/constant_folding.cc +++ b/onnxruntime/core/optimizer/constant_folding.cc @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +#include #include #include @@ -675,13 +676,11 @@ Status ConstantFolding::ApplyImpl(Graph& graph, bool& modified, int graph_level, // Build the TensorProto that corresponds to the computed OrtValue and add it as initializer to the graph. auto* constant_arg_out = node->MutableOutputDefs()[output_idx]; const Tensor& out_tensor = ort_value.Get(); - // Fallback to deep copy for aliased tensors (e.g., Reshape, Unsqueeze) - // to prevent dangling pointers when parent initializers are freed. - const bool use_tensor_buffer = out_tensor.OwnsBuffer(); + assert(out_tensor.OwnsBuffer()); ONNX_NAMESPACE::TensorProto out_tensorproto = utils::TensorToTensorProto( out_tensor, constant_arg_out->Name(), - use_tensor_buffer); + /*use_tensor_buffer=*/true); ONNX_NAMESPACE::TensorShapeProto result_shape; for (auto& dim : out_tensor.Shape().GetDims()) { @@ -689,7 +688,7 @@ Status ConstantFolding::ApplyImpl(Graph& graph, bool& modified, int graph_level, } constant_arg_out->SetShape(result_shape); - // Inlined data (small or deep-copied tensors) does not need to hold an OrtValue reference. + // Inline data does not need an OrtValue to keep a backing buffer alive. if (!utils::HasExternalData(out_tensorproto)) { ORT_THROW_IF_ERROR(graph.AddInitializedOrtValue(out_tensorproto, OrtValue())); } else {