-
Notifications
You must be signed in to change notification settings - Fork 22
Optimization Impact #750
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
Merged
Merged
Optimization Impact #750
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
df0b176
wip
aseembits93 aefedda
works, todo cleanup
aseembits93 2e8414f
todo cleanup
aseembits93 cdbe3e0
Merge branch 'main' into opt-impact-aseem
aseembits93 642ac43
Merge branch 'main' into opt-impact-aseem
HeshamHM28 d209140
Merge remote-tracking branch 'origin/main' into opt-impact-aseem
aseembits93 d766dab
logging done, start experimenting, using diff instead of raw strings
aseembits93 6a4d842
precommit fix
aseembits93 530158c
ready to review
aseembits93 4d9723d
bugfix
aseembits93 eb032bb
Merge remote-tracking branch 'origin/main' into opt-impact-aseem
aseembits93 ab1c385
add explanation
aseembits93 e380b95
Merge branch 'main' into opt-impact-aseem
aseembits93 4dcb9af
Merge remote-tracking branch 'origin/main' into opt-impact-aseem
aseembits93 90a1355
speedup relative
aseembits93 c2ffeae
Apply suggestion from @aseembits93
aseembits93 b991c33
Merge branch 'main' into opt-impact-aseem
KRRT7 27fb31b
Merge branch 'main' into opt-impact-aseem
aseembits93 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,8 @@ | |
| import os | ||
| import platform | ||
| import time | ||
| from typing import TYPE_CHECKING, Any | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any, cast | ||
|
|
||
| import requests | ||
| from pydantic.json import pydantic_encoder | ||
|
|
@@ -13,18 +14,18 @@ | |
| from codeflash.code_utils.config_consts import N_CANDIDATES_EFFECTIVE, N_CANDIDATES_LP_EFFECTIVE | ||
| from codeflash.code_utils.env_utils import get_codeflash_api_key | ||
| from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name | ||
| from codeflash.code_utils.time_utils import humanize_runtime | ||
| from codeflash.lsp.helpers import is_LSP_enabled | ||
| from codeflash.models.ExperimentMetadata import ExperimentMetadata | ||
| from codeflash.models.models import AIServiceRefinerRequest, CodeStringsMarkdown, OptimizedCandidate | ||
| from codeflash.telemetry.posthog_cf import ph | ||
| from codeflash.version import __version__ as codeflash_version | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
| from codeflash.discovery.functions_to_optimize import FunctionToOptimize | ||
| from codeflash.models.ExperimentMetadata import ExperimentMetadata | ||
| from codeflash.models.models import AIServiceRefinerRequest | ||
| from codeflash.result.explanation import Explanation | ||
|
|
||
|
|
||
| class AiServiceClient: | ||
|
|
@@ -516,6 +517,90 @@ def generate_regression_tests( # noqa: D417 | |
| ph("cli-testgen-error-response", {"response_status_code": response.status_code, "error": response.text}) | ||
| return None | ||
|
|
||
| def get_optimization_impact( | ||
| self, | ||
| original_code: dict[Path, str], | ||
| new_code: dict[Path, str], | ||
| explanation: Explanation, | ||
| existing_tests_source: str, | ||
| generated_original_test_source: str, | ||
| function_trace_id: str, | ||
| coverage_message: str, | ||
| replay_tests: str, | ||
| concolic_tests: str, | ||
| root_dir: Path, | ||
| original_line_profiler_results: str, | ||
| optimized_line_profiler_results: str, | ||
| ) -> str: | ||
| """Optimize the given python code for performance by making a request to the Django endpoint. | ||
|
|
||
| Args: | ||
| original_code: dict, | ||
| new_code: dict, | ||
| explanation: Explanation, | ||
| existing_tests_source: str, | ||
| generated_original_test_source: str, | ||
| function_trace_id: str, | ||
| coverage_message: str, | ||
| replay_tests: str, | ||
| concolic_tests: str, | ||
| root_dir: Path, | ||
| original_line_profiler_results: str, | ||
| optimized_line_profiler_results: str, | ||
|
|
||
| Returns: | ||
| ------- | ||
| - 'high','medium' or 'low' optimization impact | ||
aseembits93 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| """ | ||
| logger.info("!lsp|Computing Optimization Impact…") | ||
| original_code_str = "" | ||
| new_code_str = "" | ||
| for p, code in original_code.items(): | ||
| original_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" | ||
aseembits93 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| original_code_str += "\n" | ||
| original_code_str += code | ||
| for p, code in new_code.items(): | ||
| new_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" | ||
| new_code_str += "\n" | ||
| new_code_str += code | ||
|
|
||
| payload = { | ||
| "original_code": original_code_str, | ||
| "optimized_code": new_code_str, | ||
| "existing_tests": existing_tests_source, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Q: How much existing tests and generated tests are effective in context. Did we try the results ignoring them as well? |
||
| "generated_tests": generated_original_test_source, | ||
| "trace_id": function_trace_id, | ||
| "coverage_message": coverage_message, | ||
| "replay_tests": replay_tests, | ||
| "concolic_tests": concolic_tests, | ||
| "speedup": f"{1 + float(explanation.speedup):.2f}x", | ||
| "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), | ||
| "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, | ||
| "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), | ||
| "original_runtime": humanize_runtime(explanation.original_runtime_ns), | ||
| "original_line_profiler_results": original_line_profiler_results, | ||
| "optimized_line_profiler_results": optimized_line_profiler_results, | ||
| } | ||
| console.rule() | ||
| try: | ||
| response = self.make_ai_service_request("/optimization_impact", payload=payload, timeout=600) | ||
| except requests.exceptions.RequestException as e: | ||
| logger.exception(f"Error generating optimization refinements: {e}") | ||
| ph("cli-optimize-error-caught", {"error": str(e)}) | ||
| return "" | ||
|
|
||
| if response.status_code == 200: | ||
| return cast("str", response.json()["impact"]) | ||
aseembits93 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| error = cast("str", response.json()["error"]) | ||
| except Exception: | ||
| error = response.text | ||
| logger.error(f"Error generating impact candidates: {response.status_code} - {error}") | ||
| ph("cli-optimize-error-response", {"response_status_code": response.status_code, "error": error}) | ||
| console.rule() | ||
| return "" | ||
|
|
||
|
|
||
| class LocalAiServiceClient(AiServiceClient): | ||
| """Client for interacting with the local AI service.""" | ||
|
|
||
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
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.