1# Copyright (c) Meta Platforms, Inc. and affiliates. 2# All rights reserved. 3# 4# This source code is licensed under the BSD-style license found in the 5# LICENSE file in the root directory of this source tree. 6 7import argparse 8import binascii 9 10bytes_per_line = 32 11hex_digits_per_line = bytes_per_line * 2 12 13magic_attr = '__attribute__((section(".sram.data"), aligned(16))) uint8_t' 14 15 16def gen_header(model_path, header_path=None): 17 if header_path is not None: 18 header_path = header_path + "/model_pte.h" 19 else: 20 header_path = "model_pte.h" 21 22 with open(model_path, "rb") as fr, open(header_path, "w+") as fw: 23 data = fr.read() 24 hexstream = binascii.hexlify(data).decode("utf-8") 25 26 hexstring = magic_attr + " model_pte[] = {" 27 28 for i in range(0, len(hexstream), 2): 29 if 0 == (i % hex_digits_per_line): 30 hexstring += "\n" 31 hexstring += "0x" + hexstream[i : i + 2] + ", " 32 33 hexstring += "};\n" 34 fw.write(hexstring) 35 print(f"Wrote {len(hexstring)} bytes, original {len(data)}") 36 37 38if __name__ == "__main__": 39 parser = argparse.ArgumentParser() 40 parser.add_argument( 41 "--model_path", required=True, help=".pte file to generate header from." 42 ) 43 parser.add_argument( 44 "--header_output_path", help="Output path where the header should be placed." 45 ) 46 47 args = parser.parse_args() 48 49 gen_header(args.model_path, args.header_output_path) 50