tensor_impl.h 3.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
liaogang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
Y
yuyang18 已提交
16
#include "paddle/fluid/framework/data_type.h"
Y
Yi Wang 已提交
17 18
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/platform/enforce.h"
K
kexinzhao 已提交
19
#include "paddle/fluid/platform/float16.h"
L
liaogang 已提交
20 21 22 23 24

namespace paddle {
namespace framework {
template <typename T>
inline const T* Tensor::data() const {
25
  check_memory_size();
26
  bool valid =
27
      std::is_same<T, void>::value || type_ == DataTypeTrait<T>::DataType();
28 29 30 31 32 33
  PADDLE_ENFORCE_EQ(
      valid, true,
      platform::errors::InvalidArgument(
          "Tensor holds the wrong type, it holds %s, but desires to be %s.",
          DataTypeToString(type_),
          DataTypeToString(DataTypeTrait<T>::DataType())));
34

L
liaogang 已提交
35 36 37 38
  return reinterpret_cast<const T*>(
      reinterpret_cast<uintptr_t>(holder_->ptr()) + offset_);
}

39 40
inline bool Tensor::IsInitialized() const { return holder_ != nullptr; }

L
liaogang 已提交
41 42
template <typename T>
inline T* Tensor::data() {
43
  check_memory_size();
44
  bool valid =
45
      std::is_same<T, void>::value || type_ == DataTypeTrait<T>::DataType();
46 47 48 49 50 51 52
  PADDLE_ENFORCE_EQ(
      valid, true,
      platform::errors::InvalidArgument(
          "Tensor holds the wrong type, it holds %s, but desires to be %s",
          DataTypeToString(type_),
          DataTypeToString(DataTypeTrait<T>::DataType())));

L
liaogang 已提交
53 54 55 56 57
  return reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(holder_->ptr()) +
                              offset_);
}

template <typename T>
58
inline T* Tensor::mutable_data(const DDim& dims, const platform::Place& place,
59
                               size_t requested_size) {
60
  static_assert(std::is_pod<T>::value, "T must be POD");
L
liaogang 已提交
61
  Resize(dims);
62
  return mutable_data<T>(place, requested_size);
L
liaogang 已提交
63 64 65
}

template <typename T>
66 67
inline T* Tensor::mutable_data(const platform::Place& place,
                               size_t requested_size) {
68
  static_assert(std::is_pod<T>::value, "T must be POD");
Y
Yu Yang 已提交
69
  return reinterpret_cast<T*>(
70
      mutable_data(place, DataTypeTrait<T>::DataType(), requested_size));
71 72
}

F
fengjiayi 已提交
73
inline Tensor ReshapeToMatrix(const Tensor& src, int num_col_dims) {
F
fengjiayi 已提交
74 75
  int rank = src.dims().size();
  PADDLE_ENFORCE_GE(
76 77 78 79 80 81
      rank, 2, platform::errors::InvalidArgument(
                   "'ReshapeToMatrix()' is only used for flatten high rank "
                   "tensors to matrixs. The dimensions of Tensor must be "
                   "greater or equal than 2. "
                   "But received dimensions of Tensor is %d",
                   rank));
F
fengjiayi 已提交
82 83 84
  if (rank == 2) {
    return src;
  }
F
WIP  
fengjiayi 已提交
85
  Tensor res;
86
  res.ShareDataWith(src);
F
fengjiayi 已提交
87
  res.Resize(flatten_to_2d(src.dims(), num_col_dims));
F
WIP  
fengjiayi 已提交
88 89 90
  return res;
}

L
liaogang 已提交
91 92
}  // namespace framework
}  // namespace paddle