Bring Your Own Model
In this tutorial we explain how to integrate your own AI model with ORA's AI Oracle. We will start by looking at mlgo repository and trying to understand what's happening there. At the end we will showcase how opML works, by running a simple dispute game script inside a docker container.
Understand how to transform an AI model inference code and integrate it with ORA's AI Oracle
Execute a simple dispute game and understand the process of AI inference verification.
git installed
Clone mlgo repository
Navigate to cloned repository
To install the required dependencies for your project, run the following command:
If there are some missing dependencies, make sure to install them in your Python environment.
First we need to train a DNN model using Pytorch. The training part is shown in examples/mnist/trainning/mnist.ipynb.
After the training model is saved at examples/mnist/models/mnist/mnist-small.state_dict.
Ggml is a file format that consists of version number, followed by three components that define a large language model: the model's hyperparameters, its vocabulary, and its weights. Ggml allows for more efficient inference runs on CPU. We will now convert the Python model to ggml format by executing following steps:
Position to mnist folder
Convert python model into ggml format
In order to convert AI model written in Python to ggml format we are executing python script and providing a file that stores the model as a parameter to the script. The output is the binary file in ggml format. Note that the model is saved in big-endian, making it easy to process in the big-endian MIPS-32 VM.
Next step is to write inference code in go language. Then we will transform go binary into MIPS VM executable file.
Go supports compilation to MIPS. However, the generated executable is in ELF format. We'd like to get a pure sequence of MIPS instructions instead. To build a ML program in MIPS VM execute the following steps:
Navigate to the mnist_mips directory and build go inference code
Build script will compile go code and then run compile.py script that will transform compiled go code to the MIPS VM executable file.
Now that we compiled our AI model and inference code into MIPS VM executable code.
We can test the dispute game process. We will use a bash script from opml repository to showcase the whole verification flow.
For this part of the tutorial we will use Docker, so make sure to have it installed.
Let's first check the content of the Dockerfile that we are using:
First we need to specify the operating system that runs inside our container. In this case we're using ubuntu:22.04.
Then we need to install all the necessary dependencies in order to run dispute game script.
Then we configure ssh keys, so that docker container can clone all the required repositories.
Position to the root directory inside docker container and clone opml repository along with its submodules.
Lastly, we tell docker to build executables and run the challenge script.
In order to successfully clone opml repository, you need to generate a new ssh key and add it to your Github account. Once it's generated, save the key in the local repository where Dockerfile is placed as id_rsa
. Then add the public key to your GitHub account.
Build the docker image
Run the local Ethereum node
In another terminal run the challenge script
After executing the steps above you should be able to see interactive challenge process in the console.
Script first deploys necessary contracts to the local node. Proposer opML node executes AI inference and then the challenger nodes can dispute it, if they think that the result is not valid. Challenger and proposer are interacting in order to find the differing step between their computations. Once the dispute step is found it's sent to the smart contract for the arbitration. If the challenge is successful the proposer node gets slashed.
In this tutorial we achieved the following:
converted our AI model from Python to ggml format
compiled AI inference code written in go to MIPS VM executable format
run the dispute game inside a docker container and understood the opML verification process