constant_folding_pass.cc 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// Copyright (c) 2023 CINN Authors. All Rights Reserved.
//
// Licensed 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 "paddle/cinn/common/type.h"
#include "paddle/cinn/hlir/pass/constant_folding_pass_util.h"

namespace cinn {
namespace hlir {
namespace pass {

using framework::Graph;
using framework::Node;
using framework::NodeData;
using framework::OpPatternKind;
using framework::shape_t;

using common::GraphEdge;
using common::GraphNode;

31 32
using AlterFunction =
    std::function<void(const FusionHelperBase*, Graph*, Node*)>;
33 34 35 36 37

// Constant Fold Pass
//
class ConstantFoldingPassHelper : public FusionHelperBase {
 public:
38
  explicit ConstantFoldingPassHelper(Graph* graph)
39 40 41
      : FusionHelperBase(graph), graph_(graph) {
    RegisterAlterFunction();
  }
42 43 44 45

  void operator()() {
    bool update = false;
    do {
46
      update = false;
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
      auto nodes_inorder = std::get<0>(graph_->topological_order());
      for (auto node : nodes_inorder) {
        if (!node->safe_as<Node>()) {
          continue;
        }
        // check producer's type
        auto producers = GetProducerNode(node->safe_as<Node>());
        if (producers.empty()) {
          continue;
        }

        bool can_fold = true;
        for (auto producer : producers) {
          if (!IsConstOp(producer)) {
            can_fold = false;
            break;
          }
          // if producer's output in graph_->outputs, then will not fold
          for (auto& edge : producer->outlinks()) {
            auto graph_node = edge->sink();
67
            auto data = graph_node->safe_as<NodeData>();
68
            CHECK(data);
69 70 71
            if (std::find(graph_->outputs.begin(),
                          graph_->outputs.end(),
                          data) != graph_->outputs.end()) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
              can_fold = false;
              break;
            }
          }
        }

        if (!can_fold) continue;
        // key = "${cur_node_id}_${producer_node_id}"", for example:
        // "broadcast_to_fill_constant" means fill_constant->broadcast_to
        auto key = GetTypeName(node->safe_as<Node>());
        if (alter_function_.count(key)) {
          alter_function_[key](this, graph_, node->safe_as<Node>());
          update = true;
        }
      }
    } while (update);
  }

 private:
  void RegisterAlterFunction() {
92 93 94 95 96 97
    alter_function_ = {
        {"broadcast_to_const_scalar", fold_broadcast_to_constant},
        {"broadcast_to_fill_constant", fold_broadcast_to_constant},
        {"reshape_fill_constant", fold_reshape_fill_constant},
        {"squeeze_fill_constant", fold_squeeze_fill_constant},
        {"expand_dims_fill_constant", fold_expand_dims_fill_constant}};
98 99 100
  }

  std::string GetTypeName(Node* node) {
101
    auto producers = GetProducerNode(node->safe_as<Node>());
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    std::string key = node->op()->name;
    for (auto producer : producers) {
      key += std::string("_") + producer->op()->name;
    }
    return key;
  }

  std::unordered_map<std::string, AlterFunction> alter_function_;
  Graph* graph_;
};

void ConstantFoldingPassInternal(Graph* graph) {
  ConstantFoldingPassHelper constant_folding_pass_helper(graph);
  constant_folding_pass_helper();
}

}  // namespace pass
}  // namespace hlir
}  // namespace cinn

CINN_REGISTER_HELPER(ConstantFolding) {
  CINN_REGISTER_PASS(ConstantFolding)
      .describe("Constant Fold Pass which performs \"Constant Folding\"")
      .set_change_structure(true)
      .set_body(cinn::hlir::pass::ConstantFoldingPassInternal);

  return true;
}