graph.h 3.0 KB
Newer Older
H
hong19860320 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright (c) 2019 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.

#pragma once

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
22
#include "lite/backends/apu/neuron_adapter.h"
H
hong19860320 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 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 61 62 63 64 65 66 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#include "lite/core/op_lite.h"
#include "lite/core/tensor.h"

namespace paddle {
namespace lite {
namespace subgraph {
namespace apu {

// Graph and node is defined to collect all of converted HiAI IR nodes
class Node {
 public:
  Node(int32_t operand_idx, std::vector<uint32_t> shape)
      : idx_(operand_idx), shape_(shape) {}

  void set_shape(std::vector<uint32_t> shape) { shape_ = shape; }

  uint32_t index() { return idx_; }
  std::vector<uint32_t> shape() const { return shape_; }
  void set_data(std::shared_ptr<Tensor> data) { data_ = data; }

 private:
  int32_t idx_;
  std::vector<uint32_t> shape_;
  std::shared_ptr<Tensor> data_{nullptr};
};

class Graph {
 public:
  int Add(const std::string& name, std::shared_ptr<Node> node);

  // Variable, const or data node
  std::shared_ptr<Node> Add(const std::string& name,
                            std::vector<uint32_t> shape) {
    CHECK(shape.size()) << name << " : " << shape.size();
    auto node = std::make_shared<Node>(operandIdx_, shape);
    auto idx = Add(name, node);
    CHECK_GE(idx, 1);

    return node;
  }

  void set_model(NeuronModel* model) { model_ = model; }
  NeuronModel* model() { return model_; }

  void set_input_names(const std::vector<std::string> input_names) {
    input_names_ = input_names;
  }

  bool IsInput(const std::string& name) {
    for (int i = 0; i < input_names_.size(); i++) {
      if (input_names_[i] == name) return true;
    }
    return false;
  }

  bool IsOutput(const std::string& name) {
    for (int i = 0; i < output_names_.size(); i++) {
      if (output_names_[i] == name) return true;
    }
    return false;
  }

  void set_output_names(const std::vector<std::string> output_names) {
    output_names_ = output_names;
  }

  std::shared_ptr<Node> Get(std::string name) {
    CHECK(Has(name)) << "[APU] Node " << name << " not found.";
    return nodes_.at(name).back();
  }

  bool Has(const std::string& name) {
    return nodes_.find(name) != nodes_.end();
  }

 private:
  NeuronModel* model_;
  std::unordered_map<std::string, std::vector<std::shared_ptr<Node>>> nodes_;
  int32_t operandIdx_ = 0;
  std::vector<std::string> input_names_;
  std::vector<std::string> output_names_;
};

}  // namespace apu
}  // namespace subgraph
}  // namespace lite
}  // namespace paddle