add vxc binary for internal ops (#255)

cmake -DUSE_VXC_BINARY=1 -DVCCOMPILER_PATH=<vcCompiler path> -DGPU_CONFIG_FILE=<gpu config file> ..
This commit is contained in:
Antkillerfarm 2022-01-12 11:04:49 +08:00 committed by GitHub
parent 4229ad88b3
commit 9813a5da9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 331 additions and 0 deletions

View File

@ -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()

View File

@ -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<<EOF
/****************************************************************************
*
* Copyright (c) 2020 Vivante Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*****************************************************************************/
/* WARNING! AUTO-GENERATED, DO NOT MODIFY MANUALLY */
#ifndef __VXC_BINARIES_H__
#define __VXC_BINARIES_H__
EOF
)>$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<<EOF
#ifndef _cnt_of_array
#define _cnt_of_array( arr ) (sizeof( arr )/sizeof( arr[0] ))
#endif
typedef struct _vsi_nn_vx_bin_resource_item_type
{
char const* name;
uint8_t const* data;
uint32_t len;
} vsi_nn_vx_bin_resource_item_type;
const vsi_nn_vx_bin_resource_item_type vx_bin_resource_items_vx[] =
{
EOF
)>>$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<<EOF
};
const int vx_bin_resource_items_vx_cnt = _cnt_of_array(vx_bin_resource_items_vx);
const vsi_nn_vx_bin_resource_item_type vx_bin_resource_items_cl[] =
{
EOF
)>>$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<<EOF
};
const int vx_bin_resource_items_cl_cnt = _cnt_of_array(vx_bin_resource_items_cl);
#endif
EOF
)>>$VX_BIN_PATH/vxc_binaries.h
exit 0
}
export VIVANTE_SDK_DIR=$VIV_SDK_PATH
convert_vxc_shader
exit 0

View File

@ -1,8 +1,30 @@
message("src/tim/vx/internal") message("src/tim/vx/internal")
option(USE_VXC_BINARY "Use VXC binary file" OFF)
set(OVXLIB_API_ATTR "__attribute__\(\(visibility\(\"default\"\)\)\)") set(OVXLIB_API_ATTR "__attribute__\(\(visibility\(\"default\"\)\)\)")
add_definitions(-DOVXLIB_API=${OVXLIB_API_ATTR}) 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 INTERNAL_SRC)
aux_source_directory(./vx/internal/src/kernel INTERNAL_KERNEL) aux_source_directory(./vx/internal/src/kernel INTERNAL_KERNEL)
aux_source_directory(./vx/internal/src/kernel/cl INTERNAL_KERNEL_CL) aux_source_directory(./vx/internal/src/kernel/cl INTERNAL_KERNEL_CL)