You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/03/17 18:32:49 UTC
[GitHub] [tvm] AndrewZhaoLuo opened a new pull request #10663: [TIR] CSE-TIR Pass - More deterministic Behavior
AndrewZhaoLuo opened a new pull request #10663:
URL: https://github.com/apache/tvm/pull/10663
Running the CSE-TIR pass led to some variation in lowering to TIR from the same schedule multiple times. I tracked this down to iterating over an unordered map. I think having consistency in lowering to TIR is a desirable property to have so I made the offending code a little better in this regard.
Example test script:
https://github.com/AndrewZhaoLuo/TVM-Sandbox/blob/19284ddbd6bb28af61c0c2aa8bb334c5c53731a7/tir/test_inconsistent_tir_lowering.py#L1
^--- Running the above off main produces different hashes every time, running after this diff it produces the same hash.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829497080
##########
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:
Ah yes, thanks.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071269852
@AndrewZhaoLuo maybe we can also solve this by changing the comparison functor in
https://github.com/apache/tvm/blob/09f7be28a1ed7e629ddc0c9fcf19d4feace05dd4/src/tir/transforms/common_subexpr_elim_tools.h#L54
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071196927
cc some folks from https://github.com/apache/tvm/pull/9482: @masahi @FranckQC @mbs-octoml @jroesch
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071463950
Hmm, so the unordered map doesn't have an ordering so all it needs is some way of determining equality of elements.
An alternative would be `std::map` which does have a notion of ordering (and you can define a comparator). However, it's performance characteristics are different (usually self-balancing binary tree so O(log n) lookup instead of O(~1)).
In general though yeah, we need to be on the lookout for iterating over unordered collections cause it will be non-determinstic. I'll try refactoring this sometime later, though need to read more about performance (it's already kind of slow when processing a statement with 100 common subexpressions).
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi merged pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi merged pull request #10663:
URL: https://github.com/apache/tvm/pull/10663
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] FranckQC commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
FranckQC commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1073440581
Hi,
Sorry I couldn't discuss this with you last Thursday/Friday. Thanks for improving the CSE!
Yes, indeed, iterating through the hash table will not necessary lead to accessing its elements always in the same order. That's because the iteration order depends on the hashes (and we use `structural_hash` for that), which themselves are based on pointers, so different run will give different addresses, which will give different hashes, and thus a different order in which elements are accessed.
I guess it is not an issue as far as the CSE is concerned, because after producing this hashtable, we use it to produce a vector of semantic entities (which are pairs <expr, count>).
The main loop of the CSE will later iterate through the elements of this vector.
But before that, this vector gets sorted by the size (called "complexity") of the expression (the first component).
So the order of the end result (the vector) is non-deterministic only for elements with the same size / complexity, for which it does not matter (again, only for the CSE) in which order we common them. (i.e. it should not add or remove opportunities for commoning more later). Said differently, this non-determinism should appear for orthogonal choices.
I guess as in:
```
Mem[i1] = (a+b)+ (c+d);
Mem[i2] = a+b; // We can common this one before or after
Mem[i3] = c+d; // this one, it does not matter for the CSE
Mem[i4] = (a+b) + (c+d) + e;
```
for which we could produce:
```
let cse_var_2 = a+b in
let cse_var_3 = c+d in
let cse_var_1 = cse_var_2 + cse_var_3 in
Mem[i1] = cse_var_1;
Mem[i2] = cse_var_2 in
Mem[i3] = cse_var_3 in
Mem[i4] = cse_var_1
```
Or the alternative version were (c+d) gets introduced before (a+b).
I did not think that having this level of non-determinism would be an issue, but you're clearly right that its better to always produce exactly the same resulting TIR code, that just makes testing easier.
Your way of making it deterministic (by first transitioning the hashtable into an array, which you then sort using the actual syntax that the expressions represent) is perfectly fine to me.
According to the doc about `structural_hash` ( here : https://tvm.apache.org/docs/reference/api/python/ir.html ), it seems an alternative could have been to set `map_free_vars` to true, as that's the reason the hashes use pointers. If that was the only place were addresses are used in the hashes, it should make everything deterministic too. I'll see if that works too, out of curiosity.
Many thanks for having spotted that and for having fixed it!
Kind regards,
Franck
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] FranckQC edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
FranckQC edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1073440581
Hi,
Sorry I couldn't discuss this with you last Thursday/Friday. Thanks for improving the CSE!
Yes, indeed, iterating through the hash table will not necessary lead to accessing its elements always in the same order. That's because the iteration order depends on the hashes (and we use `structural_hash` for that), which themselves are based on pointers, so different run will give different addresses, which will give different hashes, and thus a different order in which elements are accessed.
I guess it is not an issue as far as the CSE is concerned, because after producing this hashtable, we use it to produce a vector of semantic entities (which are pairs <expr, count>).
The main loop of the CSE will later iterate through the elements of this vector.
But before that, this vector gets sorted by the size (called "complexity") of the expression (the first component).
So the order of the end result (the vector) is non-deterministic only for elements with the same size / complexity, for which it does not matter (again, only for the CSE) in which order we common them. (i.e. it should not add or remove opportunities for commoning more later). Said differently, this non-determinism should appear for orthogonal choices.
I guess as in:
```
Mem[i1] = (a+b)+ (c+d);
Mem[i2] = a+b; // We can common this one before or after
Mem[i3] = c+d; // this one, it does not matter for the CSE
Mem[i4] = (a+b) + (c+d) + e;
```
for which we could produce:
```
let cse_var_2 = a+b in
let cse_var_3 = c+d in
let cse_var_1 = cse_var_2 + cse_var_3 in
Mem[i1] = cse_var_1;
Mem[i2] = cse_var_2;
Mem[i3] = cse_var_3;
Mem[i4] = cse_var_1 + e;
```
Or the alternative version were (c+d) gets introduced before (a+b). **In practice, what is chosen currently depends on the addresses of the free variables 'a', 'b', 'c' and 'd' because they are used for the hashes**.
I did not think that having this level of non-determinism would be an issue, but you're clearly right that its better to always produce exactly the same resulting TIR code, that just makes testing easier. Sorry I did not think about it when I wrote it!
Your way of making it deterministic (by first copying the hashtable into an array, which you then sort using the actual syntax that the expressions represent) is perfectly fine to me.
According to the doc about `structural_hash` ( here : https://tvm.apache.org/docs/reference/api/python/ir.html ), it seems an alternative could have been to set its `map_free_vars` parameter to true, as that's the reason the hashes use pointers. If that was the only place were addresses are used in the hashes, it should make everything deterministic too. I'll see if that works too, out of curiosity.
Many thanks for having spotted that and for having fixed it!
Kind regards,
Franck
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] FranckQC edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
FranckQC edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1073440581
Hi,
Sorry I couldn't discuss this with you last Thursday/Friday. Thanks for improving the CSE!
Yes, indeed, iterating through the hash table will not necessary lead to accessing its elements always in the same order. That's because the iteration order depends on the hashes (and we use `structural_hash` for that), which themselves are based on pointers, so different run will give different addresses, which will give different hashes, and thus a different order in which elements are accessed.
I guess it is not an issue as far as the CSE is concerned, because after producing this hashtable, we use it to produce a vector of semantic entities (which are pairs <expr, count>).
The main loop of the CSE will later iterate through the elements of this vector.
But before that, this vector gets sorted by the size (called "complexity") of the expression (the first component).
So the order of the end result (the vector) is non-deterministic only for elements with the same size / complexity, for which it does not matter (again, only for the CSE) in which order we common them. (i.e. it should not add or remove opportunities for commoning more later). Said differently, this non-determinism should appear for orthogonal choices.
I guess as in:
```
Mem[i1] = (a+b)+ (c+d);
Mem[i2] = a+b; // We can common this one before or after
Mem[i3] = c+d; // this one, it does not matter for the CSE
Mem[i4] = (a+b) + (c+d) + e;
```
for which we could produce:
```
let cse_var_2 = a+b in
let cse_var_3 = c+d in
let cse_var_1 = cse_var_2 + cse_var_3 in
Mem[i1] = cse_var_1;
Mem[i2] = cse_var_2;
Mem[i3] = cse_var_3;
Mem[i4] = cse_var_1 + e;
```
Or the alternative version were (c+d) gets introduced before (a+b).
I did not think that having this level of non-determinism would be an issue, but you're clearly right that its better to always produce exactly the same resulting TIR code, that just makes testing easier.
Your way of making it deterministic (by first transitioning the hashtable into an array, which you then sort using the actual syntax that the expressions represent) is perfectly fine to me.
According to the doc about `structural_hash` ( here : https://tvm.apache.org/docs/reference/api/python/ir.html ), it seems an alternative could have been to set `map_free_vars` to true, as that's the reason the hashes use pointers. If that was the only place were addresses are used in the hashes, it should make everything deterministic too. I'll see if that works too, out of curiosity.
Many thanks for having spotted that and for having fixed it!
Kind regards,
Franck
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071269852
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071463950
Hmm, so the unordered map doesn't have an ordering so all it needs is some way of determining equality of elements.
An alternative would be `std::map` which does have a notion of ordering (and you can define a comparator). However, it's performance characteristics are different (usually self-balancing binary tree so O(log n) lookup instead of O(~1)).
In general though yeah, we need to be on the lookout for iterating over unordered collections cause it will be non-determinstic so might be good for maintainability to use std::map
I'll try refactoring `unordered_map --> map` sometime later, though need to read more about performance (it's already kind of slow when processing a statement with 100 common subexpressions).
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829426346
##########
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:
Done
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829398822
##########
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.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829430684
##########
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071556287
> Hmm, so the unordered map doesn't have an ordering
The actual order doesn't matter, does it? `unordered_map` doesn't store elements in a sorted order, but it does have some ordering internally determined by the comparison functor. And what we need is just a fixed order, not necessarily the sorted order, so I think `unordered map` should do the job.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829426346
##########
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:
Done
##########
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:
Ah yes, thanks.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi merged pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi merged pull request #10663:
URL: https://github.com/apache/tvm/pull/10663
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] FranckQC edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
FranckQC edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1073440581
Hi,
Sorry I couldn't discuss this with you last Thursday/Friday. Thanks for improving the CSE!
Yes, indeed, iterating through the hash table will not necessary lead to accessing its elements always in the same order. That's because the iteration order depends on the hashes (and we use `structural_hash` for that), which themselves are based on pointers, so different run will give different addresses, which will give different hashes, and thus a different order in which elements are accessed.
I guess it is not an issue as far as the CSE is concerned, because after producing this hashtable, we use it to produce a vector of semantic entities (which are pairs <expr, count>).
The main loop of the CSE will later iterate through the elements of this vector.
But before that, this vector gets sorted by the size (called "complexity") of the expression (the first component).
So the order of the end result (the vector) is non-deterministic only for elements with the same size / complexity, for which it does not matter (again, only for the CSE) in which order we common them. (i.e. it should not add or remove opportunities for commoning more later). Said differently, this non-determinism should appear for orthogonal choices.
I guess as in:
```
Mem[i1] = (a+b)+ (c+d);
Mem[i2] = a+b; // We can common this one before or after
Mem[i3] = c+d; // this one, it does not matter for the CSE
Mem[i4] = (a+b) + (c+d) + e;
```
for which we could produce:
```
let cse_var_2 = a+b in
let cse_var_3 = c+d in
let cse_var_1 = cse_var_2 + cse_var_3 in
Mem[i1] = cse_var_1;
Mem[i2] = cse_var_2;
Mem[i3] = cse_var_3;
Mem[i4] = cse_var_1 + e;
```
Or the alternative version were (c+d) gets introduced before (a+b). **In practice, what is chosen currently depends on the addresses of the free variables 'a', 'b', 'c' and 'd' because they are used for the hashes**.
I did not think that having this level of non-determinism would be an issue, but you're clearly right that its better to always produce exactly the same resulting TIR code, that just makes testing easier.
Your way of making it deterministic (by first transitioning the hashtable into an array, which you then sort using the actual syntax that the expressions represent) is perfectly fine to me.
According to the doc about `structural_hash` ( here : https://tvm.apache.org/docs/reference/api/python/ir.html ), it seems an alternative could have been to set `map_free_vars` to true, as that's the reason the hashes use pointers. If that was the only place were addresses are used in the hashes, it should make everything deterministic too. I'll see if that works too, out of curiosity.
Many thanks for having spotted that and for having fixed it!
Kind regards,
Franck
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071594457
ah sorry, I was confused with something, "but it does have some ordering internally determined by the comparison functor" is not correct.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071196927
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
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).
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071463950
Hmm, so the unordered map doesn't have an ordering so all it needs is some way of determining equality of elements.
An alternative would be `std::map` which does have a notion of ordering (and you can define a comparator). However, it's performance characteristics are different (usually self-balancing binary tree so O(log n) lookup instead of O(~1)).
In general though yeah, we need to be on the lookout for iterating over unordered collections cause it will be non-determinstic.
I'll try refactoring `unordered_map --> map` sometime later, though need to read more about performance (it's already kind of slow when processing a statement with 100 common subexpressions).
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071599662
@AndrewZhaoLuo I think we can merge this as is for now. The CI failed on the mac os, probably need to kick another job.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] AndrewZhaoLuo edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1071463950
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] FranckQC edited a comment on pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
FranckQC edited a comment on pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#issuecomment-1073440581
Hi,
Sorry I couldn't discuss this with you last Thursday/Friday. Thanks for improving the CSE!
Yes, indeed, iterating through the hash table will not necessary lead to accessing its elements always in the same order. That's because the iteration order depends on the hashes (and we use `structural_hash` for that), which themselves are based on pointers, so different run will give different addresses, which will give different hashes, and thus a different order in which elements are accessed.
I guess it is not an issue as far as the CSE is concerned, because after producing this hashtable, we use it to produce a vector of semantic entities (which are pairs <expr, count>).
The main loop of the CSE will later iterate through the elements of this vector.
But before that, this vector gets sorted by the size (called "complexity") of the expression (the first component).
So the order of the end result (the vector) is non-deterministic only for elements with the same size / complexity, for which it does not matter (again, only for the CSE) in which order we common them. (i.e. it should not add or remove opportunities for commoning more later). Said differently, this non-determinism should appear for orthogonal choices.
I guess as in:
```
Mem[i1] = (a+b)+ (c+d);
Mem[i2] = a+b; // We can common this one before or after
Mem[i3] = c+d; // this one, it does not matter for the CSE
Mem[i4] = (a+b) + (c+d) + e;
```
for which we could produce:
```
let cse_var_2 = a+b in
let cse_var_3 = c+d in
let cse_var_1 = cse_var_2 + cse_var_3 in
Mem[i1] = cse_var_1;
Mem[i2] = cse_var_2;
Mem[i3] = cse_var_3;
Mem[i4] = cse_var_1 + e;
```
Or the alternative version were (c+d) gets introduced before (a+b). **In practice, what is chosen currently depends on the addresses of the free variables 'a', 'b', 'c' and 'd' because they are used for the hashes**.
I did not think that having this level of non-determinism would be an issue, but you're clearly right that its better to always produce exactly the same resulting TIR code, that just makes testing easier. Sorry I did not think about it when I wrote it!
Your way of making it deterministic (by first copying the hashtable into an array, which you then sort using the actual syntax that the expressions represent) is perfectly fine to me.
According to the doc about `structural_hash` ( here : https://tvm.apache.org/docs/reference/api/python/ir.html ), it seems an alternative could have been to set `map_free_vars` to true, as that's the reason the hashes use pointers. If that was the only place were addresses are used in the hashes, it should make everything deterministic too. I'll see if that works too, out of curiosity.
Many thanks for having spotted that and for having fixed it!
Kind regards,
Franck
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [tvm] masahi commented on a change in pull request #10663: [TIR] CSE-TIR Pass - More deterministic behavior
Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829398822
##########
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.
--
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: commits-unsubscribe@tvm.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org