Isabella Gottardi | c64f506 | 2022-01-21 15:27:13 +0000 | [diff] [blame] | 1 | # Copyright (c) 2021 - 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | """ |
| 17 | Utility script to convert a set of pairs of npy files in a given location into |
| 18 | corresponding cpp files and a single hpp file referencing the vectors |
| 19 | from the cpp files. |
| 20 | """ |
| 21 | import datetime |
| 22 | import math |
| 23 | import os |
| 24 | import numpy as np |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 25 | from pathlib import Path |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | |
| 27 | from argparse import ArgumentParser |
| 28 | from jinja2 import Environment, FileSystemLoader |
| 29 | |
| 30 | parser = ArgumentParser() |
| 31 | parser.add_argument("--data_folder_path", type=str, help="path to ifm-ofm npy folder to convert.") |
| 32 | parser.add_argument("--source_folder_path", type=str, help="path to source folder to be generated.") |
| 33 | parser.add_argument("--header_folder_path", type=str, help="path to header folder to be generated.") |
| 34 | parser.add_argument("--usecase", type=str, default="", help="Test data file suffix.") |
| 35 | parser.add_argument("--namespaces", action='append', default=[]) |
| 36 | parser.add_argument("--license_template", type=str, help="Header template file", |
| 37 | default="header_template.txt") |
| 38 | parser.add_argument("-v", "--verbosity", action="store_true") |
| 39 | |
| 40 | args = parser.parse_args() |
| 41 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 42 | env = Environment(loader=FileSystemLoader(Path(__file__).parent / 'templates'), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | trim_blocks=True, |
| 44 | lstrip_blocks=True) |
| 45 | |
| 46 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 47 | def write_hpp_file(header_filename, cc_file_path, header_template_file, num_ifms, num_ofms, |
| 48 | ifm_array_names, ifm_sizes, ofm_array_names, ofm_sizes, iofm_data_type): |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 49 | header_file_path = Path(args.header_folder_path) / header_filename |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 50 | |
| 51 | print(f"++ Generating {header_file_path}") |
| 52 | header_template = env.get_template(header_template_file) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 53 | hdr = header_template.render(script_name=Path(__file__).name, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 54 | gen_time=datetime.datetime.now(), |
| 55 | year=datetime.datetime.now().year) |
| 56 | env.get_template('TestData.hpp.template').stream(common_template_header=hdr, |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 57 | ifm_count=num_ifms, |
| 58 | ofm_count=num_ofms, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 59 | ifm_var_names=ifm_array_names, |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 60 | ifm_var_sizes=ifm_sizes, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | ofm_var_names=ofm_array_names, |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 62 | ofm_var_sizes=ofm_sizes, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | data_type=iofm_data_type, |
| 64 | namespaces=args.namespaces) \ |
| 65 | .dump(str(header_file_path)) |
| 66 | |
| 67 | env.get_template('TestData.cc.template').stream(common_template_header=hdr, |
| 68 | include_h=header_filename, |
| 69 | ifm_var_names=ifm_array_names, |
| 70 | ofm_var_names=ofm_array_names, |
| 71 | data_type=iofm_data_type, |
| 72 | namespaces=args.namespaces) \ |
| 73 | .dump(str(cc_file_path)) |
| 74 | |
| 75 | |
| 76 | def write_individual_cc_file(filename, cc_filename, header_filename, header_template_file, array_name, iofm_data_type): |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 77 | print(f"++ Converting {filename} to {cc_filename.name}") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | header_template = env.get_template(header_template_file) |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 79 | hdr = header_template.render(script_name=Path(__file__).name, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | gen_time=datetime.datetime.now(), |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 81 | file_name=filename, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | year=datetime.datetime.now().year) |
| 83 | |
| 84 | # Convert the image and write it to the cc file |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 85 | fm_data = (np.load(Path(args.data_folder_path) / filename)).flatten() |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | type(fm_data.dtype) |
| 87 | hex_line_generator = (', '.join(map(hex, sub_arr)) |
| 88 | for sub_arr in np.array_split(fm_data, math.ceil(len(fm_data) / 20))) |
| 89 | |
Isabella Gottardi | 5cbcd9e | 2021-10-15 10:17:33 +0100 | [diff] [blame] | 90 | env.get_template('iofmdata.cc.template').stream(common_template_header=hdr, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 91 | include_h=header_filename, |
| 92 | var_name=array_name, |
| 93 | fm_data=hex_line_generator, |
| 94 | data_type=iofm_data_type, |
| 95 | namespaces=args.namespaces) \ |
| 96 | .dump(str(cc_filename)) |
| 97 | |
| 98 | |
| 99 | def get_npy_vec_size(filename: str) -> int: |
| 100 | """ |
| 101 | Gets the size of the array in the npy file |
| 102 | Args: |
| 103 | filename: npy file path. |
| 104 | Return: |
| 105 | size in bytes |
| 106 | """ |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 107 | data = np.load(Path(args.data_folder_path) / filename) |
| 108 | return data.size * data.dtype.itemsize |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | |
| 110 | |
| 111 | def main(args): |
| 112 | # Keep the count of the images converted |
| 113 | ifm_array_names = [] |
| 114 | ofm_array_names = [] |
| 115 | |
Kshitij Sisodia | b178b28 | 2022-01-04 13:37:53 +0000 | [diff] [blame] | 116 | add_usecase_fname = ("_" + args.usecase) if (args.usecase != "") else "" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 117 | header_filename = "TestData" + add_usecase_fname + ".hpp" |
| 118 | common_cc_filename = "TestData" + add_usecase_fname + ".cc" |
| 119 | |
| 120 | # In the data_folder_path there should be pairs of ifm-ofm |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 121 | # It's assumed the ifm-ofm naming convention: ifm0.npy-ofm0.npy, ifm1.npy-ofm1.npy |
| 122 | ifms_count = int(len(list(Path(args.data_folder_path).glob('ifm*.npy')))) |
| 123 | ofms_count = int(len(list(Path(args.data_folder_path).glob('ofm*.npy')))) |
| 124 | |
| 125 | #i_ofms_count = int(len([name for name in os.listdir(os.path.join(args.data_folder_path)) if name.lower().endswith('.npy')]) / 2) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 126 | |
| 127 | iofm_data_type = "int8_t" |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 128 | if ifms_count > 0: |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 129 | iofm_data_type = "int8_t" if (np.load(Path(args.data_folder_path) / "ifm0.npy").dtype == np.int8) else "uint8_t" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 130 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 131 | ifm_sizes = [] |
| 132 | ofm_sizes = [] |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 133 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 134 | for idx in range(ifms_count): |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 135 | # Save the fm cc file |
| 136 | base_name = "ifm" + str(idx) |
| 137 | filename = base_name+".npy" |
| 138 | array_name = base_name + add_usecase_fname |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 139 | cc_filename = Path(args.source_folder_path) / (array_name + ".cc") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | ifm_array_names.append(array_name) |
| 141 | write_individual_cc_file(filename, cc_filename, header_filename, args.license_template, array_name, iofm_data_type) |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 142 | ifm_sizes.append(get_npy_vec_size(filename)) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 143 | |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 144 | for idx in range(ofms_count): |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 145 | # Save the fm cc file |
| 146 | base_name = "ofm" + str(idx) |
| 147 | filename = base_name+".npy" |
| 148 | array_name = base_name + add_usecase_fname |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 149 | cc_filename = Path(args.source_folder_path) / (array_name + ".cc") |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 150 | ofm_array_names.append(array_name) |
| 151 | write_individual_cc_file(filename, cc_filename, header_filename, args.license_template, array_name, iofm_data_type) |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 152 | ofm_sizes.append(get_npy_vec_size(filename)) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 153 | |
Richard Burton | 1706962 | 2022-03-17 10:54:26 +0000 | [diff] [blame] | 154 | common_cc_filepath = Path(args.source_folder_path) / common_cc_filename |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 155 | write_hpp_file(header_filename, common_cc_filepath, args.license_template, |
Richard Burton | 0055346 | 2021-11-10 16:27:14 +0000 | [diff] [blame] | 156 | ifms_count, ofms_count, ifm_array_names, ifm_sizes, ofm_array_names, ofm_sizes, iofm_data_type) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | if args.verbosity: |
| 161 | print("Running gen_test_data_cpp with args: "+str(args)) |
| 162 | main(args) |