-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
263 lines (237 loc) · 8.09 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
cmake_minimum_required(VERSION 3.25)
# For std::filesystem in onnx optimizer
# Must be a cache variable and be set before project()
# Reference: https://cmake.org/cmake/help/latest/variable/CMAKE_OSX_DEPLOYMENT_TARGET.html
# It can be a normal variable if policy CMP0126 is set to NEW.
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.15 CACHE STRING "Minimum OS X deployment version")
project(faster-rwkv CXX)
set(CMAKE_CXX_STANDARD 17)
include(FetchContent)
option(FR_ENABLE_CUDA "Enable CUDA" OFF)
# ONNX support is a WIP
option(FR_ENABLE_ONNX_EXPORTING "Enable ONNX exporting" OFF)
option(FR_ENABLE_ONNX "Enable ONNX" OFF)
if (FR_ENABLE_ONNX OR FR_ENABLE_ONNX_EXPORTING)
option(FR_BUILD_PROTOBUF "Build protobuf (needed by onnx)" ON)
else()
option(FR_BUILD_PROTOBUF "Build protobuf (needed by onnx)" OFF)
endif()
option(FR_ENABLE_NCNN "Enable NCNN" ON)
option(FR_BUILD_PYTHON "" OFF)
if (DEFINED ANDROID_NDK)
option(FR_BUILD_JNI "" ON)
else()
option(FR_BUILD_JNI "" OFF)
endif()
option(FR_ENABLE_TESTS "Enable the tests of faster-rwkv" OFF)
option(FR_MSVC_STATIC_RUNTIME "" OFF)
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
if (FR_ENABLE_CUDA)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
set(CMAKE_CUDA_ARCHITECTURES native)
endif()
enable_language(CUDA)
set(cuda_kernel_srcs
kernels/cuda/activations/silu.cu
kernels/cuda/matmul.cpp
kernels/cuda/cat.cu
kernels/cuda/layer_norm.cu
kernels/cuda/group_norm.cu
kernels/cuda/cast_dtype.cu
kernels/cuda/att.cu
kernels/cuda/att_seq.cu
kernels/cuda/ffn.cu
kernels/cuda/ffn_seq.cu
kernels/cuda/fill.cu
kernels/cuda/flip.cu
kernels/cuda/pad.cu
kernels/cuda/repeat.cu
kernels/cuda/slice.cu
kernels/cuda/transpose.cu
kernels/cuda/element_wise.cu
kernels/cuda/allocator.cpp
)
endif()
if (FR_ENABLE_NCNN)
FetchContent_Declare(
ncnn
GIT_REPOSITORY https://github.com/daquexian/ncnn
# This commit reduces peak memory usage
# https://github.com/Tencent/ncnn/pull/4966
GIT_TAG c0daa4fd
SYSTEM
)
option(NCNN_BUILD_BENCHMARK "" OFF)
option(NCNN_BUILD_TOOLS "" OFF)
option(NCNN_BUILD_EXAMPLES "" OFF)
option(NCNN_BUILD_TESTS "" OFF)
option(NCNN_PIXEL "" OFF)
option(NCNN_PIXEL_ROTATE "" OFF)
option(NCNN_PIXEL_AFFINE "" OFF)
option(NCNN_PIXEL_DRAWING "" OFF)
option(NCNN_DISABLE_EXCEPTION "" OFF)
if (FR_BUILD_PYTHON)
# pybind11 requires RTTI
option(NCNN_DISABLE_RTTI "" OFF)
endif()
if (NOT CMAKE_SYSTEM_NAME STREQUAL "Android" AND NOT CMAKE_CROSSCOMPILING)
option(NCNN_OPENMP "" OFF)
option(NCNN_SIMPLEOMP "" ON)
endif()
# Termux
if (CMAKE_SYSTEM_NAME STREQUAL "Android" AND NOT DEFINED ANDROID_NDK)
option(NCNN_PLATFORM_API "" OFF)
endif()
include(disable_unused_ncnn_layers)
FetchContent_MakeAvailable(ncnn)
set(ncnn_deps ncnn)
set(ncnn_kernel_srcs
kernels/ncnn/init_model.cpp
kernels/ncnn/model_forward.cpp
)
endif()
if (FR_BUILD_PROTOBUF)
option(protobuf_BUILD_TESTS "Build tests" OFF)
option(protobuf_MSVC_STATIC_RUNTIME "" ${FR_USE_MSVC_STATIC_RUNTIME})
FetchContent_Declare(
protobuf
GIT_REPOSITORY https://github.com/protocolbuffers/protobuf
GIT_TAG v3.20.1
SYSTEM
SOURCE_SUBDIR cmake
)
FetchContent_MakeAvailable(protobuf)
endif()
if (FR_ENABLE_ONNX OR FR_ENABLE_ONNX_EXPORTING)
FetchContent_Declare(
onnx
GIT_REPOSITORY https://github.com/onnx/onnx
GIT_TAG v1.14.0
SYSTEM
)
option(ONNX_USE_MSVC_STATIC_RUNTIME "" ${FR_USE_MSVC_STATIC_RUNTIME})
FetchContent_MakeAvailable(onnx)
if (FR_ENABLE_ONNX_EXPORTING)
set(onnx_kernel_srcs
kernels/export-onnx/kernels.cpp
)
endif()
endif()
if (FR_ENABLE_ONNX)
# WIP
option(onnxruntime_USE_FULL_PROTOBUF "" ON)
option(onnxruntime_BUILD_UNIT_TESTS "Build ONNXRuntime unit tests" OFF)
option(onnxruntime_BUILD_SHARED_LIB "Build a shared library" ON)
option(onnxruntime_DISABLE_RTTI "Disable RTTI" OFF)
option(onnxruntime_DISABLE_EXCEPTIONS "Disable exception handling. Requires onnxruntime_MINIMAL_BUILD currently." OFF)
FetchContent_Declare(
onnxruntime
GIT_REPOSITORY https://github.com/microsoft/onnxruntime
GIT_TAG 5af6279440a0db698b0afbfc3de655400562831f
SOURCE_SUBDIR cmake
SYSTEM
)
FetchContent_MakeAvailable(onnxruntime)
list(APPEND onnx_kernel_srcs
kernels/onnx/init_model.cpp
kernels/onnx/model_forward.cpp
)
endif()
FetchContent_Declare(
msgpack
GIT_REPOSITORY https://github.com/msgpack/msgpack-c
GIT_TAG cpp-6.1.0
SYSTEM
)
option(MSGPACK_USE_BOOST "" OFF)
FetchContent_MakeAvailable(msgpack)
add_library(faster_rwkv_internal
utils.cpp
model.cpp
tensor.cpp
tokenizer.cpp
sampler.cpp
kernels/shape/shape_inference.cpp
kernels/cpu/allocator.cpp
kernels/cpu/fill.cpp
kernels/cpu/cast_dtype.cpp
kernels/cpu/repeat.cpp
kernels/cpu/transpose.cpp
kernels/cpu/slice.cpp
kernels/cpu/flip.cpp
kernels/cpu/pad.cpp
kernels/default/gather_ops.cpp
kernels/default/view_ops.cpp
kernels/cpu/softmax.cpp
kernels/default/att.cpp
kernels/default/ffn.cpp
kernels/default/init_model.cpp
kernels/default/model_forward.cpp
kernels/default/model_forward_seq.cpp
kernels/export-ncnn/kernels.cpp
${cuda_kernel_srcs}
${ncnn_kernel_srcs}
${onnx_kernel_srcs}
)
target_link_libraries(faster_rwkv_internal PUBLIC msgpack-cxx)
target_link_libraries(faster_rwkv_internal PUBLIC ${ncnn_deps})
target_include_directories(faster_rwkv_internal PUBLIC ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
if (FR_ENABLE_CUDA)
find_package(CUDAToolkit REQUIRED)
target_link_libraries(faster_rwkv_internal PUBLIC CUDA::cudart CUDA::cublas)
target_compile_definitions(faster_rwkv_internal PUBLIC FR_ENABLE_CUDA)
endif()
if (FR_ENABLE_NCNN)
target_compile_definitions(faster_rwkv_internal PUBLIC FR_ENABLE_NCNN)
endif()
if (FR_ENABLE_ONNX_EXPORTING)
target_compile_definitions(faster_rwkv_internal PUBLIC FR_ENABLE_ONNX)
target_link_libraries(faster_rwkv_internal PRIVATE onnx)
endif()
if (FR_ENABLE_ONNX)
target_link_libraries(faster_rwkv_internal PRIVATE onnxruntime)
target_include_directories(faster_rwkv_internal PRIVATE
${CMAKE_BINARY_DIR}/_deps/onnxruntime-src/include/onnxruntime/core/session/
${CMAKE_BINARY_DIR}/_deps/onnxruntime-src/include/
)
endif()
if (DEFINED ANDROID_NDK)
target_compile_definitions(faster_rwkv_internal PUBLIC FR_ENABLE_ANDROID_ASSET)
endif()
add_library(faster_rwkv INTERFACE)
target_link_libraries(faster_rwkv INTERFACE "$<LINK_LIBRARY:WHOLE_ARCHIVE,faster_rwkv_internal>")
add_executable(export_ncnn export_ncnn.cpp)
target_link_libraries(export_ncnn faster_rwkv)
option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." OFF)
option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" OFF)
FetchContent_Declare(
benchmark
GIT_REPOSITORY https://github.com/google/benchmark
GIT_TAG v1.8.2
SYSTEM
)
FetchContent_MakeAvailable(benchmark)
add_executable(bench_model bench_model.cpp)
target_link_libraries(bench_model benchmark::benchmark faster_rwkv)
add_executable(chat chat.cpp)
target_link_libraries(chat faster_rwkv)
add_executable(abc_music abc_music.cpp)
target_link_libraries(abc_music faster_rwkv)
add_executable(midi_music midi_music.cpp)
target_link_libraries(midi_music faster_rwkv)
if (FR_ENABLE_ONNX_EXPORTING)
add_subdirectory(export_onnx)
endif()
if (FR_BUILD_JNI)
add_subdirectory(aar)
endif()
if (FR_BUILD_PYTHON)
add_subdirectory(python)
endif()
add_subdirectory(tools)
if(FR_ENABLE_TESTS)
add_subdirectory(tests)
endif()