How to build tvm cpp rpc for android

I have tried:
1.
set(USE_CPP_RPC ON)
set(CMAKE_TOOLCHAIN_FILE $ENV{ANDROID_NDK}/build/cmake/android.toolchain.cmake)
set(ANDROID_ABI “arm64-v8a”)
set(ANDROID_PLATFORM android-28)

Then execute “file tvm_rpc”, which shows it’s a file for x86-64 and couldn’t run on android device.
2.
cmake …
-DCMAKE_SYSTEM_NAME=Linux
-DCMAKE_SYSTEM_VERSION=1
-DCMAKE_C_COMPILER=/usr/bin/aarch64-linux-gnu-gcc
-DCMAKE_CXX_COMPILER=/usr/bin/aarch64-linux-gnu-g++
-DCMAKE_FIND_ROOT_PATH=/usr/aarch64-linux-gnu
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY
-DMACHINE_NAME=aarch64-linux-gnu

“file tvm_rpc” shows it’s a file for aarch64, but couldn’t run on android device too. Which says “/system/bin/sh: ./tvm_rpc: No such file or directory”

Hi @A_newer!

It looks like you didn’t upload tvm_rpc to the Android device. Usually I do the following steps:

  1. Configure cmake for Android (below is my common cmake command). I think that you already did the same things by your command in boolet number 2:
    cmake -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
          -DANDROID_ABI=arm64-v8a \
          -DANDROID_NATIVE_API_LEVEL=android-23 \
          -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON \
          -DANDROID_STL=c++_static \
          -DUSE_CPP_RPC=ON \
          -DUSE_HEXAGON_SDK=OFF ..
    
  2. Make tvm_rpc:
    make -j8 tvm_rpc
    
  3. Upload libraries to the device:
    adb -s <device_hash> push {libtvm_runtime.so,tvm_rpc} /data/local/tmp
    
    You can get device hash by calling:
     adb devices
    
  4. After that you should be able to run tvm_rpc on Android, e.g.:
    adb -s <device_hash> shell LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/tvm_rpc server --host=0.0.0.0 --port=9090 --tracker=127.0.0.1:9190 --key=android --port-end=9190
    
    If you want to connect the phone through cable instead of via Wi-Fi, probably it also will be necessary to forward ports:
    adb -s <device_hash> reverse tcp:9190 tcp:9190
    adb -s <device_hash> forward tcp:9090 tcp:9090
    adb -s <device_hash> forward tcp:9091 tcp:9091
    adb -s <device_hash> forward tcp:9092 tcp:9092
    adb -s <device_hash> forward tcp:9093 tcp:9093
    

P.S. On some devices you might see the problem that when you are trying to run tvm_rpc you will see an error that it cannot find libc++_shared.so, then just copy this library from NDK to /data/local/tmp:

  • Find the library:
    find ${ANDROID_NDK_HOME} -name libc++_shared.so
    
  • Copy library on the device:
    adb -s <device_hash> push libc++_shared.so /data/local/tmp
    

Thanks for your advice.
I followed your steps, cmake and then make, adb push to /data/local/tmp and run tvm_rpc.
export LD_LIBRARY_PATH=/data/local/tmp
/data/local/tmp/tvm_rpc server --host=0.0.0.0 --port=9090 --tracker=127.0.0.1:9190 --key=android --port-end=9190

The error log shows:
/system/bin/sh: /data/local/tmp/tvm_rpc: No such file or directory

Then I try
ls /data/local/tmp/tvm_rpc -l
-rwxrwxrwx 1 shell shell 160616 2022-10-26 09:26 /data/local/tmp/tvm_rpc
file tvm_rpc
tvm_rpc: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=966b44e56ac380599f595d1f99f49c2666e03d13, for GNU/Linux 3.7.0, not stripped

I push another HelloWorld file into /data/local/tmp, it could run and print out HelloWorld.
Maybe it’s a problem related to dynamic lib. But I can’t find the lib named ld-linux-aarch64.so.1.

Hmm… It looks strange because the message tells that the file cannot be found. I see this behavior first time. I don’t think that this issue is related to the library. Did you build your HelloWorld file in the same way as you built the tvm_rpc? Could you please just double-check that the name to tvm_rpc doesn’t contain any “unusual” symbols (probably some unicode symbols or something like that)?

Thanks for reply, I have checked info you mentioned and they have no problem.
Could you please offer your “file tvm_rpc” info? Thanks a lot.

Here is the file tvm_rpc from my file:

tvm_rpc: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/bin/linker64, BuildID[sha1]=0f05e11912c8ee28aa091ff0a6773024ac78c046, with debug_info, not stripped

Thanks, I will try to reproduce your results.
Best wishes.

1 Like

I followed your steps, cmake and then make,but Then execute “file tvm_rpc”, which shows it’s a file for x86-64 and couldn’t run on android device.

I have tried:

cmake -DCMAKE_BUILD_TYPE=Release
-DCMAKE_TOOLCHAIN_FILE=/home/yuwenjun/tools/Sdk/ndk/20.1.5948944/build/cmake/android.toolchain.cmake
-DANDROID_ABI=arm64-v8a
-DANDROID_PLATFORM=“android-21”
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ON
-DANDROID_STL=c++_static
-DUSE_CPP_RPC=ON
-DUSE_HEXAGON_SDK=OFF …

The point is that, modifications in config.cmake file don’t take effect. I need to specify some configurations with cmake -D as you do. Besides that, I need to make a new build folder for my new configuration, otherwise temporary files before will exist and influence the result.
Thanks a lot.

1 Like

Please, read this comment. Probably you have the same issue.

I have solved it, I need to create a new build directory and then execute cmake -D