Skip to content

Commit 18f0460

Browse files
committed
sync with release branch
2 parents f4b87e5 + 18a396a commit 18f0460

File tree

301 files changed

+10049
-2315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+10049
-2315
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ jobs:
66
steps:
77
- checkout
88
- run: mkdir -p /tmp/coverage && mkdir -p /tmp/test_results
9-
- run: pip3 install -e .[dev]
9+
- run: pip3 install .
1010
- run:
1111
name: Test
12-
command: pytest -vv tests
12+
command: LC_ALL=C.UTF-8 LANG=C.UTF-8 pytest -vv tests -m 'not slow_test and not deprecated'
1313
#- run: genhtml main_coverage.info --output-directory /tmp/coverage
1414
#- store_test_results:
1515
# path: /tmp/test_results

.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#system files
2+
__pycache__
3+
.DS_Store
14
# virtual env
25
.venv
36
venv
@@ -8,6 +11,8 @@ dev_notes
811
!tests/deep_mlp/end_to_end.ipynb
912
*.egg*
1013
worktree/
14+
build/
15+
dist/
1116
# tests
1217
.pytest_cache
1318
tests/*/cpp
@@ -18,4 +23,4 @@ tests/deep_mlp/data
1823
.vscode
1924
# docs
2025
doc/build
21-
doc/source/_build
26+
doc/source/_build

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
include LICENSE README.md
2-
recursive-include utensor_cgen/backend/snippets/templates *
2+
recursive-include utensor_cgen/backend/utensor/snippets/templates *

Makefile

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
tests:
44
rm -f tests_log.txt
55
make test_utils test_ir test_transformer test_frontend \
6-
test_matcher test_graph_constructor
6+
test_matcher test_graph_constructor test_backend
77

88
test_%:
99
@if [ -d .venv ]; then \
@@ -12,5 +12,23 @@ test_%:
1212
pytest tests/$@ -vv -s | tee -a tests_log.txt; \
1313
fi;
1414

15+
package:
16+
rm -rf dist/*
17+
.venv/bin/python setup.py bdist_wheel sdist
18+
rm -rf build utensor_cgen.egg-info/
19+
20+
upload-test: package
21+
.venv/bin/twine upload -r pypitest dist/*
22+
23+
install-test: package
24+
pip uninstall -y utensor-cgen
25+
pip install dist/*.tar.gz
26+
27+
upload: package
28+
.venv/bin/twine upload -r pypi dist/*
29+
1530
clean:
16-
rm -rf tests_log.txt *.pdf .pytest_cache
31+
rm -rf tests_log.txt *.pdf \
32+
models data \
33+
tests/test_backend/{models,data} \
34+
.pytest_cache dist/ build/

Pipfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[[source]]
22
url = "https://pypi.python.org/simple"
3-
verify_ssl = true
3+
verify_ssl = false
44
name = "pypi"
55

66
[packages]
77
e1839a8 = {path = ".",editable = true}
88

99
[dev-packages]
10+
matplotlib = "*"
1011
pylint = "*"
1112
"flake8" = "*"
1213
pytest = "*"
@@ -16,3 +17,6 @@ scipy = "*"
1617
graphviz = "*"
1718
sphinx = "*"
1819
sphinx-autoapi = "*"
20+
21+
[pipenv]
22+
allow_prereleases = true

Pipfile.lock

Lines changed: 826 additions & 566 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.rst

Lines changed: 75 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,22 @@ Overall Architecture
7272

7373
::
7474

75-
============ +-----------------+ ===================
76-
|| model file || --> | frontend Parser | --> || uTensorGraph (IR) ||
77-
============ +-----------------+ ===================
78-
|
79-
+-------------------------------+ |
80-
| graph transformer | |
81-
| (legalization & optimization) | <------/
82-
+-------------------------------+
83-
|
84-
v
85-
===========================
86-
|| uTensorGraph ||
87-
|| (legalized and optimized) ||
88-
===========================
89-
|
90-
+--------------------------+ |
91-
| backend (code generator) | <----/
75+
============ +-----------------+ ============================
76+
|| model file || --> | frontend Parser | --> || uTensorGraph (IR, generic) ||
77+
============ +-----------------+ ============================
78+
|
79+
v
80+
+---------------------+
81+
======================= | graph transformer |
82+
|| uTensorGraph || <-- | (optimization) |
83+
|| (generic, optimized) || +---------------------+
84+
=======================
85+
|
86+
+--------------------------+ |
87+
| backend (code generator) | <--/
9288
+--------------------------+
9389
|
94-
`---> (target files, ex: model.cpp, model.hpp, weights.idx)
90+
`---> (target files, ex: model.cpp, model.hpp, weights.idx, ...etc)
9591

9692
Basic Usage
9793
===========
@@ -114,7 +110,8 @@ Convert Model File to C/C++ Code
114110
.. code-block:: console
115111
116112
$ utensor-cli convert <model.pb> \
117-
--output-nodes=<node_name>[,<node_name>,...]
113+
--output-nodes=<node name>[,<node name>,...] \
114+
[--config=config.toml]
118115
119116
Convert given pb file into cpp/hpp files.
120117

@@ -123,6 +120,8 @@ nodes you want to output, seperated by comma for multiple values.
123120

124121
In graph theory terminology, they are ``leaf`` nodes of your graph.
125122

123+
Use ``--config`` to pass a configuration file to the cli, you can use ``generate-config`` command to generate one (see below).
124+
126125
example
127126
~~~~~~~
128127

@@ -132,8 +131,34 @@ example
132131
133132
Run ``utensor-cli convert --help`` for detailed information.
134133

135-
:mod:`utensor_cgen` as Library
136-
==============================
134+
Configuration
135+
-------------
136+
137+
``utensor-cli`` use ``toml`` as configuration format.
138+
139+
You can generate configuration file of given target as following:
140+
141+
.. code-block:: console
142+
143+
$ utensor-cli generate-config --target <target name> [-o filename.toml]
144+
145+
This command will generate a ``toml`` file listing all configurable values with its defaults.
146+
147+
You can modify the value and pass the file to cli with ``--config`` flag.
148+
149+
example
150+
~~~~~~~
151+
152+
.. code-block:: console
153+
154+
# generate config file
155+
$ utensor-cli generate-config --target utensor -o myconfig.toml
156+
157+
# after editting myconfig.toml
158+
$ utensor-cli convert mymodel.pb --config=myconfig.toml --output-nodes=output,...
159+
160+
Use :mod:`utensor_cgen` as Library
161+
==================================
137162

138163
.. subgraph-match-begine
139164
@@ -201,12 +226,32 @@ Use Case: Dropout Layer Removal
201226

202227
\ |cnn-dropout|
203228

204-
.. subgraph-match-end
205-
206229
We use mainly `Tensorflow`_ for declaring the pattern graph for matcher now.
207230

208231
High-level graph builder is on its way, see `Future Works <#future-works>`_ for detail.
209232

233+
.. subgraph-match-end
234+
235+
.. offline-tensor-alloc-start
236+
237+
Offline Tensor Memory Allocation
238+
--------------------------------
239+
240+
Considering following simple multi layers perceptron (`simple_mnist.pb`_):
241+
242+
\ |mlp-alloc-graph|
243+
244+
Once enabled the optimization transformer, ``tensor_alloc``, an offline tensor memory allocation planner,
245+
``utensor-cli`` will generate ``uTensor`` runtime codes that use following optimized allocation plan:
246+
247+
\ |mlp-alloc|
248+
249+
- y-axis: tensor names ordered by topological sorting
250+
- x-axis: these are the memory span occupied by each tensor, that is, the memory address offset and
251+
the size of the tensor
252+
253+
.. offline-tensor-alloc-end
254+
210255
Tutorials
211256
=========
212257

@@ -223,8 +268,8 @@ TensorFlow_
223268

224269
1. Freeze your `tensorflow.Graph`
225270

226-
- please refer to the `official doc <https://www.tensorflow.org/guide/extend/model_files>`_
227-
and read the `Freezing <https://www.tensorflow.org/guide/extend/model_files#freezing>`_ section
271+
- please refer to this `issue track <https://github.com/tensorflow/tensorflow/issues/27614>`_ for detail
272+
- especially this `comment <https://github.com/tensorflow/tensorflow/issues/27614#issuecomment-571889676>`_ by Robin2091
228273

229274
2. Follow instructions in :ref:`install` section to install :mod:`utensor_cgen`
230275

@@ -288,6 +333,7 @@ Future Works
288333
.. _Tensorflow: https://www.tensorflow.org
289334
.. _PyTorch: https://pytorch.org/
290335
.. _uTensor: https://github.com/uTensor/uTensor
336+
.. _simple_mnist.pb: https://github.com/uTensor/utensor_cgen/blob/develop/tests/deep_mlp/simple_mnist.pb
291337

292338
.. readme_end
293339
@@ -297,7 +343,10 @@ Future Works
297343
:alt: conv-pool-fuse
298344
.. |convert-example| image:: doc/source/_images/convert_example.png
299345
:alt: convert-example
300-
346+
.. |mlp-alloc| image:: doc/source/_images/mlp_alloc.png
347+
:alt: mlp-alloc
348+
.. |mlp-alloc-graph| image:: doc/source/_images/mlp_alloc_graph.png
349+
:alt: mlp-alloc-graph
301350

302351
.. TODOs
303352
.. =====

doc/source/_images/mlp_alloc.png

24.9 KB
Loading
174 KB
Loading

doc/source/images.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@
88
:height: 300
99
.. |convert-example| image:: /_images/convert_example.png
1010
:alt: convert-example
11+
.. |mlp-alloc| image:: /_images/mlp_alloc.png
12+
:alt: mlp-alloc
13+
.. |mlp-alloc-graph| image:: /_images/mlp_alloc_graph.png
14+
:alt: mlp-alloc-graph
15+
:width: 300
16+
:height: 450

example_config.toml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# https://github.com/toml-lang/toml
2+
# <target_name>.<component>.<part>
3+
# we use string 'None' to represent python None value
4+
# you should convert the string to None if you try to write extension for utensor_cgen
5+
[utensor.backend]
6+
legacy-api = true
7+
8+
[utensor.backend.tensor_alloc_planner]
9+
max_pool_size = 1048576
10+
include_inputs = false
11+
out_fname = "None"
12+
enabled = true
13+
14+
[utensor.backend.legacy_code_generator]
15+
src_fname = "None"
16+
params_dir = "data"
17+
embed_params_dir = "/fs/data"
18+
model_dir = "models"
19+
debug_cmt = false
20+
21+
[utensor.backend.legacy_graph_lower]
22+
23+
[utensor.backend.rearch_code_generator]
24+
src_fname = "None"
25+
header_fname = "None"
26+
params_dir = "data"
27+
model_dir = "models"
28+
meta_data_pool_size = "auto"
29+
ram_data_pool_size = "auto"
30+
31+
[utensor.backend.rearch_graph_lower]
32+
33+
[utensor.backend.pipeline_transformer]
34+
save_graph = false
35+
transform_methods = [ "dropout(name_pattern=r'(dropout[_\\w\\d]*)/.*')", "linear_reorder", "quantize", "conv_pool", "inline", "biasAdd", "remove_id_op", "fake_gather_v2", "refcnt",]
36+
37+
[utensor.backend.tensor_alloc_planner.aesthetic_kwargs]
38+
split_on_large_graph = true
39+
num_tensors_per_split = 20
40+
figsize = "None"
41+
fontsize = 12
42+
lw = 12
43+
rand_seed = 1111
44+
45+
[utensor.backend.tensor_alloc_planner.dtype_size_map]
46+
float = 4
47+
double = 8
48+
uint8 = 1
49+
int = 4
50+
long = 8

plugins/dummy_backend/__init__.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from textwrap import wrap
2+
3+
from utensor_cgen.backend import BackendManager
4+
from utensor_cgen.backend.base import Backend
5+
from utensor_cgen.utils import class_property
6+
7+
8+
@BackendManager.register
9+
class DummyBackend(Backend):
10+
TARGET = 'dummy-backend'
11+
12+
def __init__(self, config):
13+
self.output_file = self.config[self.TARGET][self.COMPONENT]['output-file']
14+
15+
def apply(self, ugraph):
16+
with open(self.output_file, 'w') as fid:
17+
fid.write('#include <stdio.h>\n\n')
18+
fid.write('int main(int argc, char* argv[]) {\n')
19+
fid.write(' printf("graph name: {}\\n");\n'.format(ugraph.name))
20+
fid.write(' printf("ops in topological sorted order:\\n");\n')
21+
for op_name in ugraph.topo_order:
22+
fid.write(' printf(" {}\\n");\n'.format(op_name))
23+
fid.write(' return 0;\n}')
24+
25+
@class_property
26+
def default_config(cls):
27+
return {
28+
cls.TARGET: {
29+
cls.COMPONENT: {
30+
'output-file': 'list_op.c'
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)