forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 7
Move producer-consumer mapping functions to TensorDomain #233
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
873f4ad
Start extracting some TensorDomain utility functions.
csarofeen 1b6fee9
Move producer-consumer mapping functions to TensorDomain.
csarofeen e0ef5a6
Minor cleanup.
csarofeen 980dcc1
Clang-tidy.
csarofeen c56d321
Add a couple const& to interface.
csarofeen 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -849,6 +849,127 @@ bool TensorDomain::hasReduction(const std::vector<IterDomain*>& td) { | |
return false; | ||
} | ||
|
||
// return mapping of consumer_domain[i] = producer_domain[result_vector[i]] | ||
// assuming there exists a direct consumer-producer mapping. If axis exists in | ||
// consumer (broadcast) but not in producer, mapping will be result_vector[i] = | ||
// -1. | ||
std::vector<int64_t> TensorDomain::mapDomainCtoP( | ||
const std::vector<IterDomain*>& consumer, | ||
const std::vector<IterDomain*>& producer) { | ||
std::vector<int64_t> consumer_to_producer(consumer.size(), -1); | ||
|
||
size_t itc = 0, itp = 0; | ||
while (itc < consumer.size() && itp < producer.size()) { | ||
if (consumer[itc]->isBroadcast() && !producer[itp]->isBroadcast()) { | ||
itc++; | ||
continue; | ||
} | ||
if (producer[itp]->isReduction()) { | ||
itp++; | ||
continue; | ||
} | ||
|
||
consumer_to_producer[itc] = itp; | ||
itc++; | ||
itp++; | ||
} | ||
return consumer_to_producer; | ||
} | ||
|
||
// Create a map from consumer root IterDomains -> producer root IterDomains. | ||
// Constrain will restrict which consumer root IterDomains we map to the | ||
// producer IterDomains. Only those root consumer IDs present in | ||
// consumer_root_dims_to_map will be attempted to map to their corresponding | ||
// producer IDs. | ||
std::unordered_map<IterDomain*, IterDomain*> TensorDomain::mapRootCtoP( | ||
const TensorDomain* consumer, | ||
const TensorDomain* producer, | ||
bool constrain, | ||
std::unordered_set<IterDomain*> consumer_root_dims_to_map) { | ||
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. Make it |
||
auto consumer_root = consumer->rootDomain(); | ||
auto producer_root = producer->hasRFactor() ? producer->rfactorDomain() | ||
: producer->rootDomain(); | ||
|
||
auto c_to_p = mapDomainCtoP(consumer_root, producer_root); | ||
|
||
std::unordered_map<IterDomain*, IterDomain*> root_id_map; | ||
|
||
for (int64_t itc = 0; itc < (int64_t)c_to_p.size(); itc++) { | ||
int64_t itp = c_to_p[itc]; | ||
if (itp == -1) | ||
continue; | ||
|
||
if (!constrain || | ||
(constrain && | ||
consumer_root_dims_to_map.find(consumer_root[itc]) != | ||
consumer_root_dims_to_map.end())) { | ||
root_id_map[consumer_root[itc]] = producer_root[itp]; | ||
} | ||
} | ||
return root_id_map; | ||
} | ||
|
||
// return mapping of consumer_domain[i] = producer_domain[result_vector[i]] | ||
// assuming there exists a direct consumer-producer mapping. If axis exists in | ||
// consumer (broadcast) but not in producer, mapping will be result_vector[i] = | ||
// -1. | ||
std::vector<int64_t> TensorDomain::mapDomainPtoC( | ||
const std::vector<IterDomain*>& producer, | ||
const std::vector<IterDomain*>& consumer) { | ||
std::vector<int64_t> producer_to_consumer(producer.size(), -1); | ||
|
||
size_t itc = 0, itp = 0; | ||
while (itc < consumer.size() && itp < producer.size()) { | ||
if (consumer[itc]->isBroadcast() && !producer[itp]->isBroadcast()) { | ||
itc++; | ||
continue; | ||
} | ||
if (producer[itp]->isReduction()) { | ||
itp++; | ||
continue; | ||
} | ||
|
||
producer_to_consumer[itp] = itc; | ||
itc++; | ||
itp++; | ||
} | ||
|
||
return producer_to_consumer; | ||
} | ||
|
||
// Create a map from producer root IterDomains -> consumer root IterDomains. | ||
// Constrain will restrict which producer root IterDomains we map to the | ||
// consumer IterDomains. Only those root producer IDs present in | ||
// producer_root_dims_to_map will be attempted to map to their corresponding | ||
// consumer IDs. | ||
std::unordered_map<IterDomain*, IterDomain*> TensorDomain::mapRootPtoC( | ||
const TensorDomain* producer, | ||
const TensorDomain* consumer, | ||
bool constrain, | ||
std::unordered_set<IterDomain*> producer_root_dims_to_map) { | ||
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. Make it |
||
auto consumer_root = consumer->rootDomain(); | ||
auto producer_root = producer->hasRFactor() ? producer->rfactorDomain() | ||
: producer->rootDomain(); | ||
|
||
auto p_to_c = mapDomainPtoC(producer_root, consumer_root); | ||
|
||
std::unordered_map<IterDomain*, IterDomain*> root_id_map; | ||
|
||
for (int64_t itp = 0; itp < (int64_t)p_to_c.size(); itp++) { | ||
int64_t itc = p_to_c[itp]; | ||
if (itc == -1) | ||
continue; | ||
|
||
if (!constrain || | ||
(constrain && | ||
producer_root_dims_to_map.find(producer_root[itp]) != | ||
producer_root_dims_to_map.end())) { | ||
root_id_map[producer_root[itp]] = consumer_root[itc]; | ||
} | ||
} | ||
return root_id_map; | ||
} | ||
|
||
// pair is in order where second is the consumer of first | ||
std::pair<TensorDomain*, TensorDomain*> TensorDomain::rFactor( | ||
const std::vector<int>& axes_) { | ||
|
This file contains hidden or 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
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.
This seems very similar to
mapDomainPtoC
. I have some refactoring idea. Will send a PR once this is merged.