Skip to content

Commit f685206

Browse files
committed
add CI job for phi-3-mini
ghstack-source-id: 100ba78 ghstack-comment-id: 2364726411 Pull Request resolved: #5532
1 parent b5741a6 commit f685206

File tree

4 files changed

+154
-9
lines changed

4 files changed

+154
-9
lines changed

.ci/scripts/test_phi_3_mini.sh

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -exu
9+
10+
BUILD_TYPE=${1:-Debug}
11+
BUILD_DIR=${3:-cmake-out}
12+
MODEL_DIR=examples/models/phi-3-mini
13+
14+
echo "Building with BUILD_TYPE: $BUILD_TYPE, BUILD_DIR: $BUILD_DIR"
15+
16+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
17+
PYTHON_EXECUTABLE=python3
18+
fi
19+
20+
# Number of processes for a parallel build
21+
NPROC=8
22+
if hash nproc &> /dev/null; then NPROC=$(nproc); fi
23+
24+
cmake_install_executorch_libraries() {
25+
cmake -DPYTHON_EXECUTABLE=python \
26+
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
27+
-DEXECUTORCH_ENABLE_LOGGING=1 \
28+
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
29+
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
30+
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
31+
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
32+
-DEXECUTORCH_BUILD_XNNPACK=ON \
33+
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
34+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
35+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
36+
-B${BUILD_DIR} .
37+
38+
cmake --build ${BUILD_DIR} -j${NPROC} --target install --config ${BUILD_TYPE}
39+
}
40+
41+
cmake_build_phi_3_mini() {
42+
python_lib=$($PYTHON_EXECUTABLE -c 'from distutils.sysconfig import get_python_lib; print(get_python_lib())')
43+
44+
cmake -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
45+
-DCMAKE_INSTALL_PREFIX=${BUILD_DIR} \
46+
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
47+
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
48+
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
49+
-DEXECUTORCH_BUILD_XNNPACK=ON \
50+
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
51+
-B${BUILD_DIR}/${MODEL_DIR} \
52+
${MODEL_DIR}
53+
54+
cmake --build ${BUILD_DIR}/${MODEL_DIR} -j${NPROC} --config ${BUILD_TYPE}
55+
}
56+
57+
# Download and convert tokenizer.model
58+
prepare_tokenizer() {
59+
echo "Downloading and converting tokenizer.model"
60+
wget -O tokenizer.model "https://huggingface.co/microsoft/Phi-3-mini-128k-instruct/resolve/main/tokenizer.model?download=true"
61+
$PYTHON_EXECUTABLE -m executorch.extension.llm.tokenizer.tokenizer -t tokenizer.model -o tokenizer.bin
62+
}
63+
64+
# Export phi-3-mini model to pte
65+
export_phi_3_mini () {
66+
echo "Exporting phi-3-mini. This will take a few minutes"
67+
$PYTHON_EXECUTABLE -m executorch.examples.models.phi-3-mini.export_phi-3-mini -c "4k" -s 128 -o phi-3-mini.pte
68+
}
69+
70+
run_and_verify() {
71+
NOW=$(date +"%H:%M:%S")
72+
echo "Starting to run phi-3-mini runner at ${NOW}"
73+
if [[ ! -f "phi-3-mini.pte" ]]; then
74+
echo "Export failed. Abort"
75+
exit 1
76+
fi
77+
if [[ ! -f "tokenizer.bin" ]]; then
78+
echo "tokenizer.bin is missing."
79+
exit 1
80+
fi
81+
82+
RUNTIME_ARGS="--model_path=phi-3-mini.pte \
83+
--tokenizer_path=tokenizer.bin \
84+
--prompt=\"<|system|>You are a helpful assistant.<|end|><|user|>What is the capital of France?<|end|><|assistant|>\" \
85+
--temperature=0 \
86+
--seq_len=128"
87+
88+
${BUILD_DIR}/${MODEL_DIR}/phi_3_mini_runner ${RUNTIME_ARGS} > result.txt
89+
90+
# verify result.txt
91+
RESULT=$(cat result.txt)
92+
EXPECTED_RESULT="The capital of France is Paris."
93+
if [[ "${RESULT}" == *"${EXPECTED_RESULT}"* ]]; then
94+
echo "Expected result prefix: ${EXPECTED_RESULT}"
95+
echo "Actual result: ${RESULT}"
96+
echo "Success"
97+
exit 0
98+
else
99+
echo "Expected result prefix: ${EXPECTED_RESULT}"
100+
echo "Actual result: ${RESULT}"
101+
echo "Failure; results not the same"
102+
exit 1
103+
fi
104+
}
105+
106+
# Step 1. Build ExecuTorch and phi-3-mini runner
107+
cmake_install_executorch_libraries
108+
cmake_build_phi_3_mini
109+
110+
# Step 2. Export the tokenizer and model
111+
prepare_tokenizer
112+
export_phi_3_mini
113+
114+
# Step 3. Run and verify result
115+
run_and_verify

.github/workflows/pull.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,30 @@ jobs:
414414
PYTHON_EXECUTABLE=python bash examples/models/llama2/install_requirements.sh
415415
# Test llama2
416416
PYTHON_EXECUTABLE=python bash .ci/scripts/test_llama.sh stories110M "${BUILD_TOOL}" "${DTYPE}" "${MODE}"
417+
418+
test-phi-3-mini-runner-linux:
419+
name: test-phi-3-mini-runner-linux
420+
uses: pytorch/test-infra/.github/workflows/linux_job.yml@main
421+
strategy:
422+
fail-fast: false
423+
with:
424+
runner: linux.24xlarge
425+
docker-image: executorch-ubuntu-22.04-clang12
426+
submodules: 'true'
427+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || github.sha }}
428+
timeout: 90
429+
script: |
430+
# The generic Linux job chooses to use base env, not the one setup by the image
431+
CONDA_ENV=$(conda env list --json | jq -r ".envs | .[-1]")
432+
conda activate "${CONDA_ENV}"
433+
434+
PYTHON_EXECUTABLE=python bash .ci/scripts/setup-linux.sh "cmake"
435+
436+
# install pybind
437+
bash install_requirements.sh --pybind xnnpack
438+
439+
# install phi-3-mini requirements
440+
bash examples/models/phi-3-mini/install_requirements.sh
441+
442+
# run e2e (export, tokenizer and runner)
443+
PYTHON_EXECUTABLE=python bash .ci/scripts/test_phi_3_mini.sh
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -x
9+
10+
pip install transformers==4.44.2
11+
12+
pip list

examples/models/phi-3-mini/runner.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,8 @@ void Runner::generate(const std::string& prompt, std::size_t max_seq_len) {
4848
ET_CHECK_MSG(
4949
encode_res.error() == Error::Ok, "Failed to encode %s", prompt.c_str());
5050
auto input_tokens = encode_res.get();
51-
52-
std::cout << "Prefilling tokens ..." << std::endl;
53-
for (auto token : input_tokens) {
54-
std::cout << token << " ";
55-
}
56-
std::cout << std::endl;
57-
std::cout.flush();
5851
auto prev_token = input_tokens.back();
5952
auto current_token = prefill(input_tokens);
60-
61-
std::cout << "Generating tokens ..." << std::endl;
6253
std::cout << tokenizer_->decode(prev_token, current_token).get();
6354
std::cout.flush();
6455

0 commit comments

Comments
 (0)