Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 1 | { |
| 2 | "nbformat": 4, |
| 3 | "nbformat_minor": 0, |
| 4 | "metadata": { |
| 5 | "colab": { |
| 6 | "name": "low_level_invoke_function.ipynb", |
| 7 | "provenance": [], |
Scott Todd | 7c611b9 | 2021-06-11 14:31:28 -0700 | [diff] [blame] | 8 | "collapsed_sections": [ |
| 9 | "FH3IRpYTta2v" |
| 10 | ] |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 11 | }, |
| 12 | "kernelspec": { |
| 13 | "name": "python3", |
| 14 | "display_name": "Python 3" |
| 15 | } |
| 16 | }, |
| 17 | "cells": [ |
| 18 | { |
| 19 | "cell_type": "markdown", |
| 20 | "metadata": { |
Scott Todd | 7c611b9 | 2021-06-11 14:31:28 -0700 | [diff] [blame] | 21 | "id": "FH3IRpYTta2v" |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 22 | }, |
| 23 | "source": [ |
Scott Todd | 7c611b9 | 2021-06-11 14:31:28 -0700 | [diff] [blame] | 24 | "##### Copyright 2019 The IREE Authors" |
| 25 | ] |
| 26 | }, |
| 27 | { |
| 28 | "cell_type": "code", |
| 29 | "metadata": { |
| 30 | "id": "mWGa71_Ct2ug", |
| 31 | "cellView": "form" |
| 32 | }, |
| 33 | "source": [ |
| 34 | "#@title Licensed under the Apache License v2.0 with LLVM Exceptions.\n", |
| 35 | "# See https://llvm.org/LICENSE.txt for license information.\n", |
| 36 | "# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception" |
| 37 | ], |
| 38 | "execution_count": 1, |
| 39 | "outputs": [] |
| 40 | }, |
| 41 | { |
| 42 | "cell_type": "markdown", |
| 43 | "metadata": { |
| 44 | "id": "ZEmrd07EvthK" |
| 45 | }, |
| 46 | "source": [ |
| 47 | "# Low Level Invoke Function" |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame] | 48 | ] |
| 49 | }, |
| 50 | { |
| 51 | "cell_type": "markdown", |
| 52 | "metadata": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 53 | "id": "uMVh8_lZDRa7" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 54 | }, |
| 55 | "source": [ |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 56 | "This notebook shows off some concepts of the low level IREE python bindings." |
| 57 | ] |
| 58 | }, |
| 59 | { |
| 60 | "cell_type": "code", |
| 61 | "metadata": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 62 | "id": "Go2Nw7BgIHYU", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 63 | "colab": { |
| 64 | "base_uri": "https://localhost:8080/" |
Scott Todd | 7c611b9 | 2021-06-11 14:31:28 -0700 | [diff] [blame] | 65 | }, |
Scott Todd | 8ebe002 | 2022-09-29 11:30:28 -0700 | [diff] [blame] | 66 | "outputId": "0339165a-a35f-4b46-9cf8-f22adc69a7fe" |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 67 | }, |
| 68 | "source": [ |
Scott Todd | c27c90c | 2024-11-14 08:18:35 -0800 | [diff] [blame] | 69 | "!python -m pip install --pre iree-base-compiler iree-base-runtime -f https://iree.dev/pip-release-links.html" |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 70 | ], |
Scott Todd | 7c611b9 | 2021-06-11 14:31:28 -0700 | [diff] [blame] | 71 | "execution_count": 2, |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 72 | "outputs": [ |
| 73 | { |
| 74 | "output_type": "stream", |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 75 | "name": "stdout", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 76 | "text": [ |
Scott Todd | 74d741e | 2022-06-08 15:03:40 -0700 | [diff] [blame] | 77 | "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", |
Scott Todd | 8c34b97 | 2023-10-24 09:34:49 -0700 | [diff] [blame] | 78 | "Looking in links: https://iree.dev/pip-release-links.html\n", |
Stella Laurenzo | 0b0a4d8 | 2022-01-06 16:03:57 -0800 | [diff] [blame] | 79 | "Collecting iree-compiler\n", |
Scott Todd | 3f51a55 | 2024-04-19 11:00:27 -0700 | [diff] [blame] | 80 | " Downloading https://github.com/iree-org/iree/releases/download/candidate-20220929.281/iree_compiler-20220929.281-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.7 MB)\n", |
Scott Todd | 8ebe002 | 2022-09-29 11:30:28 -0700 | [diff] [blame] | 81 | "\u001b[K |████████████████████████████████| 49.7 MB 99 kB/s \n", |
Stella Laurenzo | 0b0a4d8 | 2022-01-06 16:03:57 -0800 | [diff] [blame] | 82 | "\u001b[?25hCollecting iree-runtime\n", |
Scott Todd | 3f51a55 | 2024-04-19 11:00:27 -0700 | [diff] [blame] | 83 | " Downloading https://github.com/iree-org/iree/releases/download/candidate-20220929.281/iree_runtime-20220929.281-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.3 MB)\n", |
Scott Todd | 8ebe002 | 2022-09-29 11:30:28 -0700 | [diff] [blame] | 84 | "\u001b[K |████████████████████████████████| 2.3 MB 40.7 MB/s \n", |
| 85 | "\u001b[?25hRequirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (6.0)\n", |
Scott Todd | 74d741e | 2022-06-08 15:03:40 -0700 | [diff] [blame] | 86 | "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (1.21.6)\n", |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 87 | "Installing collected packages: iree-runtime, iree-compiler\n", |
Scott Todd | 8ebe002 | 2022-09-29 11:30:28 -0700 | [diff] [blame] | 88 | "Successfully installed iree-compiler-20220929.281 iree-runtime-20220929.281\n" |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 89 | ] |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 90 | } |
| 91 | ] |
| 92 | }, |
| 93 | { |
| 94 | "cell_type": "code", |
| 95 | "metadata": { |
| 96 | "id": "1F144M4wAFPz" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 97 | }, |
| 98 | "source": [ |
| 99 | "import numpy as np\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 100 | "\n", |
Phoenix Meadowlark | b327096 | 2021-03-18 09:20:38 -0700 | [diff] [blame] | 101 | "from iree import runtime as ireert\n", |
Phoenix Meadowlark | 5a8954e | 2021-03-17 18:22:12 -0700 | [diff] [blame] | 102 | "from iree.compiler import compile_str" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 103 | ], |
Scott Todd | 7c611b9 | 2021-06-11 14:31:28 -0700 | [diff] [blame] | 104 | "execution_count": 3, |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 105 | "outputs": [] |
| 106 | }, |
| 107 | { |
| 108 | "cell_type": "code", |
| 109 | "metadata": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 110 | "id": "2Rq-JdzMAFPU" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 111 | }, |
| 112 | "source": [ |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 113 | "# Compile a module.\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 114 | "SIMPLE_MUL_ASM = \"\"\"\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 115 | " module @arithmetic {\n", |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 116 | " func.func @simple_mul(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32> {\n", |
| 117 | " %0 = arith.mulf %arg0, %arg1 : tensor<4xf32>\n", |
| 118 | " return %0 : tensor<4xf32>\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 119 | " } \n", |
| 120 | " }\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 121 | "\"\"\"\n", |
| 122 | "\n", |
Scott Todd | 74d741e | 2022-06-08 15:03:40 -0700 | [diff] [blame] | 123 | "# Compile using the vmvx (reference) target:\n", |
Ben Vanik | 9aa83ed | 2022-08-06 12:55:34 -0700 | [diff] [blame] | 124 | "compiled_flatbuffer = compile_str(SIMPLE_MUL_ASM, target_backends=[\"vmvx\"])" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 125 | ], |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 126 | "execution_count": 4, |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 127 | "outputs": [] |
| 128 | }, |
| 129 | { |
| 130 | "cell_type": "code", |
| 131 | "metadata": { |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 132 | "id": "TNQiNeOU_cpK", |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 133 | "colab": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 134 | "base_uri": "https://localhost:8080/" |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 135 | }, |
Scott Todd | 8ebe002 | 2022-09-29 11:30:28 -0700 | [diff] [blame] | 136 | "outputId": "4f112fcf-34fe-4d36-c5da-825dc721bdbf" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 137 | }, |
| 138 | "source": [ |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 139 | "# Register the module with a runtime context.\n", |
Scott Todd | 74d741e | 2022-06-08 15:03:40 -0700 | [diff] [blame] | 140 | "# Use the \"local-task\" CPU driver, which can load the vmvx executable:\n", |
| 141 | "config = ireert.Config(\"local-task\")\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 142 | "ctx = ireert.SystemContext(config=config)\n", |
Ben Vanik | 9aa83ed | 2022-08-06 12:55:34 -0700 | [diff] [blame] | 143 | "vm_module = ireert.VmModule.from_flatbuffer(ctx.instance, compiled_flatbuffer)\n", |
not-jenni | 1093a06 | 2021-05-19 11:23:47 -0700 | [diff] [blame] | 144 | "ctx.add_vm_module(vm_module)\n", |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 145 | "\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 146 | "# Invoke the function and print the result.\n", |
| 147 | "print(\"INVOKE simple_mul\")\n", |
| 148 | "arg0 = np.array([1., 2., 3., 4.], dtype=np.float32)\n", |
| 149 | "arg1 = np.array([4., 5., 6., 7.], dtype=np.float32)\n", |
| 150 | "f = ctx.modules.arithmetic[\"simple_mul\"]\n", |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 151 | "results = f(arg0, arg1).to_host()\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 152 | "print(\"Results:\", results)" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 153 | ], |
Scott Todd | 8ebe002 | 2022-09-29 11:30:28 -0700 | [diff] [blame] | 154 | "execution_count": 5, |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 155 | "outputs": [ |
| 156 | { |
| 157 | "output_type": "stream", |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 158 | "name": "stdout", |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 159 | "text": [ |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 160 | "INVOKE simple_mul\n", |
| 161 | "Results: [ 4. 10. 18. 28.]\n" |
Scott Todd | 3136e8f | 2022-05-15 09:21:38 -0700 | [diff] [blame] | 162 | ] |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 163 | } |
| 164 | ] |
| 165 | } |
| 166 | ] |
Scott Todd | c7b2912 | 2023-02-21 17:33:35 -0800 | [diff] [blame] | 167 | } |