-
Notifications
You must be signed in to change notification settings - Fork 22
[Chore] Async implementation for perform_function_optimization #758
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
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ba7e248
Make perform_function_optimization asynchronous
HeshamHM28 5cb0ab5
Merge branch 'main' into chore/asyncio-optimization
HeshamHM28 39f47f9
fix some errors
HeshamHM28 593ff6f
make status canceled
HeshamHM28 e07b5b4
add missing codePrint()
HeshamHM28 36fd051
Merge branch 'main' into chore/asyncio-optimization
HeshamHM28 2d57414
Merge branch 'main' into chore/asyncio-optimization
HeshamHM28 3871853
Merge branch 'main' into chore/asyncio-optimization
HeshamHM28 0a6aae2
Merge branch 'main' into chore/asyncio-optimization
HeshamHM28 e22d55e
Merge branch 'main' into chore/asyncio-optimization
KRRT7 33f15e6
Merge branch 'main' into chore/asyncio-optimization
HeshamHM28 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
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
Empty file.
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,152 @@ | ||
| import contextlib | ||
HeshamHM28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from codeflash.cli_cmds.console import code_print | ||
| from codeflash.code_utils.git_worktree_utils import create_diff_patch_from_worktree | ||
| from codeflash.either import is_successful | ||
| from codeflash.lsp.server import CodeflashLanguageServer | ||
|
|
||
|
|
||
| # ruff: noqa: PLR0911, ANN001 | ||
| def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]: | ||
| server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info") | ||
| current_function = server.optimizer.current_function_being_optimized | ||
|
|
||
| if not current_function: | ||
| server.show_message_log(f"No current function being optimized for {params.functionName}", "Error") | ||
| return { | ||
| "functionName": params.functionName, | ||
| "status": "error", | ||
| "message": "No function currently being optimized", | ||
| } | ||
|
|
||
| module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path) | ||
| if not module_prep_result: | ||
| return { | ||
| "functionName": params.functionName, | ||
| "status": "error", | ||
| "message": "Failed to prepare module for optimization", | ||
| } | ||
|
|
||
| validated_original_code, original_module_ast = module_prep_result | ||
|
|
||
| function_optimizer = server.optimizer.create_function_optimizer( | ||
| current_function, | ||
| function_to_optimize_source_code=validated_original_code[current_function.file_path].source_code, | ||
| original_module_ast=original_module_ast, | ||
| original_module_path=current_function.file_path, | ||
| function_to_tests={}, | ||
| ) | ||
|
|
||
| server.optimizer.current_function_optimizer = function_optimizer | ||
| if not function_optimizer: | ||
| return {"functionName": params.functionName, "status": "error", "message": "No function optimizer found"} | ||
|
|
||
| initialization_result = function_optimizer.can_be_optimized() | ||
| if not is_successful(initialization_result): | ||
| return {"functionName": params.functionName, "status": "error", "message": initialization_result.failure()} | ||
|
|
||
| should_run_experiment, code_context, original_helper_code = initialization_result.unwrap() | ||
|
|
||
| code_print( | ||
| code_context.read_writable_code.flat, | ||
| file_name=current_function.file_path, | ||
| function_name=current_function.function_name, | ||
| ) | ||
|
|
||
| optimizable_funcs = {current_function.file_path: [current_function]} | ||
|
|
||
| devnull_writer = open(os.devnull, "w") # noqa | ||
| with contextlib.redirect_stdout(devnull_writer): | ||
| function_to_tests, _num_discovered_tests = server.optimizer.discover_tests(optimizable_funcs) | ||
| function_optimizer.function_to_tests = function_to_tests | ||
|
|
||
| test_setup_result = function_optimizer.generate_and_instrument_tests( | ||
| code_context, should_run_experiment=should_run_experiment | ||
| ) | ||
| if not is_successful(test_setup_result): | ||
| return {"functionName": params.functionName, "status": "error", "message": test_setup_result.failure()} | ||
| ( | ||
| generated_tests, | ||
| function_to_concolic_tests, | ||
| concolic_test_str, | ||
| optimizations_set, | ||
| generated_test_paths, | ||
| generated_perf_test_paths, | ||
| instrumented_unittests_created_for_function, | ||
| original_conftest_content, | ||
| ) = test_setup_result.unwrap() | ||
|
|
||
| baseline_setup_result = function_optimizer.setup_and_establish_baseline( | ||
| code_context=code_context, | ||
| original_helper_code=original_helper_code, | ||
| function_to_concolic_tests=function_to_concolic_tests, | ||
| generated_test_paths=generated_test_paths, | ||
| generated_perf_test_paths=generated_perf_test_paths, | ||
| instrumented_unittests_created_for_function=instrumented_unittests_created_for_function, | ||
| original_conftest_content=original_conftest_content, | ||
| ) | ||
|
|
||
| if not is_successful(baseline_setup_result): | ||
| return {"functionName": params.functionName, "status": "error", "message": baseline_setup_result.failure()} | ||
|
|
||
| ( | ||
| function_to_optimize_qualified_name, | ||
| function_to_all_tests, | ||
| original_code_baseline, | ||
| test_functions_to_remove, | ||
| file_path_to_helper_classes, | ||
| ) = baseline_setup_result.unwrap() | ||
|
|
||
| best_optimization = function_optimizer.find_and_process_best_optimization( | ||
| optimizations_set=optimizations_set, | ||
| code_context=code_context, | ||
| original_code_baseline=original_code_baseline, | ||
| original_helper_code=original_helper_code, | ||
| file_path_to_helper_classes=file_path_to_helper_classes, | ||
| function_to_optimize_qualified_name=function_to_optimize_qualified_name, | ||
| function_to_all_tests=function_to_all_tests, | ||
| generated_tests=generated_tests, | ||
| test_functions_to_remove=test_functions_to_remove, | ||
| concolic_test_str=concolic_test_str, | ||
| ) | ||
|
|
||
| if not best_optimization: | ||
| server.show_message_log( | ||
| f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning" | ||
| ) | ||
| return { | ||
| "functionName": params.functionName, | ||
| "status": "error", | ||
| "message": f"No best optimizations found for function {function_to_optimize_qualified_name}", | ||
| } | ||
| # generate a patch for the optimization | ||
| relative_file_paths = [code_string.file_path for code_string in code_context.read_writable_code.code_strings] | ||
| speedup = original_code_baseline.runtime / best_optimization.runtime | ||
| # get the original file path in the actual project (not in the worktree) | ||
| original_args, _ = server.optimizer.original_args_and_test_cfg | ||
| relative_file_path = current_function.file_path.relative_to(server.optimizer.current_worktree) | ||
| original_file_path = Path(original_args.project_root / relative_file_path).resolve() | ||
|
|
||
| metadata = create_diff_patch_from_worktree( | ||
| server.optimizer.current_worktree, | ||
| relative_file_paths, | ||
| metadata_input={ | ||
| "fto_name": function_to_optimize_qualified_name, | ||
| "explanation": best_optimization.explanation_v2, | ||
| "file_path": str(original_file_path), | ||
| "speedup": speedup, | ||
| }, | ||
| ) | ||
|
|
||
| server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info") | ||
| return { | ||
| "functionName": params.functionName, | ||
| "status": "success", | ||
| "message": "Optimization completed successfully", | ||
| "extra": f"Speedup: {speedup:.2f}x faster", | ||
| "patch_file": metadata["patch_path"], | ||
| "patch_id": metadata["id"], | ||
| "explanation": best_optimization.explanation_v2, | ||
| } | ||
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.