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.75k stars 6.8k forks source link

MXNet sym.Convolution can run even when the declared shape of variable is incorrect #20870

Open mazeltovlee opened 2 years ago

mazeltovlee commented 2 years ago

Description

mx.sym.Convolution can still work even when the shape of mx.sym.Variable is incorrect.

To Reproduce

Steps to reproduce

import mxnet as mx
net = mx.symbol.Variable('data')
net = mx.sym.reshape(data=net, shape=(0, 10, 10, 3))
net = mx.sym.transpose(data=net, axes=[0,3,1,2])

weight = mx.symbol.Variable("conv2d_1/kernel1", shape=(3, 1e+10, 3, 3), stype="default", dtype=np.float32)
weight = mx.symbol.transpose(data=weight, axes=[0,1,2,3])
bias = mx.symbol.Variable("conv2d_1/bias1", shape=(3,))
net = mx.symbol.Convolution(data=net, name="conv2d_1/conv2d3",
                              kernel=(3, 3), stride=(1, 1), pad=(),
                              num_filter=3, weight=weight,
                              dilate=(1,1), bias=bias)
import numpy as np
input_shape = (10, 10, 10, 3)
x = np.random.rand(*input_shape)
e = net.bind(mx.cpu(), {'data': mx.nd.array(x), "conv2d_1/kernel1": mx.nd.array(np.random.rand(3,3,3,3)), "conv2d_1/bias1": mx.nd.array(np.random.rand(3,))})
e.forward()

When running the above code, the symbolic model e can still output a reasonable result even though I have declared the shape of the weight variable to be an unreasonable value: shape=(3, 1e+10, 3, 3). It seems that mxnet will not refer to the declared shape in mx.Variable, but intuitively speaking, setting the unreasonable variable shape while model can still run may be misleading.

I am wondering if you can add some shape checks between the declared variable shape and the actual bound data shape.

github-actions[bot] commented 2 years ago

Welcome to Apache MXNet (incubating)! We are on a mission to democratize AI, and we are glad that you are contributing to it by opening this issue. Please make sure to include all the relevant context, and one of the @apache/mxnet-committers will be here shortly. If you are interested in contributing to our project, let us know! Also, be sure to check out our guide on contributing to MXNet and our development guides wiki.