Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added strategy_generator/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions strategy_generator/feedback_loop.py
Original file line number Diff line number Diff line change
@@ -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
56 changes: 42 additions & 14 deletions strategy_generator/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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()
28 changes: 28 additions & 0 deletions strategy_generator/strategies/generated_optimization_strategy.py
Original file line number Diff line number Diff line change
@@ -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)