Skip to content

Commit 00e5c4b

Browse files
sneaxiyAnnaTrainingG
authored andcommitted
Make flag adding easier (PaddlePaddle#35823)
* make flag setter easier * update * rename macro name * fix bug of public/writable * update to pass CI * polish * fix CPU link error
1 parent 5afb181 commit 00e5c4b

19 files changed

+387
-354
lines changed

paddle/fluid/imperative/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cc_library(amp SRCS amp_auto_cast.cc DEPS layer )
1111
cc_library(tracer SRCS tracer.cc DEPS layer engine program_desc_tracer amp denormal)
1212
cc_library(basic_engine SRCS basic_engine.cc DEPS layer gradient_accumulator)
1313
cc_library(engine SRCS basic_engine.cc partial_grad_engine.cc DEPS layer gradient_accumulator)
14-
cc_library(imperative_profiler SRCS profiler.cc)
14+
cc_library(imperative_profiler SRCS profiler.cc DEPS flags)
1515
if(NOT WIN32)
1616
if(WITH_NCCL OR WITH_RCCL)
1717
cc_library(imperative_all_reduce SRCS all_reduce.cc DEPS collective_helper device_context selected_rows tensor)

paddle/fluid/imperative/flags.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
// limitations under the License.
1414

1515
#include "paddle/fluid/imperative/flags.h"
16-
#include "gflags/gflags.h"
16+
#include "paddle/fluid/platform/flags.h"
1717

18-
DEFINE_uint64(dygraph_debug, 0,
19-
"Debug level of dygraph. This flag is not "
20-
"open to users");
18+
PADDLE_DEFINE_EXPORTED_uint64(dygraph_debug, 0,
19+
"Debug level of dygraph. This flag is not "
20+
"open to users");
2121

2222
namespace paddle {
2323
namespace imperative {

paddle/fluid/imperative/profiler.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#endif
2020
#include <glog/logging.h>
2121
#include <mutex> // NOLINT
22-
#include "gflags/gflags.h"
22+
#include "paddle/fluid/platform/flags.h"
2323

24-
DEFINE_string(
24+
PADDLE_DEFINE_EXPORTED_string(
2525
tracer_profile_fname, "xxgperf",
2626
"Profiler filename for imperative tracer, which generated by gperftools."
2727
"Only valid when compiled `WITH_PROFILER=ON`. Empty if disable.");

paddle/fluid/memory/allocation/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ cc_test(allocator_facade_abs_flags_test SRCS allocator_facade_abs_flags_test.cc
9999

100100
cc_test(allocator_facade_frac_flags_test SRCS allocator_facade_frac_flags_test.cc DEPS allocator_facade)
101101

102-
cc_library(auto_growth_best_fit_allocator SRCS auto_growth_best_fit_allocator.cc DEPS allocator aligned_allocator)
102+
cc_library(auto_growth_best_fit_allocator SRCS auto_growth_best_fit_allocator.cc DEPS allocator aligned_allocator flags)
103103
cc_test(auto_growth_best_fit_allocator_facade_test SRCS auto_growth_best_fit_allocator_facade_test.cc DEPS cpu_allocator auto_growth_best_fit_allocator)
104104
cc_test(auto_growth_best_fit_allocator_test SRCS auto_growth_best_fit_allocator_test.cc DEPS auto_growth_best_fit_allocator)
105105

paddle/fluid/memory/allocation/allocator_facade.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@
3737
#endif
3838
#include "paddle/fluid/platform/npu_info.h"
3939

40-
DEFINE_int64(
40+
PADDLE_DEFINE_EXPORTED_int64(
4141
gpu_allocator_retry_time, 10000,
4242
"The retry time (milliseconds) when allocator fails "
4343
"to allocate memory. No retry if this value is not greater than 0");
4444

45-
DEFINE_bool(use_system_allocator, false,
46-
"Whether to use system allocator to allocate CPU and GPU memory. "
47-
"Only used for unittests.");
45+
PADDLE_DEFINE_EXPORTED_bool(
46+
use_system_allocator, false,
47+
"Whether to use system allocator to allocate CPU and GPU memory. "
48+
"Only used for unittests.");
4849

4950
namespace paddle {
5051
namespace memory {

paddle/fluid/memory/allocation/auto_growth_best_fit_allocator.cc

+15-12
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717
#include <algorithm>
1818
#include <mutex> // NOLINT
1919
#include "paddle/fluid/memory/allocation/aligned_allocator.h"
20-
21-
DEFINE_bool(free_idle_chunk, false,
22-
"Whether to free idle chunk when each allocation is freed. "
23-
"If false, all freed allocation would be cached to speed up next "
24-
"allocation request. If true, no allocation would be cached. This "
25-
"flag only works when FLAGS_allocator_strategy=auto_growth.");
26-
27-
DEFINE_bool(free_when_no_cache_hit, false,
28-
"Whether to free idle chunks when no cache hit. If true, idle "
29-
"chunk would be freed when no cache hit; if false, idle "
30-
"chunk would be freed when out of memory occurs. This flag "
31-
"only works when FLAGS_allocator_strategy=auto_growth.");
20+
#include "paddle/fluid/platform/flags.h"
21+
22+
PADDLE_DEFINE_EXPORTED_READONLY_bool(
23+
free_idle_chunk, false,
24+
"Whether to free idle chunk when each allocation is freed. "
25+
"If false, all freed allocation would be cached to speed up next "
26+
"allocation request. If true, no allocation would be cached. This "
27+
"flag only works when FLAGS_allocator_strategy=auto_growth.");
28+
29+
PADDLE_DEFINE_EXPORTED_READONLY_bool(
30+
free_when_no_cache_hit, false,
31+
"Whether to free idle chunks when no cache hit. If true, idle "
32+
"chunk would be freed when no cache hit; if false, idle "
33+
"chunk would be freed when out of memory occurs. This flag "
34+
"only works when FLAGS_allocator_strategy=auto_growth.");
3235

3336
namespace paddle {
3437
namespace memory {

paddle/fluid/memory/allocation/naive_best_fit_allocator.cc

+7-6
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@
3434
#include "paddle/fluid/platform/xpu/xpu_header.h"
3535
#endif
3636

37-
DEFINE_bool(init_allocated_mem, false,
38-
"It is a mistake that the values of the memory allocated by "
39-
"BuddyAllocator are always zeroed in some op's implementation. "
40-
"To find this error in time, we use init_allocated_mem to indicate "
41-
"that initializing the allocated memory with a small value "
42-
"during unit testing.");
37+
PADDLE_DEFINE_EXPORTED_bool(
38+
init_allocated_mem, false,
39+
"It is a mistake that the values of the memory allocated by "
40+
"BuddyAllocator are always zeroed in some op's implementation. "
41+
"To find this error in time, we use init_allocated_mem to indicate "
42+
"that initializing the allocated memory with a small value "
43+
"during unit testing.");
4344
DECLARE_double(fraction_of_gpu_memory_to_use);
4445
DECLARE_uint64(initial_gpu_memory_in_mb);
4546
DECLARE_uint64(reallocate_gpu_memory_in_mb);

paddle/fluid/operators/pscore/heter_listen_and_serv_op.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ limitations under the License. */
1515
#include "paddle/fluid/operators/pscore/heter_listen_and_serv_op.h"
1616
#include "paddle/fluid/framework/op_registry.h"
1717

18-
DEFINE_int32(rpc_send_thread_num, 12, "number of threads for rpc send");
18+
PADDLE_DEFINE_EXPORTED_int32(rpc_send_thread_num, 12,
19+
"number of threads for rpc send");
1920

2021
namespace paddle {
2122
namespace operators {

paddle/fluid/platform/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ if (WITH_PYTHON)
3737
endif(NOT WIN32)
3838
endif()
3939

40-
cc_library(flags SRCS flags.cc DEPS gflags)
40+
cc_library(flags SRCS flags.cc DEPS gflags boost)
4141
cc_library(denormal SRCS denormal.cc DEPS)
4242

4343
cc_library(errors SRCS errors.cc DEPS error_codes_proto)
4444
cc_test(errors_test SRCS errors_test.cc DEPS errors enforce)
4545

46-
set(enforce_deps flags errors boost)
46+
set(enforce_deps flags errors boost flags)
4747
if(WITH_GPU)
4848
set(enforce_deps ${enforce_deps} external_error_proto)
4949
endif()

paddle/fluid/platform/cpu_info.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ limitations under the License. */
3131
#endif // _WIN32
3232

3333
#include <algorithm>
34-
#include "gflags/gflags.h"
34+
#include "paddle/fluid/platform/flags.h"
3535

3636
DECLARE_double(fraction_of_cpu_memory_to_use);
3737
DECLARE_uint64(initial_cpu_memory_in_mb);
@@ -42,7 +42,8 @@ DECLARE_double(fraction_of_cuda_pinned_memory_to_use);
4242
// between host and device. Allocates too much would reduce the amount
4343
// of memory available to the system for paging. So, by default, we
4444
// should set false to use_pinned_memory.
45-
DEFINE_bool(use_pinned_memory, true, "If set, allocate cpu pinned memory.");
45+
PADDLE_DEFINE_EXPORTED_bool(use_pinned_memory, true,
46+
"If set, allocate cpu pinned memory.");
4647

4748
namespace paddle {
4849
namespace platform {
@@ -54,7 +55,9 @@ size_t CpuTotalPhysicalMemory() {
5455
mib[1] = HW_MEMSIZE;
5556
int64_t size = 0;
5657
size_t len = sizeof(size);
57-
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) return (size_t)size;
58+
if (sysctl(mib, 2, &size, &len, NULL, 0) == 0) {
59+
return static_cast<size_t>(size);
60+
}
5861
return 0L;
5962
#elif defined(_WIN32)
6063
MEMORYSTATUSEX sMeminfo;

paddle/fluid/platform/enforce.h

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ limitations under the License. */
101101
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
102102
#include "paddle/fluid/platform/type_defs.h"
103103
#endif
104+
#include "paddle/fluid/platform/flags.h"
104105

105106
namespace paddle {
106107
namespace platform {

0 commit comments

Comments
 (0)