Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Relay][Frontend] CoreML Support #2476

Merged
merged 5 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docker/install/ubuntu_install_python_package.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# install libraries for python package on ubuntu
pip2 install nose pylint numpy nose-timer cython decorator scipy tornado typing antlr4-python2-runtime attrs
pip3 install nose pylint numpy nose-timer cython decorator scipy tornado typed_ast pytest mypy orderedset antlr4-python3-runtime attrs
pip2 install nose pylint six numpy nose-timer cython decorator scipy tornado typing antlr4-python2-runtime attrs
pip3 install nose pylint six numpy nose-timer cython decorator scipy tornado typed_ast pytest mypy orderedset antlr4-python3-runtime attrs
9 changes: 5 additions & 4 deletions nnvm/python/nnvm/frontend/coreml.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ def ConvolutionLayerParams(op, insym, symtab):
else:
pos = [insym, weights]

if op.isDeconvolution:
ret = _sym.conv2d_transpose(*pos, **params)
else:
ret = _sym.conv2d(*pos, **params)
# consume padding layer
if symtab.in_padding:
params['padding'] = [sum(x) for x in zip(params.get('padding', [0, 0]), symtab.paddings)]
symtab.clear_padding()

if op.isDeconvolution:
ret = _sym.conv2d_transpose(*pos, **params)
else:
ret = _sym.conv2d(*pos, **params)
return ret

def BatchnormLayerParams(op, insym, symtab):
Expand Down
4 changes: 2 additions & 2 deletions nnvm/tests/python/frontend/coreml/model_zoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import urllib
from six.moves import urllib
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure six in installed as dependency in CI

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. thanks for reminding.

import os
from PIL import Image
import numpy as np
Expand All @@ -7,7 +7,7 @@ def download(url, path, overwrite=False):
if os.path.exists(path) and not overwrite:
return
print('Downloading {} to {}.'.format(url, path))
urllib.URLopener().retrieve(url, path)
urllib.request.urlretrieve(url, path)

def get_mobilenet():
url = 'https://docs-assets.developer.apple.com/coreml/models/MobileNet.mlmodel'
Expand Down
10 changes: 5 additions & 5 deletions nnvm/tests/python/frontend/coreml/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import model_zoo

def get_tvm_output(symbol, x, params, target, ctx,
out_shape=(1000,), input_name='image', dtype='float32'):
out_shape=(1, 1000), input_name='image', dtype='float32'):
shape_dict = {input_name : x.shape}
with nnvm.compiler.build_config(opt_level=3):
with nnvm.compiler.build_config(opt_level=2):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with opt_level=3, mobilenet fails with error below. Looks like it is due to some problem in nnvm's foldaxis pass, I will open an issue to track.

$ python -m nose -v nnvm/tests/python/frontend/coreml
test_forward.test_mobilenet_checkonly ... ERROR
test_forward.test_resnet50_checkonly ... ^C
======================================================================
ERROR: test_forward.test_mobilenet_checkonly
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
  File "/home/ubuntu/tvm/nnvm/tests/python/frontend/coreml/test_forward.py", line 41, in test_mobilenet_checkonly
    run_model_checkonly(model_file, 'mobilenet')
  File "/home/ubuntu/tvm/nnvm/tests/python/frontend/coreml/test_forward.py", line 36, in run_model_checkonly
    tvm_output = get_tvm_output(sym, x, params, target, ctx)
  File "/home/ubuntu/tvm/nnvm/tests/python/frontend/coreml/test_forward.py", line 21, in get_tvm_output
    graph, lib, params = nnvm.compiler.build(symbol, target, shape_dict, params=params)
  File "/home/ubuntu/tvm/nnvm/python/nnvm/compiler/build_module.py", line 292, in build
    graph = graph.apply("InferShape")
  File "/home/ubuntu/tvm/nnvm/python/nnvm/graph.py", line 234, in apply
    check_call(_LIB.NNGraphApplyPasses(self.handle, npass, cpass, ctypes.byref(ghandle)))
  File "/home/ubuntu/tvm/nnvm/python/nnvm/_base.py", line 75, in check_call
    raise NNVMError(py_str(_LIB.NNGetLastError()))
nnvm._base.NNVMError: Error in operator _param_9_sc: [01:28:47] /home/ubuntu/tvm/nnvm/src/top/tensor/broadcast.cc:119: Check failed: l == 1 || r == 1 operands could not be broadcast together with shapes [32,1,3,3] [32,1,1,1,112,112], l=3, r=112

Stack trace returned 10 entries:
[bt] (0) /home/ubuntu/tvm/build/libtvm.so(dmlc::StackTrace[abi:cxx11](unsigned long)+0x1f5) [0x7fca99a28365]
[bt] (1) /home/ubuntu/tvm/build/libtvm.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x3e) [0x7fca99a28fee]
[bt] (2) /home/ubuntu/tvm/nnvm/python/nnvm/../../../build/libnnvm_compiler.so(nnvm::top::BinaryBroadcastShape(nnvm::NodeAttrs const&, std::vector<nnvm::TShape, std::allocator<nnvm::TShape> >*, std::vector<nnvm::TShape, std::allocator<nnvm::TShape> >*)+0x6c0) [0x7fca949c6e00]
[bt] (3) /home/ubuntu/tvm/nnvm/python/nnvm/../../../build/libnnvm_compiler.so(+0x1174dd) [0x7fca948b04dd]
[bt] (4) /home/ubuntu/tvm/nnvm/python/nnvm/../../../build/libnnvm_compiler.so(+0x118c4a) [0x7fca948b1c4a]
[bt] (5) /home/ubuntu/tvm/nnvm/python/nnvm/../../../build/libnnvm_compiler.so(+0x119f3a) [0x7fca948b2f3a]
[bt] (6) /home/ubuntu/tvm/nnvm/python/nnvm/../../../build/libnnvm_compiler.so(nnvm::ApplyPasses(nnvm::Graph, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const&)+0x127d) [0x7fca94885eed]
[bt] (7) /home/ubuntu/tvm/nnvm/python/nnvm/../../../build/libnnvm_compiler.so(NNGraphApplyPasses+0x320) [0x7fca94863d10]
[bt] (8) /home/ubuntu/anaconda3/lib/python3.7/lib-dynload/../../libffi.so.6(ffi_call_unix64+0x4c) [0x7fcaac8cdec0]
[bt] (9) /home/ubuntu/anaconda3/lib/python3.7/lib-dynload/../../libffi.so.6(ffi_call+0x22d) [0x7fcaac8cd87d]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously this test did not run on CI. besides, it only fails on CUDA. CPU is good.
Also Relay is good.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

graph, lib, params = nnvm.compiler.build(symbol, target, shape_dict, params=params)
m = graph_runtime.create(graph, lib, ctx)
# set inputs
Expand All @@ -28,7 +28,7 @@ def get_tvm_output(symbol, x, params, target, ctx,
out = m.get_output(0, tvm.nd.empty(out_shape, dtype))
return out.asnumpy()

def test_model_checkonly(model_file, model_name=''):
def run_model_checkonly(model_file, model_name=''):
model = cm.models.MLModel(model_file)
sym, params = nnvm.frontend.from_coreml(model)
x = model_zoo.get_cat_image()
Expand All @@ -38,11 +38,11 @@ def test_model_checkonly(model_file, model_name=''):

def test_mobilenet_checkonly():
model_file = model_zoo.get_mobilenet()
test_model_checkonly(model_file, 'mobilenet')
run_model_checkonly(model_file, 'mobilenet')

def test_resnet50_checkonly():
model_file = model_zoo.get_resnet50()
test_model_checkonly(model_file, 'resnet50')
run_model_checkonly(model_file, 'resnet50')

def run_tvm_graph(graph_def, input_data, input_name, output_shape, output_dtype='float32'):
""" Generic function to compile on nnvm and execute on tvm """
Expand Down
2 changes: 1 addition & 1 deletion python/tvm/relay/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def __init__(self,
_make.Function, params, body, ret_type, type_params, attrs)

def __call__(self, *args):
"""Invoke the gobal function.
"""Invoke the global function.

Parameters
----------
Expand Down
1 change: 1 addition & 0 deletions python/tvm/relay/frontend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
from .keras import from_keras
from .onnx import from_onnx
from .tflite import from_tflite
from .coreml import from_coreml
8 changes: 8 additions & 0 deletions python/tvm/relay/frontend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ def __init__(self):
self.exprs = {}
self.params = {}
self.const_ctr = 1
self.in_padding = False

def new_const(self, value, shape=None, dtype="float32"):
name = "_param_%d" % (self.const_ctr)
Expand All @@ -257,6 +258,13 @@ def set_expr(self, name, expr):
assert isinstance(expr, _expr.Expr)
self.exprs[name] = expr

def set_padding(self, paddings):
self.paddings = paddings
self.in_padding = True

def clear_padding(self):
self.in_padding = False


class AttrCvt(object):
"""Common attribute conveter. An AttrConverter instance is a callable:
Expand Down
Loading