Skip to content

Commit

Permalink
[Hexagon] Add RPC Mechanism for Hexagon (apache#9631)
Browse files Browse the repository at this point in the history
* Add Hexagon RPC

* removed android remote and updated Readme

* Add check for workspace size

* Make libtvm_runtime consistent for Android

* Remove root access

* Fix some docstrings

* Make stack remote size as parameter

* add documentation

* Refactor test conftest

* clang format

* Decoupled USE_HEXAGON_RPC

* fix creation of test base directory on android

* Address global variable

* Fix format and Cleanup cmake

* Fix build for other targets
  • Loading branch information
mehrdadh authored and baoxinqi committed Dec 27, 2021
1 parent 16ebc94 commit 61cbafb
Show file tree
Hide file tree
Showing 22 changed files with 1,580 additions and 111 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ tvm_option(USE_ROCM "Build with ROCM" OFF)
tvm_option(ROCM_PATH "The path to rocm" /opt/rocm)
tvm_option(USE_HEXAGON_DEVICE "Build with Hexagon device support in TVM runtime" OFF)
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)
tvm_option(USE_HEXAGON_RPC "Enable Hexagon RPC using minRPC implementation over Android." OFF)
tvm_option(USE_HEXAGON_LAUNCHER "Build the Hexagon graph launcher application" OFF)
tvm_option(USE_HEXAGON_PROXY_RPC "Build the Hexagon Proxy RPC server application" OFF)
tvm_option(USE_RPC "Build with RPC" ON)
Expand Down
12 changes: 11 additions & 1 deletion apps/cpp_rpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ set(TVM_RPC_SOURCES
rpc_server.cc
)

set(TVM_RPC_LINKER_LIBS "")

if(WIN32)
list(APPEND TVM_RPC_SOURCES win32_process.cc)
endif()
Expand Down Expand Up @@ -43,4 +45,12 @@ target_include_directories(
PUBLIC DMLC_PATH
)

target_link_libraries(tvm_rpc tvm_runtime)
if (BUILD_FOR_ANDROID AND USE_HEXAGON_SDK)
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
link_directories(${HEXAGON_REMOTE_ROOT})
list(APPEND TVM_RPC_LINKER_LIBS cdsprpc log)
endif()

list(APPEND TVM_RPC_LINKER_LIBS tvm_runtime)

target_link_libraries(tvm_rpc ${TVM_RPC_LINKER_LIBS})
119 changes: 119 additions & 0 deletions cmake/libs/hexagon_rpc_skel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

cmake_minimum_required(VERSION 3.2)
include(ExternalProject)
project(HexagonRPCSkel C CXX)

set(TVM_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
set(TVM_SRC_DIR "${TVM_SOURCE_DIR}/src")


include("${TVM_SOURCE_DIR}/cmake/modules/HexagonSDK.cmake")

find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")

include_directories(SYSTEM ${HEXAGON_SDK_INCLUDES} ${HEXAGON_REMOTE_ROOT})

set(HEXAGON_RPC_H "hexagon_rpc.h")
set(HEXAGON_RPC_SKEL_C "hexagon_rpc_skel.c")
set(HEXAGON_RPC_STUB_C "hexagon_rpc_stub.c")

include_directories(
"${TVM_SOURCE_DIR}/include"
"${TVM_SOURCE_DIR}/3rdparty/dlpack/include"
"${TVM_SOURCE_DIR}/3rdparty/dmlc-core/include"
)

set(QAIC_EXE "${HEXAGON_QAIC_EXE}")
foreach(INCDIR IN LISTS HEXAGON_SDK_INCLUDES HEXAGON_REMOTE_ROOT)
list(APPEND QAIC_FLAGS "-I${INCDIR}")
endforeach()

add_custom_command(
OUTPUT ${HEXAGON_RPC_SKEL_C} ${HEXAGON_RPC_H}
COMMAND ${QAIC_EXE} ${QAIC_FLAGS} "${TVM_SRC_DIR}/runtime/hexagon/rpc/hexagon_rpc.idl"
MAIN_DEPENDENCY "${TVM_SRC_DIR}/runtime/hexagon/rpc/hexagon_rpc.idl"
)

include_directories(SYSTEM
${HEXAGON_QURT_INCLUDES}
${CMAKE_CURRENT_BINARY_DIR} # Output of qaic will go here
)

link_directories(${HEXAGON_QURT_LIBS})

add_definitions(-D_MACH_I32=int)
add_definitions(-DDMLC_CXX11_THREAD_LOCAL=0)
add_definitions(-DDMLC_USE_LOGGING_LIBRARY=<tvm/runtime/logging.h>)

# Extra compile flags (both C and C++).
set(EXTRA_COMP_FLAGS
"-O3"
"-m${USE_HEXAGON_ARCH}"
)
string(REGEX REPLACE ";" " " EXTRA_COMP_FLAGS_STR "${EXTRA_COMP_FLAGS}")
set(CMAKE_C_FLAGS "${EXTRA_COMP_FLAGS_STR} ${CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${EXTRA_COMP_FLAGS_STR} ${CMAKE_CXX_FLAGS}")

set(SKEL_SRCS
"${TVM_SRC_DIR}/runtime/hexagon/rpc/hexagon/rpc_server.cc"
)

set(MINRPC_SRCS
"${TVM_SRC_DIR}/runtime/minrpc/minrpc_server.h"
"${TVM_SRC_DIR}/runtime/minrpc/rpc_reference.h"
)

set(TVM_RPC_SRC
"${TVM_SRC_DIR}/runtime/rpc/rpc_module.cc"
"${TVM_SRC_DIR}/runtime/rpc/rpc_endpoint.cc"
"${TVM_SRC_DIR}/runtime/rpc/rpc_session.cc"
"${TVM_SRC_DIR}/runtime/rpc/rpc_local_session.cc"
)

add_library(hexagon_rpc_skel SHARED
"${HEXAGON_RPC_H}"
"${HEXAGON_RPC_SKEL_C}"
"${SKEL_SRCS}"
"${MINRPC_SRCS}"
"${TVM_RPC_SRC}"
)

ExternalProject_Add(static_hexagon_tvm_runtime
SOURCE_DIR "${TVM_SOURCE_DIR}"
BUILD_COMMAND $(MAKE) runtime
CMAKE_ARGS
"-DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}"
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
"-DCMAKE_CXX_STANDARD=14"
"-DUSE_LIBBACKTRACE=OFF"
"-DUSE_LLVM=OFF"
"-DUSE_RPC=OFF"
"-DBUILD_STATIC_RUNTIME=ON"
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
INSTALL_COMMAND ""
BUILD_ALWAYS ON
)
ExternalProject_Get_Property(static_hexagon_tvm_runtime BINARY_DIR)

add_dependencies(hexagon_rpc_skel static_hexagon_tvm_runtime)
add_library(h_tvm_runtime STATIC IMPORTED)
set_target_properties(h_tvm_runtime PROPERTIES IMPORTED_LOCATION "${BINARY_DIR}/libtvm_runtime.a")

target_link_libraries(hexagon_rpc_skel -Wl,--whole-archive h_tvm_runtime -Wl,--no-whole-archive)
138 changes: 133 additions & 5 deletions cmake/modules/Hexagon.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ if(BUILD_FOR_HEXAGON)
include_directories(SYSTEM ${HEXAGON_SDK_INCLUDES} ${HEXAGON_QURT_INCLUDES})
endif()


if (NOT USE_HEXAGON_SDK STREQUAL "" AND
NOT USE_HEXAGON_SDK STREQUAL "/path/to/sdk")
set(HEXAGON_SDK_PATH_DEFINED ${USE_HEXAGON_SDK})
Expand All @@ -73,10 +72,12 @@ endif()
# e.g. when compiling the TVM runtime for Hexagon.
if (NOT BUILD_FOR_HEXAGON AND NOT BUILD_FOR_ANDROID)
if(USE_HEXAGON_LAUNCHER STREQUAL "OFF" AND
USE_HEXAGON_PROXY_RPC STREQUAL "OFF")
USE_HEXAGON_PROXY_RPC STREQUAL "OFF" AND NOT USE_HEXAGON_RPC)
if(USE_HEXAGON_DEVICE STREQUAL "OFF")
list(APPEND COMPILER_SRCS src/target/opt/build_hexagon_off.cc)
return()
if (NOT USE_HEXAGON_RPC)
return()
endif()
elseif(NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}" AND
NOT USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
set(ERROR_MSG
Expand Down Expand Up @@ -202,6 +203,103 @@ if(USE_HEXAGON_PROXY_RPC STREQUAL "ON")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${RPC_BINARY_DIR}")
endif()

if(USE_HEXAGON_RPC)
if(DEFINED USE_ANDROID_TOOLCHAIN)
if(NOT DEFINED ANDROID_PLATFORM)
message(SEND_ERROR "Please set ANDROID_PLATFORM "
"when providing an Android cmake toolchain.")
endif()
if(NOT DEFINED ANDROID_ABI)
message(SEND_ERROR "Please set ANDROID_ABI "
"when providing an Android cmake toolchain.")
endif()
else()
message(SEND_ERROR "Please set USE_ANDROID_TOOLCHAIN to build the android "
"RPC server for Hexagon.")
endif()

if(NOT DEFINED USE_HEXAGON_SDK)
message(SEND_ERROR "Please set USE_HEXAGON_SDK to build the android "
"RPC server for Hexagon RPC.")
endif()
if(NOT DEFINED USE_HEXAGON_ARCH)
message(SEND_ERROR "Please set USE_HEXAGON_ARCH to build the android "
"RPC server for Hexagon RPC.")
endif()
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")

set(HEXAGON_RPC_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/hexagon_rpc")
file(MAKE_DIRECTORY ${HEXAGON_RPC_OUTPUT})

# Android Part
ExternalProject_Add(android_runtime_rpc
SOURCE_DIR "${CMAKE_SOURCE_DIR}"
BUILD_COMMAND $(MAKE) runtime tvm_rpc
CMAKE_ARGS
"-DCMAKE_TOOLCHAIN_FILE=${USE_ANDROID_TOOLCHAIN}"
"-DUSE_ANDROID_TOOLCHAIN=${USE_ANDROID_TOOLCHAIN}"
"-DANDROID_PLATFORM=${ANDROID_PLATFORM}"
"-DANDROID_ABI=${ANDROID_ABI}"
"-DCMAKE_CXX_STANDARD=14"
"-DUSE_LIBBACKTRACE=OFF"
"-DUSE_LLVM=OFF"
"-DUSE_RPC=ON"
"-DUSE_CPP_RPC=ON"
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
"-DCMAKE_VERBOSE_MAKEFILE=ON"
INSTALL_COMMAND ""
BUILD_ALWAYS ON
)
ExternalProject_Get_Property(android_runtime_rpc BINARY_DIR)
ExternalProject_Add_Step(android_runtime_rpc copy_binary_runtime
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BINARY_DIR}/libtvm_runtime.so
${HEXAGON_RPC_OUTPUT}/libtvm_runtime.so
DEPENDEES install
)
ExternalProject_Add_Step(android_runtime_rpc copy_binary_rpc
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BINARY_DIR}/tvm_rpc
${HEXAGON_RPC_OUTPUT}/tvm_rpc_android
DEPENDEES install
)

if("${USE_HEXAGON_TOOLCHAIN}" STREQUAL "")
message(SEND_ERROR "Please set USE_HEXAGON_TOOLCHAIN to build the hexagon "
"RPC SKEL.")
endif()
find_hexagon_toolchain()
message(STATUS "HEXAGON_TOOLCHAIN: ${HEXAGON_TOOLCHAIN}")

# Hexagon Part
ExternalProject_Add(hexagon_rpc_skel
SOURCE_DIR "${CMAKE_SOURCE_DIR}/cmake/libs/hexagon_rpc_skel"
INSTALL_DIR "${LAUNCHER_BINARY_DIR}"
CMAKE_ARGS
"-DCMAKE_C_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang"
"-DCMAKE_CXX_COMPILER=${HEXAGON_TOOLCHAIN}/bin/hexagon-clang++"
"-DFASTRPC_LIBS=SKEL"
"-DUSE_HEXAGON_ARCH=${USE_HEXAGON_ARCH}"
"-DUSE_HEXAGON_SDK=${USE_HEXAGON_SDK}"
INSTALL_COMMAND ""
BUILD_ALWAYS ON
)
ExternalProject_Get_Property(hexagon_rpc_skel BINARY_DIR)
ExternalProject_Add_Step(hexagon_rpc_skel copy_hexagon_skel
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${BINARY_DIR}/libhexagon_rpc_skel.so
${HEXAGON_RPC_OUTPUT}/libhexagon_rpc_skel.so
DEPENDEES install
)

# copy android_bash template file
configure_file("${CMAKE_SOURCE_DIR}/src/runtime/hexagon/rpc/android_bash.sh.template"
${HEXAGON_RPC_OUTPUT} COPYONLY)

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${HEXAGON_RPC_OUTPUT}")
endif()

if(USE_HEXAGON_DEVICE STREQUAL "${PICK_SIM}")
find_hexagon_toolchain()
message(STATUS "Hexagon toolchain: ${HEXAGON_TOOLCHAIN}")
Expand All @@ -227,6 +325,7 @@ elseif(USE_HEXAGON_DEVICE STREQUAL "${PICK_HW}")
${HEXAGON_RPCMEM_ROOT}/inc
${HEXAGON_REMOTE_ROOT}
)

list(APPEND TVM_RUNTIME_LINKER_LIBS "dl")
if(BUILD_FOR_ANDROID)
# Hexagon runtime uses __android_log_print, which is in liblog.
Expand All @@ -241,10 +340,39 @@ if (USE_HEXAGON_DEVICE STREQUAL "${PICK_NONE}")
elseif(BUILD_FOR_ANDROID AND HEXAGON_SDK_PATH_DEFINED)
list(APPEND RUNTIME_HEXAGON_SRCS src/runtime/hexagon/proxy_rpc/device_api.cc)
else()
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/host/*.cc)
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/host/*.cc)
endif()
else()
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/android/*.cc)
endif()

if(USE_HEXAGON_RPC)
file(GLOB RUNTIME_HEXAGON_SRCS src/runtime/hexagon/host/*.cc)
endif()

if(USE_HEXAGON_SDK AND BUILD_FOR_ANDROID)
find_hexagon_sdk_root("${USE_HEXAGON_SDK}" "${USE_HEXAGON_ARCH}")
include_directories(SYSTEM ${HEXAGON_SDK_INCLUDES} ${HEXAGON_REMOTE_ROOT})

set(QAIC_EXE "${HEXAGON_QAIC_EXE}")
foreach(INCDIR IN LISTS HEXAGON_SDK_INCLUDES HEXAGON_REMOTE_ROOT)
list(APPEND QAIC_FLAGS "-I${INCDIR}")
endforeach()

set(HEXAGON_RPC_DIR "${CMAKE_SOURCE_DIR}/src/runtime/hexagon/rpc")
set(RPC_IDL "hexagon_rpc.idl")
set(RPC_H "hexagon_rpc.h")
set(RPC_STUB_C "hexagon_rpc_stub.c")

add_custom_command(
OUTPUT "${HEXAGON_RPC_DIR}/${RPC_STUB_C}" "${HEXAGON_RPC_DIR}/${RPC_H}"
COMMAND ${QAIC_EXE} ${QAIC_FLAGS} "${HEXAGON_RPC_DIR}/${RPC_IDL}" -o ${HEXAGON_RPC_DIR}
MAIN_DEPENDENCY "${HEXAGON_RPC_DIR}/${RPC_IDL}"
)
file(GLOB HEXAGON_RPC_CPP "${HEXAGON_RPC_DIR}/android/*.cc")
set(HEXAGON_RPC_STUB_C "${HEXAGON_RPC_DIR}/${RPC_STUB_C}")
endif()

list(APPEND RUNTIME_SRCS ${RUNTIME_HEXAGON_SRCS} ${RUNTIME_HEXAGON_SIM_SRCS}
${RUNTIME_HEXAGON_DEVICE_SRCS} ${RUNTIME_HEXAGON_COMMON_SRCS})
${RUNTIME_HEXAGON_DEVICE_SRCS} ${HEXAGON_RPC_CPP} ${HEXAGON_RPC_STUB_C}
${RUNTIME_HEXAGON_COMMON_SRCS})
17 changes: 17 additions & 0 deletions python/tvm/contrib/hexagon/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Hexagon APIs."""
Loading

0 comments on commit 61cbafb

Please sign in to comment.