graph_fuse.cc 15.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.
 */

20 21 22 23 24
/*!
 *  Copyright (c) 2017 by Contributors
 * \file graph_fuse.cc
 * \brief Fuse the operators together.
 */
25 26
#include <dmlc/parameter.h>
#include <nnvm/compiler/packed_func_ext.h>
27
#include <nnvm/graph.h>
28
#include <nnvm/graph_attr_types.h>
29
#include <nnvm/node.h>
30 31
#include <nnvm/op_attr_types.h>
#include <nnvm/pass.h>
32
#include <nnvm/pass_functions.h>
33
#include <nnvm/tuple.h>
34
#include <tvm/lowered_func.h>
35
#include <tvm/runtime/packed_func.h>
36 37
#include <memory>
#include <utility>
38
#include <limits>
39
#include <unordered_map>
40

41 42 43
#include "graph_fuse.h"
#include "graph_runtime.h"
#include "pattern_util.h"
44 45 46 47 48 49 50

namespace nnvm {
namespace compiler {
using namespace tvm;

// Partition the graph into segments
// Each segment will be compiled into one operator.
51 52
// Also mark the property of the segment.
nnvm::Graph GraphFindFusibleGroups(nnvm::Graph g) {
53
  const IndexedGraph& idx = g.indexed_graph();
54 55 56 57 58
  int opt_level = 2;
  if (g.attrs.count("opt_level") != 0) {
    opt_level = g.MoveCopyAttr<int>("opt_level");
  }

59 60 61 62 63
  // Get attributes from the graph
  const ShapeVector& shape_vec = g.GetAttr<ShapeVector>("shape");

  // Reference counter of each op node
  // For now, always store result when an op is referred more than once.
64
  std::vector<uint32_t> ref_count = GetNodeRefCounts(idx);
65 66
  for (const auto& e : idx.outputs()) {
    // this line will realize all the outputs
67
    ref_count[e.node_id] += 1;
68
  }
69
  // Pattern for the subgraph
70
  PatternVec pattern_vec(idx.num_nodes(),  kOpaque);
71 72 73 74 75 76 77 78 79 80 81 82
  // Whether node can be fused to parent.
  std::vector<FuseRule> fuse_vec(idx.num_nodes(), FuseRule::kUknown);
  // Master node id of fusion segment.
  std::vector<int> master_vec(idx.num_nodes(), -1);
  // Operator pattern
  static auto& op_pattern = nnvm::Op::GetAttr<TOpPattern>("TOpPattern");

  for (uint32_t nid = 0; nid < idx.num_nodes(); ++nid) {
    const auto& inode = idx[nid];
    if (inode.source->is_variable()) {
      fuse_vec[nid] = FuseRule::kRealize; continue;
    }
83
    TOpPattern pt = op_pattern.get(inode.source->op(), kOpaque);
84 85

    if (pt <= kBroadcast) {
86
      // Check if we can fuse to the master.
87 88
      int chosen_master = -1;
      bool ewise = inode.source->num_outputs() == 1;
89
      bool mark_as_injective = false;
90 91 92 93
      for (const auto& e : inode.inputs) {
        if (fuse_vec[e.node_id] == FuseRule::kUknown) {
          TOpPattern ipt = pattern_vec[e.node_id];
          if (ipt != kElemWise) ewise = false;
94 95 96
          if (ipt <= kBroadcast) {
            fuse_vec[e.node_id] = FuseRule::kFuseToMaster;
          } else if (ipt == kInjective) {
97
            fuse_vec[e.node_id] = FuseRule::kFuseToMaster;
98
            mark_as_injective = true;
99 100 101
          } else if (ipt == kOutEWiseFusable &&
                     chosen_master == -1 &&
                     shape_vec[idx.entry_id(nid, 0)] == shape_vec[idx.entry_id(e)]) {
102 103 104 105 106 107 108 109 110 111 112 113 114 115
            chosen_master = master_vec[e.node_id];
            fuse_vec[e.node_id] = FuseRule::kFuseToMaster;
          } else {
            fuse_vec[e.node_id] = FuseRule::kRealize;
          }
        }
        if (ewise) {
          if (shape_vec[idx.entry_id(nid, 0)] != shape_vec[idx.entry_id(e)]) {
            ewise = false;
          }
        }
      }
      master_vec[nid] = chosen_master;
      if (chosen_master != -1) {
116
        pt = kOutEWiseFusable;
117 118
      } else if (mark_as_injective) {
        pt = kInjective;
119 120 121
      } else {
        pt = ewise ? kElemWise : kBroadcast;
      }
122
    } else if (pt == kInjective || pt == kCommReduce) {
123
      // Fuse to the comm reduce or injective
124 125 126 127 128 129 130 131 132 133 134 135 136
      for (const auto& e : inode.inputs) {
        if (fuse_vec[e.node_id] == FuseRule::kUknown) {
          TOpPattern ipt = pattern_vec[e.node_id];
          if (ipt <= kInjective) {
            fuse_vec[e.node_id] = FuseRule::kFuseToMaster;
          } else {
            fuse_vec[e.node_id] = FuseRule::kRealize;
          }
        }
      }
      if (pt == kCommReduce) {
        master_vec[nid] = nid;
      }
137
    } else {
138
      // Realize
139 140 141 142 143 144 145 146 147 148 149 150
      master_vec[nid] = nid;
      for (const auto& e : inode.inputs) {
        if (fuse_vec[e.node_id] == FuseRule::kUknown) {
          fuse_vec[e.node_id] = FuseRule::kRealize;
          if (master_vec[e.node_id] == -1) {
            master_vec[e.node_id] = e.node_id;
          }
        }
      }
    }

    pattern_vec[nid] = pt;
151
    if (ref_count[nid] > 1 || opt_level < 1) {
152 153 154 155 156 157 158
      fuse_vec[nid] = FuseRule::kRealize;
      if (master_vec[nid] == -1) {
        master_vec[nid] = nid;
      }
    }
  }

159 160
  // Point to the group root id of each node.
  GroupVec group_vec(idx.num_nodes(), -1);
161
  std::vector<std::vector<uint32_t> > node_ids_per_group(idx.num_nodes());
162 163 164
  for (uint32_t i = idx.num_nodes(); i != 0; --i) {
    uint32_t nid = i - 1;
    const auto& inode = idx[nid];
165
    bool is_root = false;
166 167
    if (group_vec[nid] == -1) {
      group_vec[nid] = nid;
168 169
      node_ids_per_group[nid].push_back(nid);
      is_root = true;
170
    }
171 172 173 174 175

    // Check if injective op and out_ewise_fusable op (e.g. conv2d) are in the same group.
    bool parent_out_ewise = false;
    bool parent_injective = false;
    for (const auto& e : inode.inputs) {
176
      if (fuse_vec[e.node_id] != FuseRule::kFuseToMaster) continue;
177 178 179 180 181 182 183 184
      TOpPattern pt = pattern_vec[e.node_id];
      if (pt == kOutEWiseFusable) {
        parent_out_ewise = true;
      } else if (pt == kInjective) {
        parent_injective = true;
      }
    }
    // Change the master node from out_ewise_fusable op to itself
185 186 187 188 189 190 191 192 193
    if (parent_injective && parent_out_ewise) {
      master_vec[nid] = nid;
      if (!is_root) {
        // Children nodes in the same group might be pointing to a master node in a different group.
        for (uint32_t j : node_ids_per_group[group_vec[nid]]) {
          master_vec[j] = nid;
        }
      }
    }
194

195
    // Propagate the group id.
196
    for (const auto& e : inode.inputs) {
197 198 199 200 201 202 203 204
      TOpPattern pt = pattern_vec[e.node_id];
      if (parent_out_ewise && parent_injective) {
        if (pt == kOutEWiseFusable) {
          continue;  // Do not fuse out_ewise_fusable op
        } else if (pt == kInjective) {
          master_vec[e.node_id] = nid;
        }
      }
205 206 207 208
      if (fuse_vec[e.node_id] == FuseRule::kFuseToMaster) {
        CHECK(group_vec[e.node_id] == -1||
              group_vec[e.node_id] == group_vec[nid]);
        group_vec[e.node_id] = group_vec[nid];
209
        node_ids_per_group[group_vec[nid]].push_back(e.node_id);
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 237 238 239 240 241 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 271 272 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 301 302 303 304 305 306 307

  /*
     Above algorithm will not fuse a node whose output is fed to more than one
     child node. This is because in general, it does not make sense to fuse multiple
     children branches with their parent, as in the following example.

            conv2d
            /  |  \
           /   |   \
         op    op   op
          |    |    |
          |    |    |

     However, when all children branches meet at a certain node, there is a possibility for
     further operator fusion. For example, all nodes in the following subgraph can be fused
     into a single node, if three 'in-between' nodes and the bottom node are all element wise
     operation.

            conv2d
            /  |  \
           /   |   \
         op    op   op
          \    |    /
           \   |   /
          elemwise add
               |

     This pattern is not uncommon. For example, it arises when conv2d op is followed by exponential
     linear unit. If bias add and batch normalization are also present, they can be fused as well.

     In fact, above fusion algorithm already fuses three in-between nodes and the element wise
     add node in the figure above. The following code fuses the conv2d node with the already
     fused children nodes. The following patterns are supported.

     * Any number of child nodes from the top node
     * The path from the top node to bottom node can contain any number of element wise ops.

     The only restriction is that in-between nodes cannot have more than one child.

     The overview of the algorithm below is as follows:

     1. Check if all children nodes are fused into a single op by the existing fusion algorithm
     2. Fuse the parent node to children nodes, and update its group id to be the children's group id
     3. If the parent node originally belongs to another group (for example, conv + batch norm),
        propagate the new group id to a grand parent and upward
  */
  if (opt_level >= 1) {
    std::vector<std::vector<uint32_t> > children_group_ids(idx.num_nodes());
    for (uint32_t nid = idx.num_nodes() - 1; nid != 0; --nid) {
      const auto& inode = idx[nid];
      if (inode.source->is_variable()) continue;
      CHECK_NE(group_vec[nid], -1);
      if (inode.inputs.size() != 1) continue;
      const uint32_t parent_nid = inode.inputs[0].node_id;
      // if parent node has more than one child, record each child's group id.
      if (ref_count[parent_nid] > 1) children_group_ids[parent_nid].push_back(group_vec[nid]);
    }

    std::vector<int> new_group_id(idx.num_nodes(), -1);
    for (uint32_t nid = idx.num_nodes() - 1; nid != 0; --nid) {
      if (new_group_id[group_vec[nid]] != -1) {
        // propagate new group id from child
        group_vec[nid] = new_group_id[group_vec[nid]];
      }
      TOpPattern pt = op_pattern.get(idx[nid].source->op(), kOpaque);
      if (pt == kOpaque) continue;
      const auto& group_ids = children_group_ids[nid];
      if (group_ids.size() <= 1) continue;
      const uint32_t child_group_id = group_ids[0];
      const auto& children_node_ids = node_ids_per_group[child_group_id];

      auto is_same_group_id = [child_group_id](uint32_t id) {
          return id == child_group_id;
      };
      auto is_fusible_pattern = [&idx](uint32_t child_nid) {
        TOpPattern child_pt = op_pattern.get(idx[child_nid].source->op(), kOpaque);
        return child_pt  <= kBroadcast;
      };
      // fuse this node with children if
      // all children belong to the same group and
      // all nodes in the group are element wise or broadcast op.
      const bool can_be_fused = std::all_of(group_ids.begin(), group_ids.end(), is_same_group_id) &&
        std::all_of(children_node_ids.begin(), children_node_ids.end(), is_fusible_pattern);

      if (can_be_fused) {
        new_group_id[group_vec[nid]] = child_group_id;
        group_vec[nid] = child_group_id;
        for (uint32_t nid2 : node_ids_per_group[child_group_id]) {
          pattern_vec[nid2] = pattern_vec[nid];
          master_vec[nid2] = master_vec[nid];
        }
      }
    }
  }

308 309 310 311 312 313
  g.attrs["group_root"] = std::make_shared<any>(std::move(group_vec));
  g.attrs["group_master"] = std::make_shared<any>(std::move(master_vec));
  g.attrs["pattern"] = std::make_shared<any>(std::move(pattern_vec));
  return g;
}

314 315
NNVM_REGISTER_PASS(GraphFindFusibleGroups)
.set_body(GraphFindFusibleGroups)
316
.depend_graph_attr("shape")
317
.depend_graph_attr("dtype");
318 319

// Fuse the partitioned graph into segments.
320 321 322 323 324 325
// Create a new graph with fused nodes.
// Also inherit attribute shape, dltype from the previous graph.
nnvm::Graph GraphFuse(nnvm::Graph g) {
  CHECK(g.HasAttr("group_root") && g.HasAttr("pattern"))
      << "GraphFindFusibleGroups pass hasn't been applied yet.";

326 327 328 329
  const IndexedGraph& idx = g.indexed_graph();
  // Get attributes from the graph
  const ShapeVector& shape_vec = g.GetAttr<ShapeVector>("shape");
  const DTypeVector& dtype_vec = g.GetAttr<DTypeVector>("dtype");
330 331
  const GroupVec& group_vec = g.GetAttr<GroupVec>("group_root");
  const PatternVec& pattern_vec = g.GetAttr<PatternVec>("pattern");
332

333
  // Specially handle assign op.
334
  const nnvm::Op* assign_op = nnvm::Op::Get("_assign");
335

336 337
  FuseEntryVec fuse_entries(idx.num_nodes());
  // Setup inputs and placeholder.
338 339 340 341 342
  for (uint32_t nid = 0; nid < idx.num_nodes(); ++nid) {
    const auto& inode = idx[nid];
    if (inode.source->is_variable()) continue;
    CHECK_GE(group_vec[nid], 0);
    int root_id = group_vec[nid];
343
    FuseEntry& fe = fuse_entries[root_id];
344 345
    fe.flatten_data = (pattern_vec[root_id] == kElemWise ||
                       inode.source->op() == assign_op);
346 347 348
    for (const auto& e : inode.inputs) {
      if (group_vec[e.node_id] != root_id && fe.imap.count(e) == 0) {
        Array<Expr> shape;
349
        if (fe.flatten_data) {
350
          // Elementwise support flatten
351 352 353 354 355 356 357 358 359 360 361 362 363
          int64_t prod = 1;
          for (int64_t x : shape_vec[idx.entry_id(e)]) {
            prod *= x;
          }
          CHECK_LE(prod, static_cast<int64_t>(std::numeric_limits<int>::max()));
          shape.push_back(make_const(Int(32), prod));
        } else {
          for (int64_t x : shape_vec[idx.entry_id(e)]) {
            CHECK_LE(x, static_cast<int64_t>(std::numeric_limits<int>::max()));
            shape.push_back(make_const(Int(32), x));
          }
        }
        std::ostringstream os_name;
364
        os_name << "input" << fe.imap.size();
365
        Tensor data = placeholder(
366
            shape, TVMType2Type(GetDLType(dtype_vec[idx.entry_id(e)])),
367
            os_name.str());
368 369 370 371
        NodeEntry garg = Symbol::CreateVariable(os_name.str()).outputs[0];
        fe.imap[e] = garg;
        fe.reverse_imap[garg.node.get()] = e;
        fe.input_info[garg.node.get()] = std::move(data);
372 373 374
      }
    }
  }
375

376 377
  // Setup the Subgraph
  std::vector<NodeEntry> subgraph_vec(idx.num_node_entries());
378 379 380 381
  for (uint32_t nid = 0; nid < idx.num_nodes(); ++nid) {
    const auto& inode = idx[nid];
    if (inode.source->is_variable()) continue;
    int root_id = group_vec[nid];
382 383
    FuseEntry& fe = fuse_entries[root_id];
    // Create a subgraph node.
384 385
    NodePtr gnode = Node::Create();
    gnode->attrs = inode.source->attrs;
386
    // Set input entries for the subgraph node.
387 388 389 390
    for (const auto& e : inode.inputs) {
      if (group_vec[e.node_id] != root_id) {
        auto it = fe.imap.find(e);
        CHECK(it != fe.imap.end());
391
        gnode->inputs.push_back(it->second);
392
      } else {
393 394 395 396
        const NodeEntry& ne = subgraph_vec[idx.entry_id(e)];
        CHECK(!idx[e.node_id].source->is_variable());
        CHECK(ne.node != nullptr);
        gnode->inputs.push_back(ne);
397 398
      }
    }
399
    // Schedule on the root node and use the master's schedule
400
    if (static_cast<int>(nid) != root_id) {
401 402
      for (uint32_t index = 0; index < inode.source->num_outputs(); ++index) {
        uint32_t eid = idx.entry_id(nid, index);
403
        subgraph_vec[eid] = NodeEntry{gnode, index, 0};
404 405
      }
    } else {
406 407 408
      for (uint32_t index = 0; index < inode.source->num_outputs(); ++index) {
        fe.subgraph.outputs.push_back(NodeEntry{gnode, index, 0});
      }
409 410
    }
  }
411 412
  g.attrs["fused_entry"] = std::make_shared<any>(std::move(fuse_entries));
  return g;
413 414
}

415 416 417 418 419 420 421 422
NNVM_REGISTER_PASS(GraphFuse)
    .set_body(GraphFuse)
    .set_change_graph(true)
    .provide_graph_attr("fused_entry")
    .depend_graph_attr("shape")
    .depend_graph_attr("dtype")
    .depend_graph_attr("group_root")
    .depend_graph_attr("group_master");
423

424 425
}  // namespace compiler
}  // namespace nnvm