apache / mxnet

Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more
https://mxnet.apache.org
Apache License 2.0
20.78k stars 6.79k forks source link

broadcast_like requires inputs to have same dtype #16851

Open leezu opened 4 years ago

leezu commented 4 years ago

Description

broadcast_like "Broadcasts lhs to have the same shape as rhs." Thus only the shape information of lhs and rhs array should matter. However, it also asserts that the dtypes are equal.

Error Message

[08:58:11] /home/ANT.AMAZON.COM/lausen/src/mxnet-master/src/io/../operator/elemwise_op_common.h:135: Check failed: assign(&dattr, vec.at(i)): Incompatible attr in node  at 1-th input: expected float32, got int64
Stack trace:
  [bt] (0) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x43) [0x7f8b58c46d63]
  [bt] (1) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(mxnet::op::ElemwiseAttr<int, &mxnet::op::type_is_none, &mxnet::op::type_assign, true, &mxnet::op::type_string[abi:cxx11], -1, -1>(nnvm::NodeAttrs const&, std::vector<int, std::allocator<int> >*, std::vector<int, std::allocator<int> >*, int const&)::{lambda(std::vector<int, std::allocator<int> > const&, unsigned long, char const*)#1}::operator()(std::vector<int, std::allocator<int> > const&, unsigned long, char const*) const+0x275) [0x7f8b58e26e75]
  [bt] (2) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(bool mxnet::op::ElemwiseAttr<int, &mxnet::op::type_is_none, &mxnet::op::type_assign, true, &mxnet::op::type_string[abi:cxx11], -1, -1>(nnvm::NodeAttrs const&, std::vector<int, std::allocator<int> >*, std::vector<int, std::allocator<int> >*, int const&)+0x1b3) [0x7f8b58fd7783]
  [bt] (3) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(bool mxnet::op::ElemwiseType<2, 1>(nnvm::NodeAttrs const&, std::vector<int, std::allocator<int> >*, std::vector<int, std::allocator<int> >*)+0x23f) [0x7f8b59021c6f]
  [bt] (4) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(mxnet::imperative::SetShapeType(mxnet::Context const&, nnvm::NodeAttrs const&, std::vector<mxnet::NDArray*, std::allocator<mxnet::NDArray*> > const&, std::vector<mxnet::NDArray*, std::allocator<mxnet::NDArray*> > const&, mxnet::DispatchMode*)+0x57e) [0x7f8b58df513e]
  [bt] (5) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(mxnet::Imperative::Invoke(mxnet::Context const&, nnvm::NodeAttrs const&, std::vector<mxnet::NDArray*, std::allocator<mxnet::NDArray*> > const&, std::vector<mxnet::NDArray*, std::allocator<mxnet::NDArray*> > const&)+0xeb) [0x7f8b58de6a4b]
  [bt] (6) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(MXImperativeInvokeImpl(void*, int, void**, int*, void***, int, char const**, char const**)+0x538) [0x7f8b58c82388]
  [bt] (7) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/../../build/libmxnet.so(MXImperativeInvokeEx+0x8b) [0x7f8b58c833cb]
  [bt] (8) /home/ANT.AMAZON.COM/lausen/src/mxnet-master/python/mxnet/_cy3/ndarray.cpython-37m-x86_64-linux-gnu.so(+0x13be8) [0x7f8b2f1f0be8]

To Reproduce

mx.nd.broadcast_like(mx.nd.ones((1,)), mx.nd.arange(10, dtype=int))

leezu commented 4 years ago

mx.np.broadcast_arrays(mx.np.ones((1,)), mx.np.arange(10, dtype=int)) works correctly.

leezu commented 4 years ago

The reason that this bug is important: When writing a HybridBlock, the dtype of some input array depends on the user. But some other internally used array may have a fixed dtype that could be unequal of the input dtype. Workaround is to add an extra cast prior to broadcast_like.

samskalicky commented 4 years ago

Since this issue has 2 parts:

szha commented 4 years ago

this issue is related to an op that is deprecated in 2.0, so I'm marking this as 1.x. this issue is probably not a good first issue without pointers on where to look to address it.