-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[CINN] Add ScheduleBlock graph #56122
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
zhhsplendid
merged 9 commits into
PaddlePaddle:develop
from
BiynXu:add_schedule_block_graph
Aug 16, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d01ff7d
[CINN] Add ScheduleBlock graph
BiynXu b7916fd
Merge branch 'develop' into add_schedule_block_graph
BiynXu c46138e
[fix] cmake error
BiynXu 1201f3c
[fix] include error
BiynXu 7dde157
Add some annotation
BiynXu b426a72
[Fix] x86 unittest
BiynXu 3788616
[CINN] Create a constant for reduce init suffix
BiynXu 2898961
[CINN] rename DfsTopoWalker
BiynXu ca336c8
[CINN] Add some annotation to DfsTopoWalker
BiynXu 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 |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <functional> | ||
| #include <stack> | ||
| #include <unordered_set> | ||
|
|
||
| namespace cinn { | ||
| namespace common { | ||
|
|
||
| // DFS Topological order walker. | ||
| // Try to walk in a depth first manner while ensuring topological order. | ||
| // For example: | ||
| // Graph: | ||
| // 0 -> 1 | ||
| // 2 -> 3 | ||
| // 0 -> 3 | ||
| // 1 -> 3 | ||
| // 3 -> 4 | ||
| // Start nodes: 0, 2 | ||
| // Walking order: 0 -> 1 -> 2 -> 3 -> 4 | ||
| template <typename NodeType, | ||
| typename NodeHash = std::hash<NodeType>, | ||
| typename NodeEqual = std::equal_to<NodeType>> | ||
| class DfsTopoWalker final { | ||
| public: | ||
| DfsTopoWalker(const DfsTopoWalker&) = delete; | ||
| DfsTopoWalker(DfsTopoWalker&&) = delete; | ||
|
|
||
| using NodeHandlerType = std::function<void(NodeType)>; | ||
| using NodesVisitorType = | ||
| std::function<void(NodeType, const NodeHandlerType&)>; | ||
|
|
||
| DfsTopoWalker(const NodesVisitorType& VisitPreNodes, | ||
| const NodesVisitorType& VisitNextNodes) | ||
| : VisitPreNodes_(VisitPreNodes), VisitNextNodes_(VisitNextNodes) {} | ||
|
|
||
| // Start walking from 1 node and make every effort to access all nodes that | ||
| // meet the walking rules. | ||
| // If there are more than 1 nodes with a degree of 0 in a graph, | ||
| // only one part will be accessed. | ||
| // If you want to access the entire graph, | ||
| // you need to provide all starting nodes. | ||
| void operator()(NodeType node, const NodeHandlerType& NodeHandler) const { | ||
| std::array<NodeType, 1> nodes{node}; | ||
| (*this)(nodes.begin(), nodes.end(), NodeHandler); | ||
| } | ||
|
|
||
| // Start walking from a collection of node and make every effort to access all | ||
| // nodes that meet the walking rules. | ||
| // If there are other start nodes in a graph, | ||
| // some nodes on the graph will not be accessed. | ||
| // If you want to access the entire graph, | ||
| // you need to provide all starting nodes. | ||
| template <typename NodeIt> | ||
| void operator()(NodeIt begin, | ||
| NodeIt end, | ||
| const NodeHandlerType& NodeHandler) const { | ||
| std::stack<NodeType> node_stack; | ||
| std::unordered_set<NodeType, NodeHash, NodeEqual> visited; | ||
| std::unordered_map<NodeType, int, NodeHash, NodeEqual> in_degree; | ||
| const auto& InitInDegree = [&](NodeType node) { | ||
| if (in_degree.count(node) == 0) { | ||
| in_degree[node] = 0; | ||
| VisitPreNodes_(node, [&](NodeType in_node) { ++in_degree[node]; }); | ||
| } | ||
| }; | ||
| const auto& UpdateInDegree = [&](NodeType node) { | ||
| InitInDegree(node); | ||
| --in_degree[node]; | ||
| }; | ||
| const auto& TryPush = [&](NodeType node) { | ||
| InitInDegree(node); | ||
| if (visited.count(node) == 0 && in_degree[node] == 0) { | ||
| node_stack.push(node); | ||
| visited.insert(node); | ||
| } | ||
| }; | ||
|
|
||
| for (NodeIt iter = begin; iter != end; ++iter) { | ||
zhhsplendid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TryPush(*iter); | ||
| while (!node_stack.empty()) { | ||
| NodeType cur = node_stack.top(); | ||
| node_stack.pop(); | ||
| NodeHandler(cur); | ||
| VisitNextNodes_(cur, UpdateInDegree); | ||
| VisitNextNodes_(cur, TryPush); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| NodesVisitorType VisitNextNodes_; | ||
| NodesVisitorType VisitPreNodes_; | ||
| }; | ||
|
|
||
| } // namespace common | ||
| } // namespace cinn | ||
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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) 2023 CINN Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <glog/logging.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "paddle/cinn/common/dfs_topo_walker.h" | ||
|
|
||
| namespace cinn { | ||
| namespace common { | ||
|
|
||
| TEST(DfsTopoWalker, simple) { | ||
| std::vector<std::pair<int, int>> edges{ | ||
| {0, 1}, {2, 3}, {1, 3}, {0, 3}, {3, 4}}; | ||
| DfsTopoWalker<int> walker( | ||
| [&](int node, const std::function<void(int)>& NodeHandler) { | ||
| for (const auto& pair : edges) { | ||
| if (pair.second == node) { | ||
| NodeHandler(pair.first); | ||
| } | ||
| } | ||
| }, | ||
| [&](int node, const std::function<void(int)>& NodeHandler) { | ||
| for (const auto& pair : edges) { | ||
| if (pair.first == node) { | ||
| NodeHandler(pair.second); | ||
| } | ||
| } | ||
| }); | ||
| std::vector<int> sources{0, 2}; | ||
| std::vector<int> outputs; | ||
| walker(sources.begin(), sources.end(), [&](int node) { | ||
| outputs.push_back(node); | ||
| }); | ||
| for (auto output : outputs) { | ||
| LOG(INFO) << output; | ||
| } | ||
| std::vector<int> expected{0, 1, 2, 3, 4}; | ||
zhhsplendid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| EXPECT_TRUE((outputs == expected)); | ||
| } | ||
|
|
||
| } // namespace common | ||
| } // namespace cinn | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.