Compiling AI Functions
Function’s raison d’être 🗽
Function is primarily designed to compile AI inference functions to run on-device. We will walk through the general workflow required to compile these functions.
Defining an AI Function
Let’s begin with a function that classifies an image, returning
the label along with a confidence score. To do so, we will use the MobileNet v2 model from
torchvision
:
The code above has nothing that is specific to Function. It is plain PyTorch code.
Compiling the AI Function
There are a few steps needed to prepare an AI function for compilation:
In this section, required changes to the above code are highlighted.
Decorating the Function
First, apply the @compile
decorator to the function to prepare it for compilation:
Defining the Compiler Sandbox
Depending on how you run AI inference, you will likely have to install libraries (e.g. PyTorch) and/or upload model
weights. To do so, create a Sandbox
:
Specifying Inference Backends
Let’s use the ONNXRuntime inference backend to run the AI model:
Compiling the Function
Now, compile the function using the Function CLI:
Supported Inference Backends
Function supports a fixed set of backends for running AI inference. You must opt in to using an inference backend for a specific model by providing inference metadata. The provided metadata will allow the Function compiler to lower the inference operation to native code. Below are supported inference metadata types:
You can use multiple inference backends to run a single model by providing multiple metadata
instances that
refer to the same model.
CoreML
Use the CoreMLInferenceMetadata
metadata type to compile a PyTorch model to
CoreML:
The CoreML inference backend is available only on iOS, macOS, and visionOS.
ONNX
Use the ONNXInferenceMetadata
metadata type to compile a Python model to ONNX:
ONNX Runtime
Use the ONNXRuntimeInferenceSessionMetadata
metadata type to compile an existing ONNX model and run it with
ONNXRuntime:
The model must exist at the provided model_path
in the compiler sandbox.
Llama.cpp
Coming soon 🤫.