value.h 7.5 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2021 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 <glog/logging.h>
#include <llvm/ADT/SmallVector.h>

#include <string>
#include <utility>
#include <vector>

#include "paddle/infrt/common/object.h"
#include "paddle/infrt/common/shared.h"
25
#include "paddle/infrt/dialect/infrt/common/types.h"
Y
Yan Chunwei 已提交
26
#include "paddle/infrt/host_context/function.h"
W
Wilber 已提交
27
#include "paddle/infrt/host_context/symbol_table.h"
Y
Yan Chunwei 已提交
28 29 30 31 32
#include "paddle/infrt/support/variant.h"
#include "paddle/infrt/tensor/dense_host_tensor.h"
#include "paddle/infrt/tensor/dense_tensor_view.h"
#include "paddle/infrt/tensor/tensor_map.h"
#include "paddle/infrt/tensor/tensor_shape.h"
33

34 35 36
#ifdef INFRT_WITH_PHI
#include "paddle/infrt/backends/host/phi_allocator.h"
#include "paddle/infrt/backends/host/phi_context.h"
37 38 39 40 41 42 43 44
#include "paddle/phi/backends/all_context.h"
#include "paddle/phi/common/backend.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/layout.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/scalar_array.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/meta_tensor.h"
W
Wilber 已提交
45 46 47 48 49 50 51 52 53

#ifdef INFRT_WITH_GPU
#include "paddle/phi/backends/gpu/gpu_context.h"
#endif  // INFRT_WITH_GPU
#ifdef INFRT_WITH_TRT
#include "paddle/infrt/backends/tensorrt/trt_engine.h"
#include "paddle/infrt/kernel/tensorrt/trt_kernels.h"
#endif  // INFRT_WITH_TRT
#endif  // INFRT_WITH_PHI
Y
Yan Chunwei 已提交
54 55 56 57

namespace infrt {
namespace host_context {

58 59
struct None {};

Y
Yan Chunwei 已提交
60 61
struct MlirFunctionExecutable;

W
Wilber 已提交
62
using ValueVariantType =
63 64
    Variant<None,
            int16_t,
W
Wilber 已提交
65 66 67 68 69 70 71 72 73 74 75 76
            int32_t,
            int64_t,
            float,
            double,
            bool,
            uint32_t,
            uint64_t,
            std::string,
            tensor::TensorShape,
            tensor::DenseHostTensor,
            MlirFunctionExecutable*,
            tensor::TensorMap,
77 78 79
            ::infrt::PrecisionType,
            ::infrt::LayoutType,
            ::infrt::TargetType,
80
#ifdef INFRT_WITH_PHI
81 82
            ::phi::MetaTensor,
            ::phi::DenseTensor,
83
            backends::CpuPhiContext,
W
Wilber 已提交
84 85 86 87
#ifdef INFRT_WITH_GPU
            backends::GpuPhiContext,
            ::phi::GPUContext,
#endif
88
            ::phi::CPUContext,
89
            std::vector<const phi::DenseTensor*>,
W
Wilber 已提交
90
            std::vector<phi::DenseTensor*>,
91 92
            paddle::experimental::ScalarBase<phi::DenseTensor>,
            paddle::experimental::ScalarArrayBase<phi::DenseTensor>,
93
            std::vector<phi::MetaTensor*>,
94
            phi::MetaConfig,
W
Wilber 已提交
95 96 97
            paddle::experimental::Backend,
            paddle::experimental::DataLayout,
            paddle::experimental::DataType,
W
Wilber 已提交
98 99 100 101
#ifdef INFRT_WITH_TRT
            ::infrt::backends::tensorrt::TrtEngine,
            ::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol,
#endif  // INFRT_WITH_TRT
102
#endif
W
Wilber 已提交
103 104 105 106 107
            std::vector<int16_t>,
            std::vector<int32_t>,
            std::vector<int64_t>,
            std::vector<float>,
            std::vector<double>>;
Y
Yan Chunwei 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

//! Copy content from \param from to \param to.
void CopyTo(const Value& from, Value* to);

/**
 * Represents any data type for value in host context.
 */
class Value : public common::Object {
 public:
  using variant_type = ValueVariantType;

  explicit Value() {}  // NOLINT
  explicit Value(int32_t x) : data(x) {}
  explicit Value(int64_t x) : data(x) {}
  explicit Value(float x) : data(x) {}
  explicit Value(double x) : data(x) {}
  explicit Value(bool x) : data(x) {}
125 126 127
  explicit Value(::infrt::TargetType x) : data(x) {}
  explicit Value(::infrt::LayoutType x) : data(x) {}
  explicit Value(::infrt::PrecisionType x) : data(x) {}
Y
Yan Chunwei 已提交
128 129 130 131 132 133 134 135 136 137
  explicit Value(std::string x) : data(x) {}
  explicit Value(tensor::TensorMap&& x) : data(x) {}
  explicit Value(std::vector<int16_t>&& x) : data(x) {}
  explicit Value(std::vector<int32_t>&& x) : data(x) {}
  explicit Value(std::vector<int64_t>&& x) : data(x) {}
  explicit Value(std::vector<float>&& x) : data(x) {}
  explicit Value(std::vector<double>&& x) : data(x) {}
  explicit Value(tensor::TensorShape&& x) : data(std::move(x)) {}
  explicit Value(tensor::DenseHostTensor&& x) : data(std::move(x)) {}
  explicit Value(MlirFunctionExecutable* x) : data(x) {}
138
#ifdef INFRT_WITH_PHI
139
  explicit Value(::phi::CPUContext&& x) : data(std::move(x)) {}
140
  explicit Value(backends::CpuPhiContext&& x) : data(std::move(x)) {}
W
Wilber 已提交
141 142 143 144
#ifdef INFRT_WITH_GPU
  explicit Value(::phi::GPUContext&& x) : data(std::move(x)) {}
  explicit Value(backends::GpuPhiContext&& x) : data(std::move(x)) {}
#endif
145 146
  explicit Value(::phi::DenseTensor&& x) : data(std::move(x)) {}
  explicit Value(::phi::MetaTensor&& x) : data(std::move(x)) {}
W
Wilber 已提交
147 148 149 150 151 152
#ifdef INFRT_WITH_TRT
  explicit Value(::infrt::backends::tensorrt::TrtEngine&& x)
      : data(std::move(x)) {}
  explicit Value(::infrt::kernel::tensorrt::MlirOperationWithInfrtSymbol x)
      : data(x) {}
#endif  // INFRT_WITH_TRT
153
#endif
Y
Yan Chunwei 已提交
154 155 156

  template <typename T>
  const T& get() const {
157 158
    CHECK(data.template is<T>()) << "typeid: " << data.index()
                                 << " != " << ValueVariantType::IndexOf<T>;
Y
Yan Chunwei 已提交
159 160
    return data.get<T>();
  }
161

Y
Yan Chunwei 已提交
162 163
  template <typename T>
  T& get() {
164 165
    CHECK(data.template is<T>()) << "typeid: " << data.index()
                                 << " != " << ValueVariantType::IndexOf<T>;
Y
Yan Chunwei 已提交
166 167 168
    return data.get<T>();
  }

169 170 171 172 173 174 175 176 177
  //! Get the value if assigned before or return a default value instead.
  template <class T>
  T& get_or_default() {
    if (!data.template is<T>()) {
      this->set(T{});
    }
    return get<T>();
  }

Y
Yan Chunwei 已提交
178 179 180 181 182 183 184 185 186
  template <typename T>
  void set(T&& v) {
    data = std::move(v);
  }

  void set(Value* v) { data = std::move(v->data); }

  bool valid() const { return true; }

187 188 189 190 191
  template <typename T>
  bool is_type() const {
    return data.template is<T>();
  }

Y
Yan Chunwei 已提交
192 193
  const char* type_info() const override;

194 195
  ValueVariantType::IndexT index() const { return data.index(); }

Y
Yan Chunwei 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  friend void CopyTo(const Value& from, Value* to);

 private:
  ValueVariantType data;
  static constexpr const char* __type_info__ = "host_context_value";
};

/**
 * Represents a counted reference of a Value.
 */
class ValueRef : common::Shared<Value> {
 public:
  ValueRef() = default;
  explicit ValueRef(Value* n) : common::Shared<Value>(n) {}
  explicit ValueRef(int32_t val);
  explicit ValueRef(int64_t val);
  explicit ValueRef(float val);
  explicit ValueRef(double val);
  explicit ValueRef(bool val);

  using common::Shared<Value>::get;
  using common::Shared<Value>::Reset;
  using common::Shared<Value>::operator->;
  using common::Shared<Value>::operator*;
220

Y
Yan Chunwei 已提交
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
  //! Get a readonly data.
  template <typename T>
  const T& get() const {
    CHECK(p_);
    return p_->get<T>();
  }

  template <typename T>
  T& get() {
    CHECK(p_);
    return p_->get<T>();
  }

  //! Assign a data.
  template <typename T>
  void Assign(const T& x) {
    if (!p_) {
      p_ = common::make_shared<Value>();
    }
    *p_ = x;
  }

  template <typename T, typename... Args>
  void Assign(Args... args) {
    p_ = common::make_shared<T>(std::forward<Args>(args)...);
  }

  inline bool IsValid() { return p_; }
};

}  // namespace host_context
}  // namespace infrt