From 1e9b3a3d489f8639ca99d1fc27daba9eab453f94 Mon Sep 17 00:00:00 2001 From: zyfncg Date: Wed, 17 Nov 2021 12:17:28 +0800 Subject: [PATCH] rename TensorBase interface data_type() to dtype() (#37257) --- paddle/pten/api/lib/tensor.cc | 4 ++-- paddle/pten/api/lib/utils/tensor_utils.cc | 2 +- paddle/pten/core/compat_utils.h | 2 +- paddle/pten/core/dense_tensor.cc | 16 +++++++--------- paddle/pten/core/dense_tensor.h | 2 +- paddle/pten/core/tensor_base.h | 2 +- paddle/pten/kernels/cpu/utils.cc | 4 ++-- paddle/pten/kernels/cuda/math.cu | 2 +- paddle/pten/kernels/cuda/utils.cu | 4 ++-- paddle/pten/kernels/xpu/utils.cc | 4 ++-- paddle/pten/tests/api/test_tensor_utils.cc | 11 +++++------ paddle/pten/tests/core/test_dense_tensor.cc | 2 +- 12 files changed, 26 insertions(+), 29 deletions(-) diff --git a/paddle/pten/api/lib/tensor.cc b/paddle/pten/api/lib/tensor.cc index 5d10c7209e..bb3fba8858 100644 --- a/paddle/pten/api/lib/tensor.cc +++ b/paddle/pten/api/lib/tensor.cc @@ -109,9 +109,9 @@ void Tensor::reshape(const std::vector &shape) { "and it will be implemented by calling the reshape kernel later.")); } -DataType Tensor::dtype() const { return impl_->data_type(); } +DataType Tensor::dtype() const { return impl_->dtype(); } -DataType Tensor::type() const { return impl_->data_type(); } +DataType Tensor::type() const { return impl_->dtype(); } DataLayout Tensor::layout() const { return impl_->layout(); } diff --git a/paddle/pten/api/lib/utils/tensor_utils.cc b/paddle/pten/api/lib/utils/tensor_utils.cc index 2c362a11b1..b02392e576 100644 --- a/paddle/pten/api/lib/utils/tensor_utils.cc +++ b/paddle/pten/api/lib/utils/tensor_utils.cc @@ -125,7 +125,7 @@ void MovesStorage(pten::DenseTensor* src, paddle::framework::Tensor* dst) { auto storage = src->release(); std::shared_ptr holder( new TensorStorage(std::move(storage))); - dst->ResetHolderWithType(holder, pten::TransToProtoVarType(src->data_type())); + dst->ResetHolderWithType(holder, pten::TransToProtoVarType(src->dtype())); } void MovesStorage(pten::DenseTensor* src, paddle::framework::LoDTensor* dst) { diff --git a/paddle/pten/core/compat_utils.h b/paddle/pten/core/compat_utils.h index dc65e04b3a..a0602f33e3 100644 --- a/paddle/pten/core/compat_utils.h +++ b/paddle/pten/core/compat_utils.h @@ -74,7 +74,7 @@ class CompatibleDenseTensorUtils { ret.meta_.dims[0] = end_idx - begin_idx; ret.meta_.offset = tensor->meta_.offset + begin_idx * (tensor->numel() / tensor->dims()[0]) * - paddle::experimental::SizeOf(tensor->data_type()); + paddle::experimental::SizeOf(tensor->dtype()); } return ret; } diff --git a/paddle/pten/core/dense_tensor.cc b/paddle/pten/core/dense_tensor.cc index fbc2a24312..bb38c53ada 100644 --- a/paddle/pten/core/dense_tensor.cc +++ b/paddle/pten/core/dense_tensor.cc @@ -24,14 +24,12 @@ namespace pten { DenseTensor::DenseTensor(const std::shared_ptr& a, const DenseTensorMeta& meta) : meta_(meta), - storage_( - make_intrusive(a, SizeOf(data_type()) * numel())) {} + storage_(make_intrusive(a, SizeOf(dtype()) * numel())) {} DenseTensor::DenseTensor(const std::shared_ptr& a, DenseTensorMeta&& meta) : meta_(std::move(meta)), - storage_( - make_intrusive(a, SizeOf(data_type()) * numel())) {} + storage_(make_intrusive(a, SizeOf(dtype()) * numel())) {} DenseTensor::DenseTensor(intrusive_ptr storage, const DenseTensorMeta& meta) @@ -60,7 +58,7 @@ void* DenseTensor::mutable_data(size_t request_bytes) { storage_, paddle::platform::errors::PreconditionNotMet( "The storage must be valid when call the mutable data function.")); - size_t bytes = numel() * SizeOf(data_type()); + size_t bytes = numel() * SizeOf(dtype()); if (request_bytes) { PADDLE_ENFORCE_GE(request_bytes, bytes, @@ -87,19 +85,19 @@ T* DenseTensor::mutable_data() { paddle::experimental::CppTypeToDataType::Type(); } PADDLE_ENFORCE( - (data_type() == paddle::experimental::CppTypeToDataType::Type()), + (dtype() == paddle::experimental::CppTypeToDataType::Type()), paddle::platform::errors::InvalidArgument( "The type of data (%d) we are trying to retrieve does not match the " "type of data currently contained in the container (%d).", static_cast(paddle::experimental::CppTypeToDataType::Type()), - static_cast(data_type()))); + static_cast(dtype()))); return static_cast(mutable_data()); } template const T* DenseTensor::data() const { PADDLE_ENFORCE( - (data_type() == paddle::experimental::CppTypeToDataType::Type()), + (dtype() == paddle::experimental::CppTypeToDataType::Type()), paddle::platform::errors::InvalidArgument( "The type of data we are trying to retrieve does not match the " "type of data currently contained in the container.")); @@ -115,7 +113,7 @@ const void* DenseTensor::data() const { } void DenseTensor::check_memory_size() const { - size_t bytes = numel() * SizeOf(data_type()); + size_t bytes = numel() * SizeOf(dtype()); PADDLE_ENFORCE_GE(memory_size(), bytes, paddle::platform::errors::InvalidArgument( diff --git a/paddle/pten/core/dense_tensor.h b/paddle/pten/core/dense_tensor.h index b0a4195bc6..8ece80f529 100644 --- a/paddle/pten/core/dense_tensor.h +++ b/paddle/pten/core/dense_tensor.h @@ -93,7 +93,7 @@ class DenseTensor : public TensorBase, /// \brief Returns the data type of the tensor. /// \return The data type of the tensor. - DataType data_type() const noexcept override { return meta_.type; } + DataType dtype() const noexcept override { return meta_.type; } /// \brief Returns the data layout of the tensor. /// \return The data layout of the tensor. diff --git a/paddle/pten/core/tensor_base.h b/paddle/pten/core/tensor_base.h index 79fd742aea..528a52cee8 100644 --- a/paddle/pten/core/tensor_base.h +++ b/paddle/pten/core/tensor_base.h @@ -43,7 +43,7 @@ class TensorBase { /// \brief Returns the data type of the tensor. /// \return The data type of the tensor. - virtual DataType data_type() const = 0; + virtual DataType dtype() const = 0; /// \brief Returns the data layout of the tensor. /// \return The data layout of the tensor. diff --git a/paddle/pten/kernels/cpu/utils.cc b/paddle/pten/kernels/cpu/utils.cc index 3e0bfccb1e..e089eabb0e 100644 --- a/paddle/pten/kernels/cpu/utils.cc +++ b/paddle/pten/kernels/cpu/utils.cc @@ -38,8 +38,8 @@ void Copy(const CPUContext& dev_ctx, const DenseTensor& src, DenseTensor* dst) { VLOG(4) << "src:" << src_ptr << ", dst:" << dst_ptr; CHECK(dst->layout() == src.layout()); - auto size = src.numel() * paddle::framework::SizeOfType( - TransToProtoVarType(src.data_type())); + auto size = src.numel() * + paddle::framework::SizeOfType(TransToProtoVarType(src.dtype())); if (paddle::platform::is_cpu_place(src_place) && paddle::platform::is_cpu_place(dst_place)) { diff --git a/paddle/pten/kernels/cuda/math.cu b/paddle/pten/kernels/cuda/math.cu index 73a743d58e..6a64290d39 100644 --- a/paddle/pten/kernels/cuda/math.cu +++ b/paddle/pten/kernels/cuda/math.cu @@ -81,7 +81,7 @@ void Mean(const CUDAContext& dev_ctx, const DenseTensor& x, DenseTensor* out) { dev_ctx.GetPlace()); pten::DenseTensor tmp( alloc, - DenseTensorMeta(x.data_type(), + DenseTensorMeta(x.dtype(), paddle::framework::make_ddim( {static_cast(temp_storage_bytes)}), x.layout())); diff --git a/paddle/pten/kernels/cuda/utils.cu b/paddle/pten/kernels/cuda/utils.cu index c3940b42ca..04cf1413cb 100644 --- a/paddle/pten/kernels/cuda/utils.cu +++ b/paddle/pten/kernels/cuda/utils.cu @@ -48,8 +48,8 @@ void Copy(const CUDAContext& dev_ctx, VLOG(4) << "src:" << src_ptr << ", dst:" << dst_ptr; CHECK(dst->layout() == src.layout()); - auto size = src.numel() * paddle::framework::SizeOfType( - TransToProtoVarType(src.data_type())); + auto size = src.numel() * + paddle::framework::SizeOfType(TransToProtoVarType(src.dtype())); if (paddle::platform::is_cuda_pinned_place(src_place) && // NOLINT paddle::platform::is_cuda_pinned_place(dst_place)) { diff --git a/paddle/pten/kernels/xpu/utils.cc b/paddle/pten/kernels/xpu/utils.cc index 33bdc66ff0..9bfe493f5f 100644 --- a/paddle/pten/kernels/xpu/utils.cc +++ b/paddle/pten/kernels/xpu/utils.cc @@ -38,8 +38,8 @@ void Copy(const XPUDeviceContext& dev_ctx, << dst_place; dst->Resize(src.dims()); CHECK(dst->layout() == src.layout()); - auto size = src.numel() * paddle::framework::SizeOfType( - TransToProtoVarType(src.data_type())); + auto size = src.numel() * + paddle::framework::SizeOfType(TransToProtoVarType(src.dtype())); if (paddle::platform::is_xpu_place(src_place) && // NOLINT paddle::platform::is_cpu_place(dst_place)) { diff --git a/paddle/pten/tests/api/test_tensor_utils.cc b/paddle/pten/tests/api/test_tensor_utils.cc index fd52b96542..cebb0fc07e 100644 --- a/paddle/pten/tests/api/test_tensor_utils.cc +++ b/paddle/pten/tests/api/test_tensor_utils.cc @@ -47,8 +47,7 @@ TEST(tensor_utils, dense_tensor_to_lod_tensor) { CHECK(dense_tensor.lod().size() == lod_tensor.lod().size()); CHECK(dense_tensor.lod()[0] == static_cast>((lod_tensor.lod()[0]))); - CHECK(dense_tensor.data_type() == - pten::TransToPtenDataType(lod_tensor.type())); + CHECK(dense_tensor.dtype() == pten::TransToPtenDataType(lod_tensor.type())); CHECK(dense_tensor.layout() == pten::TransToPtenDataLayout(lod_tensor.layout())); CHECK(platform::is_cpu_place(lod_tensor.place())); @@ -58,7 +57,7 @@ TEST(tensor_utils, dense_tensor_to_lod_tensor) { auto dense_tensor_1 = MakePtenDenseTensor(lod_tensor); CHECK(dense_tensor_1->dims() == dims); - CHECK(dense_tensor_1->data_type() == dtype); + CHECK(dense_tensor_1->dtype() == dtype); CHECK(dense_tensor_1->layout() == layout); CHECK(dense_tensor_1->lod().size() == lod.size()); CHECK(dense_tensor_1->lod()[0] == lod[0]); @@ -83,7 +82,7 @@ TEST(tensor_utils, dense_tensor_to_tensor) { framework::Tensor tensor; MovesStorage(&dense_tensor, &tensor); - CHECK(dense_tensor.data_type() == pten::TransToPtenDataType(tensor.type())); + CHECK(dense_tensor.dtype() == pten::TransToPtenDataType(tensor.type())); CHECK(dense_tensor.layout() == pten::TransToPtenDataLayout(tensor.layout())); CHECK(platform::is_cpu_place(tensor.place())); @@ -92,7 +91,7 @@ TEST(tensor_utils, dense_tensor_to_tensor) { auto dense_tensor_1 = MakePtenDenseTensor(tensor); CHECK(dense_tensor_1->dims() == dims); - CHECK(dense_tensor_1->data_type() == dtype); + CHECK(dense_tensor_1->dtype() == dtype); CHECK(dense_tensor_1->layout() == layout); const float* data_1 = dense_tensor_1->data(); CHECK(data_1[0] == 1.0f); @@ -117,7 +116,7 @@ TEST(PtenUtils, VarToPtTensor) { // 2. test API auto tensor_x = MakePtenTensorBaseFromVar(v, tensor_def); // 3. check result - ASSERT_EQ(tensor_x->data_type(), pten::DataType::INT32); + ASSERT_EQ(tensor_x->dtype(), pten::DataType::INT32); } } // namespace tests diff --git a/paddle/pten/tests/core/test_dense_tensor.cc b/paddle/pten/tests/core/test_dense_tensor.cc index dac2575713..69c5e9b126 100644 --- a/paddle/pten/tests/core/test_dense_tensor.cc +++ b/paddle/pten/tests/core/test_dense_tensor.cc @@ -82,7 +82,7 @@ TEST(dense_tensor, ctor) { bool r{true}; r = r && (t.numel() == product(m.dims)); r = r && (t.dims() == m.dims); - r = r && (t.data_type() == m.type); + r = r && (t.dtype() == m.type); r = r && (t.layout() == m.layout); r = r && (t.place() == paddle::platform::CPUPlace()); r = r && t.initialized(); -- GitLab