-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent self mapping in the AlmostExact graph #3926
Open
naoyam
wants to merge
12
commits into
main
Choose a base branch
from
restrict_almost_exact_mapping
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−20
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0a99dfd
Do not include merging of two broadcast IDs in trivially mapped IDs
naoyam ee0d73b
fix second attempt
naoyam 9f848c5
fix
naoyam 6b154a0
allow self mapping by default
naoyam 0d20d2c
Dont validate self mapping unless necessary
naoyam e0e55e9
fix
naoyam 339f23e
cleanup
naoyam 6967e6d
Merge branch 'main' into restrict_almost_exact_mapping
naoyam c8bea2f
cleanup
naoyam f2a5915
cleanup
naoyam 37ac5d4
comment
naoyam e1cfbd8
bug fix
naoyam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,20 +55,22 @@ void mapThroughLoopSwizzles(ValGraph& graph) { | |
|
||
} // namespace | ||
|
||
void IdModel::assertNoSelfMapping() { | ||
const ValGraph& exact_graph = idGraph(IdMappingMode::EXACT); | ||
void IdModel::assertNoSelfMapping(const ValGraph& graph) const { | ||
for (TensorView* tv : tvs_) { | ||
std::optional<SelfMapping> self_mapping = hasSelfMapping(tv, exact_graph); | ||
std::optional<SelfMapping> self_mapping = hasSelfMapping(tv, graph); | ||
if (self_mapping.has_value()) { | ||
tv->fusion()->print(); | ||
} | ||
NVF_CHECK( | ||
!self_mapping.has_value(), | ||
"Unsupported domain mapping detected in ", | ||
tv, | ||
tv->toString(), | ||
". ", | ||
self_mapping->where, | ||
" domains, ", | ||
self_mapping->id1, | ||
self_mapping->id1->toString(), | ||
" and ", | ||
self_mapping->id2, | ||
self_mapping->id2->toString(), | ||
", are mapped with each other."); | ||
} | ||
} | ||
|
@@ -413,6 +415,12 @@ ValGraph& IdModel::buildExactGraph() { | |
|
||
graph.validateConsistency(); | ||
|
||
// Make sure there's no self mapping in the Exact graph as that | ||
// would invalidate lowering assumptions. | ||
if (!allow_self_mapping_) { | ||
assertNoSelfMapping(graph); | ||
} | ||
|
||
if (isOptionEnabled(EnableOption::IdModelExtraValidation)) { | ||
checkStaticExtentGroups(graph); | ||
} | ||
|
@@ -495,6 +503,25 @@ ValGraph& IdModel::buildAlmostExactGraph() { | |
|
||
auto& almost_exact_graph = idGraph(IdMappingMode::ALMOSTEXACT); | ||
|
||
for (TensorView* tv : tvs_) { | ||
if (tv->hasRoot()) { | ||
for (auto id : tv->getRootDomain()) { | ||
almost_exact_graph.setUnmappable( | ||
{tv->getRootDomain().begin(), tv->getRootDomain().end()}); | ||
} | ||
} | ||
for (auto id : tv->getLogicalDomain()) { | ||
forbidden_pairs[id].insert( | ||
tv->getLogicalDomain().begin(), tv->getLogicalDomain().end()); | ||
} | ||
for (auto id : tv->getLoopDomain()) { | ||
forbidden_pairs[id].insert( | ||
tv->getLoopDomain().begin(), tv->getLoopDomain().end()); | ||
} | ||
} | ||
|
||
almost_exact_graph.do_not_map_vals_ = forbidden_pairs; | ||
|
||
// Maps iter domain pairs returned by calling that return mappings from | ||
// isTrivialExpr on every expression in the graph. | ||
|
||
|
@@ -514,7 +541,6 @@ ValGraph& IdModel::buildAlmostExactGraph() { | |
// Map through trivial expressions | ||
for (auto mapped_id_group : mapped_ids) { | ||
for (auto id : mapped_id_group) { | ||
// almost_exact_graph.mapVals(mapped_id_group.front(), id); | ||
ids_to_map.emplace_back(mapped_id_group.front(), id); | ||
} | ||
} | ||
|
@@ -527,6 +553,13 @@ ValGraph& IdModel::buildAlmostExactGraph() { | |
|
||
almost_exact_graph.validateConsistency(); | ||
|
||
// Even when EXACT has no self mapping, there was a case ALMOSTEXACT | ||
// had self mapping (see issue #3919). ALMOSTEXACT is used in | ||
// indexing, which assumes the graph has no self mapping. | ||
if (!allow_self_mapping_) { | ||
assertNoSelfMapping(almost_exact_graph); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this check for AlmostExact |
||
} | ||
|
||
if (isOptionEnabled(EnableOption::IdModelExtraValidation)) { | ||
checkStaticExtentGroups(almost_exact_graph); | ||
} | ||
|
@@ -851,15 +884,7 @@ void IdModel::buildAllGraphs() { | |
validator->checkExactGraphEquivalence(idGraph(IdMappingMode::EXACT)); | ||
} | ||
|
||
// Make sure there's no self mapping in the Exact graph as that | ||
// would invalidate lowering assumptions. | ||
if (!allow_self_mapping_) { | ||
assertNoSelfMapping(); | ||
} | ||
|
||
buildAlmostExactGraph(); | ||
// Skip validating the almost exact graph as the IdModel graph also | ||
// maps non-size-one broadcast domains | ||
|
||
buildPermissiveGraph(); | ||
// Validation is not implemented when compliment mapping is enabled | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,7 @@ class IdModel : public PolymorphicBase { | |
const std::vector<Expr*>& exprs, | ||
const std::vector<TensorView*>& additional_tvs = {}, | ||
bool build_graphs = false, | ||
bool allow_self_mapping = false, | ||
bool allow_self_mapping = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to not check self mapping by default since that restriction only matters for indexing. |
||
LoopPromotionMapBuilderCallback* loop_promotion_map_builder_callback = | ||
nullptr); | ||
|
||
|
@@ -129,7 +129,7 @@ class IdModel : public PolymorphicBase { | |
IdModel( | ||
Fusion* fusion, | ||
bool build_graphs = false, | ||
bool allow_self_mapping = false, | ||
bool allow_self_mapping = true, | ||
bool validate = false, | ||
LoopPromotionMapBuilderCallback* loop_promotion_map_builder_callback = | ||
nullptr); | ||
|
@@ -286,7 +286,7 @@ class IdModel : public PolymorphicBase { | |
const StatefulInliningInfo& info); | ||
|
||
// Errors if self mapping occurs | ||
void assertNoSelfMapping(); | ||
void assertNoSelfMapping(const ValGraph& graph) const; | ||
|
||
// Loop graph represents the loop structure of the given fusion, so | ||
// there must not be any mapping between the loop domains of each | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved this check to
buildExactGraph