# Convert the model to ExecuTorch format

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/)
- [Set up your development environment](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/1-prerequisites/)
- [Download the Stable Audio Open Small model](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/2-download-model/)
- [Convert the model to ExecuTorch format](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/3-convert-model/)
- [Build and run on macOS](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/4-build-run-macos/)
- [Build and run on Android](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/5-build-run-android/)
- [Next Steps](https://learn.arm.com/learning-paths/mobile-graphics-and-gaming/run-stable-audio-with-executorch/_next-steps/)

## Create a virtual environment
Create and activate a Python virtual environment to manage dependencies. Python 3.10 is recommended for compatibility with the required packages:
```
cd $WORKSPACE/ML-examples/kleidiai-examples/audiogen-et/
python3.10 -m venv .venv
source .venv/bin/activate
```

## Install ExecuTorch
Install the ExecuTorch package version 1.0.0:
```
pip install executorch==1.0.0
```
Alternatively, clone the ExecuTorch repository and run the installation script:
```
git clone [https://github.com/pytorch/executorch.git](https://github.com/pytorch/executorch.git)
cd executorch
git checkout v1.0.0
bash ./install_executorch.sh
cd ..
```

## Install Stable Audio Tools
Install the Stable Audio Open tools dependency:
```
pip install git+[https://github.com/Stability-AI/stable-audio-tools.git@31932349d98c550c48711e7a5a40b24aa3d7c509](https://github.com/Stability-AI/stable-audio-tools.git@31932349d98c550c48711e7a5a40b24aa3d7c509)
```

## Export the model submodules
Export the three model submodules to ExecuTorch format using the provided script from the `audiogen-et` directory:
```
python ./scripts/export_sao.py --ckpt_path $WORKSPACE/model.ckpt --model_config $WORKSPACE/model_config.json
```
The script converts all three submodules (Conditioners, DiT, and AutoEncoder) and exports them to `.pte` files.

### Troubleshooting conversion issues
If you see a `FileNotFoundError` related to missing `.fbs` files during conversion:
```
__output__FileNotFoundError: [Errno 2] No such file or directory: '../kleidiai-examples/audiogen-et/scripts/executorch/exir/_serialize/program.fbs'
```
Run these commands to copy the required schema files:
```
export EXECUTORCH_ROOT=<PATH-TO-EXECUTORCH>
cp $EXECUTORCH_ROOT/schema/program.fbs $EXECUTORCH_ROOT/exir/_serialize/program.fbs
cp $EXECUTORCH_ROOT/schema/scalar_type.fbs $EXECUTORCH_ROOT/exir/_serialize/scalar_type.fbs
```
Then retry the export script.

## Verify exported models
After successful conversion, three `.pte` model files are created in the current directory:
- `conditioners_model.pte`
- `dit_model.pte`
- `autoencoder_model.pte`

These files are required to run the audio generation application on Android or macOS. Verify the files were created:
```
ls -lh conditioners_model.pte dit_model.pte autoencoder_model.pte
```
The files should be in `$WORKSPACE/ML-examples/kleidiai-examples/audiogen-et/`.

## What you’ve accomplished and what’s next
You’ve installed ExecuTorch and the Stable Audio Tools dependencies, then converted the three Stable Audio Open Small submodules (Conditioners, DiT, and AutoEncoder) to ExecuTorch format. Next, you’ll build the audio generation application for your target platform (Android or macOS).
