/scale.py

https://github.com/ARM-software/SCALE-Sim · Python · 206 lines · 137 code · 52 blank · 17 comment · 18 complexity · 8c961ee813055cca321bb156b34682cd MD5 · raw file

  1. import os
  2. import time
  3. import configparser as cp
  4. import run_nets as r
  5. from absl import flags
  6. from absl import app
  7. FLAGS = flags.FLAGS
  8. #name of flag | default | explanation
  9. flags.DEFINE_string("arch_config","./configs/scale.cfg","file where we are getting our architechture from")
  10. flags.DEFINE_string("network","./topologies/conv_nets/alexnet.csv","topology that we are reading")
  11. class scale:
  12. def __init__(self, sweep = False, save = False):
  13. self.sweep = sweep
  14. self.save_space = save
  15. def parse_config(self):
  16. general = 'general'
  17. arch_sec = 'architecture_presets'
  18. net_sec = 'network_presets'
  19. # config_filename = "./scale.cfg"
  20. config_filename = FLAGS.arch_config
  21. print("Using Architechture from ",config_filename)
  22. config = cp.ConfigParser()
  23. config.read(config_filename)
  24. ## Read the run name
  25. self.run_name = config.get(general, 'run_name')
  26. ## Read the architecture_presets
  27. ## Array height min, max
  28. ar_h = config.get(arch_sec, 'ArrayHeight').split(',')
  29. self.ar_h_min = ar_h[0].strip()
  30. if len(ar_h) > 1:
  31. self.ar_h_max = ar_h[1].strip()
  32. #print("Min: " + ar_h_min + " Max: " + ar_h_max)
  33. ## Array width min, max
  34. ar_w = config.get(arch_sec, 'ArrayWidth').split(',')
  35. self.ar_w_min = ar_w[0].strip()
  36. if len(ar_w) > 1:
  37. self.ar_w_max = ar_w[1].strip()
  38. ## IFMAP SRAM buffer min, max
  39. ifmap_sram = config.get(arch_sec, 'IfmapSramSz').split(',')
  40. self.isram_min = ifmap_sram[0].strip()
  41. if len(ifmap_sram) > 1:
  42. self.isram_max = ifmap_sram[1].strip()
  43. ## FILTER SRAM buffer min, max
  44. filter_sram = config.get(arch_sec, 'FilterSramSz').split(',')
  45. self.fsram_min = filter_sram[0].strip()
  46. if len(filter_sram) > 1:
  47. self.fsram_max = filter_sram[1].strip()
  48. ## OFMAP SRAM buffer min, max
  49. ofmap_sram = config.get(arch_sec, 'OfmapSramSz').split(',')
  50. self.osram_min = ofmap_sram[0].strip()
  51. if len(ofmap_sram) > 1:
  52. self.osram_max = ofmap_sram[1].strip()
  53. self.dataflow= config.get(arch_sec, 'Dataflow')
  54. ifmap_offset = config.get(arch_sec, 'IfmapOffset')
  55. self.ifmap_offset = int(ifmap_offset.strip())
  56. filter_offset = config.get(arch_sec, 'FilterOffset')
  57. self.filter_offset = int(filter_offset.strip())
  58. ofmap_offset = config.get(arch_sec, 'OfmapOffset')
  59. self.ofmap_offset = int(ofmap_offset.strip())
  60. ## Read network_presets
  61. ## For now that is just the topology csv filename
  62. #topology_file = config.get(net_sec, 'TopologyCsvLoc')
  63. #self.topology_file = topology_file.split('"')[1] #Config reads the quotes as wells
  64. self.topology_file= FLAGS.network
  65. def run_scale(self):
  66. self.parse_config()
  67. if self.sweep == False:
  68. self.run_once()
  69. else:
  70. self.run_sweep()
  71. def run_once(self):
  72. df_string = "Output Stationary"
  73. if self.dataflow == 'ws':
  74. df_string = "Weight Stationary"
  75. elif self.dataflow == 'is':
  76. df_string = "Input Stationary"
  77. print("====================================================")
  78. print("******************* SCALE SIM **********************")
  79. print("====================================================")
  80. print("Array Size: \t" + str(self.ar_h_min) + "x" + str(self.ar_w_min))
  81. print("SRAM IFMAP: \t" + str(self.isram_min))
  82. print("SRAM Filter: \t" + str(self.fsram_min))
  83. print("SRAM OFMAP: \t" + str(self.osram_min))
  84. print("CSV file path: \t" + self.topology_file)
  85. print("Dataflow: \t" + df_string)
  86. print("====================================================")
  87. net_name = self.topology_file.split('/')[-1].split('.')[0]
  88. #print("Net name = " + net_name)
  89. offset_list = [self.ifmap_offset, self.filter_offset, self.ofmap_offset]
  90. r.run_net( ifmap_sram_size = int(self.isram_min),
  91. filter_sram_size = int(self.fsram_min),
  92. ofmap_sram_size = int(self.osram_min),
  93. array_h = int(self.ar_h_min),
  94. array_w = int(self.ar_w_min),
  95. net_name = net_name,
  96. data_flow = self.dataflow,
  97. topology_file = self.topology_file,
  98. offset_list = offset_list
  99. )
  100. self.cleanup()
  101. print("************ SCALE SIM Run Complete ****************")
  102. def cleanup(self):
  103. if not os.path.exists("./outputs/"):
  104. os.system("mkdir ./outputs")
  105. net_name = self.topology_file.split('/')[-1].split('.')[0]
  106. path = "./output/scale_out"
  107. if self.run_name == "":
  108. path = "./outputs/" + net_name +"_"+ self.dataflow
  109. else:
  110. path = "./outputs/" + self.run_name
  111. if not os.path.exists(path):
  112. os.system("mkdir " + path)
  113. else:
  114. t = time.time()
  115. new_path= path + "_" + str(t)
  116. os.system("mv " + path + " " + new_path)
  117. os.system("mkdir " + path)
  118. cmd = "mv *.csv " + path
  119. os.system(cmd)
  120. cmd = "mkdir " + path +"/layer_wise"
  121. os.system(cmd)
  122. cmd = "mv " + path +"/*sram* " + path +"/layer_wise"
  123. os.system(cmd)
  124. cmd = "mv " + path +"/*dram* " + path +"/layer_wise"
  125. os.system(cmd)
  126. if self.save_space == True:
  127. cmd = "rm -rf " + path +"/layer_wise"
  128. os.system(cmd)
  129. def run_sweep(self):
  130. all_data_flow_list = ['os', 'ws', 'is']
  131. all_arr_dim_list = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384]
  132. all_sram_sz_list = [256, 512, 1024]
  133. data_flow_list = all_data_flow_list[1:]
  134. arr_h_list = all_arr_dim_list[3:8]
  135. arr_w_list = all_arr_dim_list[3:8]
  136. #arr_w_list = list(reversed(arr_h_list))
  137. net_name = self.topology_file.split('/')[-1].split('.')[0]
  138. for df in data_flow_list:
  139. self.dataflow = df
  140. for i in range(len(arr_h_list)):
  141. self.ar_h_min = arr_h_list[i]
  142. self.ar_w_min = arr_w_list[i]
  143. self.run_name = net_name + "_" + df + "_" + str(self.ar_h_min) + "x" + str(self.ar_w_min)
  144. self.run_once()
  145. def main(argv):
  146. s = scale(save = False, sweep = False)
  147. s.run_scale()
  148. if __name__ == '__main__':
  149. app.run(main)
  150. '''
  151. if __name__ == "__main__":
  152. s = scale(save = False, sweep = False)
  153. s.run_scale()
  154. '''