-
Notifications
You must be signed in to change notification settings - Fork 2
/
MNSIM_main.py
200 lines (180 loc) · 10.5 KB
/
MNSIM_main.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
#!/usr/bin/python
# -*-coding:utf-8-*-
from pickle import FALSE
import torch
import sys
import os
import math
import argparse
import numpy as np
import pandas as pd
import torch
import collections
import configparser
import copy
from importlib import import_module
from MNSIM.Interface.interface import *
from MNSIM.Accuracy_Model.Weight_update import weight_update
from MNSIM.Mapping_Model.Behavior_mapping import behavior_mapping
from MNSIM.Mapping_Model.Tile_connection_graph import TCG
from MNSIM.Latency_Model.Model_latency import Model_latency
from MNSIM.Area_Model.Model_Area import Model_area
from MNSIM.Power_Model.Model_inference_power import Model_inference_power
from MNSIM.Energy_Model.Model_energy import Model_energy
def Data_clean():
path = os.getcwd()
NoC_file = path + '/MNSIM/NoC/'
inj_file = 'inj_dir'
log_file = 'log'
res_file = 'Final_Results'
files = os.listdir(NoC_file)
for file in files:
if file == inj_file:
for target in os.listdir(NoC_file + inj_file):
os.remove(NoC_file + inj_file + '/' + target)
elif file == log_file:
for target in os.listdir(NoC_file + log_file):
os.remove(NoC_file + log_file + '/' + target)
elif file == res_file:
for target in os.listdir(NoC_file + res_file):
os.remove(NoC_file + res_file + '/' + target)
else:
continue
# print("Removed unnecessary file.")
def main(_model='vgg16', _tiles=24, _tiles_buildin=24, _noc_bw=32):
home_path = os.getcwd()
SimConfig_path = os.path.join(home_path, "rram_config.ini")
weights_file_path = os.path.join(home_path, "MNSIM/params/cifar10_vgg16_params.pth")
parser = argparse.ArgumentParser(description='MNSIM mora edition')
# default args
parser.add_argument("-AutoDelete", "--file_auto_delete", default=True, help="Whether delete the unnecessary files automatically")
parser.add_argument("-HWdes",
"--hardware_description",
default=SimConfig_path,
help="Hardware description file location & name, default:/mora/rram_config.ini")
parser.add_argument("-Weights",
"--weights",
default=weights_file_path,
help="NN model weights file location & name, default:/MNSIM_Python/cifar10_vgg18_params.pth")
parser.add_argument("-DisHW", "--disable_hardware_modeling", action='store_true', default=False, help="Disable hardware modeling, default: false")
parser.add_argument("-DisAccu", "--disable_accuracy_simulation", action='store_true', default=True, help="Disable accuracy simulation, default: false")
parser.add_argument("-SAF", "--enable_SAF", action='store_true', default=False, help="Enable simulate SAF, default: false")
parser.add_argument("-Var", "--enable_variation", action='store_true', default=False, help="Enable simulate variation, default: false")
parser.add_argument("-Rratio", "--enable_R_ratio", action='store_true', default=False, help="Enable simulate the effect of R ratio, default: false")
parser.add_argument("-FixRange",
"--enable_fixed_Qrange",
action='store_true',
default=True,
help="Enable fixed quantization range (max value), default: false")
parser.add_argument("-DisPipe",
"--disable_inner_pipeline",
action='store_true',
default=True,
help="Disable inner layer pipeline in latency modeling, default: false")
parser.add_argument("-D", "--device", default=1, help="Determine hardware device for simulation, default: CPU")
parser.add_argument("-DisModOut",
"--disable_module_output",
action='store_true',
default=True,
help="Disable module simulation results output, default: false")
parser.add_argument("-DisLayOut",
"--disable_layer_output",
action='store_true',
default=True,
help="Disable layer-wise simulation results output, default: false")
# mora args
parser.add_argument("--model", type=str, default='vgg16', help="NN model name, default: vgg16")
parser.add_argument("--tiles", nargs='+', type=int, default=24, help="tiles [row, col] of a chip")
parser.add_argument("--noc_bw", type=int, default=20)
parser.add_argument('--scenario', type=str, default='edge', choices=['embedded', 'edge', 'cloud'])
parser.add_argument('--dataflow', type=str, default='yxp_os', choices=['ykp_os', 'yxp_os', 'kcp_ws', 'xp_ws', 'rs'])
args = parser.parse_args()
if args.file_auto_delete:
# print("use the root mode by 'sudo -s'")
Data_clean()
else:
print("You should make sure that the files are removed which may cause confusions")
'''
print("Hardware description file location:", args.hardware_description)
print("Software model file location:", args.weights)
print("Whether perform hardware simulation:", not (args.disable_hardware_modeling))
print("Whether perform accuracy simulation:", not (args.disable_accuracy_simulation))
print("Whether consider SAFs:", args.enable_SAF)
print("Whether consider variations:", args.enable_variation)
if args.enable_fixed_Qrange:
print("Quantization range: fixed range (depends on the maximum value)")
else:
print("Quantization range: dynamic range (depends on the data distribution)")
'''
if __name__ != '__main__':
args.model = _model
args.tiles = _tiles
args.noc_bw = _noc_bw
tiles_buildin = _tiles_buildin
'''
if _on_RRAM_layer_index:
on_RRAM_layer_index = copy.deepcopy(_on_RRAM_layer_index) # on_RRAM_layer_index is in-order now, no need oRli2
elif _DSE_indicator == 0:
model_csv_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'model/' + args.model + '/' + args.model + '.csv'))
model_nd = pd.read_csv(model_csv_path).to_numpy()
model_layer_num = model_nd.shape[0]
on_RRAM_layer_index = range(model_layer_num)
else: # indicates no layers RRAM can excu
on_RRAM_layer_index = []
skip_simu = True
print('No layers RRAM can excute. Skip')
'''
__TestInterface = TrainTestInterface(network_module=args.model,
dataset_module='MNSIM.Interface.cifar10',
SimConfig_path=args.hardware_description,
weights_file=args.weights,
device=args.device)
structure_file = __TestInterface.get_structure()
# MNSIM_layer_index_list = copy.deepcopy(__TestInterface.MNSIM_layer_index_list)
# MNSIM invoke ------------------------------------------------->
TCG_mapping = TCG(structure_file, args.hardware_description, args.disable_inner_pipeline, tiles_buildin)
if not (args.disable_hardware_modeling):
__latency = Model_latency(NetStruct=structure_file, SimConfig_path=args.hardware_description, TCG_mapping=TCG_mapping, inter_tile_bandwidth=args.noc_bw)
if not (args.disable_inner_pipeline):
__latency.calculate_model_latency(mode=1)
else:
__latency.calculate_model_latency_nopipe()
__area = Model_area(NetStruct=structure_file, SimConfig_path=args.hardware_description, TCG_mapping=TCG_mapping)
__power = Model_inference_power(NetStruct=structure_file, SimConfig_path=args.hardware_description, TCG_mapping=TCG_mapping)
__energy = Model_energy(NetStruct=structure_file,
SimConfig_path=args.hardware_description,
TCG_mapping=TCG_mapping,
model_latency=__latency,
model_power=__power)
if not (args.disable_accuracy_simulation):
print("Accuracy simulation will take a few minutes on GPU")
weight = __TestInterface.get_net_bits()
weight_2 = weight_update(args.hardware_description, weight, is_Variation=args.enable_variation, is_SAF=args.enable_SAF, is_Rratio=args.enable_R_ratio)
if not (args.enable_fixed_Qrange):
print("Original accuracy:", __TestInterface.origin_evaluate(method='FIX_TRAIN', adc_action='SCALE'))
print("PIM-based computing accuracy:", __TestInterface.set_net_bits_evaluate(weight_2, adc_action='SCALE'))
else:
print("Original accuracy:", __TestInterface.origin_evaluate(method='FIX_TRAIN', adc_action='FIX'))
print("PIM-based computing accuracy:", __TestInterface.set_net_bits_evaluate(weight_2, adc_action='FIX'))
# MNSIM export outputs ------------------------------------------------->
latency_list, MNSIMlatency = __latency.model_latency_output(not (args.disable_module_output), not (args.disable_layer_output))
area_list, MNSIMarea, MNSIMacc_area = __area.model_area_output(not (args.disable_module_output), not (args.disable_layer_output))
power_list, MNSIMpower, MNSIMacc_power = __power.model_power_output(not (args.disable_module_output), not (args.disable_layer_output))
energy_list, MNSIMenergy, MNSIMnoc_energy = __energy.model_energy_output(not (args.disable_module_output), not (args.disable_layer_output))
# mora post-process ------------------------------------------------->
assert len(latency_list) == len(area_list) == len(power_list) == len(energy_list)
MNSIM_layer_resultlist_dict = {'latency': latency_list, 'area': area_list, 'power': power_list, 'energy': energy_list}
MNSIM_total_result_dict = {'latency': MNSIMlatency, 'area': MNSIMarea, 'power': MNSIMpower, 'energy': MNSIMenergy}
MNSIM_additional_result_dict = {'latency': 0, 'area': MNSIMacc_area, 'power': MNSIMacc_power, 'energy': MNSIMnoc_energy}
# save: model + nocbw
MNSIM_result_csv = args.model + '_rram_noc' + str(args.noc_bw) + '.csv'
MNSIM_result_csv_path = os.path.abspath(os.path.join(home_path, 'output/' + args.model + '/' + MNSIM_result_csv))
Mdf = pd.DataFrame(MNSIM_layer_resultlist_dict, index=range(len(latency_list)))
Mdft = pd.DataFrame(MNSIM_total_result_dict, index=['tol'])
Mdfa = pd.DataFrame(MNSIM_additional_result_dict, index=['add'])
Mdf = Mdf.append(Mdft)
Mdf = Mdf.append(Mdfa)
Mdf.to_csv(MNSIM_result_csv_path, index=False)
return
if __name__ == '__main__':
main()