Skip to content

Commit

Permalink
Improve TORCH_LOGS settings error msg (pytorch#97264)
Browse files Browse the repository at this point in the history
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: pytorch#97264
Approved by: https://github.com/ezyang, https://github.com/jansel
  • Loading branch information
mlazos authored and pytorchmergebot committed Mar 22, 2023
1 parent aab34a4 commit c37ab85
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions torch/_logging/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(",")
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c37ab85

Please sign in to comment.