tensor_meta.cc 1.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

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/tensor_meta.h"

namespace pten {

19 20
DenseTensorMeta::DenseTensorMeta(DataType dtype, const DDim& dims)
    : dims(dims), dtype(dtype) {}
21

22
DenseTensorMeta::DenseTensorMeta(DataType dtype,
23 24
                                 const DDim& dims,
                                 DataLayout layout)
25
    : dims(dims), dtype(dtype), layout(layout) {}
26

27
DenseTensorMeta::DenseTensorMeta(DataType dtype,
28 29 30
                                 const DDim& dims,
                                 DataLayout layout,
                                 const std::vector<std::vector<size_t>>& lod)
31
    : dims(dims), dtype(dtype), layout(layout), lod(lod) {}
32 33 34

bool DenseTensorMeta::valid() const noexcept {
  bool valid{true};
35
  valid = valid && (dtype != DataType::UNDEFINED);
36 37 38 39 40 41 42 43
  valid = valid && (layout != DataLayout::UNDEFINED);
  valid = valid && (is_scalar || product(dims) >= 0);
  return valid;
}

bool operator==(const DenseTensorMeta& lhs, const DenseTensorMeta& rhs) {
  bool ret = true;
  return ret && (lhs.is_scalar == rhs.is_scalar) && (lhs.dims == rhs.dims) &&
44
         (lhs.dtype == rhs.dtype) && (lhs.layout == rhs.layout) &&
45 46 47
         (lhs.lod == rhs.lod) && (lhs.offset == rhs.offset);
}
}  // namespace pten