Repository navigation
Python: fix: optimize KernelArguments merge to avoid unnecessary dict copy #13598
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
Open
Nima Nikoonazar (nimanikoo)
wants to merge
19
commits into
microsoft:main
Choose a base branch
from
nimanikoo:fix/optimize-kernel-arguments-merge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2041c8a
fix: optimize KernelArguments merge to avoid unnecessary dict copy
nimanikoo da0c622
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo 574ab8e
Update python/tests/unit/functions/test_kernel_arguments_merge_optimi…
nimanikoo 9f9fa02
Update python/semantic_kernel/functions/kernel_arguments.py
nimanikoo 43bc49d
Update python/tests/unit/functions/test_kernel_arguments_merge_optimi…
nimanikoo b1374d6
Update python/semantic_kernel/functions/kernel_arguments.py
nimanikoo e2ed726
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo 76c1c3b
Update python/semantic_kernel/functions/kernel_arguments.py
nimanikoo 810cb92
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo b37a8cc
Update kernel_arguments.py
nimanikoo 44250c7
Update kernel_arguments.py
nimanikoo dca2019
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo eb25bad
Python: Fix KernelArguments merge tests to use PromptExecutionSettings
nimanikoo 53194ac
Python: Fix aliasing bug in KernelArguments constructor for dict sett…
nimanikoo d5a6c8d
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo 0daf7f4
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo 3d3819d
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo 94b6e23
Merge branch 'main' into fix/optimize-kernel-arguments-merge
eavanvalkenburg d91979e
Merge branch 'main' into fix/optimize-kernel-arguments-merge
nimanikoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
python/tests/unit/functions/test_kernel_arguments_merge_optimization.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Tests for KernelArguments merge operator behavior.""" | ||
|
|
||
| import pytest | ||
|
|
||
| from semantic_kernel.connectors.ai.prompt_execution_settings import PromptExecutionSettings | ||
| from semantic_kernel.functions.kernel_arguments import KernelArguments | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def settings_svc1(): | ||
| return PromptExecutionSettings(service_id="service1") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def settings_svc2(): | ||
| return PromptExecutionSettings(service_id="service2") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def settings_default(): | ||
| return PromptExecutionSettings(service_id="default") | ||
|
|
||
|
|
||
| class TestKernelArgumentsMergeOptimization: | ||
| """Test suite for KernelArguments merge operator behavior.""" | ||
|
|
||
| def test_or_operator_no_execution_settings(self): | ||
| """Test that | operator works when execution_settings is None.""" | ||
| args = KernelArguments(a=1, b=2) | ||
|
|
||
| result = args | {"c": 3} | ||
|
|
||
| assert result["a"] == 1 | ||
| assert result["b"] == 2 | ||
| assert result["c"] == 3 | ||
|
nimanikoo marked this conversation as resolved.
|
||
|
|
||
| def test_or_operator_merges_settings_by_service_id(self, settings_svc1, settings_svc2): | ||
| """Test | merges execution_settings keyed by service_id.""" | ||
| args1 = KernelArguments(a=1, settings=settings_svc1) | ||
| args2 = KernelArguments(b=2, settings=settings_svc2) | ||
|
|
||
| result = args1 | args2 | ||
|
|
||
| assert result["a"] == 1 | ||
| assert result["b"] == 2 | ||
| assert result.execution_settings["service1"] is settings_svc1 | ||
| assert result.execution_settings["service2"] is settings_svc2 | ||
|
|
||
| def test_or_operator_right_side_wins_on_same_service_id(self, settings_default): | ||
| """Test | overwrites execution_settings when both sides share a service_id.""" | ||
| settings_override = PromptExecutionSettings(service_id="default") | ||
| args1 = KernelArguments(a=1, settings=settings_default) | ||
| args2 = KernelArguments(b=2, settings=settings_override) | ||
|
|
||
| result = args1 | args2 | ||
|
|
||
| assert result.execution_settings["default"] is settings_override | ||
|
|
||
| def test_ror_operator_merges_plain_dict_with_kernel_arguments(self, settings_svc1): | ||
| """Test reverse | operator merges a plain dict with KernelArguments.""" | ||
| args = KernelArguments(a=1, settings=settings_svc1) | ||
|
|
||
| result = {"b": 2} | args | ||
|
|
||
| assert result["a"] == 1 | ||
| assert result["b"] == 2 | ||
| assert result.execution_settings["service1"] is settings_svc1 | ||
|
|
||
|
nimanikoo marked this conversation as resolved.
|
||
| def test_ior_operator_merges_settings_in_place(self, settings_svc1, settings_svc2): | ||
| """Test |= merges execution_settings into the existing dict in-place.""" | ||
| args1 = KernelArguments(a=1, settings=settings_svc1) | ||
| original_dict = args1.execution_settings | ||
|
|
||
| args2 = KernelArguments(b=2, settings=settings_svc2) | ||
| args1 |= args2 | ||
|
|
||
| assert args1["a"] == 1 | ||
| assert args1["b"] == 2 | ||
| assert args1.execution_settings is original_dict | ||
| assert args1.execution_settings["service1"] is settings_svc1 | ||
| assert args1.execution_settings["service2"] is settings_svc2 | ||
|
|
||
| def test_ior_operator_copies_settings_when_target_has_none(self, settings_svc1): | ||
| """Test |= creates a copy of execution_settings when target has none.""" | ||
| args1 = KernelArguments(a=1) | ||
| assert args1.execution_settings is None | ||
|
|
||
| args2 = KernelArguments(b=2, settings=settings_svc1) | ||
| args1 |= args2 | ||
|
|
||
| assert args1.execution_settings is not None | ||
| assert args1.execution_settings["service1"] is settings_svc1 | ||
| assert args1.execution_settings is not args2.execution_settings | ||
|
|
||
| def test_or_operator_does_not_mutate_original_settings(self, settings_svc1, settings_svc2): | ||
| """Test that | does not mutate the original execution_settings.""" | ||
| args1 = KernelArguments(a=1, settings=settings_svc1) | ||
| args2 = KernelArguments(b=2, settings=settings_svc2) | ||
| original_settings = args1.execution_settings | ||
|
|
||
| result = args1 | args2 | ||
|
nimanikoo marked this conversation as resolved.
|
||
|
|
||
| assert args1.execution_settings is original_settings | ||
| assert "service2" not in args1.execution_settings | ||
| assert "service2" in result.execution_settings | ||
|
|
||
| def test_ior_operator_right_side_wins_on_same_service_id(self, settings_default): | ||
| """Test |= overwrites existing settings entry when service_id matches.""" | ||
| settings_override = PromptExecutionSettings(service_id="default") | ||
| args1 = KernelArguments(a=1, settings=settings_default) | ||
| args2 = KernelArguments(b=2, settings=settings_override) | ||
|
|
||
| args1 |= args2 | ||
|
|
||
| assert args1.execution_settings["default"] is settings_override | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.