Rate This Document
Findability
Accuracy
Completeness
Readability

Integrating KTFOP with TensorFlow

This section describes how to integrate KTFOP with TensorFlow to enable Kunpeng operator acceleration.

Adaptation

  1. 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.
  2. Prepare the TensorFlow compilation environment by following instructions in "Configuring the Compilation Environment" in TensorFlow Porting Guide (Tensorflow 1.15.5) .
  3. Obtain the adaptation code of TensorFlow v1.0.0.
    1
    git clone -b v1.0.0 https://gitee.com/openeuler/sra_tensorflow_adapter.git
    
  4. Go to the sra_tensorflow_adapter directory.
    1
    cd sra_tensorflow_adapter
    
  5. 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
    
  6. 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
    
  7. Compile TensorFlow.
    1
    sh build.sh
    

    Path 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:

      1
      export KBLAS_LIB_DIR=/usr/local/kml/lib/neon/kblas/locking
      
  8. Install the TensorFlow pip software package.
    1
    pip3 install out/tensorflow-1.15.0-cp37-cp37m-linux_aarch64.whl
    

    If an error is reported during TensorFlow installation, rectify the fault by following instructions in the "Troubleshooting" section of TensorFlow Porting Guide.

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.

  1. Verify the TensorFlow version in a non-source code directory.
    1
    python3 -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
  2. Verify basic functions.
    The following operations use the TensorFlow-provided image recognition test case as an example.
    1. Go to the installation path of the sra_tensorflow_adapter source code.
      1
      cd /path/to/sra_tensorflow_adapter
      
    2. 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
      
    3. Execute the test case in a non-compilation directory.
      1
      python3 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.

      For test reference, see GitHub.