blob: f8756835b9da9282ccfeefd33aedb364b327f3f2 [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": [],
Scott Todd7c611b92021-06-11 14:31:28 -07008 "collapsed_sections": [
9 "FH3IRpYTta2v"
10 ]
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070011 },
12 "kernelspec": {
13 "name": "python3",
14 "display_name": "Python 3"
15 }
16 },
17 "cells": [
18 {
19 "cell_type": "markdown",
20 "metadata": {
Scott Todd7c611b92021-06-11 14:31:28 -070021 "id": "FH3IRpYTta2v"
Geoffrey Martin-Noble552d3f82021-05-25 17:56:09 -070022 },
23 "source": [
Scott Todd7c611b92021-06-11 14:31:28 -070024 "##### 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-Noble552d3f82021-05-25 17:56:09 -070048 ]
49 },
50 {
51 "cell_type": "markdown",
52 "metadata": {
Scott Todd982db732021-01-08 10:16:25 -080053 "id": "uMVh8_lZDRa7"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070054 },
55 "source": [
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070056 "This notebook shows off some concepts of the low level IREE python bindings."
57 ]
58 },
59 {
60 "cell_type": "code",
61 "metadata": {
Scott Todd982db732021-01-08 10:16:25 -080062 "id": "Go2Nw7BgIHYU",
Scott Todd982db732021-01-08 10:16:25 -080063 "colab": {
64 "base_uri": "https://localhost:8080/"
Scott Todd7c611b92021-06-11 14:31:28 -070065 },
Scott Todd8ebe0022022-09-29 11:30:28 -070066 "outputId": "0339165a-a35f-4b46-9cf8-f22adc69a7fe"
Scott Todd982db732021-01-08 10:16:25 -080067 },
68 "source": [
Scott Toddc27c90c2024-11-14 08:18:35 -080069 "!python -m pip install --pre iree-base-compiler iree-base-runtime -f https://iree.dev/pip-release-links.html"
Scott Todd982db732021-01-08 10:16:25 -080070 ],
Scott Todd7c611b92021-06-11 14:31:28 -070071 "execution_count": 2,
Scott Todd982db732021-01-08 10:16:25 -080072 "outputs": [
73 {
74 "output_type": "stream",
Scott Todd3136e8f2022-05-15 09:21:38 -070075 "name": "stdout",
Scott Todd982db732021-01-08 10:16:25 -080076 "text": [
Scott Todd74d741e2022-06-08 15:03:40 -070077 "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n",
Scott Todd8c34b972023-10-24 09:34:49 -070078 "Looking in links: https://iree.dev/pip-release-links.html\n",
Stella Laurenzo0b0a4d82022-01-06 16:03:57 -080079 "Collecting iree-compiler\n",
Scott Todd3f51a552024-04-19 11:00:27 -070080 " 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 Todd8ebe0022022-09-29 11:30:28 -070081 "\u001b[K |████████████████████████████████| 49.7 MB 99 kB/s \n",
Stella Laurenzo0b0a4d82022-01-06 16:03:57 -080082 "\u001b[?25hCollecting iree-runtime\n",
Scott Todd3f51a552024-04-19 11:00:27 -070083 " 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 Todd8ebe0022022-09-29 11:30:28 -070084 "\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 Todd74d741e2022-06-08 15:03:40 -070086 "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from iree-compiler) (1.21.6)\n",
Scott Todd3136e8f2022-05-15 09:21:38 -070087 "Installing collected packages: iree-runtime, iree-compiler\n",
Scott Todd8ebe0022022-09-29 11:30:28 -070088 "Successfully installed iree-compiler-20220929.281 iree-runtime-20220929.281\n"
Scott Todd3136e8f2022-05-15 09:21:38 -070089 ]
Scott Todd982db732021-01-08 10:16:25 -080090 }
91 ]
92 },
93 {
94 "cell_type": "code",
95 "metadata": {
96 "id": "1F144M4wAFPz"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -070097 },
98 "source": [
99 "import numpy as np\n",
Scott Todd982db732021-01-08 10:16:25 -0800100 "\n",
Phoenix Meadowlarkb3270962021-03-18 09:20:38 -0700101 "from iree import runtime as ireert\n",
Phoenix Meadowlark5a8954e2021-03-17 18:22:12 -0700102 "from iree.compiler import compile_str"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700103 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700104 "execution_count": 3,
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700105 "outputs": []
106 },
107 {
108 "cell_type": "code",
109 "metadata": {
Scott Todd982db732021-01-08 10:16:25 -0800110 "id": "2Rq-JdzMAFPU"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700111 },
112 "source": [
Scott Todd85b61d72020-01-08 11:00:07 -0800113 "# Compile a module.\n",
Scott Todd982db732021-01-08 10:16:25 -0800114 "SIMPLE_MUL_ASM = \"\"\"\n",
Scott Todd85b61d72020-01-08 11:00:07 -0800115 " module @arithmetic {\n",
Scott Todd3136e8f2022-05-15 09:21:38 -0700116 " 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 Todd85b61d72020-01-08 11:00:07 -0800119 " } \n",
120 " }\n",
Scott Todd982db732021-01-08 10:16:25 -0800121 "\"\"\"\n",
122 "\n",
Scott Todd74d741e2022-06-08 15:03:40 -0700123 "# Compile using the vmvx (reference) target:\n",
Ben Vanik9aa83ed2022-08-06 12:55:34 -0700124 "compiled_flatbuffer = compile_str(SIMPLE_MUL_ASM, target_backends=[\"vmvx\"])"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700125 ],
Scott Todd3136e8f2022-05-15 09:21:38 -0700126 "execution_count": 4,
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700127 "outputs": []
128 },
129 {
130 "cell_type": "code",
131 "metadata": {
Scott Todd85b61d72020-01-08 11:00:07 -0800132 "id": "TNQiNeOU_cpK",
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700133 "colab": {
Scott Todd982db732021-01-08 10:16:25 -0800134 "base_uri": "https://localhost:8080/"
Scott Todd85b61d72020-01-08 11:00:07 -0800135 },
Scott Todd8ebe0022022-09-29 11:30:28 -0700136 "outputId": "4f112fcf-34fe-4d36-c5da-825dc721bdbf"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700137 },
138 "source": [
Scott Todd85b61d72020-01-08 11:00:07 -0800139 "# Register the module with a runtime context.\n",
Scott Todd74d741e2022-06-08 15:03:40 -0700140 "# Use the \"local-task\" CPU driver, which can load the vmvx executable:\n",
141 "config = ireert.Config(\"local-task\")\n",
Scott Todd85b61d72020-01-08 11:00:07 -0800142 "ctx = ireert.SystemContext(config=config)\n",
Ben Vanik9aa83ed2022-08-06 12:55:34 -0700143 "vm_module = ireert.VmModule.from_flatbuffer(ctx.instance, compiled_flatbuffer)\n",
not-jenni1093a062021-05-19 11:23:47 -0700144 "ctx.add_vm_module(vm_module)\n",
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700145 "\n",
Scott Todd85b61d72020-01-08 11:00:07 -0800146 "# 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 Todd3136e8f2022-05-15 09:21:38 -0700151 "results = f(arg0, arg1).to_host()\n",
Scott Todd85b61d72020-01-08 11:00:07 -0800152 "print(\"Results:\", results)"
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700153 ],
Scott Todd8ebe0022022-09-29 11:30:28 -0700154 "execution_count": 5,
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700155 "outputs": [
156 {
157 "output_type": "stream",
Scott Todd3136e8f2022-05-15 09:21:38 -0700158 "name": "stdout",
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700159 "text": [
Scott Todd85b61d72020-01-08 11:00:07 -0800160 "INVOKE simple_mul\n",
161 "Results: [ 4. 10. 18. 28.]\n"
Scott Todd3136e8f2022-05-15 09:21:38 -0700162 ]
Stella Laurenzo3bdefc22019-10-23 17:29:18 -0700163 }
164 ]
165 }
166 ]
Scott Toddc7b29122023-02-21 17:33:35 -0800167}