From 0a47387bd82d6547658a0299a940ef0ca13efdc0 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 7 Aug 2020 11:24:42 +0800 Subject: [PATCH] Use static local variable instead of global variable for safty (#26018) * remove global variable * refine code --- paddle/fluid/framework/unused_var_check.cc | 69 +++++++++++----------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/paddle/fluid/framework/unused_var_check.cc b/paddle/fluid/framework/unused_var_check.cc index eee100bc81c..e7e964b4181 100644 --- a/paddle/fluid/framework/unused_var_check.cc +++ b/paddle/fluid/framework/unused_var_check.cc @@ -28,38 +28,6 @@ DEFINE_bool(enable_unused_var_check, false, "Checking whether operator contains unused inputs, " "especially for grad operator. It should be in unittest."); -// NOTE(zhiqiu): Currently, there are some operators which involves unused -// inputs and cannot be removed from the allow_list below. -// They can be mainly divided into four categories: -// 0: the inputs of which are only used in if branch, or used in cuda kernel but -// not in cpu kernel; -// 1: the inputs of which are used to indicate dtype of outputs; -// 2: the inputs of which are used in fused operators. -// The category number is presented in the comments after each operator. - -const std::unordered_set op_with_unsed_vars_allow_list = { - "batch_norm", // 0 - "batch_norm_grad", // 0 - "sync_batch_norm", // 0 - "sync_batch_norm_grad", // 0 - "inplace_abn", // 0 - "inplace_abn_grad", // 0 - "dgc_momentum", // 0 - "fake_quantize_range_abs_max", // 0 - "rmsprop", // 0 - "sequence_conv_grad", // 0 - "roi_perspective_transform_grad", // 0 - "fill_zeros_like", // 1 - "fill_any_like", // 1 - "nce_grad", // 1 - "precision_recall", // 1 - "fusion_seqpool_cvm_concat", // 2 - "fused_batch_norm_act", // 2 - "fused_batch_norm_act_grad", // 2 - "data_norm", // 0 - "data_norm_grad", // 0 -}; - namespace paddle { namespace framework { @@ -75,9 +43,44 @@ void LogVarUsageIfUnusedVarCheckEnabled(const std::string &name) { } } +static const std::unordered_set &GetOpWithUnusedVarAllowSet() { + // NOTE(zhiqiu): Currently, there are some operators which involves unused + // inputs and cannot be removed from the allow_list below. + // They can be mainly divided into four categories: + // 0: the inputs of which are only used in if branch, or used in cuda kernel + // but not in cpu kernel; 1: the inputs of which are used to indicate dtype of + // outputs; 2: the inputs of which are used in fused operators. The category + // number is presented in the comments after each operator. + // Use pointer here for safe static deinitialization + static auto *allow_set = new std::unordered_set({ + // called once + "batch_norm", // 0 + "batch_norm_grad", // 0 + "sync_batch_norm", // 0 + "sync_batch_norm_grad", // 0 + "inplace_abn", // 0 + "inplace_abn_grad", // 0 + "dgc_momentum", // 0 + "fake_quantize_range_abs_max", // 0 + "rmsprop", // 0 + "sequence_conv_grad", // 0 + "roi_perspective_transform_grad", // 0 + "fill_zeros_like", // 1 + "fill_any_like", // 1 + "nce_grad", // 1 + "precision_recall", // 1 + "fusion_seqpool_cvm_concat", // 2 + "fused_batch_norm_act", // 2 + "fused_batch_norm_act_grad", // 2 + "data_norm", // 0 + "data_norm_grad", // 0); + }); + return *allow_set; +} + void CheckUnusedVar(const OperatorBase &op, const Scope &scope) { // skip op in allow list. - if (op_with_unsed_vars_allow_list.count(op.Type()) != 0) { + if (GetOpWithUnusedVarAllowSet().count(op.Type()) != 0) { return; } auto *used_set = GetThreadLocalUsedVarNameSet(); -- GitLab