Skip to content

Conversation

@aseembits93
Copy link
Contributor

@aseembits93 aseembits93 commented Aug 4, 2025

User description

I'm interleaving refinement generation with the candidate evaluation for better cpu utilization and non blocking calls. awaiting in the end when there is nothing else to do. This should save some time in the candidate evaluation loop.


PR Type

Enhancement


Description

  • Asynchronously submit refinement tasks in loop

  • Change refine_optimizations to return Future

  • Increase executor pool size to 3 workers

  • Remove blocking synchronous refinement logic


Diagram Walkthrough

flowchart LR
  A["determine_best_candidate start"]
  B["ThreadPoolExecutor with 3 workers"]
  C["Submit line profiler tasks"]
  D["Submit refine_optimizations futures"]
  E["Wait for refinement futures"]
  F["Extend candidates with refinements"]
  A --> B
  B --> C
  C --> D
  D --> E
  E --> F
Loading

File Walkthrough

Relevant files
Enhancement
function_optimizer.py
Enable async refinement task handling                                       

codeflash/optimization/function_optimizer.py

  • Increased ThreadPoolExecutor workers from 2 to 3
  • Collected refinement tasks as futures asynchronously
  • Removed blocking refinement logic at loop end
  • Updated refine_optimizations to return Future
+25/-24 

@aseembits93 aseembits93 changed the title async refinement calls for better queing CF-666 Non-Blocking Async refinement calls for better queing CF-666 Aug 4, 2025
@github-actions
Copy link

github-actions bot commented Aug 4, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Undefined Variable

line_profiler_done is used in a condition but never defined, which will raise a NameError at runtime.

if (not len(candidates)) and line_profiler_done:
Executor Lifecycle

Verify that the ThreadPoolExecutor created in the with block remains active until all submitted futures (including those from refine_optimizations) complete before shutting down.

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    ai_service_client = self.aiservice_client if exp_type == "EXP0" else self.local_aiservice_client
Inconsistent Logging

A raw print is used to log "Added candidates from refinement"; consider using the existing logger for consistent logging output.

print("Added candidates from refinement")

@github-actions
Copy link

github-actions bot commented Aug 4, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Replace undefined completion flag

The variable line_profiler_done is not defined and will raise a NameError. Instead,
detect completion by checking that no profiling future remains (e.g.,
future_line_profile_results is None). Replace the undefined flag with a concrete
condition.

codeflash/optimization/function_optimizer.py [560]

-if (not len(candidates)) and line_profiler_done:
+if not candidates and future_line_profile_results is None:
Suggestion importance[1-10]: 9

__

Why: The variable line_profiler_done is undefined, causing a NameError and blocking the refinement logic; replacing it addresses a critical bug.

High
Flatten nested refinement results

refinement_response is a list of lists returned from multiple futures, so extending
will insert lists rather than individual candidates. Flatten the nested lists to
properly add each candidate.

codeflash/optimization/function_optimizer.py [566]

-candidates.extend(refinement_response)
+for batch in refinement_response:
+    candidates.extend(batch)
Suggestion importance[1-10]: 7

__

Why: Using extend on a list of lists will insert lists instead of individual candidates, so flattening ensures each candidate is correctly added.

Medium
General
Use logger instead of print

Replace the use of print with the module’s logger to maintain consistent logging and
allow configurable log levels.

codeflash/optimization/function_optimizer.py [567]

-print("Added candidates from refinement")
+logger.info("Added candidates from refinement")
Suggestion importance[1-10]: 4

__

Why: Replacing print with logger.info improves consistency with existing logging and configurable log levels.

Low

@misrasaurabh1
Copy link
Contributor

i will let @KRRT7 review

@aseembits93
Copy link
Contributor Author

@KRRT7 ready to review, large diffs are due to changes in indentation

@aseembits93
Copy link
Contributor Author

closing as changes present in #615

@aseembits93 aseembits93 closed this Aug 7, 2025
@aseembits93 aseembits93 deleted the opt-candidate-loop branch August 17, 2025 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants