{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using PyLRE to Deploy your Exported ONNX Model\n",
    "\n",
    "You have exported an ONNX model with LEIP Optimize, and now you want to deploy it in a target environment. This tutorial provides step-by-step instructions for loading an optimized artifact, creating an LRE instance, and performing inference."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Runtime Setup\n",
    "\n",
    "We need two components to execute a model on your target:\n",
    "- target-compatible and model-compatible runtime (LRE)\n",
    "- target-compatible model or model library (optimized output)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pylre\n",
    "from pylre import LatentRuntimeEngine as LRE\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using LEIP Optimize, get your optimized artifact. This tutorial assumes the model is compiled for `float32` and for `CPU` target. For more information, consult the [LEIP Optimize tutorial for optimizing an ONNX model](https://docs.latentai.io/leip/optimize/latest/content/notebooks/BYOMwithForgeTutorialOnnx/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "optimized_artifact_path = \"path/to/exported.onnx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pylre_options = pylre.ONNXOptions(execution_provider=\"cpu\", precision=\"float32\")\n",
    "lre = LRE(optimized_artifact_path, options=pylre_options)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With this LRE object, we can introspect on the model we have optimized"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lre.get_metadata()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a random tensor to do inference\n",
    "As the model expects only one input, we pick the first one to create a random input tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "shape = lre.input_shapes[0]\n",
    "type = lre.input_dtypes[0]\n",
    "input = np.random.random(shape).astype(type)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "With this input data tensor, we can run an inference on the model LRE instantiation we created."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "output = lre(input)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This output is in a device-independent target. But you may want to convert into a more amenable format for postprocessing. We will use NumPy for this, but depending on your application and hardware usage, you may want to explore other formats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "numpy_output = np.from_dlpack(output[0])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Verifying expected output shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "expected_output_shape = lre.output_shapes[0]\n",
    "assert numpy_output.shape == expected_output_shape"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylre-release",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
