Skip to content
Merged
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
9 changes: 4 additions & 5 deletions onnxruntime/core/optimizer/constant_folding.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#include <cassert>
#include <limits>
#include <string>

Expand Down Expand Up @@ -675,21 +676,19 @@ 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<Tensor>();
// 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()) {
result_shape.add_dim()->set_dim_value(dim);
}

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 {
Expand Down
Loading