Skip to content

Commit e0a4ed1

Browse files
authored
Use model hash from metadata if available (microsoft#25118)
### Description This change implements **Metadata-based hash override** optimization to the model hashing logic. Added logic to check for "model_hash" in model metadata before computing hashes. If present, both model_graph_hash and model_weight_hash are set to the metadata value, bypassing computation entirely. ### Motivation and Context ONNX models generated using the Olive toolchain now have the option to include the model hash as part of the ONNX metadata. For such models, it would be beneficial to use the provided hash instead of computing it from scratch.
1 parent bc95e99 commit e0a4ed1

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

onnxruntime/core/session/inference_session.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,8 +2082,20 @@ common::Status InferenceSession::Initialize() {
20822082
SetWeightDataType(model_weight_type);
20832083
#endif
20842084
#ifdef _WIN32
2085-
model_graph_hash = ComputeModelGraphHash(graph);
2086-
model_weight_hash = (model_graph_hash == "0") ? "0" : ComputeModelWeightHash(initializers);
2085+
// Check if model metadata contains a "model_hash" field
2086+
const auto& metadata = model_->MetaData();
2087+
auto model_hash_it = metadata.find("model_hash");
2088+
2089+
if (model_hash_it != metadata.end()) {
2090+
// Use the model_hash from metadata
2091+
model_graph_hash = model_hash_it->second;
2092+
model_weight_hash = model_hash_it->second;
2093+
} else {
2094+
// Compute hashes
2095+
model_graph_hash = ComputeModelGraphHash(graph);
2096+
model_weight_hash = (model_graph_hash == "0") ? "0" : ComputeModelWeightHash(initializers);
2097+
}
2098+
20872099
SetGraphHash(model_graph_hash);
20882100
SetWeightHash(model_weight_hash);
20892101
#endif

0 commit comments

Comments
 (0)