-
Notifications
You must be signed in to change notification settings - Fork 225
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
Add unit tests for lib/utils/graph
#842
Add unit tests for lib/utils/graph
#842
Conversation
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.
In general, be more thorough about making sure you are more aware of existing code. You've got a few places where you made a fix I requested, but only to one out of multiple places, or you implement a method of which multiple analogues exist but you don't match how they're implemented (not that this is always wrong, but it is usually). Before immediately jumping into and implementing things, use grep
or some fuzzy search of the code to check if something similar has already been done.
Also, I recommend looking at the existing code and trying to match its style. Generally we prefer to draw from containers.h
when possible rather than writing a bunch of raw loops, which are harder to maintain and easier to mess up. Also, refs in loops, etc.
And please run ./scripts/format.sh
on your code before requesting reviews 🙂
} | ||
|
||
TEST_CASE("traversal") { | ||
AdjacencyDiGraph g; | ||
std::vector<Node> const n = add_nodes(g, 4); | ||
std::vector<Node> n; |
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.
Why not add_nodes
?
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.
change to use add_nodes.
} | ||
// std::cout<<"********"<<std::endl; |
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.
Please review future PRs for random commented-out code
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.
You removed this code, but the next 4 lines are also commented out?
|
||
std::vector<Node> ordering = bfs_ordering(g, {n[0]}); | ||
for(DirectedEdge edge: edges) { |
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.
for(DirectedEdge edge: edges) { | |
for(DirectedEdge const &edge: edges) { |
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.
In general, be more thorough about making sure you are more aware of existing code. You've got a few places where you made a fix I requested, but only to one out of multiple places, or you implement a method of which multiple analogues exist but you don't match how they're implemented (not that this is always wrong, but it is usually). Before immediately jumping into and implementing things, use grep
or some fuzzy search of the code to check if something similar has already been done.
Also, I recommend looking at the existing code and trying to match its style. Generally we prefer to draw from containers.h
when possible rather than writing a bunch of raw loops, which are harder to maintain and easier to mess up. Also, refs in loops, etc.
And please run ./scripts/format.sh
on your code before requesting reviews 🙂
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.
Next time ideally try to address more of the requested changes before requesting another PR--I do appreciate going for quick feedback, but it takes a while to review a whole PR when you've only changed a couple things. If you have questions on individual comments, instead just leave a reply there. I've closed all of the comments that have actually been resolved, re-request review when you think you've addressed most or all of the remaining comments, and feel free to ask questions in the replys to the comments--this is the easiest way to get clarification on a single comment
@@ -27,14 +26,13 @@ class AdjacencyDiGraph : public IDiGraph { | |||
return new AdjacencyDiGraph(this->next_node_idx, this->adjacency); | |||
} | |||
|
|||
AdjacencyDiGraph() = default; |
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.
Generally we put the constructors at the top of the class
} | ||
|
||
private: | ||
AdjacencyMultiDiGraph(std::size_t next_node_idx, ContentsType const & adjacency, std::size_t next_node_port=0) |
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.
AdjacencyMultiDiGraph(std::size_t next_node_idx, ContentsType const & adjacency, std::size_t next_node_port=0) | |
AdjacencyMultiDiGraph(std::size_t next_node_idx, std::size_t next_node_port_idx, ContentsType const & adjacency) |
lib/utils/src/graph/algorithms.cc
Outdated
@@ -437,6 +501,36 @@ optional<Node> imm_post_dominator(MultiDiGraphView const &g, | |||
return get_imm_post_dominators(g).at(n); | |||
} | |||
|
|||
tl::optional<Node> get_imm_post_dominator(DiGraphView const & g, Node const & n) { |
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.
Okay, but tl::optional
is still used in the code three lines down--I generally don't leave a comment on every occurence of an issue (as it just gets repetitive), so if I leave a comment please check for any other code with that issue and fix it as well
@@ -14,6 +14,9 @@ struct DirectedEdge { | |||
Node src; | |||
Node dst; | |||
}; | |||
|
|||
std::ostream &operator<<(std::ostream &s, DirectedEdge const &e); |
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.
Replace with fmt
lib/utils/src/graph/algorithms.cc
Outdated
return get_imm_post_dominators(g).at(n); | ||
} | ||
|
||
|
||
tl::optional<Node> get_imm_post_dominator(DiGraphView const & g, std::unordered_set<Node> const & nodes ){ | ||
std::unordered_set<Node> commonDoms = get_post_dominators(g).at(*nodes.begin()); | ||
std::unordered_set<Node> commonDoms, currDoms, intersec; |
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.
Generally don't forward declare, it's tends to be confusing
lib/utils/src/graph/digraph.cc
Outdated
@@ -70,10 +69,16 @@ std::unordered_set<DirectedEdge> DiGraphView::query_edges(EdgeQuery const & quer | |||
DiGraphView unsafe_create(IDiGraphView const &graphView) { | |||
std::shared_ptr<IDiGraphView const> ptr((&graphView), | |||
[](IDiGraphView const *){}); | |||
/* |
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.
/* | |
// Set the shared_ptr's destructor to a nop so that effectively there is no ownership |
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.
Also move above the line of code, not below
@@ -16,6 +16,7 @@ | |||
namespace FlexFlow { | |||
|
|||
std::vector<Node> add_nodes(Graph &, int); | |||
std::vector<Node> add_nodes(IGraph &g, int num_nodes); |
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.
Why was this added?
|
||
TEST_CASE("AdjacencyMultiDiGraph:remove_node") { | ||
AdjacencyMultiDiGraph g; |
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.
Ideally don't repeat this code over and over again. Either do it as part of the same test or use subcases
NodePort p1 = g.add_node_port(); | ||
NodePort p2 = g.add_node_port(); | ||
NodePort p3 = g.add_node_port(); | ||
NodePort p0{0}; |
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.
Why?
} | ||
// std::cout<<"********"<<std::endl; |
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.
You removed this code, but the next 4 lines are also commented out?
Previously, lockshaw (Colin Unger) wrote…
if we delete, how to add many nodeports? |
Previously, lockshaw (Colin Unger) wrote…
if we delete, how to add many edges? |
Previously, lockshaw (Colin Unger) wrote…
remove |
Previously, lockshaw (Colin Unger) wrote…
still has some problem. |
Previously, lockshaw (Colin Unger) wrote…
currently it only use query_values |
Previously, lockshaw (Colin Unger) wrote…
I convert DiGraph to Graph, but we still has some problem use Code snippet: DiGraph g = DiGraph::create<AdjacencyDiGraph>();
std::vector<Node> n = add_nodes(g, 4);
//we want to add nodes for this DiGraph, but :45:35: error: cannot bind non-const lvalue reference of type ‘FlexFlow::Graph&’ to an rvalue of type ‘FlexFlow::Graph’
//std::vector<Node> n = add_nodes(g, 4);
//if we convert DiGraph g to Graph g, then we have to implement the Graph::add_node
Node Graph::add_node(){
return this->ptr.get_mutable()->add_node();
}
//but ptr is a IGraph, IGraph is a pure class
^ |
Previously, lockshaw (Colin Unger) wrote…
I am confused your code because currently it makes me hard to read your code. maybe you could paste your full implementation here. |
Previously, lockshaw (Colin Unger) wrote…
how to assert DirectedEdgeQuery Code snippet: struct DirectedEdgeQuery {
query_set<Node> srcs;
query_set<Node> dsts;
static DirectedEdgeQuery all();
};
//use assert(!is_matchall(q.srcs);
// assert(!is_matchall(q.dsts) |
Previously, lockshaw (Colin Unger) wrote…
I reply in other channels |
Previously, lockshaw (Colin Unger) wrote…
if we delete it, I guess you want to use a function Code snippet: void add_edges(Graph &, std::vector<DirectedEdge> const & edges)
//first, the edge in DiGraph, MultiDiGraph is different, one is DirectedEdge, another is MultiDiEdge
//second, we will meet problmes like add_nodes(Graph &, size_t). I reply in other channel. |
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.
Reviewable status: 20 of 36 files reviewed, 52 unresolved discussions (waiting on @lockshaw)
lib/utils/include/utils/graph/digraph.h
line 63 at r21 (raw file):
Previously, lockshaw (Colin Unger) wrote…
In that case it should probably be removed
Done.
lib/utils/include/utils/graph/node.h
line 57 at r29 (raw file):
Previously, lockshaw (Colin Unger) wrote…
Move into a new file called
utils/internal_only_tag.h
Done.
lib/utils/src/graph/algorithms.cc
line 509 at r7 (raw file):
Previously, lockshaw (Colin Unger) wrote…
I would do the following
// in containers.h template <typename T> std::unordered_set<T> intersection(std::vector<std::unordered_set<T>> const &v) { if (v.empty()) { throw mk_runtime_error("cannot take intersection of no sets"); } std::unordered_set<T> result = v[0]; for (int i = 1; i < v.size(); i++) { result = intersection(result, v); } return result; } // here std::unordered_set<Node> commonDoms = intersection(values(restrict_keys(get_post_dominators(g), nodes))); return ...; // see next comment
Done.
lib/utils/src/graph/node.cc
line 21 at r21 (raw file):
Previously, lockshaw (Colin Unger) wrote…
Yes, but these should be defined using
fmt
, notoperator<<
I use your sugg
Previously, lockshaw (Colin Unger) wrote…
I reply in other channel |
if we have a function add_node_ports, it definition is like? Code snippet: std::vector<NodePort> add_node_ports(DiGraph &, size_t);
std::vector<NodePort> add_node_ports(MultiDiGraph &, size_t); |
Previously, lockshaw (Colin Unger) wrote…
still have some bug. I am debuging. |
Previously, lockshaw (Colin Unger) wrote…
I modify the unecked_dfs_iterator. but the time complexity is o(n) because std::find, I try to optimize it. Code snippet: udi &udi::operator++() {
Node const last = this->operator*();
this->stack.pop_back();
std::unordered_set<DirectedEdge> outgoing = get_outgoing_edges(graph, {last});
for (DirectedEdge const &e : outgoing) {
auto it = std::find(stack.begin(), stack.end(), e.dst);
if (it == stack.end()) {
stack.push_back(e.dst);
} else {
stack.erase(it);
stack.push_back(e.dst);
}
}
return *this;
} |
Previously, lockshaw (Colin Unger) wrote…
still struggle to implement this. |
Previously, lockshaw (Colin Unger) wrote…
this shouldn't work. query_set(T const & node):{ Code snippet: template <typename T>
struct query_set {
query_set() = delete;
query_set(T const &query) : query({query}) {}
query_set(std::unordered_set<T> const &query) : query(query) {}
query_set(optional<std::unordered_set<T>> const &query) : query(query) {}
friend bool operator==(query_set const &lhs, query_set const &rhs) {
return lhs.value == rhs.value;
}
friend bool operator!=(query_set const &lhs, query_set const &rhs) {
return lhs.value != rhs.value;
}
friend bool operator<(query_set const &lhs, query_set const &rhs) {
return lhs.value < rhs.value;
}
friend bool is_matchall(query_set const &q) {
return !q.query.has_value();
}
friend std::unordered_set<T> allowed_values(query_set const &q) {
assert(!is_matchall(q));
return q.query.value();
}
static query_set<T> matchall() {
return {nullopt};
}
private:
optional<std::unordered_set<T>> query;
}; |
Previously, lambda7xx (Lambda(Xiaoxiang) Shi ) wrote…
I think there are still problems when we use fmt not operator<< |
Previously, lambda7xx (Lambda(Xiaoxiang) Shi ) wrote…
fix it |
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.
Reviewable status: 19 of 36 files reviewed, 52 unresolved discussions (waiting on @lockshaw)
lib/utils/include/utils/graph/digraph.h
line 74 at r29 (raw file):
} static DiGraphView unsafe_create(IDiGraphView const &graphView);
Done.
lib/utils/test/src/test_algorithms.cc
line 49 at r21 (raw file):
Previously, lockshaw (Colin Unger) wrote…
I still don't see it: https://github.com/lambda7xx/FlexFlow/blob/2a8f8a6bbc7e75c4c9b1396bbd79ef64e48039ff/lib/utils/test/src/test_algorithms.cc#L41
Done.
lib/utils/test/src/test_algorithms.cc
line 26 at r29 (raw file):
}; g.add_edges(e);
Done.
lib/utils/graph
Moving to #940 |
Description of changes:
This PR is for the repo-refactor utils.
Before merging:
GraphView::unsafe_create(IGraphView const &)
andunsafe(IMultiDiGraphView const &)
andunsafe(IDiGraphView const&)
This change is