-
Notifications
You must be signed in to change notification settings - Fork 988
Support multimethod in export_llama_lib #17231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
854e3b5
Support multimethod in export_llama_lib
1de0598
Update on "[wip] Support multimethod in export_llama_lib"
f0522dc
Update on "[wip] Support multimethod in export_llama_lib"
5853a6b
Update on "Support multimethod in export_llama_lib"
4b51baf
Update on "Support multimethod in export_llama_lib"
11e4f7a
Update on "Support multimethod in export_llama_lib"
542fa80
Update on "Support multimethod in export_llama_lib"
c2acf0c
Update on "Support multimethod in export_llama_lib"
1908b30
Update on "Support multimethod in export_llama_lib"
320e3aa
Update on "Support multimethod in export_llama_lib"
4aa2372
Update on "Support multimethod in export_llama_lib"
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #!/bin/bash | ||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the BSD-style license found in the | ||
| # LICENSE file in the root directory of this source tree. | ||
|
|
||
| set -exu | ||
| # shellcheck source=/dev/null | ||
| source "$(dirname "${BASH_SOURCE[0]}")/utils.sh" | ||
|
|
||
| cmake_install_executorch_libraries() { | ||
| echo "Installing libexecutorch.a, libextension_module.so, libportable_ops_lib.a" | ||
| rm -rf cmake-out | ||
| cmake --workflow llm-release | ||
| } | ||
|
|
||
| cmake_build_llama_runner() { | ||
| echo "Building llama runner" | ||
| pushd extension/llm/tokenizers | ||
| echo "Updating tokenizers submodule" | ||
| git submodule update --init | ||
| popd | ||
| make llama-cpu | ||
| } | ||
|
|
||
| cleanup_files() { | ||
| echo "Deleting downloaded and generated files" | ||
| rm -rf "${HF_QWEN_PATH}/" | ||
| rm -rf "${HF_ADAPTER_PATH}/" | ||
| rm -rf *.pte | ||
| rm -f result*.txt | ||
| } | ||
|
|
||
| # Download LoRA adapter. | ||
| python -m pip install -q huggingface_hub | ||
| HF_ADAPTER_REPO="lucylq/qwen3_06B_lora_math" | ||
| HF_ADAPTER_PATH=$( | ||
| bash "$(dirname "${BASH_SOURCE[0]}")/download_hf_hub.sh" \ | ||
| --model_id "${HF_ADAPTER_REPO}" \ | ||
| --files "adapter_config.json" "adapter_model.safetensors" | ||
| ) | ||
|
|
||
| # Download base model (for tokenizer path). | ||
| HF_QWEN_PATH=$(python -c "from huggingface_hub import snapshot_download; print(snapshot_download('unsloth/Qwen3-0.6B'))") | ||
| echo "Model downloaded to: $HF_QWEN_PATH" | ||
|
|
||
| ### EXPORT MULTIMETHOD PTE ### | ||
| # Set environment variables for OmegaConf interpolation in yaml. | ||
| export LORA_ADAPTER_CHECKPOINT="${HF_ADAPTER_PATH}/adapter_model.safetensors" | ||
| export LORA_ADAPTER_CONFIG="${HF_ADAPTER_PATH}/adapter_config.json" | ||
|
|
||
| $PYTHON_EXECUTABLE -m extension.llm.export.export_llm \ | ||
| --config examples/models/qwen3/config/qwen3_multimethod.yaml | ||
|
|
||
| ### BUILD LLAMA RUNNER ### | ||
| cmake_install_executorch_libraries | ||
| cmake_build_llama_runner | ||
|
|
||
| # Runner constants. | ||
| RUNTIME_ARGS="--tokenizer_path=${HF_QWEN_PATH}/ --temperature=0 --seq_len=100 --warmup=1" | ||
| PROMPT="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant" | ||
|
|
||
| # Expected outputs. | ||
| EXPECTED_LORA_PREFIX=" | ||
| <|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant | ||
| To calculate 15% of 80" | ||
|
|
||
| EXPECTED_BASE_PREFIX="<|im_start|>user Calculate 15% of 80?<|im_end|><|im_start|>assistant: | ||
| <think> | ||
| Okay, so I need to calculate 15% of 80." | ||
|
|
||
| ### TEST 1: Run lora_forward method ### | ||
| NOW=$(date +"%H:%M:%S") | ||
| echo "Test 1: Multimethod lora_forward. Starting at ${NOW}" | ||
| # shellcheck source=/dev/null | ||
| cmake-out/examples/models/llama/llama_main \ | ||
| --model_path=multimethod_qwen.pte \ | ||
| --method_name=lora_forward \ | ||
| --prompt="${PROMPT}" \ | ||
| ${RUNTIME_ARGS} > result_lora.txt | ||
| NOW=$(date +"%H:%M:%S") | ||
| echo "Finished at ${NOW}" | ||
|
|
||
| RESULT=$(cat result_lora.txt) | ||
| if [[ "${RESULT}" == "${EXPECTED_LORA_PREFIX}"* ]]; then | ||
| echo "Expected result prefix: ${EXPECTED_LORA_PREFIX}" | ||
| echo "Actual result: ${RESULT}" | ||
| echo "Test 1 (lora_forward): Success" | ||
| else | ||
| echo "Expected result prefix: ${EXPECTED_LORA_PREFIX}" | ||
| echo "Actual result: ${RESULT}" | ||
| echo "Test 1 (lora_forward): Failure" | ||
| cleanup_files | ||
| exit 1 | ||
| fi | ||
|
|
||
| ### TEST 2: Run base_forward method ### | ||
| NOW=$(date +"%H:%M:%S") | ||
| echo "Test 2: Multimethod base_forward. Starting at ${NOW}" | ||
| # shellcheck source=/dev/null | ||
| cmake-out/examples/models/llama/llama_main \ | ||
| --model_path=multimethod_qwen.pte \ | ||
| --method_name=base_forward \ | ||
| --prompt="${PROMPT}" \ | ||
| ${RUNTIME_ARGS} > result_base.txt | ||
| NOW=$(date +"%H:%M:%S") | ||
| echo "Finished at ${NOW}" | ||
|
|
||
| RESULT=$(cat result_base.txt) | ||
| if [[ "${RESULT}" == "${EXPECTED_BASE_PREFIX}"* ]]; then | ||
| echo "Expected result prefix: ${EXPECTED_BASE_PREFIX}" | ||
| echo "Actual result: ${RESULT}" | ||
| echo "Test 2 (base_forward): Success" | ||
| else | ||
| echo "Expected result prefix: ${EXPECTED_BASE_PREFIX}" | ||
| echo "Actual result: ${RESULT}" | ||
| echo "Test 2 (base_forward): Failure" | ||
| cleanup_files | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Multimethod tests passed!" | ||
| cleanup_files | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| base: | ||
| model_class: "qwen3_0_6b" | ||
| params: "examples/models/qwen3/config/0_6b_config.json" | ||
| metadata: '{"get_bos_id": 151644, "get_eos_ids":[151645]}' | ||
|
|
||
| model: | ||
| use_kv_cache: true | ||
| use_sdpa_with_kv_cache: true | ||
|
|
||
| export: | ||
| output_name: multimethod_qwen | ||
|
|
||
| backend: | ||
| xnnpack: | ||
| enabled: true | ||
|
|
||
| quantization: | ||
| qmode: "8da4w" | ||
| group_size: 32 | ||
|
|
||
| multimethod: | ||
| methods: | ||
| # LoRA method - adapter paths from environment variables | ||
| lora_forward: | ||
| adapter_checkpoint: ${oc.env:LORA_ADAPTER_CHECKPOINT} | ||
| adapter_config: ${oc.env:LORA_ADAPTER_CONFIG} | ||
| # Base method - no LoRA | ||
| base_forward: null |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.