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": [], |
| 8 | "collapsed_sections": [] |
| 9 | }, |
| 10 | "kernelspec": { |
| 11 | "name": "python3", |
| 12 | "display_name": "Python 3" |
| 13 | } |
| 14 | }, |
| 15 | "cells": [ |
| 16 | { |
| 17 | "cell_type": "markdown", |
| 18 | "metadata": { |
Geoffrey Martin-Noble | 552d3f8 | 2021-05-25 17:56:09 -0700 | [diff] [blame^] | 19 | "id": "license" |
| 20 | }, |
| 21 | "source": [ |
| 22 | "```\n", |
| 23 | "Copyright 2020 The IREE Authors\n", |
| 24 | "\n", |
| 25 | "Licensed under the Apache License v2.0 with LLVM Exceptions.\n", |
| 26 | "See https://llvm.org/LICENSE.txt for license information.\n", |
| 27 | "SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n", |
| 28 | "```\n" |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "cell_type": "markdown", |
| 33 | "metadata": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 34 | "id": "uMVh8_lZDRa7" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 35 | }, |
| 36 | "source": [ |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 37 | "This notebook shows off some concepts of the low level IREE python bindings." |
| 38 | ] |
| 39 | }, |
| 40 | { |
| 41 | "cell_type": "code", |
| 42 | "metadata": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 43 | "id": "Go2Nw7BgIHYU", |
| 44 | "outputId": "dbafcd78-3cc4-4fa0-f408-1b8e6270084c", |
| 45 | "colab": { |
| 46 | "base_uri": "https://localhost:8080/" |
| 47 | } |
| 48 | }, |
| 49 | "source": [ |
| 50 | "!python -m pip install iree-compiler-snapshot iree-runtime-snapshot -f https://github.com/google/iree/releases" |
| 51 | ], |
| 52 | "execution_count": 1, |
| 53 | "outputs": [ |
| 54 | { |
| 55 | "output_type": "stream", |
| 56 | "text": [ |
| 57 | "Looking in links: https://github.com/google/iree/releases\n", |
| 58 | "Collecting iree-compiler-snapshot\n", |
| 59 | "\u001b[?25l Downloading https://github.com/google/iree/releases/download/snapshot-20210105.14/iree_compiler_snapshot-20210105.14-py3-none-manylinux2014_x86_64.whl (32.1MB)\n", |
| 60 | "\u001b[K |████████████████████████████████| 32.1MB 155kB/s \n", |
| 61 | "\u001b[?25hCollecting iree-runtime-snapshot\n", |
| 62 | "\u001b[?25l Downloading https://github.com/google/iree/releases/download/snapshot-20210105.14/iree_runtime_snapshot-20210105.14-cp36-cp36m-manylinux2014_x86_64.whl (1.0MB)\n", |
| 63 | "\u001b[K |████████████████████████████████| 1.0MB 46.9MB/s \n", |
| 64 | "\u001b[?25hInstalling collected packages: iree-compiler-snapshot, iree-runtime-snapshot\n", |
| 65 | "Successfully installed iree-compiler-snapshot-20210105.14 iree-runtime-snapshot-20210105.14\n" |
| 66 | ], |
| 67 | "name": "stdout" |
| 68 | } |
| 69 | ] |
| 70 | }, |
| 71 | { |
| 72 | "cell_type": "code", |
| 73 | "metadata": { |
| 74 | "id": "1F144M4wAFPz" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 75 | }, |
| 76 | "source": [ |
| 77 | "import numpy as np\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 78 | "\n", |
Phoenix Meadowlark | b327096 | 2021-03-18 09:20:38 -0700 | [diff] [blame] | 79 | "from iree import runtime as ireert\n", |
Phoenix Meadowlark | 5a8954e | 2021-03-17 18:22:12 -0700 | [diff] [blame] | 80 | "from iree.compiler import compile_str" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 81 | ], |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 82 | "execution_count": 2, |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 83 | "outputs": [] |
| 84 | }, |
| 85 | { |
| 86 | "cell_type": "code", |
| 87 | "metadata": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 88 | "id": "2Rq-JdzMAFPU" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 89 | }, |
| 90 | "source": [ |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 91 | "# Compile a module.\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 92 | "SIMPLE_MUL_ASM = \"\"\"\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 93 | " module @arithmetic {\n", |
| 94 | " func @simple_mul(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32>\n", |
| 95 | " attributes { iree.module.export } {\n", |
Mehdi Amini | ccc47e8 | 2020-07-06 21:51:24 -0700 | [diff] [blame] | 96 | " %0 = \"mhlo.multiply\"(%arg0, %arg1) {name = \"mul.1\"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 97 | " return %0 : tensor<4xf32>\n", |
| 98 | " } \n", |
| 99 | " }\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 100 | "\"\"\"\n", |
| 101 | "\n", |
Ben Vanik | ab3b79f | 2021-05-14 11:19:17 -0700 | [diff] [blame] | 102 | "compiled_flatbuffer = compile_str(SIMPLE_MUL_ASM, target_backends=[\"vmvx\"])\n", |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 103 | "vm_module = ireert.VmModule.from_flatbuffer(compiled_flatbuffer)" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 104 | ], |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 105 | "execution_count": 3, |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 106 | "outputs": [] |
| 107 | }, |
| 108 | { |
| 109 | "cell_type": "code", |
| 110 | "metadata": { |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 111 | "id": "TNQiNeOU_cpK", |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 112 | "colab": { |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 113 | "base_uri": "https://localhost:8080/" |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 114 | }, |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 115 | "outputId": "42df9878-4af7-458d-b23f-92791873f9ab" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 116 | }, |
| 117 | "source": [ |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 118 | "# Register the module with a runtime context.\n", |
| 119 | "# Use the CPU interpreter (which has the most implementation done):\n", |
Ben Vanik | ab3b79f | 2021-05-14 11:19:17 -0700 | [diff] [blame] | 120 | "config = ireert.Config(\"vmvx\")\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 121 | "ctx = ireert.SystemContext(config=config)\n", |
not-jenni | 1093a06 | 2021-05-19 11:23:47 -0700 | [diff] [blame] | 122 | "ctx.add_vm_module(vm_module)\n", |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 123 | "\n", |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 124 | "# Invoke the function and print the result.\n", |
| 125 | "print(\"INVOKE simple_mul\")\n", |
| 126 | "arg0 = np.array([1., 2., 3., 4.], dtype=np.float32)\n", |
| 127 | "arg1 = np.array([4., 5., 6., 7.], dtype=np.float32)\n", |
| 128 | "f = ctx.modules.arithmetic[\"simple_mul\"]\n", |
| 129 | "results = f(arg0, arg1)\n", |
| 130 | "print(\"Results:\", results)" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 131 | ], |
Scott Todd | 982db73 | 2021-01-08 10:16:25 -0800 | [diff] [blame] | 132 | "execution_count": 4, |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 133 | "outputs": [ |
| 134 | { |
| 135 | "output_type": "stream", |
| 136 | "text": [ |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 137 | "INVOKE simple_mul\n", |
| 138 | "Results: [ 4. 10. 18. 28.]\n" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 139 | ], |
| 140 | "name": "stdout" |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 141 | }, |
| 142 | { |
| 143 | "output_type": "stream", |
| 144 | "text": [ |
Ben Vanik | ab3b79f | 2021-05-14 11:19:17 -0700 | [diff] [blame] | 145 | "Created IREE driver vmvx: <iree.runtime.binding.HalDriver object at 0x7f6201491458>\n", |
Phoenix Meadowlark | b327096 | 2021-03-18 09:20:38 -0700 | [diff] [blame] | 146 | "SystemContext driver=<iree.runtime.binding.HalDriver object at 0x7f6201491458>\n" |
Scott Todd | 85b61d7 | 2020-01-08 11:00:07 -0800 | [diff] [blame] | 147 | ], |
| 148 | "name": "stderr" |
Stella Laurenzo | 3bdefc2 | 2019-10-23 17:29:18 -0700 | [diff] [blame] | 149 | } |
| 150 | ] |
| 151 | } |
| 152 | ] |
Phoenix Meadowlark | 9ae2e49 | 2021-03-16 09:56:51 -0700 | [diff] [blame] | 153 | } |