diff --git a/strategy_generator/__init__.py b/strategy_generator/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/strategy_generator/feedback_loop.py b/strategy_generator/feedback_loop.py index e69de29..fcdd2d5 100644 --- a/strategy_generator/feedback_loop.py +++ b/strategy_generator/feedback_loop.py @@ -0,0 +1,16 @@ +import sys +import io + +error_buf = io.StringIO() +sys.stderr = error_buf + +try: + from strategies import generated_optimization_strategy +except Exception: + print("Error: ", error_buf.getvalue()) + +def pipeline(): + return + +def get_performance_metric(): + return \ No newline at end of file diff --git a/strategy_generator/interface.py b/strategy_generator/interface.py index e8b64f4..70c29a4 100644 --- a/strategy_generator/interface.py +++ b/strategy_generator/interface.py @@ -7,8 +7,17 @@ from dotenv import load_dotenv +import feedback_loop + MODEL_NAME = "gpt-4-turbo-preview" +tol = 1e-3 +curr_perf_metric = 1000 +prev_perf_metric = 0 + +iteration = 0 +max_iterations = 10 + # Load environment variables from .env file load_dotenv(override=True) @@ -72,6 +81,9 @@ def extract_python_code(response_content): return response_content[code_start + len("```python"):code_end].strip() else: return None + +def diff(curr_perf_metric, prev_perf_metric): + return abs(curr_perf_metric - prev_perf_metric) def main(): parser = argparse.ArgumentParser(description='CLI for OpenAI GPT-4 Turbo') @@ -105,23 +117,39 @@ def main(): with open(os.path.join(examples_dir, filename), 'r') as file: examples += f"\n******** EXAMPLE {filename} ********\n\n" + file.read() + "\n" - # build optimization strategy - optim_strat = inference(cuda_kernel, examples=examples) - optim_strat_python_code = extract_python_code(optim_strat) + while (diff(curr_perf_metric, prev_perf_metric) > tol) or (iteration < max_iterations): + # build optimization strategy + optim_strat = inference(cuda_kernel, examples=examples) + optim_strat_python_code = extract_python_code(optim_strat) - if optim_strat_python_code: - output_dir = "strategy_generator/strategies/" - if not os.path.exists(output_dir): - os.makedirs(output_dir) + if optim_strat_python_code: + output_dir = "strategy_generator/strategies/" + if not os.path.exists(output_dir): + os.makedirs(output_dir) + + file_name = "generated_optimization_strategy.py" + + with open(os.path.join(output_dir, file_name), 'w') as file: + file.write(optim_strat_python_code) + + print(f"saved at: {os.path.join(output_dir, file_name)}") + else: + print("No Python code block was found in the response.") + + # Run the feedback loop + feedback_loop.pipeline() + + # Read error buffer + error_output = feedback_loop.error_buf.getvalue() + + if error_output: + print(f"Error: {error_output}") + + # # Get the performance metric + # prev_perf_metric = curr_perf_metric + # curr_perf_metric = feedback_loop.get_performance_metric() - file_name = "generated_optimization_strategy.py" - with open(os.path.join(output_dir, file_name), 'w') as file: - file.write(optim_strat_python_code) - - print(f"saved at: {os.path.join(output_dir, file_name)}") - else: - print("No Python code block was found in the response.") if __name__ == '__main__': main() diff --git a/strategy_generator/strategies/generated_optimization_strategy.py b/strategy_generator/strategies/generated_optimization_strategy.py new file mode 100644 index 0000000..0b72008 --- /dev/null +++ b/strategy_generator/strategies/generated_optimization_strategy.py @@ -0,0 +1,28 @@ +import numpy as np +import random +from kernel_tuner import util +from kernel_tuner.searchspace import Searchspace +from kernel_tuner.strategies.common import CostFunc + +_options = dict( + popsize=("Population size", 20), + maxiter=("Maximum number of iterations", 100), + w=("Inertia weight constant", 0.5), + c1=("Cognitive constant", 2.0), + c2=("Social constant", 1.0), + crossover_rate=("Rate at which crossover is applied", 0.8), + mutation_rate=("Rate at which mutation is applied", 0.1) +) + +class HybridParticle: + def __init__(self, position): + self.position = position + self.velocity = np.random.uniform(-1, 1, len(position)) + self.best_pos = position.copy() + self.best_score = float('inf') + self.score = float('inf') + +def tune(searchspace: Searchspace, runner, tuning_options): + + options = tuning_options.strategy_options + num_particles, maxiter, w, c1, c2,crossover_rate ,mutation_rate= common.get_options(options,_options) \ No newline at end of file