!22135 rank id master 0820

Merge pull request !22135 from mindspore_ding/rank_id_master_0816
This commit is contained in:
i-robot 2021-08-27 08:51:05 +00:00 committed by Gitee
commit dd907a4d91
2 changed files with 48 additions and 3 deletions

View File

@ -427,6 +427,46 @@ def _get_stack_info(frame):
return sinfo
def _get_rank_id():
"""
get rank id
ascend, CPU: RANK_ID
GPU: OMPI_COMM_WORLD_RANK
Both RANK_ID and OMPI_COMM_WORLD_RANK exist, Report to the police, default: RANK_ID
Returns:
rank.
"""
rank_id = os.getenv('RANK_ID')
gpu_rank_id = os.getenv('OMPI_COMM_WORLD_RANK')
rank = '0'
if rank_id and gpu_rank_id:
logger.warning("Environment variables RANK_ID and OMPI_COMM_WORLD_RANK both exist,"
"RANK_ID will be used.")
if rank_id:
rank = rank_id
elif gpu_rank_id:
rank = gpu_rank_id
return rank
def _create_logfile_dir(kwargs):
"""
create logs dir
Args:
kwargs (dict): The dictionary of log configurations.
Returns:
Log_dir: Create subdirectory.
Examples:
>>> /rank_0/logs
"""
log_dir = os.path.realpath(kwargs.get('filepath'))
rank_id = _get_rank_id()
log_dir += '/rank_' + rank_id + '/logs'
if not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
return log_dir
def _setup_logger(kwargs):
"""
Set up the logger.
@ -483,7 +523,7 @@ def _setup_logger(kwargs):
# Set rotatingFileHandler for the file appender
else:
# filepath cannot be null, checked in function _verify_config ()
logfile_dir = os.path.realpath(kwargs.get('filepath'))
logfile_dir = _create_logfile_dir(kwargs)
file_name = f'{logfile_dir}/{log_name}'
logfile_handler = _MultiCompatibleRotatingFileHandler(
filename=file_name,

View File

@ -75,7 +75,9 @@ def test_log_file():
filename = ''
os.makedirs(file_path, exist_ok=True)
from mindspore import log as logger
_clear_logger(logger)
logger.warning("test log message warning")
file_path += '/rank_0/logs'
f_list = os.listdir(file_path)
# print f_list
for file_name in f_list:
@ -114,11 +116,13 @@ def test_log_backup_count():
shutil.rmtree(file_path)
os.makedirs(file_path, exist_ok=True)
from mindspore import log as logger
_clear_logger(logger)
log_count = 100
for i in range(0, log_count, 1):
logger.warning("test log message warning %r", i)
file_path += '/rank_0/logs'
cmd = f'cd {file_path};ls -l | grep \'mindspore.log.*\'|wc -l'
backup_count = '11'
file_count = os.popen(cmd).read().strip()
@ -244,9 +248,10 @@ def test_log_getconfig():
os.environ['logger_maxBytes'] = '1000'
os.environ['logger_backupCount'] = '10'
from mindspore import log as logger
logger.info("test log message info test ")
_clear_logger(logger)
logger.info("test log message info test")
configdict = logger.get_log_config()
targetdict = {'GLOG_v': '3', 'GLOG_log_dir': '/tmp/log',
targetdict = {'GLOG_v': '3', 'GLOG_log_dir': '/tmp/log/rank_0/logs',
'GLOG_logtostderr': '0', 'logger_maxBytes': 1000,
'logger_backupCount': 10, 'GLOG_stderrthreshold': '2'}
assert configdict == targetdict