From 60f8c2a3eb32ce334381f719f9b39b2d9713670c Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Thu, 10 Nov 2022 09:45:21 +0000 Subject: [PATCH 01/11] add new tensor --- paddle/fluid/framework/CMakeLists.txt | 3 +- paddle/fluid/framework/operator.cc | 17 ++ paddle/fluid/framework/string_array.h | 3 +- paddle/fluid/imperative/CMakeLists.txt | 2 +- paddle/fluid/operators/save_combine_op.cc | 8 - paddle/fluid/operators/save_combine_op.cu | 23 --- .../operators/save_load_combine_op_test.cc | 4 +- paddle/fluid/pybind/eager_method.cc | 5 +- paddle/fluid/pybind/eager_utils.cc | 8 +- paddle/fluid/pybind/eager_utils.h | 6 +- paddle/phi/CMakeLists.txt | 3 +- paddle/phi/core/CMakeLists.txt | 5 + paddle/phi/core/attribute.h | 3 +- paddle/phi/core/extended_tensor.cc | 61 ++++++++ paddle/phi/core/extended_tensor.h | 56 +++++++ paddle/phi/core/kernel_context.cc | 3 + paddle/phi/core/kernel_factory.h | 1 + paddle/phi/core/kernel_registry.h | 9 ++ paddle/phi/core/kernel_utils.h | 21 +++ paddle/phi/core/vocab.h | 83 ++++++++++ paddle/phi/kernels/CMakeLists.txt | 3 +- paddle/phi/kernels/cpu/save_combine_kernel.cc | 39 +++++ paddle/phi/kernels/gpu/save_combine_kernel.cu | 37 +++++ .../kernels/impl/save_combine_kernel_impl.h | 146 ++++++++++++++++++ paddle/phi/kernels/save_combine_kernel.h | 42 +++++ paddle/phi/ops/compat/save_combine_sig.cc | 38 +++++ 26 files changed, 581 insertions(+), 48 deletions(-) delete mode 100644 paddle/fluid/operators/save_combine_op.cu create mode 100644 paddle/phi/core/extended_tensor.cc create mode 100644 paddle/phi/core/extended_tensor.h create mode 100644 paddle/phi/core/vocab.h create mode 100644 paddle/phi/kernels/cpu/save_combine_kernel.cc create mode 100644 paddle/phi/kernels/gpu/save_combine_kernel.cu create mode 100644 paddle/phi/kernels/impl/save_combine_kernel_impl.h create mode 100644 paddle/phi/kernels/save_combine_kernel.h create mode 100644 paddle/phi/ops/compat/save_combine_sig.cc diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 447d9d87db8a2a..a3b76a9e672a8e 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -237,7 +237,8 @@ cc_test( cc_library( var_type_traits SRCS var_type_traits.cc - DEPS framework_proto scope tensor_array sparse_coo_tensor sparse_csr_tensor) + DEPS framework_proto scope tensor_array sparse_coo_tensor sparse_csr_tensor + extended_tensor) if(WITH_GPU) target_link_libraries(var_type_traits dynload_cuda) endif() diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index ec931e5a6394a5..9f2de80ce2d1d0 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -2937,6 +2937,9 @@ void OperatorWithKernel::BuildPhiKernelContext( need_prepare_phi_data_ = true; tensor_in = &(var->Get()); phi_kernel_context->EmplaceBackInputWithoutSetRange(tensor_in); + } else if (var->IsType()) { + tensor_in = &(var->Get()); + phi_kernel_context->EmplaceBackInputWithoutSetRange(tensor_in); } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupported input `%s` type when call pt kernel.", @@ -3085,6 +3088,20 @@ void OperatorWithKernel::BuildPhiKernelContext( } } break; + case phi::AttributeType::STRING_PTR: { + // std::string* is ragard as attr in PHI, + // but it is regard as output in Fluid. + auto it = ctx.outputs.find(attr_names[i]); + if (it == ctx.outputs.end() || it->second.empty()) { + phi_kernel_context->EmplaceBackAttr(nullptr); + continue; + } + auto* var = (it->second)[0]; + if (var->template IsType()) { + phi_kernel_context->EmplaceBackAttr( + var->template GetMutable()); + } + } break; case phi::AttributeType::SCALARS: { PADDLE_ENFORCE_NE( attr_iter, diff --git a/paddle/fluid/framework/string_array.h b/paddle/fluid/framework/string_array.h index b874fbac4c9e7c..17fd3bd51dfb57 100755 --- a/paddle/fluid/framework/string_array.h +++ b/paddle/fluid/framework/string_array.h @@ -20,13 +20,14 @@ limitations under the License. */ #include #include #include +#include "paddle/phi/core/vocab.h" namespace paddle { namespace framework { using String = std::string; using Strings = std::vector; -using Vocab = std::unordered_map; +using Vocab = phi::Vocab; // Convert the std::string type to the std::string type. bool ConvertStrToWstr(const std::string& src, std::wstring* res); diff --git a/paddle/fluid/imperative/CMakeLists.txt b/paddle/fluid/imperative/CMakeLists.txt index 70f2830d12067d..cc3ed77c391d87 100644 --- a/paddle/fluid/imperative/CMakeLists.txt +++ b/paddle/fluid/imperative/CMakeLists.txt @@ -5,7 +5,7 @@ cc_library( cc_library( var_helper SRCS var_helper.cc - DEPS tensor selected_rows) + DEPS tensor selected_rows extended_tensor) if(WITH_XPU) cc_library( prepared_operator diff --git a/paddle/fluid/operators/save_combine_op.cc b/paddle/fluid/operators/save_combine_op.cc index a25241d368affb..65866e7af7fb80 100644 --- a/paddle/fluid/operators/save_combine_op.cc +++ b/paddle/fluid/operators/save_combine_op.cc @@ -101,11 +101,3 @@ REGISTER_OPERATOR(save_combine, ops::SaveCombineOp, ops::SaveCombineOpProtoMaker, ops::SaveCombineOpInferVarType); - -REGISTER_OP_CPU_KERNEL( - save_combine, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel); diff --git a/paddle/fluid/operators/save_combine_op.cu b/paddle/fluid/operators/save_combine_op.cu deleted file mode 100644 index e96aafa3829782..00000000000000 --- a/paddle/fluid/operators/save_combine_op.cu +++ /dev/null @@ -1,23 +0,0 @@ -/* Copyright (c) 2018 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/fluid/operators/save_combine_op.h" - -namespace ops = paddle::operators; - -REGISTER_OP_CUDA_KERNEL(save_combine, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel, - ops::SaveCombineOpKernel); diff --git a/paddle/fluid/operators/save_load_combine_op_test.cc b/paddle/fluid/operators/save_load_combine_op_test.cc index 5f305d2d6c0c50..14ff038376b43d 100644 --- a/paddle/fluid/operators/save_load_combine_op_test.cc +++ b/paddle/fluid/operators/save_load_combine_op_test.cc @@ -20,8 +20,10 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/platform/bfloat16.h" #include "paddle/fluid/platform/float16.h" +#include "paddle/phi/core/kernel_registry.h" -USE_CPU_ONLY_OP(save_combine); +USE_OP_ITSELF(save_combine); +PD_DECLARE_KERNEL(save_combine_tensor, CPU, ALL_LAYOUT); USE_CPU_ONLY_OP(load_combine); template diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 07978fc053647b..5579ff1f4cf26d 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -54,6 +54,7 @@ typedef SSIZE_T ssize_t; #include "paddle/fluid/memory/allocation/mmap_allocator.h" #include "paddle/fluid/pybind/tensor_py.h" #include "paddle/phi/core/ddim.h" +#include "paddle/phi/core/vocab.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace paddle { @@ -1448,7 +1449,7 @@ static PyObject* tensor_method_set_vocab(TensorObject* self, PyObject* args, PyObject* kwargs) { EAGER_TRY - using Vocab = std::unordered_map; + using Vocab = phi::Vocab; auto vocab = CastPyArg2Vocab(PyTuple_GET_ITEM(args, 0), 0); auto var_tensor = std::make_shared(); *var_tensor->GetMutable() = vocab; @@ -1479,7 +1480,7 @@ static PyObject* tensor_method_get_map_tensor(TensorObject* self, true, paddle::platform::errors::Fatal( "this method is only effective for VariableCompatTensor")); - using Vocab = std::unordered_map; + using Vocab = phi::Vocab; auto* var_tensor = static_cast(self->tensor.impl().get()); return ToPyObject(var_tensor->Get()); diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 1237e4092f02f5..e33f82ec519933 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -514,11 +514,9 @@ paddle::framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, return dtype; } -std::unordered_map CastPyArg2Vocab(PyObject* obj, - ssize_t arg_pos) { +phi::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos) { if (PyDict_Check(obj)) { - return ::pybind11::handle(obj) - .cast>(); + return ::pybind11::handle(obj).cast(); } else { PADDLE_THROW(platform::errors::InvalidArgument( "argument (position %d) must be dict, but got %s", @@ -807,7 +805,7 @@ PyObject* ToPyObject( return dict; } -PyObject* ToPyObject(const std::unordered_map& value) { +PyObject* ToPyObject(const phi::Vocab& value) { PyObject* dict = PyDict_New(); for (const auto& map_iter : value) { // Convert Key diff --git a/paddle/fluid/pybind/eager_utils.h b/paddle/fluid/pybind/eager_utils.h index 145eeacc049f91..a967289a356b75 100644 --- a/paddle/fluid/pybind/eager_utils.h +++ b/paddle/fluid/pybind/eager_utils.h @@ -29,6 +29,7 @@ typedef SSIZE_T ssize_t; #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/selected_rows.h" +#include "paddle/phi/core/vocab.h" #include "pybind11/pybind11.h" #include "pybind11/stl.h" namespace paddle { @@ -73,8 +74,7 @@ std::vector> CastPyArg2VectorOfVectorOfSize_t( PyObject* obj, size_t arg_pos); framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, ssize_t arg_pos); -std::unordered_map CastPyArg2Vocab(PyObject* obj, - ssize_t arg_pos); +phi::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos); std::vector CastPyArg2Strings(PyObject* obj, ssize_t arg_pos); std::shared_ptr CastPyArg2JitFunction(PyObject* obj, ssize_t arg_pos); @@ -113,7 +113,7 @@ PyObject* ToPyObject(const paddle::framework::proto::VarType& type); PyObject* ToPyObject(const void* value); PyObject* ToPyObject( const std::unordered_map>& value); -PyObject* ToPyObject(const std::unordered_map& value); +PyObject* ToPyObject(const phi::Vocab& value); class PyTensorHook : public egr::TensorHook { public: diff --git a/paddle/phi/CMakeLists.txt b/paddle/phi/CMakeLists.txt index d4a7672583ec22..06e62dc2b79abd 100644 --- a/paddle/phi/CMakeLists.txt +++ b/paddle/phi/CMakeLists.txt @@ -41,7 +41,8 @@ set(PHI_DEPS sparse_coo_tensor string_tensor api_scalar - api_int_array) + api_int_array + extended_tensor) get_property(phi_kernels GLOBAL PROPERTY PHI_KERNELS) set(PHI_DEPS ${PHI_DEPS} ${phi_kernels}) diff --git a/paddle/phi/core/CMakeLists.txt b/paddle/phi/core/CMakeLists.txt index d34f5f658b87b2..d1445670f5b864 100644 --- a/paddle/phi/core/CMakeLists.txt +++ b/paddle/phi/core/CMakeLists.txt @@ -69,6 +69,11 @@ cc_library( SRCS tensor_array.cc DEPS dense_tensor tensor_base) +cc_library( + extended_tensor + SRCS extended_tensor.cc + DEPS tensor_base) + cc_library( meta_tensor SRCS meta_tensor.cc diff --git a/paddle/phi/core/attribute.h b/paddle/phi/core/attribute.h index 75a6b16f46e0bb..33fedab7a24b4c 100644 --- a/paddle/phi/core/attribute.h +++ b/paddle/phi/core/attribute.h @@ -46,7 +46,8 @@ using Attribute = paddle::variant; + Place, + std::string*>; using AttributeMap = paddle::flat_hash_map; diff --git a/paddle/phi/core/extended_tensor.cc b/paddle/phi/core/extended_tensor.cc new file mode 100644 index 00000000000000..6ffbcf401224a0 --- /dev/null +++ b/paddle/phi/core/extended_tensor.cc @@ -0,0 +1,61 @@ +/* Copyright (c) 2022 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/core/extended_tensor.h" + +namespace phi { + +int64_t ExtendedTensor::numel() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `numel` method.")); +} + +const DDim& ExtendedTensor::dims() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `dims` method.")); +} + +const Place& ExtendedTensor::place() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `place` method.")); +} + +DataType ExtendedTensor::dtype() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `dtype` method.")); +} + +DataLayout ExtendedTensor::layout() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `dtype` method.")); +} + +bool ExtendedTensor::valid() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `valid` method.")); +} + +bool ExtendedTensor::initialized() const { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `initialized` method.")); +} + +void* ExtendedTensor::AllocateFrom(Allocator* allocator, + DataType dtype, + size_t requested_size) { + PADDLE_THROW(phi::errors::Unavailable( + "ExtendedTensor does not support `AllocateFrom` method.")); +} + +} // namespace phi diff --git a/paddle/phi/core/extended_tensor.h b/paddle/phi/core/extended_tensor.h new file mode 100644 index 00000000000000..304a6e93b9271b --- /dev/null +++ b/paddle/phi/core/extended_tensor.h @@ -0,0 +1,56 @@ +/* Copyright (c) 2022 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/core/allocator.h" +#include "paddle/phi/core/tensor_base.h" +#include "paddle/phi/core/tensor_meta.h" + +namespace phi { + +/// \brief The ExtendedTensor is a interface for custom designed class. +/// If you want to pass some self-designed data as input/output to kernels, +/// you can inherit from this class to store your self-designed data. +class ExtendedTensor : public TensorBase, + public TypeInfoTraits { + public: + ExtendedTensor() = default; + virtual ~ExtendedTensor() = default; + + public: + /// \brief Returns the name of the class for type traits. + /// \return The name of the class. + static const char* name() { return "ExtendedTensor"; } + + int64_t numel() const override; + + const DDim& dims() const override; + + const Place& place() const override; + + DataType dtype() const override; + + DataLayout layout() const override; + + bool valid() const override; + + bool initialized() const override; + + void* AllocateFrom(Allocator* allocator, + DataType dtype, + size_t requested_size = 0) override; +}; + +} // namespace phi diff --git a/paddle/phi/core/kernel_context.cc b/paddle/phi/core/kernel_context.cc index c902fc824f8d2f..cff7635e3444d6 100644 --- a/paddle/phi/core/kernel_context.cc +++ b/paddle/phi/core/kernel_context.cc @@ -123,6 +123,8 @@ const AttrType& KernelContext::AttrAt(size_t idx) const { } } +using string_ptr = std::string*; + template const bool& KernelContext::AttrAt(size_t idx) const; template const int& KernelContext::AttrAt(size_t idx) const; template const int64_t& KernelContext::AttrAt(size_t idx) const; @@ -142,5 +144,6 @@ template const IntArray& KernelContext::AttrAt(size_t idx) const; template const DataType& KernelContext::AttrAt(size_t idx) const; template const DataLayout& KernelContext::AttrAt(size_t idx) const; template const Place& KernelContext::AttrAt(size_t idx) const; +template const string_ptr& KernelContext::AttrAt(size_t idx) const; } // namespace phi diff --git a/paddle/phi/core/kernel_factory.h b/paddle/phi/core/kernel_factory.h index 423c2b8f0a5f69..9df924e22c80b1 100644 --- a/paddle/phi/core/kernel_factory.h +++ b/paddle/phi/core/kernel_factory.h @@ -142,6 +142,7 @@ enum class AttributeType { DATA_TYPE, DATA_LAYOUT, PLACE, + STRING_PTR, }; struct AttributeArgDef { diff --git a/paddle/phi/core/kernel_registry.h b/paddle/phi/core/kernel_registry.h index 396b17dd401d56..380a82104d033d 100644 --- a/paddle/phi/core/kernel_registry.h +++ b/paddle/phi/core/kernel_registry.h @@ -27,6 +27,7 @@ #include "paddle/phi/core/kernel_utils.h" #include "paddle/phi/core/macros.h" #include "paddle/phi/core/type_defs.h" +#include "paddle/phi/core/vocab.h" namespace phi { @@ -100,6 +101,12 @@ struct KernelArgsParseFunctor { default_tensor_layout, default_key.dtype(), arg_type); + } else if (arg_type == + std::type_index(typeid(const std::vector&))) { + args_def->AppendInput(default_key.backend(), + default_tensor_layout, + default_key.dtype(), + arg_type); } else if (arg_type == std::type_index(typeid( const std::vector&))) { args_def->AppendInput(default_key.backend(), @@ -203,6 +210,8 @@ struct KernelArgsParseFunctor { args_def->AppendAttribute(AttributeType::FLOAT64); } else if (arg_type == std::type_index(typeid(std::string))) { args_def->AppendAttribute(AttributeType::STRING); + } else if (arg_type == std::type_index(typeid(std::string*))) { + args_def->AppendAttribute(AttributeType::STRING_PTR); } else if (arg_type == std::type_index(typeid(const std::vector&))) { args_def->AppendAttribute(AttributeType::BOOLS); diff --git a/paddle/phi/core/kernel_utils.h b/paddle/phi/core/kernel_utils.h index 55ea3a31eb3181..1e4f83e32fb88f 100644 --- a/paddle/phi/core/kernel_utils.h +++ b/paddle/phi/core/kernel_utils.h @@ -26,6 +26,7 @@ #include "paddle/phi/core/string_tensor.h" #include "paddle/phi/core/tensor_array.h" #include "paddle/phi/core/type_defs.h" +#include "paddle/phi/core/vocab.h" namespace phi { @@ -166,6 +167,24 @@ namespace phi { } \ } +#define PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE_PTR(attr_type) \ + template \ + struct KernelCallHelper { \ + template \ + static void Compute(KernelContext* ctx, PreviousArgs&... pargs) { \ + static_assert(out_idx == 0, \ + "Kernel's Attributes should appear before Outputs."); \ + attr_type arg = const_cast(ctx->AttrAt(attr_idx)); \ + KernelCallHelper:: \ + template Compute( \ + ctx, pargs..., arg); \ + } \ + } + #define PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(attr_type) \ template \ struct KernelCallHelper { \ @@ -264,6 +283,7 @@ struct KernelImpl { PD_SPECIALIZE_KernelCallHelper_FOR_OPTIONAL_INPUT(DenseTensor); PD_SPECIALIZE_KernelCallHelper_FOR_OPTIONAL_INPUT(SelectedRows); PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(DenseTensor); + PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(Vocab); PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(TensorBase); PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(SelectedRows); PD_SPECIALIZE_KernelCallHelper_FOR_INPUT(SelectedRows); @@ -295,6 +315,7 @@ struct KernelImpl { PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(DataType); PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(DataLayout); PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(Place); + PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE_PTR(std::string*); PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(std::string); PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(Scalar); PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(IntArray); diff --git a/paddle/phi/core/vocab.h b/paddle/phi/core/vocab.h new file mode 100644 index 00000000000000..603867d2485fc0 --- /dev/null +++ b/paddle/phi/core/vocab.h @@ -0,0 +1,83 @@ +/* Copyright (c) 2022 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/core/extended_tensor.h" + +namespace phi { + +class Vocab : public ExtendedTensor, public TypeInfoTraits { + public: + Vocab() = default; + + Vocab(Vocab&& other) = default; + + Vocab(const Vocab& other) = default; + + Vocab& operator=(const Vocab& other) = default; + + Vocab& operator=(Vocab&& other) = default; + + /// \brief Destroy the Vocab and release exclusive resources. + virtual ~Vocab() = default; + + public: + /// \brief Returns the name of the class for type traits. + /// \return The name of the class. + static const char* name() { return "Vocab"; } + + size_t size() const { return data_.size(); } + + void clear() { data_.clear(); } + + void emplace(const std::wstring& key, std::int32_t value) { + data_.emplace(key, value); + } + + std::int32_t at(const std::wstring& key) { return data_.at(key); } + + std::int32_t at(const std::wstring& key) const { return data_.at(key); } + + std::unordered_map::iterator find( + const std::wstring& key) { + return data_.find(key); + } + + std::unordered_map::const_iterator find( + const std::wstring& key) const { + return data_.find(key); + } + + std::unordered_map::iterator begin() { + return data_.begin(); + } + + std::unordered_map::const_iterator begin() const { + return data_.begin(); + } + + std::unordered_map::iterator end() { + return data_.end(); + } + + std::unordered_map::const_iterator end() const { + return data_.end(); + } + + private: + std::unordered_map data_; +}; + +} // namespace phi diff --git a/paddle/phi/kernels/CMakeLists.txt b/paddle/phi/kernels/CMakeLists.txt index 8e45da27a806ac..2853cbbe4e4516 100644 --- a/paddle/phi/kernels/CMakeLists.txt +++ b/paddle/phi/kernels/CMakeLists.txt @@ -77,7 +77,8 @@ set(COMMON_KERNEL_DEPS phi_data_layout_transform gpc utf8proc - device_memory_aligment) + device_memory_aligment + string_array) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} processgroup) if(WITH_NCCL OR WITH_RCCL) diff --git a/paddle/phi/kernels/cpu/save_combine_kernel.cc b/paddle/phi/kernels/cpu/save_combine_kernel.cc new file mode 100644 index 00000000000000..71c11ef56d25aa --- /dev/null +++ b/paddle/phi/kernels/cpu/save_combine_kernel.cc @@ -0,0 +1,39 @@ +// Copyright (c) 2022 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/kernels/save_combine_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/save_combine_kernel_impl.h" + +PD_REGISTER_KERNEL(save_combine_tensor, + CPU, + ALL_LAYOUT, + phi::SaveCombineTensorKernel, + int, + int64_t, + float, + double, + phi::dtype::bfloat16) {} + +PD_REGISTER_KERNEL(save_combine_vocab, + CPU, + ALL_LAYOUT, + phi::SaveCombineVocabKernel, + int, + int64_t, + float, + double, + phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/gpu/save_combine_kernel.cu b/paddle/phi/kernels/gpu/save_combine_kernel.cu new file mode 100644 index 00000000000000..272420ac906d35 --- /dev/null +++ b/paddle/phi/kernels/gpu/save_combine_kernel.cu @@ -0,0 +1,37 @@ +// Copyright (c) 2022 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/kernels/save_combine_kernel.h" + +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/core/kernel_registry.h" +#include "paddle/phi/kernels/impl/save_combine_kernel_impl.h" + +PD_REGISTER_KERNEL(save_combine_tensor, + GPU, + ALL_LAYOUT, + phi::SaveCombineTensorKernel, + int, + int64_t, + float, + double) {} + +PD_REGISTER_KERNEL(save_combine_vocab, + GPU, + ALL_LAYOUT, + phi::SaveCombineVocabKernel, + int, + int64_t, + float, + double) {} diff --git a/paddle/phi/kernels/impl/save_combine_kernel_impl.h b/paddle/phi/kernels/impl/save_combine_kernel_impl.h new file mode 100644 index 00000000000000..8f806c490307cc --- /dev/null +++ b/paddle/phi/kernels/impl/save_combine_kernel_impl.h @@ -0,0 +1,146 @@ +// Copyright (c) 2022 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/kernels/save_combine_kernel.h" + +#include +#include +#include +#include +#include +#include + +#include "paddle/fluid/framework/string_array.h" + +#include "paddle/phi/backends/dynload/port.h" +#include "paddle/phi/core/serialization.h" +#include "paddle/phi/kernels/funcs/data_type_transform.h" + +namespace phi { + +inline void SaveToMemory(const std::string& file_path, + const std::ostringstream& ss, + bool save_to_memory, + std::string* output) { + if (save_to_memory) { + PADDLE_ENFORCE_NE(output, + nullptr, + phi::errors::InvalidArgument( + "Cannot find variable Y for save_combine_op")); + *output = ss.str(); + } else { + MkDirRecursively(DirName(file_path).c_str()); + std::ofstream fout(file_path, std::ios::binary); + PADDLE_ENFORCE_EQ(static_cast(fout), + true, + phi::errors::Unavailable( + "Cannot open %s to save variables.", file_path)); + fout << ss.str(); + fout.close(); + } +} + +template +void SaveCombineTensorKernel(const Context& dev_ctx, + const std::vector& x, + const std::string& file_path, + bool overwrite, + bool save_as_fp16, + bool save_to_memory, + std::string* y) { + bool is_present = FileExists(file_path); + if (is_present && !overwrite) { + PADDLE_THROW(phi::errors::PreconditionNotMet( + "%s exists! Cannot save_combine to it when overwrite is set to " + "false.", + file_path, + overwrite)); + } + + std::ostringstream ss; + PADDLE_ENFORCE_GT(x.size(), + 0UL, + phi::errors::InvalidArgument( + "The number of variables to be saved is %d, expect " + "it to be greater than 0.", + x.size())); + + for (size_t i = 0; i < x.size(); i++) { + auto& tensor = *(x[i]); + PADDLE_ENFORCE_EQ( + tensor.IsInitialized(), + true, + phi::errors::InvalidArgument( + "The Tensor with Index (%d) to be saved is not initialized.", i)); + // Serialize tensors one by one + // Check types to see if a fp16 transformation is required + auto in_dtype = tensor.dtype(); + auto out_dtype = save_as_fp16 ? DataType::FLOAT16 : in_dtype; + + if (in_dtype != out_dtype) { + phi::DenseTensor out = + phi::funcs::TransDataType(dev_ctx, tensor, out_dtype); + // copy LoD info to the new tensor + out.set_lod(tensor.lod()); + phi::SerializeToStream(ss, out, dev_ctx); + } else { + phi::SerializeToStream(ss, tensor, dev_ctx); + } + } + + SaveToMemory(file_path, ss, save_to_memory, y); +} + +template +void SaveCombineVocabKernel(const Context& dev_ctx, + const std::vector& x, + const std::string& file_path, + bool overwrite, + bool save_as_fp16, + bool save_to_memory, + std::string* y) { + bool is_present = FileExists(file_path); + if (is_present && !overwrite) { + PADDLE_THROW(phi::errors::PreconditionNotMet( + "%s exists! Cannot save_combine to it when overwrite is set to " + "false.", + file_path, + overwrite)); + } + + std::ostringstream ss; + PADDLE_ENFORCE_GT(x.size(), + 0UL, + phi::errors::InvalidArgument( + "The number of variables to be saved is %d, expect " + "it to be greater than 0.", + x.size())); + + for (size_t i = 0; i < x.size(); i++) { + auto& tensor = *(x[i]); + std::unordered_map data; + for (auto it = tensor.begin(); it != tensor.end(); ++it) { + std::string t; + paddle::framework::ConvertWstrToStr(it->first, &t); + data.emplace(t, it->second); + } + paddle::framework::StringMapToStream(ss, data); + } + + SaveToMemory(file_path, ss, save_to_memory, y); +} + +} // namespace phi diff --git a/paddle/phi/kernels/save_combine_kernel.h b/paddle/phi/kernels/save_combine_kernel.h new file mode 100644 index 00000000000000..d8628a902035f1 --- /dev/null +++ b/paddle/phi/kernels/save_combine_kernel.h @@ -0,0 +1,42 @@ +/* Copyright (c) 2022 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 + +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/vocab.h" + +namespace phi { + +template +void SaveCombineTensorKernel(const Context& dev_ctx, + const std::vector& x, + const std::string& file_path, + bool overwrite, + bool save_as_fp16, + bool save_to_memory, + std::string* y); + +template +void SaveCombineVocabKernel(const Context& dev_ctx, + const std::vector& x, + const std::string& file_path, + bool overwrite, + bool save_as_fp16, + bool save_to_memory, + std::string* y); + +} // namespace phi diff --git a/paddle/phi/ops/compat/save_combine_sig.cc b/paddle/phi/ops/compat/save_combine_sig.cc new file mode 100644 index 00000000000000..a89a44bc234237 --- /dev/null +++ b/paddle/phi/ops/compat/save_combine_sig.cc @@ -0,0 +1,38 @@ +// Copyright (c) 2022 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/core/compat/op_utils.h" + +namespace phi { + +KernelSignature SaveCombineOpArgumentMapping( + const ArgumentMappingContext& ctx) { + if (ctx.IsDenseTensorInputs("X")) { + return KernelSignature( + "save_combine_tensor", + {"X"}, + {"file_path", "overwrite", "save_as_fp16", "save_to_memory", "Y"}, + {}); + } else { + return KernelSignature( + "save_combine_vocab", + {"X"}, + {"file_path", "overwrite", "save_as_fp16", "save_to_memory", "Y"}, + {}); + } +} + +} // namespace phi + +PD_REGISTER_ARG_MAPPING_FN(save_combine, phi::SaveCombineOpArgumentMapping); From 60e3717b98c039f82ebef0b3223ac8e4f04fa163 Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Thu, 10 Nov 2022 12:43:10 +0000 Subject: [PATCH 02/11] fix windows compile bugs --- paddle/phi/core/vocab.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/paddle/phi/core/vocab.h b/paddle/phi/core/vocab.h index 603867d2485fc0..e6111c34d6ccef 100644 --- a/paddle/phi/core/vocab.h +++ b/paddle/phi/core/vocab.h @@ -14,6 +14,8 @@ limitations under the License. */ #pragma once +#include + #include "paddle/phi/core/extended_tensor.h" namespace phi { From a87999ddb14842023e159a7504811200bc7e02b7 Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Fri, 18 Nov 2022 09:20:55 +0000 Subject: [PATCH 03/11] fix ci bugs --- cmake/operators.cmake | 34 +++ paddle/fluid/framework/operator.cc | 22 +- paddle/fluid/framework/string_array.h | 69 +++++- paddle/fluid/framework/variable_test.cc | 4 +- paddle/fluid/jit/layer.cc | 2 +- paddle/fluid/jit/layer_test.cc | 2 +- paddle/fluid/jit/property.cc | 2 +- paddle/fluid/operators/CMakeLists.txt | 4 +- paddle/fluid/operators/save_combine_op.cc | 24 ++ .../operators/save_combine_op.cu} | 10 +- paddle/fluid/operators/save_combine_op.h | 219 ++++++++++++------ paddle/fluid/pybind/eager_method.cc | 6 +- paddle/fluid/pybind/eager_utils.cc | 6 +- paddle/fluid/pybind/eager_utils.h | 6 +- paddle/fluid/pybind/pybind.cc | 2 +- paddle/phi/core/attribute.h | 3 +- paddle/phi/core/cplus_string.h | 74 ++++++ paddle/phi/core/kernel_context.cc | 4 +- paddle/phi/core/kernel_factory.h | 3 +- paddle/phi/core/kernel_registry.h | 13 +- paddle/phi/core/kernel_utils.h | 25 +- paddle/phi/core/vocab.h | 85 ------- paddle/phi/kernels/CMakeLists.txt | 3 +- paddle/phi/kernels/cpu/save_combine_kernel.cc | 39 ---- .../kernels/impl/save_combine_kernel_impl.h | 146 ------------ paddle/phi/kernels/save_combine_kernel.h | 42 ---- paddle/phi/ops/compat/save_combine_sig.cc | 8 +- 27 files changed, 400 insertions(+), 457 deletions(-) mode change 100755 => 100644 paddle/fluid/framework/string_array.h rename paddle/{phi/kernels/gpu/save_combine_kernel.cu => fluid/operators/save_combine_op.cu} (80%) create mode 100644 paddle/phi/core/cplus_string.h delete mode 100644 paddle/phi/core/vocab.h delete mode 100644 paddle/phi/kernels/cpu/save_combine_kernel.cc delete mode 100644 paddle/phi/kernels/impl/save_combine_kernel_impl.h delete mode 100644 paddle/phi/kernels/save_combine_kernel.h diff --git a/cmake/operators.cmake b/cmake/operators.cmake index 0cd21c942e03a0..53c6df575cd1ae 100644 --- a/cmake/operators.cmake +++ b/cmake/operators.cmake @@ -26,6 +26,36 @@ function(find_register FILENAME PATTERN OUTPUT) PARENT_SCOPE) endfunction() +function(find_phi_register FILENAME ADD_PATH) + # find the op_name of REGISTER_OPERATOR(op_name, ...), REGISTER_OP_CPU_KERNEL(op_name, ...) , etc. + # set op_name to OUTPUT + set(options "") + set(oneValueArgs "") + set(multiValueArgs "") + file(READ ${FILENAME} CONTENT) + + string( + REGEX + MATCH + "PD_REGISTER_KERNEL\\([ \t\r\n]*[a-z0-9_]*,[[ \\\t\r\n\/]*[a-z0-9_]*]?[ \\\t\r\n]*[a-zA-Z]*,[ \\\t\r\n]*[A-Z_]*" + register + "${CONTENT}") + if(NOT register STREQUAL "") + string(REPLACE "PD_REGISTER_KERNEL(" "" register "${register}") + string(REPLACE "," ";" register "${register}") + string(REGEX REPLACE "[ \\\t\r\n]+" "" register "${register}") + string(REGEX REPLACE "//cuda_only" "" register "${register}") + list(GET register 0 kernel_name) + list(GET register 1 kernel_backend) + list(GET register 2 kernel_layout) + + file( + APPEND ${ADD_PATH} + "PD_DECLARE_KERNEL(${kernel_name}, ${kernel_backend}, ${kernel_layout});\n" + ) + endif() +endfunction() + function(op_library TARGET) # op_library is a function to create op library. The interface is same as # cc_library. But it handle split GPU/CPU code and link some common library @@ -371,6 +401,8 @@ function(op_library TARGET) foreach(cc_src ${cc_srcs}) # pybind USE_OP_ITSELF set(op_name "") + # Add PHI Kernel Registry Message + find_phi_register(${cc_src} ${pybind_file}) find_register(${cc_src} "REGISTER_OPERATOR" op_name) if(NOT ${op_name} EQUAL "") file(APPEND ${pybind_file} "USE_OP_ITSELF(${op_name});\n") @@ -408,6 +440,8 @@ function(op_library TARGET) # message("cu_srcs ${cu_srcs}") foreach(cu_src ${cu_srcs}) set(op_name "") + # Add PHI Kernel Registry Message + find_phi_register(${cu_src} ${pybind_file}) find_register(${cu_src} "REGISTER_OP_CUDA_KERNEL" op_name) if(NOT ${op_name} EQUAL "") file(APPEND ${pybind_file} "USE_OP_DEVICE_KERNEL(${op_name}, CUDA);\n") diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 0eabd6c50c0e39..5a512ebc395059 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -2982,6 +2982,13 @@ void OperatorWithKernel::BuildPhiKernelContext( // Note: If the input LoDTensorArray size is 0, the output // LoDTensorArray is also 0 phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); + } else if (!var->IsInitialized()) { + // The following is for RAW type of var + if (output_defs[i].type_index == + std::type_index(typeid(phi::CPlusString*))) { + tensor_out = var->template GetMutable(); + } + phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } else { PADDLE_THROW(platform::errors::Unimplemented( "Unsupported output `%s` type when call pt kernel.", @@ -3081,20 +3088,7 @@ void OperatorWithKernel::BuildPhiKernelContext( } } break; - case phi::AttributeType::STRING_PTR: { - // std::string* is ragard as attr in PHI, - // but it is regard as output in Fluid. - auto it = ctx.outputs.find(attr_names[i]); - if (it == ctx.outputs.end() || it->second.empty()) { - phi_kernel_context->EmplaceBackAttr(nullptr); - continue; - } - auto* var = (it->second)[0]; - if (var->template IsType()) { - phi_kernel_context->EmplaceBackAttr( - var->template GetMutable()); - } - } break; + case phi::AttributeType::SCALARS: { PADDLE_ENFORCE_NE( attr_iter, diff --git a/paddle/fluid/framework/string_array.h b/paddle/fluid/framework/string_array.h old mode 100755 new mode 100644 index 17fd3bd51dfb57..f81be7fd468c2d --- a/paddle/fluid/framework/string_array.h +++ b/paddle/fluid/framework/string_array.h @@ -20,14 +20,77 @@ limitations under the License. */ #include #include #include -#include "paddle/phi/core/vocab.h" +#include "paddle/phi/core/cplus_string.h" +#include "paddle/phi/core/extended_tensor.h" namespace paddle { namespace framework { -using String = std::string; +class Vocab : public phi::ExtendedTensor, + public phi::TypeInfoTraits { + public: + Vocab() = default; + + Vocab(Vocab&& other) = default; + + Vocab(const Vocab& other) = default; + + Vocab& operator=(const Vocab& other) = default; + + Vocab& operator=(Vocab&& other) = default; + + /// \brief Destroy the Vocab and release exclusive resources. + virtual ~Vocab() = default; + + public: + /// \brief Returns the name of the class for type traits. + /// \return The name of the class. + static const char* name() { return "Vocab"; } + + size_t size() const { return data_.size(); } + + void clear() { data_.clear(); } + + void emplace(const std::wstring& key, std::int32_t value) { + data_.emplace(key, value); + } + + std::int32_t at(const std::wstring& key) { return data_.at(key); } + + std::int32_t at(const std::wstring& key) const { return data_.at(key); } + + std::unordered_map::iterator find( + const std::wstring& key) { + return data_.find(key); + } + + std::unordered_map::const_iterator find( + const std::wstring& key) const { + return data_.find(key); + } + + std::unordered_map::iterator begin() { + return data_.begin(); + } + + std::unordered_map::const_iterator begin() const { + return data_.begin(); + } + + std::unordered_map::iterator end() { + return data_.end(); + } + + std::unordered_map::const_iterator end() const { + return data_.end(); + } + + private: + std::unordered_map data_; +}; + +using String = phi::CPlusString; using Strings = std::vector; -using Vocab = phi::Vocab; // Convert the std::string type to the std::string type. bool ConvertStrToWstr(const std::string& src, std::wstring* res); diff --git a/paddle/fluid/framework/variable_test.cc b/paddle/fluid/framework/variable_test.cc index 22af9ae934e0cf..262181095e603a 100644 --- a/paddle/fluid/framework/variable_test.cc +++ b/paddle/fluid/framework/variable_test.cc @@ -22,10 +22,10 @@ namespace framework { TEST(Variable, GetMutable) { std::unique_ptr v(new Variable()); - auto* t = v->GetMutable(); + auto* t = v->GetMutable()->Get(); *t = "1234"; - const auto& tt = v->Get(); + const auto& tt = v->Get().Get(); EXPECT_EQ("1234", tt); try { diff --git a/paddle/fluid/jit/layer.cc b/paddle/fluid/jit/layer.cc index 9055120e4bbb7a..75a7e282e6be80 100644 --- a/paddle/fluid/jit/layer.cc +++ b/paddle/fluid/jit/layer.cc @@ -89,7 +89,7 @@ std::vector Layer::FunctionNames() const { PD_SPECIALZE_ATTRIBUTE_TYPE(int) PD_SPECIALZE_ATTRIBUTE_TYPE(float) -PD_SPECIALZE_ATTRIBUTE_TYPE(std::string) +PD_SPECIALZE_ATTRIBUTE_TYPE(framework::String) PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector) PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector) PD_SPECIALZE_ATTRIBUTE_TYPE(std::vector) diff --git a/paddle/fluid/jit/layer_test.cc b/paddle/fluid/jit/layer_test.cc index 7007693aa83e02..693d9e6898ca00 100644 --- a/paddle/fluid/jit/layer_test.cc +++ b/paddle/fluid/jit/layer_test.cc @@ -86,7 +86,7 @@ TEST(CpuLayerTest, Construct) { int ds = layer.Attribute("down_sampling"); EXPECT_EQ(ds, 4); - std::string fstr = layer.Attribute("fstr"); + std::string fstr = *(layer.Attribute("fstr").Get()); EXPECT_STREQ(fstr.c_str(), "save str property"); std::vector ints = layer.Attribute>("ints"); diff --git a/paddle/fluid/jit/property.cc b/paddle/fluid/jit/property.cc index 1cf303239baedd..b0c943b24a6d79 100644 --- a/paddle/fluid/jit/property.cc +++ b/paddle/fluid/jit/property.cc @@ -97,7 +97,7 @@ std::unordered_map> Property::Values() { *var->GetMutable() = static_cast(GetInt64(n)); break; case ValueProto::STRING: - *var->GetMutable() = GetString(n); + *var->GetMutable() = GetString(n); break; case ValueProto::FLOATS: *var->GetMutable>() = GetFloats(n); diff --git a/paddle/fluid/operators/CMakeLists.txt b/paddle/fluid/operators/CMakeLists.txt index 2823db516010ea..c97eedd4029453 100644 --- a/paddle/fluid/operators/CMakeLists.txt +++ b/paddle/fluid/operators/CMakeLists.txt @@ -12,7 +12,7 @@ unset(OP_LIBRARY CACHE) set(pybind_file ${PADDLE_BINARY_DIR}/paddle/fluid/pybind/pybind.h.tmp CACHE INTERNAL "pybind.h file") set(pybind_file_prune ${PADDLE_BINARY_DIR}/paddle/fluid/pybind/pybind.h.prune CACHE INTERNAL "pybind.h file") set(pybind_file_final ${PADDLE_BINARY_DIR}/paddle/fluid/pybind/pybind.h) -file(WRITE ${pybind_file} "// Generated by the paddle/fluid/operators/CMakeLists.txt. DO NOT EDIT!\n\n") +file(WRITE ${pybind_file} "#include \"paddle/phi/core/kernel_registry.h\" // Generated by the paddle/fluid/operators/CMakeLists.txt. DO NOT EDIT!\n\n") add_subdirectory(math) add_subdirectory(controlflow) @@ -109,7 +109,7 @@ register_operators(EXCLUDES py_layer_op py_func_op warpctc_op dgc_op load_combin op_library(run_program_op SRCS run_program_op.cc run_program_op.cu.cc run_program_op_npu.cc DEPS executor_cache ${OP_HEADER_DEPS}) target_link_libraries(run_program_op cuda_graph_with_memory_pool) op_library(quantize_linear_op DEPS phi) -op_library(save_combine_op DEPS string_array) +op_library(save_combine_op DEPS string_array phi) op_library(load_combine_op DEPS string_array) if (WITH_GPU OR WITH_ROCM) diff --git a/paddle/fluid/operators/save_combine_op.cc b/paddle/fluid/operators/save_combine_op.cc index 65866e7af7fb80..4cb736e715feb6 100644 --- a/paddle/fluid/operators/save_combine_op.cc +++ b/paddle/fluid/operators/save_combine_op.cc @@ -16,6 +16,10 @@ limitations under the License. */ #include +#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/phi/common/bfloat16.h" +#include "paddle/phi/core/kernel_registry.h" + namespace paddle { namespace operators { @@ -101,3 +105,23 @@ REGISTER_OPERATOR(save_combine, ops::SaveCombineOp, ops::SaveCombineOpProtoMaker, ops::SaveCombineOpInferVarType); + +PD_REGISTER_KERNEL(save_combine_tensor, + CPU, + ALL_LAYOUT, + paddle::operators::SaveCombineTensorKernel, + int, + int64_t, + float, + double, + phi::dtype::bfloat16) {} + +PD_REGISTER_KERNEL(save_combine_vocab, + CPU, + ALL_LAYOUT, + paddle::operators::SaveCombineVocabKernel, + int, + int64_t, + float, + double, + phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/gpu/save_combine_kernel.cu b/paddle/fluid/operators/save_combine_op.cu similarity index 80% rename from paddle/phi/kernels/gpu/save_combine_kernel.cu rename to paddle/fluid/operators/save_combine_op.cu index 272420ac906d35..6c6a8944a6aa8c 100644 --- a/paddle/phi/kernels/gpu/save_combine_kernel.cu +++ b/paddle/fluid/operators/save_combine_op.cu @@ -12,16 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/phi/kernels/save_combine_kernel.h" - -#include "paddle/phi/backends/cpu/cpu_context.h" +#include "paddle/fluid/operators/save_combine_op.h" +#include "paddle/phi/backends/gpu/gpu_context.h" #include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/kernels/impl/save_combine_kernel_impl.h" PD_REGISTER_KERNEL(save_combine_tensor, GPU, ALL_LAYOUT, - phi::SaveCombineTensorKernel, + paddle::operators::SaveCombineTensorKernel, int, int64_t, float, @@ -30,7 +28,7 @@ PD_REGISTER_KERNEL(save_combine_tensor, PD_REGISTER_KERNEL(save_combine_vocab, GPU, ALL_LAYOUT, - phi::SaveCombineVocabKernel, + paddle::operators::SaveCombineVocabKernel, int, int64_t, float, diff --git a/paddle/fluid/operators/save_combine_op.h b/paddle/fluid/operators/save_combine_op.h index 20baefe5974281..3f7fc64b95e6f7 100644 --- a/paddle/fluid/operators/save_combine_op.h +++ b/paddle/fluid/operators/save_combine_op.h @@ -30,32 +30,146 @@ limitations under the License. */ #include "paddle/fluid/framework/string_array.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/phi/backends/dynload/port.h" +#include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/serialization.h" namespace paddle { namespace operators { + +inline void SaveToMemory(const std::string& file_path, + const std::ostringstream& ss, + bool save_to_memory, + std::string* output) { + if (save_to_memory) { + PADDLE_ENFORCE_NE(output, + nullptr, + phi::errors::InvalidArgument( + "Cannot find variable Y for save_combine_op")); + *output = ss.str(); + } else { + MkDirRecursively(DirName(file_path).c_str()); + std::ofstream fout(file_path, std::ios::binary); + PADDLE_ENFORCE_EQ(static_cast(fout), + true, + phi::errors::Unavailable( + "Cannot open %s to save variables.", file_path)); + fout << ss.str(); + fout.close(); + } +} + +template +void SaveCombineTensorKernel(const Context& dev_ctx, + const std::vector& x, + const std::string& file_path, + bool overwrite, + bool save_as_fp16, + bool save_to_memory, + phi::CPlusString* out) { + std::string* y = out->Get(); + bool is_present = FileExists(file_path); + if (is_present && !overwrite) { + PADDLE_THROW(phi::errors::PreconditionNotMet( + "%s exists! Cannot save_combine to it when overwrite is set to " + "false.", + file_path, + overwrite)); + } + + std::ostringstream ss; + PADDLE_ENFORCE_GT(x.size(), + 0UL, + phi::errors::InvalidArgument( + "The number of variables to be saved is %d, expect " + "it to be greater than 0.", + x.size())); + + for (size_t i = 0; i < x.size(); i++) { + auto& tensor = *(x[i]); + PADDLE_ENFORCE_EQ( + tensor.IsInitialized(), + true, + phi::errors::InvalidArgument( + "The Tensor with Index (%d) to be saved is not initialized.", i)); + // Serialize tensors one by one + // Check types to see if a fp16 transformation is required + auto in_dtype = framework::TransToProtoVarType(tensor.dtype()); + auto out_dtype = save_as_fp16 ? framework::proto::VarType::FP16 : in_dtype; + if (in_dtype != out_dtype) { + auto place = dev_ctx.GetPlace(); + auto in_kernel_type = framework::OpKernelType(in_dtype, place); + auto out_kernel_type = framework::OpKernelType(out_dtype, place); + phi::DenseTensor out; + framework::TransDataType(in_kernel_type, out_kernel_type, tensor, &out); + // copy LoD info to the new tensor + out.set_lod(tensor.lod()); + phi::SerializeToStream(ss, out, dev_ctx); + } else { + phi::SerializeToStream(ss, tensor, dev_ctx); + } + } + + SaveToMemory(file_path, ss, save_to_memory, y); +} + +template +void SaveCombineVocabKernel( + const Context& dev_ctx, + const std::vector& inputs, + const std::string& file_path, + bool overwrite, + bool save_as_fp16, + bool save_to_memory, + phi::CPlusString* out) { + std::string* y = out->Get(); + std::vector x(inputs.size()); + for (auto input : inputs) { + x.push_back(static_cast(input)); + } + bool is_present = FileExists(file_path); + if (is_present && !overwrite) { + PADDLE_THROW(phi::errors::PreconditionNotMet( + "%s exists! Cannot save_combine to it when overwrite is set to " + "false.", + file_path, + overwrite)); + } + + std::ostringstream ss; + PADDLE_ENFORCE_GT(x.size(), + 0UL, + phi::errors::InvalidArgument( + "The number of variables to be saved is %d, expect " + "it to be greater than 0.", + x.size())); + + for (size_t i = 0; i < x.size(); i++) { + auto& tensor = *(x[i]); + std::unordered_map data; + for (auto it = tensor.begin(); it != tensor.end(); ++it) { + std::string t; + paddle::framework::ConvertWstrToStr(it->first, &t); + data.emplace(t, it->second); + } + paddle::framework::StringMapToStream(ss, data); + } + + SaveToMemory(file_path, ss, save_to_memory, y); +} + template class SaveCombineOpKernel : public framework::OpKernel { public: - void Compute(const framework::ExecutionContext &ctx) const override { + void Compute(const framework::ExecutionContext& ctx) const override { auto place = ctx.GetPlace(); auto filename = ctx.Attr("file_path"); auto overwrite = ctx.Attr("overwrite"); auto save_as_fp16 = ctx.Attr("save_as_fp16"); auto save_to_memory = ctx.Attr("save_to_memory"); - auto output = ctx.Output("Y"); - - bool is_present = FileExists(filename); - if (is_present && !overwrite) { - PADDLE_THROW(platform::errors::PreconditionNotMet( - "%s exists! Cannot save_combine to it when overwrite is set to " - "false.", - filename, - overwrite)); - } - - std::ostringstream ss; + auto output = ctx.Output("Y"); auto inp_var_names = ctx.InputNames("X"); - auto &inp_vars = ctx.MultiInputVar("X"); + auto& inp_vars = ctx.MultiInputVar("X"); + PADDLE_ENFORCE_GT(inp_var_names.size(), 0UL, platform::errors::InvalidArgument( @@ -64,8 +178,8 @@ class SaveCombineOpKernel : public framework::OpKernel { inp_var_names.size())); // get device context from pool - platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); - auto &dev_ctx = *pool.Get(place); + platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance(); + auto& dev_ctx = *pool.Get(place); for (size_t i = 0; i < inp_var_names.size(); i++) { PADDLE_ENFORCE_NOT_NULL( @@ -79,59 +193,32 @@ class SaveCombineOpKernel : public framework::OpKernel { "SaveCombine operator only supports saving " "LoDTensor or Vocab variable, %s has wrong type.", inp_var_names[i])); + } - if (inp_vars[i]->IsType()) { - auto &tensor = inp_vars[i]->Get(); - PADDLE_ENFORCE_EQ( - tensor.IsInitialized(), - true, - platform::errors::InvalidArgument( - "The Tensor of Variable(%s) to be saved is not initialized.", - inp_var_names[i])); - // Serialize tensors one by one - // Check types to see if a fp16 transformation is required - auto in_dtype = framework::TransToProtoVarType(tensor.dtype()); - auto out_dtype = - save_as_fp16 ? framework::proto::VarType::FP16 : in_dtype; - - if (in_dtype != out_dtype) { - auto in_kernel_type = framework::OpKernelType(in_dtype, place); - auto out_kernel_type = framework::OpKernelType(out_dtype, place); - phi::DenseTensor out; - // copy LoD info to the new tensor - out.set_lod(tensor.lod()); - framework::TransDataType( - in_kernel_type, out_kernel_type, tensor, &out); - framework::SerializeToStream(ss, out, dev_ctx); - } else { - framework::SerializeToStream(ss, tensor, dev_ctx); - } - } else { - auto &tensor = inp_vars[i]->Get(); - std::unordered_map data; - for (auto it = tensor.begin(); it != tensor.end(); ++it) { - std::string t; - framework::ConvertWstrToStr(it->first, &t); - data.emplace(t, it->second); - } - framework::StringMapToStream(ss, data); + if (inp_vars.size() > 0 && inp_vars[0]->IsType()) { + std::vector x(inp_vars.size()); + for (auto inp_var : inp_vars) { + x.push_back(&(inp_var->Get())); } - } - if (save_to_memory) { - PADDLE_ENFORCE_NE(output, - nullptr, - platform::errors::InvalidArgument( - "Cannot find variable Y for save_combine_op")); - *output = ss.str(); + SaveCombineTensorKernel(dev_ctx, + x, + filename, + overwrite, + save_as_fp16, + save_to_memory, + output); } else { - MkDirRecursively(DirName(filename).c_str()); - std::ofstream fout(filename, std::ios::binary); - PADDLE_ENFORCE_EQ(static_cast(fout), - true, - platform::errors::Unavailable( - "Cannot open %s to save variables.", filename)); - fout << ss.str(); - fout.close(); + std::vector x(inp_vars.size()); + for (auto inp_var : inp_vars) { + x.push_back(&(inp_var->Get())); + } + SaveCombineVocabKernel(dev_ctx, + x, + filename, + overwrite, + save_as_fp16, + save_to_memory, + output); } } }; diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index fe478aea8c44c8..4cb6c5cfe035bd 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -29,6 +29,7 @@ typedef SSIZE_T ssize_t; #include "paddle/fluid/eager/hooks.h" #include "paddle/fluid/eager/utils.h" #include "paddle/fluid/framework/convert_utils.h" +#include "paddle/fluid/framework/string_array.h" #include "paddle/fluid/memory/allocation/allocator.h" #include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/platform/enforce.h" @@ -54,7 +55,6 @@ typedef SSIZE_T ssize_t; #include "paddle/fluid/memory/allocation/mmap_allocator.h" #include "paddle/fluid/pybind/tensor_py.h" #include "paddle/phi/core/ddim.h" -#include "paddle/phi/core/vocab.h" #include "paddle/phi/kernels/funcs/math_function.h" namespace paddle { @@ -1449,7 +1449,7 @@ static PyObject* tensor_method_set_vocab(TensorObject* self, PyObject* args, PyObject* kwargs) { EAGER_TRY - using Vocab = phi::Vocab; + using Vocab = paddle::framework::Vocab; auto vocab = CastPyArg2Vocab(PyTuple_GET_ITEM(args, 0), 0); auto var_tensor = std::make_shared(); *var_tensor->GetMutable() = vocab; @@ -1480,7 +1480,7 @@ static PyObject* tensor_method_get_map_tensor(TensorObject* self, true, paddle::platform::errors::Fatal( "this method is only effective for VariableCompatTensor")); - using Vocab = phi::Vocab; + using Vocab = paddle::framework::Vocab; auto* var_tensor = static_cast(self->tensor.impl().get()); return ToPyObject(var_tensor->Get()); diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 4431b136f39e03..e96fb92d0fc15c 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -514,9 +514,9 @@ paddle::framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, return dtype; } -phi::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos) { +paddle::framework::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos) { if (PyDict_Check(obj)) { - return ::pybind11::handle(obj).cast(); + return ::pybind11::handle(obj).cast(); } else { PADDLE_THROW(platform::errors::InvalidArgument( "argument (position %d) must be dict, but got %s", @@ -808,7 +808,7 @@ PyObject* ToPyObject( return dict; } -PyObject* ToPyObject(const phi::Vocab& value) { +PyObject* ToPyObject(const paddle::framework::Vocab& value) { PyObject* dict = PyDict_New(); for (const auto& map_iter : value) { // Convert Key diff --git a/paddle/fluid/pybind/eager_utils.h b/paddle/fluid/pybind/eager_utils.h index 724e5e988cd302..134b2dc185373a 100644 --- a/paddle/fluid/pybind/eager_utils.h +++ b/paddle/fluid/pybind/eager_utils.h @@ -20,6 +20,7 @@ typedef SSIZE_T ssize_t; #include "paddle/fluid/eager/hooks.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/lod_tensor_array.h" +#include "paddle/fluid/framework/string_array.h" #include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/jit/function.h" #include "paddle/fluid/platform/place.h" @@ -29,7 +30,6 @@ typedef SSIZE_T ssize_t; #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/selected_rows.h" -#include "paddle/phi/core/vocab.h" #include "pybind11/pybind11.h" #include "pybind11/stl.h" namespace paddle { @@ -74,7 +74,7 @@ std::vector> CastPyArg2VectorOfVectorOfSize_t( PyObject* obj, size_t arg_pos); framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, ssize_t arg_pos); -phi::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos); +paddle::framework::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos); std::vector CastPyArg2Strings(PyObject* obj, ssize_t arg_pos); std::shared_ptr CastPyArg2JitFunction(PyObject* obj, ssize_t arg_pos); @@ -114,7 +114,7 @@ PyObject* ToPyObject(const paddle::framework::proto::VarType& type); PyObject* ToPyObject(const void* value); PyObject* ToPyObject( const std::unordered_map>& value); -PyObject* ToPyObject(const phi::Vocab& value); +PyObject* ToPyObject(const paddle::framework::Vocab& value); class PyTensorHook : public egr::TensorHook { public: diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 32bfeb8b1c3432..4462c5f305b6c1 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -942,7 +942,7 @@ All parameter, weight, gradient are variables in Paddle. py::return_value_policy::reference) .def("get_bytes", [](Variable &self) { - return py::bytes(*self.GetMutable()); + return py::bytes(*(self.GetMutable()->Get())); }) .def("set_string_list", [](Variable &self, Strings str_list) { diff --git a/paddle/phi/core/attribute.h b/paddle/phi/core/attribute.h index 33fedab7a24b4c..75a6b16f46e0bb 100644 --- a/paddle/phi/core/attribute.h +++ b/paddle/phi/core/attribute.h @@ -46,8 +46,7 @@ using Attribute = paddle::variant; + Place>; using AttributeMap = paddle::flat_hash_map; diff --git a/paddle/phi/core/cplus_string.h b/paddle/phi/core/cplus_string.h new file mode 100644 index 00000000000000..abcaedbc5f300d --- /dev/null +++ b/paddle/phi/core/cplus_string.h @@ -0,0 +1,74 @@ +/* Copyright (c) 2022 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 + +#include "paddle/phi/core/extended_tensor.h" + +namespace phi { + +class CPlusString : public ExtendedTensor, + public TypeInfoTraits { + public: + CPlusString() = default; + + CPlusString(CPlusString&& other) = default; + + CPlusString(const CPlusString& other) = default; + + CPlusString& operator=(const CPlusString& other) = default; + + CPlusString& operator=(const std::string& other) { + this->data_ = other; + return *this; + } + + CPlusString& operator=(std::string&& other) { + this->data_ = other; + return *this; + } + + CPlusString& operator=(CPlusString&& other) = default; + + /// \brief Destroy the CPlusString and release exclusive resources. + virtual ~CPlusString() = default; + + public: + /// \brief Returns the name of the class for type traits. + /// \return The name of the class. + static const char* name() { return "CPlusString"; } + + size_t size() const { return data_.size(); } + + void clear() { data_.clear(); } + + std::string::iterator begin() { return data_.begin(); } + + std::string* Get() { return &data_; } + + const std::string& Get() const { return data_; } + + std::string::const_iterator begin() const { return data_.begin(); } + + std::string::iterator end() { return data_.end(); } + + std::string::const_iterator end() const { return data_.end(); } + + private: + std::string data_; +}; + +} // namespace phi diff --git a/paddle/phi/core/kernel_context.cc b/paddle/phi/core/kernel_context.cc index cff7635e3444d6..8bac896c434418 100644 --- a/paddle/phi/core/kernel_context.cc +++ b/paddle/phi/core/kernel_context.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/core/kernel_context.h" +#include namespace phi { @@ -123,8 +124,6 @@ const AttrType& KernelContext::AttrAt(size_t idx) const { } } -using string_ptr = std::string*; - template const bool& KernelContext::AttrAt(size_t idx) const; template const int& KernelContext::AttrAt(size_t idx) const; template const int64_t& KernelContext::AttrAt(size_t idx) const; @@ -144,6 +143,5 @@ template const IntArray& KernelContext::AttrAt(size_t idx) const; template const DataType& KernelContext::AttrAt(size_t idx) const; template const DataLayout& KernelContext::AttrAt(size_t idx) const; template const Place& KernelContext::AttrAt(size_t idx) const; -template const string_ptr& KernelContext::AttrAt(size_t idx) const; } // namespace phi diff --git a/paddle/phi/core/kernel_factory.h b/paddle/phi/core/kernel_factory.h index 9df924e22c80b1..9e84379e7cef9f 100644 --- a/paddle/phi/core/kernel_factory.h +++ b/paddle/phi/core/kernel_factory.h @@ -141,8 +141,7 @@ enum class AttributeType { INT_ARRAY, DATA_TYPE, DATA_LAYOUT, - PLACE, - STRING_PTR, + PLACE }; struct AttributeArgDef { diff --git a/paddle/phi/core/kernel_registry.h b/paddle/phi/core/kernel_registry.h index 380a82104d033d..100e172dc070aa 100644 --- a/paddle/phi/core/kernel_registry.h +++ b/paddle/phi/core/kernel_registry.h @@ -23,11 +23,11 @@ #include "paddle/phi/core/custom_kernel.h" #include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/extended_tensor.h" #include "paddle/phi/core/kernel_factory.h" #include "paddle/phi/core/kernel_utils.h" #include "paddle/phi/core/macros.h" #include "paddle/phi/core/type_defs.h" -#include "paddle/phi/core/vocab.h" namespace phi { @@ -101,8 +101,8 @@ struct KernelArgsParseFunctor { default_tensor_layout, default_key.dtype(), arg_type); - } else if (arg_type == - std::type_index(typeid(const std::vector&))) { + } else if (arg_type == std::type_index(typeid( + const std::vector&))) { args_def->AppendInput(default_key.backend(), default_tensor_layout, default_key.dtype(), @@ -198,6 +198,11 @@ struct KernelArgsParseFunctor { default_tensor_layout, default_key.dtype(), arg_type); + } else if (arg_type == std::type_index(typeid(CPlusString*))) { + args_def->AppendOutput(default_key.backend(), + default_tensor_layout, + default_key.dtype(), + arg_type); } else if (arg_type == std::type_index(typeid(bool))) { args_def->AppendAttribute(AttributeType::BOOL); } else if (arg_type == std::type_index(typeid(int))) { @@ -210,8 +215,6 @@ struct KernelArgsParseFunctor { args_def->AppendAttribute(AttributeType::FLOAT64); } else if (arg_type == std::type_index(typeid(std::string))) { args_def->AppendAttribute(AttributeType::STRING); - } else if (arg_type == std::type_index(typeid(std::string*))) { - args_def->AppendAttribute(AttributeType::STRING_PTR); } else if (arg_type == std::type_index(typeid(const std::vector&))) { args_def->AppendAttribute(AttributeType::BOOLS); diff --git a/paddle/phi/core/kernel_utils.h b/paddle/phi/core/kernel_utils.h index 1e4f83e32fb88f..27d4bd877e08b9 100644 --- a/paddle/phi/core/kernel_utils.h +++ b/paddle/phi/core/kernel_utils.h @@ -17,8 +17,10 @@ #include "paddle/phi/backends/all_context.h" #include "paddle/phi/common/int_array.h" #include "paddle/phi/common/scalar.h" +#include "paddle/phi/core/cplus_string.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/enforce.h" +#include "paddle/phi/core/extended_tensor.h" #include "paddle/phi/core/kernel_context.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" @@ -26,7 +28,6 @@ #include "paddle/phi/core/string_tensor.h" #include "paddle/phi/core/tensor_array.h" #include "paddle/phi/core/type_defs.h" -#include "paddle/phi/core/vocab.h" namespace phi { @@ -167,24 +168,6 @@ namespace phi { } \ } -#define PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE_PTR(attr_type) \ - template \ - struct KernelCallHelper { \ - template \ - static void Compute(KernelContext* ctx, PreviousArgs&... pargs) { \ - static_assert(out_idx == 0, \ - "Kernel's Attributes should appear before Outputs."); \ - attr_type arg = const_cast(ctx->AttrAt(attr_idx)); \ - KernelCallHelper:: \ - template Compute( \ - ctx, pargs..., arg); \ - } \ - } - #define PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(attr_type) \ template \ struct KernelCallHelper { \ @@ -283,7 +266,7 @@ struct KernelImpl { PD_SPECIALIZE_KernelCallHelper_FOR_OPTIONAL_INPUT(DenseTensor); PD_SPECIALIZE_KernelCallHelper_FOR_OPTIONAL_INPUT(SelectedRows); PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(DenseTensor); - PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(Vocab); + PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(ExtendedTensor); PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(TensorBase); PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_INPUT(SelectedRows); PD_SPECIALIZE_KernelCallHelper_FOR_INPUT(SelectedRows); @@ -315,7 +298,6 @@ struct KernelImpl { PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(DataType); PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(DataLayout); PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE(Place); - PD_SPECIALIZE_KernelCallHelper_FOR_ATTRIBUTE_PTR(std::string*); PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(std::string); PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(Scalar); PD_SPECIALIZE_KernelCallHelper_FOR_CONST_ATTRIBUTE_REF(IntArray); @@ -344,6 +326,7 @@ struct KernelImpl { PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_OUTPUT(StringTensor); PD_SPECIALIZE_KernelCallHelper_FOR_OUTPUT(TensorArray); + PD_SPECIALIZE_KernelCallHelper_FOR_OUTPUT(CPlusString); /* End case */ template diff --git a/paddle/phi/core/vocab.h b/paddle/phi/core/vocab.h deleted file mode 100644 index e6111c34d6ccef..00000000000000 --- a/paddle/phi/core/vocab.h +++ /dev/null @@ -1,85 +0,0 @@ -/* Copyright (c) 2022 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 - -#include "paddle/phi/core/extended_tensor.h" - -namespace phi { - -class Vocab : public ExtendedTensor, public TypeInfoTraits { - public: - Vocab() = default; - - Vocab(Vocab&& other) = default; - - Vocab(const Vocab& other) = default; - - Vocab& operator=(const Vocab& other) = default; - - Vocab& operator=(Vocab&& other) = default; - - /// \brief Destroy the Vocab and release exclusive resources. - virtual ~Vocab() = default; - - public: - /// \brief Returns the name of the class for type traits. - /// \return The name of the class. - static const char* name() { return "Vocab"; } - - size_t size() const { return data_.size(); } - - void clear() { data_.clear(); } - - void emplace(const std::wstring& key, std::int32_t value) { - data_.emplace(key, value); - } - - std::int32_t at(const std::wstring& key) { return data_.at(key); } - - std::int32_t at(const std::wstring& key) const { return data_.at(key); } - - std::unordered_map::iterator find( - const std::wstring& key) { - return data_.find(key); - } - - std::unordered_map::const_iterator find( - const std::wstring& key) const { - return data_.find(key); - } - - std::unordered_map::iterator begin() { - return data_.begin(); - } - - std::unordered_map::const_iterator begin() const { - return data_.begin(); - } - - std::unordered_map::iterator end() { - return data_.end(); - } - - std::unordered_map::const_iterator end() const { - return data_.end(); - } - - private: - std::unordered_map data_; -}; - -} // namespace phi diff --git a/paddle/phi/kernels/CMakeLists.txt b/paddle/phi/kernels/CMakeLists.txt index 6a082fa8c69628..75659d2bcd81af 100644 --- a/paddle/phi/kernels/CMakeLists.txt +++ b/paddle/phi/kernels/CMakeLists.txt @@ -77,8 +77,7 @@ set(COMMON_KERNEL_DEPS phi_data_layout_transform gpc utf8proc - device_memory_aligment - string_array) + device_memory_aligment) set(COMMON_KERNEL_DEPS ${COMMON_KERNEL_DEPS} processgroup) if(WITH_NCCL OR WITH_RCCL) diff --git a/paddle/phi/kernels/cpu/save_combine_kernel.cc b/paddle/phi/kernels/cpu/save_combine_kernel.cc deleted file mode 100644 index 71c11ef56d25aa..00000000000000 --- a/paddle/phi/kernels/cpu/save_combine_kernel.cc +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (c) 2022 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/kernels/save_combine_kernel.h" - -#include "paddle/phi/backends/cpu/cpu_context.h" -#include "paddle/phi/core/kernel_registry.h" -#include "paddle/phi/kernels/impl/save_combine_kernel_impl.h" - -PD_REGISTER_KERNEL(save_combine_tensor, - CPU, - ALL_LAYOUT, - phi::SaveCombineTensorKernel, - int, - int64_t, - float, - double, - phi::dtype::bfloat16) {} - -PD_REGISTER_KERNEL(save_combine_vocab, - CPU, - ALL_LAYOUT, - phi::SaveCombineVocabKernel, - int, - int64_t, - float, - double, - phi::dtype::bfloat16) {} diff --git a/paddle/phi/kernels/impl/save_combine_kernel_impl.h b/paddle/phi/kernels/impl/save_combine_kernel_impl.h deleted file mode 100644 index 8f806c490307cc..00000000000000 --- a/paddle/phi/kernels/impl/save_combine_kernel_impl.h +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright (c) 2022 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/kernels/save_combine_kernel.h" - -#include -#include -#include -#include -#include -#include - -#include "paddle/fluid/framework/string_array.h" - -#include "paddle/phi/backends/dynload/port.h" -#include "paddle/phi/core/serialization.h" -#include "paddle/phi/kernels/funcs/data_type_transform.h" - -namespace phi { - -inline void SaveToMemory(const std::string& file_path, - const std::ostringstream& ss, - bool save_to_memory, - std::string* output) { - if (save_to_memory) { - PADDLE_ENFORCE_NE(output, - nullptr, - phi::errors::InvalidArgument( - "Cannot find variable Y for save_combine_op")); - *output = ss.str(); - } else { - MkDirRecursively(DirName(file_path).c_str()); - std::ofstream fout(file_path, std::ios::binary); - PADDLE_ENFORCE_EQ(static_cast(fout), - true, - phi::errors::Unavailable( - "Cannot open %s to save variables.", file_path)); - fout << ss.str(); - fout.close(); - } -} - -template -void SaveCombineTensorKernel(const Context& dev_ctx, - const std::vector& x, - const std::string& file_path, - bool overwrite, - bool save_as_fp16, - bool save_to_memory, - std::string* y) { - bool is_present = FileExists(file_path); - if (is_present && !overwrite) { - PADDLE_THROW(phi::errors::PreconditionNotMet( - "%s exists! Cannot save_combine to it when overwrite is set to " - "false.", - file_path, - overwrite)); - } - - std::ostringstream ss; - PADDLE_ENFORCE_GT(x.size(), - 0UL, - phi::errors::InvalidArgument( - "The number of variables to be saved is %d, expect " - "it to be greater than 0.", - x.size())); - - for (size_t i = 0; i < x.size(); i++) { - auto& tensor = *(x[i]); - PADDLE_ENFORCE_EQ( - tensor.IsInitialized(), - true, - phi::errors::InvalidArgument( - "The Tensor with Index (%d) to be saved is not initialized.", i)); - // Serialize tensors one by one - // Check types to see if a fp16 transformation is required - auto in_dtype = tensor.dtype(); - auto out_dtype = save_as_fp16 ? DataType::FLOAT16 : in_dtype; - - if (in_dtype != out_dtype) { - phi::DenseTensor out = - phi::funcs::TransDataType(dev_ctx, tensor, out_dtype); - // copy LoD info to the new tensor - out.set_lod(tensor.lod()); - phi::SerializeToStream(ss, out, dev_ctx); - } else { - phi::SerializeToStream(ss, tensor, dev_ctx); - } - } - - SaveToMemory(file_path, ss, save_to_memory, y); -} - -template -void SaveCombineVocabKernel(const Context& dev_ctx, - const std::vector& x, - const std::string& file_path, - bool overwrite, - bool save_as_fp16, - bool save_to_memory, - std::string* y) { - bool is_present = FileExists(file_path); - if (is_present && !overwrite) { - PADDLE_THROW(phi::errors::PreconditionNotMet( - "%s exists! Cannot save_combine to it when overwrite is set to " - "false.", - file_path, - overwrite)); - } - - std::ostringstream ss; - PADDLE_ENFORCE_GT(x.size(), - 0UL, - phi::errors::InvalidArgument( - "The number of variables to be saved is %d, expect " - "it to be greater than 0.", - x.size())); - - for (size_t i = 0; i < x.size(); i++) { - auto& tensor = *(x[i]); - std::unordered_map data; - for (auto it = tensor.begin(); it != tensor.end(); ++it) { - std::string t; - paddle::framework::ConvertWstrToStr(it->first, &t); - data.emplace(t, it->second); - } - paddle::framework::StringMapToStream(ss, data); - } - - SaveToMemory(file_path, ss, save_to_memory, y); -} - -} // namespace phi diff --git a/paddle/phi/kernels/save_combine_kernel.h b/paddle/phi/kernels/save_combine_kernel.h deleted file mode 100644 index d8628a902035f1..00000000000000 --- a/paddle/phi/kernels/save_combine_kernel.h +++ /dev/null @@ -1,42 +0,0 @@ -/* Copyright (c) 2022 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 - -#include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/vocab.h" - -namespace phi { - -template -void SaveCombineTensorKernel(const Context& dev_ctx, - const std::vector& x, - const std::string& file_path, - bool overwrite, - bool save_as_fp16, - bool save_to_memory, - std::string* y); - -template -void SaveCombineVocabKernel(const Context& dev_ctx, - const std::vector& x, - const std::string& file_path, - bool overwrite, - bool save_as_fp16, - bool save_to_memory, - std::string* y); - -} // namespace phi diff --git a/paddle/phi/ops/compat/save_combine_sig.cc b/paddle/phi/ops/compat/save_combine_sig.cc index a89a44bc234237..8c9760410b35ae 100644 --- a/paddle/phi/ops/compat/save_combine_sig.cc +++ b/paddle/phi/ops/compat/save_combine_sig.cc @@ -22,14 +22,14 @@ KernelSignature SaveCombineOpArgumentMapping( return KernelSignature( "save_combine_tensor", {"X"}, - {"file_path", "overwrite", "save_as_fp16", "save_to_memory", "Y"}, - {}); + {"file_path", "overwrite", "save_as_fp16", "save_to_memory"}, + {"Y"}); } else { return KernelSignature( "save_combine_vocab", {"X"}, - {"file_path", "overwrite", "save_as_fp16", "save_to_memory", "Y"}, - {}); + {"file_path", "overwrite", "save_as_fp16", "save_to_memory"}, + {"Y"}); } } From 9929a2848863d937b09434554189013c2ccb338e Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Mon, 21 Nov 2022 11:06:31 +0000 Subject: [PATCH 04/11] fix ci bugs --- paddle/fluid/framework/operator.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 5a512ebc395059..3759882d0130ed 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -2982,6 +2982,9 @@ void OperatorWithKernel::BuildPhiKernelContext( // Note: If the input LoDTensorArray size is 0, the output // LoDTensorArray is also 0 phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); + } else if (var->template IsType()) { + tensor_out = var->template GetMutable(); + phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } else if (!var->IsInitialized()) { // The following is for RAW type of var if (output_defs[i].type_index == From c4a66e6adf815f49ba8a6c29622b1681481aedfb Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Wed, 23 Nov 2022 02:43:14 +0000 Subject: [PATCH 05/11] fix ci bugs --- paddle/fluid/framework/string_array.h | 6 ++++++ paddle/fluid/operators/save_combine_op.h | 3 ++- paddle/fluid/pybind/eager_utils.cc | 5 ++++- paddle/fluid/pybind/pybind.cc | 3 ++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/string_array.h b/paddle/fluid/framework/string_array.h index f81be7fd468c2d..2b8f377a72fc9d 100644 --- a/paddle/fluid/framework/string_array.h +++ b/paddle/fluid/framework/string_array.h @@ -39,6 +39,12 @@ class Vocab : public phi::ExtendedTensor, Vocab& operator=(Vocab&& other) = default; + Vocab& operator=( + const std::unordered_map& other) { + this->data_ = other; + return *this; + } + /// \brief Destroy the Vocab and release exclusive resources. virtual ~Vocab() = default; diff --git a/paddle/fluid/operators/save_combine_op.h b/paddle/fluid/operators/save_combine_op.h index 3f7fc64b95e6f7..ca25682b0fb51d 100644 --- a/paddle/fluid/operators/save_combine_op.h +++ b/paddle/fluid/operators/save_combine_op.h @@ -122,7 +122,8 @@ void SaveCombineVocabKernel( bool save_to_memory, phi::CPlusString* out) { std::string* y = out->Get(); - std::vector x(inputs.size()); + std::vector x; + x.reserve(inputs.size()); for (auto input : inputs) { x.push_back(static_cast(input)); } diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index e96fb92d0fc15c..a8eb9be959fe46 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -516,7 +516,10 @@ paddle::framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, paddle::framework::Vocab CastPyArg2Vocab(PyObject* obj, ssize_t arg_pos) { if (PyDict_Check(obj)) { - return ::pybind11::handle(obj).cast(); + paddle::framework::Vocab vocab; + vocab = ::pybind11::handle(obj) + .cast>(); + return vocab; } else { PADDLE_THROW(platform::errors::InvalidArgument( "argument (position %d) must be dict, but got %s", diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 4462c5f305b6c1..b3f4b2481cf47f 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -949,7 +949,8 @@ All parameter, weight, gradient are variables in Paddle. *self.GetMutable() = str_list; }) .def("set_vocab", - [](Variable &self, Vocab vocab) { + [](Variable &self, + const std::unordered_map &vocab) { *self.GetMutable() = vocab; }) .def( From 01e976ccedcf9fb7864ce5653ff01b6a845bbe85 Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Mon, 28 Nov 2022 12:03:23 +0000 Subject: [PATCH 06/11] perfect according comment --- cmake/operators.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake/operators.cmake b/cmake/operators.cmake index 53c6df575cd1ae..e626669d2a73af 100644 --- a/cmake/operators.cmake +++ b/cmake/operators.cmake @@ -27,7 +27,6 @@ function(find_register FILENAME PATTERN OUTPUT) endfunction() function(find_phi_register FILENAME ADD_PATH) - # find the op_name of REGISTER_OPERATOR(op_name, ...), REGISTER_OP_CPU_KERNEL(op_name, ...) , etc. # set op_name to OUTPUT set(options "") set(oneValueArgs "") From 4010a6f68411a0a47cbc313105870c522b1e3d0c Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Wed, 30 Nov 2022 02:53:10 +0000 Subject: [PATCH 07/11] fix ci compile bugs --- paddle/fluid/framework/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index 9fa2658580bbaf..71288a44c09698 100755 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -115,7 +115,7 @@ proto_library(trainer_desc_proto SRCS trainer_desc.proto DEPS framework_proto cc_library( string_array SRCS string_array.cc - DEPS utf8proc) + DEPS utf8proc phi_enforce) cc_library( data_type From 4ec7569227f3d76ba20e984c701b0384799b1ffc Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Fri, 2 Dec 2022 03:15:38 +0000 Subject: [PATCH 08/11] add raw tensor --- paddle/fluid/framework/operator.cc | 10 +-- paddle/fluid/framework/string_array.h | 53 ++++++++++++- paddle/fluid/framework/var_type_traits.cc | 1 + paddle/fluid/framework/var_type_traits.h | 4 +- .../fluid/framework/var_type_traits_test.cc | 1 + paddle/fluid/operators/save_combine_op.h | 21 ++++-- paddle/phi/core/cplus_string.h | 74 ------------------- paddle/phi/core/extended_tensor.h | 3 +- paddle/phi/core/kernel_registry.h | 2 +- paddle/phi/core/kernel_utils.h | 4 +- paddle/phi/core/raw_tensor.h | 66 +++++++++++++++++ 11 files changed, 146 insertions(+), 93 deletions(-) delete mode 100644 paddle/phi/core/cplus_string.h create mode 100644 paddle/phi/core/raw_tensor.h diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index f112431d38ccb6..57d45c77266297 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -38,6 +38,7 @@ limitations under the License. */ #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/kernel_context.h" #include "paddle/phi/core/kernel_factory.h" +#include "paddle/phi/core/raw_tensor.h" #include "paddle/phi/ops/compat/signatures.h" namespace phi { @@ -2985,15 +2986,12 @@ void OperatorWithKernel::BuildPhiKernelContext( // Note: If the input LoDTensorArray size is 0, the output // LoDTensorArray is also 0 phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); - } else if (var->template IsType()) { - tensor_out = var->template GetMutable(); + } else if (var->template IsType()) { + tensor_out = var->template GetMutable(); phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } else if (!var->IsInitialized()) { // The following is for RAW type of var - if (output_defs[i].type_index == - std::type_index(typeid(phi::CPlusString*))) { - tensor_out = var->template GetMutable(); - } + tensor_out = var->template GetMutable(); phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } else { PADDLE_THROW(platform::errors::Unimplemented( diff --git a/paddle/fluid/framework/string_array.h b/paddle/fluid/framework/string_array.h index 2b8f377a72fc9d..4dbbd399782629 100644 --- a/paddle/fluid/framework/string_array.h +++ b/paddle/fluid/framework/string_array.h @@ -20,7 +20,6 @@ limitations under the License. */ #include #include #include -#include "paddle/phi/core/cplus_string.h" #include "paddle/phi/core/extended_tensor.h" namespace paddle { @@ -95,7 +94,57 @@ class Vocab : public phi::ExtendedTensor, std::unordered_map data_; }; -using String = phi::CPlusString; +class String : public phi::ExtendedTensor, + public phi::TypeInfoTraits { + public: + String() = default; + + String(String&& other) = default; + + String(const String& other) = default; + + String& operator=(const String& other) = default; + + String& operator=(const std::string& other) { + this->data_ = other; + return *this; + } + + String& operator=(std::string&& other) { + this->data_ = other; + return *this; + } + + String& operator=(String&& other) = default; + + /// \brief Destroy the String and release exclusive resources. + virtual ~String() = default; + + public: + /// \brief Returns the name of the class for type traits. + /// \return The name of the class. + static const char* name() { return "String"; } + + size_t size() const { return data_.size(); } + + void clear() { data_.clear(); } + + std::string::iterator begin() { return data_.begin(); } + + std::string* Get() { return &data_; } + + const std::string& Get() const { return data_; } + + std::string::const_iterator begin() const { return data_.begin(); } + + std::string::iterator end() { return data_.end(); } + + std::string::const_iterator end() const { return data_.end(); } + + private: + std::string data_; +}; + using Strings = std::vector; // Convert the std::string type to the std::string type. diff --git a/paddle/fluid/framework/var_type_traits.cc b/paddle/fluid/framework/var_type_traits.cc index 2a53e2f88523bb..9982c25340384b 100644 --- a/paddle/fluid/framework/var_type_traits.cc +++ b/paddle/fluid/framework/var_type_traits.cc @@ -42,6 +42,7 @@ #endif #include "paddle/fluid/operators/cuda_graph_with_in_out.h" +#include "paddle/phi/core/raw_tensor.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/var_type_traits.h b/paddle/fluid/framework/var_type_traits.h index eb3f90006a7889..74d3d870c05200 100644 --- a/paddle/fluid/framework/var_type_traits.h +++ b/paddle/fluid/framework/var_type_traits.h @@ -56,6 +56,7 @@ class DenseTensor; class SelectedRows; class SparseCooTensor; class SparseCsrTensor; +class RawTensor; } // namespace phi // Users should add forward declarations here @@ -219,7 +220,8 @@ using VarTypeRegistry = detail::VarTypeRegistryImpl< float, Vocab, std::vector, - std::vector>; + std::vector, + phi::RawTensor>; template struct VarTypeTrait { static_assert(VarTypeRegistry::IsRegistered(), "Must be registered type"); diff --git a/paddle/fluid/framework/var_type_traits_test.cc b/paddle/fluid/framework/var_type_traits_test.cc index 48e2a9a9c62784..3e0d98d664e6ac 100644 --- a/paddle/fluid/framework/var_type_traits_test.cc +++ b/paddle/fluid/framework/var_type_traits_test.cc @@ -38,6 +38,7 @@ #if defined(PADDLE_WITH_XPU_BKCL) #include "paddle/fluid/platform/device/xpu/bkcl_helper.h" #endif +#include "paddle/phi/core/raw_tensor.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/operators/save_combine_op.h b/paddle/fluid/operators/save_combine_op.h index 1ebb036a3985ba..fefe86cf50ee41 100644 --- a/paddle/fluid/operators/save_combine_op.h +++ b/paddle/fluid/operators/save_combine_op.h @@ -31,6 +31,7 @@ limitations under the License. */ #include "paddle/fluid/platform/device_context.h" #include "paddle/phi/backends/dynload/port.h" #include "paddle/phi/core/dense_tensor.h" +#include "paddle/phi/core/raw_tensor.h" #include "paddle/phi/core/serialization.h" namespace paddle { @@ -65,8 +66,13 @@ void SaveCombineTensorKernel(const Context& dev_ctx, bool overwrite, bool save_as_fp16, bool save_to_memory, - phi::CPlusString* out) { - std::string* y = out->Get(); + phi::ExtendedTensor* out) { + std::string* y = nullptr; + if (out != nullptr) { + auto raw_out = static_cast(out); + y = raw_out->GetMutable(); + } + bool is_present = FileExists(file_path); if (is_present && !overwrite) { PADDLE_THROW(phi::errors::PreconditionNotMet( @@ -120,8 +126,13 @@ void SaveCombineVocabKernel( bool overwrite, bool save_as_fp16, bool save_to_memory, - phi::CPlusString* out) { - std::string* y = out->Get(); + phi::ExtendedTensor* out) { + std::string* y = nullptr; + if (out != nullptr) { + auto raw_out = static_cast(out); + y = raw_out->GetMutable(); + } + std::vector x; x.reserve(inputs.size()); for (auto input : inputs) { @@ -167,7 +178,7 @@ class SaveCombineOpKernel : public framework::OpKernel { auto overwrite = ctx.Attr("overwrite"); auto save_as_fp16 = ctx.Attr("save_as_fp16"); auto save_to_memory = ctx.Attr("save_to_memory"); - auto output = ctx.Output("Y"); + auto output = ctx.Output("Y"); auto inp_var_names = ctx.InputNames("X"); auto& inp_vars = ctx.MultiInputVar("X"); diff --git a/paddle/phi/core/cplus_string.h b/paddle/phi/core/cplus_string.h deleted file mode 100644 index abcaedbc5f300d..00000000000000 --- a/paddle/phi/core/cplus_string.h +++ /dev/null @@ -1,74 +0,0 @@ -/* Copyright (c) 2022 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 - -#include "paddle/phi/core/extended_tensor.h" - -namespace phi { - -class CPlusString : public ExtendedTensor, - public TypeInfoTraits { - public: - CPlusString() = default; - - CPlusString(CPlusString&& other) = default; - - CPlusString(const CPlusString& other) = default; - - CPlusString& operator=(const CPlusString& other) = default; - - CPlusString& operator=(const std::string& other) { - this->data_ = other; - return *this; - } - - CPlusString& operator=(std::string&& other) { - this->data_ = other; - return *this; - } - - CPlusString& operator=(CPlusString&& other) = default; - - /// \brief Destroy the CPlusString and release exclusive resources. - virtual ~CPlusString() = default; - - public: - /// \brief Returns the name of the class for type traits. - /// \return The name of the class. - static const char* name() { return "CPlusString"; } - - size_t size() const { return data_.size(); } - - void clear() { data_.clear(); } - - std::string::iterator begin() { return data_.begin(); } - - std::string* Get() { return &data_; } - - const std::string& Get() const { return data_; } - - std::string::const_iterator begin() const { return data_.begin(); } - - std::string::iterator end() { return data_.end(); } - - std::string::const_iterator end() const { return data_.end(); } - - private: - std::string data_; -}; - -} // namespace phi diff --git a/paddle/phi/core/extended_tensor.h b/paddle/phi/core/extended_tensor.h index 304a6e93b9271b..404e1014bb3280 100644 --- a/paddle/phi/core/extended_tensor.h +++ b/paddle/phi/core/extended_tensor.h @@ -23,8 +23,7 @@ namespace phi { /// \brief The ExtendedTensor is a interface for custom designed class. /// If you want to pass some self-designed data as input/output to kernels, /// you can inherit from this class to store your self-designed data. -class ExtendedTensor : public TensorBase, - public TypeInfoTraits { +class ExtendedTensor : public TensorBase { public: ExtendedTensor() = default; virtual ~ExtendedTensor() = default; diff --git a/paddle/phi/core/kernel_registry.h b/paddle/phi/core/kernel_registry.h index 100e172dc070aa..a195f2ad60cbcb 100644 --- a/paddle/phi/core/kernel_registry.h +++ b/paddle/phi/core/kernel_registry.h @@ -198,7 +198,7 @@ struct KernelArgsParseFunctor { default_tensor_layout, default_key.dtype(), arg_type); - } else if (arg_type == std::type_index(typeid(CPlusString*))) { + } else if (arg_type == std::type_index(typeid(ExtendedTensor*))) { args_def->AppendOutput(default_key.backend(), default_tensor_layout, default_key.dtype(), diff --git a/paddle/phi/core/kernel_utils.h b/paddle/phi/core/kernel_utils.h index f0992117daf8ea..18e3478a70983f 100644 --- a/paddle/phi/core/kernel_utils.h +++ b/paddle/phi/core/kernel_utils.h @@ -17,11 +17,11 @@ #include "paddle/phi/backends/all_context.h" #include "paddle/phi/common/int_array.h" #include "paddle/phi/common/scalar.h" -#include "paddle/phi/core/cplus_string.h" #include "paddle/phi/core/dense_tensor.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/extended_tensor.h" #include "paddle/phi/core/kernel_context.h" +#include "paddle/phi/core/raw_tensor.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" @@ -326,7 +326,7 @@ struct KernelImpl { PD_SPECIALIZE_KernelCallHelper_FOR_MULTI_OUTPUT(StringTensor); PD_SPECIALIZE_KernelCallHelper_FOR_OUTPUT(TensorArray); - PD_SPECIALIZE_KernelCallHelper_FOR_OUTPUT(CPlusString); + PD_SPECIALIZE_KernelCallHelper_FOR_OUTPUT(ExtendedTensor); /* End case */ template diff --git a/paddle/phi/core/raw_tensor.h b/paddle/phi/core/raw_tensor.h new file mode 100644 index 00000000000000..00b2daddcf6b5c --- /dev/null +++ b/paddle/phi/core/raw_tensor.h @@ -0,0 +1,66 @@ +/* Copyright (c) 2022 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 + +#include "paddle/phi/core/extended_tensor.h" +#include "paddle/utils/any.h" + +namespace phi { + +class RawTensor : public ExtendedTensor, + public TypeInfoTraits { + public: + RawTensor() = default; + + RawTensor(RawTensor&& other) = default; + + RawTensor(const RawTensor& other) = default; + + RawTensor& operator=(RawTensor&& other) = default; + + /// \brief Destroy the RawTensor and release exclusive resources. + virtual ~RawTensor() = default; + + public: + /// \brief Returns the name of the class for type traits. + /// \return The name of the class. + static const char* name() { return "RawTensor"; } + + template + T* GetMutable() { + if (!data_.empty()) { + data_deleter_(); + } + T* created_data = new T(); + data_ = created_data; + data_deleter_ = [created_data]() { delete created_data; }; + data_type_ = std::type_index(typeid(T)); + return created_data; + } + + template + bool IsType() const { + return std::type_index(typeid(T)) == data_type_; + } + + private: + paddle::any data_; + std::function data_deleter_; + std::type_index data_type_ = std::type_index(typeid(void)); +}; + +} // namespace phi From c4cca54cc27a3e0a14eaeb18e834d02426445771 Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Fri, 2 Dec 2022 10:27:49 +0000 Subject: [PATCH 09/11] fix ci bugs --- paddle/fluid/framework/operator.cc | 8 ++--- .../core => fluid/framework}/raw_tensor.h | 33 ++++++++++++++++--- paddle/fluid/framework/var_type_traits.cc | 2 +- paddle/fluid/framework/var_type_traits.h | 4 +-- .../fluid/framework/var_type_traits_test.cc | 2 +- paddle/fluid/operators/save_combine_op.h | 8 ++--- paddle/fluid/pybind/pybind.cc | 8 ++++- paddle/phi/core/kernel_utils.h | 1 - 8 files changed, 47 insertions(+), 19 deletions(-) rename paddle/{phi/core => fluid/framework}/raw_tensor.h (63%) diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 57d45c77266297..b0753f24849ac3 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -23,6 +23,7 @@ limitations under the License. */ #include "paddle/fluid/framework/details/nan_inf_utils.h" #include "paddle/fluid/framework/op_call_stack.h" #include "paddle/fluid/framework/phi_utils.h" +#include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/framework/shape_inference.h" #include "paddle/fluid/framework/transfer_scope_cache.h" #include "paddle/fluid/framework/unused_var_check.h" @@ -38,7 +39,6 @@ limitations under the License. */ #include "paddle/phi/common/scalar.h" #include "paddle/phi/core/kernel_context.h" #include "paddle/phi/core/kernel_factory.h" -#include "paddle/phi/core/raw_tensor.h" #include "paddle/phi/ops/compat/signatures.h" namespace phi { @@ -2986,12 +2986,12 @@ void OperatorWithKernel::BuildPhiKernelContext( // Note: If the input LoDTensorArray size is 0, the output // LoDTensorArray is also 0 phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); - } else if (var->template IsType()) { - tensor_out = var->template GetMutable(); + } else if (var->template IsType()) { + tensor_out = var->template GetMutable(); phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } else if (!var->IsInitialized()) { // The following is for RAW type of var - tensor_out = var->template GetMutable(); + tensor_out = var->template GetMutable(); phi_kernel_context->EmplaceBackOutputWithoutSetRange(tensor_out); } else { PADDLE_THROW(platform::errors::Unimplemented( diff --git a/paddle/phi/core/raw_tensor.h b/paddle/fluid/framework/raw_tensor.h similarity index 63% rename from paddle/phi/core/raw_tensor.h rename to paddle/fluid/framework/raw_tensor.h index 00b2daddcf6b5c..14e89b712531cd 100644 --- a/paddle/phi/core/raw_tensor.h +++ b/paddle/fluid/framework/raw_tensor.h @@ -19,10 +19,14 @@ limitations under the License. */ #include "paddle/phi/core/extended_tensor.h" #include "paddle/utils/any.h" -namespace phi { - -class RawTensor : public ExtendedTensor, - public TypeInfoTraits { +namespace paddle { +namespace framework { + +/// \brief Fluid Kernel and PHI Kernel will be unified in the future. +/// So, we need a class in PHI that can represent the RAW type in Fluid. +/// The RawTensor is for PHI Kernel that has RAW type arguments. +class RawTensor : public phi::ExtendedTensor, + public phi::TypeInfoTraits { public: RawTensor() = default; @@ -52,6 +56,24 @@ class RawTensor : public ExtendedTensor, return created_data; } + template + T* Get() { + if (!data_.empty()) { + try { + return paddle::any_cast(data_); + } catch (paddle::bad_any_cast&) { + PADDLE_THROW(phi::errors::InvalidArgument( + "Invalid data type error, expected %s, actual %s.", + typeid(T).name(), + data_type_.name())); + } + } else { + PADDLE_THROW( + phi::errors::Unavailable("RawTensor is not initialized, if you want " + "to create data, you can use GetMutable.")); + } + } + template bool IsType() const { return std::type_index(typeid(T)) == data_type_; @@ -63,4 +85,5 @@ class RawTensor : public ExtendedTensor, std::type_index data_type_ = std::type_index(typeid(void)); }; -} // namespace phi +} // namespace framework +} // namespace paddle diff --git a/paddle/fluid/framework/var_type_traits.cc b/paddle/fluid/framework/var_type_traits.cc index 9982c25340384b..d73c9b7d959573 100644 --- a/paddle/fluid/framework/var_type_traits.cc +++ b/paddle/fluid/framework/var_type_traits.cc @@ -41,8 +41,8 @@ #include "paddle/fluid/platform/device/xpu/bkcl_helper.h" #endif +#include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/operators/cuda_graph_with_in_out.h" -#include "paddle/phi/core/raw_tensor.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/framework/var_type_traits.h b/paddle/fluid/framework/var_type_traits.h index 74d3d870c05200..1e6c110e86a30f 100644 --- a/paddle/fluid/framework/var_type_traits.h +++ b/paddle/fluid/framework/var_type_traits.h @@ -23,6 +23,7 @@ #include "paddle/fluid/framework/feed_fetch_type.h" #include "paddle/fluid/framework/lod_tensor_array.h" +#include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/framework/string_array.h" #include "paddle/fluid/platform/place.h" #ifdef PADDLE_WITH_CUDA @@ -56,7 +57,6 @@ class DenseTensor; class SelectedRows; class SparseCooTensor; class SparseCsrTensor; -class RawTensor; } // namespace phi // Users should add forward declarations here @@ -221,7 +221,7 @@ using VarTypeRegistry = detail::VarTypeRegistryImpl< Vocab, std::vector, std::vector, - phi::RawTensor>; + RawTensor>; template struct VarTypeTrait { static_assert(VarTypeRegistry::IsRegistered(), "Must be registered type"); diff --git a/paddle/fluid/framework/var_type_traits_test.cc b/paddle/fluid/framework/var_type_traits_test.cc index 3e0d98d664e6ac..54a9fa60d95786 100644 --- a/paddle/fluid/framework/var_type_traits_test.cc +++ b/paddle/fluid/framework/var_type_traits_test.cc @@ -38,7 +38,7 @@ #if defined(PADDLE_WITH_XPU_BKCL) #include "paddle/fluid/platform/device/xpu/bkcl_helper.h" #endif -#include "paddle/phi/core/raw_tensor.h" +#include "paddle/fluid/framework/raw_tensor.h" namespace paddle { namespace framework { diff --git a/paddle/fluid/operators/save_combine_op.h b/paddle/fluid/operators/save_combine_op.h index fefe86cf50ee41..77ee7fabf46062 100644 --- a/paddle/fluid/operators/save_combine_op.h +++ b/paddle/fluid/operators/save_combine_op.h @@ -27,11 +27,11 @@ limitations under the License. */ #include "paddle/fluid/framework/data_type_transform.h" #include "paddle/fluid/framework/lod_tensor.h" #include "paddle/fluid/framework/op_registry.h" +#include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/framework/string_array.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/phi/backends/dynload/port.h" #include "paddle/phi/core/dense_tensor.h" -#include "paddle/phi/core/raw_tensor.h" #include "paddle/phi/core/serialization.h" namespace paddle { @@ -69,7 +69,7 @@ void SaveCombineTensorKernel(const Context& dev_ctx, phi::ExtendedTensor* out) { std::string* y = nullptr; if (out != nullptr) { - auto raw_out = static_cast(out); + auto raw_out = static_cast(out); y = raw_out->GetMutable(); } @@ -129,7 +129,7 @@ void SaveCombineVocabKernel( phi::ExtendedTensor* out) { std::string* y = nullptr; if (out != nullptr) { - auto raw_out = static_cast(out); + auto raw_out = static_cast(out); y = raw_out->GetMutable(); } @@ -178,7 +178,7 @@ class SaveCombineOpKernel : public framework::OpKernel { auto overwrite = ctx.Attr("overwrite"); auto save_as_fp16 = ctx.Attr("save_as_fp16"); auto save_to_memory = ctx.Attr("save_to_memory"); - auto output = ctx.Output("Y"); + auto output = ctx.Output("Y"); auto inp_var_names = ctx.InputNames("X"); auto& inp_vars = ctx.MultiInputVar("X"); diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 631fa0c6877b27..5f7857ec6e1080 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -55,6 +55,7 @@ limitations under the License. */ #include "paddle/fluid/framework/parallel_executor.h" #include "paddle/fluid/framework/phi_utils.h" #include "paddle/fluid/framework/prune.h" +#include "paddle/fluid/framework/raw_tensor.h" #include "paddle/fluid/framework/reader.h" #include "paddle/fluid/framework/scope_pool.h" #include "paddle/fluid/framework/selected_rows_utils.h" @@ -942,7 +943,12 @@ All parameter, weight, gradient are variables in Paddle. py::return_value_policy::reference) .def("get_bytes", [](Variable &self) { - return py::bytes(*(self.GetMutable()->Get())); + if (self.IsType()) { + return py::bytes(*(self.GetMutable()->Get())); + } else { + return py::bytes( + *(self.GetMutable()->Get())); + } }) .def("set_string_list", [](Variable &self, Strings str_list) { diff --git a/paddle/phi/core/kernel_utils.h b/paddle/phi/core/kernel_utils.h index 18e3478a70983f..1eb3a52aebad11 100644 --- a/paddle/phi/core/kernel_utils.h +++ b/paddle/phi/core/kernel_utils.h @@ -21,7 +21,6 @@ #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/extended_tensor.h" #include "paddle/phi/core/kernel_context.h" -#include "paddle/phi/core/raw_tensor.h" #include "paddle/phi/core/selected_rows.h" #include "paddle/phi/core/sparse_coo_tensor.h" #include "paddle/phi/core/sparse_csr_tensor.h" From 9bbd5122e870860d85ebf6b2e3212819a2b4e04c Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Mon, 5 Dec 2022 08:39:41 +0000 Subject: [PATCH 10/11] modify code by comment --- paddle/fluid/framework/raw_tensor.h | 41 ++++++++++++++--------- paddle/fluid/framework/string_array.h | 4 +-- paddle/fluid/framework/variable_test.cc | 2 +- paddle/fluid/jit/layer_test.cc | 2 +- paddle/fluid/operators/save_combine_op.cu | 2 +- paddle/fluid/pybind/pybind.cc | 4 +-- paddle/phi/core/kernel_context.cc | 1 - 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/paddle/fluid/framework/raw_tensor.h b/paddle/fluid/framework/raw_tensor.h index 14e89b712531cd..019d5f68699e62 100644 --- a/paddle/fluid/framework/raw_tensor.h +++ b/paddle/fluid/framework/raw_tensor.h @@ -46,18 +46,6 @@ class RawTensor : public phi::ExtendedTensor, template T* GetMutable() { - if (!data_.empty()) { - data_deleter_(); - } - T* created_data = new T(); - data_ = created_data; - data_deleter_ = [created_data]() { delete created_data; }; - data_type_ = std::type_index(typeid(T)); - return created_data; - } - - template - T* Get() { if (!data_.empty()) { try { return paddle::any_cast(data_); @@ -67,13 +55,34 @@ class RawTensor : public phi::ExtendedTensor, typeid(T).name(), data_type_.name())); } - } else { - PADDLE_THROW( - phi::errors::Unavailable("RawTensor is not initialized, if you want " - "to create data, you can use GetMutable.")); } + T* created_data = new T(); + data_ = created_data; + data_deleter_ = [created_data]() { delete created_data; }; + data_type_ = std::type_index(typeid(T)); + return created_data; } + // template + // T* Get() { + // if (!data_.empty()) { + // try { + // return paddle::any_cast(data_); + // } catch (paddle::bad_any_cast&) { + // PADDLE_THROW(phi::errors::InvalidArgument( + // "Invalid data type error, expected %s, actual %s.", + // typeid(T).name(), + // data_type_.name())); + // } + // } else { + // PADDLE_THROW( + // phi::errors::Unavailable("RawTensor is not initialized, if you want + // " + // "to create data, you can use + // GetMutable.")); + // } + // } + template bool IsType() const { return std::type_index(typeid(T)) == data_type_; diff --git a/paddle/fluid/framework/string_array.h b/paddle/fluid/framework/string_array.h index 4dbbd399782629..93e685285bcef9 100644 --- a/paddle/fluid/framework/string_array.h +++ b/paddle/fluid/framework/string_array.h @@ -131,10 +131,10 @@ class String : public phi::ExtendedTensor, std::string::iterator begin() { return data_.begin(); } - std::string* Get() { return &data_; } - const std::string& Get() const { return data_; } + std::string* GetMutable() { return &data_; } + std::string::const_iterator begin() const { return data_.begin(); } std::string::iterator end() { return data_.end(); } diff --git a/paddle/fluid/framework/variable_test.cc b/paddle/fluid/framework/variable_test.cc index 262181095e603a..5b5bd8e032364b 100644 --- a/paddle/fluid/framework/variable_test.cc +++ b/paddle/fluid/framework/variable_test.cc @@ -22,7 +22,7 @@ namespace framework { TEST(Variable, GetMutable) { std::unique_ptr v(new Variable()); - auto* t = v->GetMutable()->Get(); + auto* t = v->GetMutable()->GetMutable(); *t = "1234"; const auto& tt = v->Get().Get(); diff --git a/paddle/fluid/jit/layer_test.cc b/paddle/fluid/jit/layer_test.cc index 693d9e6898ca00..6ee799fdb120ef 100644 --- a/paddle/fluid/jit/layer_test.cc +++ b/paddle/fluid/jit/layer_test.cc @@ -86,7 +86,7 @@ TEST(CpuLayerTest, Construct) { int ds = layer.Attribute("down_sampling"); EXPECT_EQ(ds, 4); - std::string fstr = *(layer.Attribute("fstr").Get()); + std::string fstr = layer.Attribute("fstr").Get(); EXPECT_STREQ(fstr.c_str(), "save str property"); std::vector ints = layer.Attribute>("ints"); diff --git a/paddle/fluid/operators/save_combine_op.cu b/paddle/fluid/operators/save_combine_op.cu index 6c6a8944a6aa8c..94fb8f396a5fac 100644 --- a/paddle/fluid/operators/save_combine_op.cu +++ b/paddle/fluid/operators/save_combine_op.cu @@ -1,4 +1,4 @@ -// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +// Copyright (c) 2018 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. diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 5f7857ec6e1080..529fd97f330b94 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -944,10 +944,10 @@ All parameter, weight, gradient are variables in Paddle. .def("get_bytes", [](Variable &self) { if (self.IsType()) { - return py::bytes(*(self.GetMutable()->Get())); + return py::bytes(*(self.GetMutable()->GetMutable())); } else { return py::bytes( - *(self.GetMutable()->Get())); + *(self.GetMutable()->GetMutable())); } }) .def("set_string_list", diff --git a/paddle/phi/core/kernel_context.cc b/paddle/phi/core/kernel_context.cc index 8bac896c434418..c902fc824f8d2f 100644 --- a/paddle/phi/core/kernel_context.cc +++ b/paddle/phi/core/kernel_context.cc @@ -13,7 +13,6 @@ // limitations under the License. #include "paddle/phi/core/kernel_context.h" -#include namespace phi { From e552774a8c20f1559451ff4c40f1b05142373676 Mon Sep 17 00:00:00 2001 From: YuanRisheng Date: Thu, 8 Dec 2022 07:37:07 +0000 Subject: [PATCH 11/11] delete String --- paddle/fluid/framework/raw_tensor.h | 20 --------- paddle/fluid/framework/string_array.h | 52 +----------------------- paddle/fluid/framework/variable_test.cc | 4 +- paddle/fluid/jit/layer_test.cc | 2 +- paddle/fluid/operators/save_combine_op.h | 2 +- paddle/fluid/pybind/pybind.cc | 2 +- 6 files changed, 6 insertions(+), 76 deletions(-) diff --git a/paddle/fluid/framework/raw_tensor.h b/paddle/fluid/framework/raw_tensor.h index 019d5f68699e62..dfee5acd14af0c 100644 --- a/paddle/fluid/framework/raw_tensor.h +++ b/paddle/fluid/framework/raw_tensor.h @@ -63,26 +63,6 @@ class RawTensor : public phi::ExtendedTensor, return created_data; } - // template - // T* Get() { - // if (!data_.empty()) { - // try { - // return paddle::any_cast(data_); - // } catch (paddle::bad_any_cast&) { - // PADDLE_THROW(phi::errors::InvalidArgument( - // "Invalid data type error, expected %s, actual %s.", - // typeid(T).name(), - // data_type_.name())); - // } - // } else { - // PADDLE_THROW( - // phi::errors::Unavailable("RawTensor is not initialized, if you want - // " - // "to create data, you can use - // GetMutable.")); - // } - // } - template bool IsType() const { return std::type_index(typeid(T)) == data_type_; diff --git a/paddle/fluid/framework/string_array.h b/paddle/fluid/framework/string_array.h index 93e685285bcef9..4ac8d89981bee4 100644 --- a/paddle/fluid/framework/string_array.h +++ b/paddle/fluid/framework/string_array.h @@ -94,57 +94,7 @@ class Vocab : public phi::ExtendedTensor, std::unordered_map data_; }; -class String : public phi::ExtendedTensor, - public phi::TypeInfoTraits { - public: - String() = default; - - String(String&& other) = default; - - String(const String& other) = default; - - String& operator=(const String& other) = default; - - String& operator=(const std::string& other) { - this->data_ = other; - return *this; - } - - String& operator=(std::string&& other) { - this->data_ = other; - return *this; - } - - String& operator=(String&& other) = default; - - /// \brief Destroy the String and release exclusive resources. - virtual ~String() = default; - - public: - /// \brief Returns the name of the class for type traits. - /// \return The name of the class. - static const char* name() { return "String"; } - - size_t size() const { return data_.size(); } - - void clear() { data_.clear(); } - - std::string::iterator begin() { return data_.begin(); } - - const std::string& Get() const { return data_; } - - std::string* GetMutable() { return &data_; } - - std::string::const_iterator begin() const { return data_.begin(); } - - std::string::iterator end() { return data_.end(); } - - std::string::const_iterator end() const { return data_.end(); } - - private: - std::string data_; -}; - +using String = std::string; using Strings = std::vector; // Convert the std::string type to the std::string type. diff --git a/paddle/fluid/framework/variable_test.cc b/paddle/fluid/framework/variable_test.cc index 5b5bd8e032364b..358ba3436d01ed 100644 --- a/paddle/fluid/framework/variable_test.cc +++ b/paddle/fluid/framework/variable_test.cc @@ -22,10 +22,10 @@ namespace framework { TEST(Variable, GetMutable) { std::unique_ptr v(new Variable()); - auto* t = v->GetMutable()->GetMutable(); + auto* t = v->GetMutable(); *t = "1234"; - const auto& tt = v->Get().Get(); + const auto& tt = v->Get(); EXPECT_EQ("1234", tt); try { diff --git a/paddle/fluid/jit/layer_test.cc b/paddle/fluid/jit/layer_test.cc index 6ee799fdb120ef..4e367d8cc1b510 100644 --- a/paddle/fluid/jit/layer_test.cc +++ b/paddle/fluid/jit/layer_test.cc @@ -86,7 +86,7 @@ TEST(CpuLayerTest, Construct) { int ds = layer.Attribute("down_sampling"); EXPECT_EQ(ds, 4); - std::string fstr = layer.Attribute("fstr").Get(); + std::string fstr = layer.Attribute("fstr"); EXPECT_STREQ(fstr.c_str(), "save str property"); std::vector ints = layer.Attribute>("ints"); diff --git a/paddle/fluid/operators/save_combine_op.h b/paddle/fluid/operators/save_combine_op.h index 77ee7fabf46062..bf5e2a5e4d90f2 100644 --- a/paddle/fluid/operators/save_combine_op.h +++ b/paddle/fluid/operators/save_combine_op.h @@ -178,7 +178,7 @@ class SaveCombineOpKernel : public framework::OpKernel { auto overwrite = ctx.Attr("overwrite"); auto save_as_fp16 = ctx.Attr("save_as_fp16"); auto save_to_memory = ctx.Attr("save_to_memory"); - auto output = ctx.Output("Y"); + auto output = ctx.Output("Y"); auto inp_var_names = ctx.InputNames("X"); auto& inp_vars = ctx.MultiInputVar("X"); diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 529fd97f330b94..75693950b35ef4 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -944,7 +944,7 @@ All parameter, weight, gradient are variables in Paddle. .def("get_bytes", [](Variable &self) { if (self.IsType()) { - return py::bytes(*(self.GetMutable()->GetMutable())); + return py::bytes(*(self.GetMutable())); } else { return py::bytes( *(self.GetMutable()->GetMutable()));