|
| 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 | +# shellcheck source=/dev/null |
| 10 | +source "$(dirname "${BASH_SOURCE[0]}")/utils.sh" |
| 11 | + |
| 12 | +cmake_install_executorch_libraries() { |
| 13 | + echo "Installing libexecutorch.a, libextension_module.so, libportable_ops_lib.a" |
| 14 | + rm -rf cmake-out |
| 15 | + cmake --workflow llm-release |
| 16 | +} |
| 17 | + |
| 18 | +cmake_build_llama_runner() { |
| 19 | + echo "Building llama runner" |
| 20 | + pushd extension/llm/tokenizers |
| 21 | + echo "Updating tokenizers submodule" |
| 22 | + git submodule update --init |
| 23 | + popd |
| 24 | + make llama-cpu |
| 25 | +} |
| 26 | + |
| 27 | +cleanup_files() { |
| 28 | + echo "Deleting downloaded and generated files" |
| 29 | + rm -rf "${HF_QWEN_PATH}/" |
| 30 | + rm -rf "${HF_ADAPTER_PATH}/" |
| 31 | + rm -rf *.pte |
| 32 | + rm -f result*.txt |
| 33 | +} |
| 34 | + |
| 35 | +# Download LoRA adapter. |
| 36 | +python -m pip install -q huggingface_hub |
| 37 | +HF_ADAPTER_REPO="lucylq/qwen3_06B_lora_math" |
| 38 | +HF_ADAPTER_PATH=$( |
| 39 | + bash "$(dirname "${BASH_SOURCE[0]}")/download_hf_hub.sh" \ |
| 40 | + --model_id "${HF_ADAPTER_REPO}" \ |
| 41 | + --files "adapter_config.json" "adapter_model.safetensors" |
| 42 | +) |
| 43 | + |
| 44 | +# Download base model (for tokenizer path). |
| 45 | +HF_QWEN_PATH=$(python -c "from huggingface_hub import snapshot_download; print(snapshot_download('unsloth/Qwen3-0.6B'))") |
| 46 | +echo "Model downloaded to: $HF_QWEN_PATH" |
| 47 | + |
| 48 | +### EXPORT MULTIMETHOD PTE ### |
| 49 | +# Set environment variables for OmegaConf interpolation in yaml. |
| 50 | +export LORA_ADAPTER_CHECKPOINT="${HF_ADAPTER_PATH}/adapter_model.safetensors" |
| 51 | +export LORA_ADAPTER_CONFIG="${HF_ADAPTER_PATH}/adapter_config.json" |
| 52 | + |
| 53 | +$PYTHON_EXECUTABLE -m extension.llm.export.export_llm \ |
| 54 | + --config examples/models/qwen3/config/qwen3_multimethod.yaml |
| 55 | + |
| 56 | +### BUILD LLAMA RUNNER ### |
| 57 | +cmake_install_executorch_libraries |
| 58 | +cmake_build_llama_runner |
| 59 | + |
| 60 | +# Runner constants. |
| 61 | +RUNTIME_ARGS="--tokenizer_path=${HF_QWEN_PATH}/ --temperature=0 --seq_len=100 --warmup=1" |
| 62 | +PROMPT="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant" |
| 63 | + |
| 64 | +# Expected outputs. |
| 65 | +EXPECTED_LORA_PREFIX=" |
| 66 | +<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant |
| 67 | +To calculate 15% of 80" |
| 68 | + |
| 69 | +EXPECTED_BASE_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant: |
| 70 | +<think> |
| 71 | +Okay, so I need to calculate 15% of 80." |
| 72 | + |
| 73 | +### TEST 1: Run lora_forward method ### |
| 74 | +NOW=$(date +"%H:%M:%S") |
| 75 | +echo "Test 1: Multimethod lora_forward. Starting at ${NOW}" |
| 76 | +# shellcheck source=/dev/null |
| 77 | +cmake-out/examples/models/llama/llama_main \ |
| 78 | + --model_path=multimethod_qwen.pte \ |
| 79 | + --method_name=lora_forward \ |
| 80 | + --prompt="${PROMPT}" \ |
| 81 | + ${RUNTIME_ARGS} > result_lora.txt |
| 82 | +NOW=$(date +"%H:%M:%S") |
| 83 | +echo "Finished at ${NOW}" |
| 84 | + |
| 85 | +RESULT=$(cat result_lora.txt) |
| 86 | +if [[ "${RESULT}" == "${EXPECTED_LORA_PREFIX}"* ]]; then |
| 87 | + echo "Test 1 (lora_forward): Success" |
| 88 | +else |
| 89 | + echo "Test 1 (lora_forward): Failure" |
| 90 | + echo "Expected result prefix: ${EXPECTED_LORA_PREFIX}" |
| 91 | + echo "Actual result: ${RESULT}" |
| 92 | + cleanup_files |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +### TEST 2: Run base_forward method ### |
| 97 | +NOW=$(date +"%H:%M:%S") |
| 98 | +echo "Test 2: Multimethod base_forward. Starting at ${NOW}" |
| 99 | +# shellcheck source=/dev/null |
| 100 | +cmake-out/examples/models/llama/llama_main \ |
| 101 | + --model_path=multimethod_qwen.pte \ |
| 102 | + --method_name=base_forward \ |
| 103 | + --prompt="${PROMPT}" \ |
| 104 | + ${RUNTIME_ARGS} > result_base.txt |
| 105 | +NOW=$(date +"%H:%M:%S") |
| 106 | +echo "Finished at ${NOW}" |
| 107 | + |
| 108 | +RESULT=$(cat result_base.txt) |
| 109 | +if [[ "${RESULT}" == "${EXPECTED_BASE_PREFIX}"* ]]; then |
| 110 | + echo "Test 2 (base_forward): Success" |
| 111 | +else |
| 112 | + echo "Test 2 (base_forward): Failure" |
| 113 | + echo "Expected result prefix: ${EXPECTED_BASE_PREFIX}" |
| 114 | + echo "Actual result: ${RESULT}" |
| 115 | + cleanup_files |
| 116 | + exit 1 |
| 117 | +fi |
| 118 | + |
| 119 | +echo "Multimethod tests passed!" |
| 120 | +cleanup_files |
0 commit comments