blob: bd54fac80b890051bf790a79c0062ff886cdb71c [file] [log] [blame]
Stella Laurenzo3bdefc22019-10-23 17:29:18 -07001{
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-Noble552d3f82021-05-25 17:56:09 -070019 "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 Todd982db732021-01-08 10:16:25 -080034 "id": "uMVh8_lZDRa7"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070035 },
36 "source": [
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070037 "This notebook shows off some concepts of the low level IREE python bindings."
38 ]
39 },
40 {
41 "cell_type": "code",
42 "metadata": {
Scott Todd982db732021-01-08 10:16:25 -080043 "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 Laurenzo3bdefc22019-10-23 17:29:18 -070075 },
76 "source": [
77 "import numpy as np\n",
Scott Todd982db732021-01-08 10:16:25 -080078 "\n",
Phoenix Meadowlarkb3270962021-03-18 09:20:38 -070079 "from iree import runtime as ireert\n",
Phoenix Meadowlark5a8954e2021-03-17 18:22:12 -070080 "from iree.compiler import compile_str"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070081 ],
Scott Todd982db732021-01-08 10:16:25 -080082 "execution_count": 2,
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070083 "outputs": []
84 },
85 {
86 "cell_type": "code",
87 "metadata": {
Scott Todd982db732021-01-08 10:16:25 -080088 "id": "2Rq-JdzMAFPU"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070089 },
90 "source": [
Scott Todd85b61d72020-01-08 11:00:07 -080091 "# Compile a module.\n",
Scott Todd982db732021-01-08 10:16:25 -080092 "SIMPLE_MUL_ASM = \"\"\"\n",
Scott Todd85b61d72020-01-08 11:00:07 -080093 " module @arithmetic {\n",
94 " func @simple_mul(%arg0: tensor<4xf32>, %arg1: tensor<4xf32>) -> tensor<4xf32>\n",
95 " attributes { iree.module.export } {\n",
Mehdi Aminiccc47e82020-07-06 21:51:24 -070096 " %0 = \"mhlo.multiply\"(%arg0, %arg1) {name = \"mul.1\"} : (tensor<4xf32>, tensor<4xf32>) -> tensor<4xf32>\n",
Scott Todd85b61d72020-01-08 11:00:07 -080097 " return %0 : tensor<4xf32>\n",
98 " } \n",
99 " }\n",
Scott Todd982db732021-01-08 10:16:25 -0800100 "\"\"\"\n",
101 "\n",
Ben Vanikab3b79f2021-05-14 11:19:17 -0700102 "compiled_flatbuffer = compile_str(SIMPLE_MUL_ASM, target_backends=[\"vmvx\"])\n",
Scott Todd982db732021-01-08 10:16:25 -0800103 "vm_module = ireert.VmModule.from_flatbuffer(compiled_flatbuffer)"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700104 ],
Scott Todd982db732021-01-08 10:16:25 -0800105 "execution_count": 3,
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700106 "outputs": []
107 },
108 {
109 "cell_type": "code",
110 "metadata": {
Scott Todd85b61d72020-01-08 11:00:07 -0800111 "id": "TNQiNeOU_cpK",
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700112 "colab": {
Scott Todd982db732021-01-08 10:16:25 -0800113 "base_uri": "https://localhost:8080/"
Scott Todd85b61d72020-01-08 11:00:07 -0800114 },
Scott Todd982db732021-01-08 10:16:25 -0800115 "outputId": "42df9878-4af7-458d-b23f-92791873f9ab"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700116 },
117 "source": [
Scott Todd85b61d72020-01-08 11:00:07 -0800118 "# Register the module with a runtime context.\n",
119 "# Use the CPU interpreter (which has the most implementation done):\n",
Ben Vanikab3b79f2021-05-14 11:19:17 -0700120 "config = ireert.Config(\"vmvx\")\n",
Scott Todd85b61d72020-01-08 11:00:07 -0800121 "ctx = ireert.SystemContext(config=config)\n",
not-jenni1093a062021-05-19 11:23:47 -0700122 "ctx.add_vm_module(vm_module)\n",
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700123 "\n",
Scott Todd85b61d72020-01-08 11:00:07 -0800124 "# 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 Laurenzo3bdefc22019-10-23 17:29:18 -0700131 ],
Scott Todd982db732021-01-08 10:16:25 -0800132 "execution_count": 4,
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700133 "outputs": [
134 {
135 "output_type": "stream",
136 "text": [
Scott Todd85b61d72020-01-08 11:00:07 -0800137 "INVOKE simple_mul\n",
138 "Results: [ 4. 10. 18. 28.]\n"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700139 ],
140 "name": "stdout"
Scott Todd85b61d72020-01-08 11:00:07 -0800141 },
142 {
143 "output_type": "stream",
144 "text": [
Ben Vanikab3b79f2021-05-14 11:19:17 -0700145 "Created IREE driver vmvx: <iree.runtime.binding.HalDriver object at 0x7f6201491458>\n",
Phoenix Meadowlarkb3270962021-03-18 09:20:38 -0700146 "SystemContext driver=<iree.runtime.binding.HalDriver object at 0x7f6201491458>\n"
Scott Todd85b61d72020-01-08 11:00:07 -0800147 ],
148 "name": "stderr"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700149 }
150 ]
151 }
152 ]
Phoenix Meadowlark9ae2e492021-03-16 09:56:51 -0700153}