Skip to content
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

Bug Report: Incorrect URI Prefix Stripping in MLflowLogger #20279

Open
awindmann opened this issue Sep 13, 2024 · 0 comments · May be fixed by #20365
Open

Bug Report: Incorrect URI Prefix Stripping in MLflowLogger #20279

awindmann opened this issue Sep 13, 2024 · 0 comments · May be fixed by #20365
Labels
bug Something isn't working needs triage Waiting to be triaged by maintainers ver: 2.4.x

Comments

@awindmann
Copy link

Bug description

Problem:
The save_dir property in the MLFlowLogger class currently uses str.lstrip() to remove the LOCAL_FILE_URI_PREFIX ("file:") from the tracking URI. This method can unintentionally strip additional characters that match those in the prefix. For example, "file:logs" would be incorrectly modified to "ogs" because lstrip() removes all individual characters that appear in the prefix string, not just the prefix itself.

Minimal Example:

from pytorch_lightning.loggers import MLFlowLogger

logger0 = MLFlowLogger(
    save_dir='logs'
)
logger1 = MLFlowLogger(
    save_dir='experiments'
)
logger2 = MLFlowLogger(
    save_dir='runs'  # this should work, as mlflow.LOCAL_FILE_URI_PREFIX ('file:') does not contain r
)
print(logger0.save_dir)  # ogs
print(logger1.save_dir)  # xperiments
print(logger2.save_dir)  # runs

Proposed Solutions:
For Python 3.9 and later: Utilize str.removeprefix(), which is designed to remove a specified prefix only if the string starts with it:

if self._tracking_uri.startswith(LOCAL_FILE_URI_PREFIX):
    return self._tracking_uri.removeprefix(LOCAL_FILE_URI_PREFIX)
return None

For Python 3.8 and earlier: Unfortunately, str.removeprefix() was introduced with Python 3.9.0, so another approach would have to be used. One possible solution would be to simply slice off the length of the prefix.

if self._tracking_uri.startswith(LOCAL_FILE_URI_PREFIX):
    return self._tracking_uri[len(LOCAL_FILE_URI_PREFIX):]
return None

What version are you seeing the problem on?

v2.4

How to reproduce the bug

No response

Error messages and logs

# Error messages and logs here please

Environment

Current environment
#- PyTorch Lightning Version (e.g., 2.4.0):
#- PyTorch Version (e.g., 2.4):
#- Python version (e.g., 3.12):
#- OS (e.g., Linux):
#- CUDA/cuDNN version:
#- GPU models and configuration:
#- How you installed Lightning(`conda`, `pip`, source):

More info

No response

@awindmann awindmann added bug Something isn't working needs triage Waiting to be triaged by maintainers labels Sep 13, 2024
@awindmann awindmann linked a pull request Oct 25, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage Waiting to be triaged by maintainers ver: 2.4.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant