Skip to content

Commit e28157d

Browse files
tensor-tangQiJune
authored andcommitted
fix v2 init issue on Mac (#5808)
* fix v2 init issue on Mac * refine the v2 init
1 parent 67bd4cd commit e28157d

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

python/paddle/v2/__init__.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,15 @@
6262
cp.begin_parse()
6363

6464

65-
def init(**kwargs):
66-
import py_paddle.swig_paddle as api
67-
args = []
68-
args_dict = {}
69-
# NOTE: append arguments if they are in ENV
70-
for ek, ev in os.environ.iteritems():
71-
if ek.startswith("PADDLE_INIT_"):
72-
args_dict[ek.replace("PADDLE_INIT_", "").lower()] = str(ev)
65+
def set_omp_mkl_env_vars(trainer_count):
66+
'''Auto set CPU environment if have not set before.
67+
export KMP_AFFINITY, OMP_DYNAMIC according to the Hyper Threading status.
68+
export OMP_NUM_THREADS, MKL_NUM_THREADS according to trainer_count.
69+
'''
70+
import platform
71+
if not platform.system() in ['Linux', 'Darwin']:
72+
return
7373

74-
args_dict.update(kwargs)
75-
# NOTE: overwrite arguments from ENV if it is in kwargs
76-
for key in args_dict.keys():
77-
args.append('--%s=%s' % (key, str(args_dict[key])))
78-
79-
# auto set cpu environment
8074
def set_env(key, value):
8175
'''If the key has not been set in the environment, set it with value.'''
8276
assert isinstance(key, str)
@@ -85,22 +79,59 @@ def set_env(key, value):
8579
if envset is None:
8680
os.environ[key] = value
8781

88-
ht = os.popen("lscpu |grep \"per core\"|awk -F':' '{print $2}'|xargs")
89-
ht = int(ht.read())
90-
if ht == 1: # ht is off
91-
set_env("OMP_DYNAMIC", "false")
92-
set_env("KMP_AFFINITY", "granularity=fine,compact,0,0")
93-
else:
82+
def num_physical_cores():
83+
'''Get the number of physical cores'''
84+
if platform.system() == "Linux":
85+
num_sockets = int(
86+
os.popen("lscpu |grep \"Socket\" |awk -F':' '{print $2}'|xargs")
87+
.read())
88+
num_cores_per_socket = int(
89+
os.popen(
90+
"lscpu |grep \"per socket\" |awk -F':' '{print $2}'|xargs")
91+
.read())
92+
return num_sockets * num_cores_per_socket
93+
else:
94+
cmds = {"Darwin": "sysctl hw.physicalcpu"}
95+
return int(os.popen(cmds.get(platform.system(), "expr 1")).read())
96+
97+
def num_logical_processors():
98+
'''Get the number of logical processors'''
99+
cmds = {
100+
"Linux": "grep \"processor\" /proc/cpuinfo|sort -u|wc -l",
101+
"Darwin": "sysctl hw.logicalcpu"
102+
}
103+
return int(os.popen(cmds.get(platform.system(), "expr 1")).read())
104+
105+
num_cores = num_physical_cores()
106+
num_processors = num_logical_processors()
107+
if num_processors > num_cores: # Hyper Threading is enabled
94108
set_env("OMP_DYNAMIC", "true")
95109
set_env("KMP_AFFINITY", "granularity=fine,compact,1,0")
96-
processors = os.popen("grep \"processor\" /proc/cpuinfo|sort -u|wc -l")
97-
processors = int(processors.read())
98-
trainers = kwargs.get('trainer_count', 1)
99-
threads = processors / trainers
110+
else:
111+
set_env("OMP_DYNAMIC", "false")
112+
set_env("KMP_AFFINITY", "granularity=fine,compact,0,0")
113+
threads = num_processors / trainer_count
100114
threads = '1' if threads < 1 else str(threads)
101115
set_env("OMP_NUM_THREADS", threads)
102116
set_env("MKL_NUM_THREADS", threads)
103117

118+
119+
def init(**kwargs):
120+
import py_paddle.swig_paddle as api
121+
args = []
122+
args_dict = {}
123+
# NOTE: append arguments if they are in ENV
124+
for ek, ev in os.environ.iteritems():
125+
if ek.startswith("PADDLE_INIT_"):
126+
args_dict[ek.replace("PADDLE_INIT_", "").lower()] = str(ev)
127+
128+
args_dict.update(kwargs)
129+
# NOTE: overwrite arguments from ENV if it is in kwargs
130+
for key in args_dict.keys():
131+
args.append('--%s=%s' % (key, str(args_dict[key])))
132+
133+
set_omp_mkl_env_vars(kwargs.get('trainer_count', 1))
134+
104135
if 'use_gpu' in kwargs:
105136
cp.g_command_config_args['use_gpu'] = kwargs['use_gpu']
106137
if 'use_mkldnn' in kwargs:

0 commit comments

Comments
 (0)