meta_tensor.cc 4.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) {
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) {
101
  if (phi::DenseTensor::classof(tensor_)) {
102 103 104 105
    set_dims(meta_tensor.dims());
    set_dtype(meta_tensor.dtype());
    set_layout(meta_tensor.layout());
    share_lod(meta_tensor);
106
  } else if (phi::SelectedRows::classof(tensor_)) {
107 108 109 110
    set_dims(meta_tensor.dims());
    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
  }
}

117
}  // namespace phi