From 6cd38e9f9cacb7023c3a372d7211f1ea49d05400 Mon Sep 17 00:00:00 2001 From: jiacong sun Date: Fri, 6 Dec 2024 20:57:15 +0100 Subject: [PATCH] fix lab5 script --- lab5/{ => inputs/hardware}/accelerator.yaml | 0 lab5/{ => inputs/mapping}/mapping.yaml | 3 - .../workload}/resnet18_first_layer.onnx | Bin lab5/main.py | 87 ++++++++++-------- 4 files changed, 49 insertions(+), 41 deletions(-) rename lab5/{ => inputs/hardware}/accelerator.yaml (100%) rename lab5/{ => inputs/mapping}/mapping.yaml (74%) rename lab5/{ => inputs/workload}/resnet18_first_layer.onnx (100%) diff --git a/lab5/accelerator.yaml b/lab5/inputs/hardware/accelerator.yaml similarity index 100% rename from lab5/accelerator.yaml rename to lab5/inputs/hardware/accelerator.yaml diff --git a/lab5/mapping.yaml b/lab5/inputs/mapping/mapping.yaml similarity index 74% rename from lab5/mapping.yaml rename to lab5/inputs/mapping/mapping.yaml index 5c4f0170e..523f83600 100644 --- a/lab5/mapping.yaml +++ b/lab5/inputs/mapping/mapping.yaml @@ -1,5 +1,4 @@ - name: default - core_allocation: [1] memory_operand_links: O: O W: I2 @@ -9,5 +8,3 @@ - K D2: - C - - FX - - FY diff --git a/lab5/resnet18_first_layer.onnx b/lab5/inputs/workload/resnet18_first_layer.onnx similarity index 100% rename from lab5/resnet18_first_layer.onnx rename to lab5/inputs/workload/resnet18_first_layer.onnx diff --git a/lab5/main.py b/lab5/main.py index da38c5046..366a30690 100644 --- a/lab5/main.py +++ b/lab5/main.py @@ -3,71 +3,82 @@ import sys sys.path.insert(0, os.getcwd()) -from zigzag.api import get_hardware_performance_zigzag_imc +from zigzag.api import get_hardware_performance_zigzag from zigzag.visualization.results.plot_cme import ( bar_plot_cost_model_evaluations_breakdown, - bar_plot_cost_model_evaluations_total, ) +from zigzag.visualization.results.print_mapping import print_mapping +# Initialize the logger _logging_level = _logging.INFO _logging_format = "%(asctime)s - %(funcName)s +%(lineno)s - %(levelname)s - %(message)s" _logging.basicConfig(level=_logging_level, format=_logging_format) logger = _logging.getLogger(__name__) -# Path to the workload onnx model -# onnx_model_path = "zigzag/inputs/examples/workload/resnet18.onnx" -onnx_model_path = "lab5/resnet18_first_layer.onnx" +# Define the experiment id and pickle name +hw_name = "accelerator1" +workload_name = "resnet18_first_layer" +experiment_id = f"{hw_name}-{workload_name}" +pickle_name = f"{experiment_id}-saved_list_of_cmes" -# List of accelerators architectures we run our experiment for -hardware = "lab5/accelerator.yaml" -# List of mappings for each accelerator -mapping = "lab5/mapping.yaml" +# Define main input paths +accelerator = "lab5/inputs/hardware/accelerator.yaml" +workload = "lab5/inputs/workload/resnet18_first_layer.onnx" +mapping = "lab5/inputs/mapping/mapping.yaml" -# Pickle filename to save list of cmes -pickle_filename = "lab5/outputs/list_of_cmes.pickle" -json_filename = "lab5/outputs/accelerator_layer_?.json" -# Call the zigzag api, using a provided accelerator and mapping -energy, latency, tclk, area, results = get_hardware_performance_zigzag_imc( - onnx_model_path, - hardware, - mapping, - opt="latency", - dump_filename_pattern=json_filename, +# Define other inputs of api call +temporal_mapping_search_engine = "loma" +optimization_criterion = "latency" +dump_folder = f"lab5/outputs/{experiment_id}" +pickle_filename = f"lab5/outputs/{pickle_name}.pickle" + + +# Get the hardware performance through api call +energy, latency, tclk, area, results = get_hardware_performance_zigzag( + accelerator=accelerator, + workload=workload, + mapping=mapping, + temporal_mapping_search_engine=temporal_mapping_search_engine, + opt=optimization_criterion, + dump_folder=dump_folder, pickle_filename=pickle_filename, + in_memory_compute=True, ) -cme = results[0][1][0][0] -imc_macro = cme.accelerator.cores[0].operational_array +# Save a bar plot of the cost model evaluations breakdown +cmes = [result[0] for result in results[0][1]] +save_path = "lab5/outputs/breakdown.png" +bar_plot_cost_model_evaluations_breakdown(cmes, save_path=save_path) +print_mapping(cmes[0]) -x_labels = ["imc accelerator"] -bar_plot_cost_model_evaluations_total( - [cme], - labels=x_labels, - save_path="lab5/outputs/plot_total.png", -) -bar_plot_cost_model_evaluations_breakdown([cme], save_path="lab5/outputs/plot_breakdown.png", xtick_rotation=0) -total_mac_count = cme.layer.total_MAC_count -delay_in_ns = energy * cme.tclk # unit: ns +# Calculate system-level performance metrics +total_mac_count = cmes[0].layer.total_mac_count +delay_in_ns = energy * cmes[0].tclk # unit: ns tops_system = total_mac_count * 2 / delay_in_ns / 1000 topsw_system = total_mac_count * 2 / energy -topsmm2_system = tops_system / cme.area_total +topsmm2_system = tops_system / cmes[0].area_total + +# Calculate macro-level performance metrics +imc_macro = cmes[0].accelerator.operational_array tops_peak, topsw_peak, topsmm2_peak = imc_macro.get_macro_level_peak_performance() -area_breakdown_info = f"memory area: {round(cme.mem_area, 4)}, imc area: {round(cme.imc_area, 4)}" -energy_breakdown_info = f"computation: {round(cme.mac_energy, 2)}, memory: {round(cme.mem_energy, 2)}" -cycles_breakdown_info = f"computation: {cme.ideal_temporal_cycle}, memory stalling: {cme.SS_comb}, \ -data loading: {cme.data_loading_cycle}, data offloading: {cme.data_offloading_cycle}" +# Extract area and latency details +area_breakdown_info = f"memory area: {round(cmes[0].mem_area, 4)}, imc area: {round(cmes[0].imc_area, 4)}" +energy_breakdown_info = f"computation: {round(cmes[0].mac_energy, 2)}, memory: {round(cmes[0].mem_energy, 2)}" +cycles_breakdown_info = f"computation: {cmes[0].ideal_temporal_cycle}, memory stalling: {cmes[0].stall_slack_comb}, \ +data loading: {cmes[0].data_onloading_cycle}, data offloading: {cmes[0].data_offloading_cycle}" +# Print out information in the terminal macro_performance_info = f"TOP/s: {round(tops_peak, 4)}, TOP/s/W: {round(topsw_peak, 4)}, \ TOP/s/mm^2: {round(topsmm2_peak, 4)}" system_performance_info = f"TOP/s: {round(tops_system, 4)}, TOP/s/W: {round(topsw_system, 4)}, \ TOP/s/mm^2: {round(topsmm2_system, 4)}" -logger.info("spatial mapping: %s", cme.layer.spatial_mapping) +logger.info("spatial mapping: %s", cmes[0].layer.spatial_mapping) logger.info("energy (pJ): %s [%s]", round(energy, 2), energy_breakdown_info) logger.info("#cycles: %s [%s]", latency, cycles_breakdown_info) -logger.info("Tclk (ns): %s", round(cme.tclk, 4)) -logger.info("system area (mm^2): %s [%s]", round(cme.area_total, 4), area_breakdown_info) +logger.info("Tclk (ns): %s", round(cmes[0].tclk, 4)) +logger.info("system area (mm^2): %s [%s]", round(cmes[0].area_total, 4), area_breakdown_info) logger.info("macro-level performance: [%s]", macro_performance_info) logger.info("system-level performance: [%s]", system_performance_info) exit()