Converting and deploying a PyTorch model tutorial

Note

For details on platform support, installation, and usage of ExecuTorch, please refer to the official documentation:

This tutorial describes how to convert and deploy a PyTorch model using the ML SDK for Vulkan®. In this tutorial, we generate a sample PyTorch file with a single MaxPool2D operation to demonstrate each step of the end-to-end workflow.

ExecuTorch can be installed via prebuilt wheels:

Note

Here we are installing from a developmental wheel. In the future, replace it with an official release.

pip install --upgrade --pre -f https://download.pytorch.org/whl/nightly/executorch/ "executorch==1.0.0.dev20250916"

Download the ExecuTorch repo, and install the required dependencies using the script.

Note

In order to run the setup script, Git username and email need to be configured. For example:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git clone https://github.com/pytorch/executorch.git
./executorch/examples/arm/setup.sh --disable-ethos-u-deps
  1. Add the ML SDK Model Converter to PATH:

The ExecuTorch backend relies on the ML SDK Model Converter.

export PATH=/path/containing/model-converter/:$PATH
which model-converter

This should print out the path to the model-converter binary.

  1. Run the following python script to create a PyTorch model for a single MaxPool2D operation.

#!/usr/bin/env python3
#
# SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates <open-source-office@arm.com>
# SPDX-License-Identifier: Apache-2.0
#
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from executorch.backends.arm.vgf import VgfCompileSpec
from executorch.backends.arm.vgf import VgfPartitioner
from executorch.exir import EdgeCompileConfig
from executorch.exir import to_edge_transform_and_lower


# Define model
class MaxPoolModel(nn.Module):
    def __init__(self):
        super(MaxPoolModel, self).__init__()
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)

    def forward(self, x):
        x = self.pool(x)
        return x


# Generate test input
example_input = torch.randn(1, 3, 64, 64)
np.save("input-0.npy", example_input.numpy())

model = MaxPoolModel().eval()

# Save the VGF model
compile_spec = VgfCompileSpec()
compile_spec.dump_intermediate_artifacts_to(".")

partitioner = VgfPartitioner(compile_spec)

exported_program = torch.export.export_for_training(model, (example_input,))

to_edge_transform_and_lower(
    exported_program,
    partitioner=[partitioner],
    compile_config=EdgeCompileConfig(
        _check_ir_validity=False,
    ),
)
python MaxPool2DModel.py

This generates a VGF file ${NAME}.vgf in the current working directory, where the tool generates ${NAME}. A matching example input is also generated in the same directory for testing.

  1. Run the VGF file with ML SDK Scenario Runner and ML Emulation Layer for Vulkan®

To execute the VGF file refer to How to run a VGF file with ML SDK Scenario Runner and ML Emulation Layer for Vulkan®.