sxjscience commented on a change in pull request #16465: [Gluon] [Fix] [WIP]
Fix HybridBlock when hybridize is not called
URL: https://github.com/apache/incubator-mxnet/pull/16465#discussion_r334623501
##########
File path: python/mxnet/gluon/block.py
##########
@@ -1054,34 +1098,16 @@ def register_op_hook(self, callback,
monitor_all=False):
def forward(self, x, *args):
"""Defines the forward computation. Arguments can be either
:py:class:`NDArray` or :py:class:`Symbol`."""
- flatten_args = _flatten([x] + list(args), 'inputs')[0]
- is_ndarray = None
- ctx = None
- exist_sym_nd = False
- for ele in flatten_args:
- if isinstance(ele, NDArray):
- if is_ndarray is False:
- raise ValueError('In HybridBlock, we do not support mixed
NDArrays and Symbols'
- ' types for the input.\n'
- 'Received types are: {}.'
- .format([type(ele) for ele in
flatten_args]))
- is_ndarray = True
- exist_sym_nd = True
- ctx = ele.context
- elif isinstance(ele, Symbol):
- if is_ndarray:
- raise ValueError('In HybridBlock, we do not support mixed
NDArrays and Symbols'
- ' types for the input.\n'
- 'Received types are: {}.'
- .format([type(ele) for ele in
flatten_args]))
- is_ndarray = False
- exist_sym_nd = True
- else:
- assert ele is None, 'Only support None, NDArray and Symbol as
the input'
- if not exist_sym_nd:
- raise ValueError('There must at least one NDArray or Symbol in the
input, received')
- if is_ndarray:
+ has_symbol, has_ndarray, ctx_set = _gather_type_ctx_info([x] +
list(args))
+ if has_symbol and has_ndarray:
+ raise ValueError('In HybridBlock, we do not support mixed NDArrays
and Symbols'
+ ' types for the input. Please check the type of
the args.\n')
+ if not has_symbol and not has_ndarray:
+ raise ValueError('In HybridBlock, there must be one NDArray or one
Symbol in the input.'
+ ' Please check the type of the args.\n')
+ if has_ndarray:
+ ctx = next(iter(ctx_set))
Review comment:
Nice catch, should sort it beforehand.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services