-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relax] Allow R.Prim('bool') in relax::If and assert_op #16642
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
Changes from all commits
5462910
2c64c81
f554738
95663ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 <tvm/relax/expr_functor.h> | ||
| #include <tvm/relax/transform.h> | ||
| #include <tvm/tir/analysis.h> | ||
| #include <tvm/tir/builtin.h> | ||
| #include <tvm/tir/stmt_functor.h> | ||
|
|
||
| namespace tvm { | ||
| namespace relax { | ||
|
|
||
| namespace { | ||
|
|
||
| class PrimValueComputeInjector : public ExprMutator { | ||
| public: | ||
| IRModule Finalize() const { return builder_->Finalize(); } | ||
|
|
||
| using ExprMutator::VisitExpr_; | ||
|
|
||
| Expr VisitExpr_(const PrimValueNode* op) override { | ||
| auto node = Downcast<PrimValue>(ExprMutator::VisitExpr_(op)); | ||
|
|
||
| if (node->value->IsInstance<tir::IntImmNode>() || node->value->IsInstance<tir::VarNode>()) { | ||
| return node; | ||
| } | ||
|
|
||
| auto ret_dtype = node->value->dtype; | ||
| auto param_vars = tir::UndefinedVars(node->value); | ||
|
Contributor
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. Would this call know which TIR vars are in scope per the Relax scoping rules?
Contributor
Author
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. This call isn’t aware of the Relax scoping rules, but I don’t think there’s a benefit of checking it at this point. Any well-formed input that only uses in-scope TIR variables would produce well-formed output. Any ill-formed input that uses out-of-scope TIR variables would produce ill-formed output that still uses the out-of-scope TIR variables. Validating the relax scoping rules at this point would require additional tracking the in-scope variables, which would duplicate the functionality of the well-formed checker. Since this pass wouldn’t make any ill-formed usage worse (and therefore harder to debug), I don’t think it’s worth duplicating the in-scope tracking here.
Contributor
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. Yeah I think you're right that this would still work out just fine in that case.
Contributor
Author
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. Probably overkill, but I think it ended up being simpler to just generate the appropriate |
||
| tir::Stmt body = tir::Evaluate(tir::Call(ret_dtype, tir::builtin::ret(), {node->value})); | ||
|
|
||
| tir::PrimFunc func(param_vars, body, PrimType(ret_dtype)); | ||
| func = tir::RenewDefs(func); | ||
|
|
||
| auto callee = builder_->AddFunction(func, "compute_symbolic_expr"); | ||
|
|
||
| return relax::Call(callee, param_vars.Map([](const tir::Var& tir_var) -> relax::Expr { | ||
| return relax::PrimValue(tir_var); | ||
| })); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace transform { | ||
|
|
||
| Pass ComputePrimValue() { | ||
| runtime::TypedPackedFunc<IRModule(IRModule, PassContext)> pass_func = | ||
| [=](IRModule mod, PassContext pc) -> IRModule { | ||
| PrimValueComputeInjector mutator; | ||
|
|
||
| IRModule updates; | ||
| for (const auto& [gvar, base_func] : mod->functions) { | ||
| if (auto func = base_func.as<Function>()) { | ||
| auto updated = Downcast<Function>(mutator(func.value())); | ||
| if (!updates.same_as(base_func)) { | ||
| updates->Add(gvar, updated); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (updates->functions.size()) { | ||
| auto write_ptr = mod.CopyOnWrite(); | ||
| write_ptr->Update(updates); | ||
| write_ptr->Update(mutator.Finalize()); | ||
| } | ||
|
|
||
| return mod; | ||
| }; | ||
| return CreateModulePass(pass_func, 0, "ComputePrimValue", {}); | ||
| } | ||
|
|
||
| TVM_REGISTER_GLOBAL("relax.transform.ComputePrimValue").set_body_typed(ComputePrimValue); | ||
|
|
||
| } // namespace transform | ||
|
|
||
| } // namespace relax | ||
| } // namespace tvm | ||
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.
I think this description should be more precise. I assume it's supposed to come late in the phase ordering since it inserts direct calls to PrimFuncs? (And so should probably come after we end purity checking?)
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.
Good point on improving the docstring.
Regarding phase ordering, I don’t think we need to restrict its usage. The calls to PrimFunc instances are valid in user-provided Relax functions, so this could occur early in the phase ordering. The only limitation is that it must occur before
VMShapeLower, asVMShapeLowerexpects allR.prim_value(arg)expressions to have int64 arguments.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.
Yes, but inserting the PrimFunc calls will likely change the purity of the functions where that happens.
call_tircould be used to avoid that but then that will require using this before loweringcall_tir.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.
Hmm. I think this is another argument in favor of allowing
FuncStructInfoannotations forPrimFuncobjects, as that would allow the generatedPrimFuncinstances to be marked as pure functions. I'll add a unit test to see how well that works for maintaining purity tracking when calling a pure PrimFunc from a pure Relax function.Uh oh!
There was an error while loading. Please reload this page.
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.
Are they truly pure? No modification of external values?
Edit: Yeah, they just use a return value. I imagine this means that we actually have to check the bodies of PrimFuncs to determine if they're pure and also give users the option to override the automatic judgment. The rules for that can be very simple: Consider it impure if there is any write to a tensor or external call.
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.
I was thinking an even simpler heuristic: All PrimFuncs are impure, unless explicitly annotated otherwise. In this case, since the functions are being generated in a manner that requires purity, it could also provide the annotation.
For long-term, agreed, it would be good to have the TIR-level purity analysis. I think I'd weaken the condition you mentioned slightly: A function is impure if it writes to a buffer that it didn't itself allocate.
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.
Good point. I'm fine with requiring an annotation since the great majority of PrimFuncs are going to be impure.