graph.h 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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

17
#include <map>
18 19 20 21 22 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "lite/core/op_lite.h"
#include "lite/core/tensor.h"
#include "rknpu/rknpu_pub.h"

namespace paddle {
namespace lite {
namespace subgraph {
namespace rknpu {

// Graph and node is defined to collect all of converted RKNPU IR nodes
struct QuantizationInfo {
  int enable_int8;
  int quant_bits;
  std::vector<float> scale;
};

class Node {
 public:
  enum class Role {
    kVar = 0,
    kConst,
    kData,
  };

  Node(std::shared_ptr<rk::nn::Tensor> data,
       PrecisionType precision,
       DataLayoutType layout,
       Role role)
      : data_(data), precision_(precision), layout_(layout), role_(role) {}
  Node(PrecisionType precision, DataLayoutType layout, Role role)
      : precision_(precision), layout_(layout), role_(role) {}

  void set_data(std::shared_ptr<rk::nn::Tensor> data) { data_ = data; }
  void set_precision(PrecisionType precision) { precision_ = precision; }
  void set_layout(DataLayoutType layout) { layout_ = layout; }
  void set_role(Role role) { role_ = role; }
  void set_quant_param(const QuantizationInfo& qnt) { qnt_ = qnt; }

  std::shared_ptr<rk::nn::Tensor> data() { return data_; }
  PrecisionType precision() const { return precision_; }
  DataLayoutType layout() const { return layout_; }
  Role role() const { return role_; }
  bool is_var() const { return role_ == Role::kVar; }
  bool is_const() const { return role_ == Role::kConst; }
  bool is_data() const { return role_ == Role::kData; }

 private:
  std::shared_ptr<rk::nn::Tensor> data_{nullptr};
  PrecisionType precision_{PRECISION(kFloat)};
  DataLayoutType layout_{DATALAYOUT(kNCHW)};
  Role role_{Role::kVar};
  QuantizationInfo qnt_;
};

class Graph {
 public:
  Graph();
  ~Graph();

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

  // Const or data node
  std::shared_ptr<Node> Add(const std::string& name,
                            const Tensor& tensor,
                            std::vector<int64_t> shape,
                            PrecisionType precision = PRECISION(kUnk),
                            DataLayoutType layout = DATALAYOUT(kNCHW),
                            const QuantizationInfo& qnt = QuantizationInfo());
  std::shared_ptr<Node> Get(const std::string& name) {
    CHECK(Has(name)) << "[RKNPU] Node " << name << " not found.";
    return nodes_.at(name).back();
  }

  std::shared_ptr<Node> Add(const std::string& name,
                            const Tensor& tensor,
                            PrecisionType precision = PRECISION(kUnk),
                            DataLayoutType layout = DATALAYOUT(kNCHW),
                            const QuantizationInfo& qnt = QuantizationInfo()) {
    return Add(name, tensor, tensor.dims().Vectorize(), precision, layout, qnt);
  }

  // Data node
  std::shared_ptr<Node> Add(const std::string& name,
                            std::vector<int64_t> shape,
                            PrecisionType precision = PRECISION(kFloat),
                            DataLayoutType layout = DATALAYOUT(kNCHW),
                            const QuantizationInfo& qnt = QuantizationInfo());

  std::shared_ptr<Node> Add(const std::string& name,
                            DDim dims,
                            PrecisionType precision = PRECISION(kFloat),
                            DataLayoutType layout = DATALAYOUT(kNCHW),
                            const QuantizationInfo& qnt = QuantizationInfo()) {
    return Add(name, dims.Vectorize(), precision, layout, qnt);
  }

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

  rk::nn::Graph* GetHandle() { return rgraph_; }

 private:
126
  std::map<std::string, std::vector<std::shared_ptr<Node>>> nodes_;
127 128 129 130 131 132 133
  rk::nn::Graph* rgraph_;
};

}  // namespace rknpu
}  // namespace subgraph
}  // namespace lite
}  // namespace paddle