Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify in docs how runtime parameter resolution works #4096

Merged
merged 7 commits into from
Aug 21, 2024

Conversation

merelcht
Copy link
Member

@merelcht merelcht commented Aug 15, 2024

Description

Closes #3456

Development notes

The problem is that when runtime parameters are provided, they get merged with both the config loaded from the base environment as well as the run environment. So they are duplicated and because of the destructive merge happening by default this can end up removing values present in base config and not in the runtime config.

Solutions tried:

  1. Only merge runtime_params into base config and not in all loaded configs: this meets the requirements for point 1 and 3 above, but not 2.
  2. Move the merging of runtime_params to after both base and default_run_env config have been loaded. This meets requirements for 1 and 2, but not 3.

Proposed "solution"

not change anything. All the behaviours can be met with the current implementation in place, but to meet the expectation described in #3456 the user has to specify soft merging for parameters to ensure the default_run_env doesn't destructively overwrite (nested) parameters in base config.

Developer Certificate of Origin

We need all contributions to comply with the Developer Certificate of Origin (DCO). All commits must be signed off by including a Signed-off-by line in the commit message. See our wiki for guidance.

If your PR is blocked due to unsigned commits, then you must follow the instructions under "Rebase the branch" on the GitHub Checks page for your PR. This will retroactively add the sign-off to all unsigned commits and allow the DCO check to pass.

Checklist

  • Read the contributing guidelines
  • Signed off each commit with a Developer Certificate of Origin (DCO)
  • Opened this PR as a 'Draft Pull Request' if it is work-in-progress
  • Updated the documentation to reflect the code changes
  • Added a description of this change in the RELEASE.md file
  • Added tests to cover my changes
  • Checked if this change will affect Kedro-Viz, and if so, communicated that with the Viz team

…un env is also set

Signed-off-by: Merel Theisen <merel.theisen@quantumblack.com>
@merelcht merelcht changed the title Add test to check runtime parameters are overwritten correctly when r… Fix OmegaConfLoader runtime parameter resolution when run env is set Aug 15, 2024
Signed-off-by: Merel Theisen <merel.theisen@quantumblack.com>
@merelcht merelcht marked this pull request as ready for review August 16, 2024 10:52
@merelcht merelcht self-assigned this Aug 16, 2024
Copy link
Contributor

@noklam noklam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it difficult to understand conceptually that we only include runtime_params for base_env but not the others. I think this may be buggy:

I try this with an example:

# base
model_options:
  test_size: 0.2
  random_state: 3
  features:
    - engines


# local
model_options:
  test_size: 0.2
  random_state: 3
  features:
    - engines
nok: 1
%reload_kedro --params nok=3
config = context.config_loader
config["parameters"]

The expexted parameters should give me nok=3, but instead I get nok=1. If I remove nok:1 from local then I get nok=3 again.

@merelcht
Copy link
Member Author

@noklam you're right. Let me dig some more!

@merelcht merelcht marked this pull request as draft August 19, 2024 13:02
Signed-off-by: Merel Theisen <merel.theisen@quantumblack.com>
@merelcht
Copy link
Member Author

This issue is more complicated than it seemed at first. The current behaviour is as follows:

  • base config gets loaded and any runtime_params get merged into this (with soft-merge).
  • Then default_run_env config gets loaded and any runtime_params get merged into this (with soft-merge).
  • Then by default base and default_run_env get merged with a destructive merge, meaning any duplicate keys in base and default_run_env will be overwritten with the values in default_run_env.

I've added some tests to highlight what's going on and what the desired behaviour is:

  1. If a (nested) parameter exists in the base config but not in the default_run_env config and needs to be overwritten at runtime through runtime_params only the specified parameter should be updated and not the full entry replaced, so don't destructively merge. (see test: test_runtime_params_resolution_with_env + more context Fix the overwriting of nested parameters with OmegaConfigLoader #3456 )
  2. If a (nested) parameter exists in the default_run_env but not in the base config and needs to be overwritted at runtime through runtime_params, we expect to see the updated runtime value. (see test: test_runtime_params_resolution_with_env2 + Nok's comment)
  3. Any interpolated values are overwritten by runtime_params when provided (see test: test_runtime_params_override_interpolated_value)

Solutions tried:

  1. Only merge runtime_params into base config and not in all loaded configs: this meets the requirements for point 1 and 3 above, but not 2.
  2. Move the merging of runtime_params to after both base and default_run_env config have been loaded. This meets requirements for 1 and 2, but not 3.

Proposed "solution": not change anything. All the behaviours can be met with the current implementation in place, but to meet the first expectation the user has to specify soft merging for parameters to ensure the default_run_env doesn't destructively overwrite (nested) parameters in base config.

@merelcht merelcht requested a review from DimedS August 20, 2024 11:31
@ElenaKhaustova
Copy link
Contributor

What if we just merge base and default_run_env first and then merge runtime_params once? From what I got, the problem is that base and default_run_env have different priorities when merging, and merging runtime_params to each leads to losing some parameters from runtime_params. So, if we merge runtime_params on top user can always define how to do the last (runtime_params) merge, whether they override the result of the previous merge or not.

Copy link
Contributor

@DimedS DimedS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging into that so thoroughly, @merelcht! From what I understand, the desired behavior is achievable, but we need to clearly document it for the users, right?

@merelcht
Copy link
Member Author

merelcht commented Aug 20, 2024

What if we just merge base and default_run_env first and then merge runtime_params once? From what I got, the problem is that base and default_run_env have different priorities when merging, and merging runtime_params to each leads to losing some parameters from runtime_params. So, if we merge runtime_params on top user can always define how to do the last (runtime_params) merge, whether they override the result of the previous merge or not.

@ElenaKhaustova I thought the same and tried this, but then this part doesn't work anymore:

  1. Any interpolated values are overwritten by runtime_params when provided (see test: test_runtime_params_override_interpolated_value)

@ElenaKhaustova
Copy link
Contributor

ElenaKhaustova commented Aug 20, 2024

What if we just merge base and default_run_env first and then merge runtime_params once? From what I got, the problem is that base and default_run_env have different priorities when merging, and merging runtime_params to each leads to losing some parameters from runtime_params. So, if we merge runtime_params on top user can always define how to do the last (runtime_params) merge, whether they override the result of the previous merge or not.

@ElenaKhaustova I thought the same and tried this, but then this part doesn't work anymore:

  1. Any interpolated values are overwritten by runtime_params when provided (see test: test_runtime_params_override_interpolated_value)

Then, it looks like there's no easy way to fix that without refactoring the logic. So, just explaining how to achieve the desired behaviour looks good, though it's not easy cause the examples are quite niche and require an understanding of how resolution works from the user side. We can keep an eye on this case in if we decide to refactor this part to avoid corner cases because it looks quite complex now.

…n works.

Signed-off-by: Merel Theisen <merel.theisen@quantumblack.com>
@merelcht merelcht changed the title Fix OmegaConfLoader runtime parameter resolution when run env is set Clarify in docs how runtime parameter resolution works Aug 21, 2024
@merelcht merelcht marked this pull request as ready for review August 21, 2024 10:24
@merelcht merelcht requested a review from DimedS August 21, 2024 10:24
Copy link
Contributor

@DimedS DimedS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @merelcht! I'm not sure, but perhaps including an example to the docs similar to the one you provided in the tests would help to better illustrate the concept.

Signed-off-by: Merel Theisen <merel.theisen@quantumblack.com>
@merelcht
Copy link
Member Author

Thank you, @merelcht! I'm not sure, but perhaps including an example to the docs similar to the one you provided in the tests would help to better illustrate the concept.

Done! Let me know if this is what you expected.

Signed-off-by: Merel Theisen <merel.theisen@quantumblack.com>
Copy link
Member

@astrojuanlu astrojuanlu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@DimedS
Copy link
Contributor

DimedS commented Aug 21, 2024

Thank you, @merelcht! I'm not sure, but perhaps including an example to the docs similar to the one you provided in the tests would help to better illustrate the concept.

Done! Let me know if this is what you expected.

super, thank you!

@merelcht merelcht merged commit 43b3611 into main Aug 21, 2024
41 checks passed
@merelcht merelcht deleted the fix/omegaconf-runtime-param-resolution branch August 21, 2024 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix the overwriting of nested parameters with OmegaConfigLoader
5 participants