-
Couldn't load subscription status.
- Fork 5.9k
fix v2 init issue on Mac #5808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix v2 init issue on Mac #5808
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,21 +62,15 @@ | |
| cp.begin_parse() | ||
|
|
||
|
|
||
| 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) | ||
| def auto_set_cpu_env(trainer_count): | ||
| '''Auto set CPU environment if have not set before. | ||
| export KMP_AFFINITY, OMP_DYNAMIC according to the Hyper Threading status. | ||
| export OMP_NUM_THREADS, MKL_NUM_THREADS according to trainer_count. | ||
| ''' | ||
| import platform | ||
| if platform.system() != "Linux" and platform.system() != "Darwin": | ||
| 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): | ||
| '''If the key has not been set in the environment, set it with value.''' | ||
| assert isinstance(key, str) | ||
|
|
@@ -85,22 +79,63 @@ def set_env(key, value): | |
| if envset is None: | ||
| os.environ[key] = value | ||
|
|
||
| ht = os.popen("lscpu |grep \"per core\"|awk -F':' '{print $2}'|xargs") | ||
| ht = int(ht.read()) | ||
| if ht == 1: # ht is off | ||
| set_env("OMP_DYNAMIC", "false") | ||
| set_env("KMP_AFFINITY", "granularity=fine,compact,0,0") | ||
| else: | ||
| def is_hyperthreading_enabled(): | ||
| '''If Hyper Threading is enabled.''' | ||
| if platform.system() == "Linux": | ||
| percore = os.popen( | ||
| "lscpu |grep \"per core\"|awk -F':' '{print $2}'|xargs") | ||
| return int(percore.read()) != 1 | ||
| elif platform.system() == "Darwin": | ||
| physical = int(os.popen("sysctl hw.physicalcpu").read()) | ||
| logical = int(os.popen("sysctl hw.logicalcpu").read()) | ||
| return logical > physical | ||
| else: | ||
| # do not support on other platform yet | ||
| return False | ||
|
|
||
| def get_logical_processors(): | ||
| '''Get the logical processors number''' | ||
| import platform | ||
| if platform.system() == "Linux": | ||
| processors = os.popen( | ||
|
||
| "grep \"processor\" /proc/cpuinfo|sort -u|wc -l") | ||
| return int(processors.read()) | ||
| elif platform.system() == "Darwin": | ||
| processors = os.popen("sysctl hw.logicalcpu") | ||
| return int(processors.read()) | ||
| else: | ||
| # do not support on other platform yet | ||
| return 1 | ||
|
||
|
|
||
| if is_hyperthreading_enabled(): | ||
| set_env("OMP_DYNAMIC", "true") | ||
| set_env("KMP_AFFINITY", "granularity=fine,compact,1,0") | ||
| processors = os.popen("grep \"processor\" /proc/cpuinfo|sort -u|wc -l") | ||
| processors = int(processors.read()) | ||
| trainers = kwargs.get('trainer_count', 1) | ||
| threads = processors / trainers | ||
| else: | ||
| set_env("OMP_DYNAMIC", "false") | ||
| set_env("KMP_AFFINITY", "granularity=fine,compact,0,0") | ||
| processors = get_logical_processors() | ||
| threads = processors / trainer_count | ||
| threads = '1' if threads < 1 else str(threads) | ||
| set_env("OMP_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]))) | ||
|
|
||
| auto_set_cpu_env(kwargs.get('trainer_count', 1)) | ||
|
||
|
|
||
| if 'use_gpu' in kwargs: | ||
| cp.g_command_config_args['use_gpu'] = kwargs['use_gpu'] | ||
| if 'use_mkldnn' in kwargs: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function returns the number of logical processors, other than a list of logical processors, so it should be named either
get_num_logical_processorornum_logical_processor. Otherwise, we'd lose the most important concept "number".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks, I will use
num_logical_processorinstead.