Skip to content

Commit d7fba09

Browse files
Ma-Dan马丹
andauthored
[runtime/ios] add iOS runtime (#1549)
* Add iOS build files and test application. * Clean up code and add license information * Update Podfile * Add license * Fix lint tab check * Fix lint trailing whitespace * Simplify build and fix some cpplint * Fix some cpplint * Add NOLINT to Objective C header file * Merge ios_asr_model into torch_asr_model * Fix lint * Fix lint * Fix code style Co-authored-by: 马丹 <[email protected]>
1 parent 74d2826 commit d7fba09

File tree

34 files changed

+2327
-10
lines changed

34 files changed

+2327
-10
lines changed

runtime/core/cmake/libtorch.cmake

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ if(TORCH)
55
add_definitions(-DUSE_GPU)
66
set(CUDA_NAME "cu113")
77
endif()
8+
if(IOS)
9+
add_definitions(-DIOS)
10+
endif()
811
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
912
if(GPU)
1013
message(FATAL_ERROR "GPU on Windows is unsupported, you can use CPU version")
@@ -40,17 +43,24 @@ if(TORCH)
4043
endif()
4144
set(LIBTORCH_URL "https://download.pytorch.org/libtorch/cpu/libtorch-macos-${PYTORCH_VERSION}.zip")
4245
set(URL_HASH "SHA256=07cac2c36c34f13065cb9559ad5270109ecbb468252fb0aeccfd89322322a2b5")
46+
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
47+
if(GPU)
48+
message(FATAL_ERROR "GPU on iOS is unsupported, you can use CPU version")
49+
endif()
4350
else()
4451
message(FATAL_ERROR "Unsupported CMake System Name '${CMAKE_SYSTEM_NAME}' (expected 'Windows', 'Linux' or 'Darwin')")
4552
endif()
4653

47-
FetchContent_Declare(libtorch
48-
URL ${LIBTORCH_URL}
49-
URL_HASH ${URL_HASH}
50-
)
51-
FetchContent_MakeAvailable(libtorch)
52-
find_package(Torch REQUIRED PATHS ${libtorch_SOURCE_DIR} NO_DEFAULT_PATH)
53-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -DC10_USE_GLOG")
54+
# iOS use LibTorch from pod install
55+
if(NOT IOS)
56+
FetchContent_Declare(libtorch
57+
URL ${LIBTORCH_URL}
58+
URL_HASH ${URL_HASH}
59+
)
60+
FetchContent_MakeAvailable(libtorch)
61+
find_package(Torch REQUIRED PATHS ${libtorch_SOURCE_DIR} NO_DEFAULT_PATH)
62+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -DC10_USE_GLOG")
63+
endif()
5464

5565
if(MSVC)
5666
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")

runtime/core/decoder/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ set(decoder_srcs
77
ctc_endpoint.cc
88
)
99

10-
if(NOT TORCH AND NOT ONNX AND NOT XPU)
11-
message(FATAL_ERROR "Please build with TORCH or ONNX or XPU!!!")
10+
if(NOT TORCH AND NOT ONNX AND NOT XPU AND NOT IOS)
11+
message(FATAL_ERROR "Please build with TORCH or ONNX or XPU or IOS!!!")
1212
endif()
13-
if(TORCH)
13+
if(TORCH OR IOS)
1414
list(APPEND decoder_srcs torch_asr_model.cc)
1515
endif()
1616
if(ONNX)

runtime/core/decoder/torch_asr_model.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
#include <stdexcept>
2323

2424
#include "torch/script.h"
25+
#ifndef IOS
2526
#include "torch/torch.h"
27+
#endif
2628

2729
namespace wenet {
2830

31+
#ifndef IOS
2932
void TorchAsrModel::InitEngineThreads(int num_threads) {
3033
// For multi-thread performance
3134
at::set_num_threads(num_threads);
@@ -36,6 +39,7 @@ void TorchAsrModel::InitEngineThreads(int num_threads) {
3639
VLOG(1) << "Num intra-op threads: " << at::get_num_threads();
3740
VLOG(1) << "Num inter-op threads: " << at::get_num_interop_threads();
3841
}
42+
#endif
3943

4044
void TorchAsrModel::Read(const std::string& model_path) {
4145
torch::DeviceType device = at::kCPU;

runtime/core/decoder/torch_asr_model.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include <vector>
2323

2424
#include "torch/script.h"
25+
#ifndef IOS
2526
#include "torch/torch.h"
27+
#endif
2628

2729
#include "decoder/asr_model.h"
2830
#include "utils/utils.h"
@@ -31,8 +33,10 @@ namespace wenet {
3133

3234
class TorchAsrModel : public AsrModel {
3335
public:
36+
#ifndef IOS
3437
// Note: Do not call the InitEngineThreads function more than once.
3538
static void InitEngineThreads(int num_threads = 1);
39+
#endif
3640

3741
public:
3842
using TorchModule = torch::jit::script::Module;

runtime/core/patch/openfst/src/bin/CMakeLists.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)