-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relax] Move SymbolicMatcher to tir/analysis #18650
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
Closed
+219
−122
Closed
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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,137 @@ | ||
| /* | ||
| * 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 "symbolic_matcher.h" | ||
|
|
||
| #include <tvm/arith/analyzer.h> | ||
| #include <tvm/tir/op.h> | ||
| #include <tvm/tir/stmt_functor.h> | ||
|
|
||
| namespace tvm { | ||
| namespace tir { | ||
|
|
||
| void SymbolicMatcher::Match(const ffi::Array<PrimExpr>& params, const ffi::Array<PrimExpr>& args) { | ||
| CHECK_EQ(params.size(), args.size()); | ||
| for (size_t i = 0; i < params.size(); ++i) { | ||
| Match(params[i], args[i]); | ||
| } | ||
| } | ||
|
|
||
| void SymbolicMatcher::Match(const PrimExpr& param, const PrimExpr& arg) { | ||
| VisitExpr(param, arg); | ||
| must_prove_ = analyzer_->Simplify(Substitute(must_prove_, *var_remap_)); | ||
| CHECK(!is_zero(must_prove_)); | ||
| } | ||
|
|
||
| void SymbolicMatcher::VisitExpr(const PrimExpr& node, const PrimExpr& other) { | ||
| if (node.same_as(other)) { | ||
| return; | ||
| } else if (node.dtype().code() != other.dtype().code()) { | ||
| LOG(FATAL) << "Parameter expression " << node << " with dtype " << node.dtype() | ||
| << " cannot match to argument " << other << " with dtype " << other.dtype(); | ||
| } else { | ||
| ExprFunctor::VisitExpr(node, other); | ||
| } | ||
| } | ||
|
|
||
| #define TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(OpName) \ | ||
| void SymbolicMatcher::VisitExpr_(const OpName* op, const PrimExpr& other) { \ | ||
| const auto* rhs = other.as<OpName>(); \ | ||
| if (rhs) { \ | ||
| VisitExpr(op->a, rhs->a); \ | ||
| VisitExpr(op->b, rhs->b); \ | ||
| } else { \ | ||
| must_prove_ = must_prove_ && (ffi::GetRef<PrimExpr>(op) == other); \ | ||
| } \ | ||
| } | ||
|
|
||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(AddNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(SubNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(MulNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(DivNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(ModNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(EQNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(NENode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(LTNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(LENode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(GTNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(GENode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(AndNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(OrNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(MinNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(MaxNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(FloorDivNode); | ||
| TVM_DECLARE_SYMBOLIC_MATCHER_BINOP(FloorModNode); | ||
|
|
||
| void SymbolicMatcher::VisitExpr_(const IntImmNode* op, const PrimExpr& other) { | ||
| const auto* rhs = other.as<IntImmNode>(); | ||
| if (!rhs || (op->value != rhs->value)) { | ||
| LOG(FATAL) << "Parameter expression " << ffi::GetRef<PrimExpr>(op) | ||
| << " expected an integer argument with value " << op->value << ", " | ||
| << "but was provided with the argument " << other; | ||
| } | ||
| } | ||
|
|
||
| void SymbolicMatcher::VisitExpr_(const FloatImmNode* op, const PrimExpr& other) { | ||
| const auto* rhs = other.as<FloatImmNode>(); | ||
| if (!rhs || (op->value != rhs->value)) { | ||
| LOG(FATAL) << "Parameter expression " << ffi::GetRef<PrimExpr>(op) | ||
| << " expected an float argument with value " << op->value << ", " | ||
| << "but was provided with the argument " << other; | ||
| } | ||
| } | ||
|
|
||
| void SymbolicMatcher::VisitExpr_(const CastNode* op, const PrimExpr& other) { | ||
| const auto* rhs = other.as<CastNode>(); | ||
| if (!rhs) { | ||
| LOG(FATAL) << "Parameter expression " << ffi::GetRef<PrimExpr>(op) << " expected an cast to " | ||
| << op->dtype << " as the argument, " | ||
| << "but was provided with the argument " << other; | ||
| } | ||
| VisitExpr(op->value, rhs->value); | ||
| } | ||
|
|
||
| void SymbolicMatcher::VisitExpr_(const VarNode* op, const PrimExpr& rhs) { | ||
| auto lhs = ffi::GetRef<Var>(op); | ||
|
|
||
| if (lhs.same_as(rhs)) { | ||
| // Reference identity, no further checks needed. | ||
| } else if (op->dtype.code() != rhs->dtype.code()) { | ||
| LOG(FATAL) << "Parameter expression " << ffi::GetRef<PrimExpr>(op) << " with dtype " | ||
| << op->dtype << " cannot match to argument " << rhs << " with dtype " << rhs.dtype(); | ||
| } else if (auto it = var_remap_->find(lhs); it != var_remap_->end()) { | ||
| VisitExpr((*it).second, rhs); | ||
| } else { | ||
| var_remap_->Set(lhs, rhs); | ||
| } | ||
| } | ||
|
|
||
| void SymbolicMatcher::VisitExpr_(const SelectNode* op, const PrimExpr& other) { | ||
| const auto* rhs = other.as<SelectNode>(); | ||
| if (rhs) { | ||
| VisitExpr(op->condition, rhs->condition); | ||
| VisitExpr(op->true_value, rhs->true_value); | ||
| VisitExpr(op->false_value, rhs->false_value); | ||
| } else { | ||
| must_prove_ = must_prove_ && (ffi::GetRef<PrimExpr>(op) == other); | ||
| } | ||
| } | ||
|
|
||
| } // namespace tir | ||
| } // namespace tvm | ||
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,81 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #ifndef TVM_TIR_ANALYSIS_SYMBOLIC_MATCHER_H_ | ||
| #define TVM_TIR_ANALYSIS_SYMBOLIC_MATCHER_H_ | ||
|
|
||
| #include <tvm/tir/expr.h> | ||
| #include <tvm/tir/expr_functor.h> | ||
|
|
||
| namespace tvm { | ||
|
|
||
| namespace arith { | ||
| class Analyzer; | ||
| } | ||
|
|
||
| namespace tir { | ||
|
|
||
| /*! | ||
| * \brief Match symbolic vars according to the given PrimExpr, and update the var_remap. | ||
| * Will throw errors if there is a mismatch. | ||
| */ | ||
| class SymbolicMatcher : ExprFunctor<void(const PrimExpr& n, const PrimExpr& other)> { | ||
| public: | ||
| explicit SymbolicMatcher(arith::Analyzer* analyzer, ffi::Map<tir::Var, PrimExpr>* var_remap) | ||
| : analyzer_(analyzer), var_remap_(var_remap) {} | ||
|
|
||
| void Match(const ffi::Array<PrimExpr>& params, const ffi::Array<PrimExpr>& args); | ||
| void Match(const PrimExpr& param, const PrimExpr& arg); | ||
|
|
||
| private: | ||
| void VisitExpr(const PrimExpr& node, const PrimExpr& other); | ||
|
|
||
| void VisitExpr_(const AddNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const SubNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const MulNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const DivNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const ModNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const EQNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const NENode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const LTNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const LENode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const GTNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const GENode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const AndNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const OrNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const MinNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const MaxNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const FloorDivNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const FloorModNode* op, const PrimExpr& other) final; | ||
|
|
||
| void VisitExpr_(const IntImmNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const FloatImmNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const CastNode* op, const PrimExpr& other) final; | ||
| void VisitExpr_(const VarNode* op, const PrimExpr& rhs) final; | ||
| void VisitExpr_(const SelectNode* op, const PrimExpr& other) final; | ||
|
|
||
| arith::Analyzer* analyzer_; | ||
| ffi::Map<tir::Var, PrimExpr>* var_remap_; | ||
| PrimExpr must_prove_ = Bool(true); | ||
| }; | ||
|
|
||
| } // namespace tir | ||
| } // namespace tvm | ||
|
|
||
| #endif // TVM_TIR_ANALYSIS_SYMBOLIC_MATCHER_H_ |
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.