masahi commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829395007
##########
File path: src/tir/transforms/common_subexpr_elim_tools.cc
##########
@@ -748,8 +748,26 @@ std::vector<std::pair<PrimExpr, size_t>>
SyntacticToSemanticComputations(
// We do this reservation even if it might reserve slightly more space than
is needed in the end
result.reserve(table.size());
- // For each element in the hashtable
+ // Traverse through keys in a sorted order to maintain deterministic behavior
+ // We do this by comparing the string repr of each PrimExpr to get a
determinstic ordering
+ std::vector<PrimExpr> table_keys;
+ table_keys.reserve(table.size());
for (auto elem : table) {
+ table_keys.push_back(elem.first);
+ }
+ sort(table_keys.begin(), table_keys.end(), [](PrimExpr a, PrimExpr b) {
+ std::stringstream a_stream;
+ std::stringstream b_stream;
+ a_stream << a;
+ b_stream << b;
+ return a_stream.str().compare(b_stream.str()) < 0;
+ });
+
+ // For each element in the hashtable
+ for (PrimExpr key : table_keys) {
+ size_t value = table.find(key)->second;
Review comment:
I think it is better to sort and put the sorted key + count pair into a
vector before calling `SyntacticToSemanticComputations`. That way you can
remove this `table.find(key)` and `SyntacticToSemanticComputations` becomes
`vector -> vector` transform (which is cleaner imo).
##########
File path: src/tir/transforms/common_subexpr_elim_tools.cc
##########
@@ -748,8 +748,26 @@ std::vector<std::pair<PrimExpr, size_t>>
SyntacticToSemanticComputations(
// We do this reservation even if it might reserve slightly more space than
is needed in the end
result.reserve(table.size());
- // For each element in the hashtable
+ // Traverse through keys in a sorted order to maintain deterministic behavior
+ // We do this by comparing the string repr of each PrimExpr to get a
determinstic ordering
+ std::vector<PrimExpr> table_keys;
+ table_keys.reserve(table.size());
for (auto elem : table) {
+ table_keys.push_back(elem.first);
+ }
+ sort(table_keys.begin(), table_keys.end(), [](PrimExpr a, PrimExpr b) {
+ std::stringstream a_stream;
+ std::stringstream b_stream;
+ a_stream << a;
+ b_stream << b;
+ return a_stream.str().compare(b_stream.str()) < 0;
+ });
+
+ // For each element in the hashtable
+ for (PrimExpr key : table_keys) {
+ size_t value = table.find(key)->second;
Review comment:
Actually, if you have a sorted `std::vector<std::pair<PrimExpr,
size_t>>` beforehand, this transform can be done in a one pass, rather than the
current O(N^2) implementation. Just loop through each input pair and count
until `EquivalentTerms` returns false etc.
##########
File path: src/tir/transforms/common_subexpr_elim_tools.cc
##########
@@ -748,8 +748,26 @@ std::vector<std::pair<PrimExpr, size_t>>
SyntacticToSemanticComputations(
// We do this reservation even if it might reserve slightly more space than
is needed in the end
result.reserve(table.size());
- // For each element in the hashtable
+ // Traverse through keys in a sorted order to maintain deterministic behavior
+ // We do this by comparing the string repr of each PrimExpr to get a
determinstic ordering
+ std::vector<PrimExpr> table_keys;
+ table_keys.reserve(table.size());
for (auto elem : table) {
+ table_keys.push_back(elem.first);
+ }
+ sort(table_keys.begin(), table_keys.end(), [](PrimExpr a, PrimExpr b) {
+ std::stringstream a_stream;
+ std::stringstream b_stream;
+ a_stream << a;
+ b_stream << b;
+ return a_stream.str().compare(b_stream.str()) < 0;
+ });
+
+ // For each element in the hashtable
+ for (PrimExpr key : table_keys) {
+ size_t value = table.find(key)->second;
Review comment:
~Actually, if you have a sorted `std::vector<std::pair<PrimExpr,
size_t>>` beforehand, this transform can be done in a one pass, rather than the
current O(N^2) implementation. Just loop through each input pair and count
until `EquivalentTerms` returns false etc.~
Sorry above comment is probably false since the term equivalence is not
necessarily based on the same value used for sort keys.
##########
File path: src/tir/transforms/common_subexpr_elim_tools.cc
##########
@@ -743,13 +743,30 @@ bool EquivalentTerms(const PrimExpr& a, const PrimExpr&
b) {
std::vector<std::pair<PrimExpr, size_t>> SyntacticToSemanticComputations(
const ComputationTable& table) {
std::vector<std::pair<PrimExpr, size_t>> result;
+
// table.size() is an upper-bound of the number of elements in the resulting
vector,
// as we might merge semantically equivalent computations.
// We do this reservation even if it might reserve slightly more space than
is needed in the end
result.reserve(table.size());
- // For each element in the hashtable
+ // Traverse through map in a sorted order on keys to maintain deterministic
behavior
+ // We do this by comparing the string repr of each PrimExpr to get a
determinstic ordering
+ std::vector<std::pair<PrimExpr, size_t>> sorted_map_items;
+ sorted_map_items.reserve(table.size());
for (auto elem : table) {
+ sorted_map_items.push_back(elem);
+ }
Review comment:
I think you can initialize directly by `sorted_map_items(table.begin(),
table.end())`. Can you try it?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]