@@ -57,32 +57,50 @@ def calculate_np(name,
57
57
"""Calculate the optimum number of processes for the given OP"""
58
58
eps = 1e-9 # about 1 byte
59
59
60
- if num_proc is None :
61
- num_proc = psutil .cpu_count ()
62
-
63
60
if use_cuda :
61
+ auto_num_proc = None
64
62
cuda_mem_available = get_min_cuda_memory () / 1024
65
- op_proc = min (
66
- num_proc ,
67
- math .floor (cuda_mem_available / (mem_required + eps )) *
68
- cuda_device_count ())
69
- if use_cuda and mem_required == 0 :
63
+ if mem_required == 0 :
70
64
logger .warning (f'The required cuda memory of Op[{ name } ] '
71
65
f'has not been specified. '
72
66
f'Please specify the mem_required field in the '
73
67
f'config file, or you might encounter CUDA '
74
68
f'out of memory error. You can reference '
75
69
f'the mem_required field in the '
76
70
f'config_all.yaml file.' )
77
- if op_proc < 1.0 :
78
- logger .warning (f'The required cuda memory:{ mem_required } GB might '
79
- f'be more than the available cuda memory:'
80
- f'{ cuda_mem_available } GB.'
81
- f'This Op[{ name } ] might '
82
- f'require more resource to run.' )
71
+ else :
72
+ auto_num_proc = math .floor (
73
+ cuda_mem_available / mem_required ) * cuda_device_count ()
74
+ if cuda_mem_available / mem_required < 1.0 :
75
+ logger .warning (
76
+ f'The required cuda memory:{ mem_required } GB might '
77
+ f'be more than the available cuda memory:'
78
+ f'{ cuda_mem_available } GB.'
79
+ f'This Op[{ name } ] might '
80
+ f'require more resource to run.' )
81
+
82
+ if auto_num_proc and num_proc :
83
+ op_proc = min (auto_num_proc , num_proc )
84
+ if num_proc > auto_num_proc :
85
+ logger .warning (
86
+ f'The given num_proc: { num_proc } is greater than '
87
+ f'the value { auto_num_proc } auto calculated based '
88
+ f'on the mem_required of Op[{ name } ]. '
89
+ f'Set the `num_proc` to { auto_num_proc } .' )
90
+ elif not auto_num_proc and not num_proc :
91
+ op_proc = cuda_device_count ()
92
+ logger .warning (
93
+ f'Both mem_required and num_proc of Op[{ name } ] are not set.'
94
+ f'Set the `num_proc` to { op_proc } .' )
95
+ else :
96
+ op_proc = auto_num_proc if auto_num_proc else num_proc
97
+
83
98
op_proc = max (op_proc , 1 )
84
99
return op_proc
85
100
else :
101
+ if num_proc is None :
102
+ num_proc = psutil .cpu_count ()
103
+
86
104
op_proc = num_proc
87
105
cpu_available = psutil .cpu_count ()
88
106
mem_available = psutil .virtual_memory ().available
0 commit comments