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 <string>
L
liuruilong 已提交
18 19 20 21 22 23 24 25 26 27 28 29
#include "common/enforce.h"
#include "common/types.h"
#include "framework/ddim.h"

namespace paddle_mobile {
namespace framework {

template <typename... T>
struct SizeOfTypeFunctor;

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

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

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

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

63 64
  PADDLE_MOBILE_ENFORCE(size != 0UL, "Cannot get size of type %s",
                        type.c_str());
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
  std::string 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 std::string type() const = 0;
L
liuruilong 已提交
117

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

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

  /**
   * @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