conf_utils.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright (c) 2021-2024, NVIDIA CORPORATION. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from omegaconf import OmegaConf
  15. from data.data_utils import InputTypes, DataTypes, FeatureSpec
  16. import functools
  17. from hydra.utils import get_method
  18. OmegaConf.register_new_resolver("and", lambda x, y: bool(x and y), use_cache=True)
  19. OmegaConf.register_new_resolver("feature.selector",
  20. lambda x,feat_type,embed_type:
  21. OmegaConf.create([elem for elem in x if elem.feature_type == feat_type and elem.feature_embed_type == embed_type])
  22. )
  23. OmegaConf.register_new_resolver("add", lambda x,y: x + y)
  24. OmegaConf.register_new_resolver("if", lambda x,y,z: y if x else z)
  25. OmegaConf.register_new_resolver("feature.cardinalities", lambda x: OmegaConf.create([elem.cardinality for elem in x]))
  26. OmegaConf.register_new_resolver("len", len)
  27. OmegaConf.register_new_resolver("cmp", lambda x, y: x == y)
  28. OmegaConf.register_new_resolver("cont.lower", lambda x, y: y.lower() in x.lower())
  29. def sum_nested(*args):
  30. if len(args) == 1 and isinstance(args[0], (int, float)):
  31. return args[0]
  32. return sum(arg if isinstance(arg, (int, float)) else sum_nested(*arg) for arg in args)
  33. OmegaConf.register_new_resolver("sum", sum_nested)
  34. def partial(func, *args, **kwargs):
  35. return functools.partial(get_method(func), *args, **kwargs)