Skip to content
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

[refactor] Add SNode::GradInfoProvider to isolate SNode from Expr #2207

Merged
merged 2 commits into from
Mar 10, 2021

Conversation

k-ye
Copy link
Member

@k-ye k-ye commented Mar 10, 2021

Related issue = #2196

This PR is still about decoupling SNode from taichi/program. The major issue here is around its member variable expr. Because Expr is part of the frontend and relies on taichi/program, we have to move it out from SNode:

  • Moved place() and lazy_grad() from SNode itself to a separate file taichi/program/snode_expr_utils.h/cpp (i cannot think of a better file name...). Other functions in SNode do not depend on expr.
  • Created a SNode::GradInfoProvider abstract class. This is essentially a wrapper around GlobalVariableExpression. From what I can tell, SNode::expr is mostly used to provide gradient information, e.g. is_primal(), has_grad(), etc. The actual implementation, GradInfoImpl, is in taichi/program/snode_expr_utils.cpp.
  • Created a map from SNode* to GlobalVariableExpression. The map is stored inside Program. While it is possible to retrieve GlobalVariableExpression from GradInfoImpl, that involves casting GradInfoProvider to GradInfoImpl and kind of exposes the implementation detail. I'd like to make GradInfoProvider have a single responsibility.

[Click here for the format server]


@k-ye k-ye requested a review from taichi-gardener March 10, 2021 05:58
@k-ye k-ye force-pushed the refactor-snode2 branch from 171beb4 to 99f0c85 Compare March 10, 2021 05:58
@k-ye k-ye requested review from yuanming-hu and xumingkuan March 10, 2021 06:04
taichi/program/snode_expr_utils.cpp Show resolved Hide resolved
taichi/struct/struct.cpp Show resolved Hide resolved
taichi/program/snode_expr_utils.cpp Show resolved Hide resolved
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I like the GradInfoProvider class.

taichi/program/snode_expr_utils.cpp Show resolved Hide resolved
Copy link
Contributor

@xumingkuan xumingkuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

ExprGroup indices;
for (int i = 0; i < snode->num_active_indices; i++) {
indices.push_back(Expr::make<ArgLoadExpression>(i, PrimitiveType::i32));
}
auto ret = Stmt::make<FrontendKernelReturnStmt>(
load_if_ptr((snode->expr)[indices]));
load_if_ptr(Expr(snode_to_glb_var_exprs_.at(snode))[indices]));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we need this Expr()? (same question at line 663)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Note that SNode::expr used to be of type Expr, which contains a GlobalVariableExpression. Here, snode_to_glb_var_exprs_ maps directly from SNode* to GlobalVariableExpression. As a result, when we do need to use an Expr, we have to wrap that up.

The reason for mapping to GlobalVariableExpression is that, If you look at the previous implementation of is_primal() (has_grad(), get_grad()), they do a lot of dynamic casts from Expr to GlobalVariableExpression.

@k-ye k-ye merged commit f5867d7 into taichi-dev:master Mar 10, 2021
@k-ye k-ye deleted the refactor-snode2 branch March 10, 2021 11:13
@k-ye k-ye mentioned this pull request Mar 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants