Integrating KTFOP with TensorFlow
This section describes how to integrate KTFOP with TensorFlow to enable Kunpeng operator acceleration.
Adaptation
- Install the Kunpeng Math Library (KML) of version 2.4.0 (GCC environment) by following instructions in "Installing and Configuring the Environment" in Kunpeng Math Library Developer Guide. The KML_BLAS module of KML is required for the program running.
- Prepare the TensorFlow compilation environment by following instructions in "Configuring the Compilation Environment" in TensorFlow Porting Guide (Tensorflow 1.15.5) .
- Obtain the adaptation code of TensorFlow v1.0.0.
1git clone -b v1.0.0 https://gitee.com/openeuler/sra_tensorflow_adapter.git
- Go to the sra_tensorflow_adapter directory.
1cd sra_tensorflow_adapter
- Install the dependencies.
1 2 3 4 5 6
pip3 install wheel pip3 install Cython==0.29.14 pip3 install numpy==1.18.5 --no-build-isolation pip3 install h5py==2.10.0 pip3 install protobuf==3.19.0 pip3 install keras_preprocessing==1.1.2
- If the environment variable configurations have been loaded, skip this step. If not, set the Bazel and Python environment variables based on the actual installation paths. For example:
1 2 3
export PATH=/path/to/bazel/bazel-0.26.1/output:$PATH export PATH=/usr/local/python3.7.5/bin:$PATH export LD_LIBRARY_PATH=/usr/local/python3.7.5/lib:$LD_LIBRARY_PATH
- Compile TensorFlow.
1sh build.shPath to the TensorFlow WHL package: out/tensorflow-1.15.0-cp37-cp37m-linux_aarch64.whl
Paths to the dependency libraries:- Path to the SO file of the KTFOP library:
/usr/local/sra_inference/lib/neon/libktfop.so
/usr/local/sra_inference/lib/sve/libktfop.so
- Default path to the SO file of KML 2.4: /usr/local/kml/lib/kblas/locking/libkblas.so
- Default path to the SO file of KML 2.5: /usr/local/kml/lib/neon/kblas/locking/libkblas.so
Configure the KBLAS_LIB_DIR environment variable as required to select the KML_BLAS library for compilation. For example, select the KML_BLAS library of KML 2.5 with enabled NEON feature:
1export KBLAS_LIB_DIR=/usr/local/kml/lib/neon/kblas/locking
- Path to the SO file of the KTFOP library:
- Install the TensorFlow pip software package.
Verification
After compiling and installing TensorFlow 1.15.0, use the built-in test cases to verify its basic functions. This section uses the image recognition test case to verify TensorFlow.
- Verify the TensorFlow version in a non-source code directory.
1python3 -c "import tensorflow as tf; print(tf.__version__)"

The command output shows that the version is 1.15.0.
If the message "ImportError: libkblas_armv8ss_v2.5.0.so: cannot open shared object file: No such file or directory" is displayed after the version verification command is executed, run the following command and then verify the version again:
export LD_LIBRARY_PATH=/usr/local/kml/lib/neon/kblas/locking:$LD_LIBRARY_PATH
- Verify basic functions.The following operations use the TensorFlow-provided image recognition test case as an example.
- Go to the installation path of the sra_tensorflow_adapter source code.
1cd /path/to/sra_tensorflow_adapter
- Obtain model data and verification data.
1 2
curl -L "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz" | tar -C tensorflow/examples/label_image/data -xz wget -P tensorflow/examples/label_image/data/ https://www.tensorflow.org/images/cropped_panda.jpg --no-check-certificate
- Execute the test case in a non-compilation directory.
1python3 tensorflow/examples/label_image/label_image.py --image=tensorflow/examples/label_image/data/cropped_panda.jpg

The command output shows that there is the highest probability that image tensorflow/examples/label_image/data/cropped_panda.jpg is identified as a giant panda.
- Go to the installation path of the sra_tensorflow_adapter source code.