From 9813a5da9a560fab120c167daabcfab5ab9c002c Mon Sep 17 00:00:00 2001 From: Antkillerfarm Date: Wed, 12 Jan 2022 11:04:49 +0800 Subject: [PATCH] add vxc binary for internal ops (#255) cmake -DUSE_VXC_BINARY=1 -DVCCOMPILER_PATH= -DGPU_CONFIG_FILE= .. --- src/tim/vx/internal/ConvertPGMToH.py | 119 +++++++++++++++ src/tim/vx/internal/ovxlib_bin_build.sh | 190 ++++++++++++++++++++++++ src/tim/vx/internal/tim_internal.cmake | 22 +++ 3 files changed, 331 insertions(+) create mode 100644 src/tim/vx/internal/ConvertPGMToH.py create mode 100755 src/tim/vx/internal/ovxlib_bin_build.sh diff --git a/src/tim/vx/internal/ConvertPGMToH.py b/src/tim/vx/internal/ConvertPGMToH.py new file mode 100644 index 0000000..046faed --- /dev/null +++ b/src/tim/vx/internal/ConvertPGMToH.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python + +import subprocess +import sys +import os +import platform +import datetime +import re +import string + +def checkFile(path): + return os.path.isfile(path) + + +def toHex(number): + return "0x%08X" % number + + +def usage(): + print("ConvertPGMToH: convert VXC PGM file to C header file") + print("Usage: ConvertPGMToH [-h] [-a] -i input_file -o output_file") + print("Options and arguments:") + print(" -h :show this usage") + print(" -a :append to the output_file") + print(" -i input_file :specify the .gcPGM file which include VXC shader binary") + print(" -o output_file :specify the .h file to save converted data") + + +def ConverBinToH(source, target): + global append + mmtime = os.path.getmtime(source) + + if checkFile(target): + if os.path.getmtime(target) > mmtime: + print("%s is already the latest one (nothing changed)" % target) + return + + with open(source, "rb") as fin: + buf = fin.read() + #os.remove(target) + bytes = bytearray(buf) + + fout = None + if (append): + fout = open(target, "a") + else: + fout = open(target, "w") + + if not (fout): + print("ERROR: failed to open file: %s" % (target)) + + #fout.write(datetime.datetime.now().strftime("/*Auto created on %Y-%m-%d %H:%M:%S*/\n")) + name = os.path.basename(source).replace(".", "_", 1) + name = re.sub("_all.gcPGM", "", name); + if (append == 0): + fout.write("#ifndef __%s_H__\n#define __%s_H__\n\n" % (name.upper(), name.upper())) + + fout.write("static uint8_t vxcBin%s[] = {" % (name)) + i = 0 + for c in bytes: + if i % 8 == 0: + fout.write("\n ") + fout.write("0x%02X, " % c) + i += 1 + + fout.write("\n};\n") + fout.write("#define VXC_BIN_%s_LEN 0x%08XU\n" % (name.upper(), i)) + + if (append == 0): + fout.write("\n#endif\n\n"); + fout.close() + #print("%s is generated/refreshed" % target) + return + +def main(): + global append + args = sys.argv + argc = len(args) + target_path = "." + tool = "" + source = "" + tool_opt = ["-s", "-V"] + if argc <= 1: + usage() + return + i = 1 + while i < argc: + if args[i] == "-h": + usage() + return + if args[i] == "-a": + append = 1 + elif args[i] == "-o": + i += 1 + target_path = args[i] + elif args[i] == "-i": + i += 1 + source = args[i] + else: + print("unknown parameter: " + args[i]) + usage() + return + i += 1 + + if source == "": + print("Please specify .c/cpp/h/hpp file") + return + else: + if not checkFile(source): + print("Cannot find the file %s" % source) + return + + ConverBinToH(source, target_path) + return + + +append = 0 +if __name__=='__main__': + main() diff --git a/src/tim/vx/internal/ovxlib_bin_build.sh b/src/tim/vx/internal/ovxlib_bin_build.sh new file mode 100755 index 0000000..2c273d6 --- /dev/null +++ b/src/tim/vx/internal/ovxlib_bin_build.sh @@ -0,0 +1,190 @@ +#!/bin/bash + +if [ -z $4 ]; then +echo +echo usage: +echo " $0 VIV_SDK_PATH VCCOMPILER OVXLIB GPU_CONFIG_FILE" +echo +echo " VIV_SDK_PATH: VIVANTE SDK path" +echo " VCCOMPILER: vcCompiler path" +echo " OVXLIB: ovxlib path" +echo " GPU_CONFIG_FILE: gpu config file path" +echo +echo "e.g." +echo " ./ovxlib_bin_build.sh VIV_SDK_PATH VCCOMPILER OVXLIB vip8000.config" +echo +exit 1 +fi + +export VIV_SDK_PATH=$1 +export VCCOMPILER=$2 +export OVXLIB=$3 +export GPU_CONFIG_FILE=$4 + +function convert_vxc_shader() +{ + echo "== convert VXC shader to header files ..." + + VX_BIN_PATH=$OVXLIB/include/libnnext/vx_bin + if [ ! -e "$VX_BIN_PATH" ]; then + mkdir -p $VX_BIN_PATH + fi + rm -f $VX_BIN_PATH/*.h + + ( +cat<$VX_BIN_PATH/vxc_binaries.h + + cd $OVXLIB/src/libnnext/ops/vx + rm -f *.gcPGM *.vxgcSL + + echo "== generating $VX_BIN_PATH/vxc_binaries.h ..." + + for vxFile in `ls *.vx | sed "s/\.vx//"` + do + { + if [ "${vxFile}" != "vsi_nn_kernel_header" ]; then + echo $VCCOMPILER -f${GPU_CONFIG_FILE} -allkernel -cl-viv-gcsl-driver-image \ + -o${vxFile}_vx -m vsi_nn_kernel_header.vx ${vxFile}.vx + $VCCOMPILER -f${GPU_CONFIG_FILE} -allkernel -cl-viv-gcsl-driver-image \ + -o${vxFile}_vx -m vsi_nn_kernel_header.vx ${vxFile}.vx || exit 1 + echo "python $OVXLIB/ConvertPGMToH.py -i ${vxFile}_vx_all.gcPGM \ + -o $VX_BIN_PATH/vxc_bin_${vxFile}_vx.h" + python $OVXLIB/ConvertPGMToH.py -i ${vxFile}_vx_all.gcPGM \ + -o $VX_BIN_PATH/vxc_bin_${vxFile}_vx.h || exit 1 + echo "#include \"vxc_bin_${vxFile}_vx.h\"" >> $VX_BIN_PATH/vxc_binaries.h + fi + } & + done + + wait + echo "== convert VXC shader to header files: success!" + echo "== convert VXC shader to header files: success!" + echo "== convert VXC shader to header files: success!" + rm -f *.gcPGM *.vxgcSL + + cd $OVXLIB/src/libnnext/ops/cl + rm -f *.gcPGM *.vxgcSL *.clgcSL + + for vxFile in `ls *.cl | sed "s/\.cl//"` + do + { + if [ "${vxFile}" != "eltwise_ops_helper" ]; then + cp ${vxFile}.cl ${vxFile}.vx + echo $VCCOMPILER -f${GPU_CONFIG_FILE} -allkernel -cl-viv-gcsl-driver-image \ + -o${vxFile}_cl -m eltwise_ops_helper.cl ${vxFile}.vx + $VCCOMPILER -f${GPU_CONFIG_FILE} -allkernel -cl-viv-gcsl-driver-image \ + -o${vxFile}_cl -m eltwise_ops_helper.cl ${vxFile}.vx || exit 1 + echo "python $OVXLIB/ConvertPGMToH.py -i ${vxFile}_cl_all.gcPGM \ + -o $VX_BIN_PATH/vxc_bin_${vxFile}_cl.h" + python $OVXLIB/ConvertPGMToH.py -i ${vxFile}_cl_all.gcPGM \ + -o $VX_BIN_PATH/vxc_bin_${vxFile}_cl.h || exit 1 + echo "#include \"vxc_bin_${vxFile}_cl.h\"" >> $VX_BIN_PATH/vxc_binaries.h + rm ${vxFile}.vx + fi + } & + done + + wait + echo "== convert CL shader to header files: success!" + echo "== convert CL shader to header files: success!" + echo "== convert CL shader to header files: success!" + rm -f *.gcPGM *.vxgcSL *.clgcSL + + ( +cat<>$VX_BIN_PATH/vxc_binaries.h + + cd $OVXLIB/src/libnnext/ops/vx + for vxFile in `ls *.vx | sed "s/\.vx//"` + do + vxFileUpper=`echo ${vxFile} | tr 'a-z' 'A-Z'` + if [ "${vxFile}" != "vsi_nn_kernel_header" ]; then + echo " {\"${vxFile}_vx\", vxcBin${vxFile}_vx, VXC_BIN_${vxFileUpper}_VX_LEN}," \ + >> $VX_BIN_PATH/vxc_binaries.h + fi + done + + ( +cat<>$VX_BIN_PATH/vxc_binaries.h + + + cd $OVXLIB/src/libnnext/ops/cl + for vxFile in `ls *.cl | sed "s/\.cl//"` + do + vxFileUpper=`echo ${vxFile} | tr 'a-z' 'A-Z'` + if [ "${vxFile}" != "eltwise_ops_helper" ]; then + echo " {\"${vxFile}_cl\", vxcBin${vxFile}_cl, VXC_BIN_${vxFileUpper}_CL_LEN}," \ + >> $VX_BIN_PATH/vxc_binaries.h + fi + done + ( +cat<>$VX_BIN_PATH/vxc_binaries.h + + exit 0 +} + +export VIVANTE_SDK_DIR=$VIV_SDK_PATH +convert_vxc_shader + +exit 0 diff --git a/src/tim/vx/internal/tim_internal.cmake b/src/tim/vx/internal/tim_internal.cmake index 6f93896..9f465f9 100644 --- a/src/tim/vx/internal/tim_internal.cmake +++ b/src/tim/vx/internal/tim_internal.cmake @@ -1,8 +1,30 @@ message("src/tim/vx/internal") +option(USE_VXC_BINARY "Use VXC binary file" OFF) + set(OVXLIB_API_ATTR "__attribute__\(\(visibility\(\"default\"\)\)\)") add_definitions(-DOVXLIB_API=${OVXLIB_API_ATTR}) +if(USE_VXC_BINARY) + if(EXTERNAL_VIV_SDK) + set(VIV_SDK_PATH ${EXTERNAL_VIV_SDK}) + else() + set(VIV_SDK_PATH ${PROJECT_SOURCE_DIR}/prebuilt-sdk/x86_64_linux) + endif() + if(NOT VCCOMPILER_PATH) + set(VCCOMPILER_PATH ${PROJECT_SOURCE_DIR}/prebuilt-sdk/x86_64_linux/bin/vcCompiler) + endif() + if(NOT GPU_CONFIG_FILE) + message(FATAL_ERROR "Need set GPU_CONFIG_FILE for vxc binary") + endif() + + execute_process(COMMAND bash ${CMAKE_CURRENT_SOURCE_DIR}/vx/internal/ovxlib_bin_build.sh + ${VIV_SDK_PATH} ${VCCOMPILER_PATH} + ${CMAKE_CURRENT_SOURCE_DIR}/vx/internal/ ${GPU_CONFIG_FILE}) + add_definitions(-DVSI_USE_VXC_BINARY=1) +endif() + + aux_source_directory(./vx/internal/src INTERNAL_SRC) aux_source_directory(./vx/internal/src/kernel INTERNAL_KERNEL) aux_source_directory(./vx/internal/src/kernel/cl INTERNAL_KERNEL_CL)