-
Notifications
You must be signed in to change notification settings - Fork 2
Add demo scripts #4
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
3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| #!/bin/bash | ||
| # LiteRT CLI EfficientNet Demo & Test Script | ||
| set -e | ||
|
|
||
|
|
||
| echo -e "${BLUE}${BOLD}==================================================================${NC}" | ||
| echo -e "${BLUE}${BOLD}>>> LiteRT CLI EfficientNet Demo Script${NC}" | ||
| echo -e "${BLUE}${BOLD}==================================================================${NC}" | ||
|
|
||
| # --- Environment Setup --- | ||
| export SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| export REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" | ||
| export LITERT_CLI_ROOT="/tmp/litert_cli_efficientnet" | ||
|
|
||
| # Source shared utilities | ||
| source "$SCRIPT_DIR/demo_utils.sh" | ||
|
|
||
|
|
||
| # Clean up and create work directory | ||
| echo -e "\n${YELLOW}Setting up workspace at: $LITERT_CLI_ROOT...${NC}" | ||
| rm -rf "$LITERT_CLI_ROOT" | ||
| mkdir -p "$LITERT_CLI_ROOT" | ||
| cd "$LITERT_CLI_ROOT" | ||
|
|
||
| # Create Python virtual environment | ||
| echo -e "${YELLOW}Creating Python virtual environment...${NC}" | ||
| python3 -m venv venv_efficientnet | ||
| source venv_efficientnet/bin/activate | ||
|
|
||
| # Create output directories | ||
| export MODEL_DIR="$LITERT_CLI_ROOT/models" | ||
| mkdir -p "$MODEL_DIR" | ||
|
|
||
| # Test data directory | ||
| export TEST_DATA_DIR="$REPO_ROOT/litert_cli/test_data" | ||
|
|
||
| # Install litert-cli from source | ||
| echo -e "${YELLOW}Installing litert-cli from source...${NC}" | ||
| pip install -e "$REPO_ROOT" | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # --- 1. Download EfficientNet-B1 model --- | ||
| run_case "Download: EfficientNet-B1 from HuggingFace" \ | ||
| litert download litert-community/efficientnet_b1 --file "*.tflite" --output "$MODEL_DIR/efficientnet" | ||
|
|
||
| # Verify the downloaded model exists | ||
| EFFICIENTNET_TFLITE="$MODEL_DIR/efficientnet/efficientnet_b1.tflite" | ||
| if [ ! -f "$EFFICIENTNET_TFLITE" ]; then | ||
| echo -e "${RED}Error: Downloaded model not found at $EFFICIENTNET_TFLITE${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # --- 2. Quantize the EfficientNet model --- | ||
| run_case "Quantize: EfficientNet Dynamic Range INT8" \ | ||
| litert quantize "$EFFICIENTNET_TFLITE" --type int8_dynamic --output "$MODEL_DIR/efficientnet/efficientnet_b1_int8_dynamic.tflite" | ||
|
|
||
| run_case "Quantize: EfficientNet Weight-Only INT8" \ | ||
| litert quantize "$EFFICIENTNET_TFLITE" --type int8_weight_only --output "$MODEL_DIR/efficientnet/efficientnet_b1_int8_weight_only.tflite" | ||
|
|
||
| # --- 3. Run Inference (Desktop & Android) --- | ||
| run_case "Run: EfficientNet FP32 on Desktop (CPU)" \ | ||
| litert run "$EFFICIENTNET_TFLITE" --desktop --cpu --iterations 1 | ||
|
|
||
| if has_desktop_gpu "$EFFICIENTNET_TFLITE"; then | ||
| run_case "Run: EfficientNet FP32 on Desktop (GPU)" \ | ||
| litert run "$EFFICIENTNET_TFLITE" --desktop --gpu --iterations 1 | ||
| else | ||
| echo -e "\n${YELLOW}Desktop GPU delegate is not supported. Skipping Desktop GPU run.${NC}" | ||
| fi | ||
|
|
||
|
|
||
| run_case "Run: EfficientNet Dynamic INT8 on Desktop (CPU)" \ | ||
| litert run "$MODEL_DIR/efficientnet/efficientnet_b1_int8_dynamic.tflite" --desktop --cpu --iterations 1 | ||
|
|
||
| if has_android_device; then | ||
| echo -e "\n${GREEN}Android device detected. Running Android inference...${NC}" | ||
| run_case "Run: EfficientNet FP32 on Android (CPU)" \ | ||
| litert run "$EFFICIENTNET_TFLITE" --android --cpu --iterations 1 | ||
|
|
||
| run_case "Run: EfficientNet FP32 on Android (GPU)" \ | ||
| litert run "$EFFICIENTNET_TFLITE" --android --gpu --iterations 1 | ||
|
|
||
| run_case "Run: EfficientNet Dynamic INT8 on Android (CPU)" \ | ||
| litert run "$MODEL_DIR/efficientnet/efficientnet_b1_int8_dynamic.tflite" --android --cpu --iterations 1 | ||
| fi | ||
|
|
||
| # --- 4. Benchmark (Android) --- | ||
| if has_android_device; then | ||
| echo -e "\n${GREEN}Android device detected. Running Android benchmarks...${NC}" | ||
| run_case "Benchmark: EfficientNet FP32 on Android (CPU)" \ | ||
| litert benchmark "$EFFICIENTNET_TFLITE" --android | ||
|
|
||
| run_case "Benchmark: EfficientNet FP32 on Android (GPU)" \ | ||
| litert benchmark "$EFFICIENTNET_TFLITE" --android --gpu | ||
|
|
||
| run_case "Benchmark: EfficientNet Dynamic INT8 on Android" \ | ||
| litert benchmark "$MODEL_DIR/efficientnet/efficientnet_b1_int8_dynamic.tflite" --android | ||
| else | ||
| echo -e "\n${YELLOW}No Android device detected. Skipping benchmarks (litert benchmark only supports Android/GCP).${NC}" | ||
| fi | ||
|
|
||
|
|
||
| # --- 5. Compile (AOT Compilation) --- | ||
| # TODO: Add this back when we fix the NPU compile issue. | ||
| # run_case "Compile: EfficientNet FP32 for Qualcomm sm8750 NPU" \ | ||
| # litert compile "$EFFICIENTNET_TFLITE" --target sm8750 --output-dir "$MODEL_DIR/efficientnet" | ||
|
|
||
| # --- 6. Visualize (Model Explorer) --- | ||
| run_case "Visualize: Launch Model Explorer in the background" \ | ||
| litert visualize "$EFFICIENTNET_TFLITE" | ||
|
|
||
| run_case "Visualize: Stop all Model Explorer servers" \ | ||
| litert visualize --stop-all | ||
|
|
||
|
|
||
| # --- Summary Report --- | ||
| print_summary_report "EfficientNet" | ||
|
|
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,61 @@ | ||
| #!/bin/bash | ||
| # LiteRT CLI Gemma4 LLM Demo & Test Script | ||
| set -e | ||
|
|
||
|
|
||
| echo -e "${BLUE}${BOLD}==================================================================${NC}" | ||
| echo -e "${BLUE}${BOLD}>>> LiteRT CLI Gemma4 LLM Demo Script${NC}" | ||
| echo -e "${BLUE}${BOLD}==================================================================${NC}" | ||
|
|
||
| # --- Environment Setup --- | ||
| export SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| export REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" | ||
| export LITERT_CLI_ROOT="/tmp/litert_cli_gemma4" | ||
|
|
||
| # Source shared utilities | ||
| source "$SCRIPT_DIR/demo_utils.sh" | ||
|
|
||
|
|
||
| # Clean up and create work directory | ||
| echo -e "\n${YELLOW}Setting up workspace at: $LITERT_CLI_ROOT...${NC}" | ||
| rm -rf "$LITERT_CLI_ROOT" | ||
| mkdir -p "$LITERT_CLI_ROOT" | ||
| cd "$LITERT_CLI_ROOT" | ||
|
|
||
| # Create output directories | ||
| export MODEL_DIR="$LITERT_CLI_ROOT/models" | ||
| mkdir -p "$MODEL_DIR" | ||
|
|
||
|
|
||
| # Create Python virtual environment | ||
| echo -e "${YELLOW}Creating Python virtual environment...${NC}" | ||
| python3 -m venv venv_gemma4 | ||
| source venv_gemma4/bin/activate | ||
|
|
||
| # Install litert-cli from source | ||
| echo -e "${YELLOW}Installing litert-cli from source...${NC}" | ||
| pip install -e "$REPO_ROOT" | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # --- 1. Convert HuggingFace Model google/gemma-4-E2B-it --- | ||
| # TODO: Bring this back when we add support for --externalize_embedder in CLI convert command. | ||
| # run_case "Convert: HuggingFace google/gemma-4-E2B-it" \ | ||
| # litert convert google/gemma-4-E2B-it --output "$MODEL_DIR/gemma4" | ||
|
|
||
|
|
||
| # --- 2. Run Gemma4 Generative LLM Model --- | ||
| run_case "Run Gemma4: Generative inference with custom prompt" \ | ||
| litert lm run gemma-4-E2B-it.litertlm --prompt "Explain machine learning in one sentence." | ||
|
|
||
|
|
||
| # --- 3. Benchmark Gemma4 LLM Model --- | ||
| run_case "Benchmark Gemma4: Local benchmark of LLM generation" \ | ||
| litert lm benchmark gemma-4-E2B-it.litertlm | ||
|
|
||
|
|
||
|
|
||
| # --- Summary Report --- | ||
| print_summary_report "Gemma4" | ||
|
|
||
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,119 @@ | ||
| #!/bin/bash | ||
| # LiteRT CLI ResNet Demo & Test Script | ||
| set -e | ||
|
|
||
| echo -e "${BLUE}${BOLD}==================================================================${NC}" | ||
| echo -e "${BLUE}${BOLD}>>> LiteRT CLI ResNet Demo Script${NC}" | ||
| echo -e "${BLUE}${BOLD}==================================================================${NC}" | ||
|
|
||
| # --- Environment Setup --- | ||
| export SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| export REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" | ||
| export LITERT_CLI_ROOT="/tmp/litert_cli_resnet" | ||
|
|
||
| # Source shared utilities | ||
| source "$SCRIPT_DIR/demo_utils.sh" | ||
|
|
||
|
|
||
| # Clean up and create work directory | ||
| echo -e "\n${YELLOW}Setting up workspace at: $LITERT_CLI_ROOT...${NC}" | ||
| rm -rf "$LITERT_CLI_ROOT" | ||
| mkdir -p "$LITERT_CLI_ROOT" | ||
| cd "$LITERT_CLI_ROOT" | ||
|
|
||
| # Create Python virtual environment | ||
| echo -e "${YELLOW}Creating Python virtual environment...${NC}" | ||
| python3 -m venv venv_resnet | ||
| source venv_resnet/bin/activate | ||
|
|
||
| # Create output directories | ||
| export MODEL_DIR="$LITERT_CLI_ROOT/models" | ||
| mkdir -p "$MODEL_DIR" | ||
|
|
||
| # Test data directory | ||
| export TEST_DATA_DIR="$REPO_ROOT/litert_cli/test_data" | ||
|
|
||
| # Install litert-cli from source | ||
| echo -e "${YELLOW}Installing litert-cli from source...${NC}" | ||
| pip install -e "$REPO_ROOT" | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # --- 1. Convert PyTorch ResNet18 model to LiteRT --- | ||
| run_case "Convert: PyTorch ResNet18 to LiteRT" \ | ||
| litert convert "$TEST_DATA_DIR/resnet18.py" --output "$MODEL_DIR/resnet18" | ||
|
|
||
| # Verify the converted model exists | ||
| RESNET_TFLITE="$MODEL_DIR/resnet18/resnet18.tflite" | ||
| if [ ! -f "$RESNET_TFLITE" ]; then | ||
| echo -e "${RED}Error: Converted model not found at $RESNET_TFLITE${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # --- 2. Quantize the ResNet18 model --- | ||
| run_case "Quantize: ResNet18 Dynamic Range INT8" \ | ||
| litert quantize "$RESNET_TFLITE" --type int8_dynamic --output "$MODEL_DIR/resnet18/resnet18_int8_dynamic.tflite" | ||
|
|
||
| run_case "Quantize: ResNet18 Weight-Only INT8" \ | ||
| litert quantize "$RESNET_TFLITE" --type int8_weight_only --output "$MODEL_DIR/resnet18/resnet18_int8_weight_only.tflite" | ||
|
|
||
| # --- 3. Run Inference (Desktop & Android) --- | ||
| run_case "Run: ResNet18 FP32 on Desktop (CPU)" \ | ||
| litert run "$RESNET_TFLITE" --desktop --cpu --iterations 1 | ||
|
|
||
| if has_desktop_gpu "$RESNET_TFLITE"; then | ||
| run_case "Run: ResNet18 FP32 on Desktop (GPU)" \ | ||
| litert run "$RESNET_TFLITE" --desktop --gpu --iterations 1 | ||
| else | ||
| echo -e "\n${YELLOW}Desktop GPU delegate is not supported. Skipping Desktop GPU run.${NC}" | ||
| fi | ||
|
|
||
|
|
||
| run_case "Run: ResNet18 Dynamic INT8 on Desktop (CPU)" \ | ||
| litert run "$MODEL_DIR/resnet18/resnet18_int8_dynamic.tflite" --desktop --cpu --iterations 1 | ||
|
|
||
| if has_android_device; then | ||
| echo -e "\n${GREEN}Android device detected. Running Android inference...${NC}" | ||
| run_case "Run: ResNet18 FP32 on Android (CPU)" \ | ||
| litert run "$RESNET_TFLITE" --android --cpu --iterations 1 | ||
|
|
||
| run_case "Run: ResNet18 FP32 on Android (GPU)" \ | ||
| litert run "$RESNET_TFLITE" --android --gpu --iterations 1 | ||
|
|
||
| run_case "Run: ResNet18 Dynamic INT8 on Android (CPU)" \ | ||
| litert run "$MODEL_DIR/resnet18/resnet18_int8_dynamic.tflite" --android --cpu --iterations 1 | ||
| fi | ||
|
|
||
| # --- 4. Benchmark (Android) --- | ||
| if has_android_device; then | ||
| echo -e "\n${GREEN}Android device detected. Running Android benchmarks...${NC}" | ||
| run_case "Benchmark: ResNet18 FP32 on Android (CPU)" \ | ||
| litert benchmark "$RESNET_TFLITE" --android | ||
|
|
||
| run_case "Benchmark: ResNet18 FP32 on Android (GPU)" \ | ||
| litert benchmark "$RESNET_TFLITE" --android --gpu | ||
|
|
||
| run_case "Benchmark: ResNet18 Dynamic INT8 on Android" \ | ||
| litert benchmark "$MODEL_DIR/resnet18/resnet18_int8_dynamic.tflite" --android | ||
| else | ||
| echo -e "\n${YELLOW}No Android device detected. Skipping benchmarks (litert benchmark only supports Android/GCP).${NC}" | ||
| fi | ||
|
|
||
|
|
||
| # --- 5. Compile (AOT Compilation) --- | ||
| # TODO: Add this back when we fix the NPU compile issue. | ||
| # run_case "Compile: ResNet18 FP32 for Qualcomm sm8750 NPU" \ | ||
| # litert compile "$RESNET_TFLITE" --target sm8750 --output-dir "$MODEL_DIR/resnet18" | ||
|
|
||
| # --- 6. Visualize (Model Explorer) --- | ||
| run_case "Visualize: Launch Model Explorer in the background" \ | ||
| litert visualize "$RESNET_TFLITE" | ||
|
|
||
| run_case "Visualize: Stop all Model Explorer servers" \ | ||
| litert visualize --stop-all | ||
|
|
||
|
|
||
| # --- Summary Report --- | ||
| print_summary_report "ResNet" | ||
|
|
Oops, something went wrong.
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.
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.
This might be not working, if you don't have models above.
But this should work:
litert lm run
--from-huggingface-repo=litert-community/gemma-4-E2B-it-litert-lm
gemma-4-E2B-it.litertlm
--prompt="What is the capital of France?"
Basically we can replace the examples below from "litert-lm", to "litert lm":
https://ai.google.dev/edge/litert-lm/cli
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.
DONE