Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e4c8244
Adjust InlineIfNode
yuslepukhin Jul 2, 2025
5dc217e
Merge branch 'main' into yusleoukhin/ort_initializers_ii
yuslepukhin Jul 8, 2025
ffb9bbe
Fix a bug in Initializer::ToProtoWithOrtValue
yuslepukhin Jul 8, 2025
e19b938
Address type conversion
yuslepukhin Jul 8, 2025
564c29f
Adjust ToProto() handling of in external data in memory
yuslepukhin Jul 9, 2025
507eb56
Make CoreML accept external initializers
yuslepukhin Jul 9, 2025
5e89498
Fix handling of external data in ToProto*()
yuslepukhin Jul 10, 2025
fdcdc12
Fix compiler error
yuslepukhin Jul 10, 2025
6e29d73
Merge branch 'yuslepukhin/ort_initializers_mac' into yusleoukhin/ort_…
yuslepukhin Jul 10, 2025
a0635b7
Address compile error in Mac code
yuslepukhin Jul 10, 2025
e81b06c
Merge branch 'main' into yusleoukhin/ort_initializers_ii
yuslepukhin Jul 10, 2025
4cbddc9
Adjust in memory references when saving optimized model
yuslepukhin Jul 12, 2025
6ab9839
Merge branch 'main' into yusleoukhin/ort_initializers_ii
yuslepukhin Jul 14, 2025
582f27c
Address ToGraphProto() issues
yuslepukhin Jul 15, 2025
a636579
Fix ToGraphProto() and recreate test databases for test_embedlayer_fu…
yuslepukhin Jul 16, 2025
83c313f
GCC not happy about attr placement
yuslepukhin Jul 16, 2025
2c0f97e
Address build error
yuslepukhin Jul 16, 2025
5c15725
Address compiler error
yuslepukhin Jul 16, 2025
a6b2576
Adjust test data for fastgelu fusion
yuslepukhin Jul 17, 2025
f59c9c6
Address review comments
yuslepukhin Jul 21, 2025
64de315
Add comment and resolve compiler error
yuslepukhin Jul 21, 2025
397fd13
Address review comments
yuslepukhin Jul 22, 2025
93d5614
Address review comments
yuslepukhin Jul 23, 2025
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
18 changes: 17 additions & 1 deletion include/onnxruntime/core/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,16 @@ class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve exi
#endif

#if !defined(ORT_MINIMAL_BUILD)
/** Gets the GraphProto representation of this Graph. */
/** Gets the GraphProto representation of this Graph only. */
const ONNX_NAMESPACE::GraphProto& ToGraphProto();

/// <summary>
// This function recurses subgraphs and examines each initializer
// If initializer data points to in-memory location, it is inlined
// otherwise, the initializer is copied as is including any
// external data references.
/// </summary>
/// <returns>GraphProto</returns>
ONNX_NAMESPACE::GraphProto ToGraphProto() const;

/** Gets the GraphProto representation of this Graph
Expand Down Expand Up @@ -1561,6 +1569,14 @@ class Graph { // NOLINT(clang-analyzer-optin.performance.Padding): preserve exi
Status AddConstantProtoAsInitializer(const ONNX_NAMESPACE::NodeProto& constant_node_proto,
std::optional<std::string_view> new_name);

/// <summary>
/// Subgraph initializers are already copied using Node::ToProto()
/// </summary>
/// <param name="output_graph_proto"></param>
/// <param name="process_main">process main graph if true</param>
/// <returns>Status</returns>
Status ProcessSubgraphsInMemoryData(ONNX_NAMESPACE::GraphProto& output_graph_proto, bool process_main) const;

/// <summary>
/// This function traverses the graph bottom up and externalizes
/// constant initializers along with their pre-packed blobs from different
Expand Down
31 changes: 26 additions & 5 deletions onnxruntime/core/framework/tensorprotoutils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ DEFINE_INT4_UNPACK_TENSOR_WITH_RAW_DATA_IMPL(UInt4x2)
Status ReadExternalDataForTensor(const ONNX_NAMESPACE::TensorProto& tensor_proto,
const std::filesystem::path& tensor_proto_dir,
std::vector<uint8_t>& unpacked_tensor) {
ORT_RETURN_IF(utils::HasString(tensor_proto), "This function does not support string data");
PathString external_file_path;
onnxruntime::FileOffsetType file_offset;
SafeInt<size_t> tensor_byte_size;
Expand Down Expand Up @@ -264,12 +265,32 @@ Status TensorProtoWithExternalDataToTensorProto(
result.set_data_type(ten_proto.data_type());
result.mutable_dims()->CopyFrom(ten_proto.dims());

// Load the external data into memory
std::vector<uint8_t> unpacked_data;
ORT_RETURN_IF_ERROR(ReadExternalDataForTensor(ten_proto, model_path, unpacked_data));
// Strings can only be in memory
if (utils::HasString(ten_proto)) {
ORT_RETURN_IF_NOT(HasExternalDataInMemory(ten_proto),
"TensorProto with string data can only be in memory");

std::unique_ptr<onnxruntime::ExternalDataInfo> external_data_info;
ORT_RETURN_IF_ERROR(onnxruntime::ExternalDataInfo::Create(ten_proto.external_data(), external_data_info));

// file_offset is address
if (utils::HasString(ten_proto)) {
auto tensor_shape = utils::GetTensorShapeFromTensorProto(ten_proto);
std::string* data = reinterpret_cast<std::string*>(external_data_info->GetOffset());
for (size_t i = 0, lim = narrow<size_t>(tensor_shape.Size()); i < lim; ++i) {
// set in raw data
result.add_string_data(*data);
++data;
}
}
} else {
// Load the external data into memory
std::vector<uint8_t> unpacked_data;
ORT_RETURN_IF_ERROR(ReadExternalDataForTensor(ten_proto, model_path, unpacked_data));

// Set the raw data in the new tensor
result.set_raw_data(unpacked_data.data(), unpacked_data.size());
// Set the raw data in the new tensor
result.set_raw_data(unpacked_data.data(), unpacked_data.size());
}

new_tensor_proto = std::move(result);

Expand Down
2 changes: 1 addition & 1 deletion onnxruntime/core/framework/tensorprotoutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ inline bool HasName(const ONNX_NAMESPACE::TypeProto_Opaque& op_proto) {
/// </summary>
/// <param name="tensor_proto">tensor_proto</param>
/// <returns>true if ten_proto has external data and it is in memory</returns>
bool HasExternalDataInMemory(const ONNX_NAMESPACE::TensorProto& tensor_proto);
[[nodiscard]] bool HasExternalDataInMemory(const ONNX_NAMESPACE::TensorProto& tensor_proto);

/// <summary>
/// This function converts TensorProto with external data to TensorProto with inline data.
Expand Down
Loading
Loading