-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
152 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
recursive-include mgeconvert/backend *.sh *.cc *.h *.so *.so.1 flatc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash -e | ||
basepath=$(cd `dirname $0`; pwd) | ||
|
||
. ${basepath}/../mgeconvert/backend/ir_to_tflite/build_flatbuffer.sh | ||
|
||
cd $basepath/.. | ||
python3 setup.py sdist | ||
python3 setup.py bdist_wheel tflite | ||
python3 setup.py bdist_wheel caffe | ||
python3 setup.py bdist_wheel onnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
numpy>=1.17 | ||
tqdm | ||
onnx==1.7.0 | ||
onnxoptimizer | ||
onnx-simplifier==0.3.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash -e | ||
basepath=$(cd `dirname $0`; pwd) | ||
|
||
|
||
if [ ! -d /tmp/mgeconvert ]; then | ||
mkdir /tmp/mgeconvert | ||
fi | ||
TMP_DIR="/tmp/mgeconvert/flatbuffers" | ||
rm -rf $TMP_DIR | ||
|
||
# build flatbuffers | ||
git clone https://github.com/google/flatbuffers.git -b v1.12.0 $TMP_DIR | ||
echo "building flatbuffers..." | ||
cd $TMP_DIR | ||
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_SHAREDLIB=on -DCMAKE_INSTALL_PREFIX=${basepath}/pyflexbuffers | ||
make -j; make install | ||
echo "build flatbuffers done" | ||
|
||
rm -rf $TMP_DIR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import os | ||
import re | ||
import subprocess | ||
import sys | ||
|
||
from setuptools import Extension, find_packages, setup | ||
from setuptools.command.build_ext import build_ext as _build_ext | ||
|
@@ -16,6 +17,25 @@ | |
tfversion = None | ||
|
||
|
||
def write_init(targets, tflite_schema_version=None): | ||
with open("mgeconvert/__init__.py", "w") as init_file: | ||
[ | ||
init_file.write("from .converters.mge_to_%s import mge_to_%s\n" % (i, i)) | ||
for i in targets | ||
] | ||
[ | ||
init_file.write( | ||
"from .converters.tm_to_%s import tracedmodule_to_%s\n" % (i, i) | ||
) | ||
for i in targets | ||
] | ||
if "onnx" in targets: | ||
init_file.write("from .converters.onnx_to_mge import onnx_to_mge\n") | ||
init_file.write("from .converters.onnx_to_tm import onnx_to_tracedmodule\n") | ||
if tflite_schema_version: | ||
init_file.write(f"tflite_schema_version={tflite_schema_version}\n") | ||
|
||
|
||
class install(_install): | ||
user_options = _install.user_options + [ | ||
("targets=", None, "<description for this custom option>"), | ||
|
@@ -34,27 +54,28 @@ def run(self): | |
options = ["caffe", "onnx", "tflite"] | ||
if self.targets == "all": | ||
targets.extend(options) | ||
elif self.targets is None: | ||
pass | ||
else: | ||
targets.extend(i for i in options if self.targets.find(i) >= 0) | ||
|
||
with open("mgeconvert/__init__.py", "w") as init_file: | ||
[ | ||
init_file.write( | ||
"from .converters.mge_to_%s import mge_to_%s\n" % (i, i) | ||
write_init(targets, self.tfversion) | ||
|
||
if "tflite" in targets: | ||
tflite_path = os.path.join( | ||
os.path.dirname(__file__), "mgeconvert", "backend", "ir_to_tflite" | ||
) | ||
if ( | ||
not os.path.exists( | ||
os.path.join(tflite_path, "pyflexbuffers", "bin", "flatc") | ||
) | ||
for i in targets | ||
] | ||
[ | ||
init_file.write( | ||
"from .converters.tm_to_%s import tracedmodule_to_%s\n" % (i, i) | ||
) | ||
for i in targets | ||
] | ||
if "onnx" in targets: | ||
init_file.write("from .converters.onnx_to_mge import onnx_to_mge\n") | ||
init_file.write( | ||
"from .converters.onnx_to_tm import onnx_to_tracedmodule\n" | ||
or not os.path.exists( | ||
os.path.join(tflite_path, "pyflexbuffers", "include", "flatbuffers") | ||
) | ||
or not os.path.exists(os.path.join(tflite_path, "pyflexbuffers", "lib")) | ||
): | ||
ret = os.system(f"{tflite_path}/build_flatbuffer.sh") | ||
if ret: | ||
raise RuntimeError("build flatbuffer failed!") | ||
|
||
global tfversion | ||
tfversion = self.tfversion | ||
|
@@ -74,10 +95,11 @@ def find_extension(self, name): | |
raise TypeError("can not build %s" % name) | ||
|
||
def build_all(self, ext): | ||
if ext.name == "tflite" and tfversion is not None: | ||
subprocess.check_call([ext.script, tfversion]) | ||
else: | ||
subprocess.check_call(ext.script) | ||
if ext.script: | ||
if ext.name == "tflite" and tfversion is not None: | ||
subprocess.check_call([ext.script, tfversion]) | ||
else: | ||
subprocess.check_call(ext.script) | ||
if ext.artifacts is not None: | ||
self.copy_tree(ext.artifacts, os.path.join(self.build_lib, ext.artifacts)) | ||
|
||
|
@@ -103,17 +125,61 @@ def __init__(self, name, script, artifacts=None): | |
), | ||
] | ||
|
||
setup( | ||
name="mgeconvert", | ||
version=__version__, | ||
description="MegEngine Converter", | ||
author="Megvii Engine Team", | ||
author_email="[email protected]", | ||
url="https://github.com/MegEngine/mgeconvert", | ||
packages=find_packages(exclude=["test", "test.*"]), | ||
ext_modules=ext_modules, | ||
cmdclass={"install": install, "build_ext": build_ext}, | ||
include_package_data=True, | ||
install_requires=["numpy"], | ||
scripts=["bin/convert"], | ||
) | ||
if __name__ == "__main__": | ||
install_requires = ["numpy", "tqdm"] | ||
requires_mapping = { | ||
"onnx": ["onnx>=1.7.0", "onnx-simplifier", "protobuf",], | ||
"caffe": ["protobuf>=3.11.1"], | ||
"tflite": ["flatbuffers==1.12.0", "pybind11==2.6.2"], | ||
"all": [ | ||
"onnx>=1.7.0", | ||
"onnx-simplifier", | ||
"protobuf>=3.11.1", | ||
"flatbuffers==1.12.0", | ||
], | ||
} | ||
pkg_name = "mgeconvert" | ||
if len(sys.argv) >= 2: | ||
if sys.argv[1] == "bdist_wheel": | ||
if len(sys.argv) == 2: | ||
targets.extend(["onnx", "caffe", "tflite"]) | ||
else: | ||
assert sys.argv[2] in ["onnx", "caffe", "tflite"] | ||
targets.append(sys.argv[2]) | ||
pkg_name += "-" + sys.argv[2] | ||
sys.argv = sys.argv[:2] | ||
for t in targets: | ||
if t in requires_mapping: | ||
install_requires.extend(requires_mapping[t]) | ||
elif sys.argv[1] == "install": | ||
assert ( | ||
len(sys.argv) > 2 | ||
), 'use "--targets=[eg, tflite,caffe,onnx,all]" to indicate converters to install' | ||
for v in sys.argv[2:]: | ||
if v.startswith("--targets="): | ||
target_cvts = v.split("=")[1].split(",") | ||
for opt in target_cvts: | ||
assert opt in requires_mapping | ||
install_requires.extend(requires_mapping[opt]) | ||
break | ||
write_init(targets) | ||
|
||
setup( | ||
name=pkg_name, | ||
version=__version__, | ||
description="MegEngine Converter", | ||
author="Megvii Engine Team", | ||
author_email="[email protected]", | ||
url="https://github.com/MegEngine/mgeconvert", | ||
packages=find_packages(exclude=["test", "test.*"]), | ||
ext_modules=ext_modules, | ||
cmdclass={"install": install, "build_ext": build_ext}, | ||
include_package_data=True, | ||
install_requires=install_requires, | ||
scripts=["bin/convert"], | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
) |