-
Notifications
You must be signed in to change notification settings - Fork 7
Lora example #52
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
Open
lucylq
wants to merge
2
commits into
lfq.refactor-example
Choose a base branch
from
lfq.lora
base: lfq.refactor-example
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lora example #52
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,86 @@ | ||
# ExecuTorch Program Data Separation Demo C++. | ||
|
||
This directory contains the C++ code to run the examples generated in [program-data-separation](../program-data-separation/README.md). | ||
|
||
|
||
## Virtual environment setup. | ||
Create and activate a Python virtual environment: | ||
```bash | ||
python3 -m venv .venv && source .venv/bin/activate && pip install --upgrade pip | ||
``` | ||
Or alternatively, [install conda on your machine](https://conda.io/projects/conda/en/latest/user-guide/install/index.html) | ||
```bash | ||
conda create -yn executorch-ptd python=3.10.0 && conda activate executorch-ptd | ||
``` | ||
|
||
Install dependencies: | ||
LoRA isn't available in the 0.7.0 release of ExecuTorch. Instead, please install from source until ExecuTorch 1.0 is released. | ||
|
||
[Install ExecuTorch pip package from source](https://docs.pytorch.org/executorch/stable/using-executorch-building-from-source.html#install-executorch-pip-package-from-source). | ||
|
||
Currently, the LoRA changes aren't in nightlies. Once they are in, you can also install from the nightly build. | ||
``` | ||
pip install executorch==0.8.0.devYYYYMMDD --extra-index-url https://download.pytorch.org/whl/nightly/cpu | ||
``` | ||
|
||
## Export the model/s. | ||
Change into the program-data-separation directory and create a directory to hold exported artifacts. | ||
```bash | ||
cd ~/executorch-examples/program-data-separation | ||
mkdir models | ||
``` | ||
|
||
Export models into the `models` directory. The first command will generated undelegated model/data files, and the second will generate XNNPACK-delegated model/data files. | ||
```bash | ||
sh export_lora.sh | ||
``` | ||
Expect the files: | ||
- llama_3_2_1B.pte | ||
- llama_3_2_1B.ptd | ||
- llama_3_2_1B_lora.pte | ||
- foundation_weights.ptd | ||
- tokenizer.model | ||
|
||
llama_3_2_1B.ptd and foundation_weights.ptd contain the same contents, and you can remove llama_3_2_1B.ptd. | ||
tokenizer.model is copied from the temp directory where we downloaded the HF artifacts. It will be used at runtime. | ||
|
||
Note: | ||
- PTE: contains the program execution logic. | ||
- PTD: contains the constant tensors used by the PTE. | ||
|
||
## Install runtime dependencies. | ||
The ExecuTorch repository is configured as a git submodule at `~/executorch-examples/program-data-separation/cpp/executorch`. To initialize it: | ||
```bash | ||
cd ~/executorch-examples/ | ||
git submodule sync | ||
git submodule update --init --recursive | ||
``` | ||
Install dev requirements for ExecuTorch | ||
|
||
```bash | ||
cd ~/executorch-examples/program-data-separation/cpp/executorch | ||
pip install -r requirements-dev.txt | ||
``` | ||
|
||
## Build the runtime. | ||
Install some dependencies: | ||
```bash | ||
cd ~/executorch-examples/program-data-separation/cpp/executorch | ||
sh examples/models/llama/install_requirements.sh | ||
``` | ||
|
||
Build the executable: | ||
```bash | ||
cd ~/executorch-examples/program-data-separation/cpp/lora_example | ||
sh build_example.sh | ||
``` | ||
|
||
## Run the executable. | ||
``` | ||
./build/bin/executorch_program_data_separation --lora_model_path=../../llama_3_2_1B_lora.pte --llama_model_path=../../llama_3_2_1B.pte --tokenizer_path=../../tokenizer.model --data_path=../../foundation.ptd | ||
``` | ||
|
||
## Clean up. | ||
rm -rf build | ||
cd ~/executorch-examples/program-data-separation | ||
rm -rf *.pte *.ptd tokenizer.model |
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,15 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Clean and create build directory if it doesn't exist | ||
rm -rf build | ||
mkdir -p build | ||
cd build | ||
|
||
# Configure CMake | ||
cmake -DCMAKE_BUILD_TYPE=Release -DEXECUTORCH_BUILD_LORA_DEMO=True ../.. | ||
|
||
# Build the project | ||
cmake --build . -j$(nproc) | ||
|
||
echo "Build complete! Executable located at: ./build/bin/executorch_program_data_separation" |
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,92 @@ | ||
/* | ||
* 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. | ||
* @lint-ignore-every CLANGTIDY facebook-hte-Deprecated | ||
*/ | ||
#include <gflags/gflags.h> | ||
|
||
#include <executorch/examples/models/llama/runner/runner.h> | ||
|
||
#if defined(ET_USE_THREADPOOL) | ||
#include <executorch/extension/threadpool/cpuinfo_utils.h> | ||
#include <executorch/extension/threadpool/threadpool.h> | ||
#endif | ||
|
||
DEFINE_string(lora_model_path, "llama_3_2_1B_lora.pte", | ||
"LoRA model serialized in flatbuffer format."); | ||
DEFINE_string(llama_model_path, "llama_3_2_1B.pte", | ||
"Model serialized in flatbuffer format."); | ||
DEFINE_string(data_path, "foundation.ptd", | ||
"Data serialized in flatbuffer format."); | ||
|
||
DEFINE_string(tokenizer_path, "tokenizer.model", "Tokenizer stuff."); | ||
|
||
DEFINE_string(prompt, "The answer to the ultimate question is", "Prompt."); | ||
|
||
DEFINE_double(temperature, 0, | ||
"Temperature; Default is 0. 0 = greedy argmax sampling " | ||
"(deterministic). Lower temperature = more deterministic"); | ||
|
||
DEFINE_int32( | ||
seq_len, 128, | ||
"Total number of tokens to generate (prompt + output). Defaults to " | ||
"max_seq_len. If the number of input tokens + seq_len > max_seq_len, the " | ||
"output will be truncated to max_seq_len tokens."); | ||
|
||
using namespace ::executorch::extension; | ||
|
||
int main(int argc, char *argv[]) { | ||
ET_LOG(Info, "Running program-data separation lora example..."); | ||
|
||
gflags::ParseCommandLineFlags(&argc, &argv, true); | ||
|
||
const char *lora_model_path = FLAGS_lora_model_path.c_str(); | ||
const char *llama_model_path = FLAGS_llama_model_path.c_str(); | ||
const char *data_path = FLAGS_data_path.c_str(); | ||
|
||
const char *tokenizer_path = FLAGS_tokenizer_path.c_str(); | ||
const char *prompt = FLAGS_prompt.c_str(); | ||
float temperature = FLAGS_temperature; | ||
int32_t seq_len = 128; | ||
int32_t cpu_threads = -1; | ||
|
||
// Create runner for lora model. | ||
std::unique_ptr<::executorch::extension::llm::TextLLMRunner> lora_runner = | ||
example::create_llama_runner(lora_model_path, tokenizer_path, data_path); | ||
if (lora_runner == nullptr) { | ||
ET_LOG(Error, "Failed to create lora_runner."); | ||
return 1; | ||
} | ||
|
||
// create runner for llama model | ||
std::unique_ptr<::executorch::extension::llm::TextLLMRunner> llama_runner = | ||
example::create_llama_runner(llama_model_path, tokenizer_path, data_path); | ||
if (llama_runner == nullptr) { | ||
ET_LOG(Error, "Failed to create llama_runner."); | ||
return 1; | ||
} | ||
|
||
// generate | ||
executorch::extension::llm::GenerationConfig config{ | ||
.seq_len = seq_len, .temperature = temperature}; | ||
|
||
auto error = lora_runner->generate(prompt, config); | ||
if (error != executorch::runtime::Error::Ok) { | ||
ET_LOG(Error, "Failed to generate with lora_runner, error code %zu.", | ||
error); | ||
return 1; | ||
} | ||
|
||
ET_LOG(Info, "Generating with llama..."); | ||
error = llama_runner->generate(prompt, config); | ||
if (error != executorch::runtime::Error::Ok) { | ||
ET_LOG(Error, "Failed to generate with llama_runner, error code %zu.", | ||
error); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
} |
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,53 @@ | ||
#!/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 | ||
|
||
python -m pip install torchtune==0.7.0.dev20250730 --extra-index-url https://download.pytorch.org/whl/nightly/cpu | ||
|
||
# Download model artifacts from HF. | ||
DOWNLOADED_PATH=$(python -c " | ||
from huggingface_hub import snapshot_download | ||
path=snapshot_download( | ||
repo_id=\"lucylq/llama3_1B_lora\", | ||
) | ||
import os | ||
print(path) | ||
") | ||
|
||
# Copy over tokenizer, for use at runtime. | ||
cp "${DOWNLOADED_PATH}/tokenizer.model" . | ||
|
||
# Export a non-LoRA model with program-data separated. | ||
MODEL="llama_3_2_1B" | ||
python -m executorch.extension.llm.export.export_llm \ | ||
base.checkpoint="${DOWNLOADED_PATH}/consolidated.00.pth" \ | ||
base.params="${DOWNLOADED_PATH}/params.json" \ | ||
base.tokenizer_path="${DOWNLOADED_PATH}/tokenizer.model" \ | ||
model.use_kv_cache=true \ | ||
model.use_sdpa_with_kv_cache=true \ | ||
model.dtype_override="fp32" \ | ||
backend.xnnpack.enabled=true \ | ||
backend.xnnpack.extended_ops=true \ | ||
export.output_name="${MODEL}.pte" \ | ||
export.foundation_weights_file="${MODEL}.ptd" | ||
|
||
# Export a LoRA model, with program and data separated. | ||
LORA_MODEL="llama_3_2_1B_lora" | ||
python -m executorch.extension.llm.export.export_llm \ | ||
base.checkpoint="${DOWNLOADED_PATH}/consolidated.00.pth" \ | ||
base.params="${DOWNLOADED_PATH}/params.json" \ | ||
base.adapter_checkpoint="${DOWNLOADED_PATH}/adapter_model.pt" \ | ||
base.adapter_config="${DOWNLOADED_PATH}/adapter_config.json" \ | ||
base.tokenizer_path="${DOWNLOADED_PATH}/tokenizer.model" \ | ||
model.use_kv_cache=true \ | ||
model.use_sdpa_with_kv_cache=true \ | ||
model.dtype_override="fp32" \ | ||
backend.xnnpack.enabled=true \ | ||
backend.xnnpack.extended_ops=true \ | ||
export.output_name="${LORA_MODEL}.pte" \ | ||
export.foundation_weights_file="foundation.ptd" |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't depend on examples module
showcase how a typical user would use this API via executorch/extension/llm/runner/text_llm_runner.h
don't call example::create_llama_runner.