meta_tensor.cc 2.7 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 36 37 38 39 40 41 42
  } else {
    PADDLE_THROW(paddle::platform::errors::Unimplemented(
        "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 46 47 48 49 50 51 52
        ->dtype = dtype;
  } else {
    PADDLE_THROW(paddle::platform::errors::Unimplemented(
        "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 56 57 58 59 60 61 62
        ->layout = layout;
  } else {
    PADDLE_THROW(paddle::platform::errors::Unimplemented(
        "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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  } else {
    PADDLE_THROW(paddle::platform::errors::Unimplemented(
        "Unsupported share lod inplace for `%s`.",
        tensor_->type_info().name()));
  }
}

const LoD& MetaTensor::lod() const {
  if (pten::DenseTensor::classof(tensor_)) {
    return static_cast<DenseTensor*>(tensor_)->lod();
  } else {
    PADDLE_THROW(paddle::platform::errors::Unimplemented(
        "Unsupported setting dims for `%s`.", tensor_->type_info().name()));
  }
}

}  // namespace pten