From c37ab85d96fd786d8839e0a439bbe37c04b659ea Mon Sep 17 00:00:00 2001 From: Michael Lazos Date: Wed, 22 Mar 2023 13:26:53 +0000 Subject: [PATCH] Improve TORCH_LOGS settings error msg (#97264) Lists registered loggable entities if an invalid settings string is passed via TORCH_LOGS [before](https://gist.github.com/mlazos/91fcbc3d577f874bcb3daea44f8b41f2) [after](https://gist.github.com/mlazos/815ea9e76aca665602228f960e0eb0d6) Pull Request resolved: https://github.com/pytorch/pytorch/pull/97264 Approved by: https://github.com/ezyang, https://github.com/jansel --- torch/_logging/_internal.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/torch/_logging/_internal.py b/torch/_logging/_internal.py index 1c2b4c1af0390..448e593a00512 100644 --- a/torch/_logging/_internal.py +++ b/torch/_logging/_internal.py @@ -253,15 +253,26 @@ def _validate_settings(settings): return re.fullmatch(_gen_settings_regex(), settings) is not None +def _invalid_settings_err_msg(settings): + entities = "\n " + "\n ".join( + itertools.chain( + log_registry.log_alias_to_log_qname.keys(), log_registry.artifact_names + ) + ) + msg = ( + f"Invalid log settings: {settings}, must be a comma separated list of fully qualified module names, " + f"registered log names or registered artifact names.\nCurrently registered names: {entities}" + ) + return msg + + @functools.lru_cache() def _parse_log_settings(settings): if settings == "": return dict() if not _validate_settings(settings): - raise ValueError( - f"Invalid log settings: {settings}, must be a comma separated list of registerered log or artifact names." - ) + raise ValueError(_invalid_settings_err_msg(settings)) settings = re.sub(r"\s+", "", settings) log_names = settings.split(",") @@ -295,9 +306,7 @@ def get_name_level_pair(name): log_registry.register_child_log(name) log_state.enable_log(name, level) else: - raise ValueError( - f"Invalid log settings: '{settings}', must be a comma separated list of log or artifact names." - ) + raise ValueError(_invalid_settings_err_msg(settings)) return log_state