meta_tensor.cc 5.1 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 20
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/selected_rows.h"
#include "paddle/phi/core/tensor_utils.h"
21

22
namespace phi {
23 24 25 26 27 28 29 30 31 32

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) {
33
  if (phi::DenseTensor::classof(tensor_)) {
34 35
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))->dims =
        dims;
36
  } else if (phi::SelectedRows::classof(tensor_)) {
37 38 39
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->dims = dims;
40
  } else {
41
    PADDLE_THROW(phi::errors::Unimplemented(
42 43 44 45 46
        "Unsupported setting dims for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::set_dtype(DataType dtype) {
47
  if (phi::DenseTensor::classof(tensor_)) {
48
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))
49
        ->dtype = dtype;
50
  } else if (phi::SelectedRows::classof(tensor_)) {
51 52 53
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->dtype = dtype;
54
  } else {
55
    PADDLE_THROW(phi::errors::Unimplemented(
56 57 58 59 60
        "Unsupported settting dtype for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::set_layout(DataLayout layout) {
61
  if (phi::DenseTensor::classof(tensor_)) {
62
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))
63
        ->layout = layout;
64
  } else if (phi::SelectedRows::classof(tensor_)) {
65 66 67
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->layout = layout;
68
  } else {
69
    PADDLE_THROW(phi::errors::Unimplemented(
70 71 72 73 74
        "Unsupported settting layout for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::share_lod(const MetaTensor& meta_tensor) {
H
hong 已提交
75 76 77 78
  if (meta_tensor.lod().size() == 0) {
    // no need share
    return;
  }
79
  if (phi::DenseTensor::classof(tensor_)) {
80 81
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))->lod =
        meta_tensor.lod();
82
  } else if (phi::SelectedRows::classof(tensor_)) {
83 84 85
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->lod = meta_tensor.lod();
86
  } else {
87
    PADDLE_THROW(
88 89
        phi::errors::Unimplemented("Unsupported sharing lod inplace for `%s`.",
                                   tensor_->type_info().name()));
90 91 92 93
  }
}

const LoD& MetaTensor::lod() const {
94
  if (phi::DenseTensor::classof(tensor_)) {
95
    return static_cast<DenseTensor*>(tensor_)->lod();
96
  } else if (phi::SelectedRows::classof(tensor_)) {
97
    return static_cast<SelectedRows*>(tensor_)->value().lod();
98
  } else {
99 100
    PADDLE_THROW(phi::errors::Unimplemented("Unsupported getting lod of `%s`.",
                                            tensor_->type_info().name()));
101 102 103 104
  }
}

void MetaTensor::share_meta(const MetaTensor& meta_tensor) {
Y
YuanRisheng 已提交
105 106 107
  if (phi::DenseTensor::classof(tensor_) ||
      phi::SelectedRows::classof(tensor_)) {
    share_dims(meta_tensor);
108 109 110
    set_dtype(meta_tensor.dtype());
    set_layout(meta_tensor.layout());
    share_lod(meta_tensor);
111
  } else {
112
    PADDLE_THROW(phi::errors::Unimplemented(
113
        "Unsupported sharing meta for `%s`.", tensor_->type_info().name()));
114 115 116
  }
}

C
Chen Weihang 已提交
117
TensorBase* MetaTensor::tensor() const { return tensor_; }
Y
YuanRisheng 已提交
118 119 120 121 122 123 124

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 已提交
125
      const auto in_tensor_base = meta_tensor.tensor();
Y
YuanRisheng 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      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()));
  }
}

142
}  // namespace phi