Skip to content

Commit 5b77187

Browse files
Add batch runner example and refactor single sequence runner to use text stream (pytorch#22627)
Co-authored-by: kiymetakdemir <kiymet@meta.com>
1 parent 04f9002 commit 5b77187

9 files changed

Lines changed: 725 additions & 158 deletions

File tree

‎.github/workflows/mlx.yml‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,13 @@ jobs:
10371037
${CONDA_RUN} cmake --build cmake-out/backends/mlx/examples/llm \
10381038
-j$(( $(sysctl -n hw.ncpu) - 1 ))
10391039
RUNNER=cmake-out/backends/mlx/examples/llm/mlx_run_llm_hf
1040-
if [ ! -x "${RUNNER}" ]; then
1041-
echo "Failed: runner not found at ${RUNNER}"
1042-
exit 1
1043-
fi
1040+
BATCHED_RUNNER=cmake-out/backends/mlx/examples/llm/mlx_run_llm_batched
1041+
for binary in "${RUNNER}" "${BATCHED_RUNNER}"; do
1042+
if [ ! -x "${binary}" ]; then
1043+
echo "Failed: runner not found at ${binary}"
1044+
exit 1
1045+
fi
1046+
done
10441047
echo "::endgroup::"
10451048
10461049
echo "::group::Install LLM requirements"
@@ -1085,3 +1088,28 @@ jobs:
10851088
exit 1
10861089
fi
10871090
echo "::endgroup::"
1091+
1092+
echo "::group::Run ${MODEL_NAME} batched off-graph inference"
1093+
${BATCHED_RUNNER} \
1094+
--pte /tmp/${MODEL_NAME}_offgraph.pte \
1095+
--tokenizer "${TOKENIZER}" \
1096+
--chat "${CHAT}" \
1097+
--max-session-tokens 1024 \
1098+
--max-new-tokens 50 \
1099+
--max-decode-sequences 2 \
1100+
--out-prefix /tmp/${MODEL_NAME}_batched \
1101+
"What is the capital of France?" \
1102+
"What color is grass?"
1103+
for check in "0:Paris" "1:green"; do
1104+
index="${check%%:*}"
1105+
expected="${check#*:}"
1106+
output="/tmp/${MODEL_NAME}_batched_${index}.txt"
1107+
if grep -iq "${expected}" "${output}"; then
1108+
echo "Success: '${expected}' found in ${output}"
1109+
else
1110+
echo "Failed: Expected '${expected}' not found in ${output}"
1111+
cat "${output}"
1112+
exit 1
1113+
fi
1114+
done
1115+
echo "::endgroup::"

‎backends/mlx/examples/llm/CMakeLists.txt‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,10 @@ executorch_target_link_options_shared_lib(executorch)
3535
set(gflags_DIR ${CMAKE_CURRENT_BINARY_DIR}/../../../../third-party/gflags)
3636
find_package(gflags REQUIRED)
3737

38-
set(link_libraries
39-
executorch
40-
extension_module
41-
extension_tensor
42-
extension_llm_cache
43-
extension_llm_runner
44-
extension_llm_sampler
45-
gflags
46-
)
38+
set(link_libraries executorch extension_llm_runner gflags)
4739

4840
if(NOT TARGET mlxdelegate)
49-
message(FATAL_ERROR "mlx_run_llm_hf requires the MLX backend (mlxdelegate)")
41+
message(FATAL_ERROR "MLX LLM runners require the MLX backend (mlxdelegate)")
5042
endif()
5143
list(APPEND link_libraries mlxdelegate mlx)
5244
executorch_target_link_options_shared_lib(mlxdelegate)
@@ -67,7 +59,27 @@ target_include_directories(
6759
)
6860
target_link_libraries(mlx_run_llm_hf PUBLIC ${link_libraries})
6961

62+
add_executable(mlx_run_llm_batched run_llm_batched.cpp)
63+
target_include_directories(
64+
mlx_run_llm_batched PUBLIC ${_common_include_directories} ${_json_include}
65+
${_flatbuffers_include}
66+
)
67+
target_link_libraries(
68+
mlx_run_llm_batched
69+
PUBLIC executorch
70+
extension_llm_batching_module
71+
extension_llm_runner
72+
gflags
73+
mlxdelegate
74+
mlx
75+
tokenizers::tokenizers
76+
)
77+
if(TARGET optimized_native_cpu_ops_lib)
78+
target_link_libraries(mlx_run_llm_batched PUBLIC optimized_native_cpu_ops_lib)
79+
endif()
80+
7081
# The copy helper is gated on EXECUTORCH_BUILD_MLX, which the installed config
7182
# does not set; reaching here means mlxdelegate exists.
7283
set(EXECUTORCH_BUILD_MLX ON)
7384
executorch_target_copy_mlx_metallib(mlx_run_llm_hf)
85+
executorch_target_copy_mlx_metallib(mlx_run_llm_batched)

0 commit comments

Comments
 (0)