dot_merger.cc 17.4 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) 2022 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/graph_utils.h"
#include "paddle/cinn/hlir/framework/graph.h"
#include "paddle/cinn/hlir/framework/pass.h"
#include "paddle/cinn/hlir/pass/infershape.h"

namespace cinn {
namespace hlir {
namespace pass {
namespace {

using common::GraphNode;
using framework::Node;
using framework::NodeData;
using framework::Operator;

template <typename T>
31 32 33 34 35
using OpValueType = cinn::hlir::framework::OpValueType<T>;
using infershape_t = std::function<std::vector<framework::shape_t>(
    const std::vector<framework::shape_t>&, const framework::AttrMapType&)>;
using inferdtype_t = std::function<std::vector<Type>(
    const std::vector<Type>&, const framework::AttrMapType&)>;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
using dtype_dict_t = absl::flat_hash_map<std::string, common::Type>;
using shape_dict_t = absl::flat_hash_map<std::string, framework::shape_t>;

bool accessible(GraphNode* start, GraphNode* end) {
  std::set<GraphNode const*> marked;
  std::function<void(GraphNode const*)> dfs = [&](GraphNode const* node) {
    marked.emplace(node);
    for (const auto& edge : node->outlinks()) {
      if (!marked.count(edge->sink())) {
        dfs(edge->sink());
      }
    }
  };
  dfs(start);
  return marked.count(end);
}

template <typename T>
T get_attr(Node* instr, const std::string& attr, T def) {
  if (!instr->attrs.attr_store.count(attr)) {
    return def;
  }
  return absl::get<T>(instr->attrs.attr_store.at(attr));
}

61 62 63 64 65 66
NodeData* input_operand(Node* instr, int idx) {
  return instr->inlinks_in_order()[idx]->source()->safe_as<NodeData>();
}
NodeData* output_operand(Node* instr, int idx) {
  return instr->outlinks_in_order()[idx]->sink()->safe_as<NodeData>();
}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

void remove_node(framework::Graph* graph, GraphNode* node) {
  auto inlinks = node->inlinks();
  for (auto& link : inlinks) {
    link->source()->UnLinkSingleTo(link->sink());
  }
  auto outlinks = node->outlinks();
  for (auto& link : outlinks) {
    link->source()->UnLinkSingleTo(link->sink());
  }
  graph->DropNode(node);
}

template <typename T>
bool all_equal(const T& arg) {
  return arg[0] == arg[1];
}

template <typename T, typename... Args>
bool all_equal(const T& arg, const Args&... args) {
  return all_equal(arg) && all_equal(args...);
}

void PrintAllMatmulOps(framework::Graph* graph, const std::string& dot_type) {
  auto& dtype_dict{graph->GetMutableAttrs<dtype_dict_t>("inferdtype")};
  auto& shape_dict{graph->GetMutableAttrs<shape_dict_t>("infershape")};
93
  auto nodes = std::get<0>(graph->topological_order());
94 95 96 97 98 99 100 101 102 103
  auto print_shape = [](const std::vector<int32_t>& shape) -> std::string {
    std::stringstream ss;
    for (auto i : shape) {
      ss << i << ",";
    }
    return ss.str();
  };
  for (auto* n : nodes) {
    auto* op_node = n->safe_as<Node>();
    if (op_node && op_node->op()->name == dot_type) {
104 105
      auto a_id = input_operand(op_node, 0)->id();
      auto b_id = input_operand(op_node, 1)->id();
106 107 108 109
      auto a_shape = shape_dict.at(a_id);
      auto b_shape = shape_dict.at(b_id);
      LOG(INFO) << "Find op: " << dot_type;
      LOG(INFO) << "Attrs: "
110 111 112 113
                << "trans_a = " << get_attr<bool>(op_node, "trans_a", false)
                << ", "
                << "trans_b = " << get_attr<bool>(op_node, "trans_b", false)
                << ", "
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                << "a: " << a_id << ", " << print_shape(a_shape) << " "
                << "b: " << b_id << ", " << print_shape(b_shape);
    }
  }
}

class DotBuilder {
 public:
  explicit DotBuilder(framework::Graph* graph, std::string dot_type)
      : graph_{graph},
        dot_type_{std::move(dot_type)},
        dtype_dict_{graph_->GetMutableAttrs<dtype_dict_t>("inferdtype")},
        shape_dict_{graph_->GetMutableAttrs<shape_dict_t>("infershape")} {}

  framework::Graph* graph() const { return graph_; }
129 130
  const dtype_dict_t& dtype_dict() const { return dtype_dict_; }
  const shape_dict_t& shape_dict() const { return shape_dict_; }
131 132 133 134 135 136 137 138 139 140 141 142 143

  // Currently the constructor of `NodeData` needs to pass in `Shared<Node>`.
  NodeData* Var(common::Shared<Node>& producer) {
    auto* res = new NodeData(producer, 0, 0, node_name("var"), false);
    graph_->RegisterNode(producer->id(), res);
    graph_->RegisterNode(res->id(), producer.get());
    producer->LinkTo(res);
    InferShape(producer.get(), dtype_dict_, shape_dict_);
    return res;
  }

  NodeData* Concat(int axis, std::vector<NodeData*> inputs) {
    const std::string type{"concat"};
144 145
    auto instr = common::Shared<Node>(
        new Node(framework::Operator::Get(type), type, node_name(type)));
146 147 148 149 150 151 152 153
    instr->attrs.attr_store["axis"] = axis;
    for (auto* in : inputs) {
      in->LinkTo(instr.get());
    }
    auto* output = Var(instr);
    return output;
  }

154 155 156 157 158 159
  NodeData* Matmul(bool trans_a,
                   bool trans_b,
                   bool trans_out,
                   float alpha,
                   NodeData* lhs,
                   NodeData* rhs) {
160
    const std::string type{dot_type_};
161 162 163 164 165
    auto instr = common::Shared<Node>(
        new Node(framework::Operator::Get(type), type, node_name(type)));
    matmul_ = instr.get();
    instr->attrs.attr_store["trans_a"] = trans_a;
    instr->attrs.attr_store["trans_b"] = trans_b;
166
    instr->attrs.attr_store["trans_out"] = trans_out;
167
    instr->attrs.attr_store["alpha"] = alpha;
168 169 170 171 172 173
    lhs->LinkTo(instr.get());
    rhs->LinkTo(instr.get());
    auto* output = Var(instr);
    return output;
  }

174 175 176 177 178
  NodeData* Slice(std::vector<int> axes,
                  std::vector<int> starts,
                  std::vector<int> ends,
                  NodeData* input,
                  NodeData* output) {
179
    const std::string type{"slice"};
180 181 182 183 184 185 186
    auto instr = common::Shared<Node>(
        new Node(framework::Operator::Get(type), type, node_name(type)));
    instr->attrs.attr_store["axes"] = std::move(axes);
    instr->attrs.attr_store["starts"] = std::move(starts);
    instr->attrs.attr_store["ends"] = std::move(ends);
    instr->attrs.attr_store["infer_flags"] = std::vector<int>{};
    instr->attrs.attr_store["strides"] = std::vector<int>{};
187 188 189 190 191 192 193 194 195 196
    instr->attrs.attr_store["decrease_axis"] = std::vector<int>{};
    input->LinkTo(instr.get());
    instr->LinkTo(output);
    graph_->RegisterNode(instr->id(), instr.get());
    InferShape(instr.get(), dtype_dict_, shape_dict_);
    output->source_node = instr;
    return output;
  }

  std::string node_name(std::string prefix) const {
197 198
    return std::move(
        prefix.append("__dot_merger_").append(std::to_string(idx_++)));
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
  }

  Node* matmul_op() const { return matmul_; }

 private:
  static int idx_;
  framework::Graph* graph_{};
  const std::string dot_type_;
  dtype_dict_t& dtype_dict_;
  shape_dict_t& shape_dict_;
  Node* matmul_{};
};

int DotBuilder::idx_ = 0;

class DotMergerPass {
 public:
  // Find the same input for matrix multiplication and recursively fuse.
  static int Apply(framework::Graph* graph, const std::string& dot_type) {
    int cnt{};
    // In the return map, the key is a shared variable, and the values
    // are the dot operators to be fused.
    auto clusters = GetClusters(graph, dot_type);
    std::set<Node*> nodes_to_remove;
    DotBuilder builder(graph, dot_type);
    for (auto& c : clusters) {
      auto& dots = c.second;
      for (size_t i = 0; i < dots.size(); ++i) {
        auto*& a = dots[i];
        if (!a) {
          VLOG(5) << "The node has been fused and removed, skipped.";
          continue;
        }
        std::vector<Node*> merge_nodes;
        merge_nodes.clear();
        merge_nodes.push_back(a);
        for (size_t j = i + 1; j < dots.size(); ++j) {
          auto* b = dots[j];
237 238
          if (!b || nodes_to_remove.count(a) || nodes_to_remove.count(b) ||
              accessible(a, b) || accessible(b, a)) {
239
            VLOG(5) << "Because nodes `" << a->id() << "` and `" << b->id()
240 241
                    << " have data dependencies or have been deleted, they "
                       "cannot be merged.";
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
            continue;
          }
          if (!is_merge(&builder, a, b)) {
            continue;
          }
          merge_nodes.push_back(dots[j]);
        }
        if (merge_nodes.size() < 2) {
          continue;
        }
        auto* merged = NewMergeDots(&builder, merge_nodes);
        cnt += 1;
        for (size_t j = 0; j < merge_nodes.size(); ++j) {
          nodes_to_remove.insert(dots[j]);
          if (j != 0) {
            dots[j] = nullptr;
          }
        }
        dots[i] = merged;
      }
    }

    for (auto* n : nodes_to_remove) {
      remove_node(graph, n);
    }
    return cnt;
  }

 private:
271 272
  static std::map<NodeData*, std::vector<Node*>> GetClusters(
      framework::Graph* graph, const std::string& op_type) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    std::map<NodeData*, std::vector<Node*>> clusters;
    auto nodes = std::get<0>(graph->topological_order());
    for (auto* n : nodes) {
      auto* op_node = n->safe_as<Node>();
      if (op_node && op_node->op()->name == op_type) {
        for (auto& edge : n->inlinks()) {
          auto* var_node = edge->source()->safe_as<NodeData>();
          CHECK(var_node) << "The variable node can not be null.";
          clusters[var_node].push_back(op_node);
        }
      }
    }
    std::vector<std::map<NodeData*, std::vector<Node*>>::iterator> del;
    for (auto it = clusters.begin(); it != clusters.end(); ++it) {
      // At least 2 operators are required to fuse.
      if (it->second.size() < 2) {
        del.push_back(it);
      }
    }
    for (auto& it : del) {
      clusters.erase(it);
    }
    VLOG(3) << "clusters size = " << clusters.size();
    return clusters;
  }

  static bool is_merge(DotBuilder* builder, Node* a, Node* b) {
    CHECK(a && b) << "The pointer of node is illegal.";
301 302 303 304 305 306 307 308
    const std::array<bool, 2> trans_a{get_attr<bool>(a, "trans_a", false),
                                      get_attr<bool>(b, "trans_a", false)};
    const std::array<bool, 2> trans_b{get_attr<bool>(a, "trans_b", false),
                                      get_attr<bool>(b, "trans_b", false)};
    const std::array<bool, 2> trans_out{get_attr<bool>(a, "trans_out", false),
                                        get_attr<bool>(b, "trans_out", false)};
    const std::array<float, 2> alpha{get_attr<float>(a, "alpha", 1.f),
                                     get_attr<float>(b, "alpha", 1.f)};
309 310 311 312 313 314
    if (!all_equal(trans_a, trans_b, trans_out, alpha)) {
      return false;
    }
    NodeData *shared_input{}, *input_a{}, *input_b{};
    if (input_operand(a, 1) == input_operand(b, 1)) {
      shared_input = input_operand(a, 1);
315 316
      input_a = input_operand(a, 0);
      input_b = input_operand(b, 0);
317 318
    } else if (input_operand(a, 0) == input_operand(b, 0)) {
      shared_input = input_operand(a, 0);
319 320
      input_a = input_operand(a, 1);
      input_b = input_operand(b, 1);
321 322 323
    } else {
      return false;
    }
324 325
    auto* output_a = output_operand(a, 0);
    auto* output_b = output_operand(b, 0);
326 327
    auto& graph_outs = builder->graph()->outputs;
    for (auto* n : {shared_input, input_a, input_b}) {
328 329
      if (std::find(graph_outs.begin(), graph_outs.end(), n) !=
          graph_outs.end()) {
330 331 332 333 334 335
        return false;
      }
    }
    return true;
  }

336 337 338 339 340 341 342 343 344 345 346
  static Node* NewMergeDots(DotBuilder* builder,
                            std::vector<Node*> merge_nodes) {
    const std::array<bool, 2> trans_a{
        get_attr<bool>(merge_nodes[0], "trans_a", false),
        get_attr<bool>(merge_nodes[1], "trans_a", false)};
    const std::array<bool, 2> trans_b{
        get_attr<bool>(merge_nodes[0], "trans_b", false),
        get_attr<bool>(merge_nodes[1], "trans_b", false)};
    const std::array<float, 2> alpha{
        get_attr<float>(merge_nodes[0], "alpha", 1.f),
        get_attr<float>(merge_nodes[1], "alpha", 1.f)};
347 348 349 350 351 352 353

    bool lhs{true};
    int axis{1};
    NodeData* shared_input = input_operand(merge_nodes[0], 0);

    if (input_operand(merge_nodes[0], 1) == input_operand(merge_nodes[1], 1)) {
      shared_input = input_operand(merge_nodes[0], 1);
354
      lhs = false;
355 356 357 358 359 360 361 362 363 364 365 366
      if (!trans_a[0]) {
        axis = 0;
      } else if (trans_b[0]) {
        axis = 0;
      }
    }
    CHECK(shared_input) << "The input node type must be variable.";
    std::vector<NodeData*> concat_nodes;
    concat_nodes.clear();
    auto shape_shared = builder->shape_dict().at(shared_input->id());
    concat_nodes.push_back(input_operand(merge_nodes[0], axis));
    for (size_t i = 1; i < merge_nodes.size(); ++i) {
367 368 369 370
      auto shape_a = builder->shape_dict().at(
          input_operand(merge_nodes[i - 1], axis)->id());
      auto shape_b =
          builder->shape_dict().at(input_operand(merge_nodes[i], axis)->id());
371
      CHECK_EQ(shape_a[1 - axis], shape_b[1 - axis])
372 373
          << "The shape of matmul is error. " << shape_a.size() << ", "
          << shape_b.size();
374 375 376 377 378
      concat_nodes.push_back(input_operand(merge_nodes[i], axis));
    }
    auto* concat_out = builder->Concat(axis, concat_nodes);
    NodeData* matmul_out{};
    if (!lhs) {
379 380
      matmul_out = builder->Matmul(
          trans_a[0], trans_b[0], false, alpha[0], concat_out, shared_input);
381
    } else {
382 383
      matmul_out = builder->Matmul(
          trans_a[0], trans_b[0], false, alpha[0], shared_input, concat_out);
384 385 386
    }
    auto start_shape = 0;
    for (size_t i = 0; i < concat_nodes.size(); ++i) {
387 388
      auto shape =
          builder->shape_dict().at(input_operand(merge_nodes[i], axis)->id());
389
      auto* output = output_operand(merge_nodes[i], 0);
390 391 392 393 394
      builder->Slice({axis},
                     {start_shape},
                     {start_shape + shape[axis]},
                     matmul_out,
                     output);
395 396 397 398 399 400 401
      start_shape += shape[axis];
    }
    return builder->matmul_op();
  }

  static Node* MergeDots(DotBuilder* builder, Node* a, Node* b) {
    CHECK(a && b) << "The pointer of node is illegal.";
402 403 404 405 406 407 408 409
    const std::array<bool, 2> trans_a{get_attr<bool>(a, "trans_a", false),
                                      get_attr<bool>(b, "trans_a", false)};
    const std::array<bool, 2> trans_b{get_attr<bool>(a, "trans_b", false),
                                      get_attr<bool>(b, "trans_b", false)};
    const std::array<bool, 2> trans_out{get_attr<bool>(a, "trans_out", false),
                                        get_attr<bool>(b, "trans_out", false)};
    const std::array<float, 2> alpha{get_attr<float>(a, "alpha", 1.f),
                                     get_attr<float>(b, "alpha", 1.f)};
410 411 412 413 414 415 416 417
    if (!all_equal(trans_a, trans_b, trans_out, alpha)) {
      return nullptr;
    }
    bool lhs{true};
    int axis{1};
    NodeData *shared_input{}, *input_a{}, *input_b{};
    if (input_operand(a, 1) == input_operand(b, 1)) {
      shared_input = input_operand(a, 1);
418 419 420
      input_a = input_operand(a, 0);
      input_b = input_operand(b, 0);
      lhs = false;
421 422 423 424 425 426 427
      if (!trans_a[0]) {
        axis = 0;
      } else if (trans_b[0]) {
        axis = 0;
      }
    } else if (input_operand(a, 0) == input_operand(b, 0)) {
      shared_input = input_operand(a, 0);
428 429
      input_a = input_operand(a, 1);
      input_b = input_operand(b, 1);
430 431 432
    } else {
      return nullptr;
    }
433 434
    auto* output_a = output_operand(a, 0);
    auto* output_b = output_operand(b, 0);
435 436
    auto& graph_outs = builder->graph()->outputs;
    for (auto* n : {shared_input, input_a, input_b}) {
437 438
      if (std::find(graph_outs.begin(), graph_outs.end(), n) !=
          graph_outs.end()) {
439 440 441
        return nullptr;
      }
    }
442 443
    CHECK(shared_input && input_a && input_b)
        << "The input node type must be variable.";
444
    auto shape_shared = builder->shape_dict().at(shared_input->id());
445 446
    auto shape_a = builder->shape_dict().at(input_a->id());
    auto shape_b = builder->shape_dict().at(input_b->id());
447
    CHECK_EQ(shape_a[1 - axis], shape_b[1 - axis])
448 449
        << "The shape of matmul is error. " << shape_a.size() << ", "
        << shape_b.size();
450 451 452
    auto* concat_out = builder->Concat(axis, {input_a, input_b});
    NodeData* matmul_out{};
    if (!lhs) {
453 454
      matmul_out = builder->Matmul(
          trans_a[0], trans_b[0], false, alpha[0], concat_out, shared_input);
455
    } else {
456 457
      matmul_out = builder->Matmul(
          trans_a[0], trans_b[0], false, alpha[0], shared_input, concat_out);
458 459
    }
    builder->Slice({axis}, {0}, {shape_a[axis]}, matmul_out, output_a);
460 461 462 463 464
    builder->Slice({axis},
                   {shape_a[axis]},
                   {shape_a[axis] + shape_b[axis]},
                   matmul_out,
                   output_b);
465 466 467 468 469 470 471 472 473 474
    return builder->matmul_op();
  }
};

}  // namespace

void DotMergerPassFunc(framework::Graph* graph) {
  // The cublas gemm is not yet supported.
  for (auto& dot_type : {"matmul", "cublas_matmul"}) {
    int n = DotMergerPass::Apply(graph, dot_type);
475 476
    VLOG(3) << "The fusion of `" << dot_type << "` was performed " << n
            << " times.";
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  }
}

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

CINN_REGISTER_HELPER(DotMerger) {
  CINN_REGISTER_PASS(DotMerger)
      .describe("")
      .set_change_structure(false)
      .provide_graph_attr("infershape")
      .provide_graph_attr("inferdtype")
      .set_body(cinn::hlir::pass::DotMergerPassFunc);
  return true;
}