meta_tensor.cc 5.5 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

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

15
#include "paddle/phi/core/meta_tensor.h"
16

17 18 19
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/selected_rows.h"
J
Jack Zhou 已提交
20 21
#include "paddle/phi/core/string_tensor.h"
#include "paddle/phi/core/string_tensor_utils.h"
22
#include "paddle/phi/core/tensor_utils.h"
23

24
namespace phi {
25 26 27 28 29 30 31 32 33 34

int64_t MetaTensor::numel() const { return tensor_->numel(); }

DDim MetaTensor::dims() const { return tensor_->dims(); }

DataType MetaTensor::dtype() const { return tensor_->dtype(); }

DataLayout MetaTensor::layout() const { return tensor_->layout(); }

void MetaTensor::set_dims(const DDim& dims) {
35
  if (phi::DenseTensor::classof(tensor_)) {
36 37
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))->dims =
        dims;
J
Jack Zhou 已提交
38 39 40
  } else if (phi::StringTensor::classof(tensor_)) {
    StringTensorUtils::GetMutableMeta(static_cast<StringTensor*>(tensor_))
        ->dims = dims;
41
  } else if (phi::SelectedRows::classof(tensor_)) {
42 43 44
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->dims = dims;
45
  } else {
46
    PADDLE_THROW(phi::errors::Unimplemented(
47 48 49 50 51
        "Unsupported setting dims for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::set_dtype(DataType dtype) {
52
  if (phi::DenseTensor::classof(tensor_)) {
53
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))
54
        ->dtype = dtype;
J
Jack Zhou 已提交
55 56
  } else if (phi::StringTensor::classof(tensor_)) {
    // No need to set dtype
57
  } else if (phi::SelectedRows::classof(tensor_)) {
58 59 60
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->dtype = dtype;
61
  } else {
62
    PADDLE_THROW(phi::errors::Unimplemented(
63 64 65 66 67
        "Unsupported settting dtype for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::set_layout(DataLayout layout) {
68
  if (phi::DenseTensor::classof(tensor_)) {
69
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))
70
        ->layout = layout;
J
Jack Zhou 已提交
71 72
  } else if (phi::StringTensor::classof(tensor_)) {
    // No need to set layout
73
  } else if (phi::SelectedRows::classof(tensor_)) {
74 75 76
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->layout = layout;
77
  } else {
78
    PADDLE_THROW(phi::errors::Unimplemented(
79 80 81 82 83
        "Unsupported settting layout for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::share_lod(const MetaTensor& meta_tensor) {
H
hong 已提交
84 85 86 87
  if (meta_tensor.lod().size() == 0) {
    // no need share
    return;
  }
88
  if (phi::DenseTensor::classof(tensor_)) {
89 90
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))->lod =
        meta_tensor.lod();
91
  } else if (phi::SelectedRows::classof(tensor_)) {
92 93 94
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->lod = meta_tensor.lod();
95
  } else {
96
    PADDLE_THROW(
97 98
        phi::errors::Unimplemented("Unsupported sharing lod inplace for `%s`.",
                                   tensor_->type_info().name()));
99 100 101 102
  }
}

const LoD& MetaTensor::lod() const {
103
  if (phi::DenseTensor::classof(tensor_)) {
104
    return static_cast<DenseTensor*>(tensor_)->lod();
105
  } else if (phi::SelectedRows::classof(tensor_)) {
106
    return static_cast<SelectedRows*>(tensor_)->value().lod();
107
  } else {
108 109
    PADDLE_THROW(phi::errors::Unimplemented("Unsupported getting lod of `%s`.",
                                            tensor_->type_info().name()));
110 111 112 113
  }
}

void MetaTensor::share_meta(const MetaTensor& meta_tensor) {
Y
YuanRisheng 已提交
114 115 116
  if (phi::DenseTensor::classof(tensor_) ||
      phi::SelectedRows::classof(tensor_)) {
    share_dims(meta_tensor);
117 118 119
    set_dtype(meta_tensor.dtype());
    set_layout(meta_tensor.layout());
    share_lod(meta_tensor);
120
  } else {
121
    PADDLE_THROW(phi::errors::Unimplemented(
122
        "Unsupported sharing meta for `%s`.", tensor_->type_info().name()));
123 124 125
  }
}

C
Chen Weihang 已提交
126
TensorBase* MetaTensor::tensor() const { return tensor_; }
Y
YuanRisheng 已提交
127 128 129 130 131 132 133

void MetaTensor::share_dims(const MetaTensor& meta_tensor) {
  bool is_dense_tensor = phi::DenseTensor::classof(tensor_);
  bool is_selected_rows = phi::SelectedRows::classof(tensor_);
  if (is_dense_tensor || is_selected_rows) {
    set_dims(meta_tensor.dims());
    if (is_selected_rows) {
C
Chen Weihang 已提交
134
      const auto in_tensor_base = meta_tensor.tensor();
Y
YuanRisheng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      PADDLE_ENFORCE_EQ(
          phi::SelectedRows::classof(in_tensor_base),
          true,
          errors::InvalidArgument("The input MetaTensor is SelectedRows, but "
                                  "the output MetaTensor is not this type."));
      auto* selected_rows_out = static_cast<SelectedRows*>(tensor_);
      auto* selected_rows_in = static_cast<SelectedRows*>(in_tensor_base);
      selected_rows_out->set_rows(selected_rows_in->rows());
      selected_rows_out->set_height(selected_rows_in->height());
    }
  } else {
    PADDLE_THROW(phi::errors::Unimplemented(
        "Unsupported sharing dims for `%s`.", tensor_->type_info().name()));
  }
}

151 152
bool MetaTensor::initialized() const { return tensor_ != nullptr; }

153
}  // namespace phi