node.cpp 8.0 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 PaddlePaddle 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. */

L
liuruilong 已提交
15
#include "framework/program/program-optimize/node.h"
E
eclipsess 已提交
16
#include <algorithm>
17 18
#include <map>
#include <memory>
L
liuruilong 已提交
19
#include "framework/operator.h"
L
liuruilong 已提交
20 21 22 23 24

namespace paddle_mobile {

namespace framework {

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
std::vector<Node *> Node::operator[](int index) {
  std::vector<Node *> nodes;
  GetNodesWithLocation(index, 0, &nodes);
  return nodes;
}

void Node::GetNodesWithLocation(int index, int now_index,
                                std::vector<Node *> *nodes) {
  if (index == now_index) {
    nodes->push_back(this);
  }

  for (int i = 0; i < this->outputs_.size(); ++i) {
    this->outputs_[i]->GetNodesWithLocation(index, now_index + 1, nodes);
  }
}

L
liuruilong 已提交
42 43 44 45 46 47 48 49 50 51
Node &Node::operator>(std::shared_ptr<Node> node) {
  outputs_.push_back(node);
  node->inputs_.push_back(this);
  return *node;
}

bool Node::operator==(const Node &in) {
  if (in.type_ == this->type_) {
    if (this->outputs_.size() == in.outputs_.size()) {
      for (int i = 0; i < outputs_.size(); ++i) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        if (!(this->outputs_[i]->MedianEqual(*in.outputs_[i]))) {
          return false;
        }
      }
    } else {
      return false;
    }
  } else {
    return false;
  }
  return true;
}

bool Node::MedianEqual(const Node &in) {
  if (in.type_ == this->type_) {
    if (this->outputs_.size() == in.outputs_.size()) {
      //      if (this->inputs_.size() != in.inputs_.size()) {
      //        DLOG << " == - this input size: " << this->inputs_.size();
      //        DLOG << " == - ptr of this " << this;
      //        DLOG << " == - in input size: " << in.inputs_.size();
      //        DLOG << " == - input size not equal ";
      //        return false;
      //      } else {
      //        for (int i = 0; i < this->inputs_.size(); ++i) {
      //          if (this->inputs_[i]->type_ != in.inputs_[i]->type_) {
      //            DLOG << " == - input type not equal ";
      //            return false;
      //          }
      //        }
      //      }

      for (int i = 0; i < outputs_.size(); ++i) {
        if (!((*outputs_[i]).MedianEqual(*in.outputs_[i]))) {
L
liuruilong 已提交
85 86 87 88
          return false;
        }
      }
    } else {
89
      //      DLOG << " == - output size not equal ";
L
liuruilong 已提交
90 91 92
      return false;
    }
  } else {
93
    //    DLOG << " == - median type is not equal ";
L
liuruilong 已提交
94 95 96 97 98
    return false;
  }
  return true;
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
std::map<std::string, Node *> Node::Relationship() {
  std::map<std::string, Node *> map;
  RelationshipPrivate(&map);
  return map;
}

void Node::RelationshipPrivate(std::map<std::string, Node *> *map) {
  for (auto output : op_desc_->outputs_) {
    for (auto output_key : output.second) {
      (*map)[output_key] = this;
    }
  }
  for (auto output : this->outputs_) {
    output->RelationshipPrivate(map);
  }
}

L
liuruilong 已提交
116 117 118 119 120 121 122
std::shared_ptr<Node> Node::To(int size) {
  std::shared_ptr<Node> node = std::make_shared<Node>();
  this->To(size - 1, node);
  return node;
}

void Node::To(int index, std::shared_ptr<Node> node) {
123
  node->op_desc_ = this->op_desc_;
L
liuruilong 已提交
124
  node->type_ = this->type_;
125
  node->inputs_ = this->inputs_;
L
liuruilong 已提交
126 127 128 129 130 131 132 133 134 135 136 137
  if (index != 0) {
  } else {
    return;
  }

  for (int j = 0; j < this->outputs_.size(); ++j) {
    std::shared_ptr<Node> sub_node = std::make_shared<Node>();
    node->outputs_.push_back(sub_node);
    outputs_[j]->To(index - 1, sub_node);
  }
}

L
liuruilong 已提交
138 139
int Node::Depth(int begin) {
  int depth = 0;
L
liuruilong 已提交
140 141
  begin++;
  for (int i = 0; i < outputs_.size(); ++i) {
L
liuruilong 已提交
142
    int output_depth = outputs_[i]->Depth(begin);
L
liuruilong 已提交
143 144 145 146 147 148
    depth = output_depth > depth ? output_depth : depth;
  }
  return begin > depth ? begin : depth;
}

Node &Node::Folder(
L
liuruilong 已提交
149
    int size, std::string type,
E
eclipsess 已提交
150 151
    std::map<std::string, std::vector<std::pair<std::string, std::string>>>
        change,
L
liuruilong 已提交
152
    std::vector<std::shared_ptr<Node>> *removed_nodes) {
L
liuruilong 已提交
153 154 155 156
  std::shared_ptr<framework::OpDesc> op_desc =
      std::make_shared<framework::OpDesc>();
  op_desc->inputs_ = this->op_desc_->inputs_;
  std::vector<std::shared_ptr<Node>> outputs;
L
liuruilong 已提交
157
  this->Folder(op_desc, &outputs, size - 1, &change, this, removed_nodes);
L
liuruilong 已提交
158 159 160 161 162 163 164 165 166
  this->outputs_ = outputs;
  this->type_ = type;
  this->op_desc_ = op_desc;
  this->op_desc_->type_ = type;
  return *this;
}

void Node::Folder(
    std::shared_ptr<framework::OpDesc> op_desc,
L
liuruilong 已提交
167
    std::vector<std::shared_ptr<Node>> *outputs, int index,
E
eclipsess 已提交
168 169
    std::map<std::string, std::vector<std::pair<std::string, std::string>>>
        *change,
L
liuruilong 已提交
170
    Node *begin_node, std::vector<std::shared_ptr<Node>> *removed_nodes) {
L
liuruilong 已提交
171
  if (change->find(this->type_) != change->end()) {
E
eclipsess 已提交
172 173
    auto change_pairs = (*change)[this->type_];
    for (const auto &change_pair : change_pairs) {
174 175 176 177 178 179 180 181 182 183 184 185 186
      std::map<std::string, int> f;
      if (this->op_desc_->GetInputs().find(change_pair.first) !=
          this->op_desc_->GetInputs().end()) {
        if (op_desc->GetInputs().find(change_pair.second) !=
            op_desc->GetInputs().end()) {
          for (auto value : this->op_desc_->GetInputs()[change_pair.first]) {
            op_desc->GetInputs()[change_pair.second].push_back(value);
          }
        } else {
          op_desc->GetInputs()[change_pair.second] =
              this->op_desc_->GetInputs()[change_pair.first];
        }
      }
E
eclipsess 已提交
187
    }
L
liuruilong 已提交
188 189 190 191 192 193 194
  }

  for (auto &attr_pair : this->op_desc_->attrs_) {
    op_desc->attrs_.emplace(attr_pair.first, attr_pair.second);
  }
  if (index > 0) {
    --index;
195

L
liuruilong 已提交
196
    for (auto output : outputs_) {
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
      if (change->find(this->type_) != change->end()) {
        auto change_pairs = (*change)[this->type_];
        for (const auto &change_pair : change_pairs) {
          std::map<std::string, int> f;
          if (this->op_desc_->GetOutputs().find(change_pair.first) !=
              this->op_desc_->GetOutputs().end()) {
            if (op_desc->GetInputs().find(change_pair.second) !=
                op_desc->GetInputs().end()) {
              for (auto value :
                   this->op_desc_->GetOutputs()[change_pair.first]) {
                op_desc->GetInputs()[change_pair.second].push_back(value);
              }
            } else {
              op_desc->GetInputs()[change_pair.second] =
                  this->op_desc_->GetOutputs()[change_pair.first];
            }
          }
        }
      }

L
liuruilong 已提交
217
      removed_nodes->push_back(output);
L
liuruilong 已提交
218 219
      output->Folder(op_desc, outputs, index, change, begin_node,
                     removed_nodes);
L
liuruilong 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    }
  } else {
    for (auto &op_output : this->op_desc_->outputs_) {
      op_desc->outputs_.emplace(op_output.first, op_output.second);
    }

    for (auto &output : this->outputs_) {
      auto iter =
          std::find(output->inputs_.begin(), output->inputs_.end(), this);

      if (iter != output->inputs_.end()) {
        output->inputs_.erase(iter);
      }
      output->inputs_.push_back(begin_node);
      outputs->push_back(output);
    }
  }
}
W
wangliu 已提交
238
#ifdef PADDLE_MOBILE_DEBUG
L
liuruilong 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
std::string Node::ToString(std::string blank, const Node *node) const {
  std::stringstream ss;
  ss << type_ << "-> \n";

  if (inputs_.size() > 1 && node != inputs_.back()) {
    return ss.str();
  } else if (inputs_.size() > 1 && node == inputs_.back()) {
    ss << "\n" << blank << type_ << "\n";
  }

  for (int i = 0; i < outputs_.size(); ++i) {
    ss << blank << outputs_[i]->ToString(blank + "  ", this) << "";
  }
  return ss.str();
}

std::string Node::ToString() const { return this->ToString("  ", this); }

L
liuruilong 已提交
257 258 259 260 261 262 263 264 265 266 267 268
void Node::Description() {
  if (op_desc_.get()) {
    DLOG << *op_desc_;
  } else {
    DLOG << " null ";
  }
}

Print &operator<<(Print &printer, const Node &node) {
  printer << node.ToString();
  return printer;
}
W
wangliu 已提交
269
#endif
L
liuruilong 已提交
270 271 272

}  // namespace framework
}  // namespace paddle_mobile