|
| 1 | +# Resnet18 Example For WASI-NN with PyTorch Backend |
| 2 | + |
| 3 | +This package is a high-level Rust bindings for [wasi-nn] example of Resnet18 with PyTorch backend. |
| 4 | + |
| 5 | +[wasi-nn]: https://github.com/WebAssembly/wasi-nn |
| 6 | + |
| 7 | +## Dependencies |
| 8 | + |
| 9 | +This crate depends on the `wasi-nn` in the `Cargo.toml`: |
| 10 | + |
| 11 | +```toml |
| 12 | +[dependencies] |
| 13 | +wasi-nn = "0.6.0" |
| 14 | +``` |
| 15 | + |
| 16 | +## Build |
| 17 | + |
| 18 | +Compile the application to WebAssembly: |
| 19 | + |
| 20 | +```bash |
| 21 | +cargo build --target=wasm32-wasi --release |
| 22 | +``` |
| 23 | + |
| 24 | +Because here, we will demonstrate two ways of using wasi-nn. So the output WASM files will be at [`target/wasm32-wasi/release/wasmedge-wasinn-example-resnet18-image.wasm`](wasmedge-wasinn-example-resnet18-image.wasm) and [`target/wasm32-wasi/release/wasmedge-wasinn-example-resnet18-image-named-model.wasm`](wasmedge-wasinn-example-resnet18-image-named-model.wasm). |
| 25 | +To speed up the image processing, we can enable the AOT mode in WasmEdge with: |
| 26 | + |
| 27 | +```bash |
| 28 | +wasmedgec rust/target/wasm32-wasi/release/wasmedge-wasinn-example-resnet18-image.wasm wasmedge-wasinn-example-resnet18-image-aot.wasm |
| 29 | + |
| 30 | +wasmedgec rust/target/wasm32-wasi/release/wasmedge-wasinn-example-resnet18-image-named-model.wasm wasmedge-wasinn-example-resnet18-image-named-model-aot.wasm |
| 31 | +``` |
| 32 | + |
| 33 | +## Run |
| 34 | + |
| 35 | +### Generate Model |
| 36 | + |
| 37 | +First generate the fixture of the pre-trained mobilenet with the script: |
| 38 | + |
| 39 | +```bash |
| 40 | +pip3 install torch==2.4.1 numpy pillow --extra-index-url https://download.pytorch.org/whl/lts/1.8/cpu |
| 41 | +# generate the model fixture |
| 42 | +python3 gen_resnet18_model.py |
| 43 | +``` |
| 44 | + |
| 45 | +(Or you can use the pre-generated one at [`resnet18.pt`](resnet18.pt)) |
| 46 | + |
| 47 | +### Test Image |
| 48 | + |
| 49 | +The testing image `input.jpg` is downloaded from <https://github.com/bytecodealliance/wasi-nn/raw/main/rust/examples/images/1.jpg> with license Apache-2.0 |
| 50 | + |
| 51 | +### Generate Tensor |
| 52 | + |
| 53 | +If you want to generate the [raw tensor](image-1x3x224x224.rgb), you can run: |
| 54 | + |
| 55 | +```bash |
| 56 | +python3 gen_tensor.py input.jpg image-1x3x224x224.rgb |
| 57 | +``` |
| 58 | + |
| 59 | +### Execute |
| 60 | + |
| 61 | +Users should [install the WasmEdge with WASI-NN PyTorch backend plug-in](https://wasmedge.org/docs/start/install#wasi-nn-plug-in-with-pytorch-backend). |
| 62 | + |
| 63 | +Execute the WASM with the `wasmedge` with PyTorch supporting: |
| 64 | + |
| 65 | +- Case 1: |
| 66 | + |
| 67 | +```bash |
| 68 | +wasmedge --dir .:. wasmedge-wasinn-example-resnet18-image.wasm resnet18.pt input.jpg |
| 69 | +``` |
| 70 | + |
| 71 | +You will get the output: |
| 72 | + |
| 73 | +```console |
| 74 | +Loaded graph into wasi-nn with ID: 0 |
| 75 | +Created wasi-nn execution context with ID: 0 |
| 76 | +Read input tensor, size in bytes: 602112 |
| 77 | +Executed graph inference |
| 78 | + 1.) [954](18.0458)banana |
| 79 | + 2.) [940](15.6954)spaghetti squash |
| 80 | + 3.) [951](14.1337)lemon |
| 81 | + 4.) [942](13.2925)butternut squash |
| 82 | + 5.) [941](10.6792)acorn squash |
| 83 | +``` |
| 84 | + |
| 85 | +- Case 2: Apply named model feature |
| 86 | +> requirement wasi-nn >= 0.5.0 and WasmEdge-plugin-wasi_nn-(*) >= 0.13.4 and |
| 87 | +> --nn-preload argument format follow <name>:<encoding>:<target>:<model_path> |
| 88 | +
|
| 89 | +```bash |
| 90 | +wasmedge --dir .:. --nn-preload demo:PyTorch:CPU:resnet18.pt wasmedge-wasinn-example-resnet18-image-named-model.wasm demo input.jpg |
| 91 | +``` |
| 92 | + |
| 93 | +You will get the same output: |
| 94 | + |
| 95 | +```console |
| 96 | +Loaded graph into wasi-nn with ID: 0 |
| 97 | +Created wasi-nn execution context with ID: 0 |
| 98 | +Read input tensor, size in bytes: 602112 |
| 99 | +Executed graph inference |
| 100 | + 1.) [954](18.0458)banana |
| 101 | + 2.) [940](15.6954)spaghetti squash |
| 102 | + 3.) [951](14.1337)lemon |
| 103 | + 4.) [942](13.2925)butternut squash |
| 104 | + 5.) [941](10.6792)acorn squash |
| 105 | +``` |
| 106 | + |
| 107 | +## Run from AOTInductor |
| 108 | + |
| 109 | +### Generate Model |
| 110 | +PyTorch backend also support load from the AOTInductor (Shared Library). To compile the pytorch model, please follow the Pytorch official tutorial. |
| 111 | + |
| 112 | +* https://pytorch.org/tutorials/recipes/torch_export_aoti_python.html |
| 113 | + |
| 114 | + |
| 115 | +Or you can use the pre-generated one at [`resnet18_pt2.so`](resnet18_pt2.so). However it may not suitable for your machine. it is suggested to use [`gen_resnet18_aoti`](gen_resnet18_aoti) recompile the model. |
| 116 | + |
| 117 | +> Notice: The AOTInductor from pip will use old c++ abi interface, it is maybe incompatible with wasmedge release, you may need to install the libtorch **without c++11 abi** and rebuild the wasmedge with `-DWASMEDGE_USE_CXX11_ABI=OFF`. |
| 118 | +
|
| 119 | + |
| 120 | +```bash |
| 121 | +## Build Wasmedge with cmake example |
| 122 | +cmake -Bbuild -GNinja -DWASMEDGE_USE_CXX11_ABI=OFF -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=PyTorch . |
| 123 | +``` |
| 124 | + |
| 125 | +### Execute |
| 126 | + |
| 127 | +To run the AOT Inductor, you need use `--nn-preload` with `PyTorchAOTI` interface and specify absolute path to load the shared library. |
| 128 | + |
| 129 | +```bash |
| 130 | +export LD_LIBRARY_PATH=/path_to_libtorch/lib |
| 131 | +./wasmedge --dir .:. --nn-preload demo:PyTorchAOTI:CPU:/absolute_path_model/resnet18_pt2.so wasmedge-wasinn-example-resnet18-image-named-model.wasm demo input.jpg |
| 132 | +``` |
| 133 | + |
| 134 | +```console |
| 135 | +Loaded graph into wasi-nn with ID: 0 |
| 136 | +Created wasi-nn execution context with ID: 0 |
| 137 | +Read input tensor, size in bytes: 602112 |
| 138 | +Executed graph inference |
| 139 | + 1.) [954](18.0458)banana |
| 140 | + 2.) [940](15.6954)spaghetti squash |
| 141 | + 3.) [951](14.1337)lemon |
| 142 | + 4.) [942](13.2925)butternut squash |
| 143 | + 5.) [941](10.6792)acorn squash |
| 144 | +``` |
0 commit comments