forked from stevehoover/conversion-to-TLV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
1248 lines (1076 loc) · 48.8 KB
/
convert.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# A script for refactoring a Verilog module, then converting it to TL-Verilog.
# The refactoring steps are performed by an LLM such as ChatGPT-4 via its API.
# Manual refactoring is also possible. All refactoring steps are formally verified using SymbiYosys.
# Usage:
# python3 convert.py
# This begins or continues the conversion process for the only *.v file in the current directory.
# This script works with these files:
# - <module_name>_orig.v: The trusted Verilog module to convert. This is the original file for the current conversion step.
# - <module_name>.v: The current WIP refactored/modified Verilog module, against which FEV will be run.
# - prompt_id.txt: A file containing the ID number of the prompt for this step. (The actual prompt may have been modified by the human.)
# - messages.json: The messages to be sent to the LLM API (as in the ChatGPT API).
# Additionally, these files may be created and captured in the process:
# - tmp/fev.sby: The FEV script for this conversion job.
# - <module_name>_prep.v: The file sent to the LLM API.
# - <module_name>_llm.v: The LLM output file.
# - llm_response.txt: The LLM response file.
#
# A history of all refactoring steps is stored in history/#, where "#" is the "refactoring step", starting with history/1.
# This directory is initialized when the step is begun, and fully populated when the refactoring change is accepted.
# Contents includes:
# - history/#/prompt_id.txt: (on init) The ID of the prompt used for this refactoring step. (The actual prompt may have been modified by the human.)
# - history/#/<module_name>.v: The refactored file at each step.
# - history/#/messages.json: The messages sent to the LLM API for each step.
# Although Git naturally captures a history, it may be desirable to capture this folder in Git, simply for convenience, since it may be desirable to
# easily examine file differences or to rework the conversion steps after the fact.
#
# Each refactoring step may involve a number of individual code modifications, recorded in a modification history within the refactoring step directory.
# Each modification is captured, whether accepted, rejected, or reverted.
#
# A modification is stored in history/#/mod_#/ (where # are sequential numbers).
# Contents include:
# - history/#/mod_#/<module_name>.v: The modified Verilog file.
# - history/#/mod_#/messages.json: The messages sent to the LLM API (for LLM modifications only).
# - history/#/mod_#/status.json: Metadata about the modification, as below, written after testing.
#
# history/#/mod_0 are checkpoints of the initial code for each refactoring step. Thus, history/1/mod_0/<module_name>.v is the initial
# code for the entire conversion.
#
# history/#/mod_# can also be a symlink to a prior history/#/mod_#, recording a code reversion. A reversion will not reference
# another reversion.
#
# The status.json file reflects the status of the modification, updated as fields become known:
# {
# "by": "human"|"llm",
# "compile": "passed"|"failed" (or non-existent if not compiled),
# "fev": "passed"|"failed" (or non-existent if not run),
# "incomplete": true|false A sticky field (held for each checkpoint of the refactoring step) assigned or updated by each LLM run,
# indicating whether the LLM response was incomplete.
# "modified": true|false (or non-existent if not run) Indicates whether the code from the LLM was actually modified.
# "accepted": true|non-existent Exists as true for the final modification of a refactoring step that was accepted.
# }
#
# With each rejected refactoring step, a new candidate is captured under a new candidate number under the next history number directory.
#
# <repo>/prompts.json contains the default prompts used for refactoring steps as a JSON array of objects with the following fields:
# - desc: a brief description of the refactoring step
# - prompt: prompt string
#
# When launched, this script first determines the current state of the conversions process. This state is:
# - The current candidate:
# - The current refactoring step, which is the latest history/#.
# - The next candidate number, which is the next history/#/mod_#
# - The next prompt ID, which is the ID of the prompt for the current refactoring step. This is the next prompt ID following the
# most recent that can be found in history/#/.
# Note that history/#/mod_#/ can be traced backward to determine what has been done so far.
#
# This is a command-line utility which prompts the user for input. Edits to <module_name>.v and/or prompt.txt can be made while input is pending.
# It is suggested to have <module_name>.v and prompt.txt open in an editor and in a diff utility, such as meld, while running this script. Users
# must be careful to save files before responding to prompts.
#
# To begin each step, the user is given instructions and prompted for input.
# The user makes edits and enters commands until a candidate is accepted or rejected, and the process repeats.
### eqy ###
# To ensure the functional equivalence of Verilog files, we compare an original Verilog file with its modified version. This is accomplished using the Equivalence Check tool, "Equivalence.eqy".
# The original Verilog file is located in history/#/mod_#.
# The modified Verilog file is located in CONVERSION-TO-TLV/.
# Use "Equivalence.eqy" to perform the equivalence check between the original and modified Verilog files.
# Upon running the equivalence check, an eqy_configuration_updated directory will be created containing all the updated configuration files.
# The eqy_configuration_updated.eqy file will be created inside the eqytmp/ directory.
# After executing a new equivalence check, the previous eqy_configuration_updated configuration folder will be moved to the eqytmp/ directory to preserve past configurations.
import os
import subprocess
from openai import OpenAI
import sys
import termios
import tty
import atexit
import signal
from select import select
from abc import ABC, abstractmethod
import json
import re
import shutil
###################################
# Abstract Base Class for LLM API #
###################################
class LLM_API(ABC):
name = "LLM"
model = None
def __init__(self):
pass
def setModel(self, model):
self.model = model
# Run the LLM API on the prompt file, producing a (TL-)Verilog file.
@abstractmethod
def run(self):
pass
# A class responsible for bundling messages objects into text and visa versa.
# This class isolates the format of LLM messages from the functionality and enables message formats to be used
# that are optimized for the LLM.
class MessageBundler:
# Convert the given object to text.
# The object format is:
# {
# "desc": "This is a description.",
# "prompt": "This is a prompt.\n\nIt has multiple lines."
# }
@abstractmethod
def obj_to_content(self, json):
pass
# Convert the given LLM response text into an object of the form:
# {
# "overview": "This is an overview.",
# "verilog": "This is the Verilog code.",
# "notes": "These are notes.",
# "issues": "These are issues.",
# "modified": true,
# "incomplete": true
# }
@abstractmethod
def content_to_obj(self, content):
pass
# Add Verilog to last message to be sent to the API.
# messages: The messages.json object in OpenAI format.
# verilog: The current Verilog file contents.
@abstractmethod
def add_verilog(self, messages, verilog):
pass
class OpenAI_API(LLM_API):
name = "OpenAI"
model = "gpt-3.5-turbo"
def __init__(self):
super().__init__()
# if OPENAI_API_KEY env var does not exist, get it from ~/.openai/key.txt or input prompt.
if not os.getenv("OPENAI_API_KEY"):
key_file_name = os.path.expanduser("~/.openai/key.txt")
if os.path.exists(key_file_name):
with open(key_file_name) as file:
os.environ["OPENAI_API_KEY"] = file.read()
else:
os.environ["OPENAI_API_KEY"] = input("Enter your OpenAI API key: ")
# Use an organization in the request if one is provided, either in the OPENAI_ORG_ID env var or in ~/.openai/org_id.txt.
self.org_id = os.getenv("OPENAI_ORG_ID")
if not self.org_id:
org_file_name = os.path.expanduser("~/.openai/org_id.txt")
if os.path.exists(org_file_name):
with open(org_file_name) as file:
self.org_id = file.read()
# Init OpenAI.
self.client = OpenAI() if self.org_id is None else OpenAI(organization=self.org_id)
#self.models = self.client.models.list()
def setModel(self, model):
# TODO:...
#if model not in self.models.data...:
# print("Error: Model " + model + " not found.")
# sys.exit(1)
self.model = model
# Set up the initial messages object for the current refactoring step based on the given system message and prompt
# (from this step's prompt.txt).
def initPrompt(self, system, message):
return [
{"role": "system", "content": system},
{"role": "user", "content": message}
]
# Run the LLM API on the messages.json file appended with the verilog code, returning the response string from the LLM.
def run(self, messages, verilog):
# Add verilog to the last message.
message_bundler.add_verilog(messages, verilog)
# Call the API.
print("Calling " + self.model + "...")
# TODO: Not supported in ChatGPT-3.5: response_format = {"type": "json_object"}
api_response = self.client.chat.completions.create(model=self.model, messages=messages, max_tokens=500, temperature=0.0)
print("Response received from " + self.model)
# Parse the response.
try:
response_str = api_response.choices[0].message.content
finish_reason = api_response.choices[0].finish_reason
completion_tokens = api_response.usage.completion_tokens
print("API response finish reason: " + finish_reason)
print("API response completion tokens: " + str(completion_tokens))
except Exception as e:
print("Error: API response is invalid.")
print(str(e))
sys.exit(1)
return response_str
# Response fields.
sticky_response_fields = {"clock", "reset", "assertion"} # ("incomplete" is also sticky, but only between LLM runs, so it has special treatment.)
legal_response_fields = sticky_response_fields | {"overview", "verilog", "modified", "incomplete", "issues", "notes", "plan"}
class PseudoMarkdownMessageBundler(MessageBundler):
# Convert the given object to a pseudo-Markdown format. Markdown syntax is familiar to the LLM, and fields can be
# provided without any awkward escaping and other formatting, as described in default_system_message.txt.
# Example JSON:
# {"instructions": "These are instructions.", "verilog": "module...\nendmodule"}
# Example output:
# ## Instructions
#
# These are instructions.
#
# ## Verilog
#
# ```
# module...
# endmodule
# ```
def obj_to_request(self, obj):
content = "# Request\n\n"
separator = ""
for key in obj:
# Convert (single-word) key to title case.
name = key[0].upper() + key[1:]
content += separator + "## " + name + "\n\n" + obj[key]
separator = "\n\n"
return content
# Convert the given LLM API response string from the pseudo-Markdown format requested into an object, as described
# in default_system_message.txt.
def response_to_obj(self, response):
# Parse the response, line by line, looking for second-level Markdown header lines.
lines = response.split("\n")
# Parse "# Response" header.
if not re.match(r"^# Response$", lines[0]):
print("Warning: API response is missing \"# Response\" header.")
else:
lines = lines[1:]
if not re.match(r"^\s*$", lines[0]):
print("Warning: API response is missing blank line after \"# Response\" header.")
else:
lines = lines[1:]
l = 0
fields = {}
field = None
while l < len(lines):
# Parse body lines until the next field header or end of message.
body = ""
separator = ""
while l < len(lines) and not lines[l].startswith("## "):
if (body != "") or (re.match(r"^\s*$", lines[l]) is None): # Ignore leading blank lines.
body += separator + lines[l]
separator = "\n"
l += 1
# Found header line or EOM.
# Process the body field that ended.
# Strip trailing whitespace.
body = re.sub(r"\s*$", "", body)
if field is None:
if body != "":
print("Error: The following body text was found before the first header and will be ignored:")
print(body)
else:
# "verilog" field should be in block quotes. Confirm, and remove them.
if field == "verilog":
body, n = re.subn(r"^```\n(.*)\n+```$", r"\1\n", body, flags=re.DOTALL)
if n != 1:
print("Warning: The \"Verilog\" field of the response was not contained in block quotes and may be malformed.")
# Capture the previous field.
# Boolean responses.
if body == "true" or body == "false":
body = body == "true"
fields[field] = body
if l < len(lines):
# Parse the header line with a regular expression.
field = re.match(r"## +(\w+)", lines[l]).group(1)
# The field name should be a single upper-case word.
if not re.match(r"[A-Z][a-z]*", field):
print("Error: The following non-standard field was found in the response:")
print(field)
# Convert field name to lower case.
field = field.lower()
# Check for legal field name.
if field not in legal_response_fields:
print("Warning: The following non-standard field was found in the response:")
print(field)
# Done with this header line.
l += 1
return fields
# Add Verilog to last message to be sent to the API.
# messages: The messages.json object in OpenAI format.
# verilog: The current Verilog file contents.
def add_verilog(self, messages, verilog):
# Add verilog to the last message.
messages[-1]["content"] += "\n\n## Verilog\n\n```" + verilog + "```"
def changes_pending():
return os.path.exists(mod_path() + "/" + working_verilog_file_name) and diff(working_verilog_file_name, mod_path() + "/" + working_verilog_file_name)
# See if there were any manual edits to the Verilog file and capture them in the history if so.
def checkpoint_if_pending():
# if latest mod file exists and is different from working file, checkpoint it.
if changes_pending():
print("Manual edits were made and are being checkpointed.")
checkpoint({ "by": "human" })
# Checkpoint any manual edits, run LLM, and checkpoint the result if successful. Return nothing.
# messages: The messages.json object in OpenAI format.
# verilog: The current Verilog file contents.
def run_llm(messages, verilog):
checkpoint_if_pending()
# Run the LLM, passing the messages.json and verilog file contents.
# Confirm.
print("")
print("The following prompt will be sent to the API together with the Verilog and prior messages:")
print("")
print(messages[-1]["content"])
print("")
press_any_key()
# If there is already a response, prompt the user about possibly reusing it.
ch = "n"
if os.path.exists("llm_response.txt"):
print("There is already a response to this prompt. Would you like to reuse it [y/N]?")
print("> ", end="")
ch = getch()
print("")
if ch == "y":
# Use llm_response.txt.
with open("llm_response.txt") as file:
response_str = file.read()
else:
# Call the API.
response_str = llm_api.run(messages, verilog)
# Write llm_response.txt.
with open("llm_response.txt", "w") as file:
file.write(response_str)
response_obj = message_bundler.response_to_obj(response_str)
# Commented code here is for requesting a JSON object response from the API, which is not the current approach.
#
## LLM tends to respond with multi-line strings, which are not valid JSON. Fix this.
#response_json = response_json.replace("\n", "\\n")
#try:
# response = json.loads(response_json)
#except:
# print("Error: API response was invalid JSON:")
# print(response_json)
# sys.exit(1)
# Response should include "modified", but if it is missing and "verilog" is present, assume "modified" is True.
if "modified" not in response_obj and "verilog" in response_obj:
response_obj["modified"] = True
print("Warning: API response is missing \"modified\" field. Assuming \"modified\" is True.")
if (response_obj.get("modified", False) and "verilog" not in response_obj) or "modified" not in response_obj:
print("Error: API response fields are incomplete or inconsistent.")
sys.exit(1)
# Confirm.
print("")
print("The following response was received from the API, to replace the Verilog file:")
print("")
# Reformat the JSON into multiple lines and extract the verilog for cleaner printing.
code = response_obj.get("verilog")
if code:
del response_obj["verilog"]
print(json.dumps(response_obj, indent=4))
if code:
print("-------------")
print(code)
print("-------------")
# Repare the response.
response_obj["verilog"] = code
print("")
press_any_key()
if "notes" in response_obj:
print("Notes:\n " + response_obj["notes"].replace("\n", "\n ") + "\n")
modified = response_obj["modified"] # As reported by the LLM, and updated to reflect reality.
if modified:
code = response_obj["verilog"]
# Get working code.
with open(working_verilog_file_name) as file:
working_code = file.read()
# Write the resulting Verilog file.
with open(working_verilog_file_name, "w") as file:
file.write(code)
# Confirm that there were in fact changes.
if code == working_code:
print("Note: No changes were made, though the API response indicates otherwise. (Checkpointing anyway.)")
modified = False
else:
print("Checkpointing changes.")
else:
# LLM says no changes.
print("No changes were made for this refactoring step. (Checkpointing anyway.)")
# Checkpoint, whether modified or not.
orig_status = readStatus()
status = { "by": "llm", "incomplete": response_obj.get("incomplete", False), "modified": modified }
if not modified:
# Reflect FEV and compile status from prior checkpoint.
status["compile"] = orig_status.get("compile")
status["fev"] = orig_status.get("fev")
# Apply sticky fields to status.
for field in sticky_response_fields:
if field in response_obj:
status[field] = response_obj[field]
checkpoint(status)
# Response accepted, so delete llm_response.txt.
os.remove("llm_response.txt")
if "issues" in response_obj:
print(llm_api.model + " reports the following issues:")
print(" " + response_obj["issues"].replace("\n", "\n ") + "\n")
return response_obj
#############
# Constants #
#############
llm_api = OpenAI_API()
message_bundler = PseudoMarkdownMessageBundler()
# Get the directory of this script.
repo_dir = os.path.dirname(os.path.realpath(__file__))
#
# Find FEV script.
#
fev_script = repo_dir + "/" + "fev.sby"
if not os.path.exists(fev_script):
print("Error: Conversion repository does not contain " + fev_script + ".")
usage()
# Read prompts.json.
# prompts.json is a slight extension to JSON to support newlines in strings. Lines beginning with "+" continue a string with an implied newline.
with open(repo_dir + "/prompts.json") as file:
raw_contents = file.read()
json_str = raw_contents.replace("\n+", "\\n")
prompts = json.loads(json_str)
####################
# Helper functions #
####################
# Report a usage message.
def usage():
print("Usage: python3 .../convert.py")
print(" Call from a directory containing a single Verilog file to convert or a \"history\" directory.")
sys.exit(0)
# Determine if a filename has a Verilog/SystemVerilog extension.
def is_verilog(filename):
return filename.endswith(".v") or filename.endswith(".sv")
# Run SymbiYosys.
def run_sby():
subprocess.run(["sby", "-f", "tmp/fev.sby"])
# Run FEV using Yosys on the given top-level module name and orig and modified files.
# Return the subprocess.CompletedProcess of the FEV command.
def run_yosys_fev(module_name, orig_file_name, modified_file_name):
env = {"TOP_MODULE": module_name, "ORIGINAL_VERILOG_FILE": orig_file_name, "MODIFIED_VERILOG_FILE": modified_file_name}
return subprocess.run(["/home/owais/yosys/yosys", repo_dir + "/fev.tcl"], env=env)
# Functions that determine the state of the refactoring step based on the state of the files.
# TODO: replace?
def llm_passed():
return os.path.exists(llm_verilog_file_name)
def llm_finished():
return not readStatus().get("incomplete", True)
def fev_passed():
return os.path.exists("fev/PASS") and os.system("diff " + module_name + ".v fev/src/" + module_name + ".v") == 0
def diff(file1, file2):
return os.system("diff -q '" + file1 + "' '" + file2 + "' > /dev/null") != 0
# Capture Verilog file in a new history/#/mod_#/, and if this was an LLM modification, capture messages.json.
# status: The status to save with the checkpoint.
# Sticky status is applied from current status. Status["incomplete"] will be carried over from the prior checkpoint for non-LLM updates.
def checkpoint(status):
global mod_num
# Carry over sticky status from the prior checkpoint.
for field in sticky_response_fields:
if field not in status and field in readStatus():
status[field] = readStatus()[field]
if status.get("by") != "llm" and not (readStatus().get("incomplete") is None):
status["incomplete"] = readStatus()["incomplete"]
# Capture the current Verilog file.
mod_num += 1
os.mkdir(mod_path())
os.system("cp " + working_verilog_file_name + " history/" + str(refactoring_step) + "/mod_" + str(mod_num) + "/")
# Capture messages.json if this was an LLM modification.
if status.get("by") == "llm":
os.system("cp messages.json history/" + str(refactoring_step) + "/mod_" + str(mod_num) + "/")
# Write status.json.
writeStatus(status)
# Create a reversion checkpoint as a symlink, or if the previous change was a reversion, update its symlink.
def checkpoint_reversion(prev_mod):
global mod_num
if os.path.islink(mod_path()):
os.remove(mod_path())
else:
mod_num += 1
os.symlink("mod_" + str(prev_mod), mod_path())
def readStatus(mod = None):
# Default mod to mod_num
if mod is None:
mod = mod_num
# Read status from latest history change directory.
try:
with open(mod_path(mod) + "/status.json") as file:
return json.load(file)
except:
return {}
def writeStatus(status):
# Write status to latest history change directory.
with open(mod_path() + "/status.json", "w") as file:
json.dump(status, file)
# Print the main user prompt.
def print_prompt():
print("The next refactoring step (" + str(refactoring_step) + ") for the LLM uses prompt " + str(prompt_id) + ":\n")
# Output the README for the current prompt ID, indented by 5 spaces.
print(" | " + prompts[prompt_id]["desc"].replace("\n", "\n | "))
print(" ")
print(" Make edits and enter command characters until a candidate is accepted or rejected. Generally, the sequence is:")
print(" - (optional) Make any desired manual edits to " + working_verilog_file_name + " and/or prompt.txt.")
print(" - l: (optional) Run the LLM step. (If this fails or is incomplete, make any further manual edits and try again.)")
print(" - (optional) Make any desired manual edits to " + working_verilog_file_name + ". (You may use \"f\" to run FEV first.)")
print(" - f: Run FEV. (If this fails, make further manual Verilog edits and try again.)")
print(" - y: Accept the current code as the completion of this refactoring step.")
print(" (At any time: use \"n\" to undo changes; \"h\" for help; \"x\" to exit.)")
print(" ")
print(" Enter one of the following commands:")
print(" l: LLM. Send the current prompt.txt to the LLM....Run this refactoring step in ChatGPT-4/Claude2 (if LLM not already completed).")
print(" f: Run FEV on the current code.")
print(" y: Yes. Accept the current code as the completion of this refactoring step (if FEV already run and passed).")
print(" u: Undo. Revert to a previous version of the code.")
print(" U: Redo. Reapply a reverted code change (possible until next modification or exit).")
print(" c: Checkpoint the current human edits in the history.")
print(" h: History. Show a history of recent changes in this refactoring step.")
print(" ?: Help. Repeat this message.")
print(" x: Exit.")
# Function to initialize the conversion directory for the next refactoring step.
def init_refactoring_step():
global refactoring_step, mod_num, prompt_id
# Get sticky status from current refactoring step before creating next.
old_status = {}
if refactoring_step > 0:
old_status = readStatus()
refactoring_step += 1
mod_num = -1
# Initialize the prompt.
prompt_id += 1
with open("prompt_id.txt", "w") as file:
file.write(str(prompt_id))
# Make history/# directory and populate it.
os.mkdir("history/" + str(refactoring_step))
os.system("cp prompt_id.txt history/" + str(refactoring_step) + "/")
# Also, create an initial mod_0 directory populated with initial verilog and status.json indicating initial code.
status = { "initial": True, "fev": "passed" }
# Apply sticky status from last refactoring step.
for field in sticky_response_fields:
if field in old_status:
status[field] = old_status[field]
checkpoint(status)
# (mod_num now 0)
# Initialize messages.json.
try:
# Read the system message from <repo>/default_system_message.txt.
with open(repo_dir + "/default_system_message.txt") as file:
system = file.read()
with open("messages.json", "w") as file:
message = message_bundler.obj_to_request({'prompt': prompts[prompt_id]["prompt"]})
json.dump(llm_api.initPrompt(system, message), file, indent=4)
except Exception as e:
print("Error: Failed to initialize messages.json due to: " + str(e))
sys.exit(1)
# Evaluate the given anonymous function, fn(mod), from the most recent modification to the least recent until fn indicates completion.
# fn(mod) returns False to keep iterating or True to terminate.
# Return the terminating mod number or None.
def most_recent(fn, mod=None):
# Default mod to mod_num
if mod is None:
mod = mod_num
while mod >= 0:
mod = actual_mod(mod)
if fn(mod):
return mod
mod -= 1
return None
# Run FEV against the last successfully FEVed code (if not in this refactoring step, the the original code for this step).
# Update status.json.
def run_fev():
checkpoint_if_pending()
status = readStatus()
# Get the most recently FEVed code (mod with status["fev"] == "passed").
last_fev_mod = most_recent(lambda mn: (readStatus(mn).get("fev") == "passed"))
assert(last_fev_mod is not None)
# FEV vs. last successful FEV.
orig_file_name = mod_path(last_fev_mod) + "/" + working_verilog_file_name
print("Running FEV against " + orig_file_name + ". Diff:")
print("==================")
diff_status = os.system("diff " + orig_file_name + " " + working_verilog_file_name)
print("==================")
ret = False
# Run FEV.
if diff_status == 0:
print("No changes to FEV. Choose a different command.")
ret = True
else:
# Run FEV.
# Create fev.sby.
# This is done by copying in <repo>/fev.sby and substituting "{MODULE_NAME}", "{ORIGINAL_FILE}", and "{MODIFIED_FILE}" using sed.
os.system(f"cp {fev_script} tmp/fev.sby")
os.system(f"sed -i 's/<MODULE_NAME>/{module_name}/g' tmp/fev.sby")
# These paths must be absolute.
os.system(f"sed -i 's|<ORIGINAL_FILE>|{os.getcwd()}/{orig_file_name}|g' tmp/fev.sby")
os.system(f"sed -i 's|<MODIFIED_FILE>|{os.getcwd()}/{working_verilog_file_name}|g' tmp/fev.sby")
# To run the above manually in bash, as a one-liner from the conversion directory, providing <MODULE_NAME>, <ORIGINAL_FILE>, and <MODIFIED_FILE>:
# cp ../fev.sby fev.sby && sed -i 's/<MODULE_NAME>/<module_name>/g' fev.sby && sed -i "s|<ORIGINAL_FILE>|$PWD/<original_file>|g" fev.sby && sed -i "s|<MODIFIED_FILE>|$PWD/<modified_file>|g" fev.sby
#run_sby()
proc = run_yosys_fev(module_name, orig_file_name, working_verilog_file_name)
# Check for success.
#passed = fev_passed()
passed = proc.returncode == 0
if passed:
print("FEV passed.")
status["fev"] = "passed"
else:
print("Error: FEV failed. Try again.")
status["fev"] = "failed"
writeStatus(status)
# TODO: If failed, bundle failure info for LLM, and call LLM (with approval).
ret = passed
return ret
# Number of the most recent modification (that actually made a change) or None.
def most_recent_mod():
return most_recent(lambda mod: (readStatus(mod).get("modified", False)))
# The path of the latest modification of this refactoring step.
def mod_path(mod = None):
# Default mod to mod_num
if mod is None:
mod = mod_num
return "history/" + str(refactoring_step) + "/mod_" + str(mod)
# Show a diff between the given (or current) modification and the previous one.
# Return true is shown, or false if there is no previous modification.
def show_diff(mod = None, prev_mod = None):
# Default mod to mod_num
if mod is None:
mod = mod_num
mod = actual_mod(mod)
# Get the previous modification.
if prev_mod is None:
prev_mod = most_recent(lambda mn: (mn < mod), mod)
if prev_mod is None:
print("There is no previous modification.")
return False
# Show the diff.
print("Diff between mod_" + str(prev_mod) + " and mod_" + str(mod) + ":")
print("==================")
os.system("diff " + mod_path(prev_mod) + "/" + working_verilog_file_name + " " + mod_path(mod) + "/" + working_verilog_file_name)
print("==================")
return True
def execute_eqy_commands(verilog1_file, verilog2_file, module_name1, module_name2, eqy_config_file):
# Create the temp directory
temp_directory = "eqytmp"
if os.path.exists(temp_directory):
shutil.rmtree(temp_directory)
os.makedirs(temp_directory)
with open(eqy_config_file, "r") as f:
eqy_config_content = f.read()
# Replace placeholders in the configuration content
eqy_config_content = eqy_config_content.replace("{MODULE_NAME1}", module_name1)
eqy_config_content = eqy_config_content.replace("{MODULE_NAME2}", module_name2)
eqy_config_content = eqy_config_content.replace("<ORIGINAL_VERILOG_FILE>", os.path.abspath(verilog2_file))
eqy_config_content = eqy_config_content.replace("<MODIFIED_VERILOG_FILE>", os.path.abspath(verilog1_file))
# Define the name of the updated EQY configuration file within the temp_directory
updated_eqy_config_file = os.path.join(temp_directory, "eqy_configuration_updated.eqy")
with open(updated_eqy_config_file, "w") as f:
f.write(eqy_config_content)
# Define the eqy_directory within the temp_directory
eqy_directory = os.path.join(temp_directory, "eqy_configuration_updated")
# Remove and recreate the eqy_directory
if os.path.exists(eqy_directory):
shutil.rmtree(eqy_directory)
shutil.rmtree('eqy_configuration_updated')
os.makedirs(eqy_directory)
# Move any existing files in the previous eqy_configuration_updated directory to the new one in the temp_directory
prev_eqy_directory = "eqy_configuration_updated"
if os.path.exists(prev_eqy_directory) and os.path.isdir(prev_eqy_directory):
for filename in os.listdir(prev_eqy_directory):
src_file = os.path.join(prev_eqy_directory, filename)
dest_file = os.path.join(eqy_directory, filename)
shutil.move(src_file, dest_file)
# Remove the now empty old_eqy_directory
shutil.rmtree(prev_eqy_directory)
# Execute the EQY command
try:
subprocess.run(["eqy", updated_eqy_config_file], check=True)
except subprocess.CalledProcessError as e:
print(f"Error executing EQY command: {e}")
# Extract module name of verilog file
def extract_module_name(verilog_file):
# Regular expression to match the module declaration
module_pattern = re.compile(r"^\s*module\s+(\w+)\s*\(")
with open(verilog_file, "r") as f:
for line in f:
# Check if the line matches the module declaration pattern
match = module_pattern.match(line)
if match:
module_name = match.group(1)
# print(f"Module Name: {module_name}")
return module_name
# Main function to run the EQY command
def run_eqy():
verilog_files = [f for f in os.listdir() if f.endswith(".v")]
if not verilog_files:
print("Error: No Verilog files found in the current directory.")
sys.exit(1)
# if current verilog file doenot exist
current_verilog_file = working_verilog_file_name
if not os.path.exists(current_verilog_file):
print(f"Error: Working Verilog file '{current_verilog_file}' not found.")
sys.exit(1)
last_fev_mod = most_recent(lambda mn: (readStatus(mn).get("fev") == "passed"))
# if no previous modification with FEV passed is found
if last_fev_mod is None:
print("Error: No previous modification found with FEV passed.")
sys.exit(1)
original_verilog_file = mod_path(last_fev_mod) + "/" + working_verilog_file_name
module_name1 = extract_module_name(current_verilog_file)
module_name2 = extract_module_name(original_verilog_file)
eqy_config_file ="/home/owais/conversion-to-TLV/equivalence.eqy" # give path of eqy file
execute_eqy_commands(current_verilog_file, original_verilog_file, module_name1, module_name2, eqy_config_file)
##################
# Terminal input #
##################
def set_raw_mode(fd):
attrs = termios.tcgetattr(fd) # get current attributes
attrs[3] = attrs[3] & ~termios.ICANON # clear ICANON flag
termios.tcsetattr(fd, termios.TCSANOW, attrs) # set new attributes
def set_cooked_mode(fd):
attrs = termios.tcgetattr(fd) # get current attributes
attrs[3] = attrs[3] | termios.ICANON # set ICANON flag
termios.tcsetattr(fd, termios.TCSANOW, attrs) # set new attributes
# Set to default cooked mode (in case the last run was exited in raw mode).
set_cooked_mode(sys.stdin.fileno())
def getch():
## Save the current terminal settings
#old_settings = termios.tcgetattr(sys.stdin)
try:
# Set the terminal to raw mode
set_raw_mode(sys.stdin.fileno())
# Wait for input to be available
[i], _, _ = select([sys.stdin.fileno()], [], [], None)
# Read a single character
ch = sys.stdin.read(1)
finally:
# Restore the terminal settings
set_cooked_mode(sys.stdin.fileno())
return ch
## Capture the current terminal settings before setting raw mode
#default_settings = termios.tcgetattr(sys.stdin)
##old_settings = termios.tcgetattr(sys.stdin)
def cleanup():
print("Exiting cleanly.")
# Set the terminal settings to the default settings
#termios.tcsetattr(sys.stdin, termios.TCSADRAIN, default_settings)
#set_cooked_mode(sys.stdin.fileno())
# Register the cleanup function
atexit.register(cleanup)
# Accept terminal input command character from among the given list.
def get_command(options):
while True:
print("\nPress one of the following command keys: {}".format(", ".join(options)))
print("> ", end="")
ch = getch()
print("")
if ch not in options:
print("Error: Invalid key. Try again.")
else:
return ch
# Catch signals for proper cleanup.
# Define a handler for signals that will perform cleanup
def signal_handler(signum, frame):
print(f"Caught signal {signum}, exiting...")
sys.exit(1)
# Register the signal handler for as many signals as possible.
for sig in [signal.SIGABRT, signal.SIGINT, signal.SIGTERM]:
signal.signal(sig, signal_handler)
# Pause for a key press.
def press_any_key():
print("Press any key to continue...\n> ", end="")
getch()
######################
# #
# Main entry point #
# #
######################
###########################
# Parse command-line args #
###########################
# (None)
##################
# Initialization #
##################
#
# Determine file names.
#
# Find the Verilog file to convert, ending in ".v" or ".sv" as the shortest Verilog file in the directory.
files = [f for f in os.listdir(".") if is_verilog(f)]
if len(files) != 1 and not os.path.exists("history"):
print("Error: There must be exactly one Verilog file or a \"history\" directory in the current working directory.")
usage()
# Choose the shortest Verilog file name as the one to convert.
file_name_len = 1000
working_verilog_file_name = None
for file in files:
if len(file) < file_name_len:
file_name_len = len(file)
working_verilog_file_name = file
if not working_verilog_file_name:
print("Error: No Verilog file found in current working directory.")
usage()
# Derived file names.
module_name = working_verilog_file_name.split(".")[0]
orig_verilog_file_name = module_name + "_orig.v"
llm_verilog_file_name = module_name + "_llm.v"
####################
# Initialize state #
####################
#
# Determine which refactoring step we are on
#
# Current state variables.
refactoring_step = 0 # The current refactoring step (history/<refactoring_step>).
mod_num = 0 # The current mod number (history/#/mod_<mod_num>).
prompt_id = 0 # The current prompt ID (prompt_id.txt).
if not os.path.exists("history"):
# Initialize the conversion job.
os.mkdir("history")
if not os.path.exists("tmp"):
os.mkdir("tmp")
init_refactoring_step()
else:
# Determine the current state of the conversion process.
# Find the current refactoring step.
for step in os.listdir("history"):
refactoring_step = max(refactoring_step, int(step))
# Find the current modification number.
for dir in os.listdir("history/" + str(refactoring_step)):
if dir.startswith("mod_"):
mod_num = max(mod_num, int(dir.split("_")[1]))
# Get the prompt ID from the most recent prompt_id.txt file. Look back through the history directories until/if one is found.
cn = refactoring_step
while cn >= 0 and prompt_id == 0:
if os.path.exists("history/" + str(cn) + "/prompt_id.txt"):
with open("history/" + str(cn) + "/prompt_id.txt") as f:
prompt_id = int(f.read())
cn -= 1
# Get the actual modification of the given modification number (or current). In other words, if the given mod is a
# reversion, follow the symlink.
def actual_mod(mod=None):
if mod is None:
mod = mod_num
if os.path.islink(mod_path(mod)):
tmp1 = os.readlink(mod_path(mod))[4:]
tmp2 = int(tmp1)
return tmp2
else:
return mod
###############
# #
# Main loop #
# #
###############