Skip to content

Commit 3f7f879

Browse files
author
Sean Heelan
committed
Initial import. Support for findfaster.
0 parents  commit 3f7f879

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

.flake8

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
max-line-length=120

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv
2+
__pycache__/

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"python.linting.flake8Enabled": true,
3+
"python.linting.pylintEnabled": false,
4+
"python.linting.enabled": true
5+
}

perf-gpt.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
3+
# perf-gpt is an experimental tool for performance analysis and optimisation
4+
# using LLMs. Its purpose is to take data from existing profilers and provide
5+
# the user with helpful summaries, advice and direction.
6+
#
7+
# Author: Sean Heelan
8+
# Email: sean@heelan.io
9+
10+
from perfgpt.commands import (
11+
findfaster,
12+
topn
13+
)
14+
15+
import argparse
16+
import os
17+
import sys
18+
19+
import openai
20+
21+
from dotenv import load_dotenv
22+
load_dotenv()
23+
24+
openai.api_key = os.environ["OPENAI_API_KEY"]
25+
26+
commands = {
27+
findfaster.command: findfaster,
28+
topn.command: topn
29+
}
30+
31+
parser = argparse.ArgumentParser(
32+
prog=sys.argv[0],
33+
description="Performance analysis and optimisation with LLMs"
34+
)
35+
parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
36+
parser.add_argument("-d", "--debug", action="store_true", help="Debug output")
37+
parser.add_argument("--temperature", type=float, default=0, help="ChatGPT temperature. See OpenAI docs.")
38+
39+
subparsers = parser.add_subparsers(help="sub-command help", dest="command")
40+
for v in commands.values():
41+
v.add_to_command_parser(subparsers)
42+
43+
args = parser.parse_args()
44+
45+
if not args.command:
46+
parser.print_help(sys.stderr)
47+
sys.stderr.write("\nNo sub-command selected\n")
48+
sys.exit(1)
49+
50+
if args.command not in commands:
51+
parser.print_help(sys.stderr)
52+
sys.stderr.write("\nUnknown sub-command\n")
53+
sys.exit(1)
54+
55+
sys.exit(commands[args.command].run(parser, args))

perfgpt/commands/findfaster.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import openai
2+
3+
command = "findfaster"
4+
help = "Try to find a faster version of the provided library or program"
5+
6+
7+
def add_to_command_parser(subparsers):
8+
parser = subparsers.add_parser(command)
9+
parser.add_argument("-t", "--software-type", choices=["program", "library", "pylibrary"], default="library",
10+
help="Specify the type of software. Not necessary, but can lead to better results.")
11+
parser.add_argument("target", help="The program or library to find a faster version of")
12+
13+
14+
software_type_prompts = {
15+
"program": """What are the fastest and most memory-efficient programs that provide
16+
the same functionality as {target}, specifically those that use SIMD instructions
17+
or are optimized for high performance? Output the suggested programs in a list.
18+
For each program, give a summary.""",
19+
"library": """What are the fastest and most memory-efficient libraries that provide
20+
the same functionality as {target}, specifically those that use SIMD instructions
21+
or are optimized for high performance? Output the suggested libraries in a list.
22+
For each library, give a summary.""",
23+
"pylibrary": """What are the fastest and most memory-efficient Python libraries that
24+
provide the same functionality as Python's {target} library, specifically those that use
25+
SIMD instructions or are optimized for high performance? I'm also interested in any
26+
lightweight, low-level libraries that may provide better performance than pure-Python
27+
libraries. Output the suggested libraries in a list. For each library, give a summary."""
28+
}
29+
30+
31+
def run(args_parser, args):
32+
target = args.target
33+
temp = args.temperature
34+
35+
prompt = software_type_prompts[args.software_type]
36+
37+
completion = openai.ChatCompletion.create(
38+
model="gpt-3.5-turbo",
39+
temperature=temp,
40+
messages=[
41+
{
42+
"role": "system",
43+
"content": """You are perf-gpt, a helpful assistant for performance analysis and optimisation
44+
of software. Answer as concisely as possible."""
45+
},
46+
{
47+
"role": "user",
48+
"content": prompt.format(target=target)
49+
}
50+
]
51+
)
52+
53+
result = completion['choices'][0]['message']['content']
54+
print(result)
55+
return 0

perfgpt/commands/topn.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
command = "topn"
2+
help = "Analyse Top-N output from a profiler"
3+
4+
5+
def add_to_command_parser(subparsers):
6+
parser = subparsers.add_parser(command)
7+
parser.add_argument("-f", help="File containing the Top N data")

0 commit comments

Comments
 (0)