Skip to content

Incorrect URI Prefix Stripping in MLflowLogger #20279

@awindmann

Description

@awindmann

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions