From c23afce1bb4e2cafb1db85b761b3d75c4564c4bd Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Tue, 14 Dec 2021 20:32:46 -0600 Subject: [PATCH] move tensor using to single header (#38142) --- paddle/fluid/framework/custom_operator.cc | 10 ++++----- paddle/pten/api/all.h | 1 + paddle/pten/api/ext/op_meta_info.h | 2 +- paddle/pten/api/ext/tensor_compat.h | 25 ++++++++++++++++++++++ paddle/pten/api/include/tensor.h | 5 ----- paddle/pten/tests/api/test_pten_tensor.cc | 26 +++++++++++------------ paddle/pten/tests/api/test_scale_api.cc | 2 +- 7 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 paddle/pten/api/ext/tensor_compat.h diff --git a/paddle/fluid/framework/custom_operator.cc b/paddle/fluid/framework/custom_operator.cc index 5f731ea9ac8..c70bb72e673 100644 --- a/paddle/fluid/framework/custom_operator.cc +++ b/paddle/fluid/framework/custom_operator.cc @@ -110,8 +110,8 @@ static void RunKernelFunc(const framework::ExecutionContext& ctx, const std::vector& outputs, const std::vector& attrs) { VLOG(1) << "Custom Operator: Start run KernelFunc."; - std::vector custom_ins; - std::vector> custom_vec_ins; + std::vector custom_ins; + std::vector> custom_vec_ins; for (auto& in_name : inputs) { VLOG(1) << "Custom Operator: input name - " << in_name; if (detail::IsDuplicableVar(in_name)) { @@ -120,7 +120,7 @@ static void RunKernelFunc(const framework::ExecutionContext& ctx, PADDLE_ENFORCE_NE(vec_x.empty(), true, platform::errors::NotFound( "Input vector (%s) is empty.", in_name)); - std::vector custom_vec_in; + std::vector custom_vec_in; for (size_t i = 0; i < vec_x.size(); ++i) { auto* x = vec_x[i]; PADDLE_ENFORCE_NOT_NULL( @@ -132,7 +132,7 @@ static void RunKernelFunc(const framework::ExecutionContext& ctx, "The %d-th tensor in input vector (%s) " "is not initialized.", i, in_name)); - paddle::Tensor custom_t; + paddle::experimental::Tensor custom_t; custom_t.set_impl(std::move(experimental::MakePtenDenseTensor(*x))); custom_vec_in.emplace_back(custom_t); } @@ -144,7 +144,7 @@ static void RunKernelFunc(const framework::ExecutionContext& ctx, PADDLE_ENFORCE_EQ(x->IsInitialized(), true, platform::errors::InvalidArgument( "Input tensor (%s) is not initialized.", in_name)); - paddle::Tensor custom_in; + paddle::experimental::Tensor custom_in; custom_in.set_impl(std::move(experimental::MakePtenDenseTensor(*x))); custom_ins.emplace_back(custom_in); } diff --git a/paddle/pten/api/all.h b/paddle/pten/api/all.h index e853ae331e4..4451a5c372b 100644 --- a/paddle/pten/api/all.h +++ b/paddle/pten/api/all.h @@ -42,3 +42,4 @@ limitations under the License. */ #include "paddle/pten/api/ext/exception.h" #include "paddle/pten/api/ext/op_meta_info.h" #include "paddle/pten/api/ext/place.h" +#include "paddle/pten/api/ext/tensor_compat.h" diff --git a/paddle/pten/api/ext/op_meta_info.h b/paddle/pten/api/ext/op_meta_info.h index 66336c7466b..c461d0f9eb6 100644 --- a/paddle/pten/api/ext/op_meta_info.h +++ b/paddle/pten/api/ext/op_meta_info.h @@ -36,7 +36,7 @@ namespace framework { class PADDLE_API OpMetaInfoHelper; } // namespace framework -using Tensor = paddle::Tensor; +using Tensor = paddle::experimental::Tensor; ///////////////// Util Marco Define //////////////// diff --git a/paddle/pten/api/ext/tensor_compat.h b/paddle/pten/api/ext/tensor_compat.h new file mode 100644 index 00000000000..bc243030d6f --- /dev/null +++ b/paddle/pten/api/ext/tensor_compat.h @@ -0,0 +1,25 @@ +/* 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. */ + +#pragma once + +#include "paddle/pten/api/include/tensor.h" + +// Note(chenweihang): In order to be compatible with the original custom +// operator Tensor interface, only available to external users, the file +// cannot be includeed in paddle + +namespace paddle { +using Tensor = paddle::experimental::Tensor; +} // namespace paddle diff --git a/paddle/pten/api/include/tensor.h b/paddle/pten/api/include/tensor.h index 6693dbf78f4..3471a87b21c 100644 --- a/paddle/pten/api/include/tensor.h +++ b/paddle/pten/api/include/tensor.h @@ -489,8 +489,3 @@ class PADDLE_API Tensor final { } // namespace experimental } // namespace paddle - -namespace paddle { -// In order to be compatible with the original custom operator Tensor interface -using Tensor = paddle::experimental::Tensor; -} // namespace paddle diff --git a/paddle/pten/tests/api/test_pten_tensor.cc b/paddle/pten/tests/api/test_pten_tensor.cc index 81d46fdb57a..bffc1b8d89f 100644 --- a/paddle/pten/tests/api/test_pten_tensor.cc +++ b/paddle/pten/tests/api/test_pten_tensor.cc @@ -21,9 +21,9 @@ namespace paddle { namespace tests { template -paddle::Tensor InitCPUTensorForTest() { +experimental::Tensor InitCPUTensorForTest() { std::vector tensor_shape{5, 5}; - auto t1 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape); + auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape); auto* p_data_ptr = t1.mutable_data(paddle::PlaceType::kCPU); for (int64_t i = 0; i < t1.size(); i++) { p_data_ptr[i] = T(5); @@ -57,18 +57,18 @@ void TestCopyTensor() { void TestAPIPlace() { std::vector tensor_shape = {5, 5}; #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - auto t1 = paddle::Tensor(paddle::PlaceType::kGPU, tensor_shape); + auto t1 = experimental::Tensor(paddle::PlaceType::kGPU, tensor_shape); t1.mutable_data(); CHECK((paddle::PlaceType::kGPU == t1.place())); #endif - auto t2 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape); + auto t2 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape); t2.mutable_data(); CHECK((paddle::PlaceType::kCPU == t2.place())); } void TestAPISizeAndShape() { std::vector tensor_shape = {5, 5}; - auto t1 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape); + auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape); CHECK_EQ(t1.size(), 25); CHECK(t1.shape() == tensor_shape); } @@ -79,19 +79,19 @@ void TestAPISlice() { std::vector tensor_shape_origin2 = {5, 5, 5}; std::vector tensor_shape_sub2 = {1, 5, 5}; #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - auto t1 = paddle::Tensor(paddle::PlaceType::kGPU, tensor_shape_origin1); + auto t1 = experimental::Tensor(paddle::PlaceType::kGPU, tensor_shape_origin1); t1.mutable_data(); CHECK(t1.slice(0, 5).shape() == tensor_shape_origin1); CHECK(t1.slice(0, 3).shape() == tensor_shape_sub1); - auto t2 = paddle::Tensor(paddle::PlaceType::kGPU, tensor_shape_origin2); + auto t2 = experimental::Tensor(paddle::PlaceType::kGPU, tensor_shape_origin2); t2.mutable_data(); CHECK(t2.slice(4, 5).shape() == tensor_shape_sub2); #endif - auto t3 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape_origin1); + auto t3 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape_origin1); t3.mutable_data(); CHECK(t3.slice(0, 5).shape() == tensor_shape_origin1); CHECK(t3.slice(0, 3).shape() == tensor_shape_sub1); - auto t4 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape_origin2); + auto t4 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape_origin2); t4.mutable_data(); CHECK(t4.slice(4, 5).shape() == tensor_shape_sub2); @@ -111,7 +111,7 @@ void TestAPISlice() { template paddle::DataType TestDtype() { std::vector tensor_shape = {5, 5}; - auto t1 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape); + auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape); t1.template mutable_data(); return t1.type(); } @@ -119,12 +119,12 @@ paddle::DataType TestDtype() { template void TestCast(paddle::DataType data_type) { std::vector tensor_shape = {5, 5}; - auto t1 = paddle::Tensor(paddle::PlaceType::kCPU, tensor_shape); + auto t1 = experimental::Tensor(paddle::PlaceType::kCPU, tensor_shape); t1.template mutable_data(); auto t2 = t1.cast(data_type); CHECK(t2.type() == data_type); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - auto tg1 = paddle::Tensor(paddle::PlaceType::kGPU); + auto tg1 = experimental::Tensor(paddle::PlaceType::kGPU); tg1.reshape(tensor_shape); tg1.template mutable_data(); auto tg2 = tg1.cast(data_type); @@ -192,7 +192,7 @@ void GroupTestDtype() { } void TestInitilized() { - paddle::Tensor test_tensor(paddle::PlaceType::kCPU, {1, 1}); + experimental::Tensor test_tensor(paddle::PlaceType::kCPU, {1, 1}); CHECK(test_tensor.is_initialized() == false); test_tensor.mutable_data(); CHECK(test_tensor.is_initialized() == true); diff --git a/paddle/pten/tests/api/test_scale_api.cc b/paddle/pten/tests/api/test_scale_api.cc index 3541e3b85cc..5ad52142765 100644 --- a/paddle/pten/tests/api/test_scale_api.cc +++ b/paddle/pten/tests/api/test_scale_api.cc @@ -26,7 +26,7 @@ namespace tests { namespace framework = paddle::framework; using DDim = paddle::framework::DDim; -void CheckScaleResult(Tensor* out) { +void CheckScaleResult(experimental::Tensor* out) { ASSERT_EQ(out->dims().size(), 2); ASSERT_EQ(out->dims()[0], 3); ASSERT_EQ(out->dims()[1], 4); -- GitLab