|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file src/relax/transform/canonicalize_bindings.cc |
| 22 | + * \brief Pass for simplifying modules by folding var bindings and match shape nodes. |
| 23 | + * May include other forms of simplification in the future. |
| 24 | + * Ideally should be used before constant folding and eliminating unused bindings. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <tvm/relax/expr.h> |
| 28 | +#include <tvm/relax/expr_functor.h> |
| 29 | +#include <tvm/relax/struct_info.h> |
| 30 | +#include <tvm/relax/transform.h> |
| 31 | + |
| 32 | +namespace tvm { |
| 33 | +namespace relax { |
| 34 | + |
| 35 | +class BindingCanonicalizer : public ExprMutator { |
| 36 | + public: |
| 37 | + BindingCanonicalizer() {} |
| 38 | + |
| 39 | + Expr VisitExpr_(const VarNode* op) override { |
| 40 | + // remap first |
| 41 | + Var v = Downcast<Var>(ExprMutator::VisitExpr_(op)); |
| 42 | + if (!CanCanonicalizeVar(v)) { |
| 43 | + return Downcast<Expr>(v); |
| 44 | + } |
| 45 | + // visit again in case we need to do a substitution in the value |
| 46 | + return ExprMutator::VisitExpr_(LookupBinding(v).as<VarNode>()); |
| 47 | + } |
| 48 | + |
| 49 | + Expr VisitExpr_(const DataflowVarNode* op) override { |
| 50 | + Var v = Downcast<Var>(ExprMutator::VisitExpr_(op)); |
| 51 | + if (!CanCanonicalizeVar(v)) { |
| 52 | + return Downcast<Expr>(v); |
| 53 | + } |
| 54 | + return ExprMutator::VisitExpr_(LookupBinding(v).as<DataflowVarNode>()); |
| 55 | + } |
| 56 | + |
| 57 | + void VisitBinding_(const VarBindingNode* binding) override { |
| 58 | + // Unlike default visitor, we do not permit the checked type to change |
| 59 | + // if the new value's checked type is different (this preserves user annotations) |
| 60 | + Expr new_value = this->VisitExpr(binding->value); |
| 61 | + Var new_var = this->VisitVarDef(binding->var); |
| 62 | + |
| 63 | + if (new_var.same_as(binding->var) && new_value.same_as(binding->value)) { |
| 64 | + this->builder_->EmitNormalized(GetRef<VarBinding>(binding)); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + this->builder_->EmitNormalized(VarBinding(new_var, new_value)); |
| 69 | + } |
| 70 | + |
| 71 | + void VisitBinding_(const MatchCastNode* binding) override { |
| 72 | + // If we have a trivial shape check (the shape_ of LHS and RHS is the same), |
| 73 | + // we can canonicalize to a var binding |
| 74 | + Expr new_value = this->VisitExpr(binding->value); |
| 75 | + |
| 76 | + // if the LHS and RHS have the same struct info, we canonicalize to a var binding instead |
| 77 | + if (StructuralEqual()(binding->struct_info, GetStructInfo(new_value))) { |
| 78 | + builder_->EmitNormalized(VarBinding(binding->var, new_value)); |
| 79 | + } else if (new_value.same_as(binding->value)) { |
| 80 | + builder_->EmitNormalized(GetRef<MatchCast>(binding)); |
| 81 | + } else { |
| 82 | + builder_->EmitNormalized(MatchCast(binding->var, new_value, binding->struct_info)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private: |
| 87 | + bool AnnotationsDiffer(const ObjectRef& obj1, const ObjectRef& obj2, |
| 88 | + std::function<bool(const ObjectRef&, const ObjectRef&)> check_eq) { |
| 89 | + // annotations differ if one is present but not the other |
| 90 | + // or they're both present and they differ |
| 91 | + bool both_present = obj1.defined() && obj2.defined(); |
| 92 | + bool neither_present = !obj1.defined() && !obj2.defined(); |
| 93 | + return !(both_present || neither_present) || (both_present && !check_eq(obj1, obj2)); |
| 94 | + } |
| 95 | + |
| 96 | + bool CanCanonicalizeVar(Var v) { |
| 97 | + Optional<Expr> value = LookupBinding(v); |
| 98 | + // can replace only if the value is also a var |
| 99 | + if (!value || !value.as<VarNode>()) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + Var parent_var = Downcast<Var>(value); |
| 103 | + |
| 104 | + // Cases when we conservatively do not unify: |
| 105 | + // 1. checked_type_ or shape_ of the child differs from that of the parent |
| 106 | + // In this case, we could be overriding user annotations. |
| 107 | + // 2. If the child is a Var and the parent is a DataflowVar. |
| 108 | + // That could result in a DataflowVar leaving the current DataflowBlock. |
| 109 | + bool annotations_differ = AnnotationsDiffer(v->struct_info_, parent_var->struct_info_, |
| 110 | + [&](const ObjectRef& lhs, const ObjectRef& rhs) { |
| 111 | + return tvm::StructuralEqual()(lhs, rhs); |
| 112 | + }); |
| 113 | + bool var_to_dataflow = (!v.as<DataflowVarNode>() && parent_var.as<DataflowVarNode>()); |
| 114 | + return !annotations_differ && !var_to_dataflow; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +Expr CanonicalizeBindings(const Expr& e) { return BindingCanonicalizer().VisitExpr(e); } |
| 119 | + |
| 120 | +namespace transform { |
| 121 | + |
| 122 | +Pass CanonicalizeBindings() { |
| 123 | + runtime::TypedPackedFunc<Function(Function, IRModule, PassContext)> pass_func = |
| 124 | + [=](Function f, IRModule m, PassContext pc) { |
| 125 | + return Downcast<Function>(CanonicalizeBindings(f)); |
| 126 | + }; |
| 127 | + return CreateFunctionPass(pass_func, 1, "CanonicalizeBindings", {}); |
| 128 | +} |
| 129 | + |
| 130 | +TVM_REGISTER_GLOBAL("relax.transform.CanonicalizeBindings").set_body_typed(CanonicalizeBindings); |
| 131 | + |
| 132 | +} // namespace transform |
| 133 | + |
| 134 | +} // namespace relax |
| 135 | +} // namespace tvm |
0 commit comments