forked from huawei-noah/HEBO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_space.py
More file actions
148 lines (123 loc) · 4.34 KB
/
action_space.py
File metadata and controls
148 lines (123 loc) · 4.34 KB
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
# Copyright (C) 2022. Huawei Technologies Co., Ltd. All rights reserved. Redistribution and use in source and binary
# forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
# products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from subprocess import check_output
from typing import List, Dict
class Action:
""" abc action """
def __init__(self, act_id: str, act_str: str):
"""
Args:
act_id: action id
act_str: string used to apply the action
"""
self.act_id = act_id
self.act_str = act_str
def __repr__(self):
return f"{self.act_id} -> {self.act_str}"
class ActionSimple(Action):
""" Action for which act_str = act_id + ; """
def __init__(self, act_id: str):
"""
Args:
act_id: action id
"""
super().__init__(act_id=act_id, act_str=act_id + ';')
class ActionCompo(Action):
def __init__(self, act_id: str):
"""
Args:
act_id: action id
"""
act_str = f'&get -n; {act_id}; &put;'
super().__init__(act_id=act_id, act_str=act_str)
BALANCE = ActionSimple('balance')
REWRITE = ActionSimple('rewrite')
REWRITE_Z = ActionSimple('rewrite -z')
REFACTOR = ActionSimple('refactor')
REFACTOR_Z = ActionSimple('refactor -z')
RESUB = ActionSimple('resub')
RESUB_Z = ActionSimple('resub -z')
FRAIG = ActionSimple('fraig')
SOPB = ActionCompo('&sopb')
BLUT = ActionCompo('&blut')
DSDB = ActionCompo('&dsdb')
STRASH = ActionSimple('strash')
STD_ACTION_SPACE: List[Action] = [
REWRITE,
REWRITE_Z,
REFACTOR,
REFACTOR_Z,
RESUB,
RESUB_Z,
BALANCE
]
EXTENDED_ACTION_SPACE: List[Action] = [
REWRITE,
REWRITE_Z,
REFACTOR,
REFACTOR_Z,
RESUB,
RESUB_Z,
BALANCE,
FRAIG,
SOPB,
BLUT,
DSDB
]
STRASH_EXTENDED_ACTION_SPACE: List[Action] = [
REWRITE,
REWRITE_Z,
REFACTOR,
REFACTOR_Z,
RESUB,
RESUB_Z,
BALANCE,
FRAIG,
SOPB,
BLUT,
DSDB,
STRASH
]
ACTION_SPACES: Dict[str, List[Action]] = {
'standard': STD_ACTION_SPACE,
'extended': EXTENDED_ACTION_SPACE,
'strash_extended': STRASH_EXTENDED_ACTION_SPACE
}
if __name__ == '__main__':
import sys
from pathlib import Path
import os
ROOT_PROJECT = str(Path(os.path.realpath(__file__)).parent.parent)
sys.path[0] = ROOT_PROJECT
from DRiLLS.drills.fpga_session import FPGASession
for action_space in (EXTENDED_ACTION_SPACE, STD_ACTION_SPACE):
design_file = os.path.join(ROOT_PROJECT, 'data/epfl-benchmark/arithmetic/adder.v')
abc_binary = 'yosys-abc'
library_file = os.path.join(ROOT_PROJECT, './asap7.lib')
abc_command = f'read {library_file}; '
abc_command += f'read {design_file}; '
abc_command += 'strash; '
for action in action_space:
abc_command += action.act_str
abc_command += 'if -K 4;'
abc_command += 'print_stats;'
print(abc_command)
proc = check_output([abc_binary, '-c', abc_command])
print(FPGASession._get_metrics(proc))