From c07c7712e296bc77afd7180398393d6a99cb2cf5 Mon Sep 17 00:00:00 2001 From: Huang Jiyi <43315610+huangjiyi@users.noreply.github.com> Date: Thu, 16 Mar 2023 16:32:08 +0800 Subject: [PATCH] Update from_blob API (#51646) * remove contexts in tensor_utils * update from_blob * update from_blob * update from_blob * fix bug * fix bug --- .../collective/process_group_bkcl.cc | 1 + paddle/fluid/framework/CMakeLists.txt | 10 +- paddle/fluid/framework/phi_utils.cc | 2 +- paddle/fluid/framework/phi_utils.h | 1 - paddle/fluid/inference/paddle_inference.map | 2 + .../elementwise/elementwise_op_broadcast.cu.h | 4 +- .../elementwise/elementwise_op_function.h | 1 - .../elementwise/elementwise_op_impl.cu.h | 4 +- paddle/fluid/operators/reduce_ops/reduce_op.h | 4 +- paddle/fluid/operators/reshape_op.cc | 1 - .../from_blob.h => include/tensor_utils.h} | 10 +- paddle/phi/api/lib/CMakeLists.txt | 41 +++++--- .../api/lib/{from_blob.cc => tensor_utils.cc} | 93 +++++++++++-------- paddle/phi/api/lib/utils/CMakeLists.txt | 4 - paddle/phi/api/lib/utils/tensor_utils.cc | 40 -------- paddle/phi/api/lib/utils/tensor_utils.h | 31 ------- paddle/phi/api/yaml/generator/api_gen.py | 9 +- .../phi/api/yaml/generator/strings_api_gen.py | 9 -- paddle/phi/common/int_array.cc | 6 ++ paddle/phi/common/int_array.h | 6 ++ paddle/phi/kernels/CMakeLists.txt | 4 +- .../phi/kernels/gpu/reduce_amin_amax_common.h | 5 +- paddle/phi/tests/api/CMakeLists.txt | 2 +- paddle/phi/tests/api/test_from_blob.cc | 12 +-- paddle/phi/tests/kernels/CMakeLists.txt | 18 ++-- paddle/phi/tools/CMakeLists.txt | 2 +- 26 files changed, 139 insertions(+), 183 deletions(-) rename paddle/phi/api/{lib/from_blob.h => include/tensor_utils.h} (81%) rename paddle/phi/api/lib/{from_blob.cc => tensor_utils.cc} (70%) delete mode 100644 paddle/phi/api/lib/utils/CMakeLists.txt delete mode 100644 paddle/phi/api/lib/utils/tensor_utils.cc delete mode 100644 paddle/phi/api/lib/utils/tensor_utils.h diff --git a/paddle/fluid/distributed/collective/process_group_bkcl.cc b/paddle/fluid/distributed/collective/process_group_bkcl.cc index 4f49ea9596..1b6f512ec8 100644 --- a/paddle/fluid/distributed/collective/process_group_bkcl.cc +++ b/paddle/fluid/distributed/collective/process_group_bkcl.cc @@ -19,6 +19,7 @@ #include "paddle/fluid/distributed/collective/utils.h" #include "paddle/fluid/platform/device/xpu/bkcl_helper.h" #include "paddle/fluid/platform/device/xpu/xpu_info.h" +#include "paddle/phi/api/lib/utils/allocator.h" #include "paddle/phi/core/device_context.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/errors.h" diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 9544bc90d4..a4b4351a14 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -431,13 +431,14 @@ if(WITH_XPU) DEPS lod_tensor dense_tensor selected_rows_utils + int_array + scalar place phi var_type_traits op_info xpu_op_list - convert_utils - phi_api_utils) + convert_utils) else() cc_library( phi_utils @@ -445,12 +446,13 @@ else() DEPS lod_tensor dense_tensor selected_rows_utils + int_array + scalar place phi var_type_traits op_info - convert_utils - phi_api_utils) + convert_utils) endif() if(WITH_XPU) diff --git a/paddle/fluid/framework/phi_utils.cc b/paddle/fluid/framework/phi_utils.cc index d388586347..44a3069ac1 100644 --- a/paddle/fluid/framework/phi_utils.cc +++ b/paddle/fluid/framework/phi_utils.cc @@ -312,7 +312,7 @@ phi::Scalar MakePhiScalarFromVar(const framework::Variable& variable) { phi::IntArray MakePhiIntArrayFromVar(const framework::Variable& variable) { if (variable.IsType()) { const auto& tensor = variable.Get(); - return paddle::experimental::MakePhiIntArray(tensor); + return phi::IntArray(tensor); } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupport casting input `%s` type to IntArray when call pt " diff --git a/paddle/fluid/framework/phi_utils.h b/paddle/fluid/framework/phi_utils.h index 9eef7caea2..f8589e95ff 100644 --- a/paddle/fluid/framework/phi_utils.h +++ b/paddle/fluid/framework/phi_utils.h @@ -26,7 +26,6 @@ limitations under the License. */ #include "paddle/fluid/framework/variable.h" #include "paddle/fluid/platform/macros.h" #include "paddle/fluid/platform/place.h" -#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/phi/common/backend.h" #include "paddle/phi/core/compat/arg_map_context.h" #include "paddle/phi/core/kernel_factory.h" diff --git a/paddle/fluid/inference/paddle_inference.map b/paddle/fluid/inference/paddle_inference.map index 56ea5c1e6d..cb4e8b3065 100644 --- a/paddle/fluid/inference/paddle_inference.map +++ b/paddle/fluid/inference/paddle_inference.map @@ -41,6 +41,8 @@ *paddle::CPUContextResource*; *paddle::OpMetaInfoBuilder*; *paddle::CustomOpKernelContext*; + *paddle::RegisterSymbolsFor*; + *paddle::from_blob*; /* ut needs the following symbol, we need to modify all the ut to hidden such symbols */ diff --git a/paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h b/paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h index 25e22f9e28..7ca62b1969 100644 --- a/paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h +++ b/paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h @@ -41,11 +41,11 @@ void LaunchElementwiseCudaKernel( std::vector> pt_outputs_tmp; for (auto in : ins) { pt_inputs_tmp.emplace_back( - std::move(paddle::experimental::MakePhiDenseTensor(*in))); + std::move(std::make_unique(*in))); } for (auto out : *outs) { pt_outputs_tmp.emplace_back( - std::move(paddle::experimental::MakePhiDenseTensor(*out))); + std::move(std::make_unique(*out))); } for (int i = 0; i < pt_inputs_tmp.size(); i++) { pt_inputs.push_back(pt_inputs_tmp[i].get()); diff --git a/paddle/fluid/operators/elementwise/elementwise_op_function.h b/paddle/fluid/operators/elementwise/elementwise_op_function.h index 8121ab075e..251df9b9d9 100644 --- a/paddle/fluid/operators/elementwise/elementwise_op_function.h +++ b/paddle/fluid/operators/elementwise/elementwise_op_function.h @@ -28,7 +28,6 @@ limitations under the License. */ #include "paddle/fluid/memory/malloc.h" #include "paddle/fluid/operators/elementwise/elementwise_functor.h" #include "paddle/fluid/platform/device/gpu/gpu_info.h" -#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/phi/common/transform.h" #include "paddle/phi/kernels/cpu/elementwise.h" #include "paddle/phi/kernels/cpu/elementwise_grad.h" diff --git a/paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h b/paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h index 5e61a35acf..2680c8ecc5 100644 --- a/paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h +++ b/paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h @@ -42,11 +42,11 @@ void LaunchSameDimsElementwiseCudaKernel( std::vector> pt_outputs_tmp; for (auto in : ins) { pt_inputs_tmp.emplace_back( - std::move(paddle::experimental::MakePhiDenseTensor(*in))); + std::move(std::make_unique(*in))); } for (auto out : *outs) { pt_outputs_tmp.emplace_back( - std::move(paddle::experimental::MakePhiDenseTensor(*out))); + std::move(std::make_unique(*out))); } for (int i = 0; i < pt_inputs_tmp.size(); i++) { pt_inputs.push_back(pt_inputs_tmp[i].get()); diff --git a/paddle/fluid/operators/reduce_ops/reduce_op.h b/paddle/fluid/operators/reduce_ops/reduce_op.h index 3bedf55757..2f25124a0b 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_op.h +++ b/paddle/fluid/operators/reduce_ops/reduce_op.h @@ -830,8 +830,8 @@ class ReduceCudaGradKernel : public framework::OpKernel { } else { d_x->mutable_data(dev_ctx.GetPlace(), d_out->dtype()); } - auto pt_d_out = paddle::experimental::MakePhiDenseTensor(new_d_out); - auto pt_d_x = paddle::experimental::MakePhiDenseTensor(*d_x); + auto pt_d_out = std::make_unique(new_d_out); + auto pt_d_x = std::make_unique(*d_x); if (out_dtype <= 0) { pt_out_dtype = d_out->dtype(); } diff --git a/paddle/fluid/operators/reshape_op.cc b/paddle/fluid/operators/reshape_op.cc index 2a28ceaa18..c9a2b6d1ae 100644 --- a/paddle/fluid/operators/reshape_op.cc +++ b/paddle/fluid/operators/reshape_op.cc @@ -21,7 +21,6 @@ limitations under the License. */ // only can include the headers in paddle/phi/api dirs #include "paddle/fluid/prim/api/composite_backward/composite_backward_api.h" #include "paddle/fluid/prim/utils/static/composite_grad_desc_maker.h" -#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/common/int_array.h" #include "paddle/phi/core/infermeta_utils.h" diff --git a/paddle/phi/api/lib/from_blob.h b/paddle/phi/api/include/tensor_utils.h similarity index 81% rename from paddle/phi/api/lib/from_blob.h rename to paddle/phi/api/include/tensor_utils.h index 1fefea2bdd..43a19a09c1 100644 --- a/paddle/phi/api/lib/from_blob.h +++ b/paddle/phi/api/include/tensor_utils.h @@ -19,7 +19,6 @@ limitations under the License. */ #include "paddle/phi/api/include/tensor.h" namespace paddle { -namespace experimental { using Deleter = std::function; @@ -32,21 +31,24 @@ using Deleter = std::function; * @param data The pointer to the memory buffer. * @param shape The dims of the tensor. * @param dtype The data type of the tensor, should correspond to data type of - * `data`. See PD_FOR_EACH_DATA_TYPE in phi/common/data_type.h + * `data`. See PD_FOR_EACH_DATA_TYPE in `phi/common/data_type.h` * @param layout The data layout of the tensor. * @param place The place where the tensor is located, should correspond to * place of `data`. + * If `place` use the default value, it will be inferred from + * `data`, However,the feature is only supported on CPU or GPU. + * So make sure that `place` is equal to the place of `data` when + * using other devices. * @param deleter A function or function object that will be called to free the * memory buffer. * * @return A Tensor object constructed from the buffer */ PADDLE_API Tensor from_blob(void* data, - const phi::DDim& shape, + const phi::IntArray& shape, phi::DataType dtype, phi::DataLayout layout = phi::DataLayout::NCHW, const phi::Place& place = phi::Place(), const Deleter& deleter = nullptr); -} // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/CMakeLists.txt b/paddle/phi/api/lib/CMakeLists.txt index 29f4babe86..831de5bd29 100644 --- a/paddle/phi/api/lib/CMakeLists.txt +++ b/paddle/phi/api/lib/CMakeLists.txt @@ -1,23 +1,36 @@ -add_subdirectory(utils) - if(WITH_GPU) nv_library( phi_tensor_raw SRCS tensor.cc - DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool - tensor_api) + DEPS tensor_base + dense_tensor + phi_enforce + context_pool + tensor_api + int_array + scalar) elseif(WITH_ROCM) hip_library( phi_tensor_raw SRCS tensor.cc - DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool - tensor_api) + DEPS tensor_base + dense_tensor + phi_enforce + context_pool + tensor_api + int_array + scalar) else() cc_library( phi_tensor_raw SRCS tensor.cc - DEPS tensor_base dense_tensor phi_api_utils phi_enforce context_pool - tensor_api) + DEPS tensor_base + dense_tensor + phi_enforce + context_pool + tensor_api + int_array + scalar) endif() set(api_gen_base ${CMAKE_SOURCE_DIR}/paddle/phi/api/yaml/generator/api_base.py) @@ -290,6 +303,10 @@ cc_library( context_pool SRCS context_pool.cc DEPS phi_backends phi_enforce place init phi_device_context) +cc_library( + api_tensor_utils + SRCS tensor_utils.cc + DEPS phi_tensor_raw) cc_library( kernel_dispatch @@ -323,8 +340,8 @@ cc_library( api_gen_utils phi_data_transform api_custom_impl - phi_profiler - from_blob) + api_tensor_utils + phi_profiler) cc_library( phi_bw_function_api SRCS ${bw_api_source_file} @@ -391,10 +408,6 @@ cc_library( api_int_array SRCS int_array.cc DEPS tensor_copy) -cc_library( - from_blob - SRCS from_blob.cc - DEPS phi_tensor_raw) cc_library( phi_tensor_operants diff --git a/paddle/phi/api/lib/from_blob.cc b/paddle/phi/api/lib/tensor_utils.cc similarity index 70% rename from paddle/phi/api/lib/from_blob.cc rename to paddle/phi/api/lib/tensor_utils.cc index 03ca57a92f..c083e5be5f 100644 --- a/paddle/phi/api/lib/from_blob.cc +++ b/paddle/phi/api/lib/tensor_utils.cc @@ -12,7 +12,9 @@ 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/phi/api/lib/from_blob.h" +#include "paddle/phi/api/include/tensor_utils.h" + +#include #include "paddle/phi/api/lib/api_registry.h" #include "paddle/phi/core/dense_tensor.h" @@ -26,37 +28,72 @@ limitations under the License. */ #endif namespace paddle { -namespace experimental { PD_REGISTER_API(from_blob) -phi::Place GetPlaceFromPtr(void* data); +phi::Place GetPlaceFromPtr(void* data) { +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +#ifdef PADDLE_WITH_CUDA +#if CUDA_VERSION >= 10000 + cudaPointerAttributes attr; + cudaError_t status = cudaPointerGetAttributes(&attr, data); + if (status == cudaSuccess && attr.type == cudaMemoryTypeDevice) { + return phi::GPUPlace(attr.device); + } +#else + PADDLE_THROW( + phi::errors::Unimplemented("The GetPlaceFromPtr() method is only " + "supported when CUDA version >= 10.0.")); +#endif +#else + hipPointerAttribute_t attr; + hipError_t status = hipPointerGetAttributes(&attr, data); + if (status == hipSuccess && attr.memoryType == hipMemoryTypeDevice) { + return phi::GPUPlace(attr.device); + } +#endif +#endif + return phi::CPUPlace(); +} using AllocationDeleter = void (*)(phi::Allocation*); PADDLE_API Tensor from_blob(void* data, - const phi::DDim& shape, + const phi::IntArray& shape, phi::DataType dtype, phi::DataLayout layout, const phi::Place& place, const Deleter& deleter) { PADDLE_ENFORCE_NOT_NULL( - data, phi::errors::InvalidArgument("data can not be nullptr")); + data, phi::errors::InvalidArgument("data can not be nullptr.")); + + PADDLE_ENFORCE_EQ(shape.FromTensor(), + false, + phi::errors::InvalidArgument( + "shape cannot be constructed from a Tensor.")); - auto data_place = GetPlaceFromPtr(data); // TODO(huangjiyi): We need copy data to specified place when // the input place is different with place of data. - if (place.GetType() != phi::AllocationType::UNDEFINED) { - PADDLE_ENFORCE_EQ( - data_place, - place, - phi::errors::InvalidArgument("Specified ", - data_place.DebugString(), - " does not match place of data ", - place.DebugString())); + phi::Place data_place; + if (place.GetType() == phi::AllocationType::UNDEFINED || + place.GetType() == phi::AllocationType::CPU || + place.GetType() == phi::AllocationType::GPU) { + data_place = GetPlaceFromPtr(data); + if (place.GetType() != phi::AllocationType::UNDEFINED) { + PADDLE_ENFORCE_EQ(data_place, + place, + phi::errors::InvalidArgument( + "Specified place does not match place of data. ", + "Specified: %s, Exptected: %s.", + data_place.DebugString(), + place.DebugString())); + } + } else { + data_place = place; } - auto meta = phi::DenseTensorMeta(dtype, shape, layout); + auto meta = + phi::DenseTensorMeta(dtype, phi::make_ddim(shape.GetData()), layout); size_t size = SizeOf(dtype) * (meta.is_scalar ? 1 : product(meta.dims)); @@ -72,30 +109,4 @@ PADDLE_API Tensor from_blob(void* data, return Tensor(std::make_shared(alloc, meta)); } -phi::Place GetPlaceFromPtr(void* data) { -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) -#ifdef PADDLE_WITH_CUDA -#if CUDA_VERSION >= 10000 - cudaPointerAttributes attr; - cudaError_t status = cudaPointerGetAttributes(&attr, data); - if (status == cudaSuccess && attr.type == cudaMemoryTypeDevice) { - return phi::GPUPlace(attr.device); - } -#else - PADDLE_THROW( - phi::errors::Unimplemented("The GetPlaceFromPtr() method is only " - "supported when CUDA version >= 10.0.")); -#endif -#else - hipPointerAttribute_t attr; - hipError_t status = hipPointerGetAttributes(&attr, data); - if (status == hipSuccess && attr.memoryType == hipMemoryTypeDevice) { - return phi::GPUPlace(attr.device); - } -#endif -#endif - return phi::CPUPlace(); -} - -} // namespace experimental } // namespace paddle diff --git a/paddle/phi/api/lib/utils/CMakeLists.txt b/paddle/phi/api/lib/utils/CMakeLists.txt deleted file mode 100644 index b233a12051..0000000000 --- a/paddle/phi/api/lib/utils/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -cc_library( - phi_api_utils - SRCS tensor_utils.cc - DEPS dense_tensor int_array scalar) diff --git a/paddle/phi/api/lib/utils/tensor_utils.cc b/paddle/phi/api/lib/utils/tensor_utils.cc deleted file mode 100644 index 9bb252bd67..0000000000 --- a/paddle/phi/api/lib/utils/tensor_utils.cc +++ /dev/null @@ -1,40 +0,0 @@ -/* 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/phi/api/lib/utils/tensor_utils.h" - -#include -#include - -namespace paddle { -namespace experimental { - -template -void SetLoD(DstLoD* dst, const SrcLoD& src) { - dst->reserve(src.size()); - dst->clear(); - for (auto&& v : src) { - dst->emplace_back(v); - } -} - -std::unique_ptr MakePhiDenseTensor( - const phi::DenseTensor& src) { - return std::make_unique(src); -} - -phi::IntArray MakePhiIntArray(const phi::DenseTensor& src) { return {src}; } - -} // namespace experimental -} // namespace paddle diff --git a/paddle/phi/api/lib/utils/tensor_utils.h b/paddle/phi/api/lib/utils/tensor_utils.h deleted file mode 100644 index fd545154bb..0000000000 --- a/paddle/phi/api/lib/utils/tensor_utils.h +++ /dev/null @@ -1,31 +0,0 @@ -/* 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/phi/api/lib/utils/allocator.h" -#include "paddle/phi/common/int_array.h" -#include "paddle/phi/common/scalar.h" -#include "paddle/phi/core/dense_tensor.h" - -namespace paddle { -namespace experimental { - -std::unique_ptr MakePhiDenseTensor( - const phi::DenseTensor& src); - -phi::IntArray MakePhiIntArray(const phi::DenseTensor& src); - -} // namespace experimental -} // namespace paddle diff --git a/paddle/phi/api/yaml/generator/api_gen.py b/paddle/phi/api/yaml/generator/api_gen.py index 062eef0b06..5dbe5c4183 100644 --- a/paddle/phi/api/yaml/generator/api_gen.py +++ b/paddle/phi/api/yaml/generator/api_gen.py @@ -365,7 +365,7 @@ def source_include(header_file_path): #include "paddle/phi/api/lib/api_gen_utils.h" #include "paddle/phi/api/lib/api_registry.h" #include "paddle/phi/api/lib/data_transform.h" -#include "paddle/phi/api/lib/from_blob.h" +#include "paddle/phi/api/include/tensor_utils.h" #include "paddle/phi/api/lib/kernel_dispatch.h" #include "paddle/phi/common/type_traits.h" #include "paddle/phi/core/kernel_registry.h" @@ -400,8 +400,9 @@ namespace experimental { def declare_extension_api(): return """ - +namespace paddle { PD_DECLARE_API(from_blob); +} // namespace paddle """ @@ -435,11 +436,11 @@ def generate_api(api_yaml_path, header_file_path, source_file_path): header_file.write(foward_api.gene_api_declaration()) source_file.write(foward_api.gene_api_code()) - source_file.write(declare_extension_api()) - header_file.write(namespace[1]) source_file.write(namespace[1]) + source_file.write(declare_extension_api()) + header_file.close() source_file.close() diff --git a/paddle/phi/api/yaml/generator/strings_api_gen.py b/paddle/phi/api/yaml/generator/strings_api_gen.py index 00557ed615..7a25e21ae3 100644 --- a/paddle/phi/api/yaml/generator/strings_api_gen.py +++ b/paddle/phi/api/yaml/generator/strings_api_gen.py @@ -334,7 +334,6 @@ def source_include(header_file_path): #include "paddle/phi/core/string_tensor.h" #include "paddle/phi/infermeta/strings/nullary.h" #include "paddle/phi/infermeta/strings/unary.h" -#include "paddle/phi/api/lib/api_registry.h" #include "paddle/phi/api/lib/kernel_dispatch.h" #include "paddle/phi/core/kernel_registry.h" @@ -342,12 +341,6 @@ DECLARE_int32(low_precision_op_list); """ -def api_register(): - return """ -PD_REGISTER_API(StringsApi); -""" - - def api_namespace(): return ( """ @@ -390,8 +383,6 @@ def generate_api(api_yaml_path, header_file_path, source_file_path): header_file.write(namespace[1]) source_file.write(namespace[1]) - # source_file.write(api_register()) - header_file.close() source_file.close() diff --git a/paddle/phi/common/int_array.cc b/paddle/phi/common/int_array.cc index 6684ff0299..abbe205bc9 100644 --- a/paddle/phi/common/int_array.cc +++ b/paddle/phi/common/int_array.cc @@ -17,11 +17,17 @@ limitations under the License. */ #include "paddle/phi/backends/context_pool.h" #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/common/place.h" +#include "paddle/phi/core/ddim.h" #include "paddle/phi/core/tensor_utils.h" namespace paddle { namespace experimental { +template +IntArrayBase::IntArrayBase(const phi::DDim& dims) { + AssignData(dims.Get(), dims.size()); +} + template <> IntArrayBase::IntArrayBase( const phi::DenseTensor& tensor) { // NOLINT diff --git a/paddle/phi/common/int_array.h b/paddle/phi/common/int_array.h index ce0076bd76..50f88ddb6f 100644 --- a/paddle/phi/common/int_array.h +++ b/paddle/phi/common/int_array.h @@ -19,6 +19,10 @@ limitations under the License. */ #include "paddle/phi/api/ext/exception.h" #include "paddle/phi/common/data_type.h" +namespace phi { +class DDim; +} // namespace phi + namespace paddle { class Tensor; namespace experimental { @@ -50,6 +54,8 @@ class IntArrayBase { void SetFromTensor(bool val) { is_from_tensor_ = val; } + explicit IntArrayBase(const phi::DDim& dims); + // The Tensor must have one dim IntArrayBase(const T& tensor); // NOLINT diff --git a/paddle/phi/kernels/CMakeLists.txt b/paddle/phi/kernels/CMakeLists.txt index c70c876bf2..9c40316b55 100644 --- a/paddle/phi/kernels/CMakeLists.txt +++ b/paddle/phi/kernels/CMakeLists.txt @@ -29,6 +29,8 @@ set(COMMON_KERNEL_DEPS sparse_coo_tensor sparse_csr_tensor tensor_array + int_array + scalar kernel_context kernel_factory arg_map_context @@ -47,7 +49,7 @@ set(COMMON_KERNEL_DEPS concat_and_split_functor selected_rows_functor) # remove this dep after removing fluid deps on tensor creation -set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} phi_api_utils lod_utils) +set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} lod_utils) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} infermeta infermeta_utils sparse_infermeta) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} switch_autotune) diff --git a/paddle/phi/kernels/gpu/reduce_amin_amax_common.h b/paddle/phi/kernels/gpu/reduce_amin_amax_common.h index ff15b5698f..5ba779d027 100644 --- a/paddle/phi/kernels/gpu/reduce_amin_amax_common.h +++ b/paddle/phi/kernels/gpu/reduce_amin_amax_common.h @@ -13,7 +13,6 @@ // limitations under the License. #pragma once -#include "paddle/phi/api/lib/utils/tensor_utils.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/funcs/broadcast_function.h" #include "paddle/phi/kernels/funcs/compare_functors.h" @@ -58,10 +57,10 @@ void ReduceCudaAMaxAMinGrad(const Context& dev_ctx, new_dout.Resize(phi::make_ddim(update_dims)); dev_ctx.Alloc(d_x, d_out->dtype()); - auto new_in = paddle::experimental::MakePhiDenseTensor(*in_x); + auto new_in = std::make_unique(*in_x); auto new_in_tensor = new_in.get(); - auto new_dx = paddle::experimental::MakePhiDenseTensor(*d_x); + auto new_dx = std::make_unique(*d_x); auto new_dx_tensor = new_dx.get(); // make equal_out diff --git a/paddle/phi/tests/api/CMakeLists.txt b/paddle/phi/tests/api/CMakeLists.txt index 84e3037bcc..c2898a2fde 100644 --- a/paddle/phi/tests/api/CMakeLists.txt +++ b/paddle/phi/tests/api/CMakeLists.txt @@ -1,4 +1,4 @@ -set(COMMON_API_TEST_DEPS phi_tensor phi_api phi_api_utils) +set(COMMON_API_TEST_DEPS phi_tensor phi_api api_tensor_utils) if(WITH_GPU) nv_test( diff --git a/paddle/phi/tests/api/test_from_blob.cc b/paddle/phi/tests/api/test_from_blob.cc index 77180486d1..f73f238a4d 100644 --- a/paddle/phi/tests/api/test_from_blob.cc +++ b/paddle/phi/tests/api/test_from_blob.cc @@ -15,7 +15,7 @@ limitations under the License. */ #include #include "paddle/phi/api/include/api.h" -#include "paddle/phi/api/lib/from_blob.h" +#include "paddle/phi/api/include/tensor_utils.h" #include "paddle/phi/core/kernel_registry.h" #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -31,20 +31,18 @@ PD_DECLARE_KERNEL(pow, CPU, ALL_LAYOUT); PD_DECLARE_KERNEL(pow, GPU, ALL_LAYOUT); #endif -using paddle::experimental::DataType; -using paddle::experimental::from_blob; +using paddle::from_blob; +using phi::DataType; namespace paddle { -namespace experimental { phi::Place GetPlaceFromPtr(void* data); -} // namespace experimental } // namespace paddle TEST(from_blob, CPU) { // 1. create data int64_t data[] = {4, 3, 2, 1}; - ASSERT_EQ(paddle::experimental::GetPlaceFromPtr(data), phi::CPUPlace()); + ASSERT_EQ(paddle::GetPlaceFromPtr(data), phi::CPUPlace()); // 2. test API auto test_tesnor = from_blob(data, {1, 2, 2}, DataType::INT64); @@ -84,7 +82,7 @@ TEST(from_blob, CPU) { using phi::memory_utils::Copy; TEST(GetPlaceFromPtr, GPU) { - using paddle::experimental::GetPlaceFromPtr; + using paddle::GetPlaceFromPtr; float cpu_data[6]; auto cpu_data_place = GetPlaceFromPtr(cpu_data); diff --git a/paddle/phi/tests/kernels/CMakeLists.txt b/paddle/phi/tests/kernels/CMakeLists.txt index cd541980a1..a9e897eb61 100644 --- a/paddle/phi/tests/kernels/CMakeLists.txt +++ b/paddle/phi/tests/kernels/CMakeLists.txt @@ -28,44 +28,44 @@ cc_test( cc_test( test_strings_lower_upper_dev_api SRCS test_strings_lower_upper_dev_api.cc - DEPS phi phi_api_utils) + DEPS phi) if(WITH_GPU) nv_test( test_strings_lower_upper_dev_gpu_api SRCS test_strings_lower_upper_dev_api.cu - DEPS phi phi_api_utils) + DEPS phi) elseif(WITH_ROCM) hip_test( test_strings_lower_upper_dev_gpu_api SRCS test_strings_lower_upper_dev_api.cu - DEPS phi phi_api_utils) + DEPS phi) endif() cc_test( test_strings_copy_dev_api SRCS test_strings_copy_dev_api.cc - DEPS phi phi_api_utils) + DEPS phi) if(WITH_GPU) nv_test( test_strings_copy_dev_gpu_api SRCS test_strings_copy_dev_api.cu - DEPS phi phi_api_utils) + DEPS phi) elseif(WITH_ROCM) hip_test( test_strings_copy_dev_gpu_api SRCS test_strings_copy_dev_api.cu - DEPS phi phi_api_utils) + DEPS phi) endif() cc_test( test_memcpy_dev_api SRCS test_memcpy_dev_api.cc - DEPS phi phi_api_utils) + DEPS phi) cc_test( test_transfer_layout_dev_api SRCS test_transfer_layout_dev_api.cc - DEPS phi phi_api_utils) + DEPS phi) if(WITH_GPU) nv_test( @@ -79,7 +79,7 @@ if(WITH_GPU) cc_test( test_fused_adam_kernel SRCS test_fused_adam_kernel.cc - DEPS gtest phi phi_api_utils) + DEPS gtest phi) elseif(WITH_ROCM) hip_test( test_gpu_timer diff --git a/paddle/phi/tools/CMakeLists.txt b/paddle/phi/tools/CMakeLists.txt index bf66522862..74a9bd66a7 100644 --- a/paddle/phi/tools/CMakeLists.txt +++ b/paddle/phi/tools/CMakeLists.txt @@ -6,7 +6,7 @@ if(WITH_GPU) endif() add_executable(print_phi_kernels print_phi_kernels.cc) -target_link_libraries(print_phi_kernels phi phi_api_utils) +target_link_libraries(print_phi_kernels phi) if(WIN32) target_link_libraries(print_phi_kernels shlwapi.lib) endif() -- GitLab