Skip to content

Commit 836f216

Browse files
committed
start experimenting
1 parent 86cbcae commit 836f216

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

codeflash/api/aiservice.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,13 @@ def get_new_explanation(
284284
optimized_code: str,
285285
dependency_code: str,
286286
trace_id: str,
287-
existing_explanation: str,
287+
original_line_profiler_results: str,
288+
optimized_line_profiler_results: str,
289+
original_code_runtime: str,
290+
optimized_code_runtime: str,
291+
speedup: str,
292+
annotated_tests: str,
293+
optimization_id: str,
288294
) -> str:
289295
"""Optimize the given python code for performance by making a request to the Django endpoint.
290296
@@ -305,16 +311,22 @@ def get_new_explanation(
305311
payload = {
306312
"trace_id": trace_id,
307313
"source_code": source_code,
308-
"optimized_code":optimized_code,
309-
"existing_explanation": existing_explanation,
314+
"optimized_code": optimized_code,
315+
"original_line_profiler_results": original_line_profiler_results,
316+
"optimized_line_profiler_results": optimized_line_profiler_results,
317+
"original_code_runtime": original_code_runtime,
318+
"optimized_code_runtime": optimized_code_runtime,
319+
"speedup": speedup,
320+
"annotated_tests": annotated_tests,
321+
"optimization_id": optimization_id,
310322
"dependency_code": dependency_code,
311323
}
312-
logger.info("Generating optimized candidates…")
324+
logger.info("Generating explanation")
313325
console.rule()
314326
try:
315327
response = self.make_ai_service_request("/explain", payload=payload, timeout=600)
316328
except requests.exceptions.RequestException as e:
317-
logger.exception(f"Error generating optimized candidates: {e}")
329+
logger.exception(f"Error generating explanations: {e}")
318330
ph("cli-optimize-error-caught", {"error": str(e)})
319331
return ""
320332

codeflash/optimization/function_optimizer.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -574,19 +574,6 @@ def determine_best_candidate(
574574
if not len(self.valid_optimizations):
575575
return None
576576
# need to figure out the best candidate here before we return best_optimization
577-
# new_explanation = self.aiservice_client.get_new_explanation(
578-
# source_code=code_context.read_writable_code,
579-
# dependency_code=code_context.read_only_context_code,
580-
# trace_id=self.function_trace_id,
581-
# num_candidates=1,
582-
# experiment_metadata=None,
583-
# existing_explanation=best_optimization.candidate.explanation,
584-
# )
585-
#
586-
# best_optimization.candidate = replace(
587-
# best_optimization.candidate,
588-
# explanation=new_explanation if new_explanation != "" else best_optimization.candidate.explanation,
589-
# )
590577
diff_lens_list = [] # character level diff
591578
runtimes_list = []
592579
for valid_opt in self.valid_optimizations:
@@ -1134,11 +1121,30 @@ def find_and_process_best_optimization(
11341121
)
11351122
if concolic_test_str:
11361123
generated_tests_str += "\n\n" + concolic_test_str
1137-
1124+
new_explanation_raw_str = self.aiservice_client.get_new_explanation(
1125+
source_code=code_context.read_writable_code,
1126+
dependency_code=code_context.read_only_context_code,
1127+
trace_id=self.function_trace_id[:-4] + exp_type,
1128+
optimized_code=best_optimization.candidate.source_code,
1129+
original_line_profiler_results=original_code_baseline.line_profile_results["str_out"],
1130+
optimized_line_profiler_results=best_optimization.line_profiler_test_results["str_out"],
1131+
original_code_runtime=humanize_runtime(original_code_baseline.runtime),
1132+
optimized_code_runtime=humanize_runtime(best_optimization.runtime),
1133+
speedup=f"{int(performance_gain(original_runtime_ns=original_code_baseline.runtime, optimized_runtime_ns=best_optimization.runtime) * 100)}%",
1134+
annotated_tests=generated_tests_str,
1135+
optimization_id=best_optimization.candidate.optimization_id,
1136+
)
1137+
new_explanation = Explanation(raw_explanation_message=new_explanation_raw_str, winning_behavior_test_results=explanation.winning_behavior_test_results,
1138+
winning_benchmarking_test_results=explanation.winning_benchmarking_test_results,
1139+
original_runtime_ns=explanation.original_runtime_ns,
1140+
best_runtime_ns=explanation.best_runtime_ns,
1141+
function_name=explanation.function_name,
1142+
file_path=explanation.file_path,
1143+
benchmark_details=explanation.benchmark_details)
11381144
check_create_pr(
11391145
original_code=original_code_combined,
11401146
new_code=new_code_combined,
1141-
explanation=explanation,
1147+
explanation=new_explanation,
11421148
existing_tests_source=existing_tests,
11431149
generated_original_test_source=generated_tests_str,
11441150
function_trace_id=self.function_trace_id[:-4] + exp_type

0 commit comments

Comments
 (0)