Skip to content

Commit 6c32f97

Browse files
Add minimal forwarding RPC server for host driven python execution on Hexagon (#9526)
* Minimal proxy RPC server for hexagon. Functions by routing from Android to Hexagon via QTI FastRPC calls. Interim solution until Hexagon on-device RPC server is ready. * Apply clang-format. * Fix build to support building alongside Hexagon Launcher. * Add readme. * src/runtime/hexagon/rpc -> src/runtime/hexagon/proxy_rpc * Added small refactors to hexagon test_matmul.py. * Add skipif on additional env vars. * Fix IOS build. * Rename USE_HEXAGON_PROXY_RPC and add tvm_options entry. * Add NDArray::Container deleters. Co-authored-by: Eric Lunderberg <[email protected]>
1 parent bb50884 commit 6c32f97

File tree

23 files changed

+1568
-79
lines changed

23 files changed

+1568
-79
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ tvm_option(USE_ROCM "Build with ROCM" OFF)
3030
tvm_option(ROCM_PATH "The path to rocm" /opt/rocm)
3131
tvm_option(USE_HEXAGON_DEVICE "Build with Hexagon device support in TVM runtime" OFF)
3232
tvm_option(USE_HEXAGON_SDK "Path to the Hexagon SDK root (required for Hexagon support in TVM runtime or for building TVM runtime for Hexagon)" /path/to/sdk)
33+
tvm_option(USE_HEXAGON_LAUNCHER "Build the Hexagon graph launcher application" OFF)
34+
tvm_option(USE_HEXAGON_PROXY_RPC "Build the Hexagon Proxy RPC server application" OFF)
3335
tvm_option(USE_RPC "Build with RPC" ON)
3436
tvm_option(USE_THREADS "Build with thread support" ON)
3537
tvm_option(USE_LLVM "Build with LLVM, can be set to specific llvm-config path" OFF)

apps/hexagon_launcher/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ The Hexagon launcher application is an android binary and thus requires the use
6363
of an android toolchain for compilation. Similarly, the Hexagon tvm runtime
6464
requires the use of the Hexagon toolchain and depends on the Hexagon SDK. The
6565
resulting hexagon launcher binaries can be found in the `apps_hexagon_launcher`
66-
subdirectory of the cmake build directory. Please note that the above command
67-
will not build support for Hexagon codegen in the TVM library, for that please
68-
additionally define the `USE_HEXAGON_DEVICE` variable. Also, the LLVM used in
69-
`USE_LLVM` should have Hexagon target built in.
66+
subdirectory of the cmake build directory. The above command
67+
will build support for Hexagon codegen in the TVM library that requires
68+
`USE_LLVM` to be set to an llvm-config that has the Hexagon target built in.
69+
7070

7171
### Manual compilation
7272

apps/hexagon_proxy_rpc/Readme.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
2+
<!--- or more contributor license agreements. See the NOTICE file -->
3+
<!--- distributed with this work for additional information -->
4+
<!--- regarding copyright ownership. The ASF licenses this file -->
5+
<!--- to you under the Apache License, Version 2.0 (the -->
6+
<!--- "License"); you may not use this file except in compliance -->
7+
<!--- with the License. You may obtain a copy of the License at -->
8+
9+
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
10+
11+
<!--- Unless required by applicable law or agreed to in writing, -->
12+
<!--- software distributed under the License is distributed on an -->
13+
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
14+
<!--- KIND, either express or implied. See the License for the -->
15+
<!--- specific language governing permissions and limitations -->
16+
<!--- under the License. -->
17+
# Hexagon Proxy RPC server
18+
19+
The proxy RPC server for Hexagon is a wrapper which takes standard TVM RPC calls from a python host
20+
to a remote Android device and forwards them across FastRPC to Hexagon. This RPC flow will be replaced
21+
by running a minimal RPC server directly on Hexagon. For now we provide a prototype forwarding RPC server
22+
for host driven execution on Hexagon.
23+
24+
## Compilation
25+
26+
Project inventory:
27+
* Android
28+
* libtvm_runtime.so (containing HexagonHostDeviceAPI src/runtime/Hexagon/proxy_rpc/device_api.cc)
29+
* tvm_rpc (C++ RPC server)
30+
* librpc_env (Hexagon specific RPC proxy environment)
31+
32+
* Hexagon
33+
* libhexagon_proxy_rpc_skel.so (Hexagon device code containing FastRPC endpoints for the Hexagon Proxy RPC server)
34+
35+
All Android and Hexagon device artifacts will be placed in `apps_hexagon_proxy_rpc` from which they can be pushed
36+
to an attached `adb` device.
37+
38+
### Prerequisites
39+
40+
1. Android NDK version r19c or later.
41+
2. Hexagon SDK version 4.0.0 or later.
42+
43+
Android NDK can be downloaded from https://developer.android.com/ndk.
44+
Hexagon SDK is available at //developer.qualcomm.com/software/Hexagon-dsp-sdk.
45+
46+
### Compilation with TVM
47+
48+
Building the Hexagon Proxy RPC as a component of the main TVM build
49+
used for Hexagon codegen can be achieved by setting `USE_HEXAGON_PROXY_RPC=ON`.
50+
A minimal example invocation for compiling TVM along with the Hexagon Proxy RPC server
51+
is included below:
52+
53+
```
54+
cmake -DCMAKE_C_COMPILER=/path/to/clang \
55+
-DCMAKE_CXX_COMPILER=/path/to/clang++ \
56+
-DCMAKE_CXX_FLAGS='-stdlib=libc++' \
57+
-DCMAKE_CXX_STANDARD=14 \
58+
-DUSE_RPC=ON \
59+
-DUSE_LLVM=/path/to/llvm/bin/llvm-config \
60+
-DUSE_HEXAGON_PROXY_RPC=ON \
61+
-DANDROID_ABI=arm64-v8a \
62+
-DANDROID_PLATFORM=android-28 \
63+
-DUSE_ANDROID_TOOLCHAIN=/path/to/android-ndk/build/cmake/android.toolchain.cmake \
64+
-DUSE_HEXAGON_ARCH=v65|v66|v68 \
65+
-DUSE_HEXAGON_SDK=/path/to/Hexagon/SDK \
66+
-DUSE_HEXAGON_TOOLCHAIN=/path/to/Hexagon/toolchain/ ..
67+
```
68+
69+
where `v65|v66|v68` means "one of" these architecture versions.
70+
The Hexagon proxy RPC application (tvm_rpc) is an android binary and thus requires the use
71+
of an android toolchain for compilation. Similarly, the Hexagon tvm runtime
72+
requires the use of the Hexagon toolchain and depends on the Hexagon SDK. The
73+
resulting Hexagon launcher binaries can be found in the `apps_Hexagon_launcher`
74+
subdirectory of the cmake build directory. The above command
75+
will build support for Hexagon codegen in the TVM library that requires
76+
`USE_LLVM` to be set to an llvm-config that has the Hexagon target built in.
77+
78+
79+
# Disclaimer
80+
81+
The Hexagon proxy RPC is intended for use with prototyping and does not utilize any
82+
performance acceleration, as such the measured performance may be very poor.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
if(NOT DEFINED USE_HEXAGON_SDK)
19+
message(SEND_ERROR "Please set USE_HEXAGON_SDK to the location of Hexagon SDK")
20+
endif()
21+
if (NOT DEFINED USE_HEXAGON_ARCH)
22+
message(SEND_ERROR "Please set USE_HEXAGON_ARCH to the Hexagon architecture version")
23+
endif()
24+
25+
set(TVM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../")
26+
27+
include(ExternalProject)
28+
include("${TVM_SOURCE_DIR}/cmake/modules/HexagonSDK.cmake")
29+
30+
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
31+
32+
include_directories(SYSTEM ${HEXAGON_SDK_INCLUDES} ${HEXAGON_REMOTE_ROOT})
33+
34+
set(QAIC_EXE "${HEXAGON_QAIC_EXE}")
35+
foreach(INCDIR IN LISTS HEXAGON_SDK_INCLUDES HEXAGON_REMOTE_ROOT)
36+
list(APPEND QAIC_FLAGS "-I${INCDIR}")
37+
endforeach()
38+
39+
set(HEXAGON_PROXY_RPC_SRC "${CMAKE_CURRENT_SOURCE_DIR}/../../")
40+
set(CMAKE_SKIP_RPATH TRUE)
41+
42+
# Qaic for the domain header.
43+
#
44+
# Don't add paths to these filenames, or otherwise cmake may spontaneously
45+
# add -o option to the qaic invocation (with an undesirable path).
46+
set(HEXAGON_PROXY_RPC_IDL "hexagon_proxy_rpc.idl")
47+
set(HEXAGON_PROXY_RPC_H "hexagon_proxy_rpc.h")
48+
set(HEXAGON_PROXY_RPC_SKEL_C "hexagon_proxy_rpc_skel.c")
49+
set(HEXAGON_PROXY_RPC_STUB_C "hexagon_proxy_rpc_stub.c")
50+
51+
include_directories(
52+
"${HEXAGON_PROXY_RPC_SRC}"
53+
"${TVM_SOURCE_DIR}/include"
54+
"${TVM_SOURCE_DIR}/3rdparty/dlpack/include"
55+
"${TVM_SOURCE_DIR}/3rdparty/dmlc-core/include"
56+
)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
cmake_minimum_required(VERSION 3.2)
19+
project(HexagonAndroidRPC C CXX)
20+
21+
include("${CMAKE_CURRENT_SOURCE_DIR}/../HexagonRPC.cmake")
22+
23+
add_custom_command(
24+
OUTPUT ${HEXAGON_PROXY_RPC_STUB_C} ${HEXAGON_PROXY_RPC_H}
25+
COMMAND ${QAIC_EXE} ${QAIC_FLAGS} "${HEXAGON_PROXY_RPC_SRC}/${HEXAGON_PROXY_RPC_IDL}"
26+
MAIN_DEPENDENCY "${HEXAGON_PROXY_RPC_SRC}/${HEXAGON_PROXY_RPC_IDL}"
27+
)
28+
29+
include_directories(SYSTEM
30+
"${HEXAGON_SDK_INCLUDES}"
31+
"${HEXAGON_RPCMEM_ROOT}/inc"
32+
"${CMAKE_CURRENT_BINARY_DIR}" # Output of qaic will go here
33+
)
34+
35+
link_directories(${HEXAGON_REMOTE_ROOT})
36+
37+
add_definitions(-DDMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
38+
39+
set(TVM_RPC_ENV_SOURCES
40+
${HEXAGON_PROXY_RPC_SRC}/rpc_env.cc
41+
)
42+
43+
add_library(rpc_env SHARED
44+
${TVM_RPC_ENV_SOURCES}
45+
${HEXAGON_PROXY_RPC_H}
46+
${HEXAGON_PROXY_RPC_STUB_C}
47+
)
48+
49+
ExternalProject_Add(android_tvm_runtime
50+
SOURCE_DIR "${TVM_SOURCE_DIR}"
51+
BUILD_COMMAND $(MAKE) runtime
52+
CMAKE_ARGS
53+
"-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
54+
"-DANDROID_PLATFORM=${ANDROID_PLATFORM}"
55+
"-DANDROID_ABI=${ANDROID_ABI}"
56+
"-DCMAKE_CXX_STANDARD=14"
57+
"-DUSE_LIBBACKTRACE=OFF"
58+
"-DUSE_LLVM=OFF"
59+
"-DUSE_RPC=ON"
60+
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
61+
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
62+
INSTALL_COMMAND ""
63+
BUILD_ALWAYS ON
64+
)
65+
ExternalProject_Get_Property(android_tvm_runtime BINARY_DIR)
66+
ExternalProject_Add_Step(android_tvm_runtime copy_binaries
67+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
68+
${BINARY_DIR}/libtvm_runtime.so
69+
${CMAKE_CURRENT_BINARY_DIR}
70+
DEPENDEES install
71+
)
72+
73+
add_dependencies(rpc_env android_tvm_runtime)
74+
add_library(a_tvm_runtime SHARED IMPORTED)
75+
set_target_properties(a_tvm_runtime PROPERTIES IMPORTED_LOCATION "${BINARY_DIR}/libtvm_runtime.so")
76+
77+
target_link_libraries(rpc_env cdsprpc log a_tvm_runtime)
78+
79+
# TVM CPP RPC build
80+
set(TVM_RPC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../cpp_rpc")
81+
82+
83+
set(TVM_RPC_SOURCES
84+
${TVM_RPC_DIR}/main.cc
85+
${TVM_RPC_DIR}/rpc_server.cc
86+
)
87+
88+
# Set output to same directory as the other TVM libs
89+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
90+
add_executable(tvm_rpc ${TVM_RPC_SOURCES})
91+
92+
93+
target_include_directories(
94+
tvm_rpc
95+
PUBLIC "${TVM_RPC_DIR}../../include"
96+
PUBLIC "${TVM_RPC_DIR}../../3rdparty/dlpack"
97+
PUBLIC "${TVM_RPC_DIR}../../3rdparty/dmlc-core"
98+
)
99+
100+
add_dependencies(rpc_env android_tvm_runtime)
101+
target_link_libraries(rpc_env a_tvm_runtime)
102+
103+
add_dependencies(tvm_rpc android_tvm_runtime rpc_env)
104+
target_link_libraries(tvm_rpc a_tvm_runtime rpc_env)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
cmake_minimum_required(VERSION 3.2)
19+
project(HexagonRPCSkel C CXX)
20+
21+
include("${CMAKE_CURRENT_SOURCE_DIR}/../HexagonRPC.cmake")
22+
23+
add_custom_command(
24+
OUTPUT ${HEXAGON_PROXY_RPC_SKEL_C} ${HEXAGON_PROXY_RPC_H}
25+
COMMAND ${QAIC_EXE} ${QAIC_FLAGS} "${HEXAGON_PROXY_RPC_SRC}/${HEXAGON_PROXY_RPC_IDL}"
26+
MAIN_DEPENDENCY "${HEXAGON_PROXY_RPC_SRC}/${HEXAGON_PROXY_RPC_IDL}"
27+
)
28+
29+
include_directories(SYSTEM
30+
${HEXAGON_QURT_INCLUDES}
31+
${CMAKE_CURRENT_BINARY_DIR} # Output of qaic will go here
32+
)
33+
34+
link_directories(${HEXAGON_QURT_LIBS})
35+
36+
add_definitions(-D_MACH_I32=int)
37+
add_definitions(-DDMLC_CXX11_THREAD_LOCAL=0)
38+
add_definitions(-DDMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)
39+
40+
# Extra compile flags (both C and C++).
41+
set(EXTRA_COMP_FLAGS
42+
"-O3"
43+
"-m${USE_HEXAGON_ARCH}"
44+
)
45+
string(REGEX REPLACE ";" " " EXTRA_COMP_FLAGS_STR "${EXTRA_COMP_FLAGS}")
46+
set(CMAKE_C_FLAGS "${EXTRA_COMP_FLAGS_STR} ${CMAKE_C_FLAGS}")
47+
set(CMAKE_CXX_FLAGS "${EXTRA_COMP_FLAGS_STR} ${CMAKE_CXX_FLAGS}")
48+
49+
set(SKEL_SRCS
50+
"${HEXAGON_PROXY_RPC_SRC}/hexagon_core.cc"
51+
)
52+
53+
add_library(hexagon_proxy_rpc_skel SHARED
54+
"${HEXAGON_PROXY_RPC_H}"
55+
"${HEXAGON_PROXY_RPC_SKEL_C}"
56+
"${SKEL_SRCS}"
57+
)
58+
59+
ExternalProject_Add(static_hexagon_tvm_runtime
60+
SOURCE_DIR "${TVM_SOURCE_DIR}"
61+
BUILD_COMMAND $(MAKE) runtime
62+
CMAKE_ARGS
63+
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
64+
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
65+
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
66+
"-DCMAKE_CXX_STANDARD=14"
67+
"-DUSE_LIBBACKTRACE=OFF"
68+
"-DUSE_LLVM=OFF"
69+
"-DUSE_RPC=OFF"
70+
"-DBUILD_STATIC_RUNTIME=ON"
71+
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
72+
INSTALL_COMMAND ""
73+
BUILD_ALWAYS ON
74+
)
75+
ExternalProject_Get_Property(static_hexagon_tvm_runtime BINARY_DIR)
76+
77+
add_dependencies(hexagon_proxy_rpc_skel static_hexagon_tvm_runtime)
78+
add_library(h_tvm_runtime STATIC IMPORTED)
79+
set_target_properties(h_tvm_runtime PROPERTIES IMPORTED_LOCATION "${BINARY_DIR}/libtvm_runtime.a")
80+
81+
target_link_libraries(hexagon_proxy_rpc_skel -Wl,--whole-archive h_tvm_runtime -Wl,--no-whole-archive)

0 commit comments

Comments
 (0)