From 01fedf4f7c684f2131f854de83e18d071073d8ff Mon Sep 17 00:00:00 2001 From: Ruibiao Chen Date: Mon, 4 Jul 2022 11:10:13 +0800 Subject: [PATCH] Remove boost::static_visitor (#44024) --- paddle/fluid/framework/dlpack_tensor.cc | 3 ++- paddle/fluid/framework/ir/generate_pass.cc | 4 ++-- paddle/fluid/framework/op_desc.cc | 2 +- paddle/fluid/framework/tensor_util.cc | 8 ++++---- .../fluid/imperative/gradient_accumulator.cc | 3 ++- .../allocation/naive_best_fit_allocator.cc | 8 ++++---- .../fluid/operators/array_to_lod_tensor_op.cc | 2 +- paddle/fluid/operators/controlflow/feed_op.cc | 2 +- .../fluid/operators/controlflow/op_variant.cc | 11 ++++------- .../fluid/operators/lod_tensor_to_array_op.cc | 3 ++- .../fluid/operators/math/matrix_bit_code.cc | 17 ++++++++--------- .../fluid/platform/device/ipu/ipu_compiler.cc | 4 ++-- paddle/fluid/platform/variant.h | 1 - .../pybind/global_value_getter_setter.cc | 2 +- paddle/fluid/pybind/pybind_boost_headers.h | 19 +------------------ paddle/phi/kernels/funcs/math_function.cc | 3 ++- 16 files changed, 37 insertions(+), 55 deletions(-) diff --git a/paddle/fluid/framework/dlpack_tensor.cc b/paddle/fluid/framework/dlpack_tensor.cc index 057a19f3175..b7bca733b8f 100644 --- a/paddle/fluid/framework/dlpack_tensor.cc +++ b/paddle/fluid/framework/dlpack_tensor.cc @@ -69,7 +69,8 @@ static DLDataType GetDLDataTypeFromTypeIndex(proto::VarType::Type type) { #undef REG_DL_DATA_TYPE } -struct DLDeviceVisitor : public boost::static_visitor<::DLDevice> { +struct DLDeviceVisitor + : public std::unary_function { inline ::DLDevice operator()(const platform::CPUPlace &place) const { ::DLDevice device; device.device_type = kDLCPU; diff --git a/paddle/fluid/framework/ir/generate_pass.cc b/paddle/fluid/framework/ir/generate_pass.cc index 160304784a9..455af834278 100644 --- a/paddle/fluid/framework/ir/generate_pass.cc +++ b/paddle/fluid/framework/ir/generate_pass.cc @@ -21,7 +21,7 @@ namespace paddle { namespace framework { namespace ir { -class element_visitor : public boost::static_visitor { +class element_visitor { public: explicit element_visitor(int index) : index_(index) {} @@ -47,7 +47,7 @@ class element_visitor : public boost::static_visitor { int index_; }; -class operation_visitor : public boost::static_visitor { +class operation_visitor { public: explicit operation_visitor(const proto::PassDesc::OperationType& type) : type_(type) {} diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index 169722b971b..c0a9528c281 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -764,7 +764,7 @@ void OpDesc::RenameInput(const std::string &old_name, need_update_ = true; } -struct SetAttrDescVisitor : public boost::static_visitor { +struct SetAttrDescVisitor { explicit SetAttrDescVisitor(proto::OpDesc::Attr *attr) : attr_(attr) {} mutable proto::OpDesc::Attr *attr_; void operator()(int v) const { attr_->set_i(v); } diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index dd80458b624..dbb549efa25 100644 --- a/paddle/fluid/framework/tensor_util.cc +++ b/paddle/fluid/framework/tensor_util.cc @@ -689,7 +689,7 @@ inline void AnyImpl(Predicate predicate, } template -class AnyVisitor : public boost::static_visitor { +class AnyVisitor : public std::unary_function { private: const framework::Tensor& tensor_; Predicate predicate_; @@ -774,7 +774,7 @@ class AnyVisitor : public boost::static_visitor { }; template -class AnyOutVisitor : public boost::static_visitor<> { +class AnyOutVisitor : public std::unary_function { private: const framework::Tensor& tensor_; mutable framework::Tensor* out_; @@ -843,7 +843,7 @@ inline void AllImpl(Predicate predicate, } template -class AllOutVisitor : public boost::static_visitor<> { +class AllOutVisitor : public std::unary_function { private: const framework::Tensor& tensor_; mutable framework::Tensor* out_; @@ -942,7 +942,7 @@ static inline void __global__ BothFalse(const T* cmp, T* out, int element_num) { } #endif -struct BothFalseVisitor : public boost::static_visitor<> { +struct BothFalseVisitor : public std::unary_function { const framework::Tensor& in_; mutable framework::Tensor* out_; BothFalseVisitor(const framework::Tensor& in, framework::Tensor* out) diff --git a/paddle/fluid/imperative/gradient_accumulator.cc b/paddle/fluid/imperative/gradient_accumulator.cc index 4a8fc6a5d54..f6883fe6c6a 100644 --- a/paddle/fluid/imperative/gradient_accumulator.cc +++ b/paddle/fluid/imperative/gradient_accumulator.cc @@ -79,7 +79,8 @@ static void MoveOrCopyVar(framework::Variable* dst, } template -class TensorAddFunctor : public boost::static_visitor<> { +class TensorAddFunctor + : public std::unary_function { public: TensorAddFunctor(int64_t numel, const T* x, T* y) : numel_(numel), x_(x), y_(y) {} diff --git a/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc b/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc index 9d5f048a165..57c5941d522 100644 --- a/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc +++ b/paddle/fluid/memory/allocation/naive_best_fit_allocator.cc @@ -59,7 +59,7 @@ uint64_t Release(const Place &place); template size_t Used(const Place &place); -struct Usage : public boost::static_visitor { +struct Usage { size_t operator()(const platform::CPUPlace &cpu) const; size_t operator()(const platform::CUDAPlace &gpu) const; size_t operator()(const platform::CUDAPinnedPlace &cuda_pinned) const; @@ -894,7 +894,7 @@ size_t Used(const platform::CustomPlace &place) { #endif } -struct AllocVisitor : public boost::static_visitor { +struct AllocVisitor : std::unary_function { inline explicit AllocVisitor(size_t size) : size_(size) {} template @@ -906,7 +906,7 @@ struct AllocVisitor : public boost::static_visitor { size_t size_; }; -struct FreeVisitor : public boost::static_visitor { +struct FreeVisitor : public std::unary_function { inline explicit FreeVisitor(void *ptr, size_t size) : ptr_(ptr), size_(size) {} @@ -920,7 +920,7 @@ struct FreeVisitor : public boost::static_visitor { size_t size_; }; -struct ReleaseVisitor : public boost::static_visitor { +struct ReleaseVisitor : std::unary_function { template inline uint64_t operator()(const Place &place) const { return Release(place); diff --git a/paddle/fluid/operators/array_to_lod_tensor_op.cc b/paddle/fluid/operators/array_to_lod_tensor_op.cc index 5b23ff60475..a2af64e2276 100644 --- a/paddle/fluid/operators/array_to_lod_tensor_op.cc +++ b/paddle/fluid/operators/array_to_lod_tensor_op.cc @@ -43,7 +43,7 @@ struct ArrayToLoDFunctorImpl { void apply(); }; -struct ArrayToLoDFunctor : public boost::static_visitor { +struct ArrayToLoDFunctor : public std::unary_function { std::vector in; mutable framework::Tensor *out; diff --git a/paddle/fluid/operators/controlflow/feed_op.cc b/paddle/fluid/operators/controlflow/feed_op.cc index 00806d18c06..4cef1044965 100644 --- a/paddle/fluid/operators/controlflow/feed_op.cc +++ b/paddle/fluid/operators/controlflow/feed_op.cc @@ -29,7 +29,7 @@ namespace operators { // FeedVariableVisitor is to feed the variable data // according to data type (LoDTensor or Strings). -class FeedVariableVisitor : public boost::static_visitor { +class FeedVariableVisitor { public: explicit FeedVariableVisitor(framework::Variable *out_var, const platform::Place &place) diff --git a/paddle/fluid/operators/controlflow/op_variant.cc b/paddle/fluid/operators/controlflow/op_variant.cc index 60f58955adb..48b7a434106 100644 --- a/paddle/fluid/operators/controlflow/op_variant.cc +++ b/paddle/fluid/operators/controlflow/op_variant.cc @@ -17,24 +17,21 @@ namespace paddle { namespace operators { -struct InputsVisitor - : public boost::static_visitor { +struct InputsVisitor { template const framework::VariableNameMap *operator()(const OpType *op) const { return &(op->Inputs()); } }; -struct OutputsVisitor - : public boost::static_visitor { +struct OutputsVisitor { template const framework::VariableNameMap *operator()(const OpType *op) const { return &(op->Outputs()); } }; -struct AttributeMapVisitor - : public boost::static_visitor { +struct AttributeMapVisitor { const framework::AttributeMap *operator()(const framework::OpDesc *op) const { return &(op->GetAttrMap()); } @@ -45,7 +42,7 @@ struct AttributeMapVisitor } }; -struct RawPointerVisitor : public boost::static_visitor { +struct RawPointerVisitor { template const void *operator()(const OpType *op) const { return op; diff --git a/paddle/fluid/operators/lod_tensor_to_array_op.cc b/paddle/fluid/operators/lod_tensor_to_array_op.cc index 147b23f56ac..d4b36f31e62 100644 --- a/paddle/fluid/operators/lod_tensor_to_array_op.cc +++ b/paddle/fluid/operators/lod_tensor_to_array_op.cc @@ -44,7 +44,8 @@ struct LoDTensorToArrayFunctorImpl { void apply(); }; -struct LoDTensorToArrayFunctor : public boost::static_visitor { +struct LoDTensorToArrayFunctor + : public std::unary_function { std::vector ref_inputs_; mutable std::vector outputs_; const framework::Tensor &input_; diff --git a/paddle/fluid/operators/math/matrix_bit_code.cc b/paddle/fluid/operators/math/matrix_bit_code.cc index 8a6f098baef..0648f2497d9 100644 --- a/paddle/fluid/operators/math/matrix_bit_code.cc +++ b/paddle/fluid/operators/math/matrix_bit_code.cc @@ -19,7 +19,7 @@ namespace operators { namespace math { template -struct MatrixBitCodeFunctorAdd : public boost::static_visitor { +struct MatrixBitCodeFunctorAdd { const framework::Tensor &vec_; framework::Tensor *tmat_; @@ -51,7 +51,7 @@ void MatrixBitCodeFunctor::Add(const framework::Tensor &vec, } template -struct MatrixBitCodeFunctorAddGrad : public boost::static_visitor { +struct MatrixBitCodeFunctorAddGrad { const framework::Tensor &tmat_; framework::Tensor *vec_; MatrixBitCodeFunctorAddGrad(const framework::Tensor &tmat, @@ -83,7 +83,7 @@ void MatrixBitCodeFunctor::AddGrad(const framework::Tensor &tmat, } template -struct MatrixBitCodeFunctorSum : public boost::static_visitor { +struct MatrixBitCodeFunctorSum { const framework::Tensor &tmat_; framework::Tensor *sum_; T scale_sum_; @@ -125,7 +125,7 @@ void MatrixBitCodeFunctor::Sum(const framework::Tensor &tmat, } template -struct MatrixBitCodeFunctorMul : public boost::static_visitor { +struct MatrixBitCodeFunctorMul { framework::Tensor *tmat_; const framework::Tensor &weight_; const framework::Tensor &input_; @@ -174,7 +174,7 @@ class ReservedVector : public std::vector { }; template -struct MatrixBitCodeFunctorMulGradWeight : public boost::static_visitor { +struct MatrixBitCodeFunctorMulGradWeight { const framework::Tensor &tmat_; framework::Tensor *weight_; const framework::Tensor &input_; @@ -224,8 +224,7 @@ void MatrixBitCodeFunctor::MulGradWeight(const framework::Tensor &tmat, } template -struct MatrixBitCodeFunctorMulGradWeightSR - : public boost::static_visitor { +struct MatrixBitCodeFunctorMulGradWeightSR { const framework::Tensor &tmat_; phi::SelectedRows *weight_; const framework::Tensor &input_; @@ -280,7 +279,7 @@ void MatrixBitCodeFunctor::MulGradWeight(const framework::Tensor &tmat, } template -struct MatrixBitCodeFunctorMulGradError : public boost::static_visitor { +struct MatrixBitCodeFunctorMulGradError { const framework::Tensor &tmat_; const framework::Tensor &weight_; framework::Tensor *input_; @@ -324,7 +323,7 @@ void MatrixBitCodeFunctor::MulGradError(const framework::Tensor &tmat, } template -struct MatrixBitCodeFunctorSub : public boost::static_visitor { +struct MatrixBitCodeFunctorSub { framework::Tensor *tmat_; explicit MatrixBitCodeFunctorSub(framework::Tensor *tmat) : tmat_(tmat) {} diff --git a/paddle/fluid/platform/device/ipu/ipu_compiler.cc b/paddle/fluid/platform/device/ipu/ipu_compiler.cc index 39ff4601b67..330ddef577e 100644 --- a/paddle/fluid/platform/device/ipu/ipu_compiler.cc +++ b/paddle/fluid/platform/device/ipu/ipu_compiler.cc @@ -32,7 +32,7 @@ namespace ipu { namespace { -struct CustomOpAttrVisitor : public boost::static_visitor { +struct CustomOpAttrVisitor { CustomOpAttrVisitor(std::map* attr, const std::string& attr_name) : attrs_(attr), attr_name_(attr_name) {} @@ -82,7 +82,7 @@ struct CustomOpAttrVisitor : public boost::static_visitor { } }; -struct ConstantOpAttrVisitor : public boost::static_visitor { +struct ConstantOpAttrVisitor { ConstantOpAttrVisitor(framework::LoDTensor* tensor, VarType::Type dtype) : tensor_(tensor), dtype_(dtype) {} diff --git a/paddle/fluid/platform/variant.h b/paddle/fluid/platform/variant.h index fb4772abd30..9682749898f 100644 --- a/paddle/fluid/platform/variant.h +++ b/paddle/fluid/platform/variant.h @@ -40,7 +40,6 @@ limitations under the License. */ #include #include -#include #include "paddle/utils/any.h" #include "paddle/utils/optional.h" diff --git a/paddle/fluid/pybind/global_value_getter_setter.cc b/paddle/fluid/pybind/global_value_getter_setter.cc index 2871d1de567..b2a52e568ae 100644 --- a/paddle/fluid/pybind/global_value_getter_setter.cc +++ b/paddle/fluid/pybind/global_value_getter_setter.cc @@ -217,7 +217,7 @@ void BindGlobalValueGetterSetter(pybind11::module *module) { GlobalVarGetterSetterRegistry::CreateSetter(&var)); \ } while (0) -struct RegisterGetterSetterVisitor : public boost::static_visitor { +struct RegisterGetterSetterVisitor { RegisterGetterSetterVisitor(const std::string &name, bool is_writable, void *value_ptr) diff --git a/paddle/fluid/pybind/pybind_boost_headers.h b/paddle/fluid/pybind/pybind_boost_headers.h index 623ec84acda..2a25990944d 100644 --- a/paddle/fluid/pybind/pybind_boost_headers.h +++ b/paddle/fluid/pybind/pybind_boost_headers.h @@ -18,14 +18,12 @@ limitations under the License. */ #include #include "glog/logging.h" -#include "paddle/fluid/platform/variant.h" #include "paddle/utils/variant.h" #include "pybind11/numpy.h" #include "pybind11/pybind11.h" #include "pybind11/stl.h" // Cast paddle::variant for PyBind. // Copy from - // https://github.com/pybind/pybind11/issues/576#issuecomment-269563199 namespace pybind11 { namespace detail { @@ -78,10 +76,7 @@ struct paddle_variant_caster> { using Type = V; template - typename std::enable_if< - !std::is_same::value, - bool>::type - try_load(handle src, bool convert) { + bool try_load(handle src, bool convert) { auto caster = make_caster(); if (!load_success_ && caster.load(src, convert)) { load_success_ = true; @@ -112,13 +107,6 @@ struct paddle_variant_caster> { return false; } - template - typename std::enable_if::value, - bool>::type - try_load(handle src, bool convert) { - return false; - } - bool load(handle src, bool convert) { auto unused = {false, try_load(src, convert)...}; (void)(unused); @@ -128,11 +116,6 @@ struct paddle_variant_caster> { static handle cast(Type const& src, return_value_policy policy, handle parent) { - /* - auto paddle_variant_caster_visitor = [&](Type const& src)->handle { - return make_caster::cast(src, policy, parent); - } - */ paddle_variant_caster_visitor visitor(policy, parent); return paddle::visit(visitor, src); } diff --git a/paddle/phi/kernels/funcs/math_function.cc b/paddle/phi/kernels/funcs/math_function.cc index 61cedb57faf..15a708f02f4 100644 --- a/paddle/phi/kernels/funcs/math_function.cc +++ b/paddle/phi/kernels/funcs/math_function.cc @@ -214,7 +214,8 @@ void set_constant_with_place( phi::VisitDataType(tensor->dtype(), TensorSetConstantCPU(tensor, value)); } -struct TensorSetConstantWithPlace : public boost::static_visitor { +struct TensorSetConstantWithPlace + : public std::unary_function { TensorSetConstantWithPlace(const paddle::platform::DeviceContext& context, paddle::framework::Tensor* tensor, float value) -- GitLab