blob: 7a0fdc908c04ed21621862b9c4effac97c61103b [file] [log] [blame]
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>6. Model Tuning with microTVM &mdash; tvm 0.17.dev0 documentation</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery-binder.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery-dataframe.css" type="text/css" />
<link rel="stylesheet" href="../../_static/sg_gallery-rendered-html.css" type="text/css" />
<link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../../_static/css/tlcpack_theme.css" type="text/css" />
<link rel="shortcut icon" href="../../_static/tvm-logo-square.png"/>
<script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
<script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
<script src="../../_static/jquery.js"></script>
<script src="../../_static/underscore.js"></script>
<script src="../../_static/doctools.js"></script>
<script type="text/javascript" src="../../_static/js/theme.js"></script>
<script type="text/javascript" src="../../_static/js/tlcpack_theme.js"></script>
<link rel="index" title="Index" href="../../genindex.html" />
<link rel="search" title="Search" href="../../search.html" />
<link rel="next" title="7. Running TVM on bare metal Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU with CMSIS-NN" href="micro_ethosu.html" />
<link rel="prev" title="5. Training Vision Models for microTVM on Arduino" href="micro_train.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<header class="header">
<div class="innercontainer">
<div class="headerInner d-flex justify-content-between align-items-center">
<div class="headerLogo">
<a href="https://tvm.apache.org/"><img src=https://tvm.apache.org/assets/images/logo.svg alt="logo"></a>
</div>
<div id="headMenu" class="headerNav">
<button type="button" id="closeHeadMenu" class="navCloseBtn"><img src="../../_static/img/close-icon.svg" alt="Close"></button>
<ul class="nav">
<li class="nav-item">
<a class="nav-link" href=https://tvm.apache.org/community>Community</a>
</li>
<li class="nav-item">
<a class="nav-link" href=https://tvm.apache.org/download>Download</a>
</li>
<li class="nav-item">
<a class="nav-link" href=https://tvm.apache.org/vta>VTA</a>
</li>
<li class="nav-item">
<a class="nav-link" href=https://tvm.apache.org/blog>Blog</a>
</li>
<li class="nav-item">
<a class="nav-link" href=https://tvm.apache.org/docs>Docs</a>
</li>
<li class="nav-item">
<a class="nav-link" href=https://tvmconf.org>Conference</a>
</li>
<li class="nav-item">
<a class="nav-link" href=https://github.com/apache/tvm/>Github</a>
</li>
</ul>
<div class="responsivetlcdropdown">
<button type="button" class="btn-link">
ASF
</button>
<ul>
<li>
<a href=https://apache.org/>Apache Homepage</a>
</li>
<li>
<a href=https://www.apache.org/licenses/>License</a>
</li>
<li>
<a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
</li>
<li>
<a href=https://www.apache.org/security/>Security</a>
</li>
<li>
<a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
</li>
<li>
<a href=https://www.apache.org/events/current-event>Events</a>
</li>
</ul>
</div>
</div>
<div class="responsiveMenuIcon">
<button type="button" id="menuBtn" class="btn-menu"><img src="../../_static/img/menu-icon.svg" alt="Menu Icon"></button>
</div>
<div class="tlcDropdown">
<div class="dropdown">
<button type="button" class="btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
ASF
</button>
<div class="dropdown-menu dropdown-menu-right">
<ul>
<li>
<a href=https://apache.org/>Apache Homepage</a>
</li>
<li>
<a href=https://www.apache.org/licenses/>License</a>
</li>
<li>
<a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
</li>
<li>
<a href=https://www.apache.org/security/>Security</a>
</li>
<li>
<a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
</li>
<li>
<a href=https://www.apache.org/events/current-event>Events</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</header>
<nav data-toggle="wy-nav-shift" class="wy-nav-side fixed">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../../index.html">
<img src="../../_static/tvm-logo-small.png" class="logo" alt="Logo"/>
</a>
<input type="checkbox" class="version-toggle-box" hidden id="version-toggle">
<label for="version-toggle" class="version-toggle-label">
<div tabindex="0" class="version version-selector version-selector-show">
0.17.dev0 <span class="chevron versions-hidden"><svg fill="none" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="m8 4 8 8-8 8" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/></svg></span><span class="chevron versions-shown"><svg fill="none" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="m4 8 8 8 8-8" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"/></svg></span>
</div>
</label>
<div class="version-details wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption" role="heading"><span class="caption-text">Versions</span></p>
<ol style="text-align: left">
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="/">0.17.dev0 (main)</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.8.0/">v0.8.0</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.9.0/">v0.9.0</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.10.0/">v0.10.0</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.11.0/">v0.11.0</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.12.0/">v0.12.0</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.13.0/">v0.13.0</a></div></li>
<li><div class="version"><a style="font-size: 0.8em; padding: 4px" href="v0.14.0/">v0.14.0</a></div></li>
</ol>
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption" role="heading"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../install/index.html">Installing TVM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../contribute/index.html">Contributor Guide</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">User Guide</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../tutorial/index.html">User Tutorial</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../index.html">How To Guides</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../compile_models/index.html">Compile Deep Learning Models</a></li>
<li class="toctree-l2"><a class="reference internal" href="../deploy/index.html">Deploy Models and Integrate TVM</a></li>
<li class="toctree-l2"><a class="reference internal" href="../work_with_relay/index.html">Work With Relay</a></li>
<li class="toctree-l2"><a class="reference internal" href="../work_with_schedules/index.html">Work With Tensor Expression and Schedules</a></li>
<li class="toctree-l2"><a class="reference internal" href="../optimize_operators/index.html">Optimize Tensor Operators</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tune_with_autotvm/index.html">Auto-Tune with Templates and AutoTVM</a></li>
<li class="toctree-l2"><a class="reference internal" href="../tune_with_autoscheduler/index.html">Use AutoScheduler for Template-Free Scheduling</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="index.html">Work With microTVM</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="micro_tvmc.html">1. microTVM CLI Tool</a></li>
<li class="toctree-l3"><a class="reference internal" href="micro_tflite.html">2. microTVM TFLite Tutorial</a></li>
<li class="toctree-l3"><a class="reference internal" href="micro_aot.html">3. microTVM Ahead-of-Time (AOT) Compilation</a></li>
<li class="toctree-l3"><a class="reference internal" href="micro_pytorch.html">4. microTVM PyTorch Tutorial</a></li>
<li class="toctree-l3"><a class="reference internal" href="micro_train.html">5. Training Vision Models for microTVM on Arduino</a></li>
<li class="toctree-l3 current"><a class="current reference internal" href="#">6. Model Tuning with microTVM</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#install-microtvm-python-dependencies">Install microTVM Python dependencies</a></li>
<li class="toctree-l4"><a class="reference internal" href="#install-zephyr">Install Zephyr</a></li>
<li class="toctree-l4"><a class="reference internal" href="#import-python-dependencies">Import Python dependencies</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="micro_ethosu.html">7. Running TVM on bare metal Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU with CMSIS-NN</a></li>
<li class="toctree-l3"><a class="reference internal" href="micro_mlperftiny.html">8. Creating Your MLPerfTiny Submission with microTVM</a></li>
<li class="toctree-l3"><a class="reference internal" href="micro_custom_ide.html">9. Bring microTVM to your own development environment</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../extend_tvm/index.html">Extend TVM</a></li>
<li class="toctree-l2"><a class="reference internal" href="../profile/index.html">Profile Models</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../errors.html">Handle TVM Errors</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../faq.html">Frequently Asked Questions</a></li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Developer Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../dev/tutorial/index.html">Developer Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../dev/how_to/how_to.html">Developer How-To Guide</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Architecture Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../arch/index.html">Design and Architecture</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Topic Guides</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../topic/microtvm/index.html">microTVM: TVM on bare-metal</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../topic/vta/index.html">VTA: Versatile Tensor Accelerator</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Reference Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../reference/langref/index.html">Language Reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/api/python/index.html">Python API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/api/links.html">Other APIs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../reference/publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../genindex.html">Index</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation" data-toggle="wy-nav-top">
<div class="togglemenu">
</div>
<div class="nav-content">
<!-- tvm -->
Table of Contents
</div>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../index.html">Docs</a> <span class="br-arrow">></span></li>
<li><a href="../index.html">How To Guides</a> <span class="br-arrow">></span></li>
<li><a href="index.html">Work With microTVM</a> <span class="br-arrow">></span></li>
<li>6. Model Tuning with microTVM</li>
<li class="wy-breadcrumbs-aside">
<a href="https://github.com/apache/tvm/edit/main/docs/how_to/work_with_microtvm/micro_autotune.rst" class="fa fa-github"> Edit on GitHub</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="sphx-glr-download-link-note admonition note">
<p class="admonition-title">Note</p>
<p>This tutorial can be used interactively with Google Colab! You can also click
<a class="reference internal" href="#sphx-glr-download-how-to-work-with-microtvm-micro-autotune-py"><span class="std std-ref">here</span></a> to run the Jupyter notebook locally.</p>
<a class="reference external image-reference" href="https://colab.research.google.com/github/apache/tvm-site/blob/asf-site/docs/_downloads/f83ba3df2d52f9b54cf141114359481a/micro_autotune.ipynb"><img alt="https://raw.githubusercontent.com/tlc-pack/web-data/main/images/utilities/colab_button.svg" class="align-center" src="https://raw.githubusercontent.com/tlc-pack/web-data/main/images/utilities/colab_button.svg" width="300px" /></a>
</div>
<div class="sphx-glr-example-title section" id="model-tuning-with-microtvm">
<span id="tutorial-micro-autotune"></span><span id="sphx-glr-how-to-work-with-microtvm-micro-autotune-py"></span><h1>6. Model Tuning with microTVM<a class="headerlink" href="#model-tuning-with-microtvm" title="Permalink to this headline"></a></h1>
<p><strong>Authors</strong>:
<a class="reference external" href="https://github.com/areusch">Andrew Reusch</a>,
<a class="reference external" href="https://github.com/mehrdadh">Mehrdad Hessar</a></p>
<p>This tutorial explains how to autotune a model using the C runtime.</p>
<div class="section" id="install-microtvm-python-dependencies">
<h2>Install microTVM Python dependencies<a class="headerlink" href="#install-microtvm-python-dependencies" title="Permalink to this headline"></a></h2>
<p>TVM does not include a package for Python serial communication, so
we must install one before using microTVM. We will also need TFLite
to load models.</p>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>%%shell
pip<span class="w"> </span>install<span class="w"> </span><span class="nv">pyserial</span><span class="o">==</span><span class="m">3</span>.5<span class="w"> </span><span class="nv">tflite</span><span class="o">==</span><span class="m">2</span>.1
</pre></div>
</div>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># You can skip the following section (installing Zephyr) if the following flag is False.</span>
<span class="c1"># Installing Zephyr takes ~20 min.</span>
<span class="kn">import</span> <span class="nn">os</span>
<a href="https://docs.python.org/3/library/functions.html#bool" title="builtins.bool" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">use_physical_hw</span></a> <span class="o">=</span> <span class="nb">bool</span><span class="p">(</span><a href="https://docs.python.org/3/library/os.html#os.getenv" title="os.getenv" class="sphx-glr-backref-module-os sphx-glr-backref-type-py-function"><span class="n">os</span><span class="o">.</span><span class="n">getenv</span></a><span class="p">(</span><span class="s2">&quot;TVM_MICRO_USE_HW&quot;</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="install-zephyr">
<h2>Install Zephyr<a class="headerlink" href="#install-zephyr" title="Permalink to this headline"></a></h2>
<blockquote>
<div><div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>%%shell
<span class="c1"># Install west and ninja</span>
python3<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>west
apt-get<span class="w"> </span>install<span class="w"> </span>-y<span class="w"> </span>ninja-build
<span class="c1"># Install ZephyrProject</span>
<span class="nv">ZEPHYR_PROJECT_PATH</span><span class="o">=</span><span class="s2">&quot;/content/zephyrproject&quot;</span>
<span class="nb">export</span><span class="w"> </span><span class="nv">ZEPHYR_BASE</span><span class="o">=</span><span class="si">${</span><span class="nv">ZEPHYR_PROJECT_PATH</span><span class="si">}</span>/zephyr
west<span class="w"> </span>init<span class="w"> </span><span class="si">${</span><span class="nv">ZEPHYR_PROJECT_PATH</span><span class="si">}</span>
<span class="nb">cd</span><span class="w"> </span><span class="si">${</span><span class="nv">ZEPHYR_BASE</span><span class="si">}</span>
git<span class="w"> </span>checkout<span class="w"> </span>v3.2-branch
<span class="nb">cd</span><span class="w"> </span>..
west<span class="w"> </span>update
west<span class="w"> </span>zephyr-export
chmod<span class="w"> </span>-R<span class="w"> </span>o+w<span class="w"> </span><span class="si">${</span><span class="nv">ZEPHYR_PROJECT_PATH</span><span class="si">}</span>
<span class="c1"># Install Zephyr SDK</span>
<span class="nb">cd</span><span class="w"> </span>/content
<span class="nv">ZEPHYR_SDK_VERSION</span><span class="o">=</span><span class="s2">&quot;0.15.2&quot;</span>
wget<span class="w"> </span><span class="s2">&quot;https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v</span><span class="si">${</span><span class="nv">ZEPHYR_SDK_VERSION</span><span class="si">}</span><span class="s2">/zephyr-sdk-</span><span class="si">${</span><span class="nv">ZEPHYR_SDK_VERSION</span><span class="si">}</span><span class="s2">_linux-x86_64.tar.gz&quot;</span>
tar<span class="w"> </span>xvf<span class="w"> </span><span class="s2">&quot;zephyr-sdk-</span><span class="si">${</span><span class="nv">ZEPHYR_SDK_VERSION</span><span class="si">}</span><span class="s2">_linux-x86_64.tar.gz&quot;</span>
mv<span class="w"> </span><span class="s2">&quot;zephyr-sdk-</span><span class="si">${</span><span class="nv">ZEPHYR_SDK_VERSION</span><span class="si">}</span><span class="s2">&quot;</span><span class="w"> </span>zephyr-sdk
rm<span class="w"> </span><span class="s2">&quot;zephyr-sdk-</span><span class="si">${</span><span class="nv">ZEPHYR_SDK_VERSION</span><span class="si">}</span><span class="s2">_linux-x86_64.tar.gz&quot;</span>
<span class="c1"># Install python dependencies</span>
python3<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>-r<span class="w"> </span><span class="s2">&quot;</span><span class="si">${</span><span class="nv">ZEPHYR_BASE</span><span class="si">}</span><span class="s2">/scripts/requirements.txt&quot;</span>
</pre></div>
</div>
</div></blockquote>
</div>
<div class="section" id="import-python-dependencies">
<h2>Import Python dependencies<a class="headerlink" href="#import-python-dependencies" title="Permalink to this headline"></a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">json</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pathlib</span>
<span class="kn">import</span> <span class="nn">tvm</span>
<span class="kn">from</span> <span class="nn">tvm.relay.backend</span> <span class="kn">import</span> <span class="n">Runtime</span>
<span class="kn">import</span> <span class="nn">tvm.micro.testing</span>
</pre></div>
</div>
<div class="section" id="defining-the-model">
<h3>Defining the model<a class="headerlink" href="#defining-the-model" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>To begin with, define a model in Relay to be executed on-device. Then create an IRModule from relay model and
fill parameters with random numbers.</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">data_shape</span></a> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">weight_shape</span></a> <span class="o">=</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="s2">&quot;data&quot;</span><span class="p">,</span> <a href="../../reference/api/python/ir.html#tvm.ir.TensorType" title="tvm.ir.TensorType" class="sphx-glr-backref-module-tvm-ir sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">TensorType</span></a><span class="p">(</span><a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">data_shape</span></a><span class="p">,</span> <span class="s2">&quot;float32&quot;</span><span class="p">))</span>
<span class="n">weight</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">var</span><span class="p">(</span><span class="s2">&quot;weight&quot;</span><span class="p">,</span> <a href="../../reference/api/python/ir.html#tvm.ir.TensorType" title="tvm.ir.TensorType" class="sphx-glr-backref-module-tvm-ir sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">TensorType</span></a><span class="p">(</span><a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">weight_shape</span></a><span class="p">,</span> <span class="s2">&quot;float32&quot;</span><span class="p">))</span>
<span class="n">y</span> <span class="o">=</span> <a href="../../reference/api/python/relay/nn.html#tvm.relay.nn.conv2d" title="tvm.relay.nn.conv2d" class="sphx-glr-backref-module-tvm-relay-nn sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">nn</span><span class="o">.</span><span class="n">conv2d</span></a><span class="p">(</span>
<span class="n">data</span><span class="p">,</span>
<span class="n">weight</span><span class="p">,</span>
<span class="n">padding</span><span class="o">=</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="n">kernel_size</span><span class="o">=</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span>
<span class="n">kernel_layout</span><span class="o">=</span><span class="s2">&quot;OIHW&quot;</span><span class="p">,</span>
<span class="n">out_dtype</span><span class="o">=</span><span class="s2">&quot;float32&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <a href="../../reference/api/python/ir.html#tvm.ir.BaseFunc" title="tvm.ir.BaseFunc" class="sphx-glr-backref-module-tvm-ir sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">Function</span></a><span class="p">([</span><span class="n">data</span><span class="p">,</span> <span class="n">weight</span><span class="p">],</span> <span class="n">y</span><span class="p">)</span>
<span class="n">relay_mod</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">IRModule</span><span class="o">.</span><span class="n">from_expr</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="n">relay_mod</span> <span class="o">=</span> <a href="../../reference/api/python/relay/transform.html#tvm.relay.transform.InferType" title="tvm.relay.transform.InferType" class="sphx-glr-backref-module-tvm-relay-transform sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">transform</span><span class="o">.</span><span class="n">InferType</span></a><span class="p">()(</span><span class="n">relay_mod</span><span class="p">)</span>
<span class="n">weight_sample</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span>
<a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">weight_shape</span></a><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">weight_shape</span></a><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">weight_shape</span></a><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <a href="https://docs.python.org/3/library/stdtypes.html#tuple" title="builtins.tuple" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">weight_shape</span></a><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">&quot;float32&quot;</span><span class="p">)</span>
<a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">params</span></a> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;weight&quot;</span><span class="p">:</span> <span class="n">weight_sample</span><span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="defining-the-target">
<h3>Defining the target<a class="headerlink" href="#defining-the-target" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Now we define the TVM target that describes the execution environment. This looks very similar
to target definitions from other microTVM tutorials. Alongside this we pick the C Runtime to code
generate our model against.</p>
<p>When running on physical hardware, choose a target and a board that
describe the hardware. There are multiple hardware targets that could be selected from
PLATFORM list in this tutorial. You can chose the platform by passing –platform argument when running
this tutorial.</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">RUNTIME</span> <span class="o">=</span> <span class="n">Runtime</span><span class="p">(</span><span class="s2">&quot;crt&quot;</span><span class="p">,</span> <span class="p">{</span><span class="s2">&quot;system-lib&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">})</span>
<a href="../../reference/api/python/target.html#tvm.target.Target" title="tvm.target.Target" class="sphx-glr-backref-module-tvm-target sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">TARGET</span></a> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">get_target</span><span class="p">(</span><span class="s2">&quot;crt&quot;</span><span class="p">)</span>
<span class="c1"># Compiling for physical hardware</span>
<span class="c1"># --------------------------------------------------------------------------</span>
<span class="c1"># When running on physical hardware, choose a TARGET and a BOARD that describe the hardware. The</span>
<span class="c1"># STM32L4R5ZI Nucleo target and board is chosen in the example below.</span>
<span class="k">if</span> <a href="https://docs.python.org/3/library/functions.html#bool" title="builtins.bool" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">use_physical_hw</span></a><span class="p">:</span>
<span class="n">BOARD</span> <span class="o">=</span> <a href="https://docs.python.org/3/library/os.html#os.getenv" title="os.getenv" class="sphx-glr-backref-module-os sphx-glr-backref-type-py-function"><span class="n">os</span><span class="o">.</span><span class="n">getenv</span></a><span class="p">(</span><span class="s2">&quot;TVM_MICRO_BOARD&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="s2">&quot;nucleo_l4r5zi&quot;</span><span class="p">)</span>
<span class="n">SERIAL</span> <span class="o">=</span> <a href="https://docs.python.org/3/library/os.html#os.getenv" title="os.getenv" class="sphx-glr-backref-module-os sphx-glr-backref-type-py-function"><span class="n">os</span><span class="o">.</span><span class="n">getenv</span></a><span class="p">(</span><span class="s2">&quot;TVM_MICRO_SERIAL&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<a href="../../reference/api/python/target.html#tvm.target.Target" title="tvm.target.Target" class="sphx-glr-backref-module-tvm-target sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">TARGET</span></a> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">get_target</span><span class="p">(</span><span class="s2">&quot;zephyr&quot;</span><span class="p">,</span> <span class="n">BOARD</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="extracting-tuning-tasks">
<h3>Extracting tuning tasks<a class="headerlink" href="#extracting-tuning-tasks" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Not all operators in the Relay program printed above can be tuned. Some are so trivial that only
a single implementation is defined; others don’t make sense as tuning tasks. Using
<cite>extract_from_program</cite>, you can produce a list of tunable tasks.</p>
<p>Because task extraction involves running the compiler, we first configure the compiler’s
transformation passes; we’ll apply the same configuration later on during autotuning.</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">pass_context</span> <span class="o">=</span> <a href="../../reference/api/python/ir.html#tvm.transform.PassContext" title="tvm.transform.PassContext" class="sphx-glr-backref-module-tvm-transform sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tvm</span><span class="o">.</span><span class="n">transform</span><span class="o">.</span><span class="n">PassContext</span></a><span class="p">(</span><span class="n">opt_level</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">config</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;tir.disable_vectorize&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">})</span>
<span class="k">with</span> <span class="n">pass_context</span><span class="p">:</span>
<a href="https://docs.python.org/3/library/stdtypes.html#list" title="builtins.list" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tasks</span></a> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">task</span><span class="o">.</span><span class="n">extract_from_program</span><span class="p">(</span><span class="n">relay_mod</span><span class="p">[</span><span class="s2">&quot;main&quot;</span><span class="p">],</span> <span class="p">{},</span> <a href="../../reference/api/python/target.html#tvm.target.Target" title="tvm.target.Target" class="sphx-glr-backref-module-tvm-target sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">TARGET</span></a><span class="p">)</span>
<span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><a href="https://docs.python.org/3/library/stdtypes.html#list" title="builtins.list" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tasks</span></a><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span>
</pre></div>
</div>
</div>
<div class="section" id="configuring-microtvm">
<h3>Configuring microTVM<a class="headerlink" href="#configuring-microtvm" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Before autotuning, we need to define a module loader and then pass that to
a <cite>tvm.autotvm.LocalBuilder</cite>. Then we create a <cite>tvm.autotvm.LocalRunner</cite> and use
both builder and runner to generates multiple measurements for auto tunner.</p>
<p>In this tutorial, we have the option to use x86 host as an example or use different targets
from Zephyr RTOS. If you choose pass <cite>–platform=host</cite> to this tutorial it will uses x86. You can
choose other options by choosing from <cite>PLATFORM</cite> list.</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">module_loader</span></a> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">AutoTvmModuleLoader</span></a><span class="p">(</span>
<span class="n">template_project_dir</span><span class="o">=</span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path" title="pathlib.Path" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class"><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span></a><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.get_microtvm_template_projects" title="tvm.micro.get_microtvm_template_projects" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">get_microtvm_template_projects</span></a><span class="p">(</span><span class="s2">&quot;crt&quot;</span><span class="p">)),</span>
<span class="n">project_options</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;verbose&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
<span class="p">)</span>
<span class="n">builder</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalBuilder</span><span class="p">(</span>
<span class="n">n_parallel</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">build_kwargs</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;build_option&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;tir.disable_vectorize&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}},</span>
<span class="n">do_fork</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">build_func</span><span class="o">=</span><a href="../../reference/api/python/micro.html#tvm.micro.autotvm_build_func" title="tvm.micro.autotvm_build_func" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">autotvm_build_func</span></a><span class="p">,</span>
<span class="n">runtime</span><span class="o">=</span><span class="n">RUNTIME</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">runner</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalRunner</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">module_loader</span></a><span class="o">=</span><a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">module_loader</span></a><span class="p">)</span>
<a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">measure_option</span></a> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">measure_option</span></a><span class="p">(</span><span class="n">builder</span><span class="o">=</span><span class="n">builder</span><span class="p">,</span> <span class="n">runner</span><span class="o">=</span><span class="n">runner</span><span class="p">)</span>
<span class="c1"># Compiling for physical hardware</span>
<span class="k">if</span> <a href="https://docs.python.org/3/library/functions.html#bool" title="builtins.bool" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">use_physical_hw</span></a><span class="p">:</span>
<a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">module_loader</span></a> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">AutoTvmModuleLoader</span></a><span class="p">(</span>
<span class="n">template_project_dir</span><span class="o">=</span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path" title="pathlib.Path" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class"><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span></a><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.get_microtvm_template_projects" title="tvm.micro.get_microtvm_template_projects" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">get_microtvm_template_projects</span></a><span class="p">(</span><span class="s2">&quot;zephyr&quot;</span><span class="p">)),</span>
<span class="n">project_options</span><span class="o">=</span><span class="p">{</span>
<span class="s2">&quot;board&quot;</span><span class="p">:</span> <span class="n">BOARD</span><span class="p">,</span>
<span class="s2">&quot;verbose&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
<span class="s2">&quot;project_type&quot;</span><span class="p">:</span> <span class="s2">&quot;host_driven&quot;</span><span class="p">,</span>
<span class="s2">&quot;serial_number&quot;</span><span class="p">:</span> <span class="n">SERIAL</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">)</span>
<span class="n">builder</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalBuilder</span><span class="p">(</span>
<span class="n">n_parallel</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="n">build_kwargs</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;build_option&quot;</span><span class="p">:</span> <span class="p">{</span><span class="s2">&quot;tir.disable_vectorize&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">}},</span>
<span class="n">do_fork</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
<span class="n">build_func</span><span class="o">=</span><a href="../../reference/api/python/micro.html#tvm.micro.autotvm_build_func" title="tvm.micro.autotvm_build_func" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">autotvm_build_func</span></a><span class="p">,</span>
<span class="n">runtime</span><span class="o">=</span><span class="n">RUNTIME</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">runner</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">LocalRunner</span><span class="p">(</span><span class="n">number</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">module_loader</span></a><span class="o">=</span><a href="../../reference/api/python/micro.html#tvm.micro.AutoTvmModuleLoader" title="tvm.micro.AutoTvmModuleLoader" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">module_loader</span></a><span class="p">)</span>
<a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">measure_option</span></a> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">measure_option</span></a><span class="p">(</span><span class="n">builder</span><span class="o">=</span><span class="n">builder</span><span class="p">,</span> <span class="n">runner</span><span class="o">=</span><span class="n">runner</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="run-autotuning">
<h3>Run Autotuning<a class="headerlink" href="#run-autotuning" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Now we can run autotuning separately on each extracted task on microTVM device.</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath" title="pathlib.PosixPath" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">autotune_log_file</span></a> <span class="o">=</span> <a href="https://docs.python.org/3/library/pathlib.html#pathlib.Path" title="pathlib.Path" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class"><span class="n">pathlib</span><span class="o">.</span><span class="n">Path</span></a><span class="p">(</span><span class="s2">&quot;microtvm_autotune.log.txt&quot;</span><span class="p">)</span>
<span class="k">if</span> <a href="https://docs.python.org/3/library/os.path.html#os.path.exists" title="os.path.exists" class="sphx-glr-backref-module-os-path sphx-glr-backref-type-py-function"><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span></a><span class="p">(</span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath" title="pathlib.PosixPath" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">autotune_log_file</span></a><span class="p">):</span>
<a href="https://docs.python.org/3/library/os.html#os.remove" title="os.remove" class="sphx-glr-backref-module-os sphx-glr-backref-type-py-function"><span class="n">os</span><span class="o">.</span><span class="n">remove</span></a><span class="p">(</span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath" title="pathlib.PosixPath" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">autotune_log_file</span></a><span class="p">)</span>
<a href="https://docs.python.org/3/library/functions.html#int" title="builtins.int" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">num_trials</span></a> <span class="o">=</span> <span class="mi">10</span>
<span class="k">for</span> <span class="n">task</span> <span class="ow">in</span> <a href="https://docs.python.org/3/library/stdtypes.html#list" title="builtins.list" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tasks</span></a><span class="p">:</span>
<a href="../../reference/api/python/autotvm.html#tvm.autotvm.tuner.GATuner" title="tvm.autotvm.tuner.GATuner" class="sphx-glr-backref-module-tvm-autotvm-tuner sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">tuner</span></a> <span class="o">=</span> <a href="../../reference/api/python/autotvm.html#tvm.autotvm.tuner.GATuner" title="tvm.autotvm.tuner.GATuner" class="sphx-glr-backref-module-tvm-autotvm-tuner sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">tuner</span><span class="o">.</span><span class="n">GATuner</span></a><span class="p">(</span><span class="n">task</span><span class="p">)</span>
<a href="../../reference/api/python/autotvm.html#tvm.autotvm.tuner.GATuner.tune" title="tvm.autotvm.tuner.GATuner.tune" class="sphx-glr-backref-module-tvm-autotvm-tuner sphx-glr-backref-type-py-method"><span class="n">tuner</span><span class="o">.</span><span class="n">tune</span></a><span class="p">(</span>
<span class="n">n_trial</span><span class="o">=</span><a href="https://docs.python.org/3/library/functions.html#int" title="builtins.int" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">num_trials</span></a><span class="p">,</span>
<a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">measure_option</span></a><span class="o">=</span><a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">measure_option</span></a><span class="p">,</span>
<span class="n">callbacks</span><span class="o">=</span><span class="p">[</span>
<span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">callback</span><span class="o">.</span><span class="n">log_to_file</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath" title="pathlib.PosixPath" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">autotune_log_file</span></a><span class="p">)),</span>
<span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">callback</span><span class="o">.</span><span class="n">progress_bar</span><span class="p">(</span><a href="https://docs.python.org/3/library/functions.html#int" title="builtins.int" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">num_trials</span></a><span class="p">,</span> <span class="n">si_prefix</span><span class="o">=</span><span class="s2">&quot;M&quot;</span><span class="p">),</span>
<span class="p">],</span>
<span class="n">si_prefix</span><span class="o">=</span><span class="s2">&quot;M&quot;</span><span class="p">,</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="timing-the-untuned-program">
<h3>Timing the untuned program<a class="headerlink" href="#timing-the-untuned-program" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>For comparison, let’s compile and run the graph without imposing any autotuning schedules. TVM
will select a randomly-tuned implementation for each operator, which should not perform as well as
the tuned operator.</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">pass_context</span><span class="p">:</span>
<span class="n">lowered</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">relay_mod</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><a href="../../reference/api/python/target.html#tvm.target.Target" title="tvm.target.Target" class="sphx-glr-backref-module-tvm-target sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">TARGET</span></a><span class="p">,</span> <span class="n">runtime</span><span class="o">=</span><span class="n">RUNTIME</span><span class="p">,</span> <a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">params</span></a><span class="o">=</span><a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">params</span></a><span class="p">)</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">=</span> <a href="../../reference/api/python/contrib.html#tvm.contrib.utils.tempdir" title="tvm.contrib.utils.tempdir" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">contrib</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">tempdir</span></a><span class="p">()</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.generate_project" title="tvm.micro.generate_project" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">generate_project</span></a><span class="p">(</span>
<span class="nb">str</span><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.get_microtvm_template_projects" title="tvm.micro.get_microtvm_template_projects" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">get_microtvm_template_projects</span></a><span class="p">(</span><span class="s2">&quot;crt&quot;</span><span class="p">)),</span>
<span class="n">lowered</span><span class="p">,</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">/</span> <span class="s2">&quot;project&quot;</span><span class="p">,</span>
<span class="p">{</span><span class="s2">&quot;verbose&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
<span class="p">)</span>
<span class="c1"># Compiling for physical hardware</span>
<span class="k">if</span> <a href="https://docs.python.org/3/library/functions.html#bool" title="builtins.bool" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">use_physical_hw</span></a><span class="p">:</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">=</span> <a href="../../reference/api/python/contrib.html#tvm.contrib.utils.tempdir" title="tvm.contrib.utils.tempdir" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">contrib</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">tempdir</span></a><span class="p">()</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.generate_project" title="tvm.micro.generate_project" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">generate_project</span></a><span class="p">(</span>
<span class="nb">str</span><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.get_microtvm_template_projects" title="tvm.micro.get_microtvm_template_projects" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">get_microtvm_template_projects</span></a><span class="p">(</span><span class="s2">&quot;zephyr&quot;</span><span class="p">)),</span>
<span class="n">lowered</span><span class="p">,</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">/</span> <span class="s2">&quot;project&quot;</span><span class="p">,</span>
<span class="p">{</span>
<span class="s2">&quot;board&quot;</span><span class="p">:</span> <span class="n">BOARD</span><span class="p">,</span>
<span class="s2">&quot;verbose&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
<span class="s2">&quot;project_type&quot;</span><span class="p">:</span> <span class="s2">&quot;host_driven&quot;</span><span class="p">,</span>
<span class="s2">&quot;serial_number&quot;</span><span class="p">:</span> <span class="n">SERIAL</span><span class="p">,</span>
<span class="s2">&quot;config_main_stack_size&quot;</span><span class="p">:</span> <span class="mi">4096</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">)</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a><span class="o">.</span><span class="n">build</span><span class="p">()</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a><span class="o">.</span><span class="n">flash</span><span class="p">()</span>
<span class="k">with</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">Session</span></a><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a><span class="o">.</span><span class="n">transport</span><span class="p">())</span> <span class="k">as</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">session</span></a><span class="p">:</span>
<span class="n">debug_module</span> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.create_local_debug_executor" title="tvm.micro.create_local_debug_executor" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">create_local_debug_executor</span></a><span class="p">(</span>
<span class="n">lowered</span><span class="o">.</span><span class="n">get_graph_json</span><span class="p">(),</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">session</span></a><span class="o">.</span><span class="n">get_system_lib</span><span class="p">(),</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">session</span></a><span class="o">.</span><span class="n">device</span>
<span class="p">)</span>
<span class="n">debug_module</span><span class="o">.</span><span class="n">set_input</span><span class="p">(</span><span class="o">**</span><span class="n">lowered</span><span class="o">.</span><span class="n">get_params</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;########## Build without Autotuning ##########&quot;</span><span class="p">)</span>
<span class="n">debug_module</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">del</span> <span class="n">debug_module</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>########## Build without Autotuning ##########
Node Name Ops Time(us) Time(%) Shape Inputs Outputs Measurements(us)
--------- --- -------- ------- ----- ------ ------- ----------------
tvmgen_default_fused_nn_contrib_conv2d_NCHWc tvmgen_default_fused_nn_contrib_conv2d_NCHWc 302.9 98.717 (1, 2, 10, 10, 3) 2 1 [302.9]
tvmgen_default_fused_layout_transform_1 tvmgen_default_fused_layout_transform_1 2.983 0.972 (1, 6, 10, 10) 1 1 [2.983]
tvmgen_default_fused_layout_transform tvmgen_default_fused_layout_transform 0.954 0.311 (1, 1, 10, 10, 3) 1 1 [0.954]
Total_time - 306.836 - - - - -
</pre></div>
</div>
</div>
<div class="section" id="timing-the-tuned-program">
<h3>Timing the tuned program<a class="headerlink" href="#timing-the-tuned-program" title="Permalink to this headline"></a></h3>
<blockquote>
<div><p>Once autotuning completes, you can time execution of the entire program using the Debug Runtime:</p>
</div></blockquote>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <a href="../../reference/api/python/autotvm.html#tvm.autotvm.apply_history_best" title="tvm.autotvm.apply_history_best" class="sphx-glr-backref-module-tvm-autotvm sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">autotvm</span><span class="o">.</span><span class="n">apply_history_best</span></a><span class="p">(</span><span class="nb">str</span><span class="p">(</span><a href="https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath" title="pathlib.PosixPath" class="sphx-glr-backref-module-pathlib sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">autotune_log_file</span></a><span class="p">)):</span>
<span class="k">with</span> <span class="n">pass_context</span><span class="p">:</span>
<span class="n">lowered_tuned</span> <span class="o">=</span> <span class="n">tvm</span><span class="o">.</span><span class="n">relay</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">relay_mod</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><a href="../../reference/api/python/target.html#tvm.target.Target" title="tvm.target.Target" class="sphx-glr-backref-module-tvm-target sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">TARGET</span></a><span class="p">,</span> <span class="n">runtime</span><span class="o">=</span><span class="n">RUNTIME</span><span class="p">,</span> <a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">params</span></a><span class="o">=</span><a href="https://docs.python.org/3/library/stdtypes.html#dict" title="builtins.dict" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">params</span></a><span class="p">)</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">=</span> <a href="../../reference/api/python/contrib.html#tvm.contrib.utils.tempdir" title="tvm.contrib.utils.tempdir" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">contrib</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">tempdir</span></a><span class="p">()</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.generate_project" title="tvm.micro.generate_project" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">generate_project</span></a><span class="p">(</span>
<span class="nb">str</span><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.get_microtvm_template_projects" title="tvm.micro.get_microtvm_template_projects" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">get_microtvm_template_projects</span></a><span class="p">(</span><span class="s2">&quot;crt&quot;</span><span class="p">)),</span>
<span class="n">lowered_tuned</span><span class="p">,</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">/</span> <span class="s2">&quot;project&quot;</span><span class="p">,</span>
<span class="p">{</span><span class="s2">&quot;verbose&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">},</span>
<span class="p">)</span>
<span class="c1"># Compiling for physical hardware</span>
<span class="k">if</span> <a href="https://docs.python.org/3/library/functions.html#bool" title="builtins.bool" class="sphx-glr-backref-module-builtins sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">use_physical_hw</span></a><span class="p">:</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">=</span> <a href="../../reference/api/python/contrib.html#tvm.contrib.utils.tempdir" title="tvm.contrib.utils.tempdir" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">contrib</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">tempdir</span></a><span class="p">()</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.generate_project" title="tvm.micro.generate_project" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">generate_project</span></a><span class="p">(</span>
<span class="nb">str</span><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.get_microtvm_template_projects" title="tvm.micro.get_microtvm_template_projects" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">get_microtvm_template_projects</span></a><span class="p">(</span><span class="s2">&quot;zephyr&quot;</span><span class="p">)),</span>
<span class="n">lowered_tuned</span><span class="p">,</span>
<a href="../../reference/api/python/contrib.html#tvm.contrib.utils.TempDirectory" title="tvm.contrib.utils.TempDirectory" class="sphx-glr-backref-module-tvm-contrib-utils sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">temp_dir</span></a> <span class="o">/</span> <span class="s2">&quot;project&quot;</span><span class="p">,</span>
<span class="p">{</span>
<span class="s2">&quot;board&quot;</span><span class="p">:</span> <span class="n">BOARD</span><span class="p">,</span>
<span class="s2">&quot;verbose&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
<span class="s2">&quot;project_type&quot;</span><span class="p">:</span> <span class="s2">&quot;host_driven&quot;</span><span class="p">,</span>
<span class="s2">&quot;serial_number&quot;</span><span class="p">:</span> <span class="n">SERIAL</span><span class="p">,</span>
<span class="s2">&quot;config_main_stack_size&quot;</span><span class="p">:</span> <span class="mi">4096</span><span class="p">,</span>
<span class="p">},</span>
<span class="p">)</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a><span class="o">.</span><span class="n">build</span><span class="p">()</span>
<a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a><span class="o">.</span><span class="n">flash</span><span class="p">()</span>
<span class="k">with</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">Session</span></a><span class="p">(</span><a href="../../reference/api/python/micro.html#tvm.micro.GeneratedProject" title="tvm.micro.GeneratedProject" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">project</span></a><span class="o">.</span><span class="n">transport</span><span class="p">())</span> <span class="k">as</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">session</span></a><span class="p">:</span>
<span class="n">debug_module</span> <span class="o">=</span> <a href="../../reference/api/python/micro.html#tvm.micro.create_local_debug_executor" title="tvm.micro.create_local_debug_executor" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-function"><span class="n">tvm</span><span class="o">.</span><span class="n">micro</span><span class="o">.</span><span class="n">create_local_debug_executor</span></a><span class="p">(</span>
<span class="n">lowered_tuned</span><span class="o">.</span><span class="n">get_graph_json</span><span class="p">(),</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">session</span></a><span class="o">.</span><span class="n">get_system_lib</span><span class="p">(),</span> <a href="../../reference/api/python/micro.html#tvm.micro.Session" title="tvm.micro.Session" class="sphx-glr-backref-module-tvm-micro sphx-glr-backref-type-py-class sphx-glr-backref-instance"><span class="n">session</span></a><span class="o">.</span><span class="n">device</span>
<span class="p">)</span>
<span class="n">debug_module</span><span class="o">.</span><span class="n">set_input</span><span class="p">(</span><span class="o">**</span><span class="n">lowered_tuned</span><span class="o">.</span><span class="n">get_params</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;########## Build with Autotuning ##########&quot;</span><span class="p">)</span>
<span class="n">debug_module</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
<span class="k">del</span> <span class="n">debug_module</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>########## Build with Autotuning ##########
Node Name Ops Time(us) Time(%) Shape Inputs Outputs Measurements(us)
--------- --- -------- ------- ----- ------ ------- ----------------
tvmgen_default_fused_nn_contrib_conv2d_NCHWc tvmgen_default_fused_nn_contrib_conv2d_NCHWc 100.5 97.31 (1, 6, 10, 10, 1) 2 1 [100.5]
tvmgen_default_fused_layout_transform_1 tvmgen_default_fused_layout_transform_1 1.795 1.738 (1, 6, 10, 10) 1 1 [1.795]
tvmgen_default_fused_layout_transform tvmgen_default_fused_layout_transform 0.984 0.953 (1, 1, 10, 10, 3) 1 1 [0.984]
Total_time - 103.278 - - - - -
</pre></div>
</div>
<p class="sphx-glr-timing"><strong>Total running time of the script:</strong> ( 1 minutes 31.783 seconds)</p>
<div class="sphx-glr-footer sphx-glr-footer-example docutils container" id="sphx-glr-download-how-to-work-with-microtvm-micro-autotune-py">
<div class="sphx-glr-download sphx-glr-download-python docutils container">
<p><a class="reference download internal" download="" href="../../_downloads/9ccca8fd489a1486ac71b55a55c320c5/micro_autotune.py"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Python</span> <span class="pre">source</span> <span class="pre">code:</span> <span class="pre">micro_autotune.py</span></code></a></p>
</div>
<div class="sphx-glr-download sphx-glr-download-jupyter docutils container">
<p><a class="reference download internal" download="" href="../../_downloads/f83ba3df2d52f9b54cf141114359481a/micro_autotune.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Download</span> <span class="pre">Jupyter</span> <span class="pre">notebook:</span> <span class="pre">micro_autotune.ipynb</span></code></a></p>
</div>
</div>
<p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</a></p>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="micro_ethosu.html" class="btn btn-neutral float-right" title="7. Running TVM on bare metal Arm(R) Cortex(R)-M55 CPU and Ethos(TM)-U55 NPU with CMSIS-NN" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="micro_train.html" class="btn btn-neutral float-left" title="5. Training Vision Models for microTVM on Arduino" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<div id="button" class="backtop"><img src="../../_static/img/right.svg" alt="backtop"/> </div>
<section class="footerSec">
<div class="footerHeader">
<div class="d-flex align-md-items-center justify-content-between flex-column flex-md-row">
<div class="copywrite d-flex align-items-center">
<h5 id="copy-right-info">© 2023 Apache Software Foundation | All rights reserved</h5>
</div>
</div>
</div>
<div>
<div class="footernote">Copyright © 2023 The Apache Software Foundation. Apache TVM, Apache, the Apache feather, and the Apache TVM project logo are either trademarks or registered trademarks of the Apache Software Foundation.</div>
</div>
</section>
</footer>
</div>
</div>
</section>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<!-- Theme Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-75982049-2', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>