-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossbar.py
288 lines (231 loc) · 10.6 KB
/
crossbar.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
'''
Author: CSuperlei
Date: 2024-02-15 20:39:13
LastEditTime: 2024-02-15 21:38:41
Description:
'''
import os
import json
import numpy as np
import numba as nb
import math
import warnings
import time
warnings.filterwarnings('ignore')
class Addr_manager:
def __init__(self, config_path) -> None:
self.config = Config(config_path)
self.bcapa = self.config.dict["bank_capacity"]
self.acapa = self.config.dict["array_capacity"]
self.bnum = self.config.dict["bank_number"]
self.anum = self.config.dict["crossbar_number"]
self.acol = self.config.dict["crossbar_col"]
self.arow = self.config.dict["crossbar_row"]
self.ccapa = self.bcapa - self.acapa * self.anum
self.cucapa = self.config.dict["cache_unit_capacity"]
self.byte = self.config.dict["byte_unit"]
self.bucapa = self.config.dict["buffer_capacity"]
class Config:
def __init__(self, config_file_path) -> None:
self.dict = {}
self.file = config_file_path
self.load_config_file()
def load_config_file(self):
curr_dir = os.path.dirname(os.path.realpath(__file__))
config_path = os.path.join(curr_dir, self.file)
with open(config_path, "r") as f:
tmp_config_dict = json.load(f)
for config_name, config_value in tmp_config_dict.items():
self.dict[config_name] = config_value
return
class Module:
def __init__(self, config_path) -> None:
self.config = Config(config_path)
self.addrManager = Addr_manager("addr.json")
self.power = 0
self.latency = 0
def process(self, **kwargs):
return self.work(**kwargs)
def work(self, **kwargs):
return
def load_config(self, ):
self.config.load_config_file()
for k, v in self.config.dict.items():
setattr(self, k, v)
class Device:
def __init__(self, rram_datatype, rram_resistance, rram_high_resis, rram_low_resis) -> None:
self.rram_data_type = rram_datatype
self.rram_resistance = rram_resistance
self.rram_high_resis = rram_high_resis
self.rram_low_resis = rram_low_resis
class Transistor:
def __init__(self) -> None:
pass
class MemCell:
def __init__(self, rram_datatype, rram_resistance, rram_high_resis, rram_low_resis) -> None:
self.mem_cell = Device(rram_datatype, rram_resistance, rram_high_resis, rram_low_resis)
def write_cell(self, target_res):
self.mem_cell.rram_resistance = target_res
def read_cell(self):
return self.mem_cell.rram_resistance
class DAC:
def __init__(self, dac_inputbit, dac_power, dac_latency, dac_level, dac_area) -> None:
self.dac_inputbit = dac_inputbit
self.dac_power = dac_power
self.dac_latency = dac_latency
self.dac_level = dac_level
self.dac_area = dac_area
self.dac_value = None
def work(self, input):
self.dac_value = input
class ADC:
def __init__(self, adc_power, adc_latency, adc_level, adc_area, adc_outbit) -> None:
self.adc_power = adc_power
self.adc_latency = adc_latency
self.adc_level = adc_level
self.adc_area = adc_area
self.adc_outbit = adc_outbit
def work(self, input):
res = input
return res
class SA:
def __init__(self, sa_threshold, sa_power, sa_latency, sa_area) -> None:
self.sa_threshold = sa_threshold
self.sa_power = sa_power
self.sa_latency = sa_latency
self.sa_area = sa_area
def work(self, input):
res = input
return res
class CrossBar(Module):
def __init__(self, config_path) -> None:
super().__init__(config_path)
self.load_config()
start = time.time()
self.crossbar, self.cross_colflag, self.cross_rowflag = self._construct_crossbar()
self.dac = self._construct_dac()
self.adc = self._construct_adc()
self.sa = self._construct_sa()
def _construct_crossbar(self):
crossbar = []
crossbar_colflag = [True for _ in range(self.cross_colnum)]
crossbar_rowflag = [True for _ in range(self.cross_rownum)]
total_cell_num = self.cross_rownum * self.cross_colnum
for _ in range(total_cell_num):
crossbar.append(MemCell(self.rram_datatype, self.rram_resistance, self.rram_high_resis, self.rram_low_resis ))
crossbar = np.array(crossbar).reshape((self.cross_rownum, self.cross_colnum))
return crossbar, crossbar_colflag, crossbar_rowflag
def _construct_dac(self):
dac = []
for _ in range(self.cross_rownum):
dac.append(DAC(self.dac_inputbit, self.dac_power, self.dac_latency, self.dac_level, self.dac_area))
dac = np.array(dac)
return dac
def _construct_adc(self):
adc = []
for _ in range(self.cross_colnum // self.cross_colreuse):
adc.append(ADC(adc_power=self.adc_power, adc_latency=self.adc_latency, adc_level=self.adc_level, adc_area=self.adc_area, adc_outbit=self.adc_outbit))
adc = np.array(adc)
return adc
def _construct_sa(self):
sa = []
for _ in range(self.cross_colnum):
sa.append(SA(sa_threshold=self.sa_threshold, sa_power=self.sa_power, sa_latency=self.sa_latency, sa_area=self.sa_area))
sa = np.array(sa)
return sa
def compute_area(self):
total_area = (self.adc_area + self.dac_area + self.sa_area ) * self.area_margin
return total_area
def _compute_cycle(self, value=None):
local_latency = math.ceil(value / self.cycle_time)
return local_latency
def _compute_power(self, row_index, col_index):
row_index = np.array(row_index)
col_index = np.array(col_index)
open_cell = np.sum(row_index) * np.sum(col_index)
close_cell = self.addrManager.acol * self.addrManager.arow - open_cell
open_cell = open_cell * self.cell_open_power
close_cell = close_cell * self.cell_close_power
return close_cell + open_cell + self.adc_power
def process(self, op_code, row_index=None, col_index=None, rram_resistance=None, input_cross=None):
self.log.update_latency(self.__class__.__name__, self.config.dict["latency"])
res = self.work(op_code, row_index, col_index, rram_resistance, input_cross)
self.log.update_power(self.__class__.__name__, res[2])
return res[0], res[1]
def work(self, op_code, row_index=None, col_index=None, rram_resistance=None, input_cross=None):
'''
op_code: crossbar operation
row_index: row index including: open row number index
col_index: column index including: open col number index
rram_resistance: write crossbar resistance
input_cross: input data in crossbar
'''
if op_code == "wt_cross":
'''
write crossbar
'''
index = -1
local_cycle_wt = 0
local_power_wt = 0
for h in range(len(row_index)):
for w in range(len(col_index)):
if row_index[h] == 1 and col_index[w] == 1 and self.cross_rowflag[h] and self.cross_colflag[w]:
index += 1
self.crossbar[h][w].write_cell(rram_resistance[index])
if rram_resistance[index] > 0.5:
local_cycle_wt += self.write_low_latency
local_power_wt + self.write_low_power
else:
local_cycle_wt += self.write_high_latency
local_power_wt + self.write_high_power
local_cycle_wt += self.cross_wirelatency
local_power_wt += self.wire_power
return None, self._compute_cycle(local_cycle_wt), local_power_wt
if op_code == "reset_cross":
for idx in range(len(row_index)):
if row_index[idx] == 1:
self.cross_rowflag[idx] = True
for idx in range(len(col_index)):
if col_index[idx] == 1:
self.cross_colflag[idx] = True
return None, 0, 0
if op_code == 'rd_cross':
'''
read crossbara
'''
local_cycle_rd = 0
local_power_rd = 0
for idx, r in enumerate(zip(self.cross_rowflag, row_index)):
if r[0] and r[1] == 1:
self.dac[idx].work(input_cross[idx])
local_cycle_rd += self.dac_latency
local_power_rd += self.dac_power
else:
self.dac[idx].work(0)
self.out_cross = np.zeros(self.cross_colnum)
for i in range(self.cross_rownum):
tmp_row = np.zeros(self.cross_colnum)
for j, r in enumerate(zip(self.cross_colflag, col_index)):
if r[0] and r[1] == 1:
tmp_row[j] = self.crossbar[i][j].read_cell()
if tmp_row[j] > 0.5:
local_power_rd += self.read_voltage ** 2 / self.rram_high_resis
else:
local_power_rd += self.read_voltage ** 2 / self.rram_low_resis
self.out_cross += self.dac[i].dac_value * tmp_row
local_cycle_rd += self.read_latency
if self.cross_isadc:
res = []
for idx, t in enumerate(range(0, self.cross_colnum, self.cross_colreuse)):
res.extend(self.adc[idx].work(self.out_cross[t : t + self.cross_colreuse]))
local_power_rd += self.adc_power
local_cycle_rd += self.adc_latency * self.cross_colreuse
return np.array(res), self._compute_cycle(local_cycle_rd), local_power_rd
else:
res = []
for idx in range(self.cross_colnum):
res.append(self.sa[idx].work(self.out_cross[idx]))
local_power_rd += self.sa_power
local_cycle_rd += self.sa_latency
return np.array(res), self._compute_cycle(local_cycle_rd), local_power_rd
return None