From 5ada98b87f98956602b46e4ea380054bec4ac5c3 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Mon, 7 Aug 2023 16:27:41 +0800 Subject: [PATCH] [clang-tidy] NO.6 enable `modernize-avoid-c-arrays` step: 2 (#55954) --- .clang-tidy | 2 +- paddle/fluid/framework/data_type.cc | 2 + .../details/fused_all_reduce_op_handle.cc | 2 +- paddle/fluid/framework/device_worker.cc | 7 ++- paddle/fluid/framework/hogwild_worker.cc | 11 ++-- paddle/fluid/framework/io/shell.cc | 11 ++-- .../framework/ir/lock_free_optimize_pass.cc | 4 +- .../framework/ir/multi_batch_merge_pass.cc | 2 +- .../multi_devices_graph_pass.cc | 2 +- paddle/fluid/framework/ir/node.cc | 2 +- paddle/fluid/framework/ir/pass.cc | 2 +- .../ir/runtime_context_cache_pass.cc | 2 +- paddle/fluid/framework/operator.cc | 7 ++- paddle/fluid/framework/prune.cc | 10 ++-- paddle/fluid/framework/tensor_util.cc | 8 +-- paddle/fluid/framework/tensor_util_test.cc | 7 ++- .../ir_params_sync_among_devices_pass.cc | 2 +- paddle/fluid/inference/api/api_impl.cc | 2 +- paddle/fluid/inference/io.cc | 4 +- paddle/fluid/ir/dialect/kernel_op.cc | 6 +- .../ir_adaptor/translator/op_translator.cc | 4 +- paddle/fluid/operators/assert_op.cc | 26 ++++---- .../controlflow/conditional_block_op.cc | 12 ++-- .../detection/roi_perspective_transform_op.cc | 47 +++++++++------ paddle/fluid/operators/pad2d_op.cc | 9 +-- paddle/fluid/operators/print_op.cc | 19 +++--- paddle/fluid/operators/py_func_op.cc | 32 +++++----- paddle/fluid/operators/recurrent_op.cc | 34 ++++++----- paddle/fluid/platform/collective_helper.cc | 6 +- .../device/custom/custom_device_test.cc | 5 +- paddle/fluid/platform/device_event_base.cc | 16 ++--- paddle/fluid/platform/enforce_test.cc | 3 +- paddle/fluid/platform/flags.h | 2 +- paddle/fluid/platform/gen_comm_id_helper.cc | 59 +++++++++++-------- paddle/fluid/platform/init.cc | 2 +- paddle/fluid/platform/profiler.cc | 2 +- paddle/fluid/platform/profiler/utils.cc | 9 ++- paddle/fluid/pybind/eager.cc | 14 ++--- paddle/fluid/pybind/eager_method.cc | 3 +- paddle/fluid/pybind/eager_properties.cc | 4 +- paddle/fluid/pybind/eager_py_layer.cc | 4 +- paddle/phi/backends/custom/custom_device.cc | 4 +- paddle/phi/kernels/cpu/box_coder_kernel.cc | 5 +- .../cpu/fill_diagonal_tensor_grad_kernel.cc | 12 +++- .../cpu/fill_diagonal_tensor_kernel.cc | 12 +++- .../phi/kernels/cpu/index_put_grad_kernel.cc | 11 ++-- paddle/phi/kernels/cpu/index_put_kernel.cc | 5 +- .../kernels/cpu/interpolate_grad_kernel.cc | 9 +-- paddle/phi/kernels/cpu/interpolate_kernel.cc | 7 ++- paddle/phi/kernels/cpu/nms_kernel.cc | 7 ++- paddle/phi/kernels/cpu/yolo_box_kernel.cc | 13 ++-- paddle/phi/kernels/funcs/jit/benchmark.cc | 2 +- paddle/phi/kernels/funcs/jit/gen/act.cc | 6 +- paddle/phi/kernels/funcs/jit/test.cc | 9 ++- paddle/phi/kernels/strings/unicode.cc | 4 +- paddle/utils/array_ref_test.cc | 11 ++-- paddle/utils/span_test.cc | 36 +++++------ paddle/utils/string/string_helper_test.cc | 2 +- paddle/utils/string/to_string_test.cc | 2 +- test/cpp/fluid/benchmark/op_tester.cc | 2 +- test/cpp/fluid/detection/mask_util_test.cc | 21 +++++-- test/cpp/fluid/math/im2col_test.cc | 11 ++-- test/cpp/fluid/math/vol2col_test.cc | 10 ++-- .../cpp/inference/analysis/analyzer_tester.cc | 7 ++- test/cpp/ir/core/ir_infershape_test.cc | 7 ++- test/cpp/ir/core/ir_op_test.cc | 14 +++-- .../pattern_rewrite/pattern_rewrite_test.cc | 30 +++++----- test/cpp/phi/api/test_from_blob.cc | 2 +- .../phi/api/test_strings_lower_upper_api.cc | 17 ++++-- test/cpp/phi/core/unroll_array_ops_test.cc | 12 ++-- test/cpp/phi/kernels/strided_memcpy_test.cc | 18 +++--- test/cpp/phi/kernels/test_math_function.cc | 25 ++++---- .../phi/kernels/test_strings_copy_dev_api.cc | 2 +- .../test_strings_lower_upper_dev_api.cc | 6 +- 74 files changed, 429 insertions(+), 319 deletions(-) mode change 100755 => 100644 paddle/fluid/framework/ir/pass.cc diff --git a/.clang-tidy b/.clang-tidy index c9e147ed803..7c1f905f2eb 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -166,7 +166,7 @@ bugprone-unused-raii, -misc-unused-alias-decls, -misc-unused-using-decls, modernize-avoid-bind, --modernize-avoid-c-arrays, +modernize-avoid-c-arrays, -modernize-deprecated-headers, -modernize-deprecated-ios-base-aliases, modernize-loop-convert, diff --git a/paddle/fluid/framework/data_type.cc b/paddle/fluid/framework/data_type.cc index fda588db4d8..7dbd1c84849 100644 --- a/paddle/fluid/framework/data_type.cc +++ b/paddle/fluid/framework/data_type.cc @@ -150,6 +150,7 @@ proto::VarType::Type PromoteTypesIfComplexExists( // Here is a complete rules table, but some rules are not used. // It is still written this way because array accessing is still // more efficient than if-else + // NOLINTBEGIN(*-avoid-c-arrays) static constexpr proto::VarType::Type promote_types_table[4][4] = { /* f4 f8 c4 c8*/ /* f4 */ {f4, f8, c4, c8}, @@ -157,6 +158,7 @@ proto::VarType::Type PromoteTypesIfComplexExists( /* c4 */ {c4, c8, c4, c8}, /* c8 */ {c8, c8, c8, c8}, }; + // NOLINTEND(*-avoid-c-arrays) return promote_types_table[type_an][type_bn]; } diff --git a/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc b/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc index 29d5697b23f..66428661503 100644 --- a/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc +++ b/paddle/fluid/framework/details/fused_all_reduce_op_handle.cc @@ -21,7 +21,7 @@ #include "paddle/phi/backends/device_memory_aligment.h" #include "paddle/phi/core/flags.h" -DEFINE_bool(skip_fused_all_reduce_check, false, ""); +DEFINE_bool(skip_fused_all_reduce_check, false, ""); // NOLINT PHI_DECLARE_bool(allreduce_record_one_event); namespace paddle { diff --git a/paddle/fluid/framework/device_worker.cc b/paddle/fluid/framework/device_worker.cc index f69bcd46898..d2bc5b2b21f 100644 --- a/paddle/fluid/framework/device_worker.cc +++ b/paddle/fluid/framework/device_worker.cc @@ -14,6 +14,7 @@ limitations under the License. */ #include "paddle/fluid/framework/device_worker.h" +#include #include #include "paddle/fluid/framework/convert_utils.h" namespace phi { @@ -90,7 +91,7 @@ void PrintLodTensorType(phi::DenseTensor* tensor, std::string& out_val, // NOLINT char separator, bool need_leading_separator) { - char buf[MAX_FLOAT_BUFF_SIZE]; + std::array buf; auto count = tensor->numel(); if (start < 0 || end > count) { VLOG(3) << "access violation"; @@ -104,8 +105,8 @@ void PrintLodTensorType(phi::DenseTensor* tensor, tensor->data()[i] < FLOAT_EPS) { out_val += "0"; } else { - sprintf(buf, "%.9f", tensor->data()[i]); // NOLINT - out_val += buf; + sprintf(buf.data(), "%.9f", tensor->data()[i]); // NOLINT + out_val += buf.data(); } } } diff --git a/paddle/fluid/framework/hogwild_worker.cc b/paddle/fluid/framework/hogwild_worker.cc index 3b162e0a451..4b2d089d628 100644 --- a/paddle/fluid/framework/hogwild_worker.cc +++ b/paddle/fluid/framework/hogwild_worker.cc @@ -12,6 +12,7 @@ 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 #include #include "paddle/fluid/framework/barrier.h" @@ -425,12 +426,14 @@ void HogwildWorker::PrintFetchVars() { if (thread_id_ == 0 && batch_num_ % batch_per_print == 0) { time_t curtime; time(&curtime); - char mbstr[80]; - std::strftime( - mbstr, sizeof(mbstr), "%Y-%m-%d %H:%M:%S", std::localtime(&curtime)); + std::array mbstr; + std::strftime(mbstr.data(), + sizeof(mbstr), + "%Y-%m-%d %H:%M:%S", + std::localtime(&curtime)); std::stringstream ss; - ss << "time: [" << mbstr << "], "; + ss << "time: [" << mbstr.data() << "], "; ss << "batch: [" << batch_num_ << "], "; for (int i = 0; i < fetch_var_num; ++i) { diff --git a/paddle/fluid/framework/io/shell.cc b/paddle/fluid/framework/io/shell.cc index 62855afcf04..312122b45f3 100644 --- a/paddle/fluid/framework/io/shell.cc +++ b/paddle/fluid/framework/io/shell.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h #include "paddle/fluid/framework/io/shell.h" @@ -150,14 +151,14 @@ static int shell_popen_fork_internal(const char* real_cmd, } static int read_from_pipe(FILE* fp, std::string* output) { - char buf[4096]; + std::array buf; while (1) { - int n = fread(buf, 1, 4096, fp); + int n = fread(buf.data(), 1, 4096, fp); if (n <= 0) { break; } - output->append(buf, n); + output->append(buf.data(), n); } if (!feof(fp)) { @@ -249,8 +250,8 @@ std::shared_ptr shell_popen(const std::string& cmd, } static int shell_p2open_fork_internal(const char* real_cmd, - int pipein_fds[2], - int pipeout_fds[2]) { + int pipein_fds[2], // NOLINT + int pipeout_fds[2]) { // NOLINT #if defined(_WIN32) || defined(__APPLE__) || defined(PADDLE_ARM) return 0; #else diff --git a/paddle/fluid/framework/ir/lock_free_optimize_pass.cc b/paddle/fluid/framework/ir/lock_free_optimize_pass.cc index 794fa151fc7..901777f2c8b 100644 --- a/paddle/fluid/framework/ir/lock_free_optimize_pass.cc +++ b/paddle/fluid/framework/ir/lock_free_optimize_pass.cc @@ -25,10 +25,10 @@ namespace paddle { namespace framework { namespace ir { -const char kSumGradOpName[] = "sum"; +const char kSumGradOpName[] = "sum"; // NOLINT // TODO(minqiyang): only support sgd at current time, please add // other optimizers later. -const char kOptimizerType[] = "sgd"; +const char kOptimizerType[] = "sgd"; // NOLINT void LockFreeOptimizePass::ApplyImpl(ir::Graph* graph) const { PADDLE_ENFORCE_NOT_NULL( diff --git a/paddle/fluid/framework/ir/multi_batch_merge_pass.cc b/paddle/fluid/framework/ir/multi_batch_merge_pass.cc index 13465610e47..3332d5128f8 100644 --- a/paddle/fluid/framework/ir/multi_batch_merge_pass.cc +++ b/paddle/fluid/framework/ir/multi_batch_merge_pass.cc @@ -23,7 +23,7 @@ namespace paddle { namespace framework { namespace ir { -static const char kNumRepeats[] = "num_repeats"; +static const char kNumRepeats[] = "num_repeats"; // NOLINT typedef std::unordered_map> SSAVarList; ir::Node* SameNameVar(std::unordered_set all, ir::Node* target) { diff --git a/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc b/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc index 07b4062def7..cadf613f798 100644 --- a/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc +++ b/paddle/fluid/framework/ir/multi_devices_graph_pass/multi_devices_graph_pass.cc @@ -50,7 +50,7 @@ namespace { // all operators. NOTE that even we use a vector here, the operators is // unordered. typedef std::vector GraphOps; -const char kGraphOps[] = "ops"; +const char kGraphOps[] = "ops"; // NOLINT bool OpHaveRole(const ir::Node &node, const framework::OpRole &role) { return PADDLE_GET_CONST( diff --git a/paddle/fluid/framework/ir/node.cc b/paddle/fluid/framework/ir/node.cc index fd33e0e53a1..5516a2d799a 100644 --- a/paddle/fluid/framework/ir/node.cc +++ b/paddle/fluid/framework/ir/node.cc @@ -23,7 +23,7 @@ namespace ir { #if !defined(_WIN32) && (__cplusplus < 201703L) constexpr char Node::kControlDepVarName[]; #else -const char Node::kControlDepVarName[] = "__control_var"; +const char Node::kControlDepVarName[] = "__control_var"; // NOLINT #endif std::unique_ptr CreateNodeForTest(const std::string &name, diff --git a/paddle/fluid/framework/ir/pass.cc b/paddle/fluid/framework/ir/pass.cc old mode 100755 new mode 100644 index f1cd1face34..43b6439200f --- a/paddle/fluid/framework/ir/pass.cc +++ b/paddle/fluid/framework/ir/pass.cc @@ -36,7 +36,7 @@ namespace paddle { namespace framework { namespace ir { -static const char kParamScopeAttr[] = "__param_scope__"; +static const char kParamScopeAttr[] = "__param_scope__"; // NOLINT static const std::vector support_subgraph_passes = { "simplify_with_basic_ops_pass", diff --git a/paddle/fluid/framework/ir/runtime_context_cache_pass.cc b/paddle/fluid/framework/ir/runtime_context_cache_pass.cc index 4f5e5edb893..e4d73975230 100644 --- a/paddle/fluid/framework/ir/runtime_context_cache_pass.cc +++ b/paddle/fluid/framework/ir/runtime_context_cache_pass.cc @@ -22,7 +22,7 @@ namespace framework { namespace ir { void RuntimeContextCachePass::ApplyImpl(ir::Graph* graph) const { - static constexpr char kNotAllowInferShapeCahce[] = + static constexpr char kNotAllowInferShapeCahce[] = // NOLINT "@NOT_ALLOW_INFERSHAPE_CACHE@"; VLOG(3) << "Applies Runtime Context Cache strategy."; for (const Node* n : graph->Nodes()) { diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 9f89cf32b06..ec0469e7825 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -1227,7 +1227,7 @@ bool OpSupportGPU(const std::string& op_type) { } struct OperatorWithKernel::CacheImpl { - static const char kNotAllowInferShapeCahce[]; + static const char kNotAllowInferShapeCahce[]; // NOLINT explicit CacheImpl(phi::KernelContext* kernel_ctx, RuntimeInferShapeContext* infer_shape_ctx, const std::vector& tensors, @@ -1273,8 +1273,9 @@ struct OperatorWithKernel::CacheImpl { bool not_allow_infer_shape_cache_; std::vector last_ddims_; }; -const char OperatorWithKernel::CacheImpl::kNotAllowInferShapeCahce[] = - "@NOT_ALLOW_INFERSHAPE_CACHE@"; +const char // NOLINT + OperatorWithKernel::CacheImpl::kNotAllowInferShapeCahce[] = + "@NOT_ALLOW_INFERSHAPE_CACHE@"; static void CheckTensorNANOrInf(const std::string& op_type, const std::string& name, diff --git a/paddle/fluid/framework/prune.cc b/paddle/fluid/framework/prune.cc index b66c503726a..bf55d0c312e 100644 --- a/paddle/fluid/framework/prune.cc +++ b/paddle/fluid/framework/prune.cc @@ -23,12 +23,12 @@ limitations under the License. */ namespace paddle { namespace framework { -const char kFeedOpType[] = "feed"; -const char kFetchOpType[] = "fetch"; +const char kFeedOpType[] = "feed"; // NOLINT +const char kFetchOpType[] = "fetch"; // NOLINT -const char kRecurrent[] = "recurrent"; -const char kStates[] = "states"; -const char kExStates[] = "ex_states"; +const char kRecurrent[] = "recurrent"; // NOLINT +const char kStates[] = "states"; // NOLINT +const char kExStates[] = "ex_states"; // NOLINT bool HasDependentInputVar( const proto::OpDesc& op_desc, diff --git a/paddle/fluid/framework/tensor_util.cc b/paddle/fluid/framework/tensor_util.cc index 9d7afc469bf..e654fdb3cfa 100644 --- a/paddle/fluid/framework/tensor_util.cc +++ b/paddle/fluid/framework/tensor_util.cc @@ -531,8 +531,8 @@ void TensorToStream(std::ostream& os, #endif } else if (platform::is_custom_place(tensor.place())) { #ifdef PADDLE_WITH_CUSTOM_DEVICE - constexpr size_t kBufSize = 1024 * 1024 * 64; // 64MB - std::unique_ptr buf(new char[kBufSize]); + constexpr size_t kBufSize = 1024 * 1024 * 64; // 64MB + std::unique_ptr buf(new char[kBufSize]); // NOLINT auto& custom_device_context = static_cast(dev_ctx); platform::CPUPlace cpu; @@ -598,7 +598,7 @@ void TensorFromStream(std::istream& is, // proto buffer int32_t size; is.read(reinterpret_cast(&size), sizeof(size)); - std::unique_ptr buf(new char[size]); + std::unique_ptr buf(new char[size]); // NOLINT is.read(reinterpret_cast(buf.get()), size); PADDLE_ENFORCE_EQ( desc.ParseFromArray(buf.get(), size), @@ -671,7 +671,7 @@ void TensorFromStream(std::istream& is, 0, platform::errors::InvalidArgument( "phi::DenseTensor desc size should >= 0")); - std::unique_ptr buf(new char[size]); + std::unique_ptr buf(new char[size]); // NOLINT is.read(reinterpret_cast(buf.get()), size); PADDLE_ENFORCE_EQ( desc.ParseFromArray(buf.get(), size), diff --git a/paddle/fluid/framework/tensor_util_test.cc b/paddle/fluid/framework/tensor_util_test.cc index bda2681f57f..65c2bf3b64b 100644 --- a/paddle/fluid/framework/tensor_util_test.cc +++ b/paddle/fluid/framework/tensor_util_test.cc @@ -13,6 +13,7 @@ // limitations under the License. #include +#include #include #include "paddle/fluid/framework/tensor_util.h" @@ -28,8 +29,8 @@ TEST(TensorCopy, Tensor) { int* src_ptr = src_tensor.mutable_data(phi::make_ddim({3, 3}), platform::CPUPlace()); - int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - memcpy(src_ptr, arr, 9 * sizeof(int)); + std::array arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + memcpy(src_ptr, arr.data(), 9 * sizeof(int)); src_tensor.set_layout(DataLayout::kAnyLayout); auto cpu_place = new platform::CPUPlace(); @@ -467,7 +468,7 @@ TEST(TensorIsfinite, CPU) { TEST(Tensor, FromAndToStream) { phi::DenseTensor src_tensor; - int array[6] = {1, 2, 3, 4, 5, 6}; + std::array array = {1, 2, 3, 4, 5, 6}; src_tensor.Resize({2, 3}); int* src_ptr = src_tensor.mutable_data(platform::CPUPlace()); for (int i = 0; i < 6; ++i) { diff --git a/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc b/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc index 36398739483..fa35ffc45c2 100644 --- a/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc +++ b/paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc @@ -27,7 +27,7 @@ #include "paddle/fluid/platform/place.h" #include "paddle/phi/core/dense_tensor.h" -DEFINE_bool( +DEFINE_bool( // NOLINT custom_model_save_cpu, false, "Keep old mode for developers, the model is saved on cpu not device."); diff --git a/paddle/fluid/inference/api/api_impl.cc b/paddle/fluid/inference/api/api_impl.cc index 05ce09cca1a..1f389af7b17 100644 --- a/paddle/fluid/inference/api/api_impl.cc +++ b/paddle/fluid/inference/api/api_impl.cc @@ -26,7 +26,7 @@ limitations under the License. */ #include "paddle/fluid/platform/place.h" #include "paddle/fluid/platform/profiler.h" -DEFINE_bool(profile, false, "Turn on profiler for fluid"); +DEFINE_bool(profile, false, "Turn on profiler for fluid"); // NOLINT namespace paddle { namespace { diff --git a/paddle/fluid/inference/io.cc b/paddle/fluid/inference/io.cc index 253df637633..fe502a15798 100644 --- a/paddle/fluid/inference/io.cc +++ b/paddle/fluid/inference/io.cc @@ -29,7 +29,9 @@ limitations under the License. */ // phi #include "paddle/phi/kernels/declarations.h" -DEFINE_string(devices, "", "The devices to be used which is joined by comma."); +DEFINE_string(devices, // NOLINT + "", + "The devices to be used which is joined by comma."); DEFINE_int32(math_num_threads, 1, "Number of threads used to run math functions."); diff --git a/paddle/fluid/ir/dialect/kernel_op.cc b/paddle/fluid/ir/dialect/kernel_op.cc index 45dce38f11b..57127c0a57f 100644 --- a/paddle/fluid/ir/dialect/kernel_op.cc +++ b/paddle/fluid/ir/dialect/kernel_op.cc @@ -20,8 +20,10 @@ namespace paddle { namespace dialect { -const char* PhiKernelOp::attributes_name[attributes_num] = { - "op_name", "kernel_name", "kernel_key"}; +const char* PhiKernelOp::attributes_name[attributes_num] = { // NOLINT + "op_name", + "kernel_name", + "kernel_key"}; void PhiKernelOp::Verify() { VLOG(4) << "Verifying inputs, outputs and attributes for: PhiKernelOp."; diff --git a/paddle/fluid/ir_adaptor/translator/op_translator.cc b/paddle/fluid/ir_adaptor/translator/op_translator.cc index 1168a6af4d8..941ef7ba47b 100644 --- a/paddle/fluid/ir_adaptor/translator/op_translator.cc +++ b/paddle/fluid/ir_adaptor/translator/op_translator.cc @@ -69,8 +69,8 @@ using InputHandlerFn = std::function; using AttributeHandlerFn = std::function; -constexpr char kTargetDialectPrefix[] = "pd."; -constexpr char kEmptyVarName[] = "@EMPTY@"; +constexpr char kTargetDialectPrefix[] = "pd."; // NOLINT +constexpr char kEmptyVarName[] = "@EMPTY@"; // NOLINT static const std::unordered_set special_non_inplace_ops = {}; diff --git a/paddle/fluid/operators/assert_op.cc b/paddle/fluid/operators/assert_op.cc index 2795eb15b55..4ab60914908 100644 --- a/paddle/fluid/operators/assert_op.cc +++ b/paddle/fluid/operators/assert_op.cc @@ -12,6 +12,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/controlflow/while_op_helper.h" #include "paddle/phi/kernels/funcs/tensor_formatter.h" @@ -34,9 +35,10 @@ class OpBase; } // namespace imperative } // namespace paddle -const char kCond[] = "Cond"; -const char kData[] = "Data"; -const char kSummarize[] = "summarize"; +// const char kCond[] = "Cond"; +std::array kCond = {"Cond"}; +std::array kData = {"Data"}; +std::array kSummarize = {"summarize"}; namespace paddle { namespace operators { @@ -52,7 +54,8 @@ class AssertOp : public framework::OperatorBase { private: void RunImpl(const framework::Scope &scope, const platform::Place &dev_place) const override { - const framework::Variable *cond_var_ptr = scope.FindVar(Input(kCond)); + const framework::Variable *cond_var_ptr = + scope.FindVar(Input(kCond.data())); PADDLE_ENFORCE_NOT_NULL(cond_var_ptr, platform::errors::NotFound( "Input(Condition) of AssertOp is not found.")); @@ -71,9 +74,9 @@ class AssertOp : public framework::OperatorBase { } funcs::TensorFormatter formatter; - formatter.SetSummarize(Attr(kSummarize)); + formatter.SetSummarize(Attr(kSummarize.data())); - const std::vector &x_names = Inputs(kData); + const std::vector &x_names = Inputs(kData.data()); for (const std::string &name : x_names) { const framework::Variable *x_var_ptr = scope.FindVar(name); const phi::DenseTensor &x_tensor = x_var_ptr->Get(); @@ -83,7 +86,7 @@ class AssertOp : public framework::OperatorBase { PADDLE_THROW(platform::errors::InvalidArgument( "The condition variable '%s' of AssertOp must be " "true, but received false", - Input(kCond))); + Input(kCond.data()))); } }; @@ -91,13 +94,13 @@ class AssertOpProtoMaker : public framework::OpProtoAndCheckerMaker { public: void Make() override { AddInput( - kCond, + kCond.data(), "The boolean scalar condition tensor which is asserted to be true."); - AddInput(kData, + AddInput(kData.data(), "The tensors to print when the assert condition is not true.") .AsDuplicable(); AddAttr( - kSummarize, + kSummarize.data(), "The number of entries of each tensor to print when the " "assert condition is not true. -1 means print all entries. If " "the number of entries of a tensor is less then " @@ -111,7 +114,8 @@ class AssertOpProtoMaker : public framework::OpProtoAndCheckerMaker { class AssertOpInferShape : public framework::InferShapeBase { public: void operator()(framework::InferShapeContext *context) const override { - OP_INOUT_CHECK(context->HasInputs(kCond), "Input", "Condition", "AssertOp"); + OP_INOUT_CHECK( + context->HasInputs(kCond.data()), "Input", "Condition", "AssertOp"); } }; diff --git a/paddle/fluid/operators/controlflow/conditional_block_op.cc b/paddle/fluid/operators/controlflow/conditional_block_op.cc index f3e847aa15f..d4b0d28bad2 100644 --- a/paddle/fluid/operators/controlflow/conditional_block_op.cc +++ b/paddle/fluid/operators/controlflow/conditional_block_op.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/fluid/operators/controlflow/conditional_block_op.h" +#include #include "paddle/fluid/framework/new_executor/standalone_executor.h" #include "paddle/fluid/operators/assign_op.h" @@ -29,11 +30,12 @@ PHI_DECLARE_bool(use_mkldnn); namespace paddle { namespace operators { -const char ConditionalOp::kInputs[] = "Input"; -const char ConditionalOp::kOutputs[] = "Out"; -const char ConditionalOp::kCondition[] = "Cond"; -const char ConditionalOp::kScope[] = "Scope"; -const char ConditionalOp::kSkipEagerDeletionVars[] = "skip_eager_deletion_vars"; +const char ConditionalOp::kInputs[] = "Input"; // NOLINT +const char ConditionalOp::kOutputs[] = "Out"; // NOLINT +const char ConditionalOp::kCondition[] = "Cond"; // NOLINT +const char ConditionalOp::kScope[] = "Scope"; // NOLINT +const char ConditionalOp::kSkipEagerDeletionVars[] = + "skip_eager_deletion_vars"; // NOLINT using Executor = framework::Executor; using ExecutorPrepareContext = framework::ExecutorPrepareContext; diff --git a/paddle/fluid/operators/detection/roi_perspective_transform_op.cc b/paddle/fluid/operators/detection/roi_perspective_transform_op.cc index 5b506ee41ee..5f732e423b2 100644 --- a/paddle/fluid/operators/detection/roi_perspective_transform_op.cc +++ b/paddle/fluid/operators/detection/roi_perspective_transform_op.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include +#include #include #include @@ -41,7 +42,7 @@ bool GT(T a, T b) { *check if (x, y) is in the boundary of roi */ template -bool in_quad(T x, T y, T roi_x[], T roi_y[]) { +bool in_quad(T x, T y, T roi_x[], T roi_y[]) { // NOLINT for (int i = 0; i < 4; i++) { T xs = roi_x[i]; T ys = roi_y[i]; @@ -107,9 +108,9 @@ bool in_quad(T x, T y, T roi_x[], T roi_y[]) { template void get_transform_matrix(const int transformed_width, const int transformed_height, - T roi_x[], - T roi_y[], - T matrix[]) { + T roi_x[], // NOLINT + T roi_y[], // NOLINT + T matrix[]) { // NOLINT T x0 = roi_x[0]; T x1 = roi_x[1]; T x2 = roi_x[2]; @@ -170,7 +171,8 @@ void get_transform_matrix(const int transformed_width, * */ template -void get_source_coords(T matrix[], int out_w, int out_h, T* in_w, T* in_h) { +void get_source_coords( + T matrix[], int out_w, int out_h, T* in_w, T* in_h) { // NOLINT T u = matrix[0] * out_w + matrix[1] * out_h + matrix[2]; T v = matrix[3] * out_w + matrix[4] * out_h + matrix[5]; T w = matrix[6] * out_w + matrix[7] * out_h + matrix[8]; @@ -283,17 +285,20 @@ class CPUROIPerspectiveTransformOpKernel : public framework::OpKernel { for (int n = 0; n < rois_num; ++n) { const T* n_rois = rois_data + n * 8; - T roi_x[4]; - T roi_y[4]; + std::array roi_x; + std::array roi_y; for (int k = 0; k < 4; ++k) { roi_x[k] = n_rois[2 * k] * spatial_scale; roi_y[k] = n_rois[2 * k + 1] * spatial_scale; } int image_id = roi2image_data[n]; // Get transform matrix - T matrix[9]; - get_transform_matrix( - transformed_width, transformed_height, roi_x, roi_y, matrix); + std::array matrix; + get_transform_matrix(transformed_width, + transformed_height, + roi_x.data(), + roi_y.data(), + matrix.data()); for (int i = 0; i < 9; i++) { transform_matrix[n * 9 + i] = matrix[i]; } @@ -305,8 +310,8 @@ class CPUROIPerspectiveTransformOpKernel : public framework::OpKernel { c * transformed_height * transformed_width + out_h * transformed_width + out_w; T in_w, in_h; - get_source_coords(matrix, out_w, out_h, &in_w, &in_h); - if (in_quad(in_w, in_h, roi_x, roi_y)) { + get_source_coords(matrix.data(), out_w, out_h, &in_w, &in_h); + if (in_quad(in_w, in_h, roi_x.data(), roi_y.data())) { if (GT_E(-0.5, in_w) || GT_E(in_w, static_cast(in_width - 0.5)) || GT_E(-0.5, in_h) || @@ -431,17 +436,20 @@ class CPUROIPerspectiveTransformGradOpKernel : public framework::OpKernel { T gradient = 0.0; for (size_t roi_idx = lod[n]; roi_idx < lod[n + 1]; ++roi_idx) { const T* rois = rois_data + roi_idx * 8; - T roi_x[4]; - T roi_y[4]; + std::array roi_x; + std::array roi_y; for (int k = 0; k < 4; ++k) { roi_x[k] = rois[2 * k] * spatial_scale; roi_y[k] = rois[2 * k + 1] * spatial_scale; } // Get transform matrix - T matrix[9]; - get_transform_matrix( - transformed_width, transformed_height, roi_x, roi_y, matrix); + std::array matrix; + get_transform_matrix(transformed_width, + transformed_height, + roi_x.data(), + roi_y.data(), + matrix.data()); const T* out_grad_ptr = out_grad_data + (roi_idx * channels + c) * transformed_height * transformed_width; @@ -449,8 +457,9 @@ class CPUROIPerspectiveTransformGradOpKernel : public framework::OpKernel { for (int out_w = 0; out_w < transformed_width; ++out_w) { T src_w; T src_h; - get_source_coords(matrix, out_w, out_h, &src_w, &src_h); - if (in_quad(src_w, src_h, roi_x, roi_y)) { + get_source_coords( + matrix.data(), out_w, out_h, &src_w, &src_h); + if (in_quad(src_w, src_h, roi_x.data(), roi_y.data())) { if (GT_E(-0.5, src_w) || GT_E(src_w, static_cast(in_width - 0.5)) || GT_E(-0.5, src_h) || diff --git a/paddle/fluid/operators/pad2d_op.cc b/paddle/fluid/operators/pad2d_op.cc index 68d31499946..36ef4526542 100644 --- a/paddle/fluid/operators/pad2d_op.cc +++ b/paddle/fluid/operators/pad2d_op.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include +#include #include #include #include @@ -406,8 +407,8 @@ template class Pad2dCPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { - int pads[4]; - GetPaddings(pads, context); + std::array pads; + GetPaddings(pads.data(), context); auto mode = context.Attr("mode"); auto data_format = context.Attr("data_format"); T value = static_cast(context.Attr("pad_value")); @@ -524,8 +525,8 @@ template class Pad2dGradCPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& context) const override { - int pads[4]; - GetPaddings(pads, context); + std::array pads; + GetPaddings(pads.data(), context); auto mode = context.Attr("mode"); auto data_format = context.Attr("data_format"); auto* d_out = diff --git a/paddle/fluid/operators/print_op.cc b/paddle/fluid/operators/print_op.cc index 3d014d417bc..a2e1866300d 100644 --- a/paddle/fluid/operators/print_op.cc +++ b/paddle/fluid/operators/print_op.cc @@ -12,6 +12,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" #include "paddle/phi/kernels/funcs/tensor_formatter.h" @@ -37,9 +38,9 @@ using framework::GradVarName; #define CLOG std::cout -const char kForward[] = "FORWARD"; -const char kBackward[] = "BACKWARD"; -const char kBoth[] = "BOTH"; +std::array kForward = {"FORWARD"}; +std::array kBackward = {"BACKWARD"}; +std::array kBoth = {"BOTH"}; // TODO(ChunweiYan) there should be some other printers for TensorArray class PrintOp : public framework::OperatorBase { @@ -79,8 +80,8 @@ class PrintOp : public framework::OperatorBase { std::string print_phase = Attr("print_phase"); bool is_forward = Attr("is_forward"); - if ((is_forward && print_phase == kBackward) || - (!is_forward && print_phase == kForward)) { + if ((is_forward && print_phase == kBackward.data()) || + (!is_forward && print_phase == kForward.data())) { return; } @@ -125,10 +126,10 @@ class PrintOpProtoAndCheckMaker : public framework::OpProtoAndCheckerMaker { "(string, default 'FORWARD') Which phase to display " "including 'FORWARD' " "'BACKWARD' and 'BOTH'.") - .SetDefault(std::string(kBoth)) - .InEnum({std::string(kForward), - std::string(kBackward), - std::string(kBoth)}); + .SetDefault(std::string(kBoth.data())) + .InEnum({std::string(kForward.data()), + std::string(kBackward.data()), + std::string(kBoth.data())}); AddAttr("is_forward", "Whether is forward or not").SetDefault(true); AddComment(R"DOC( Creates a print op that will print when a tensor is accessed. diff --git a/paddle/fluid/operators/py_func_op.cc b/paddle/fluid/operators/py_func_op.cc index 2c5736d3668..ecdded21bb3 100644 --- a/paddle/fluid/operators/py_func_op.cc +++ b/paddle/fluid/operators/py_func_op.cc @@ -14,6 +14,7 @@ #include "paddle/fluid/operators/py_func_op.h" +#include #include #include #include @@ -30,9 +31,9 @@ namespace py = ::pybind11; static std::vector g_py_callables; -const char kForwardPythonCallableId[] = "forward_callable_id"; -const char kBackwardPythonCallableId[] = "backward_callable_id"; -const char kPyFuncBackwardSkipVars[] = "backward_skip_vars"; +std::array kForwardPythonCallableId = {"forward_callable_id"}; +std::array kBackwardPythonCallableId = {"backward_callable_id"}; +std::array kPyFuncBackwardSkipVars = {"backward_skip_vars"}; size_t AppendPythonCallableObjectAndReturnId(const py::object &py_obj) { g_py_callables.emplace_back(py_obj); @@ -144,11 +145,12 @@ class PyFuncOpVarTypeInference : public framework::StaticGraphVarTypeInference { has_out)); PADDLE_ENFORCE_GE( - PADDLE_GET_CONST(int, ctx->GetAttr(kForwardPythonCallableId)), + PADDLE_GET_CONST(int, ctx->GetAttr(kForwardPythonCallableId.data())), 0, platform::errors::InvalidArgument( "Function id cannot be less than 0, but received value is %d.", - PADDLE_GET_CONST(int, ctx->GetAttr(kForwardPythonCallableId)))); + PADDLE_GET_CONST(int, + ctx->GetAttr(kForwardPythonCallableId.data())))); if (!has_out) return; @@ -200,13 +202,13 @@ class PyFuncOpMaker : public framework::OpProtoAndCheckerMaker { void Make() override { AddInput("X", "Inputs of py_func op.").AsDuplicable(); AddOutput("Out", "Outputs of py_func op").AsDuplicable(); - AddAttr(kForwardPythonCallableId, + AddAttr(kForwardPythonCallableId.data(), "Index of registered forward Python function.") .SetDefault(0); - AddAttr(kBackwardPythonCallableId, + AddAttr(kBackwardPythonCallableId.data(), "Index of registered backward Python function.") .SetDefault(-1); - AddAttr>(kPyFuncBackwardSkipVars, + AddAttr>(kPyFuncBackwardSkipVars.data(), "Unused forward in/out in backward op") .SetDefault(std::vector()); AddComment(R"DOC("PyFunc Op")DOC"); @@ -241,7 +243,8 @@ class PyFuncOpGradDescMaker : public framework::GradOpDescMakerBase { std::vector> operator()() const override { auto &fwd_attrs = Attrs(); // no backward op when backward_id is less than 0 - if (PADDLE_GET_CONST(int, fwd_attrs.at(kBackwardPythonCallableId)) < 0) { + if (PADDLE_GET_CONST(int, fwd_attrs.at(kBackwardPythonCallableId.data())) < + 0) { return {}; } @@ -249,9 +252,9 @@ class PyFuncOpGradDescMaker : public framework::GradOpDescMakerBase { grad_op->SetType("py_func"); framework::AttributeMap bwd_attrs; - bwd_attrs[kForwardPythonCallableId] = - fwd_attrs.at(kBackwardPythonCallableId); - bwd_attrs[kBackwardPythonCallableId] = -1; + bwd_attrs[kForwardPythonCallableId.data()] = + fwd_attrs.at(kBackwardPythonCallableId.data()); + bwd_attrs[kBackwardPythonCallableId.data()] = -1; grad_op->SetAttrMap(bwd_attrs); // All forward inputs @@ -262,7 +265,7 @@ class PyFuncOpGradDescMaker : public framework::GradOpDescMakerBase { // For memory reused, some inputs/output in forward part may be not needed // in backward part. Skipping these vars helps to save memory auto &backward_skip_var_list = PADDLE_GET_CONST( - std::vector, fwd_attrs.at(kPyFuncBackwardSkipVars)); + std::vector, fwd_attrs.at(kPyFuncBackwardSkipVars.data())); std::unordered_set backward_skip_var_set( backward_skip_var_list.begin(), backward_skip_var_list.end()); std::vector bwd_ins; @@ -336,7 +339,8 @@ class PyFuncOp : public framework::OperatorBase { outputs[i] = out_var ? out_var->GetMutable() : nullptr; } - auto callable_id = static_cast(Attr(kForwardPythonCallableId)); + auto callable_id = + static_cast(Attr(kForwardPythonCallableId.data())); auto *py_callable = GetPythonCallableObject(callable_id); VLOG(10) << "Call Python function with id " << callable_id << ": " << PythonFuncDebugString(*py_callable); diff --git a/paddle/fluid/operators/recurrent_op.cc b/paddle/fluid/operators/recurrent_op.cc index 0a4d0c209fd..af925b13a0b 100644 --- a/paddle/fluid/operators/recurrent_op.cc +++ b/paddle/fluid/operators/recurrent_op.cc @@ -30,23 +30,25 @@ namespace operators { using StepScopeVar = std::vector; -const char RecurrentBase::kInputs[] = "inputs"; -const char RecurrentBase::kInitialStates[] = "initial_states"; -const char RecurrentBase::kParameters[] = "parameters"; -const char RecurrentBase::kOutputs[] = "outputs"; -const char RecurrentBase::kStepScopes[] = "step_scopes"; -const char RecurrentBase::kHasStates[] = "has_states"; -const char RecurrentBase::kExStates[] = "ex_states"; -const char RecurrentBase::kStates[] = "states"; -const char RecurrentBase::kStepBlock[] = "sub_block"; -const char RecurrentBase::kReverse[] = "reverse"; -const char RecurrentBase::kIsTrain[] = "is_train"; -const char RecurrentBase::kSkipEagerDeletionVars[] = "skip_eager_deletion_vars"; +const char RecurrentBase::kInputs[] = "inputs"; // NOLINT +const char RecurrentBase::kInitialStates[] = "initial_states"; // NOLINT +const char RecurrentBase::kParameters[] = "parameters"; // NOLINT +const char RecurrentBase::kOutputs[] = "outputs"; // NOLINT +const char RecurrentBase::kStepScopes[] = "step_scopes"; // NOLINT +const char RecurrentBase::kHasStates[] = "has_states"; // NOLINT +const char RecurrentBase::kExStates[] = "ex_states"; // NOLINT +const char RecurrentBase::kStates[] = "states"; // NOLINT +const char RecurrentBase::kStepBlock[] = "sub_block"; // NOLINT +const char RecurrentBase::kReverse[] = "reverse"; // NOLINT +const char RecurrentBase::kIsTrain[] = "is_train"; // NOLINT +const char RecurrentBase::kSkipEagerDeletionVars[] = + "skip_eager_deletion_vars"; // NOLINT #define GRAD_SUFFIX "@GRAD" -const char RecurrentBase::kInputGrads[] = "inputs" GRAD_SUFFIX; -const char RecurrentBase::kOutputGrads[] = "outputs" GRAD_SUFFIX; -const char RecurrentBase::kParamGrads[] = "parameters" GRAD_SUFFIX; -const char RecurrentBase::kInitStateGrads[] = "initial_states" GRAD_SUFFIX; +const char RecurrentBase::kInputGrads[] = "inputs" GRAD_SUFFIX; // NOLINT +const char RecurrentBase::kOutputGrads[] = "outputs" GRAD_SUFFIX; // NOLINT +const char RecurrentBase::kParamGrads[] = "parameters" GRAD_SUFFIX; // NOLINT +const char RecurrentBase::kInitStateGrads[] = + "initial_states" GRAD_SUFFIX; // NOLINT static void ClearStepScopes(const platform::DeviceContext &dev_ctx, framework::Scope *parent_scope, diff --git a/paddle/fluid/platform/collective_helper.cc b/paddle/fluid/platform/collective_helper.cc index b133a57d523..4ffcf53b1a5 100644 --- a/paddle/fluid/platform/collective_helper.cc +++ b/paddle/fluid/platform/collective_helper.cc @@ -15,6 +15,7 @@ #include "paddle/fluid/platform/collective_helper.h" #include +#include #include "paddle/fluid/memory/allocation/allocator_facade.h" #include "paddle/fluid/platform/device/device_wrapper.h" @@ -537,7 +538,8 @@ void XCCLCommContext::CreateXCCLCommMultiTrainer( VLOG(1) << "Begin CreateXCCLCommMultiTrainer. device number: " << kDevices << ", ntrainers: " << ntrainers << ", train_id: " << train_id << ", rind_id: " << ring_id; - phi::ccl::CCLComm comms[kDevices]; + std::vector comms; + comms.resize(kDevices); { for (int i = 0; i < kDevices; i++) { phi::DeviceManager::SetDevice(device_type_, i); @@ -545,7 +547,7 @@ void XCCLCommContext::CreateXCCLCommMultiTrainer( kDevices * ntrainers, xccl_id, train_id * kDevices + i, - comms + i); + comms.data() + i); VLOG(1) << "CCLCommInitRank: " << i; } } diff --git a/paddle/fluid/platform/device/custom/custom_device_test.cc b/paddle/fluid/platform/device/custom/custom_device_test.cc index 2acbb5dae5c..7cb38b8850b 100644 --- a/paddle/fluid/platform/device/custom/custom_device_test.cc +++ b/paddle/fluid/platform/device/custom/custom_device_test.cc @@ -14,6 +14,7 @@ #include +#include #include #include "paddle/fluid/framework/tensor_util.h" @@ -120,8 +121,8 @@ void TestTensorUtils(const paddle::platform::Place& place) { int* src_ptr = src_tensor.mutable_data(phi::make_ddim({3, 3}), paddle::platform::CPUPlace()); - int arr[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - memcpy(src_ptr, arr, 9 * sizeof(int)); + std::array arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + memcpy(src_ptr, arr.data(), 9 * sizeof(int)); // CPU Tensor to GPU Tensor paddle::platform::CustomDeviceContext gpu_ctx(place); diff --git a/paddle/fluid/platform/device_event_base.cc b/paddle/fluid/platform/device_event_base.cc index 87cb91ba84d..cd2d31f1fbe 100644 --- a/paddle/fluid/platform/device_event_base.cc +++ b/paddle/fluid/platform/device_event_base.cc @@ -20,13 +20,15 @@ namespace paddle { namespace platform { -EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; -EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; -EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; -EventFinishFunction DeviceEvent::event_finisher_[MaxDeviceTypes]; -EventSetFinishedFunction DeviceEvent::event_finished_setter_[MaxDeviceTypes]; -EventWaitFunction DeviceEvent::event_waiter_[MaxDeviceTypes][MaxDeviceTypes]; -EventResetFunction DeviceEvent::event_resetter_[MaxDeviceTypes]; +EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; // NOLINT +EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; // NOLINT +EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; // NOLINT +EventFinishFunction DeviceEvent::event_finisher_[MaxDeviceTypes]; // NOLINT +EventSetFinishedFunction // NOLINT + DeviceEvent::event_finished_setter_[MaxDeviceTypes]; +EventWaitFunction DeviceEvent::event_waiter_[MaxDeviceTypes] // NOLINT + [MaxDeviceTypes]; +EventResetFunction DeviceEvent::event_resetter_[MaxDeviceTypes]; // NOLINT /* * Generate flag used to create event on all sorts of equipment. diff --git a/paddle/fluid/platform/enforce_test.cc b/paddle/fluid/platform/enforce_test.cc index 9fc200ca82f..690580d8f9c 100644 --- a/paddle/fluid/platform/enforce_test.cc +++ b/paddle/fluid/platform/enforce_test.cc @@ -11,6 +11,7 @@ limitations under the License. */ #include "paddle/fluid/platform/enforce.h" +#include #include #include "gtest/gtest.h" @@ -286,7 +287,7 @@ TEST(ENFORCE_NOT_NULL, FAIL) { } struct Dims { - size_t dims_[4]; + std::array dims_; bool operator==(const Dims& o) const { for (size_t i = 0; i < 4; ++i) { diff --git a/paddle/fluid/platform/flags.h b/paddle/fluid/platform/flags.h index 32ce438deef..b08aececd2b 100644 --- a/paddle/fluid/platform/flags.h +++ b/paddle/fluid/platform/flags.h @@ -23,7 +23,7 @@ #define __PADDLE_DEFINE_EXPORTED_FLAG( \ __name, __is_writable, __cpp_type, __gflag_type, __default_value, __doc) \ - DEFINE_##__gflag_type(__name, __default_value, __doc); \ + DEFINE_##__gflag_type(__name, __default_value, __doc); /* NOLINT */ \ struct __PaddleRegisterFlag_##__name { \ __PaddleRegisterFlag_##__name() { \ using FlagDeclaredType = \ diff --git a/paddle/fluid/platform/gen_comm_id_helper.cc b/paddle/fluid/platform/gen_comm_id_helper.cc index fd284a9e540..215064b32fc 100644 --- a/paddle/fluid/platform/gen_comm_id_helper.cc +++ b/paddle/fluid/platform/gen_comm_id_helper.cc @@ -23,6 +23,7 @@ limitations under the License. */ #include #include +#include #include #include // NOLINT @@ -220,7 +221,7 @@ static int SocketAccept(int server_fd, const CommHead head) { struct sockaddr_in client_addr; socklen_t addr_length = sizeof(client_addr); - char buffer[1024] = {0}; + std::array buffer{0}; int conn = -1; const char* phead = reinterpret_cast(&head); @@ -231,8 +232,8 @@ static int SocketAccept(int server_fd, const CommHead head) { "accept", conn); - int ret_val = SocketRecv(conn, buffer, sizeof(head)); - if (ret_val > 0 && memcmp(buffer, phead, sizeof(head)) == 0) { + int ret_val = SocketRecv(conn, buffer.data(), sizeof(head)); + if (ret_val > 0 && memcmp(buffer.data(), phead, sizeof(head)) == 0) { // send a message to the sender, indicating that the link is correct CHECK_SYS_CALL(SocketSend(conn, phead, sizeof(head)), "send"); break; // accept client @@ -289,7 +290,7 @@ static int ConnectAddr(const std::string& ep, const CommHead head) { static_assert(sizeof(CommHead) <= 1024, "sizeof(CommHead) must <= buffer size"); - char buffer[1024] = {0}; + std::array buffer{0}; const char* phead = reinterpret_cast(&head); // TODO(wangxi) Set from env, default 900s=15min @@ -312,8 +313,8 @@ static int ConnectAddr(const std::string& ep, const CommHead head) { } CHECK_SYS_CALL(SocketSend(sock, phead, sizeof(head)), "send"); - ret_val = SocketRecv(sock, buffer, sizeof(head)); - if (ret_val > 0 && memcmp(buffer, phead, sizeof(head)) == 0) { + ret_val = SocketRecv(sock, buffer.data(), sizeof(head)); + if (ret_val > 0 && memcmp(buffer.data(), phead, sizeof(head)) == 0) { // recv same message from receiver, indicating that the link is correct break; // accept client } else { @@ -333,71 +334,77 @@ static int ConnectAddr(const std::string& ep, const CommHead head) { template static void RecvCommID(int conn, CommUniqueId* nccl_id) { - char buffer[MAX_COMMUNIQUEID_LEN] = {0}; + std::array buffer{0}; static_assert(sizeof(CommUniqueId) <= MAX_COMMUNIQUEID_LEN, "nccl id bytes must <= buffer size"); - CHECK_SYS_CALL(SocketRecv(conn, buffer, sizeof(CommUniqueId)), + CHECK_SYS_CALL(SocketRecv(conn, buffer.data(), sizeof(CommUniqueId)), "recv comm unique id"); - memcpy(nccl_id, buffer, sizeof(CommUniqueId)); + memcpy(nccl_id, buffer.data(), sizeof(CommUniqueId)); } template static void SendCommID(int conn, CommUniqueId* nccl_id) { - char buffer[MAX_COMMUNIQUEID_LEN] = {0}; - memcpy(buffer, nccl_id, sizeof(CommUniqueId)); + std::array buffer{0}; + memcpy(buffer.data(), nccl_id, sizeof(CommUniqueId)); - CHECK_SYS_CALL(SocketSend(conn, buffer, sizeof(CommUniqueId)), + CHECK_SYS_CALL(SocketSend(conn, buffer.data(), sizeof(CommUniqueId)), "send comm unique id"); } #ifdef PADDLE_WITH_CUSTOM_DEVICE template <> void RecvCommID(int conn, phi::ccl::CCLRootId* nccl_id) { - char buffer[MAX_COMMUNIQUEID_LEN] = {0}; - CHECK_SYS_CALL(SocketRecv(conn, buffer, sizeof(size_t)), + std::array buffer{0}; + CHECK_SYS_CALL(SocketRecv(conn, buffer.data(), sizeof(size_t)), "recv comm unique id size"); - size_t unique_id_size = *reinterpret_cast(buffer); + size_t unique_id_size = *reinterpret_cast(buffer.data()); VLOG(6) << "RecvCommID size: " << unique_id_size; nccl_id->resize(unique_id_size); size_t n_repeat = unique_id_size / MAX_COMMUNIQUEID_LEN; size_t n_remain = unique_id_size % MAX_COMMUNIQUEID_LEN; for (size_t i = 0; i < n_repeat; ++i) { - CHECK_SYS_CALL(SocketRecv(conn, buffer, MAX_COMMUNIQUEID_LEN), + CHECK_SYS_CALL(SocketRecv(conn, buffer.data(), MAX_COMMUNIQUEID_LEN), "recv comm unique id"); memcpy(nccl_id->data() + i * MAX_COMMUNIQUEID_LEN, - buffer, + buffer.data(), MAX_COMMUNIQUEID_LEN); } if (n_remain) { - CHECK_SYS_CALL(SocketRecv(conn, buffer, n_remain), "recv comm unique id"); - memcpy(nccl_id->data() + n_repeat * MAX_COMMUNIQUEID_LEN, buffer, n_remain); + CHECK_SYS_CALL(SocketRecv(conn, buffer.data(), n_remain), + "recv comm unique id"); + memcpy(nccl_id->data() + n_repeat * MAX_COMMUNIQUEID_LEN, + buffer.data(), + n_remain); } VLOG(6) << "RecvCommID done"; } template <> void SendCommID(int conn, phi::ccl::CCLRootId* nccl_id) { - char buffer[MAX_COMMUNIQUEID_LEN] = {0}; + std::array buffer{0}; size_t unique_id_size = nccl_id->size(); VLOG(6) << "SendCommID size: " << unique_id_size; - memcpy(buffer, &unique_id_size, sizeof(size_t)); - CHECK_SYS_CALL(SocketSend(conn, buffer, sizeof(size_t)), + memcpy(buffer.data(), &unique_id_size, sizeof(size_t)); + CHECK_SYS_CALL(SocketSend(conn, buffer.data(), sizeof(size_t)), "send comm unique id size"); size_t n_repeat = unique_id_size / MAX_COMMUNIQUEID_LEN; size_t n_remain = unique_id_size % MAX_COMMUNIQUEID_LEN; for (size_t i = 0; i < n_repeat; ++i) { - memcpy(buffer, + memcpy(buffer.data(), nccl_id->data() + i * MAX_COMMUNIQUEID_LEN, MAX_COMMUNIQUEID_LEN); - CHECK_SYS_CALL(SocketSend(conn, buffer, MAX_COMMUNIQUEID_LEN), + CHECK_SYS_CALL(SocketSend(conn, buffer.data(), MAX_COMMUNIQUEID_LEN), "send comm unique id"); } if (n_remain) { - memcpy(buffer, nccl_id->data() + n_repeat * MAX_COMMUNIQUEID_LEN, n_remain); - CHECK_SYS_CALL(SocketSend(conn, buffer, n_remain), "send comm unique id"); + memcpy(buffer.data(), + nccl_id->data() + n_repeat * MAX_COMMUNIQUEID_LEN, + n_remain); + CHECK_SYS_CALL(SocketSend(conn, buffer.data(), n_remain), + "send comm unique id"); } VLOG(6) << "SendCommID done"; } diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 49c996b9b62..7ef5c23c47b 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -263,7 +263,7 @@ void InitDevices(const std::vector devices) { #ifndef _WIN32 // Description Quoted from // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html -const struct { +const struct { // NOLINT int signal_number; const char *name; const char *error_string; diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index 8ebb8c1ee6c..f3bc89b8fce 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -38,7 +38,7 @@ PADDLE_DEFINE_EXPORTED_bool(enable_rpc_profiler, false, "Enable rpc profiler or not."); -DEFINE_bool(enable_record_memory, false, "enable memory recorder"); +DEFINE_bool(enable_record_memory, false, "enable memory recorder"); // NOLINT #if defined(_WIN32) && defined(PHI_SHARED) phi::ProfilerState phi::ProfilerHelper::g_state = phi::ProfilerState::kDisabled; diff --git a/paddle/fluid/platform/profiler/utils.cc b/paddle/fluid/platform/profiler/utils.cc index 957911fe17c..46a94e7fcb2 100644 --- a/paddle/fluid/platform/profiler/utils.cc +++ b/paddle/fluid/platform/profiler/utils.cc @@ -145,13 +145,16 @@ float CalculateEstOccupancy(uint32_t DeviceId, #endif // PADDLE_WITH_CUPTI const char* StringTracerMemEventType(TracerMemEventType type) { - static const char* categary_name_[] = { - "Allocate", "Free", "ReservedAllocate", "ReservedFree"}; + static const char* categary_name_[] = {// NOLINT + "Allocate", + "Free", + "ReservedAllocate", + "ReservedFree"}; return categary_name_[static_cast(type)]; } const char* StringTracerEventType(TracerEventType type) { - static const char* categary_name_[] = {"Operator", + static const char* categary_name_[] = {"Operator", // NOLINT "Dataloader", "ProfileStep", "CudaRuntime", diff --git a/paddle/fluid/pybind/eager.cc b/paddle/fluid/pybind/eager.cc index e2d23fa80d4..bd2f5e75f50 100644 --- a/paddle/fluid/pybind/eager.cc +++ b/paddle/fluid/pybind/eager.cc @@ -777,7 +777,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { PyObject* kw_dist_attr = nullptr; // the keywords argument - static char* kwlist[] = {const_cast("value"), + static char* kwlist[] = {const_cast("value"), // NOLINT const_cast("place"), const_cast("persistable"), const_cast("zero_copy"), @@ -1129,7 +1129,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { PyObject* kw_dims = nullptr; // the keywords argument - static char* kwlist[] = {const_cast("value"), + static char* kwlist[] = {const_cast("value"), // NOLINT const_cast("zero_copy"), const_cast("name"), const_cast("dims"), @@ -1317,12 +1317,12 @@ static void TensorDealloc(TensorObject* self) { Py_TYPE(self)->tp_free(reinterpret_cast(self)); } -extern struct PyGetSetDef variable_properties[]; -extern struct PyGetSetDef string_tensor_variable_properties[]; +extern struct PyGetSetDef variable_properties[]; // NOLINT +extern struct PyGetSetDef string_tensor_variable_properties[]; // NOLINT -extern PyMethodDef variable_methods[]; -extern PyMethodDef math_op_patch_methods[]; -extern PyMethodDef string_tensor_variable_methods[]; +extern PyMethodDef variable_methods[]; // NOLINT +extern PyMethodDef math_op_patch_methods[]; // NOLINT +extern PyMethodDef string_tensor_variable_methods[]; // NOLINT PyNumberMethods number_methods; PySequenceMethods sequence_methods; diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 06edbeef48c..6544d681704 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -455,7 +455,8 @@ static PyObject* tensor_method_numpy_for_string_tensor(TensorObject* self, longest_pstring->data(), longest_pstring->size()); max_unicode_length = (max_unicode_length == 0) ? 1 : max_unicode_length; VLOG(6) << "The max unicode length is " << max_unicode_length; - auto sp = std::make_unique(max_unicode_length * numel); + auto sp = + std::make_unique(max_unicode_length * numel); // NOLINT auto py_array_data = sp.get(); memset(py_array_data, 0, max_unicode_length * numel * sizeof(uint32_t)); for (int64_t i = 0; i < numel; ++i) { diff --git a/paddle/fluid/pybind/eager_properties.cc b/paddle/fluid/pybind/eager_properties.cc index 23d1d219ff0..f6ade48deb6 100644 --- a/paddle/fluid/pybind/eager_properties.cc +++ b/paddle/fluid/pybind/eager_properties.cc @@ -445,7 +445,7 @@ PyObject* tensor_properties_get_grad_fn(TensorObject* self, void* closure) { EAGER_CATCH_AND_THROW_RETURN_NULL } -struct PyGetSetDef variable_properties[] = { +struct PyGetSetDef variable_properties[] = { // NOLINT {"data", (getter)tensor_properties_get_data, (setter)tensor_properties_set_data, @@ -510,7 +510,7 @@ struct PyGetSetDef variable_properties[] = { {nullptr, nullptr, nullptr, nullptr, nullptr}}; // variable_properties for core.eager.StringTensor -struct PyGetSetDef string_tensor_variable_properties[] = { +struct PyGetSetDef string_tensor_variable_properties[] = { // NOLINT {"name", (getter)tensor_properties_get_name, (setter)tensor_properties_set_name, diff --git a/paddle/fluid/pybind/eager_py_layer.cc b/paddle/fluid/pybind/eager_py_layer.cc index 6d5814bdaf8..5f5e69ff2a8 100644 --- a/paddle/fluid/pybind/eager_py_layer.cc +++ b/paddle/fluid/pybind/eager_py_layer.cc @@ -662,7 +662,7 @@ int tensor_properties_set_materialize_grads(PyLayerObject* self, EAGER_CATCH_AND_THROW_RETURN_NEG } -PyMethodDef pylayer_methods[] = {{"name", +PyMethodDef pylayer_methods[] = {{"name", // NOLINT (PyCFunction)(void (*)())pylayer_method_name, METH_NOARGS, nullptr}, @@ -672,7 +672,7 @@ PyMethodDef pylayer_methods[] = {{"name", nullptr}, {nullptr, nullptr, 0, nullptr}}; -struct PyGetSetDef pylayer_properties[] { +struct PyGetSetDef pylayer_properties[] { // NOLINT {"container", (getter)tensor_properties_get_container, (setter)tensor_properties_set_container, diff --git a/paddle/phi/backends/custom/custom_device.cc b/paddle/phi/backends/custom/custom_device.cc index cd4148b94eb..b51695c8083 100644 --- a/paddle/phi/backends/custom/custom_device.cc +++ b/paddle/phi/backends/custom/custom_device.cc @@ -356,7 +356,7 @@ class CustomDevice : public DeviceInterface { } } else { if (!pimpl_->memory_copy_p2p) { - std::unique_ptr tmp(new uint8_t[size]); + std::unique_ptr tmp(new uint8_t[size]); // NOLINT MemoryCopyD2H(src_dev_id, tmp.get(), src, size); MemoryCopyH2D(dst_dev_id, dst, tmp.get(), size); } else { @@ -447,7 +447,7 @@ class CustomDevice : public DeviceInterface { PADDLE_ENFORCE_CUSTOM_DEVICE_SUCCESS( pimpl_->device_memory_set(device, ptr, value, size)); } else { - std::unique_ptr tmp(new uint8_t[size]); + std::unique_ptr tmp(new uint8_t[size]); // NOLINT memset(tmp.get(), value, size); MemoryCopyH2D(dev_id, ptr, tmp.get(), size); } diff --git a/paddle/phi/kernels/cpu/box_coder_kernel.cc b/paddle/phi/kernels/cpu/box_coder_kernel.cc index a8bd1c5b0bf..484f89c3f28 100644 --- a/paddle/phi/kernels/cpu/box_coder_kernel.cc +++ b/paddle/phi/kernels/cpu/box_coder_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/box_coder_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -116,8 +117,8 @@ void DecodeCenterSize(const DenseTensor *target_box, auto *target_box_data = target_box->data(); auto *prior_box_data = prior_box->data(); - T var_data[4] = {1., 1., 1., 1.}; - T *var_ptr = var_data; + std::array var_data{1., 1., 1., 1.}; + T *var_ptr = var_data.data(); size_t offset = i * col * len + j * len; int prior_box_offset = axis == 0 ? j * len : i * len; diff --git a/paddle/phi/kernels/cpu/fill_diagonal_tensor_grad_kernel.cc b/paddle/phi/kernels/cpu/fill_diagonal_tensor_grad_kernel.cc index 318e2016097..a89bd02ccda 100644 --- a/paddle/phi/kernels/cpu/fill_diagonal_tensor_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/fill_diagonal_tensor_grad_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/fill_diagonal_tensor_grad_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -38,10 +39,17 @@ void FillDiagonalTensorGradKernel(const Context& ctx, } } - int64_t new_dims[2], strides[2]; + std::array new_dims; + std::array strides; std::vector matdim; matdim.resize(matrows); - CalMatDims(dx_dims, dim1, dim2, &offset, new_dims, strides, matdim.data()); + CalMatDims(dx_dims, + dim1, + dim2, + &offset, + new_dims.data(), + strides.data(), + matdim.data()); auto size = x_grad->numel(); phi::Copy(ctx, out_grad, ctx.GetPlace(), false, x_grad); diff --git a/paddle/phi/kernels/cpu/fill_diagonal_tensor_kernel.cc b/paddle/phi/kernels/cpu/fill_diagonal_tensor_kernel.cc index 4e8030199d1..a232cbbb58e 100644 --- a/paddle/phi/kernels/cpu/fill_diagonal_tensor_kernel.cc +++ b/paddle/phi/kernels/cpu/fill_diagonal_tensor_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/fill_diagonal_tensor_kernel.h" +#include #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/core/tensor_utils.h" @@ -87,10 +88,17 @@ void FillDiagonalTensorKernel(const Context &ctx, auto matdims = y.dims(); auto fill_dims = phi::flatten_to_2d(matdims, matdims.size() - 1); - int64_t new_dims[2], strides[2]; + std::array new_dims; + std::array strides; std::vector matdim; matdim.resize(fill_dims[0]); - CalMatDims(out_dims, dim1, dim2, &offset, new_dims, strides, matdim.data()); + CalMatDims(out_dims, + dim1, + dim2, + &offset, + new_dims.data(), + strides.data(), + matdim.data()); PADDLE_ENFORCE_EQ( new_dims[0], fill_dims[0], diff --git a/paddle/phi/kernels/cpu/index_put_grad_kernel.cc b/paddle/phi/kernels/cpu/index_put_grad_kernel.cc index 89b3ba61b83..b8990e11809 100644 --- a/paddle/phi/kernels/cpu/index_put_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/index_put_grad_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/index_put_grad_kernel.h" +#include #include #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/cast_kernel.h" @@ -78,7 +79,7 @@ void LaunchIndexPutGradKernel(const Context& dev_ctx, bool accumulate, DenseTensor* value_grad, DenseTensor* x_grad) { - const int64_t* pd_indices[7]; + std::array pd_indices; for (size_t i = 0; i < indices.size(); ++i) { pd_indices[i] = indices[i]->data(); } @@ -93,7 +94,7 @@ void LaunchIndexPutGradKernel(const Context& dev_ctx, auto x_grad_stride = phi::stride(x_grad_dims); set_zero_kernel( - numel, pd_indices, x_grad_stride, x_grad_dims, x_grad_data); + numel, pd_indices.data(), x_grad_stride, x_grad_dims, x_grad_data); } } @@ -111,7 +112,7 @@ void LaunchIndexPutGradKernel(const Context& dev_ctx, index_put_grad_kernel(numel, out_grad_data, - pd_indices, + pd_indices.data(), out_grad_stride, out_grad_dims, tmp_value_grad_data); @@ -131,7 +132,7 @@ void LaunchIndexPutGradKernel(const Context& dev_ctx, index_put_grad_kernel(numel, out_grad_data, - pd_indices, + pd_indices.data(), out_grad_stride, out_grad_dims, value_grad_data); @@ -144,7 +145,7 @@ void LaunchIndexPutGradKernel(const Context& dev_ctx, index_put_grad_kernel(numel, out_grad_data, - pd_indices, + pd_indices.data(), out_grad_stride, out_grad_dims, tmp_value_grad_data); diff --git a/paddle/phi/kernels/cpu/index_put_kernel.cc b/paddle/phi/kernels/cpu/index_put_kernel.cc index 34701539c8e..ded3eb6180a 100644 --- a/paddle/phi/kernels/cpu/index_put_kernel.cc +++ b/paddle/phi/kernels/cpu/index_put_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/index_put_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/cast_kernel.h" @@ -75,7 +76,7 @@ void LaunchIndexPutKernel(const Context& dev_ctx, int64_t is_single_val_tensor = (value.numel() == 1) ? 0 : INT64_MAX; - const int64_t* pd_indices[7]; + std::array pd_indices; for (size_t i = 0; i < indices.size(); ++i) { pd_indices[i] = indices[i]->data(); } @@ -83,7 +84,7 @@ void LaunchIndexPutKernel(const Context& dev_ctx, index_put_kernel(numel, x_data, val_data, - pd_indices, + pd_indices.data(), x_stride, x_dims, is_single_val_tensor, diff --git a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc index 6361680f2ba..f72bb3f55e0 100644 --- a/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_grad_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/interpolate_grad_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/common/amp_type_traits.h" @@ -190,11 +191,11 @@ static void BicubicInterpolationGrad(const DenseTensor& output_grad, int input_x = floorf(x_n); MT x_t = x_n - input_x; - MT x_coeffs[4]; - MT y_coeffs[4]; + std::array x_coeffs; + std::array y_coeffs; - funcs::get_cubic_upsample_coefficients(x_coeffs, x_t); - funcs::get_cubic_upsample_coefficients(y_coeffs, y_t); + funcs::get_cubic_upsample_coefficients(x_coeffs.data(), x_t); + funcs::get_cubic_upsample_coefficients(y_coeffs.data(), y_t); for (int i = 0; i < n; i++) { // loop for batches for (int j = 0; j < c; j++) { // loop for channels diff --git a/paddle/phi/kernels/cpu/interpolate_kernel.cc b/paddle/phi/kernels/cpu/interpolate_kernel.cc index 3cd02f67fb2..9c2a9f56254 100644 --- a/paddle/phi/kernels/cpu/interpolate_kernel.cc +++ b/paddle/phi/kernels/cpu/interpolate_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/interpolate_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/common/amp_type_traits.h" @@ -24,8 +25,8 @@ namespace phi { template static inline T cubic_interp(T x0, T x1, T x2, T x3, T t) { - T coeffs[4]; - funcs::get_cubic_upsample_coefficients(coeffs, t); + std::array coeffs; + funcs::get_cubic_upsample_coefficients(coeffs.data(), t); return x0 * coeffs[0] + x1 * coeffs[1] + x2 * coeffs[2] + x3 * coeffs[3]; } @@ -274,7 +275,7 @@ static void BicubicInterpolation(const DenseTensor& input, for (int i = 0; i < n; i++) { // loop for batches for (int j = 0; j < c; j++) { // loop for channels - MT coefficients[4]; + std::array coefficients; // interp 4 times in x direction for (int ii = 0; ii < 4; ii++) { int access_y = std::max(std::min(input_y - 1 + ii, in_h - 1), diff --git a/paddle/phi/kernels/cpu/nms_kernel.cc b/paddle/phi/kernels/cpu/nms_kernel.cc index 8ce7593e326..5534fd71a18 100644 --- a/paddle/phi/kernels/cpu/nms_kernel.cc +++ b/paddle/phi/kernels/cpu/nms_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/nms_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -32,17 +33,17 @@ static int64_t NMS(const T* boxes_data, for (int64_t i = 0; i < num_boxes; ++i) { if (masks[i / 64] & 1ULL << (i % 64)) continue; - T box_1[4]; + std::array box_1; for (int k = 0; k < 4; ++k) { box_1[k] = boxes_data[i * 4 + k]; } for (int64_t j = i + 1; j < num_boxes; ++j) { if (masks[j / 64] & 1ULL << (j % 64)) continue; - T box_2[4]; + std::array box_2; for (int k = 0; k < 4; ++k) { box_2[k] = boxes_data[j * 4 + k]; } - bool is_overlap = CalculateIoU(box_1, box_2, threshold); + bool is_overlap = CalculateIoU(box_1.data(), box_2.data(), threshold); if (is_overlap) { masks[j / 64] |= 1ULL << (j % 64); } diff --git a/paddle/phi/kernels/cpu/yolo_box_kernel.cc b/paddle/phi/kernels/cpu/yolo_box_kernel.cc index 0c04c78214a..9e596f6d1da 100644 --- a/paddle/phi/kernels/cpu/yolo_box_kernel.cc +++ b/paddle/phi/kernels/cpu/yolo_box_kernel.cc @@ -13,6 +13,7 @@ // limitations under the License. #include "paddle/phi/kernels/yolo_box_kernel.h" +#include #include "paddle/phi/backends/cpu/cpu_context.h" #include "paddle/phi/core/kernel_registry.h" @@ -66,7 +67,7 @@ void YoloBoxKernel(const Context& dev_ctx, memset(scores_data, 0, scores->numel() * sizeof(T)); - T box[4]; + std::array box; for (int i = 0; i < n; i++) { int img_height = imgsize_data[2 * i]; int img_width = imgsize_data[2 * i + 1]; @@ -90,7 +91,7 @@ void YoloBoxKernel(const Context& dev_ctx, int box_idx = funcs::GetEntryIndex( i, j, k * w + l, an_num, an_stride, stride, 0, iou_aware); - funcs::GetYoloBox(box, + funcs::GetYoloBox(box.data(), input_data, anchors_data, l, @@ -107,8 +108,12 @@ void YoloBoxKernel(const Context& dev_ctx, scale, bias); box_idx = (i * box_num + j * stride + k * w + l) * 4; - funcs::CalcDetectionBox( - boxes_data, box, box_idx, img_height, img_width, clip_bbox); + funcs::CalcDetectionBox(boxes_data, + box.data(), + box_idx, + img_height, + img_width, + clip_bbox); int label_idx = funcs::GetEntryIndex( i, j, k * w + l, an_num, an_stride, stride, 5, iou_aware); diff --git a/paddle/phi/kernels/funcs/jit/benchmark.cc b/paddle/phi/kernels/funcs/jit/benchmark.cc index 26552308cd8..ad7146db0d9 100644 --- a/paddle/phi/kernels/funcs/jit/benchmark.cc +++ b/paddle/phi/kernels/funcs/jit/benchmark.cc @@ -26,7 +26,7 @@ DEFINE_int32(burning, 10, "Burning times."); DEFINE_int32(repeat, 3000, "Repeat times."); DEFINE_int32(max_size, 1000, "The Max size would be tested."); -DEFINE_string(filter, "", "The Benchmark name would be run."); +DEFINE_string(filter, "", "The Benchmark name would be run."); // NOLINT class BenchJITKernel { public: diff --git a/paddle/phi/kernels/funcs/jit/gen/act.cc b/paddle/phi/kernels/funcs/jit/gen/act.cc index dbd23e23023..731910b35d0 100644 --- a/paddle/phi/kernels/funcs/jit/gen/act.cc +++ b/paddle/phi/kernels/funcs/jit/gen/act.cc @@ -41,9 +41,9 @@ const float ALIGN32_BEG exp_float_consts[] ALIGN32_END = { // NOLINT REPEAT_8TIMES(SIGMOID_THRESHOLD_MAX), REPEAT_8TIMES(SIGMOID_THRESHOLD_MIN)}; -const int ALIGN32_BEG exp_int_0x7f[] ALIGN32_END = { - REPEAT_8TIMES(0x7f)}; // NOLINT -int ALIGN32_BEG g_tmp_mem[16] ALIGN32_END = {0}; // NOLINT +const int ALIGN32_BEG exp_int_0x7f[] ALIGN32_END = { // NOLINT + REPEAT_8TIMES(0x7f)}; // NOLINT +int ALIGN32_BEG g_tmp_mem[16] ALIGN32_END = {0}; // NOLINT void VActJitCode::genCode() { int offset = 0; diff --git a/paddle/phi/kernels/funcs/jit/test.cc b/paddle/phi/kernels/funcs/jit/test.cc index 34fb77c8617..0dd5f6c6ba5 100644 --- a/paddle/phi/kernels/funcs/jit/test.cc +++ b/paddle/phi/kernels/funcs/jit/test.cc @@ -12,6 +12,7 @@ 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 #include #include @@ -1171,7 +1172,9 @@ TEST(JITKernel_helper, GetAllCandidateFuncs) { TEST(JITKernel_helper, pack_weights) { const int N = 8 * 60, K = 2; - float src[K][N], yref[K][N], y[K * N]; + std::array, K> src; + std::array, K> yref; + std::array y; float* x = &(src[0][0]); float* ref = &(yref[0][0]); for (int i = 0; i < N * K; ++i) { @@ -1200,8 +1203,8 @@ TEST(JITKernel_helper, pack_weights) { acc += g; } - jit::pack_weights(x, y, N, K); - ExpectEQ(y, ref, N * K); + jit::pack_weights(x, y.data(), N, K); + ExpectEQ(y.data(), ref, N * K); } TEST(JITKernel_helper, attr) { diff --git a/paddle/phi/kernels/strings/unicode.cc b/paddle/phi/kernels/strings/unicode.cc index 9f636809de8..2d92104fdb5 100644 --- a/paddle/phi/kernels/strings/unicode.cc +++ b/paddle/phi/kernels/strings/unicode.cc @@ -22,8 +22,8 @@ limitations under the License. */ namespace phi { namespace strings { -static const void* utils_map[4] = {nullptr}; -static uint16_t CHARCASES_MAP[65536] = {0}; +static const void* utils_map[4] = {nullptr}; // NOLINT +static uint16_t CHARCASES_MAP[65536] = {0}; // NOLINT const uint8_t* GetUniFlagMap() { if (utils_map[1] == nullptr) { diff --git a/paddle/utils/array_ref_test.cc b/paddle/utils/array_ref_test.cc index 1c9087f1a89..73fdd01fc3b 100644 --- a/paddle/utils/array_ref_test.cc +++ b/paddle/utils/array_ref_test.cc @@ -14,6 +14,7 @@ #include "paddle/utils/array_ref.h" +#include #include #include @@ -35,15 +36,15 @@ TEST(array_ref, array_ref) { CHECK_EQ(c.data(), &v); CHECK_EQ(c.equals(paddle::make_array_ref(v)), true); - int v1[5] = {1, 2, 3, 4, 5}; - paddle::array_ref d(v1, 5); + std::array v1 = {1, 2, 3, 4, 5}; + paddle::array_ref d(v1.data(), 5); CHECK_EQ(d.size(), size_t(5)); - CHECK_EQ(d.data(), v1); - CHECK_EQ(d.equals(paddle::make_array_ref(v1, 5)), true); + CHECK_EQ(d.data(), v1.data()); + CHECK_EQ(d.equals(paddle::make_array_ref(v1.data(), 5)), true); paddle::array_ref e(&v1[0], &v1[4]); CHECK_EQ(e.size(), size_t(4)); - CHECK_EQ(e.data(), v1); + CHECK_EQ(e.data(), v1.data()); CHECK_EQ(e.equals(paddle::make_array_ref(&v1[0], &v1[4])), true); paddle::small_vector small_vector{1, 2, 3}; diff --git a/paddle/utils/span_test.cc b/paddle/utils/span_test.cc index 9ca34b7fefb..3244990221f 100644 --- a/paddle/utils/span_test.cc +++ b/paddle/utils/span_test.cc @@ -69,7 +69,7 @@ TEST(pointer_length_ctor, span) { // dynamic size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s(arr, 3); CHECK_EQ(s.size(), 3UL); @@ -80,7 +80,7 @@ TEST(pointer_length_ctor, span) { // fixed size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s(arr, 3); CHECK_EQ(s.size(), 3UL); @@ -100,7 +100,7 @@ TEST(pointer_pointer_ctor, span) { // dynamic size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s{arr, arr + 3}; CHECK_EQ(s.size(), 3UL); @@ -111,7 +111,7 @@ TEST(pointer_pointer_ctor, span) { // fixed size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s{arr, arr + 3}; CHECK_EQ(s.size(), 3UL); @@ -122,8 +122,8 @@ TEST(pointer_pointer_ctor, span) { } TEST(c_array_ctor, span) { - using int_array_t = int[3]; - using float_array_t = float[3]; + using int_array_t = int[3]; // NOLINT + using float_array_t = float[3]; // NOLINT static_assert(std::is_nothrow_constructible, int_array_t&>::value, ""); @@ -171,7 +171,7 @@ TEST(c_array_ctor, span) { // non-const, dynamic size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s{arr}; CHECK_EQ(s.size(), 3UL); CHECK_EQ(s.data(), arr); @@ -181,7 +181,7 @@ TEST(c_array_ctor, span) { // const, dynamic size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s{arr}; CHECK_EQ(s.size(), 3UL); CHECK_EQ(s.data(), arr); @@ -191,7 +191,7 @@ TEST(c_array_ctor, span) { // non-const, static size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s{arr}; CHECK_EQ(s.size(), 3UL); CHECK_EQ(s.data(), arr); @@ -201,7 +201,7 @@ TEST(c_array_ctor, span) { // const, dynamic size { - int arr[] = {1, 2, 3}; + int arr[] = {1, 2, 3}; // NOLINT span s{arr}; CHECK_EQ(s.size(), 3UL); CHECK_EQ(s.data(), arr); @@ -492,7 +492,7 @@ TEST(ctor_from_spans, span) { TEST(subview, span) { // first { - int arr[] = {1, 2, 3, 4, 5}; + int arr[] = {1, 2, 3, 4, 5}; // NOLINT span s{arr}; auto f = s.first<3>(); @@ -505,7 +505,7 @@ TEST(subview, span) { // last { - int arr[] = {1, 2, 3, 4, 5}; + int arr[] = {1, 2, 3, 4, 5}; // NOLINT span s{arr}; auto l = s.last<3>(); @@ -518,7 +518,7 @@ TEST(subview, span) { // subspan { - int arr[] = {1, 2, 3, 4, 5}; + int arr[] = {1, 2, 3, 4, 5}; // NOLINT span s{arr}; auto ss = s.subspan<1, 2>(); @@ -531,7 +531,7 @@ TEST(subview, span) { // first(n) { - int arr[] = {1, 2, 3, 4, 5}; + int arr[] = {1, 2, 3, 4, 5}; // NOLINT span s{arr}; auto f = s.first(3); @@ -544,7 +544,7 @@ TEST(subview, span) { // last(n) { - int arr[] = {1, 2, 3, 4, 5}; + int arr[] = {1, 2, 3, 4, 5}; // NOLINT span s{arr}; auto l = s.last(3); @@ -557,7 +557,7 @@ TEST(subview, span) { // subspan(n) { - int arr[] = {1, 2, 3, 4, 5}; + int arr[] = {1, 2, 3, 4, 5}; // NOLINT span s{arr}; auto ss = s.subspan(1, 2); @@ -577,13 +577,13 @@ TEST(observers, span) { static_assert(empty.size() == 0, ""); // NOLINT static_assert(empty.empty(), ""); - constexpr int arr[] = {1, 2, 3}; + constexpr int arr[] = {1, 2, 3}; // NOLINT static_assert(span{arr}.size() == 3, ""); static_assert(!span{arr}.empty(), ""); } TEST(element_access, span) { - constexpr int arr[] = {1, 2, 3}; + constexpr int arr[] = {1, 2, 3}; // NOLINT span s{arr}; CHECK_EQ(s[0], arr[0]); diff --git a/paddle/utils/string/string_helper_test.cc b/paddle/utils/string/string_helper_test.cc index 631edca2408..f35e4d37f33 100644 --- a/paddle/utils/string/string_helper_test.cc +++ b/paddle/utils/string/string_helper_test.cc @@ -33,7 +33,7 @@ TEST(StringHelper, EndsWith) { TEST(StringHelper, FormatStringAppend) { std::string str("hello"); - char fmt[] = "%d"; + char fmt[] = "%d"; // NOLINT paddle::string::format_string_append(str, fmt, 10); EXPECT_EQ(str, "hello10"); diff --git a/paddle/utils/string/to_string_test.cc b/paddle/utils/string/to_string_test.cc index 740e4435fc3..c436e59635f 100644 --- a/paddle/utils/string/to_string_test.cc +++ b/paddle/utils/string/to_string_test.cc @@ -16,7 +16,7 @@ limitations under the License. */ #include -constexpr char kOutputString[] = "User Defined Output"; +constexpr char kOutputString[] = "User Defined Output"; // NOLINT class UserDefinedClass { public: }; diff --git a/test/cpp/fluid/benchmark/op_tester.cc b/test/cpp/fluid/benchmark/op_tester.cc index eeb8220e73b..447561d0915 100644 --- a/test/cpp/fluid/benchmark/op_tester.cc +++ b/test/cpp/fluid/benchmark/op_tester.cc @@ -33,7 +33,7 @@ namespace paddle { namespace operators { namespace benchmark { -DEFINE_string(op_config_list, "", "Path of op config file."); +DEFINE_string(op_config_list, "", "Path of op config file."); // NOLINT DEFINE_int32(specified_config_id, -1, "Test the specified op config."); void OpTester::Init(const std::string &filename) { diff --git a/test/cpp/fluid/detection/mask_util_test.cc b/test/cpp/fluid/detection/mask_util_test.cc index e64b5fa953a..b1fbbf106b6 100644 --- a/test/cpp/fluid/detection/mask_util_test.cc +++ b/test/cpp/fluid/detection/mask_util_test.cc @@ -29,12 +29,21 @@ void Compare(const T* a, const T* b, const int n) { } TEST(MaskUtil, Poly2MaskTest) { - float polys[] = { - 1.97f, 1.88f, 5.81f, 1.88f, 1.69f, 6.53f, 5.94f, 6.38f, 1.97f, 1.88f}; + float polys[] = {// NOLINT + 1.97f, + 1.88f, + 5.81f, + 1.88f, + 1.69f, + 6.53f, + 5.94f, + 6.38f, + 1.97f, + 1.88f}; int h = 8, w = 8; int k = 5; // length(polys) / 2 // clang-format off - uint8_t expect_mask[] = { + uint8_t expect_mask[] = { // NOLINT 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, @@ -67,7 +76,7 @@ TEST(MaskUtil, Poly2BoxesTest) { {{1.97f, 1.88f, 5.81f, 1.88f, 1.69f, 6.53f, 5.94f, 6.38f, 1.97f, 1.88f}}, {{2.97f, 1.88f, 3.81f, 1.68f, 1.69f, 6.63f, 6.94f, 6.58f, 2.97f, 0.88f}} }; - float expect_boxes[] = { + float expect_boxes[] = { // NOLINT 1.69f, 1.88f, 5.94f, 6.53f, 1.69f, 0.88f, 6.94f, 6.63f }; @@ -85,10 +94,10 @@ TEST(MaskUtil, Polys2MaskWrtBoxTest) { std::vector>> polys = {{ {1.97f, 1.88f, 5.81f, 1.88f, 1.69f, 6.53f, 5.94f, 6.38f, 1.97f, 1.88f}, {2.97f, 1.88f, 3.81f, 1.68f, 1.69f, 6.63f, 6.94f, 6.58f, 2.97f, 0.88f}}}; - float expect_boxes[] = { + float expect_boxes[] = { // NOLINT 1.69f, 0.88f, 6.94f, 6.63f }; - uint8_t expect_mask[] = { + uint8_t expect_mask[] = { // NOLINT 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, diff --git a/test/cpp/fluid/math/im2col_test.cc b/test/cpp/fluid/math/im2col_test.cc index e89d6009074..13a4f0b6750 100644 --- a/test/cpp/fluid/math/im2col_test.cc +++ b/test/cpp/fluid/math/im2col_test.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/phi/kernels/funcs/im2col.h" #include +#include #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/platform/device_context.h" @@ -60,8 +61,8 @@ void testIm2col() { (input_width - filter_size + padding[2] + padding[3]) / stride[1] + 1; float* input_ptr = input_tmp.mutable_data( {1, input_height, input_width}, paddle::platform::CPUPlace()); - float arr[6] = {0, 1, 2, 3, 4, 5}; - memcpy(input_ptr, arr, 6 * sizeof(float)); + std::array arr = {0, 1, 2, 3, 4, 5}; + memcpy(input_ptr, arr.data(), 6 * sizeof(float)); auto* place = new Place(); DeviceContext* context = new DeviceContext(*place); @@ -85,8 +86,8 @@ void testIm2col() { im2col(*context, input, dilation, stride, padding, &output_cfo); im2col_ocf(*context, input, dilation, stride, padding, &output_ocf); - float out_cfo_data[] = {0, 1, 1, 2, 3, 4, 4, 5}; - float out_ocf_data[] = {0, 1, 3, 4, 1, 2, 4, 5}; + std::array out_cfo_data = {0, 1, 1, 2, 3, 4, 4, 5}; + std::array out_ocf_data = {0, 1, 3, 4, 1, 2, 4, 5}; float* out_cfo_ptr; if (paddle::platform::is_cpu_place(*place)) { @@ -118,7 +119,7 @@ void testIm2col() { col2im; phi::funcs::Col2ImFunctor col2im_ocf; - float col2im_data[] = {0, 2, 2, 3, 8, 5}; + std::array col2im_data = {0, 2, 2, 3, 8, 5}; memset(input_ptr, 0, 6 * sizeof(float)); if (paddle::platform::is_cpu_place(*place)) { diff --git a/test/cpp/fluid/math/vol2col_test.cc b/test/cpp/fluid/math/vol2col_test.cc index 7c44a975135..27a873082a1 100644 --- a/test/cpp/fluid/math/vol2col_test.cc +++ b/test/cpp/fluid/math/vol2col_test.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include "paddle/phi/kernels/funcs/vol2col.h" #include +#include #include "paddle/fluid/framework/tensor_util.h" #include "paddle/fluid/platform/device_context.h" @@ -68,8 +69,8 @@ void testVol2col() { float* input_ptr = input_tmp.mutable_data({1, input_depth, input_height, input_width}, paddle::platform::CPUPlace()); - float arr[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; - memcpy(input_ptr, arr, 12 * sizeof(float)); + std::array arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + memcpy(input_ptr, arr.data(), 12 * sizeof(float)); if (paddle::platform::is_cpu_place(*place)) { input = input_tmp; @@ -88,7 +89,8 @@ void testVol2col() { phi::funcs::Vol2ColFunctor vol2col; vol2col(*context, input, dilations, strides, paddings, &output); - float vol_2_col[] = {0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11}; + std::array vol_2_col = { + 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 11}; float* out_cfo_ptr; if (paddle::platform::is_cpu_place(*place)) { out_cfo_ptr = output.data(); @@ -103,7 +105,7 @@ void testVol2col() { } // Col2Vol test - float col_2_vol[] = {0, 2, 2, 3, 8, 5, 6, 14, 8, 9, 20, 11}; + std::array col_2_vol = {0, 2, 2, 3, 8, 5, 6, 14, 8, 9, 20, 11}; memset(input_ptr, 0, 12 * sizeof(float)); if (paddle::platform::is_cpu_place(*place)) { input = input_tmp; diff --git a/test/cpp/inference/analysis/analyzer_tester.cc b/test/cpp/inference/analysis/analyzer_tester.cc index 3f5be92f5a3..87ce0be0173 100644 --- a/test/cpp/inference/analysis/analyzer_tester.cc +++ b/test/cpp/inference/analysis/analyzer_tester.cc @@ -14,6 +14,7 @@ #include #include +#include #include "paddle/fluid/inference/analysis/analyzer.h" #include "paddle/fluid/inference/analysis/ut_helper.h" @@ -66,10 +67,10 @@ void TestWord2vecPrediction(const std::string& model_path) { // One single batch - int64_t data[4] = {1, 2, 3, 4}; + std::array data = {1, 2, 3, 4}; PaddleTensor tensor; tensor.shape = std::vector({4, 1}); - tensor.data = PaddleBuf(data, sizeof(data)); + tensor.data = PaddleBuf(data.data(), sizeof(data)); tensor.dtype = PaddleDType::INT64; // For simplicity, we set all the slots with the same data. @@ -87,7 +88,7 @@ void TestWord2vecPrediction(const std::string& model_path) { platform::errors::PreconditionNotMet( "Output's data length should be 33168 but got %d", outputs.front().data.length())); - float result[5] = { + std::array result = { 0.00129761, 0.00151112, 0.000423564, 0.00108815, 0.000932706}; const size_t num_elements = outputs.front().data.length() / sizeof(float); // The outputs' buffers are in CPU memory. diff --git a/test/cpp/ir/core/ir_infershape_test.cc b/test/cpp/ir/core/ir_infershape_test.cc index 36121cfef75..b3877bf8604 100644 --- a/test/cpp/ir/core/ir_infershape_test.cc +++ b/test/cpp/ir/core/ir_infershape_test.cc @@ -44,7 +44,7 @@ class OperationTest using Op::Op; static const char *name() { return "test.operation2"; } static constexpr uint32_t attributes_num = 2; - static const char *attributes_name[attributes_num]; + static const char *attributes_name[attributes_num]; // NOLINT static void Verify() {} static void InferMeta(phi::InferMetaContext *infer_meta) { auto fn = PD_INFER_META(phi::CreateInferMeta); @@ -54,8 +54,9 @@ class OperationTest IR_DECLARE_EXPLICIT_TYPE_ID(OperationTest) IR_DEFINE_EXPLICIT_TYPE_ID(OperationTest) -const char *OperationTest::attributes_name[attributes_num] = {"op2_attr1", - "op2_attr2"}; +const char *OperationTest::attributes_name[attributes_num] = { // NOLINT + "op2_attr1", + "op2_attr2"}; // Define a dialect, op1 and op2 will be registered by this dialect. class TestDialect : public ir::Dialect { diff --git a/test/cpp/ir/core/ir_op_test.cc b/test/cpp/ir/core/ir_op_test.cc index 0f530e41b8a..16cfc01730d 100644 --- a/test/cpp/ir/core/ir_op_test.cc +++ b/test/cpp/ir/core/ir_op_test.cc @@ -89,7 +89,7 @@ class Operation1 : public ir::Op { using Op::Op; static const char *name() { return "test.operation1"; } static constexpr uint32_t attributes_num = 2; - static const char *attributes_name[attributes_num]; + static const char *attributes_name[attributes_num]; // NOLINT void Verify() { auto &attributes = this->attributes(); if (attributes.count("op1_attr1") == 0 || @@ -114,8 +114,9 @@ class Operation1 : public ir::Op { argument.AddAttributes(attributes.begin(), attributes.end()); } }; -const char *Operation1::attributes_name[attributes_num] = {"op1_attr1", - "op1_attr2"}; +const char *Operation1::attributes_name[attributes_num] = { // NOLINT + "op1_attr1", + "op1_attr2"}; IR_DECLARE_EXPLICIT_TYPE_ID(Operation1) IR_DEFINE_EXPLICIT_TYPE_ID(Operation1) @@ -127,7 +128,7 @@ class Operation2 using Op::Op; static const char *name() { return "test.operation2"; } static constexpr uint32_t attributes_num = 2; - static const char *attributes_name[attributes_num]; + static const char *attributes_name[attributes_num]; // NOLINT void Verify() { auto &attributes = this->attributes(); if (attributes.count("op2_attr1") == 0 || @@ -141,8 +142,9 @@ class Operation2 } static void InferShape() { VLOG(2) << "This is op2's InferShape interface."; } }; -const char *Operation2::attributes_name[attributes_num] = {"op2_attr1", - "op2_attr2"}; +const char *Operation2::attributes_name[attributes_num] = { // NOLINT + "op2_attr1", + "op2_attr2"}; IR_DECLARE_EXPLICIT_TYPE_ID(Operation2) IR_DEFINE_EXPLICIT_TYPE_ID(Operation2) diff --git a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc index 1a9714b0339..977afcc8563 100644 --- a/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc +++ b/test/cpp/ir/pattern_rewrite/pattern_rewrite_test.cc @@ -77,7 +77,7 @@ class Operation1 : public ir::Op { using Op::Op; static const char *name() { return "test.Operation1"; } static constexpr uint32_t attributes_num = 2; - static const char *attributes_name[attributes_num]; + static const char *attributes_name[attributes_num]; // NOLINT void Verify(); static void InferShape() { VLOG(2) << "This is op2's InferShape interface."; } }; @@ -93,8 +93,9 @@ void Operation1::Verify() { throw("Type of attribute: parameter_name is not right."); } } -const char *Operation1::attributes_name[attributes_num] = {"op2_attr1", - "op2_attr2"}; +const char *Operation1::attributes_name[attributes_num] = { // NOLINT + "op2_attr1", + "op2_attr2"}; IR_DECLARE_EXPLICIT_TYPE_ID(Operation1) IR_DEFINE_EXPLICIT_TYPE_ID(Operation1) @@ -358,7 +359,7 @@ class Conv2dFusionOpTest : public ir::Op inputs = { diff --git a/test/cpp/phi/api/test_from_blob.cc b/test/cpp/phi/api/test_from_blob.cc index f73f238a4d1..9c6a3bea23f 100644 --- a/test/cpp/phi/api/test_from_blob.cc +++ b/test/cpp/phi/api/test_from_blob.cc @@ -40,7 +40,7 @@ phi::Place GetPlaceFromPtr(void* data); TEST(from_blob, CPU) { // 1. create data - int64_t data[] = {4, 3, 2, 1}; + int64_t data[] = {4, 3, 2, 1}; // NOLINT ASSERT_EQ(paddle::GetPlaceFromPtr(data), phi::CPUPlace()); diff --git a/test/cpp/phi/api/test_strings_lower_upper_api.cc b/test/cpp/phi/api/test_strings_lower_upper_api.cc index f81ab9dba94..67b723a62cc 100644 --- a/test/cpp/phi/api/test_strings_lower_upper_api.cc +++ b/test/cpp/phi/api/test_strings_lower_upper_api.cc @@ -47,13 +47,14 @@ TEST(API, case_convert) { pstring* cpu_strings_x_data = dev_ctx->template Alloc(cpu_strings_x.get()); - std::string strs[] = {"A Short Pstring.", + std::string strs[] = {"A Short Pstring.", // NOLINT "A Large Pstring Whose Length Is Longer Than 22."}; for (int i = 0; i < 2; ++i) { cpu_strings_x_data[i] = strs[i]; } // 2. get expected results - std::string expected_results[] = {strs[0], strs[0], strs[1], strs[1]}; + std::string expected_results[] = { + strs[0], strs[0], strs[1], strs[1]}; // NOLINT std::transform( strs[0].begin(), strs[0].end(), expected_results[0].begin(), ::tolower); std::transform( @@ -77,7 +78,8 @@ TEST(API, case_convert) { auto lower_tensor_ptr = lower_tensor->data(); auto upper_tensor_ptr = upper_tensor->data(); - const std::string cpu_results[] = {lower_tensor_ptr[0].data(), + const std::string cpu_results[] = {// NOLINT + lower_tensor_ptr[0].data(), upper_tensor_ptr[0].data(), lower_tensor_ptr[1].data(), upper_tensor_ptr[1].data()}; @@ -101,12 +103,14 @@ TEST(API, case_convert_utf8) { pstring* cpu_strings_x_data = dev_ctx->template Alloc(cpu_strings_x.get()); - std::string strs[] = {"óÓsscHloëË", "óÓsscHloëËóÓsscHloëËóÓsscHloëË"}; + std::string strs[] = {"óÓsscHloëË", + "óÓsscHloëËóÓsscHloëËóÓsscHloëË"}; // NOLINT for (int i = 0; i < 2; ++i) { cpu_strings_x_data[i] = strs[i]; } // 2. get expected results - std::string expected_results[] = {"óósschloëë", + std::string expected_results[] = {// NOLINT + "óósschloëë", "ÓÓSSCHLOËË", "óósschloëëóósschloëëóósschloëë", "ÓÓSSCHLOËËÓÓSSCHLOËËÓÓSSCHLOËË"}; @@ -125,7 +129,8 @@ TEST(API, case_convert_utf8) { auto lower_tensor_ptr = lower_tensor->data(); auto upper_tensor_ptr = upper_tensor->data(); - const char* cpu_results[] = {lower_tensor_ptr[0].data(), + const char* cpu_results[] = {// NOLINT + lower_tensor_ptr[0].data(), upper_tensor_ptr[0].data(), lower_tensor_ptr[1].data(), upper_tensor_ptr[1].data()}; diff --git a/test/cpp/phi/core/unroll_array_ops_test.cc b/test/cpp/phi/core/unroll_array_ops_test.cc index 03a4beb374e..ddcf48844a7 100644 --- a/test/cpp/phi/core/unroll_array_ops_test.cc +++ b/test/cpp/phi/core/unroll_array_ops_test.cc @@ -45,8 +45,8 @@ TEST(unroll_ops, fill_constant) { } TEST(unroll_ops, assign) { - const int a[] = {1, 2, 3, 4, 5}; - int b[] = {0, 0, 0, 0, 0}; + const int a[] = {1, 2, 3, 4, 5}; // NOLINT + int b[] = {0, 0, 0, 0, 0}; // NOLINT UnrollAssign<3>::Run(a, b); EXPECT_EQ(b[0], 1); EXPECT_EQ(b[1], 2); @@ -56,7 +56,7 @@ TEST(unroll_ops, assign) { } TEST(unroll_ops, var_args_assign) { - int a[] = {0, 0, 0}; + int a[] = {0, 0, 0}; // NOLINT UnrollVarArgsAssign::Run(a, 1, 2); EXPECT_EQ(a[0], 1); EXPECT_EQ(a[1], 2); @@ -64,8 +64,8 @@ TEST(unroll_ops, var_args_assign) { } TEST(unroll_ops, compare) { - int a[] = {1, 2, 3}; - int b[] = {1, 2, 4}; + int a[] = {1, 2, 3}; // NOLINT + int b[] = {1, 2, 4}; // NOLINT EXPECT_TRUE(UnrollCompare<2>::Run(a, b)); EXPECT_FALSE(UnrollCompare<3>::Run(a, b)); @@ -75,7 +75,7 @@ TEST(unroll_ops, compare) { } TEST(unroll_ops, product) { - int a[] = {2, 3, 4}; + int a[] = {2, 3, 4}; // NOLINT EXPECT_EQ(UnrollProduct<3>::Run(a), a[0] * a[1] * a[2]); } diff --git a/test/cpp/phi/kernels/strided_memcpy_test.cc b/test/cpp/phi/kernels/strided_memcpy_test.cc index 0fe0d6f4801..9bd893bcd10 100644 --- a/test/cpp/phi/kernels/strided_memcpy_test.cc +++ b/test/cpp/phi/kernels/strided_memcpy_test.cc @@ -13,6 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/kernels/funcs/strided_memcpy.h" +#include #include "gtest/gtest.h" #include "paddle/phi/backends/context_pool.h" @@ -22,7 +23,7 @@ namespace tests { TEST(StridedMemcpy, CPUCrop) { // clang-format off - int src[] = { + int src[] = {// NOLINT 0, 1, 2, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 0, 0, @@ -31,13 +32,13 @@ TEST(StridedMemcpy, CPUCrop) { phi::DDim src_stride({5, 1}); - int dst[4]; + std::array dst; phi::DDim dst_dim({2, 2}); phi::DDim dst_stride({2, 1}); phi::CPUContext ctx; phi::funcs::StridedMemcpy( - ctx, src + 1, src_stride, dst_dim, dst_stride, dst); + ctx, src + 1, src_stride, dst_dim, dst_stride, dst.data()); ASSERT_EQ(1, dst[0]); ASSERT_EQ(2, dst[1]); @@ -47,26 +48,25 @@ TEST(StridedMemcpy, CPUCrop) { TEST(StridedMemcpy, CPUConcat) { // clang-format off - int src[] = { + int src[] = { // NOLINT 1, 2, 3, 4 }; // clang-format on - int dst[8]; - + std::array dst; phi::DDim src_stride({2, 1}); phi::DDim dst_dim({2, 2}); phi::DDim dst_stride({4, 1}); phi::CPUContext ctx; phi::funcs::StridedMemcpy( - ctx, src, src_stride, dst_dim, dst_stride, dst); + ctx, src, src_stride, dst_dim, dst_stride, dst.data()); phi::funcs::StridedMemcpy( - ctx, src, src_stride, dst_dim, dst_stride, dst + 2); + ctx, src, src_stride, dst_dim, dst_stride, dst.data() + 2); // clang-format off - int expect_dst[] = { + int expect_dst[] = { // NOLINT 1, 2, 1, 2, 3, 4, 3, 4 }; diff --git a/test/cpp/phi/kernels/test_math_function.cc b/test/cpp/phi/kernels/test_math_function.cc index 094dfccb5a3..3f1befb57f5 100644 --- a/test/cpp/phi/kernels/test_math_function.cc +++ b/test/cpp/phi/kernels/test_math_function.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include "gtest/gtest.h" @@ -41,16 +42,16 @@ TEST(math_function, gemm_notrans_cblas) { input1.Resize({2, 3}); float* input1_ptr = dev_ctx->template Alloc(&input1); - float arr1[6] = {0, 1, 2, 3, 4, 5}; - memcpy(input1_ptr, arr1, 6 * sizeof(float)); + std::array arr1 = {0, 1, 2, 3, 4, 5}; + memcpy(input1_ptr, arr1.data(), 6 * sizeof(float)); input2.Resize({3, 4}); float* input2_ptr = dev_ctx->template Alloc(&input2); - float arr2[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; - memcpy(input2_ptr, arr2, 12 * sizeof(float)); + std::array arr2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; + memcpy(input2_ptr, arr2.data(), 12 * sizeof(float)); input3.Resize({2, 4}); float* input3_ptr = dev_ctx->template Alloc(&input3); - float arr3[8] = {0, 1, 2, 3, 4, 5, 6, 7}; - memcpy(input3_ptr, arr3, 8 * sizeof(float)); + std::array arr3 = {0, 1, 2, 3, 4, 5, 6, 7}; + memcpy(input3_ptr, arr3.data(), 8 * sizeof(float)); GetBlas(*dev_ctx).GEMM(false, false, @@ -172,16 +173,16 @@ TEST(math_function, gemm_trans_cblas) { input1.Resize({2, 3}); float* input1_ptr = dev_ctx->template Alloc(&input1); - float arr1[6] = {0, 1, 2, 3, 4, 5}; - memcpy(input1_ptr, arr1, 6 * sizeof(float)); + std::array arr1 = {0, 1, 2, 3, 4, 5}; + memcpy(input1_ptr, arr1.data(), 6 * sizeof(float)); input2.Resize({4, 3}); float* input2_ptr = dev_ctx->template Alloc(&input2); - float arr2[12] = {0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11}; - memcpy(input2_ptr, arr2, 12 * sizeof(float)); + std::array arr2 = {0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11}; + memcpy(input2_ptr, arr2.data(), 12 * sizeof(float)); input3.Resize({2, 4}); float* input3_ptr = dev_ctx->template Alloc(&input3); - float arr3[8] = {0, 1, 2, 3, 4, 5, 6, 7}; - memcpy(input3_ptr, arr3, 8 * sizeof(float)); + std::array arr3 = {0, 1, 2, 3, 4, 5, 6, 7}; + memcpy(input3_ptr, arr3.data(), 8 * sizeof(float)); GetBlas(*dev_ctx).GEMM(false, true, diff --git a/test/cpp/phi/kernels/test_strings_copy_dev_api.cc b/test/cpp/phi/kernels/test_strings_copy_dev_api.cc index d704039a243..0aeacc71da9 100644 --- a/test/cpp/phi/kernels/test_strings_copy_dev_api.cc +++ b/test/cpp/phi/kernels/test_strings_copy_dev_api.cc @@ -46,7 +46,7 @@ TEST(DEV_API, strings_copy) { StringTensor string_dst(alloc, meta); // 2. Assign input text - const char* input[] = {"A Short Pstring.", + const char* input[] = {"A Short Pstring.", // NOLINT "A Large Pstring Whose Length Is Longer Than 22.", "abc", "defg", diff --git a/test/cpp/phi/kernels/test_strings_lower_upper_dev_api.cc b/test/cpp/phi/kernels/test_strings_lower_upper_dev_api.cc index 2a7aa5cdaf7..5f0cc69df62 100644 --- a/test/cpp/phi/kernels/test_strings_lower_upper_dev_api.cc +++ b/test/cpp/phi/kernels/test_strings_lower_upper_dev_api.cc @@ -56,7 +56,8 @@ TEST(DEV_API, strings_cast_convert) { dense_x_data[1] = long_str; // 2. get expected results - std::string expected_results[] = {short_str, short_str, long_str, long_str}; + std::string expected_results[] = { + short_str, short_str, long_str, long_str}; // NOLINT std::transform(short_str.begin(), short_str.end(), expected_results[0].begin(), @@ -107,7 +108,8 @@ TEST(DEV_API, strings_cast_convert_utf8) { dense_x_data[0] = utf8_str; // 2. get expected results - std::string expected_results[] = {"óósschloëëóósschloëëóósschloëë", + std::string expected_results[] = {// NOLINT + "óósschloëëóósschloëëóósschloëë", "ÓÓSSCHLOËËÓÓSSCHLOËËÓÓSSCHLOËË"}; // 3. test API, ascii encoding -- GitLab