This section will illustrate how to register a custom op step-by-step.
xdputil xmodel tf2_custom_op.xmodel --op custom_layer
Then, you can create a directory for the new op and all your coding and building can be done under this new folder. Since Mylayer op has been realized in VAI2.0, you may use ~/Vitis-AI/examples/custom_operator/tensorflow2_example/op_registration/cpp/op_Mylayer for reference.
Code
Custom OP registration supports both C++ and Python. The following shows how to implement the OP in C++. For the OP implementation in Python, refer to Vitis-AI/examples/custom_operator/tensorflow2_example/op_registration/python/- Create cpp file for the new op. It's my_Mylayer_op.cpp in this example.
- Include the header <vart/op_imp.h>.
- Define a class with a constructor and a function named calculate. In this example, the class name is MylayerOp.
- Implement your algorithm in the calculate function. For this example, PReLU is implemented.
- Register your class by DEF_XIR_OP_IMP(className), for Mylayer,
className is MylayerOp.
Tips: You can copy the cpp file from my_Mylayer_op.cpp and modify the class name as desired, and then you can focus on step 4 and 5. The details of cpp file for Mylayer op are shown below.
Build
- Create a Makefile to build the op library.
Refer to ~/Vitis-AI/examples/custom_operator/tensorflow2_example/op_registration/cpp/op_Mylayer/Makefile.
- Set the output directory and add the dependency between target (output .so),
object (.o) and source (.cpp) files.
If you use the reference Makefile in step 1, you only need to replace the file names with yours, including libvart_op_imp_Mylayer.so, my_Mylayer_op.o and my_Mylayer_op.cpp.
- Execute “make” to complete the build of the library.
After building, libvart_op_imp_Mylayer.so library will be generated.
OUTPUT_DIR = $(PWD)
all: $(OUTPUT_DIR) $(OUTPUT_DIR)/libvart_op_imp_Mylayer.so
$(OUTPUT_DIR):
mkdir -p $@
$(OUTPUT_DIR)/my_Mylayer_op.o: my_Mylayer_op.cpp
$(CXX) -std=c++17 -fPIC -c -o $@ -I. -I=/install/Debug/include -Wall -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 $<
$(OUTPUT_DIR)/libvart_op_imp_Mylayer.so: $(OUTPUT_DIR)/my_Mylayer_op.o
$(CXX) -Wl,--no-undefined -shared -o $@ $+ -L=/install/Debug/lib -lglog -lvitis_ai_library-runner_helper -lvart-runner -lxir
Debug
xdputil run_op <model_file> <op_name> [-r ref] [-d dump]
Execute xdputil run_op -h to view the valid arguments for run_op.
If you still don’t
know what the names should be, you can just put your input files in ref, then try to
execute run_op, then you can also find the file names expected in the resulting
error message as shown in the following figure.
After the successfully execution of run_op, as shown in the following figure, you can find the output
bin file with the same name as the output tensor in the dump directory. Then, you
can compare it with the golden output and debug your code until they are the same.
For Mylayer, the output tensor name is custom_layer, so the output bin file name should be custom_layer.bin as shown in the following
figure.
xdputil comp_float <golden_file> <dump_file> [-t threshold] [--verbose]
-t: threshold, the default value is 0.5, in %