Skip to content

Commit 3221bee

Browse files
config values
1 parent ace7524 commit 3221bee

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

codeflash/code_utils/config_consts.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
REPAIR_UNMATCHED_PERCENTAGE_LIMIT = 0.4 # if the percentage of unmatched tests is greater than this, we won't fix it (lowering this value makes the repair more stricted)
3636
MAX_REPAIRS_PER_TRACE = 4 # maximum number of repairs we will do for each function
3737

38+
# Adaptive optimization
39+
# TODO (ali): make this configurable with effort arg once the PR is merged
40+
ADAPTIVE_OPTIMIZATION_THRESHOLD = 2 # Max adaptive optimizations per single candidate tree (for example : optimize -> refine -> adaptive -> another adaptive).
41+
MAX_ADAPTIVE_OPTIMIZATIONS_PER_TRACE = 4 # maximum number of adaptive optimizations we will do for each function (this can be 2 adaptive optimizations for 2 candidates for example)
42+
3843
MAX_N_CANDIDATES = 5
3944
MAX_N_CANDIDATES_LP = 6
4045

codeflash/optimization/function_optimizer.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@
4343
unified_diff_strings,
4444
)
4545
from codeflash.code_utils.config_consts import (
46+
ADAPTIVE_OPTIMIZATION_THRESHOLD,
4647
COVERAGE_THRESHOLD,
4748
INDIVIDUAL_TESTCASE_TIMEOUT,
49+
MAX_ADAPTIVE_OPTIMIZATIONS_PER_TRACE,
4850
MAX_REPAIRS_PER_TRACE,
4951
N_TESTS_TO_GENERATE_EFFECTIVE,
5052
REFINE_ALL_THRESHOLD,
@@ -385,6 +387,7 @@ def __init__(
385387
self.future_all_code_repair: list[concurrent.futures.Future] = []
386388
self.future_adaptive_optimizations: list[concurrent.futures.Future] = []
387389
self.repair_counter = 0 # track how many repairs we did for each function
390+
self.adaptive_optimization_counter = 0 # track how many adaptive optimizations we did for each function
388391

389392
def can_be_optimized(self) -> Result[tuple[bool, CodeOptimizationContext, dict[Path, str]], str]:
390393
should_run_experiment = self.experiment_id is not None
@@ -843,7 +846,6 @@ def process_single_candidate(
843846

844847
logger.info(f"h3|Optimization candidate {candidate_index}/{total_candidates}:")
845848
candidate = candidate_node.candidate
846-
print(f" {' -> '.join([c.source for c in candidate_node.path_to_root()])}")
847849
code_print(
848850
candidate.source_code.flat,
849851
file_name=f"candidate_{candidate_index}.py",
@@ -999,6 +1001,7 @@ def determine_best_candidate(
9991001
self.future_all_code_repair.clear()
10001002
self.future_adaptive_optimizations.clear()
10011003
self.repair_counter = 0
1004+
self.adaptive_optimization_counter = 0
10021005

10031006
ai_service_client = self.aiservice_client if exp_type == "EXP0" else self.local_aiservice_client
10041007
assert ai_service_client is not None, "AI service client must be set for optimization"
@@ -1086,16 +1089,20 @@ def call_adaptive_optimize(
10861089
eval_ctx: CandidateEvaluationContext,
10871090
ai_service_client: AiServiceClient,
10881091
) -> concurrent.futures.Future[OptimizedCandidate | None] | None:
1092+
if self.adaptive_optimization_counter >= MAX_ADAPTIVE_OPTIMIZATIONS_PER_TRACE:
1093+
logger.debug(
1094+
f"Max adaptive optimizations reached for {self.function_to_optimize.qualified_name}: {self.adaptive_optimization_counter}"
1095+
)
1096+
return None
1097+
10891098
prev_candidates = candidate_node.path_to_root()
10901099
if len(prev_candidates) == 1:
10911100
# we already have the refinement going for this single candidate tree, no need to do adaptive optimize
10921101
return None
10931102

10941103
adaptive_count = sum(1 for c in prev_candidates if c.source == OptimizedCandidateSource.ADAPTIVE)
1095-
# is_candidate_refined = any(c.source == OptimizedCandidateSource.REFINE for c in prev_candidates)
10961104

1097-
# TODO (ali): make this configurable with effort arg
1098-
if adaptive_count >= 2:
1105+
if adaptive_count >= ADAPTIVE_OPTIMIZATION_THRESHOLD:
10991106
return None
11001107

11011108
request_candidates = []
@@ -1117,6 +1124,7 @@ def call_adaptive_optimize(
11171124
request = AIServiceAdaptiveOptimizeRequest(
11181125
trace_id=trace_id, original_source_code=original_source_code, candidates=request_candidates
11191126
)
1127+
self.adaptive_optimization_counter += 1
11201128
return self.executor.submit(ai_service_client.adaptive_optimize, request=request)
11211129

11221130
def repair_optimization(

0 commit comments

Comments
 (0)