提交 e28157d7 编写于 作者: T tensor-tang 提交者: QI JUN

fix v2 init issue on Mac (#5808)

* fix v2 init issue on Mac

* refine the v2 init
上级 67bd4cd6
...@@ -62,21 +62,15 @@ __all__ = [ ...@@ -62,21 +62,15 @@ __all__ = [
cp.begin_parse() cp.begin_parse()
def init(**kwargs): def set_omp_mkl_env_vars(trainer_count):
import py_paddle.swig_paddle as api '''Auto set CPU environment if have not set before.
args = [] export KMP_AFFINITY, OMP_DYNAMIC according to the Hyper Threading status.
args_dict = {} export OMP_NUM_THREADS, MKL_NUM_THREADS according to trainer_count.
# NOTE: append arguments if they are in ENV '''
for ek, ev in os.environ.iteritems(): import platform
if ek.startswith("PADDLE_INIT_"): if not platform.system() in ['Linux', 'Darwin']:
args_dict[ek.replace("PADDLE_INIT_", "").lower()] = str(ev) return
args_dict.update(kwargs)
# NOTE: overwrite arguments from ENV if it is in kwargs
for key in args_dict.keys():
args.append('--%s=%s' % (key, str(args_dict[key])))
# auto set cpu environment
def set_env(key, value): def set_env(key, value):
'''If the key has not been set in the environment, set it with value.''' '''If the key has not been set in the environment, set it with value.'''
assert isinstance(key, str) assert isinstance(key, str)
...@@ -85,22 +79,59 @@ def init(**kwargs): ...@@ -85,22 +79,59 @@ def init(**kwargs):
if envset is None: if envset is None:
os.environ[key] = value os.environ[key] = value
ht = os.popen("lscpu |grep \"per core\"|awk -F':' '{print $2}'|xargs") def num_physical_cores():
ht = int(ht.read()) '''Get the number of physical cores'''
if ht == 1: # ht is off if platform.system() == "Linux":
set_env("OMP_DYNAMIC", "false") num_sockets = int(
set_env("KMP_AFFINITY", "granularity=fine,compact,0,0") os.popen("lscpu |grep \"Socket\" |awk -F':' '{print $2}'|xargs")
else: .read())
num_cores_per_socket = int(
os.popen(
"lscpu |grep \"per socket\" |awk -F':' '{print $2}'|xargs")
.read())
return num_sockets * num_cores_per_socket
else:
cmds = {"Darwin": "sysctl hw.physicalcpu"}
return int(os.popen(cmds.get(platform.system(), "expr 1")).read())
def num_logical_processors():
'''Get the number of logical processors'''
cmds = {
"Linux": "grep \"processor\" /proc/cpuinfo|sort -u|wc -l",
"Darwin": "sysctl hw.logicalcpu"
}
return int(os.popen(cmds.get(platform.system(), "expr 1")).read())
num_cores = num_physical_cores()
num_processors = num_logical_processors()
if num_processors > num_cores: # Hyper Threading is enabled
set_env("OMP_DYNAMIC", "true") set_env("OMP_DYNAMIC", "true")
set_env("KMP_AFFINITY", "granularity=fine,compact,1,0") set_env("KMP_AFFINITY", "granularity=fine,compact,1,0")
processors = os.popen("grep \"processor\" /proc/cpuinfo|sort -u|wc -l") else:
processors = int(processors.read()) set_env("OMP_DYNAMIC", "false")
trainers = kwargs.get('trainer_count', 1) set_env("KMP_AFFINITY", "granularity=fine,compact,0,0")
threads = processors / trainers threads = num_processors / trainer_count
threads = '1' if threads < 1 else str(threads) threads = '1' if threads < 1 else str(threads)
set_env("OMP_NUM_THREADS", threads) set_env("OMP_NUM_THREADS", threads)
set_env("MKL_NUM_THREADS", threads) set_env("MKL_NUM_THREADS", threads)
def init(**kwargs):
import py_paddle.swig_paddle as api
args = []
args_dict = {}
# NOTE: append arguments if they are in ENV
for ek, ev in os.environ.iteritems():
if ek.startswith("PADDLE_INIT_"):
args_dict[ek.replace("PADDLE_INIT_", "").lower()] = str(ev)
args_dict.update(kwargs)
# NOTE: overwrite arguments from ENV if it is in kwargs
for key in args_dict.keys():
args.append('--%s=%s' % (key, str(args_dict[key])))
set_omp_mkl_env_vars(kwargs.get('trainer_count', 1))
if 'use_gpu' in kwargs: if 'use_gpu' in kwargs:
cp.g_command_config_args['use_gpu'] = kwargs['use_gpu'] cp.g_command_config_args['use_gpu'] = kwargs['use_gpu']
if 'use_mkldnn' in kwargs: if 'use_mkldnn' in kwargs:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册