! * \brief computing graph optimizer * * The optimization takes place during graph construction; currently two * optimizations are implemented: * 1. common subexpression elimination * 2. swap type_cvt followed by broadcast when value is constant * 3. merge multiple broadcasts when value is constant * 4. constant folding */
| 16 | * 4. constant folding |
| 17 | */ |
| 18 | class GraphOptimizer { |
| 19 | //! group operator nodes by hash value, for CSE |
| 20 | ThinHashMap<size_t, std::vector<OperatorNodeBase*>> m_opr_hash_list; |
| 21 | |
| 22 | //! map from const inferable var node to its ImmutableTensor opr |
| 23 | ThinHashMap<VarNode*, OperatorNodeBase*> m_const_map; |
| 24 | |
| 25 | /*! |
| 26 | * \brief try to replace multiple broadcasts into one |
| 27 | * |
| 28 | * \return nullptr if failed to replace; otherwise it returns the new |
| 29 | * Broadcast opr |
| 30 | */ |
| 31 | OperatorNodeBase* merge_bcast(VarNode* var); |
| 32 | |
| 33 | /*! |
| 34 | * \brief try to swap a TypeCvt followed by a Broadcast |
| 35 | * |
| 36 | * \return nullptr if failed to swap; otherwise it returns the swapped |
| 37 | * oprs |
| 38 | */ |
| 39 | OperatorNodeBase* swap_typecvt_and_bcast(VarNode* var); |
| 40 | |
| 41 | /*! |
| 42 | * \brief try to replace a var by an ImmutableTensor |
| 43 | * |
| 44 | * \return nullptr if failed to replace; otherwise it returns the new |
| 45 | * ImmutableTensor opr |
| 46 | */ |
| 47 | OperatorNodeBase* replace_const_var(VarNode* var); |
| 48 | |
| 49 | public: |
| 50 | /*! |
| 51 | * \brief called at beginning of inserting opr to graph |
| 52 | * |
| 53 | * This method should be first quried when inserting an operator; if it |
| 54 | * returns nullptr, normal insertion procedure continuous; otherwise the |
| 55 | * returned opr should be used and new opr to be inserted should be |
| 56 | * discarded. |
| 57 | */ |
| 58 | OperatorNodeBase* insert_pre(OperatorNodeBase* opr); |
| 59 | |
| 60 | /*! |
| 61 | * \brief called at end of inserting opr to graph |
| 62 | * |
| 63 | * This method should be quried after new operator is initialized and |
| 64 | * stored; it would either return *opr*, or an optimized version of |
| 65 | * *opr*. |
| 66 | * |
| 67 | * Currently it only replaces const values for single output operator. |
| 68 | */ |
| 69 | OperatorNodeBase* insert_post(OperatorNodeBase* opr); |
| 70 | }; |
| 71 | |
| 72 | } // namespace cg |
| 73 | } // namespace mgb |
no outgoing calls
no test coverage detected