neptune-ai/neptune-client

BUG: neptune-optuna crashes with AttributeError: 'int' object has no attribute 'split'

lepremiere opened this issue · 2 comments

Describe the bug

When using the neptune-optuna integration (npt_utils.NeptuneCallback) on a a neptune run and reporting intermediate values to optuna, neptune crashes with the error: AttributeError: 'int' object has no attribute 'split'.
Following the traceback, I found that "stringify_unsupported" in the utils package misses to stringify the key of the dictionary.
Therefore, the keys of a Namespace object are not ensured to be of type string, hence key.split("/) would not work.

FIX:
Changing
{k: stringify_unsupported(v) for k, v in value.items()}
to
{str(k): stringify_unsupported(v) for k, v in value.items()}
in line 47 neptune.utils.py fixes the error for me.

Reproduction

def objective(trial: optuna.Trial):
    suggestions = {
        "learning_rate": trial.suggest_float("learning_rate", 1e-3, 5e-1, log=True),
    }
    for epoch in range(EPOCHS):
         ....
         trial.report(metric, epoch)

   return final_metric

if __name__ == "__main__":
      run = neptune.init_run(project="Test")

      # HPO
      study = optuna.create_study(direction="maximize")
      neptune_callback = npt_utils.NeptuneCallback(run)
      study.optimize(
          objective, 
          n_trials=10, 
          callbacks=[neptune_callback],
      )

Expected behavior

Not crash.

Traceback

Traceback (most recent call last):
File "/home/.../HumAIneCode/hpo_working.py", line 78, in
study.optimize(
File "/home/.../quantized/lib/python3.8/site-packages/optuna/study/study.py", line 442, in optimize
_optimize(
File "/home/.../quantized/lib/python3.8/site-packages/optuna/study/_optimize.py", line 66, in _optimize
_optimize_sequential(
File "/home/.../quantized/lib/python3.8/site-packages/optuna/study/_optimize.py", line 174, in _optimize_sequential
callback(study, frozen_trial)
File "/home/.../quantized/lib/python3.8/site-packages/neptune_optuna/impl/init.py", line 197, in call
self._log_trial(study, trial)
File "/home/.../quantized/lib/python3.8/site-packages/neptune_optuna/impl/init.py", line 205, in _log_trial
_log_single_trial(self.run, study, trial=trial, namespaces=self._namespaces)
File "/home/.../quantized/lib/python3.8/site-packages/neptune_optuna/impl/init.py", line 603, in _log_single_trial
handle[f"trials/{trial._trial_id}/intermediate_values"] = stringify_unsupported(trial.intermediate_values)
File "/home/.../quantized/lib/python3.8/site-packages/neptune/handler.py", line 117, in setitem
self[key].assign(value)
File "/home/.../quantized/lib/python3.8/site-packages/neptune/handler.py", line 86, in inner_fun
return fun(self, *args, **kwargs)
File "/home/../quantized/lib/python3.8/site-packages/neptune/handler.py", line 211, in assign
self._container.define(self._path, value)
File "/home/.../quantized/lib/python3.8/site-packages/neptune/metadata_containers/metadata_container.py", line 433, in define
neptune_value = cast_value(value)
File "/home/.../quantized/lib/python3.8/site-packages/neptune/types/type_casting.py", line 86, in cast_value
return Namespace(value)
File "/home/.../quantized/lib/python3.8/site-packages/neptune/types/namespace.py", line 41, in init
empty_keys = [k for k in self.value.keys() if not parse_path(k)]
File "/home/.../quantized/lib/python3.8/site-packages/neptune/types/namespace.py", line 41, in
empty_keys = [k for k in self.value.keys() if not parse_path(k)]
File "/home/.../quantized/lib/python3.8/site-packages/neptune/internal/utils/paths.py", line 26, in parse_path
return _remove_empty_paths(path.split("/"))
AttributeError: 'int' object has no attribute 'split'

Environment

The output of pip list:

The operating system you're using:
The output of python --version:

Additional context

Add any other context about the problem here.

Duplicate of #1401

Hey @lepremiere ,

We just released neptune v1.6.0, which fixes this issue. 🚀

Please let me know if this works for you!