vanhauser-thc / thc-hydra

hydra
GNU Affero General Public License v3.0
9.24k stars 1.93k forks source link

Add more meaningful information on hydra resolving interpolation error #930

Closed gardiens closed 4 months ago

gardiens commented 4 months ago

Hello, I have been debugging a bit with hydra and it has been a pain when the error happens in some interpolation inside the config. Indeed, when it raises an error, it doesn't completely specify what is the mistake and where the error happens. for example let's take this cfg.yaml :

target: dummyclass point_hf:

  • pos
  • anisotropy

num_hf_point: ${eval:'sum([ ${feat_size}[k] for k in ${point_hf} ])'}

feat_size: pos : 3

and this code : import hydra from omegaconf import DictConfig, OmegaConf import time OmegaConf.register_new_resolver("eval", eval) @hydra.main(version_base="1.2", config_path="./config/", config_name="config") def my_app(cfg : DictConfig) -> None: instantiate= hydra.utils.instantiate instantiate(cfg) class dummyclass: def init(self,point_hf,num_hf_point): self.point_hf=point_hf self.num_hf_point=num_hf_point

if name=="main": my_app()

if you run it : you got the only meaningful error :

omegaconf.errors.InterpolationResolutionError: KeyError raised while resolving interpolation: 'anisotropy'

the main issue is that you don't know what was the key that made the mistake and where the interpolation failed. the issue could have been on omegaconf but if we change the code with :

@hydra.main(version_base="1.2", config_path="./config/", config_name="config") def my_app(cfg : DictConfig) -> None:

instantiate= hydra.utils.instantiate

# instantiate(cfg)
print(cfg.num_hf_point)

the errors becomes:

omegaconf.errors.InterpolationResolutionError: KeyError raised while resolving interpolation: 'anisotropy' full_key: num_hf_point object_type=dict

which is much more easy to maintain

vanhauser-thc commented 4 months ago

Different hydra project :)