|
| 1 | +.. Licensed to the Apache Software Foundation (ASF) under one |
| 2 | + or more contributor license agreements. See the NOTICE file |
| 3 | + distributed with this work for additional information |
| 4 | + regarding copyright ownership. The ASF licenses this file |
| 5 | + to you under the Apache License, Version 2.0 (the |
| 6 | + "License"); you may not use this file except in compliance |
| 7 | + with the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | +.. http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +.. Unless required by applicable law or agreed to in writing, |
| 12 | + software distributed under the License is distributed on an |
| 13 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + KIND, either express or implied. See the License for the |
| 15 | + specific language governing permissions and limitations |
| 16 | + under the License. |
| 17 | +
|
| 18 | +.. _microtvm_project_api: |
| 19 | + |
| 20 | +microTVM Project API |
| 21 | +==================== |
| 22 | + |
| 23 | +About microTVM Project API |
| 24 | +-------------------------- |
| 25 | + |
| 26 | +The microTVM Project API allows TVM to automatically run models on |
| 27 | +unconventional or embedded platforms. It allows platforms to define a standard |
| 28 | +function to integrate TVM compiler output with boilerplate platform-specific |
| 29 | +code, producing a runnable **Project**. Project API then further defines |
| 30 | +functions to build that project, program compatible devices accessible from the |
| 31 | +TVM machine, and communicate with the running code so that TVM can perform |
| 32 | +host-driven inference and autotuning. |
| 33 | + |
| 34 | +There are many cases where it might be desirable simply to invoke microTVM as a |
| 35 | +tool from your platform's build process. Indeed, for the average firmware |
| 36 | +developer, this is likely to be all they need. However, there are a couple of |
| 37 | +use cases when you may want to teach microTVM how to build firmware using your |
| 38 | +platform's build tool: |
| 39 | + |
| 40 | +1. To enable AutoTVM and AutoScheduling on your platform. Defining a Project |
| 41 | + API implementation allows TVM to tune models for peak performance on your |
| 42 | + platform. |
| 43 | +2. To enable engineers without firmware expertise to experiment with models on |
| 44 | + your platform. Defining a Project API implementation allows these engineers |
| 45 | + to leverage the standard TVM Python workflows to perform host-driven |
| 46 | + inference on your platform. |
| 47 | +3. Integration Testing. Defining a Project API implementation allows you to |
| 48 | + create Continuous Integration Tests which verify model correctness and |
| 49 | + performance on your platform. |
| 50 | + |
| 51 | +API Definition |
| 52 | +-------------- |
| 53 | + |
| 54 | +The full API is the ``abstractmethod`` defined on ``ProjectAPIHandler`` in |
| 55 | +`python/tvm/micro/project_api/server.py <https://github.com/apache/tvm/blob/main/python/tvm/micro/project_api/server.py>`_. |
| 56 | +Rather than duplicate the documentation here, we simply refer you to that class. |
| 57 | + |
| 58 | +How TVM uses Project API |
| 59 | +------------------------ |
| 60 | + |
| 61 | +This section explains how the Project API should be used with TVM. Project API |
| 62 | +is defined around the *Project* as the buildable unit of firmware. TVM expects |
| 63 | +to be provided initially with a directory containing a *Template Project*, which |
| 64 | +together with a :ref:`Model Library Format <model_library_format>` file can be |
| 65 | +built into a runnable project. |
| 66 | + |
| 67 | +Inside the Template Directory is (typically) a Python script implementing the |
| 68 | +API server. TVM launches this script in a subprocess and sends commands to the |
| 69 | +server to perform each of the actions outlined above. |
| 70 | + |
| 71 | +The typical usage flow is as follows: |
| 72 | + |
| 73 | +1. Launch Project API server in Template Project. |
| 74 | +2. Verify the API server is version-compatible with TVM, plus read properties |
| 75 | + of the implementation, by sending ``server_info_query`` command. |
| 76 | +3. Generate a new project by sending command ``generate_project`` to create a |
| 77 | + new project. The arguments to this command is a Model Library Format and a |
| 78 | + non-existent directory which should be populated with the generated |
| 79 | + project. The Template Project API server should copy itself into the |
| 80 | + newly-generated project. |
| 81 | +4. Terminate the Template Project API server. |
| 82 | +5. Launch Project API server in Generated Project. |
| 83 | +6. Verify the API server is version-compatible with TVM, plus read properties |
| 84 | + of the implementation, by sending ``server_info_query`` command. |
| 85 | +7. Build and flash the projec by sending commands ``build`` and ``flash`` to the |
| 86 | + API server. |
| 87 | +8. Communicate with the target. Send command ``open_transport`` followed by |
| 88 | + commands ``write_transport`` and ``read_transport`` to write and read from |
| 89 | + e.g. a serial port attached to the target. Upon completion, |
| 90 | + ``close_transport`` is sent. |
| 91 | +9. Terminate Project API server. |
| 92 | + |
| 93 | +Disk Layout of the Project |
| 94 | +-------------------------- |
| 95 | + |
| 96 | +In the root directory of a project (template or generated), one of the following |
| 97 | +two files must exist: |
| 98 | + |
| 99 | +- ``microtvm_api_server.py`` - the suggested approach. Place a |
| 100 | + python3-compatible Python script in the root directory. TVM will execute this |
| 101 | + script in its own process using the same interpreter used to execute TVM. |
| 102 | +- ``microtvm_api_server.sh`` (on Windows, ``microtvm_api_server.bat``) - |
| 103 | + alternate approach. When a different Python interpreter is necessary, or |
| 104 | + when you want to implement the server in a different language, create this |
| 105 | + executable file. TVM will launch this file in a separate process. |
| 106 | + |
| 107 | +Aside from these two files, no other restrictions are made on the layout. |
| 108 | + |
| 109 | +Communication between TVM and Project API Server |
| 110 | +------------------------------------------------ |
| 111 | + |
| 112 | +TVM communicates with the Project API server using `JSON-RPC 2.0 |
| 113 | +<https://www.jsonrpc.org/specification>`_. TVM always launches API servers using |
| 114 | +the following command-line: |
| 115 | + |
| 116 | +``microtvm_api_server.py --read-fd <n> --write-fd <n>`` |
| 117 | + |
| 118 | +Commands are sent from TVM to the server over the file descriptor given by |
| 119 | +``--read-fd`` and replies are received by TVM from the server over the file |
| 120 | +descriptor given by ``--write-fd``. |
| 121 | + |
| 122 | +Helpers for Implementing the API server in Python |
| 123 | +------------------------------------------------- |
| 124 | + |
| 125 | +TVM provides helper utilities that make it easy to implement the server in Python. |
| 126 | +To implement the server in Python, create ``microtvm_api_server.py`` and add |
| 127 | +``from tvm.micro.project_api import server`` (or, copy this file into your template |
| 128 | +project--there are no dependencies--and import it there instead). Next, subclass |
| 129 | +``ProjectAPIHander``:: |
| 130 | + |
| 131 | + class Handler(server.ProjectAPIHandler): |
| 132 | + def server_info_query(self, tvm_version): |
| 133 | + # Implement server_info_query |
| 134 | + |
| 135 | + def generate_project(self, model_library_format_path, standalone_crt_dir, project_dir, options): |
| 136 | + # Implement generate_project |
| 137 | + |
| 138 | + # ... |
| 139 | + |
| 140 | +Finally, invoke the helper ``main()``:: |
| 141 | + |
| 142 | + if __name__ == "__main__": |
| 143 | + server.main(Handler()) |
| 144 | + |
| 145 | +Using Project API from ``tvmc`` |
| 146 | +------------------------------- |
| 147 | + |
| 148 | +Each major Project API command is available through the ``tvmc micro`` |
| 149 | +sub-command to make debugging interactions simple. Invoke ``tvmc micro --help`` |
| 150 | +for more information. |
0 commit comments