2021-08-10 04:55:20 +08:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
2021-09-28 07:44:07 +08:00
"id": "0060537a",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.\n",
"# See https://llvm.org/LICENSE.txt for license information.\n",
2021-10-05 01:53:46 +08:00
"# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n",
2021-10-05 01:52:12 +08:00
"# Also available under a BSD-style license. See LICENSE."
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "0e6ebf55",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"## Setup\n",
"\n",
"### Configuring jupyter kernel.\n",
"\n",
2021-09-28 07:44:07 +08:00
"We assume that you have followed the instructions for setting up torch-mlir. See [README.md](https://github.com/llvm/torch-mlir) if not.\n",
2021-08-10 04:55:20 +08:00
"\n",
2021-09-28 07:44:07 +08:00
"To run this notebook, you need to configure jupyter to access the torch-mlir Python modules that are built as part of your development setup. An easy way to do this is to run the following command with the same Python (and shell) that is correctly set up and able to run the torch-mlir end-to-end tests with RefBackend:\n",
2021-08-10 04:55:20 +08:00
"\n",
"```shell\n",
2021-09-28 07:44:07 +08:00
"python -m ipykernel install --user --name=torch-mlir --env PYTHONPATH \"$PYTHONPATH\"\n",
2021-08-10 04:55:20 +08:00
"```\n",
"\n",
"You should then have an option in jupyter to select this kernel for running this notebook.\n",
"\n",
"**TODO**: Make this notebook standalone and work based entirely on pip-installable packages.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "2aa20cfc",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"### Additional dependencies for this notebook"
]
},
{
"cell_type": "code",
2021-09-28 07:44:07 +08:00
"execution_count": 2,
"id": "ee25f979",
2021-08-10 04:55:20 +08:00
"metadata": {},
2021-09-28 07:44:07 +08:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Requirement already satisfied: requests in /usr/lib/python3/dist-packages (2.25.1)\n",
"Requirement already satisfied: pillow in /usr/lib/python3/dist-packages (8.1.2)\n"
]
}
],
2021-08-10 04:55:20 +08:00
"source": [
"!python -m pip install requests pillow"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "c8c95904",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "c4149213",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
2021-09-28 07:44:07 +08:00
"### torch-mlir imports"
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "code",
"execution_count": 3,
2021-09-28 07:44:07 +08:00
"id": "847868f0",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"import torchvision\n",
"\n",
2021-09-25 06:03:25 +08:00
"from torch_mlir.dialects.torch.importer.jit_ir import ClassAnnotator, ModuleBuilder\n",
"from torch_mlir.dialects.torch.importer.jit_ir.torchscript_annotations import extract_annotations\n",
2021-09-28 07:44:07 +08:00
"from torch_mlir_e2e_test.torchscript.annotations import annotate_args, export\n",
2021-09-25 06:03:25 +08:00
"\n",
2021-09-28 07:44:07 +08:00
"from torch_mlir.passmanager import PassManager\n",
"from torch_mlir_e2e_test.linalg_on_tensors_backends.refbackend import RefBackendLinalgOnTensorsBackend"
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "91992cea",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"### General dependencies"
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 4,
2021-09-28 07:44:07 +08:00
"id": "a80963f8",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"from PIL import Image"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "322eaa75",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"### Utilities"
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 5,
2021-09-28 07:44:07 +08:00
"id": "f4bdf926",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
2021-09-28 07:44:07 +08:00
"BACKEND = RefBackendLinalgOnTensorsBackend()\n",
"\n",
2021-09-25 06:03:25 +08:00
"def compile_module(program: torch.nn.Module):\n",
" \"\"\"Compiles a torch.nn.Module into an compiled artifact.\n",
2021-08-10 04:55:20 +08:00
" \n",
" This artifact is suitable for inclusion in a user's application. It only\n",
2021-09-25 06:03:25 +08:00
" depends on the rebackend runtime.\n",
2021-08-10 04:55:20 +08:00
" \"\"\"\n",
" ## Script the program.\n",
" scripted = torch.jit.script(program)\n",
"\n",
" ## Extract annotations.\n",
2021-09-25 06:03:25 +08:00
" class_annotator = ClassAnnotator()\n",
2021-08-10 04:55:20 +08:00
" extract_annotations(program, scripted, class_annotator)\n",
"\n",
" ## Import the TorchScript module into MLIR.\n",
2021-09-25 06:03:25 +08:00
" mb = ModuleBuilder()\n",
2021-08-10 04:55:20 +08:00
" mb.import_module(scripted._c, class_annotator)\n",
"\n",
2021-09-28 07:44:07 +08:00
" ## Lower the MLIR from TorchScript to RefBackend, passing through linalg-on-tensors.\n",
2021-10-01 03:45:43 +08:00
" pm = PassManager.parse('torchscript-module-to-linalg-on-tensors-backend-pipeline', mb.module.context)\n",
2021-09-28 07:44:07 +08:00
" pm.run(mb.module)\n",
2021-08-10 04:55:20 +08:00
"\n",
2021-09-28 07:44:07 +08:00
" ## Invoke RefBackend to compile to compiled artifact form.\n",
" return BACKEND.compile(mb.module)"
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "71cc1403",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"## Basic tanh module"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "269f3dc5",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"A simple tiny module that is easier to understand and look at than a full ResNet."
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 6,
2021-09-28 07:44:07 +08:00
"id": "aed1869b",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"class TanhModule(torch.nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
"\n",
2021-09-28 07:44:07 +08:00
" # The `export` annotation controls which parts of the model the torch-mlir\n",
2021-08-10 04:55:20 +08:00
" # compiler should assume are externally accessible. By default,\n",
2021-09-28 07:44:07 +08:00
" # the torch-mlir compiler will only export the explicitly exported functions.\n",
" # NOTE: This is different from `torch.jit.export`. The `torch.jit.export`\n",
2021-08-10 04:55:20 +08:00
" # decorator controls which methods of the original torch.nn.Module get\n",
" # compiled into TorchScript. This decorator\n",
2021-09-28 07:44:07 +08:00
" # (`torch_mlir_e2e_test.torchscript.annotations.export`) controls which TorchScript\n",
" # methods are compiled by torch-mlir. \n",
2021-08-10 04:55:20 +08:00
" @export\n",
2021-09-28 07:44:07 +08:00
" # The `annotate_args` annotation provides metadata to the torch-mlir compiler\n",
2021-08-10 04:55:20 +08:00
" # regarding the constraints on arguments. The value `None` means that\n",
" # no additional information is provided for that argument. Otherwise,\n",
" # it is a 3-tuple specifying the shape (`-1` for unknown extent along a dimension),\n",
" # along with the dtype and whether the tensor has\n",
" # value semantics (this would be False if you need to mutate an input\n",
" # tensor in-place).\n",
" @annotate_args([\n",
" None,\n",
" ([-1], torch.float32, True)\n",
" ])\n",
" def forward(self, a):\n",
" return torch.tanh(a)"
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 7,
2021-09-28 07:44:07 +08:00
"id": "71a421a0",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2021-09-25 06:03:25 +08:00
"array([-0.7615941, 0.7615941, 0. ], dtype=float32)"
2021-08-10 04:55:20 +08:00
]
},
2021-09-25 06:03:25 +08:00
"execution_count": 7,
2021-08-10 04:55:20 +08:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Create the module and compile it.\n",
2021-09-25 06:03:25 +08:00
"compiled = compile_module(TanhModule())\n",
"# Loads the compiled artifact into the runtime\n",
2021-09-28 07:44:07 +08:00
"jit_module = BACKEND.load(compiled)\n",
2021-08-10 04:55:20 +08:00
"# Run it!\n",
2021-09-25 06:03:25 +08:00
"jit_module.forward(torch.tensor([-1.0, 1.0, 0.0]).numpy())"
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "b7f8773b",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"## ResNet Inference"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "dd226e86",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"Do some one-time preparation."
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 8,
2021-09-28 07:44:07 +08:00
"id": "88e8a5c3",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"def _load_labels():\n",
" classes_text = requests.get(\n",
" \"https://raw.githubusercontent.com/cathyzhyi/ml-data/main/imagenet-classes.txt\",\n",
" stream=True,\n",
" ).text\n",
" labels = [line.strip() for line in classes_text.splitlines()]\n",
" return labels\n",
"IMAGENET_LABELS = _load_labels()\n",
"\n",
"def _get_preprocess_transforms():\n",
" # See preprocessing specification at: https://pytorch.org/vision/stable/models.html\n",
" T = torchvision.transforms\n",
" return T.Compose(\n",
" [\n",
" T.Resize(256),\n",
" T.CenterCrop(224),\n",
" T.ToTensor(),\n",
" T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n",
" ]\n",
" )\n",
"PREPROCESS_TRANSFORMS = _get_preprocess_transforms()"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "d42e8a1b",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"Define some helper functions."
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 9,
2021-09-28 07:44:07 +08:00
"id": "abd53b61",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"def fetch_image(url: str):\n",
" # Use some \"realistic\" User-Agent so that we aren't mistaken for being a scraper.\n",
" headers = {\"User-Agent\": \"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36\"}\n",
" return Image.open(requests.get(url, headers=headers, stream=True).raw).convert(\"RGB\")\n",
"\n",
"def preprocess_image(img: Image):\n",
" # Preprocess and add a batch dimension.\n",
" return torch.unsqueeze(PREPROCESS_TRANSFORMS(img), 0)"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "dbec8e96",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"### Fetch our sample image."
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 10,
2021-09-28 07:44:07 +08:00
"id": "8270ef94",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [
{
"data": {
2021-09-28 07:44:07 +08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAk4AAAHgCAIAAADcxXWhAAEAAElEQVR4nKz92Y5kSZIlCBIRL3eVRTdTM3M391g8q6o7uqsxQKF+ax7nJ+azBpipt8EUenKJCM+IcHczU9NFtrvxQkTzwCLqlp3VtQBzkfDQVBOV5QozE9E5hw7h//X/9n9PKYQQUl6Mxbp2TV1bS1dXV99995vbmztDVQr89PTy80+f94cvZCdjwTmP4FSobft3bz/c3983TTPNw6dPP/3tpz+HOH347v7f/2//yw+//d1tfwsKqqAEMabTMuWclZAFu3blK6sKHGGZYbebd/uHu7t2e9WtVp0qpCWmlMgAEfVNA5eL4NcL4b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6H4ZQzlw+IiD5LCQwp5xBBVQGEzPkuee+bplmv103dOee8933XV1XlTc0pD6d/OhwOwzitdi9dt5qWebfbzfPc9/3V1erDhw8fPnzofK2qcQlExCnVdX2uTphFhFWE84toSk3XN1XlnAUAUIAsYomy5ByjiCAassYaRwT4r9aps0jkL5kMWEOvkazUMYioAHpZ4URwvuGIAKiKmSXnfDwed7v9H//pzz/99Iuq3t7e3t3dtW1rra2qap5HALDGzfNcjjZVHYajq92yLPMwSspN09zc3BhDVeWH0/H27sY6w5y8rZjT8Xg8HA6VrZZlOQyn4/H4l7/8rWvazeZqtVpdXV01TbPZbNq+8waU8hRMtoY5iaiIlPym8r723tq
2021-08-10 04:55:20 +08:00
"text/plain": [
2021-09-28 07:44:07 +08:00
"<PIL.Image.Image image mode=RGB size=590x480 at 0x7FC9CE7278E0>"
2021-08-10 04:55:20 +08:00
]
},
2021-09-25 06:03:25 +08:00
"execution_count": 10,
2021-08-10 04:55:20 +08:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"img = fetch_image(\"https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Red_Smooth_Saluki.jpg/590px-Red_Smooth_Saluki.jpg\")\n",
"img_preprocessed = preprocess_image(img)\n",
"\n",
"img"
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "55852670",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"### Define the module and compile it"
]
},
{
"cell_type": "code",
2021-09-25 06:03:25 +08:00
"execution_count": 11,
2021-09-28 07:44:07 +08:00
"id": "3af95ee7",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"class ResNet18Module(torch.nn.Module):\n",
" def __init__(self):\n",
" super().__init__()\n",
" self.resnet = torchvision.models.resnet18(pretrained=True)\n",
" self.train(False)\n",
" @export\n",
" @annotate_args([\n",
" None,\n",
" ([1, 3, 224, 224], torch.float32, True),\n",
" ])\n",
" def forward(self, img):\n",
" return self.resnet.forward(img)"
]
},
{
"cell_type": "code",
2021-09-28 07:44:07 +08:00
"execution_count": 12,
"id": "89cbebf6",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
"# Create the module and compile it.\n",
2021-09-25 06:03:25 +08:00
"compiled = compile_module(ResNet18Module())\n",
2021-08-10 04:55:20 +08:00
"# Load it for in-process execution.\n",
2021-09-28 07:44:07 +08:00
"jit_module = BACKEND.load(compiled)"
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "markdown",
2021-09-28 07:44:07 +08:00
"id": "2890c8c6",
2021-08-10 04:55:20 +08:00
"metadata": {},
"source": [
"### Execute the classification!"
]
},
{
"cell_type": "code",
2021-09-28 07:44:07 +08:00
"execution_count": 13,
"id": "23476f38",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
2021-09-25 06:03:25 +08:00
"logits = torch.from_numpy(jit_module.forward(img_preprocessed.numpy()))"
2021-08-10 04:55:20 +08:00
]
},
{
"cell_type": "code",
2021-09-28 07:44:07 +08:00
"execution_count": 14,
"id": "a826c789",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [],
"source": [
2021-09-28 07:44:07 +08:00
"# torch-mlir doesn't currently support these final postprocessing operations, so perform them in Torch.\n",
2021-08-10 04:55:20 +08:00
"def top3_possibilities(logits):\n",
" _, indexes = torch.sort(logits, descending=True)\n",
" percentage = torch.nn.functional.softmax(logits, dim=1)[0] * 100\n",
" top3 = [(IMAGENET_LABELS[idx], percentage[idx].item()) for idx in indexes[0][:3]]\n",
" return top3"
]
},
{
"cell_type": "code",
2021-09-28 07:44:07 +08:00
"execution_count": 15,
"id": "355ca74e",
2021-08-10 04:55:20 +08:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2021-09-28 07:44:07 +08:00
"[('Saluki, gazelle hound', 74.8702163696289),\n",
" ('Ibizan hound, Ibizan Podenco', 18.07537841796875),\n",
" ('whippet', 6.3394775390625)]"
2021-08-10 04:55:20 +08:00
]
},
2021-09-28 07:44:07 +08:00
"execution_count": 15,
2021-08-10 04:55:20 +08:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"top3_possibilities(logits)"
]
2021-09-28 07:44:07 +08:00
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a622c8f",
"metadata": {},
"outputs": [],
"source": []
2021-08-10 04:55:20 +08:00
}
],
"metadata": {
2021-09-28 07:44:07 +08:00
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
},
2021-08-10 04:55:20 +08:00
"kernelspec": {
2021-09-28 07:44:07 +08:00
"display_name": "torch-mlir",
2021-08-10 04:55:20 +08:00
"language": "python",
2021-09-28 07:44:07 +08:00
"name": "torch-mlir"
2021-08-10 04:55:20 +08:00
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2021-09-25 06:03:25 +08:00
"version": "3.9.7"
2021-08-10 04:55:20 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}