Skip to content

Commit 95e3b53

Browse files
Support qwen phi gemma whisper (#14110)
### Summary 1. Added AoT support for qwen2, qwen2.5, qwen3, gemma2, gemma3, phi3, phi4, whisper 2. Added runner support for qwen, gemma2, phi3 ### TODO 1. Add runner support for gemma3, phi4 and whisper.
1 parent 4c90a53 commit 95e3b53

File tree

78 files changed

+21680
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+21680
-167
lines changed

backends/mediatek/preprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
HEADER_SIZE = 13
2727
HEADER_VERSION = 1
2828
REQUIRED_COMPILE_SPEC_KEYS = {"platform-config"}
29-
SUPPORTED_PLATFORM_CONFIGS = {"mt6989", "mt6991"}
29+
SUPPORTED_PLATFORM_CONFIGS = {"mt6989", "mt6991", "mt6993"}
3030

3131

3232
def assert_default_dim_order(edge_graph_module: torch.fx.GraphModule) -> None:

backends/mediatek/runtime/include/api/NeuronAdapter.h

Lines changed: 2385 additions & 0 deletions
Large diffs are not rendered by default.

examples/mediatek/CMakeLists.txt

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ if(${ANDROID})
122122
${EXTENSIONS_LLM_DIR}/tokenizers/third-party/abseil-cpp
123123
)
124124
set(THIRD_PARTY_RE2_DIR ${EXTENSIONS_LLM_DIR}/tokenizers/third-party/re2)
125+
set(THIRD_PARTY_JSON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../third-party/json)
126+
set(THIRD_PARTY_UNICODE_DIR
127+
${EXTENSIONS_LLM_DIR}/tokenizers/third-party/llama.cpp-unicode
128+
)
129+
set(THIRD_PARTY_PCRE2_DIR ${EXTENSIONS_LLM_DIR}/tokenizers/third-party/pcre2)
125130
set(ABSL_ENABLE_INSTALL ON)
126131
set(ABSL_PROPAGATE_CXX_STD ON)
127132
set(_pic_flag ${CMAKE_POSITION_INDEPENDENT_CODE})
@@ -134,9 +139,22 @@ if(${ANDROID})
134139
${THIRD_PARTY_RE2_DIR}
135140
${CMAKE_CURRENT_BINARY_DIR}/tokenizers/third-party/re2
136141
)
142+
add_subdirectory(
143+
${THIRD_PARTY_JSON_DIR}
144+
${CMAKE_CURRENT_BINARY_DIR}/tokenizers/third-party/json
145+
)
146+
add_subdirectory(
147+
${THIRD_PARTY_UNICODE_DIR}
148+
${CMAKE_CURRENT_BINARY_DIR}/tokenizers/third-party/llama.cpp-unicode
149+
)
150+
add_subdirectory(
151+
${THIRD_PARTY_PCRE2_DIR}
152+
${CMAKE_CURRENT_BINARY_DIR}/tokenizers/third-party/pcre2
153+
)
137154
set(CMAKE_POSITION_INDEPENDENT_CODE ${_pic_flag})
138155

139156
# Build tokenizers
157+
set(SUPPORT_REGEX_LOOKAHEAD ON)
140158
set(LLAMA2_TOKENIZER_DIR ${EXTENSIONS_LLM_DIR}/tokenizers)
141159
add_library(tokenizer STATIC)
142160
target_include_directories(
@@ -147,8 +165,12 @@ if(${ANDROID})
147165
${LLAMA2_TOKENIZER_DIR}/include
148166
${CMAKE_CURRENT_BINARY_DIR}/tokenizers/third-party/pcre2
149167
${EXECUTORCH_ROOT}/extension/llm/tokenizers/include
168+
${THIRD_PARTY_JSON_DIR}
169+
${THIRD_PARTY_UNICODE_DIR}/include
170+
${THIRD_PARTY_PCRE2_DIR}
150171
)
151172
target_link_libraries(tokenizer PRIVATE re2::re2)
173+
152174
target_sources(
153175
tokenizer
154176
PRIVATE
@@ -157,9 +179,55 @@ if(${ANDROID})
157179
${LLAMA2_TOKENIZER_DIR}/src/regex.cpp
158180
${LLAMA2_TOKENIZER_DIR}/src/bpe_tokenizer_base.cpp
159181
${LLAMA2_TOKENIZER_DIR}/src/re2_regex.cpp
182+
${LLAMA2_TOKENIZER_DIR}/src/hf_tokenizer.cpp
183+
${LLAMA2_TOKENIZER_DIR}/src/pre_tokenizer.cpp
184+
${LLAMA2_TOKENIZER_DIR}/src/token_decoder.cpp
185+
${LLAMA2_TOKENIZER_DIR}/src/normalizer.cpp
186+
${LLAMA2_TOKENIZER_DIR}/third-party/llama.cpp-unicode/src/unicode.cpp
187+
${LLAMA2_TOKENIZER_DIR}/third-party/llama.cpp-unicode/src/unicode-data.cpp
160188
${CMAKE_CURRENT_SOURCE_DIR}/../models/llama/tokenizer/llama_tiktoken.cpp
161189
)
162190

191+
# Add support for regex_lookahead
192+
set(PCRE2_STATIC_PIC ON)
193+
set(PCRE2_BUILD_PCRE2_8 ON)
194+
set(PCRE2_BUILD_PCRE2_16 OFF)
195+
set(PCRE2_BUILD_PCRE2_32 OFF)
196+
set(PCRE2_BUILD_TESTS OFF)
197+
set(PCRE2_BUILD_PCRE2GREP OFF)
198+
set(PCRE2_BUILD_PCRE2TEST OFF)
199+
set(PCRE2_BUILD_PCRE2GPERF OFF)
200+
set(PCRE2_BUILD_DOCS OFF)
201+
set(PCRE2_BUILD_LIBPCRE2_PDB OFF)
202+
203+
# Set the INTERFACE_INCLUDE_DIRECTORIES property for pcre2-8-static
204+
set_target_properties(
205+
pcre2-8-static
206+
PROPERTIES
207+
INTERFACE_INCLUDE_DIRECTORIES
208+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/tokenizers/third-party/pcre2>
209+
)
210+
add_library(
211+
regex_lookahead STATIC
212+
${LLAMA2_TOKENIZER_DIR}/src/pcre2_regex.cpp
213+
${LLAMA2_TOKENIZER_DIR}/src/regex_lookahead.cpp
214+
${LLAMA2_TOKENIZER_DIR}/src/std_regex.cpp
215+
)
216+
add_library(tokenizer::regex_lookahead ALIAS regex_lookahead)
217+
target_link_libraries(regex_lookahead PUBLIC pcre2-8-static)
218+
target_include_directories(
219+
regex_lookahead
220+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
221+
)
222+
target_link_libraries(tokenizer PUBLIC regex_lookahead)
223+
install(
224+
TARGETS regex_lookahead pcre2-8-static
225+
EXPORT tokenizers-targets
226+
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
227+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
228+
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
229+
)
230+
163231
# Include directory for neuron headers
164232
include_directories(
165233
BEFORE ${_common_include_directories}
@@ -174,7 +242,11 @@ if(${ANDROID})
174242

175243
target_link_libraries(
176244
mtk_llama_executor_runner ${_executor_runner_libs} neuron_backend gflags
177-
mtk_llama_executor_lib tokenizer
245+
mtk_llama_executor_lib
246+
)
247+
target_link_libraries(
248+
mtk_llama_executor_runner tokenizer
249+
$<LINK_LIBRARY:WHOLE_ARCHIVE,regex_lookahead>
178250
)
179251
target_compile_options(
180252
mtk_llama_executor_runner PUBLIC ${_common_compile_options}

examples/mediatek/aot_utils/llm_utils/loggingprinter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) MediaTek Inc.
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
17
import sys
28

39

examples/mediatek/aot_utils/llm_utils/preformatter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Copyright (c) MediaTek Inc.
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
17
import json
28
import os
39
from typing import Union
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"description": "Template used by gemma.",
3+
"prompt_input": "<start_of_turn>user\n{instruction}<end_of_turn>\n<start_of_turn>model\n",
4+
"prompt_no_input": "<start_of_turn>user\n{instruction}<end_of_turn>\n<start_of_turn>model\n",
5+
"response_split": "<start_of_turn>model\n"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"description": "Template used by Phi-3.",
3+
"prompt_input": "<|system|>\nYou are a helpful AI assistant. Please provide safe, ethical and accurate information to the user.\n<|user|>\n {instruction} \n <|assistant|>",
4+
"prompt_no_input": "<|system|>\nYou are a helpful AI assistant. Please provide safe, ethical and accurate information to the user.\n<|user|>\n {instruction} \n <|assistant|>",
5+
"response_split": "<|assistant|>"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"description": "Template used by Phi-4.",
3+
"prompt_input": "<|system|>Your name is Phi, an AI expert developed by Microsoft.<|end|><|user|>{instruction}<|end|><|assistant|>",
4+
"prompt_no_input": "<|system|>Your name is Phi, an AI expert developed by Microsoft.<|end|><|user|>{instruction}<|end|><|assistant|>",
5+
"response_split": "<|assistant|>"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"description": "Template used by Qwen.",
3+
"prompt_input": "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{instruction}<|im_end|>\n<|im_start|>assistant\n",
4+
"prompt_no_input": "<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{instruction}<|im_end|>\n<|im_start|>assistant\n",
5+
"response_split": "<|im_start|>assistant\n"
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"description": "Template used by Qwen3.",
3+
"prompt_input": "<|im_start|>user\n{instruction}<|im_end|>\n<|im_start|>assistant\n",
4+
"prompt_no_input": "<|im_start|>user\n{instruction}<|im_end|>\n<|im_start|>assistant\n",
5+
"response_split": "assistant\n"
6+
}

0 commit comments

Comments
 (0)