Skip to content

Commit 30c8988

Browse files
committed
give throughput context to explanations
1 parent 515b976 commit 30c8988

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

codeflash/api/aiservice.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ def get_new_explanation( # noqa: D417
298298
annotated_tests: str,
299299
optimization_id: str,
300300
original_explanation: str,
301+
original_throughput: str | None = None,
302+
optimized_throughput: str | None = None,
303+
throughput_improvement: str | None = None,
301304
) -> str:
302305
"""Optimize the given python code for performance by making a request to the Django endpoint.
303306
@@ -314,6 +317,9 @@ def get_new_explanation( # noqa: D417
314317
- annotated_tests: str - test functions annotated with runtime
315318
- optimization_id: str - unique id of opt candidate
316319
- original_explanation: str - original_explanation generated for the opt candidate
320+
- original_throughput: str | None - throughput for the baseline code (operations per second)
321+
- optimized_throughput: str | None - throughput for the optimized code (operations per second)
322+
- throughput_improvement: str | None - throughput improvement percentage
317323
318324
Returns
319325
-------
@@ -333,6 +339,9 @@ def get_new_explanation( # noqa: D417
333339
"optimization_id": optimization_id,
334340
"original_explanation": original_explanation,
335341
"dependency_code": dependency_code,
342+
"original_throughput": original_throughput,
343+
"optimized_throughput": optimized_throughput,
344+
"throughput_improvement": throughput_improvement,
336345
}
337346
logger.info("Generating explanation")
338347
console.rule()

codeflash/models/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class BestOptimization(BaseModel):
100100
winning_benchmarking_test_results: TestResults
101101
winning_replay_benchmarking_test_results: Optional[TestResults] = None
102102
line_profiler_test_results: dict
103+
async_throughput: Optional[int] = None
103104

104105

105106
@dataclass(frozen=True)

codeflash/optimization/function_optimizer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ def determine_best_candidate(
635635
replay_performance_gain=replay_perf_gain if self.args.benchmark else None,
636636
winning_benchmarking_test_results=candidate_result.benchmarking_test_results,
637637
winning_replay_benchmarking_test_results=candidate_result.benchmarking_test_results,
638+
async_throughput=candidate_result.async_throughput,
638639
)
639640
valid_optimizations.append(best_optimization)
640641
# queue corresponding refined optimization for best optimization
@@ -697,6 +698,7 @@ def determine_best_candidate(
697698
replay_performance_gain=valid_opt.replay_performance_gain,
698699
winning_benchmarking_test_results=valid_opt.winning_benchmarking_test_results,
699700
winning_replay_benchmarking_test_results=valid_opt.winning_replay_benchmarking_test_results,
701+
async_throughput=valid_opt.async_throughput,
700702
)
701703
valid_candidates_with_shorter_code.append(new_best_opt)
702704
diff_lens_list.append(
@@ -1281,6 +1283,23 @@ def process_review(
12811283
original_runtimes_all=original_runtime_by_test,
12821284
optimized_runtimes_all=optimized_runtime_by_test,
12831285
)
1286+
original_throughput_str = None
1287+
optimized_throughput_str = None
1288+
throughput_improvement_str = None
1289+
1290+
if (
1291+
self.function_to_optimize.is_async
1292+
and original_code_baseline.async_throughput is not None
1293+
and best_optimization.async_throughput is not None
1294+
):
1295+
original_throughput_str = f"{original_code_baseline.async_throughput} operations/second"
1296+
optimized_throughput_str = f"{best_optimization.async_throughput} operations/second"
1297+
throughput_improvement_value = throughput_gain(
1298+
original_throughput=original_code_baseline.async_throughput,
1299+
optimized_throughput=best_optimization.async_throughput,
1300+
)
1301+
throughput_improvement_str = f"{throughput_improvement_value * 100:.1f}%"
1302+
12841303
new_explanation_raw_str = self.aiservice_client.get_new_explanation(
12851304
source_code=code_context.read_writable_code.flat,
12861305
dependency_code=code_context.read_only_context_code,
@@ -1294,6 +1313,9 @@ def process_review(
12941313
annotated_tests=generated_tests_str,
12951314
optimization_id=best_optimization.candidate.optimization_id,
12961315
original_explanation=best_optimization.candidate.explanation,
1316+
original_throughput=original_throughput_str,
1317+
optimized_throughput=optimized_throughput_str,
1318+
throughput_improvement=throughput_improvement_str,
12971319
)
12981320
new_explanation = Explanation(
12991321
raw_explanation_message=new_explanation_raw_str or explanation.raw_explanation_message,

0 commit comments

Comments
 (0)