From fd3b7acb794aa453f1d86c24852e22e996d8e21b Mon Sep 17 00:00:00 2001 From: Jared Smolens Date: Wed, 1 Dec 2021 10:11:08 -0800 Subject: Sort tensor list by name for serialization - Tensors are stored in an unordered_map from mlir::Value::hash_value to tensor name. - Before serialization, sort the tensor list alphabetically by name so that tensors always appear in a determinstic (and logical) order. Signed-off-by: Jared Smolens Change-Id: I21c0e2ec04b7520e6a753b8bf62572a702002d5b --- src/TosaSerialize.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src/TosaSerialize.cpp') diff --git a/src/TosaSerialize.cpp b/src/TosaSerialize.cpp index 5699ffe..0557520 100644 --- a/src/TosaSerialize.cpp +++ b/src/TosaSerialize.cpp @@ -24,6 +24,7 @@ #include "tosa_serialization_handler.h" #include #include +#include // The namespace might be confusing here. We have mlir::tosa:: defined in MLIR // and tosa:: defined in serialization library @@ -1510,9 +1511,17 @@ mlir::LogicalResult TosaSerializationBlockBuilder::BuildAllOpsInRegion( } // Build tensor - for (auto pair : tensor_map) { - ser_tensor = BuildTosaSerializationTensor(pair.first /* val */, - pair.second /* name */); + + // The tensor_map is sorted by hashed mlir::Value types. + // For serialization, sort tensors alphabetically by name for a deterministic + // and human-friendly ordering. + std::map tensor_name_sort; + for (auto pair : tensor_map) + tensor_name_sort[pair.second] = pair.first; + + for (auto pair : tensor_name_sort) { + ser_tensor = BuildTosaSerializationTensor(pair.second /* val */, + pair.first /* name */); if (!ser_tensor) { llvm::errs() << "ERROR: Failed to build TosaSerializationTensor\n"; return mlir::failure(); -- cgit v1.2.1