Skip to content

Commit

Permalink
MlflowLogger limit parameter value length to 250 char (#5893)
Browse files Browse the repository at this point in the history
  • Loading branch information
ducthienbui97 committed Feb 16, 2021
1 parent 4062c62 commit a0494ab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pytorch_lightning/loggers/mlflow.py
Expand Up @@ -150,6 +150,12 @@ def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
params = self._convert_params(params)
params = self._flatten_dict(params)
for k, v in params.items():
if len(str(v)) > 250:
rank_zero_warn(
f"Mlflow only allows parameters with up to 250 characters. Discard {k}={v}", RuntimeWarning
)
continue

self.experiment.log_param(self.run_id, k, v)

@rank_zero_only
Expand Down
15 changes: 15 additions & 0 deletions tests/loggers/test_mlflow.py
Expand Up @@ -184,3 +184,18 @@ def test_mlflow_logger_with_unexpected_characters(client, mlflow, tmpdir):

with pytest.warns(RuntimeWarning, match='special characters in metric name'):
logger.log_metrics(metrics)


@mock.patch('pytorch_lightning.loggers.mlflow.mlflow')
@mock.patch('pytorch_lightning.loggers.mlflow.MlflowClient')
def test_mlflow_logger_with_long_param_value(client, mlflow, tmpdir):
"""
Test that the logger raises warning with special characters not accepted by MLFlow.
"""
logger = MLFlowLogger('test', save_dir=tmpdir)
value = 'test' * 100
key = 'test_param'
params = {key: value}

with pytest.warns(RuntimeWarning, match=f'Discard {key}={value}'):
logger.log_hyperparams(params)

0 comments on commit a0494ab

Please sign in to comment.