readthedocs: https://utensor-cgen.readthedocs.io/en/latest/
.. readme_begin
.. _readme:
.. _install:
setup.py.. code:: console
$ python setup.py install
.. code:: console
$ pip install utensor_cgen
.. _install_dev:
.. code:: console
$ pip install -e .[dev]
.. code:: console
# install `utensor_cgen` (develop mode)
$ PIPENV_VENV_IN_PROJECT=1 pipenv install -d
# spawn a subshell and activate virtualenv
$ pipenv shell
# get help message of `utensor-cli`
$ utensor-cli -h
Troubleshooting with pipenv_
- If you have troubles with installation using pipenv_, try
.. code:: console
$ PIPENV_VENV_IN_PROJECT=1 pipenv install -d --skip-lock
- there is known issue of pip_ and pipenv_, plz refer to this
`issue <https://github.com/pypa/pipenv/issues/2924>`_ for detail
- short answer: downgrade to ``pip==18.0`` may help :)
- Tensorflow_ requires ``setuptools<=39.1.0`` (the latest is ``40.4.3``
by the time this README is writen)
- plz downgrade to ``setuptools==39.1.0``
- my recommendation is to use ``virtualenv``
Overall Architecture
====================
::
============ +-----------------+ ===================
|| model file || --> | frontend Parser | --> || uTensorGraph (IR) ||
============ +-----------------+ ===================
|
+-------------------------------+ |
| graph transformer | |
| (legalization & optimization) | <------/
+-------------------------------+
|
v
===========================
|| uTensorGraph ||
|| (legalized and optimized) ||
===========================
|
+--------------------------+ |
| backend (code generator) | <----/
+--------------------------+
|
`---> (target files, ex: model.cpp, model.hpp, weights.idx)
Basic Usage
===========
Model File Inspection
---------------------
.. code-block:: console
$ utensor-cli show <model.pb>
Show all nodes and detailed information of given pb file or
a :class:`.uTensorGraph` pickle file
Run ``utensor-cli show --help`` for detailed information.
Convert Model File to C/C++ Code
--------------------------------
.. code-block:: console
$ utensor-cli convert <model.pb> \
--output-nodes=<node_name>[,<node_name>,...]
Convert given pb file into cpp/hpp files.
Note that ``--output-nodes`` is required options. It's the names of
nodes you want to output, seperated by comma for multiple values.
In graph theory terminology, they are ``leaf`` nodes of your graph.
example
~~~~~~~
.. code-block:: console
$ utensor-cli convert simple_model.pb --output-nodes=pred,logits
Run ``utensor-cli convert --help`` for detailed information.
:mod:`utensor_cgen` as Library
==============================
.. subgraph-match-begine
Subgraph Isomorphic Matcher
---------------------------
With :class:`.uTensorGraphMatcher`, performing isomorphic subgraph matching
along with replacing or manipulating the matched subgraph(s) takes just a
few line of code:
.. code-block:: python
from utensor_cgen.matcher import uTensorGraphMatcher
# `pattrn_ugraph` is the pattern to match with
pattrn_ugraph = ...
matcher = uTensorGraphMatcher(pattrn_ugraph)
# a larget graph to perform subgraph match
subject_ugraph = ...
# matches is a list of `uTensorGraphMatch` objects
matches = matcher.match_all(subject_ugraph)
if matches:
# do stuff with the matches
Use Case: Node Fusion
~~~~~~~~~~~~~~~~~~~~~
Note: we'll use **operation**/**node**/**layer** interchangeably in the
documentation
- It's commonly seen pattern in convolution neural network (``CNN``),
``conv -> relu -> pooling``. That is, a 2D convolution followed by a
relu layer and then a pooling down sampling layer.
- With our :class:`.uTensorGraphMatcher`, you can locate such pattern in your
``CNN`` model and fuse/replace matched nodes into one optimized
:class:`.QuantizedFusedConv2DMaxpool` node.
- Left: original graph
- Middle: matched convolution layer
- Right: replace the matched layer with specialized
``QuantizedFusedConv2DMaxpool`` node
\ |conv-pool-fuse|
Use Case: Dropout Layer Removal
Though dropout is an effective technique to improve training
performance of your model, it's not necessary during inference
phrase.
In the mainstream frameworks such as Tensorflow_ or PyTorch_,
an dropout layer is typically implemented with other elementary
operations/nodes. As a result, finding and removing those nodes for
inference optimization (both in model size and prediciton time) is
not trivial and error prone.
With our :class:.uTensorGraphMatcher, you can find and remove the dropout
nodes as illustrated in the following picture.
\ |cnn-dropout|
.. subgraph-match-end
We use mainly Tensorflow_ for declaring the pattern graph for matcher now.
High-level graph builder is on its way, see Future Works <#future-works>_ for detail.
Deep Multilayer Perceptron <https://github.com/uTensor/utensor_cgen/tree/develop/tests/deep_mlp>_End-to-End Convolution NN <https://github.com/uTensor/simple_cnn_tutorial>_tensorflow.Graphofficial doc <https://www.tensorflow.org/guide/extend/model_files>_
and read the Freezing <https://www.tensorflow.org/guide/extend/model_files#freezing>_ sectioninstall section to install :mod:utensor_cgenutensor-cli should be available in your console.. code-block:: console
# verbose mode
$ utensor-cli show graph.pb
# or oneline mode
$ utensor-cli show graph.pb --oneline
4. convert the protobuf file to C/C++ source code with utensor-cli
pred in graph.pb.. code-block:: console
$ utensor-cli convert --output-nodes=pred graph.pb
5. Compile your application code with generated C/C++ and weights files
\ |convert-example|
install_dev section.. code-block:: console
# run with `make`
$ make tests
# run with `pipenv`
$ pipenv run pytest tests
.. design philosophy
.. 12 Factor CLI App <https://medium.com/@jdxcode/12-factor-cli-apps-dd3c227a0e46?fbclid=IwAR1Gfq0D7oh3b-mXaIMV3RwYu39TAPrPXfz5sBKC4Rz1t-cckvC8WjBVl_w>_
High-level graph builder api for building :class:.uTensorGraph.
utensor_cgen uses TensorFlow api for building IR graph, uTensorGraph.uTensorGraph easily and do not need
to take care of the integrity of the graph.
The builder will take care of it automatically... _pip: https://pip.pypa.io/en/stable/ .. _pipenv: https://github.com/pypa/pipenv .. _Tensorflow: https://www.tensorflow.org .. _PyTorch: https://pytorch.org/ .. _uTensor: https://github.com/uTensor/uTensor
.. readme_end
.. |cnn-dropout| image:: doc/source/_images/cnn_dropout.png :alt: cnn-dropout .. |conv-pool-fuse| image:: doc/source/_images/conv_pool_fuse.png :alt: conv-pool-fuse .. |convert-example| image:: doc/source/_images/convert_example.png :alt: convert-example
.. TODOs .. =====
.. 1. (done?) core code generator implementation
.. - We need some refactoring, PRs are welcomed!
.. 2. type alias in C/C++
.. - ex: use uint8_t or unsigned char?
.. - a lot more about this....
.. 3. Relation among snippets/containers
.. - shared template variables? (headers, shared placeholders...etc)
.. 4. Better configuration schema
.. - json .. - yaml .. - or ?
Content type
Image
Digest
Size
1.1 GB
Last updated
almost 7 years ago
docker pull dboyliao/utensor-cli