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

#include "paddle/pten/core/dense_tensor.h"

17 18 19 20 21
// See Note [ Why still include the fluid headers? ]
#include "paddle/fluid/platform/bfloat16.h"
#include "paddle/fluid/platform/complex.h"
#include "paddle/fluid/platform/float16.h"

22 23 24 25 26
namespace pten {

DenseTensor::DenseTensor(const std::shared_ptr<Allocator>& a,
                         const DenseTensorMeta& meta)
    : meta_(meta),
27
      storage_(make_intrusive<TensorStorage>(a, SizeOf(dtype()) * numel())) {}
28 29 30 31

DenseTensor::DenseTensor(const std::shared_ptr<Allocator>& a,
                         DenseTensorMeta&& meta)
    : meta_(std::move(meta)),
32
      storage_(make_intrusive<TensorStorage>(a, SizeOf(dtype()) * numel())) {}
33 34 35 36 37 38 39 40

DenseTensor::DenseTensor(intrusive_ptr<Storage> storage,
                         const DenseTensorMeta& meta)
    : meta_(meta), storage_(std::move(storage)) {}

DenseTensor::DenseTensor(intrusive_ptr<Storage> storage, DenseTensorMeta&& meta)
    : meta_(std::move(meta)), storage_(std::move(storage)) {}

41 42 43
DenseTensor::DenseTensor(const DenseTensor& other)
    : meta_(other.meta()), storage_(copy_intrusive(other.storage_)) {}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
int64_t DenseTensor::numel() const {
  if (meta_.is_scalar) {
    return 1;
  }
  return product(meta_.dims);
}

bool DenseTensor::IsSharedWith(const DenseTensor& b) const {
  return storage_.get() == b.storage_.get() && storage_.get() != nullptr;
}

void* DenseTensor::mutable_data(size_t request_bytes) {
  PADDLE_ENFORCE(
      valid(),
      paddle::platform::errors::PreconditionNotMet(
          "The meta data must be valid when call the mutable data function."));
  PADDLE_ENFORCE_NOT_NULL(
      storage_,
      paddle::platform::errors::PreconditionNotMet(
          "The storage must be valid when call the mutable data function."));
64
  size_t bytes = numel() * SizeOf(dtype());
65 66 67 68 69 70 71 72 73 74
  if (request_bytes) {
    PADDLE_ENFORCE_GE(request_bytes,
                      bytes,
                      paddle::platform::errors::InvalidArgument(
                          "The reserved size %d should be enough to meet the "
                          "volume required by metadata %d.",
                          request_bytes,
                          bytes));
    bytes = request_bytes;
  }
75
  if (storage_->size() < bytes + meta_.offset || storage_->size() == 0) {
76 77
    VLOG(10) << "mutbale data realloc, original size: " << storage_->size()
             << ", new size: " << bytes;
78
    storage_->Realloc(bytes);
79
    meta_.offset = 0;
80
  }
81 82
  return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(storage_->data()) +
                                 meta_.offset);
83 84 85 86
}

template <typename T>
T* DenseTensor::mutable_data() {
87 88 89
  // In order to be compatible with the original Tensor design and
  // execution system, we have to reset the datatype in mutable_data<T>.
  // When the compatibility phase is over in the future, we can delete it
90
  if (meta_.dtype == DataType::UNDEFINED) {
91 92
    VLOG(10) << "change data type in mutbale_data, target dtype - "
             << paddle::experimental::CppTypeToDataType<T>::Type();
93
    const_cast<DataType&>(meta_.dtype) =
94 95
        paddle::experimental::CppTypeToDataType<T>::Type();
  }
96
  PADDLE_ENFORCE(
97
      (dtype() == paddle::experimental::CppTypeToDataType<T>::Type()),
98
      paddle::platform::errors::InvalidArgument(
99 100 101
          "The type of data (%d) we are trying to retrieve does not match the "
          "type of data currently contained in the container (%d).",
          static_cast<int>(paddle::experimental::CppTypeToDataType<T>::Type()),
102
          static_cast<int>(dtype())));
103 104 105 106 107 108
  return static_cast<T*>(mutable_data());
}

template <typename T>
const T* DenseTensor::data() const {
  PADDLE_ENFORCE(
109
      (dtype() == paddle::experimental::CppTypeToDataType<T>::Type()),
110
      paddle::platform::errors::InvalidArgument(
111 112 113 114 115 116 117 118 119 120
          "The type of data we are trying to retrieve does not match the "
          "type of data currently contained in the container."));
  return static_cast<const T*>(data());
}

const void* DenseTensor::data() const {
  PADDLE_ENFORCE_NOT_NULL(
      storage_,
      paddle::platform::errors::PreconditionNotMet(
          "The storage must be valid when call the mutable data function."));
121 122
  return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(storage_->data()) +
                                 meta_.offset);
123 124
}

125 126 127 128 129 130
void DenseTensor::set_meta(DenseTensorMeta&& meta) {
  PADDLE_ENFORCE(!meta_.valid(),
                 paddle::platform::errors::InvalidArgument(
                     "Only when the original attribute of Tensor is "
                     "incomplete, can it be reset."));
  meta_ = std::move(meta);
石晓伟 已提交
131 132
}

133
void DenseTensor::Resize(const DDim& dims) {
石晓伟 已提交
134
  meta_.dims = dims;
135
  mutable_data();
石晓伟 已提交
136 137
}

138 139
void DenseTensor::ResetLoD(const LoD& lod) { meta_.lod = lod; }

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
#define DATA_MEMBER_FUNC_INSTANTIATION(dtype)  \
  template dtype* DenseTensor::mutable_data(); \
  template const dtype* DenseTensor::data() const;

DATA_MEMBER_FUNC_INSTANTIATION(bool);
DATA_MEMBER_FUNC_INSTANTIATION(int8_t);
DATA_MEMBER_FUNC_INSTANTIATION(uint8_t);
DATA_MEMBER_FUNC_INSTANTIATION(int16_t);
DATA_MEMBER_FUNC_INSTANTIATION(uint16_t);
DATA_MEMBER_FUNC_INSTANTIATION(int32_t);
DATA_MEMBER_FUNC_INSTANTIATION(uint32_t);
DATA_MEMBER_FUNC_INSTANTIATION(int64_t);
DATA_MEMBER_FUNC_INSTANTIATION(uint64_t);
DATA_MEMBER_FUNC_INSTANTIATION(::paddle::platform::bfloat16);
DATA_MEMBER_FUNC_INSTANTIATION(::paddle::platform::float16);
DATA_MEMBER_FUNC_INSTANTIATION(float);
DATA_MEMBER_FUNC_INSTANTIATION(double);
DATA_MEMBER_FUNC_INSTANTIATION(::paddle::experimental::complex64);
DATA_MEMBER_FUNC_INSTANTIATION(::paddle::experimental::complex128);

#undef DATA_MEMBER_FUNC_INSTANTIATION

}  // namespace pten