tensor_base.h 3.8 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17
#include <memory>
L
liuruilong 已提交
18
#include "common/enforce.h"
19
#include "common/type_define.h"
L
liuruilong 已提交
20 21 22 23 24 25 26 27 28 29 30
#include "common/types.h"
#include "framework/ddim.h"

namespace paddle_mobile {
namespace framework {

template <typename... T>
struct SizeOfTypeFunctor;

template <typename T>
struct SizeOfTypeFunctor<T> {
31 32
  size_t operator()(const kTypeId_t type) const {
    if (type_id<T>().hash_code() == type) {
L
liuruilong 已提交
33 34 35 36 37 38 39 40 41
      return sizeof(T);
    } else {
      return 0UL;
    }
  }
};

template <>
struct SizeOfTypeFunctor<> {
42
  size_t operator()(const kTypeId_t type) const { return 0UL; }
L
liuruilong 已提交
43 44 45 46
};

template <typename HEAD, typename... TAIL>
struct SizeOfTypeFunctor<HEAD, TAIL...> {
47
  size_t operator()(const kTypeId_t type) const {
L
liuruilong 已提交
48 49 50 51 52 53 54 55 56 57
    SizeOfTypeFunctor<HEAD> head;
    size_t head_size = head(type);
    if (head_size != 0) {
      return head_size;
    }
    SizeOfTypeFunctor<TAIL...> tail;
    return tail(type);
  }
};

58
static inline size_t SizeOfType(const kTypeId_t type) {
59 60
  SizeOfTypeFunctor<int8_t, uint8_t, int, half, float, double, int16_t, int64_t,
                    bool, size_t>
L
liuruilong 已提交
61 62 63
      functor;
  size_t size = functor(type);

64
  PADDLE_MOBILE_ENFORCE(size != 0UL, "Cannot get size of type %d", type);
L
liuruilong 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  return size;
}

class TensorBase {
 public:
  virtual inline TensorBase &Resize(const DDim &dims) = 0;

  inline bool IsInitialized() const { return holder_ != nullptr; }

  /*! Return the dimensions of the memory block. */
  inline const DDim &dims() const { return dims_; }

  /*! Return the numel of the memory block. */
  inline int64_t numel() const { return product(dims_); }

80
  kTypeId_t type() const {
L
liuruilong 已提交
81 82 83 84 85 86 87 88 89 90 91 92
    PADDLE_MOBILE_ENFORCE(
        holder_ != nullptr,
        "Tensor not initialized yet when Tensor::type() is called.")
    return holder_->type();
  }

  // memory size returns the holding memory size in byte.
  size_t memory_size() const {
    return holder_ == nullptr ? 0UL : holder_->size() - offset_;
  }

  inline void check_memory_size() const {
93 94 95
#ifdef PADDLE_MOBILE_FPGA
    return;
#endif
L
liuruilong 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    PADDLE_MOBILE_ENFORCE(
        holder_ != nullptr,
        "Tensor holds no memory. Call Tensor::mutable_data first.");
    PADDLE_MOBILE_ENFORCE(numel() * SizeOfType(type()) <= memory_size(),
                          "Tensor's dims_ is out of bound. ");
  }

 protected:
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a
   * template
   *          parameter of Variable.
   */
  struct Placeholder {
    virtual ~Placeholder() = default;

    virtual void *ptr() const = 0;

    virtual size_t size() const = 0;

116
    virtual kTypeId_t type() const = 0;
L
liuruilong 已提交
117

118
    virtual void set_type(kTypeId_t type) = 0;
H
hjchen2 已提交
119 120

    virtual void resize(size_t size) = 0;
121 122

    virtual void realloc(size_t size) = 0;
L
liuruilong 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  };

  /**
   * @brief points to elements dimensions.
   *
   * @note dims_ do not indicate the memory block size.
   */

  DDim dims_;

  /*! holds the memory block if allocated. */
  std::shared_ptr<Placeholder> holder_;

  /**
   * @brief   A PlaceHolder may be shared by more than one tensor.
   *
   * @note    Some of them may be slices of the others. So the offset_
   *          is introduced here to indicate the byte offset between
   *          PlaceHolder::ptr_ and where the tensor data really
   * begins.
   */
  size_t offset_ = 0;
};

}  // namespace framework
}  // namespace paddle_mobile