-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Relay] Mutual Recursion Support #5881
Closed
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d4a51ff
resolved cherry pick
hypercubestart 25660c8
support ADT
hypercubestart 7b5c9a7
lint
hypercubestart 42cc5fc
format
hypercubestart 581b5ca
fix warning
hypercubestart addce05
add move semantics to avoid copying
hypercubestart 3f8381d
WIP
hypercubestart c788cca
undo lgi changes
hypercubestart ed183e3
remove unrelated changes
hypercubestart 47814b9
seems to be working?
hypercubestart 2764600
add prelude test
hypercubestart 2907ee8
working rn
hypercubestart 9e58d7e
fixup
hypercubestart 58b05a6
add even/odd test
hypercubestart fbafb06
fix reolsving type conflict
hypercubestart 0cdcf2a
remove type_params where possible
hypercubestart 7524af1
fix broadcastcomprel
hypercubestart 4dc95fc
fix changes
hypercubestart e1c2c57
remove type relation for binary op
hypercubestart 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,8 @@ def InferType(): | |
""" | ||
return _ffi_api.InferType() | ||
|
||
def InferTypeAll(): | ||
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. Add a doc string |
||
return _ffi_api.InferTypeAll() | ||
|
||
def FoldScaleAxis(): | ||
"""Fold the scaling of axis into weights of conv2d/dense. This pass will | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,6 +109,44 @@ class TypeInferencer : private ExprFunctor<Type(const Expr&)>, | |
// inference the type of expr. | ||
Expr Infer(Expr expr); | ||
|
||
void SetCurrentFunc(GlobalVar current_func) { | ||
this->current_func_ = current_func; | ||
this->solver_.SetCurrentFunc(current_func); | ||
} | ||
|
||
void Solve(); | ||
Expr ResolveType(Expr expr); | ||
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. add a comment. |
||
|
||
// Lazily get type for expr | ||
// expression, we will populate it now, and return the result. | ||
Type GetType(const Expr& expr) { | ||
auto it = type_map_.find(expr); | ||
if (it != type_map_.end() && it->second.checked_type.defined()) { | ||
if (expr.as<GlobalVarNode>() != nullptr) { | ||
// if we don't dedup GlobalVarNode, two functions that use the same GlobalVar | ||
// may resolve to the same type incorrectly | ||
return DeDupType(it->second.checked_type); | ||
} | ||
return it->second.checked_type; | ||
} | ||
Type ret = this->VisitExpr(expr); | ||
CHECK(ret.defined()); | ||
KindCheck(ret, mod_); | ||
ResolvedTypeInfo& rti = type_map_[expr]; | ||
rti.checked_type = ret; | ||
return ret; | ||
} | ||
|
||
// Lazily get type for GlobalVar | ||
// expression, we will populate it now, and return the result. | ||
Type GetTypeGlobalVar(const GlobalVar& expr) { | ||
// we have to visit functiion | ||
// or else it may not be type-checked | ||
auto f = Downcast<Function>(mod_->Lookup(expr)); | ||
Type ret = GetType(f); | ||
return GetType(expr); | ||
} | ||
|
||
private: | ||
// type resolver that maps back to type | ||
class Resolver; | ||
|
@@ -143,21 +181,6 @@ class TypeInferencer : private ExprFunctor<Type(const Expr&)>, | |
} | ||
} | ||
|
||
// Lazily get type for expr | ||
// expression, we will populate it now, and return the result. | ||
Type GetType(const Expr& expr) { | ||
auto it = type_map_.find(expr); | ||
if (it != type_map_.end() && it->second.checked_type.defined()) { | ||
return it->second.checked_type; | ||
} | ||
Type ret = this->VisitExpr(expr); | ||
CHECK(ret.defined()); | ||
KindCheck(ret, mod_); | ||
ResolvedTypeInfo& rti = type_map_[expr]; | ||
rti.checked_type = ret; | ||
return ret; | ||
} | ||
|
||
void ReportFatalError(const ObjectRef& expr, const Error& err) { | ||
CHECK(this->current_func_.defined()); | ||
this->err_reporter.ReportAt(this->current_func_, expr, err); | ||
|
@@ -303,7 +326,6 @@ class TypeInferencer : private ExprFunctor<Type(const Expr&)>, | |
this->ReportFatalError(match, ss); | ||
} | ||
} | ||
|
||
return rtype; | ||
} | ||
|
||
|
@@ -543,14 +565,6 @@ class TypeInferencer : private ExprFunctor<Type(const Expr&)>, | |
} | ||
return FuncType(c->inputs, TypeCall(c->belong_to, types), td->type_vars, {}); | ||
} | ||
|
||
void Solve() { | ||
solver_.Solve(); | ||
|
||
if (err_reporter.AnyErrors()) { | ||
err_reporter.RenderErrors(mod_); | ||
} | ||
} | ||
}; | ||
|
||
class TypeInferencer::Resolver : public ExprMutator, PatternMutator { | ||
|
@@ -698,6 +712,20 @@ Expr TypeInferencer::Infer(Expr expr) { | |
return resolved_expr; | ||
} | ||
|
||
void TypeInferencer::Solve() { | ||
solver_.Solve(); | ||
|
||
if (err_reporter.AnyErrors()) { | ||
err_reporter.RenderErrors(mod_); | ||
} | ||
} | ||
|
||
Expr TypeInferencer::ResolveType(Expr expr) { | ||
auto resolved_expr = Resolver(type_map_, &solver_).VisitExpr(expr); | ||
CHECK(WellFormed(resolved_expr)); | ||
return resolved_expr; | ||
} | ||
|
||
struct AllCheckTypePopulated : ExprVisitor { | ||
void VisitExpr(const Expr& e) { | ||
if (e.as<OpNode>()) { | ||
|
@@ -742,6 +770,43 @@ Function InferType(const Function& func, const IRModule& mod, const GlobalVar& v | |
return Downcast<Function>(func_ret); | ||
} | ||
|
||
IRModule InferTypeAll(const IRModule& mod) { | ||
CHECK(mod.defined()) << "internal error: module must be set for type inference"; | ||
const auto& globalvars = mod->GetGlobalVars(); | ||
|
||
// first pass: fill in type for all functions | ||
for (const auto& var : globalvars) { | ||
relay::Function func = Downcast<relay::Function>(mod->Lookup(var)); | ||
func->checked_type_ = func->func_type_annotation(); | ||
mod->AddUnchecked(var, func); | ||
} | ||
|
||
// use dummy var, will be updated as we fill constraints/ | ||
// solve for each GlobalVar | ||
TypeInferencer ti = TypeInferencer(mod, GlobalVar("dummy")); | ||
|
||
// second pass, fill in constraints | ||
for (const auto& var : globalvars) { | ||
ti.SetCurrentFunc(var); | ||
ti.GetTypeGlobalVar(var); | ||
} | ||
|
||
// solve constraints | ||
ti.Solve(); | ||
|
||
// third pass, resolve types | ||
for (const auto& var : globalvars) { | ||
ti.SetCurrentFunc(var); | ||
|
||
relay::Function func = Downcast<relay::Function>(mod->Lookup(var)); | ||
Expr func_ret = ti.ResolveType(func); | ||
// add function back to module | ||
mod->AddUnchecked(var, Downcast<relay::Function>(func_ret)); | ||
} | ||
|
||
return mod; | ||
} | ||
|
||
namespace transform { | ||
|
||
Pass InferType() { | ||
|
@@ -750,8 +815,18 @@ Pass InferType() { | |
return CreateFunctionPass(pass_func, 0, "InferType", {}); | ||
} | ||
|
||
Pass InferTypeAll() { | ||
runtime::TypedPackedFunc<IRModule(IRModule, PassContext)> pass_func = | ||
[=](IRModule m, PassContext pc) { return relay::InferTypeAll(m); }; | ||
return CreateModulePass(pass_func, 0, "InferTypeAll", {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay._transform.InferType").set_body_typed([]() { return InferType(); }); | ||
|
||
TVM_REGISTER_GLOBAL("relay._transform.InferTypeAll").set_body_typed([]() { | ||
return InferTypeAll(); | ||
}); | ||
|
||
} // namespace transform | ||
|
||
} // namespace relay | ||
|
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.
add a doc string