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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### JetBrains+iml template
|
2 |
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
3 |
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
4 |
+
|
5 |
+
# User-specific stuff
|
6 |
+
.idea/**/workspace.xml
|
7 |
+
.idea/**/tasks.xml
|
8 |
+
.idea/**/usage.statistics.xml
|
9 |
+
.idea/**/dictionaries
|
10 |
+
.idea/**/shelf
|
11 |
+
|
12 |
+
# AWS User-specific
|
13 |
+
.idea/**/aws.xml
|
14 |
+
|
15 |
+
# Generated files
|
16 |
+
.idea/**/contentModel.xml
|
17 |
+
|
18 |
+
# Sensitive or high-churn files
|
19 |
+
.idea/**/dataSources/
|
20 |
+
.idea/**/dataSources.ids
|
21 |
+
.idea/**/dataSources.local.xml
|
22 |
+
.idea/**/sqlDataSources.xml
|
23 |
+
.idea/**/dynamic.xml
|
24 |
+
.idea/**/uiDesigner.xml
|
25 |
+
.idea/**/dbnavigator.xml
|
26 |
+
|
27 |
+
# Gradle
|
28 |
+
.idea/**/gradle.xml
|
29 |
+
.idea/**/libraries
|
30 |
+
|
31 |
+
# Gradle and Maven with auto-import
|
32 |
+
# When using Gradle or Maven with auto-import, you should exclude module files,
|
33 |
+
# since they will be recreated, and may cause churn. Uncomment if using
|
34 |
+
# auto-import.
|
35 |
+
# .idea/artifacts
|
36 |
+
# .idea/compiler.xml
|
37 |
+
# .idea/jarRepositories.xml
|
38 |
+
# .idea/modules.xml
|
39 |
+
# .idea/*.iml
|
40 |
+
# .idea/modules
|
41 |
+
# *.iml
|
42 |
+
# *.ipr
|
43 |
+
|
44 |
+
# CMake
|
45 |
+
cmake-build-*/
|
46 |
+
|
47 |
+
# Mongo Explorer plugin
|
48 |
+
.idea/**/mongoSettings.xml
|
49 |
+
|
50 |
+
# File-based project format
|
51 |
+
*.iws
|
52 |
+
|
53 |
+
# IntelliJ
|
54 |
+
out/
|
55 |
+
|
56 |
+
# mpeltonen/sbt-idea plugin
|
57 |
+
.idea_modules/
|
58 |
+
|
59 |
+
# JIRA plugin
|
60 |
+
atlassian-ide-plugin.xml
|
61 |
+
|
62 |
+
# Cursive Clojure plugin
|
63 |
+
.idea/replstate.xml
|
64 |
+
|
65 |
+
# SonarLint plugin
|
66 |
+
.idea/sonarlint/
|
67 |
+
|
68 |
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
69 |
+
com_crashlytics_export_strings.xml
|
70 |
+
crashlytics.properties
|
71 |
+
crashlytics-build.properties
|
72 |
+
fabric.properties
|
73 |
+
|
74 |
+
# Editor-based Rest Client
|
75 |
+
.idea/httpRequests
|
76 |
+
|
77 |
+
# Android studio 3.1+ serialized cache file
|
78 |
+
.idea/caches/build_file_checksums.ser
|
79 |
+
|
80 |
+
### Python template
|
81 |
+
# Byte-compiled / optimized / DLL files
|
82 |
+
__pycache__/
|
83 |
+
*.py[cod]
|
84 |
+
*$py.class
|
85 |
+
|
86 |
+
# C extensions
|
87 |
+
*.so
|
88 |
+
|
89 |
+
# Distribution / packaging
|
90 |
+
.Python
|
91 |
+
build/
|
92 |
+
develop-eggs/
|
93 |
+
dist/
|
94 |
+
downloads/
|
95 |
+
eggs/
|
96 |
+
.eggs/
|
97 |
+
lib/
|
98 |
+
lib64/
|
99 |
+
parts/
|
100 |
+
sdist/
|
101 |
+
var/
|
102 |
+
wheels/
|
103 |
+
share/python-wheels/
|
104 |
+
*.egg-info/
|
105 |
+
.installed.cfg
|
106 |
+
*.egg
|
107 |
+
MANIFEST
|
108 |
+
|
109 |
+
# PyInstaller
|
110 |
+
# Usually these files are written by a python script from a template
|
111 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
112 |
+
*.manifest
|
113 |
+
*.spec
|
114 |
+
|
115 |
+
# Installer logs
|
116 |
+
pip-log.txt
|
117 |
+
pip-delete-this-directory.txt
|
118 |
+
|
119 |
+
# Unit test / coverage reports
|
120 |
+
htmlcov/
|
121 |
+
.tox/
|
122 |
+
.nox/
|
123 |
+
.coverage
|
124 |
+
.coverage.*
|
125 |
+
.cache
|
126 |
+
nosetests.xml
|
127 |
+
coverage.xml
|
128 |
+
*.cover
|
129 |
+
*.py,cover
|
130 |
+
.hypothesis/
|
131 |
+
.pytest_cache/
|
132 |
+
cover/
|
133 |
+
|
134 |
+
# Translations
|
135 |
+
*.mo
|
136 |
+
*.pot
|
137 |
+
|
138 |
+
# Django stuff:
|
139 |
+
*.log
|
140 |
+
local_settings.py
|
141 |
+
db.sqlite3
|
142 |
+
db.sqlite3-journal
|
143 |
+
|
144 |
+
# Flask stuff:
|
145 |
+
instance/
|
146 |
+
.webassets-cache
|
147 |
+
|
148 |
+
# Scrapy stuff:
|
149 |
+
.scrapy
|
150 |
+
|
151 |
+
# Sphinx documentation
|
152 |
+
docs/_build/
|
153 |
+
|
154 |
+
# PyBuilder
|
155 |
+
.pybuilder/
|
156 |
+
target/
|
157 |
+
|
158 |
+
# Jupyter Notebook
|
159 |
+
.ipynb_checkpoints
|
160 |
+
|
161 |
+
# IPython
|
162 |
+
profile_default/
|
163 |
+
ipython_config.py
|
164 |
+
|
165 |
+
# pyenv
|
166 |
+
# For a library or package, you might want to ignore these files since the code is
|
167 |
+
# intended to run in multiple environments; otherwise, check them in:
|
168 |
+
# .python-version
|
169 |
+
|
170 |
+
# pipenv
|
171 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
172 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
173 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
174 |
+
# install all needed dependencies.
|
175 |
+
#Pipfile.lock
|
176 |
+
|
177 |
+
# poetry
|
178 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
179 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
180 |
+
# commonly ignored for libraries.
|
181 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
182 |
+
#poetry.lock
|
183 |
+
|
184 |
+
# pdm
|
185 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
186 |
+
#pdm.lock
|
187 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
188 |
+
# in version control.
|
189 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
190 |
+
.pdm.toml
|
191 |
+
.pdm-python
|
192 |
+
.pdm-build/
|
193 |
+
|
194 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
195 |
+
__pypackages__/
|
196 |
+
|
197 |
+
# Celery stuff
|
198 |
+
celerybeat-schedule
|
199 |
+
celerybeat.pid
|
200 |
+
|
201 |
+
# SageMath parsed files
|
202 |
+
*.sage.py
|
203 |
+
|
204 |
+
# Environments
|
205 |
+
.env
|
206 |
+
.venv
|
207 |
+
env/
|
208 |
+
venv/
|
209 |
+
ENV/
|
210 |
+
env.bak/
|
211 |
+
venv.bak/
|
212 |
+
|
213 |
+
# Spyder project settings
|
214 |
+
.spyderproject
|
215 |
+
.spyproject
|
216 |
+
|
217 |
+
# Rope project settings
|
218 |
+
.ropeproject
|
219 |
+
|
220 |
+
# mkdocs documentation
|
221 |
+
/site
|
222 |
+
|
223 |
+
# mypy
|
224 |
+
.mypy_cache/
|
225 |
+
.dmypy.json
|
226 |
+
dmypy.json
|
227 |
+
|
228 |
+
# Pyre type checker
|
229 |
+
.pyre/
|
230 |
+
|
231 |
+
# pytype static type analyzer
|
232 |
+
.pytype/
|
233 |
+
|
234 |
+
# Cython debug symbols
|
235 |
+
cython_debug/
|
236 |
+
|
237 |
+
# PyCharm
|
238 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
239 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
240 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
241 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
242 |
+
.idea/
|
243 |
+
|
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import circuit_transformer as ct
|
4 |
+
|
5 |
+
circuit_transformer = ct.CircuitTransformer()
|
6 |
+
aiger_str = """aag 33 8 0 2 25
|
7 |
+
2\n4\n6\n8\n10\n12\n14\n16\n58\n67
|
8 |
+
18 13 16\n20 19 7\n22 21 15\n24 3 9\n26 25 11
|
9 |
+
28 27 17\n30 3 6\n32 29 31\n34 29 32\n36 23 35
|
10 |
+
38 7 36\n40 10 29\n42 41 32\n44 13 15\n46 42 45
|
11 |
+
48 47 21\n50 39 49\n52 4 45\n54 25 53\n56 54 5
|
12 |
+
58 51 57\n60 45 12\n62 18 61\n64 63 19\n66 48 64
|
13 |
+
"""
|
14 |
+
|
15 |
+
def predict(input_text, mcts_playouts):
|
16 |
+
if input_text[-1] != '\n':
|
17 |
+
input_text += '\n'
|
18 |
+
try:
|
19 |
+
aig, info = ct.read_aiger(aiger_str=input_text)
|
20 |
+
except Exception as e:
|
21 |
+
raise gr.Error("Aiger format error!")
|
22 |
+
num_inputs, num_outputs = info[1], info[3]
|
23 |
+
if not isinstance(aig, list):
|
24 |
+
aig = [aig]
|
25 |
+
if num_inputs > 8:
|
26 |
+
raise gr.Error("Number of inputs should <= 8")
|
27 |
+
if num_outputs > 2:
|
28 |
+
raise gr.Error("Number of outputs should <= 2")
|
29 |
+
mcts_playouts = int(mcts_playouts)
|
30 |
+
optimized_aigs = circuit_transformer.optimize(
|
31 |
+
aigs=[aig],
|
32 |
+
num_mcts_steps=1 if mcts_playouts > 0 else 0,
|
33 |
+
num_mcts_playouts_per_step=mcts_playouts
|
34 |
+
)
|
35 |
+
optimized_aig = optimized_aigs[0]
|
36 |
+
input_tt = ct.compute_input_tt(num_inputs)
|
37 |
+
aig_tts = ct.compute_tts(aig, input_tt=input_tt)
|
38 |
+
aig_tts_str = "\n".join([tt.to01() for tt in aig_tts])
|
39 |
+
optimized_aig_tts = ct.compute_tts(optimized_aig, input_tt=input_tt)
|
40 |
+
optimized_aig_tts_str = "\n".join([tt.to01() for tt in optimized_aig_tts])
|
41 |
+
num_ands = pd.DataFrame(data={
|
42 |
+
"circuit": ["Original", "Optimized"],
|
43 |
+
"size": [ct.count_num_ands(aig), ct.count_num_ands(optimized_aig)]
|
44 |
+
})
|
45 |
+
ct.plot_network(aig, view=False, filename="aig.png")
|
46 |
+
ct.plot_network(optimized_aig, view=False, filename="optimized_aig.png")
|
47 |
+
return ct.write_aiger(optimized_aig), num_ands, "aig.png", "optimized_aig.png", aig_tts_str, optimized_aig_tts_str, str(ct.cec(aig, optimized_aig))
|
48 |
+
|
49 |
+
gradio_app = gr.Interface(
|
50 |
+
predict,
|
51 |
+
inputs=[gr.Code(value=aiger_str, label="Input And-Inverter Graph (Aiger format, #(inputs) <= 8, #(outputs) <= 2)"),
|
52 |
+
gr.Slider(minimum=0, maximum=8, value=0, step=1, label="Monte-Carlo tree search steps")],
|
53 |
+
outputs=[gr.Code(label="Optimized And-Inverter Graph (Aiger format)"),
|
54 |
+
gr.BarPlot(label="Circuit Size Comparison", x="circuit", y="size", sort="-x"),
|
55 |
+
gr.Image(label="Original And-Inverter Graph"),
|
56 |
+
gr.Image(label="Optimized And-Inverter Graph"),
|
57 |
+
gr.Text(label="Truth table of the original circuit"),
|
58 |
+
gr.Text(label="Truth table of the optimized circuit"),
|
59 |
+
gr.Text(label="Equivalence"),],
|
60 |
+
title="Circuit Transformer for Size Minimization",
|
61 |
+
description="""This is a demo to show how a [Circuit Transformer](https://openreview.net/forum?id=kpnW12Lm9p)
|
62 |
+
minimize the size of a logic circuit by next token prediction, while strictly preserving logical equivalence."""
|
63 |
+
)
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
gradio_app.launch()
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
graphviz
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
circuit-transformer
|
2 |
+
pandas
|