value.h 6.1 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 25 26 27 28 29 30
// 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"
#include "paddle/infrt/host_context/function.h"
#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"
31 32 33 34 35 36 37 38
#include "paddle/pten/core/meta_tensor.h"

#ifdef INFRT_WITH_PTEN
#include "paddle/infrt/backends/host/pten_allocator.h"
#include "paddle/infrt/backends/host/pten_context.h"
#include "paddle/pten/backends/cpu/cpu_context.h"
#include "paddle/pten/core/dense_tensor.h"
#endif
Y
Yan Chunwei 已提交
39 40 41 42 43 44 45 46 47 48 49 50

namespace infrt {
namespace host_context {

struct MlirFunctionExecutable;

using ValueVariantType = Variant<int16_t,
                                 int32_t,
                                 int64_t,
                                 float,
                                 double,
                                 bool,
51 52
                                 uint32_t,
                                 uint64_t,
Y
Yan Chunwei 已提交
53 54 55 56 57
                                 std::string,
                                 tensor::TensorShape,
                                 tensor::DenseHostTensor,
                                 MlirFunctionExecutable*,
                                 tensor::TensorMap,
58 59 60 61 62 63 64
#ifdef INFRT_WITH_PTEN
                                 ::pten::MetaTensor,
                                 ::pten::DenseTensor,
                                 backends::CpuPtenAllocator,
                                 backends::CpuPtenContext,
                                 ::pten::CPUContext,
#endif
Y
Yan Chunwei 已提交
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
                                 std::vector<int16_t>,
                                 std::vector<int32_t>,
                                 std::vector<int64_t>,
                                 std::vector<float>,
                                 std::vector<double>>;

//! 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) {}
  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) {}
97 98 99 100 101 102 103
#ifdef INFRT_WITH_PTEN
  explicit Value(backends::CpuPtenContext&& x) : data(std::move(x)) {}
  explicit Value(::pten::CPUContext&& x) : data(std::move(x)) {}
  explicit Value(::pten::DenseTensor&& x) : data(std::move(x)) {}
  explicit Value(::pten::MetaTensor&& x) : data(std::move(x)) {}
  explicit Value(backends::CpuPtenAllocator&& x) : data(std::move(x)) {}
#endif
Y
Yan Chunwei 已提交
104 105 106

  template <typename T>
  const T& get() const {
107
    CHECK(data.template is<T>());
Y
Yan Chunwei 已提交
108 109
    return data.get<T>();
  }
110

Y
Yan Chunwei 已提交
111 112
  template <typename T>
  T& get() {
113
    CHECK(data.template is<T>());
Y
Yan Chunwei 已提交
114 115 116
    return data.get<T>();
  }

117 118 119 120 121 122 123 124 125
  //! 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 已提交
126 127 128 129 130 131 132 133 134
  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; }

135 136 137 138 139
  template <typename T>
  bool is_type() const {
    return data.template is<T>();
  }

Y
Yan Chunwei 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
  const char* type_info() const override;

  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);
161 162 163 164
  explicit ValueRef(::pten::MetaTensor&& val);
  explicit ValueRef(backends::CpuPtenContext&& x);
  explicit ValueRef(::pten::CPUContext&& x);
  explicit ValueRef(::pten::DenseTensor&& x);
Y
Yan Chunwei 已提交
165 166 167 168 169

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

Y
Yan Chunwei 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  //! 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