meta_tensor.cc 3.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 15 16 17

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

#include "paddle/pten/core/meta_tensor.h"

#include "paddle/pten/core/dense_tensor.h"
18 19
#include "paddle/pten/core/enforce.h"
#include "paddle/pten/core/tensor_utils.h"
20 21 22 23 24 25 26 27 28 29 30 31 32

namespace pten {

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

void MetaTensor::set_dtype(DataType dtype) {
  if (pten::DenseTensor::classof(tensor_)) {
43
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))
44 45
        ->dtype = dtype;
  } else {
46
    PADDLE_THROW(pten::errors::Unimplemented(
47 48 49 50 51 52
        "Unsupported settting dtype for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::set_layout(DataLayout layout) {
  if (pten::DenseTensor::classof(tensor_)) {
53
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))
54 55
        ->layout = layout;
  } else {
56
    PADDLE_THROW(pten::errors::Unimplemented(
57 58 59 60 61 62
        "Unsupported settting layout for `%s`.", tensor_->type_info().name()));
  }
}

void MetaTensor::share_lod(const MetaTensor& meta_tensor) {
  if (pten::DenseTensor::classof(tensor_)) {
63 64
    DenseTensorUtils::GetMutableMeta(static_cast<DenseTensor*>(tensor_))->lod =
        meta_tensor.lod();
65
  } else {
66 67 68
    PADDLE_THROW(
        pten::errors::Unimplemented("Unsupported sharing lod inplace for `%s`.",
                                    tensor_->type_info().name()));
69 70 71 72 73 74 75
  }
}

const LoD& MetaTensor::lod() const {
  if (pten::DenseTensor::classof(tensor_)) {
    return static_cast<DenseTensor*>(tensor_)->lod();
  } else {
76 77 78 79 80 81 82 83 84 85 86 87 88 89
    PADDLE_THROW(pten::errors::Unimplemented("Unsupported getting lod of `%s`.",
                                             tensor_->type_info().name()));
  }
}

void MetaTensor::share_meta(const MetaTensor& meta_tensor) {
  if (pten::DenseTensor::classof(tensor_)) {
    set_dims(meta_tensor.dims());
    set_dtype(meta_tensor.dtype());
    set_layout(meta_tensor.layout());
    share_lod(meta_tensor);
  } else {
    PADDLE_THROW(pten::errors::Unimplemented(
        "Unsupported sharing meta for `%s`.", tensor_->type_info().name()));
90 91 92 93
  }
}

}  // namespace pten