meta_tensor.cc 5.0 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) {
75
  if (phi::DenseTensor::classof(tensor_)) {
76 77
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))->lod =
        meta_tensor.lod();
78
  } else if (phi::SelectedRows::classof(tensor_)) {
79 80 81
    DenseTensorUtils::GetMutableMeta(
        static_cast<SelectedRows*>(tensor_)->mutable_value())
        ->lod = meta_tensor.lod();
82
  } else {
83
    PADDLE_THROW(
84 85
        phi::errors::Unimplemented("Unsupported sharing lod inplace for `%s`.",
                                   tensor_->type_info().name()));
86 87 88 89
  }
}

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

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

Y
YuanRisheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
TensorBase* MetaTensor::get_tensor() const { return tensor_; }

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) {
      const auto in_tensor_base = meta_tensor.get_tensor();
      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()));
  }
}

138
}  // namespace phi