From 5502fa0bb7047bc77128a8e235f587241f810a5c Mon Sep 17 00:00:00 2001 From: Lai Wei Date: Thu, 2 May 2019 22:41:34 -0700 Subject: [PATCH] change check and shape_is_known --- src/operator/bilinear_sampler-inl.h | 4 +-- src/operator/image/image_random-inl.h | 2 +- src/operator/instance_norm-inl.h | 2 +- src/operator/l2_normalization-inl.h | 12 +++---- src/operator/l2_normalization.cc | 4 +-- src/operator/nn/lrn.cc | 2 +- src/operator/nn/mkldnn/mkldnn_convolution.cc | 36 +++++++++---------- .../nn/mkldnn/mkldnn_deconvolution.cc | 18 +++++----- src/operator/nn/pooling.cc | 6 ++-- src/operator/nn/upsampling.cc | 2 +- src/operator/operator_util.cc | 2 +- src/operator/pooling_v1-inl.h | 4 +-- .../quantization/quantized_flatten-inl.h | 2 +- src/operator/random/sample_multinomial_op.h | 2 +- src/operator/regression_output-inl.h | 2 +- src/operator/softmax_output-inl.h | 2 +- src/operator/spatial_transformer-inl.h | 4 +-- src/operator/tensor/broadcast_reduce_op.h | 10 +++--- src/operator/tensor/dot-inl.h | 2 ++ .../tensor/elemwise_binary_broadcast_op.h | 4 +-- src/operator/tensor/indexing_op.h | 8 ++--- src/operator/tensor/matrix_op-inl.h | 10 +++--- tests/python/unittest/test_operator.py | 26 ++++++++++++++ 23 files changed, 97 insertions(+), 69 deletions(-) diff --git a/src/operator/bilinear_sampler-inl.h b/src/operator/bilinear_sampler-inl.h index abb4a61dc84c..efcd3c37a8ff 100644 --- a/src/operator/bilinear_sampler-inl.h +++ b/src/operator/bilinear_sampler-inl.h @@ -149,10 +149,10 @@ class BilinearSamplerProp : public OperatorProperty { CHECK_EQ(in_shape->size(), 2U) << "Input:[data, grid]"; const mxnet::TShape &dshape = (*in_shape)[bs::kData]; const mxnet::TShape &lshape = (*in_shape)[bs::kGrid]; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; CHECK_EQ(dshape.ndim(), 4U) \ << "input data should be 4D in batch-num_filter-y-x"; - if (!shape_is_known(lshape)) return false; + if (!ndim_is_known(lshape)) return false; CHECK_EQ(lshape.ndim(), 4U) \ << "Sampler grid should be 4D in batch-2-y-x"; CHECK_EQ(dshape[0], lshape[0]); diff --git a/src/operator/image/image_random-inl.h b/src/operator/image/image_random-inl.h index aeb189f35b78..bcce99e6a162 100644 --- a/src/operator/image/image_random-inl.h +++ b/src/operator/image/image_random-inl.h @@ -93,7 +93,7 @@ inline bool ToTensorShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(out_attrs->size(), 1U); mxnet::TShape &shp = (*in_attrs)[0]; - if (!shape_is_known(shp)) return false; + if (!ndim_is_known(shp)) return false; CHECK((shp.ndim() == 3) || (shp.ndim() == 4)) << "Input image must have shape (height, width, channels), or " diff --git a/src/operator/instance_norm-inl.h b/src/operator/instance_norm-inl.h index b7e579e2d066..c71cbe043afd 100644 --- a/src/operator/instance_norm-inl.h +++ b/src/operator/instance_norm-inl.h @@ -113,7 +113,7 @@ class InstanceNormOp : public Operator { CHECK_EQ(in_data.size(), 3U); CHECK_EQ(out_data.size(), 3U); - CHECK_GE(in_data[instance_norm::kData].ndim(), 3U) + CHECK_GE(in_data[instance_norm::kData].ndim(), 3) << "InstanceNorm only supports input tensors of rank > 2."; Stream *s = ctx.get_stream(); diff --git a/src/operator/l2_normalization-inl.h b/src/operator/l2_normalization-inl.h index 975e81f78c25..210d91823075 100644 --- a/src/operator/l2_normalization-inl.h +++ b/src/operator/l2_normalization-inl.h @@ -102,7 +102,7 @@ class L2NormalizationOp : public Operator { norm = F(norm); out = data / broadcast<0>(norm, out.shape_); } else if (param_.mode == l2_normalization::kChannel) { - CHECK_GE(orig_shape.ndim(), 3U); + CHECK_GE(orig_shape.ndim(), 3); Shape<3> dshape = Shape3(orig_shape[0], orig_shape[1], orig_shape.ProdShape(2, orig_shape.ndim())); Tensor data = in_data[l2_normalization::kData] @@ -120,7 +120,7 @@ class L2NormalizationOp : public Operator { norm = F(norm); out = data / broadcast_with_axis(norm, 0, orig_shape[1]); } else if (param_.mode == l2_normalization::kSpatial) { - CHECK_GE(orig_shape.ndim(), 3U); + CHECK_GE(orig_shape.ndim(), 3); Shape<3> dshape = Shape3(orig_shape[0], orig_shape[1], orig_shape.ProdShape(2, orig_shape.ndim())); Tensor data = in_data[l2_normalization::kData] @@ -174,7 +174,7 @@ class L2NormalizationOp : public Operator { (grad_out - data * broadcast<0>(temp, data.shape_)) / broadcast<0>(norm, data.shape_)); } else if (param_.mode == l2_normalization::kChannel) { - CHECK_GE(orig_shape.ndim(), 3U); + CHECK_GE(orig_shape.ndim(), 3); Shape<3> dshape = Shape3(orig_shape[0], orig_shape[1], orig_shape.ProdShape(2, orig_shape.ndim())); Tensor data = out_data[l2_normalization::kOut] @@ -193,7 +193,7 @@ class L2NormalizationOp : public Operator { (grad_out - data * broadcast_with_axis(temp, 0, orig_shape[1])) / broadcast_with_axis(norm, 0, orig_shape[1])); } else if (param_.mode == l2_normalization::kSpatial) { - CHECK_GE(orig_shape.ndim(), 3U); + CHECK_GE(orig_shape.ndim(), 3); Shape<3> dshape = Shape3(orig_shape[0], orig_shape[1], orig_shape.ProdShape(2, orig_shape.ndim())); Tensor data = out_data[l2_normalization::kOut] @@ -273,12 +273,12 @@ class L2NormalizationProp : public OperatorProperty { if (param_.mode == l2_normalization::kInstance) { out_shape->push_back(Shape1(dshape[0])); } else if (param_.mode == l2_normalization::kChannel) { - CHECK_GE(dshape.ndim(), 3U) << "At lease 3 dimensions required in channel mode"; + CHECK_GE(dshape.ndim(), 3) << "At lease 3 dimensions required in channel mode"; mxnet::TShape norm_shape = dshape; norm_shape[1] = 1; out_shape->push_back(norm_shape); } else if (param_.mode == l2_normalization::kSpatial) { - CHECK_GE(dshape.ndim(), 3U) << "At lease 3 dimensions required in spatial mode"; + CHECK_GE(dshape.ndim(), 3) << "At lease 3 dimensions required in spatial mode"; out_shape->push_back(Shape2(dshape[0], dshape[1])); } else { return false; diff --git a/src/operator/l2_normalization.cc b/src/operator/l2_normalization.cc index 92307af814d2..cbe2caeb394e 100644 --- a/src/operator/l2_normalization.cc +++ b/src/operator/l2_normalization.cc @@ -70,7 +70,7 @@ class L2NormalizationOpCPU : public L2NormalizationOp { } } } else if (this->param_.mode == l2_normalization::kChannel) { - CHECK_GE(orig_shape.ndim(), 3U); + CHECK_GE(orig_shape.ndim(), 3); Shape<3> dshape = Shape3(orig_shape[0], orig_shape[1], orig_shape.ProdShape(2, orig_shape.ndim())); Tensor data = in_data[l2_normalization::kData] @@ -94,7 +94,7 @@ class L2NormalizationOpCPU : public L2NormalizationOp { } } } else if (this->param_.mode == l2_normalization::kSpatial) { - CHECK_GE(orig_shape.ndim(), 3U); + CHECK_GE(orig_shape.ndim(), 3); Shape<3> dshape = Shape3(orig_shape[0], orig_shape[1], orig_shape.ProdShape(2, orig_shape.ndim())); Tensor data = in_data[l2_normalization::kData] diff --git a/src/operator/nn/lrn.cc b/src/operator/nn/lrn.cc index b632e35b57fe..6343cee8ec21 100644 --- a/src/operator/nn/lrn.cc +++ b/src/operator/nn/lrn.cc @@ -40,7 +40,7 @@ bool LRNShape(const nnvm::NodeAttrs& attrs, using namespace mshadow; CHECK_EQ(in_shape->size(), 1U) << "Input:[data]"; const mxnet::TShape &dshape = in_shape->at(0); - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; out_shape->clear(); out_shape->push_back(dshape); out_shape->push_back(dshape); diff --git a/src/operator/nn/mkldnn/mkldnn_convolution.cc b/src/operator/nn/mkldnn/mkldnn_convolution.cc index a3aca98d9f81..9870f7a810d5 100644 --- a/src/operator/nn/mkldnn/mkldnn_convolution.cc +++ b/src/operator/nn/mkldnn/mkldnn_convolution.cc @@ -63,15 +63,15 @@ mkldnn::convolution_forward::primitive_desc GetConvFwdImpl(const MKLDNNConvFullP mkldnn::memory::dims strides(param.conv_param.kernel.ndim()); mkldnn::memory::dims padding(param.conv_param.kernel.ndim()); if (param.conv_param.kernel.ndim() == 1) { - CHECK_GE(param.conv_param.stride.ndim(), 1U); - CHECK_GE(param.conv_param.pad.ndim(), 1U); - CHECK_GE(param.conv_param.dilate.ndim(), 1U); + CHECK_GE(param.conv_param.stride.ndim(), 1); + CHECK_GE(param.conv_param.pad.ndim(), 1); + CHECK_GE(param.conv_param.dilate.ndim(), 1); strides[0] = param.conv_param.stride[0]; padding[0] = param.conv_param.pad[0]; } else if (param.conv_param.kernel.ndim() == 2) { - CHECK_GE(param.conv_param.stride.ndim(), 2U); - CHECK_GE(param.conv_param.pad.ndim(), 2U); - CHECK_GE(param.conv_param.dilate.ndim(), 2U); + CHECK_GE(param.conv_param.stride.ndim(), 2); + CHECK_GE(param.conv_param.pad.ndim(), 2); + CHECK_GE(param.conv_param.dilate.ndim(), 2); strides[0] = param.conv_param.stride[0]; strides[1] = param.conv_param.stride[1]; padding[0] = param.conv_param.pad[0]; @@ -173,15 +173,15 @@ static mkldnn::convolution_backward_data::primitive_desc GetConvBwdData( mkldnn::memory::dims strides(param.kernel.ndim()); mkldnn::memory::dims padding(param.kernel.ndim()); if (param.kernel.ndim() == 1) { - CHECK_GE(param.stride.ndim(), 1U); - CHECK_GE(param.pad.ndim(), 1U); - CHECK_GE(param.dilate.ndim(), 1U); + CHECK_GE(param.stride.ndim(), 1); + CHECK_GE(param.pad.ndim(), 1); + CHECK_GE(param.dilate.ndim(), 1); strides[0] = param.stride[0]; padding[0] = param.pad[0]; } else if (param.kernel.ndim() == 2) { - CHECK_GE(param.stride.ndim(), 2U); - CHECK_GE(param.pad.ndim(), 2U); - CHECK_GE(param.dilate.ndim(), 2U); + CHECK_GE(param.stride.ndim(), 2); + CHECK_GE(param.pad.ndim(), 2); + CHECK_GE(param.dilate.ndim(), 2); strides[0] = param.stride[0]; strides[1] = param.stride[1]; padding[0] = param.pad[0]; @@ -241,15 +241,15 @@ static mkldnn::convolution_backward_weights::primitive_desc GetConvBwdWeights( mkldnn::memory::dims strides(param.kernel.ndim()); mkldnn::memory::dims padding(param.kernel.ndim()); if (param.kernel.ndim() == 1) { - CHECK_GE(param.stride.ndim(), 1U); - CHECK_GE(param.pad.ndim(), 1U); - CHECK_GE(param.dilate.ndim(), 1U); + CHECK_GE(param.stride.ndim(), 1); + CHECK_GE(param.pad.ndim(), 1); + CHECK_GE(param.dilate.ndim(), 1); strides[0] = param.stride[0]; padding[0] = param.pad[0]; } else if (param.kernel.ndim() == 2) { - CHECK_GE(param.stride.ndim(), 2U); - CHECK_GE(param.pad.ndim(), 2U); - CHECK_GE(param.dilate.ndim(), 2U); + CHECK_GE(param.stride.ndim(), 2); + CHECK_GE(param.pad.ndim(), 2); + CHECK_GE(param.dilate.ndim(), 2); strides[0] = param.stride[0]; strides[1] = param.stride[1]; padding[0] = param.pad[0]; diff --git a/src/operator/nn/mkldnn/mkldnn_deconvolution.cc b/src/operator/nn/mkldnn/mkldnn_deconvolution.cc index 4da48fa3f83c..aec5d13c5de9 100644 --- a/src/operator/nn/mkldnn/mkldnn_deconvolution.cc +++ b/src/operator/nn/mkldnn/mkldnn_deconvolution.cc @@ -90,9 +90,9 @@ static mkldnn::convolution_backward_data::primitive_desc GetDeconvFwdImpl( auto weight_md = GetWeightDesc(weights, param.num_group); auto out_md = GetMemDesc(output); auto engine = CpuEngine::Get()->get_engine(); - CHECK_GE(param.stride.ndim(), 2U); - CHECK_GE(param.pad.ndim(), 2U); - CHECK_GE(param.dilate.ndim(), 2U); + CHECK_GE(param.stride.ndim(), 2); + CHECK_GE(param.pad.ndim(), 2); + CHECK_GE(param.dilate.ndim(), 2); mkldnn::memory::dims strides{0, 0}; strides[0] = param.stride[0]; strides[1] = param.stride[1]; @@ -128,9 +128,9 @@ static mkldnn::convolution_forward::primitive_desc GetDeconvBwdDataImpl( auto weight_md = GetWeightDesc(weights, param.num_group); auto out_md = GetMemDesc(output); auto engine = CpuEngine::Get()->get_engine(); - CHECK_GE(param.stride.ndim(), 2U); - CHECK_GE(param.pad.ndim(), 2U); - CHECK_GE(param.dilate.ndim(), 2U); + CHECK_GE(param.stride.ndim(), 2); + CHECK_GE(param.pad.ndim(), 2); + CHECK_GE(param.dilate.ndim(), 2); mkldnn::memory::dims strides{0, 0}; strides[0] = param.stride[0]; strides[1] = param.stride[1]; @@ -153,9 +153,9 @@ GetDeconvBwdWeightsImpl( auto weight_md = GetWeightDesc(weights, param.num_group); auto out_md = GetMemDesc(output); auto engine = CpuEngine::Get()->get_engine(); - CHECK_GE(param.stride.ndim(), 2U); - CHECK_GE(param.pad.ndim(), 2U); - CHECK_GE(param.dilate.ndim(), 2U); + CHECK_GE(param.stride.ndim(), 2); + CHECK_GE(param.pad.ndim(), 2); + CHECK_GE(param.dilate.ndim(), 2); mkldnn::memory::dims strides{0, 0}; strides[0] = param.stride[0]; strides[1] = param.stride[1]; diff --git a/src/operator/nn/pooling.cc b/src/operator/nn/pooling.cc index 3e081c9a0552..bc2b92e3fa9f 100644 --- a/src/operator/nn/pooling.cc +++ b/src/operator/nn/pooling.cc @@ -100,17 +100,17 @@ static bool PoolingShape(const nnvm::NodeAttrs &attrs, } const mxnet::TShape &dshape = (*in_shape)[0]; if (param.pooling_convention == pool_enum::kSame) { - CHECK_EQ(dshape.ndim(), 3U) + CHECK_EQ(dshape.ndim(), 3) << "Pooling: Input data should be 3D in (batch, channel, x)" << ". Currently 'same' supports Max Pooling 1-D"; CHECK(param.pad[0] == 0 && param.pad[1] == 0 && param.pad[2] == 0) << "Same pooling convention disables the use of pad parameter."; } - CHECK_GE(dshape.ndim(), 3U) + CHECK_GE(dshape.ndim(), 3) << "Pooling: Input data should be 3D in (batch, channel, x)" << " Or 4D in (batch, channel, y, x) " << " Or 5D in (batch, channel, d, y, x)"; - CHECK_LE(dshape.ndim(), 5U) + CHECK_LE(dshape.ndim(), 5) << "Pooling: Input data should be 3D in (batch, channel, x)" << " Or 4D in (batch, channel, y, x) " << " Or 5D in (batch, channel, d, y, x)"; diff --git a/src/operator/nn/upsampling.cc b/src/operator/nn/upsampling.cc index cb57b1b2d16f..5c260d166edf 100644 --- a/src/operator/nn/upsampling.cc +++ b/src/operator/nn/upsampling.cc @@ -60,7 +60,7 @@ static bool UpSamplingShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(in_shape->size(), 2U) << "Input:[data, weight]"; CHECK_EQ(dshape.ndim(), 4U) << \ "UpSamplingBilinear: Input data should be 4D in (batch, channel, y, x)"; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; int kernel = 2 * param_.scale - param_.scale % 2; SHAPE_ASSIGN_CHECK(*in_shape, up_enum::kWeight, diff --git a/src/operator/operator_util.cc b/src/operator/operator_util.cc index bc097a5b0c1c..fef066bd2c68 100644 --- a/src/operator/operator_util.cc +++ b/src/operator/operator_util.cc @@ -774,7 +774,7 @@ class SimpleUnaryOpProp : public SimpleOpPropBase { using namespace mshadow; CHECK_EQ(in_shape->size(), 1) << "Input:[data]"; const mxnet::TShape &dshape = in_shape->at(0); - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; out_shape->clear(); if (source->unary_shape_ == nullptr) { out_shape->push_back(dshape); diff --git a/src/operator/pooling_v1-inl.h b/src/operator/pooling_v1-inl.h index 4241b08a0c5e..efd211312093 100644 --- a/src/operator/pooling_v1-inl.h +++ b/src/operator/pooling_v1-inl.h @@ -243,9 +243,9 @@ class PoolingV1Prop : public OperatorProperty { mxnet::ShapeVector *aux_shape) const override { CHECK_EQ(in_shape->size(), 1U); const mxnet::TShape &dshape = (*in_shape)[0]; - CHECK_GE(dshape.ndim(), 4U) << "Pooling: Input data should be 4D in (batch, channel, y, x) " + CHECK_GE(dshape.ndim(), 4) << "Pooling: Input data should be 4D in (batch, channel, y, x) " << "Or 5D in (batch, channel, d, y, x)"; - CHECK_LE(dshape.ndim(), 5U) << "Pooling: Input data should be 4D in (batch, channel, y, x) " + CHECK_LE(dshape.ndim(), 5) << "Pooling: Input data should be 4D in (batch, channel, y, x) " << "Or 5D in (batch, channel, d, y, x)"; mxnet::TShape oshape = dshape; if (dshape.ndim() == -1) return false; diff --git a/src/operator/quantization/quantized_flatten-inl.h b/src/operator/quantization/quantized_flatten-inl.h index de051b969659..73b393e23727 100644 --- a/src/operator/quantization/quantized_flatten-inl.h +++ b/src/operator/quantization/quantized_flatten-inl.h @@ -86,7 +86,7 @@ inline bool QuantizedFlattenShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(out_attrs->size(), 3U); const mxnet::TShape &dshape = (*in_attrs)[0]; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; dim_t target_dim = 1; for (int i = 1; i < dshape.ndim(); ++i) { diff --git a/src/operator/random/sample_multinomial_op.h b/src/operator/random/sample_multinomial_op.h index b38aefbc1634..cfe6e764a4df 100644 --- a/src/operator/random/sample_multinomial_op.h +++ b/src/operator/random/sample_multinomial_op.h @@ -68,7 +68,7 @@ inline bool SampleMultinomialOpShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), param.get_prob ? 2U : 1U); const mxnet::TShape& ishape = (*in_attrs)[0]; - if (!shape_is_known(ishape)) return false; + if (!ndim_is_known(ishape)) return false; MSHADOW_TYPE_SWITCH(param.dtype, DType, { CHECK_LE(ishape[ishape.ndim() - 1], mxnet::common::MaxIntegerValue()) diff --git a/src/operator/regression_output-inl.h b/src/operator/regression_output-inl.h index d8f102de1675..0f54efc3a114 100644 --- a/src/operator/regression_output-inl.h +++ b/src/operator/regression_output-inl.h @@ -57,7 +57,7 @@ inline bool RegressionOpShape(const nnvm::NodeAttrs& attrs, using namespace mshadow; CHECK_EQ(in_attrs->size(), 2U) << "Input:[data, label]"; const mxnet::TShape &dshape = in_attrs->at(0); - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; auto &lshape = (*in_attrs)[1]; if (lshape.ndim() == 0) { // special treatment for 1D output, to allow 1D label by default. diff --git a/src/operator/softmax_output-inl.h b/src/operator/softmax_output-inl.h index 80ab40ef6c50..fab14d231b4c 100644 --- a/src/operator/softmax_output-inl.h +++ b/src/operator/softmax_output-inl.h @@ -337,7 +337,7 @@ class SoftmaxOutputProp : public OperatorProperty { using namespace mshadow; CHECK_EQ(in_shape->size(), 2U) << "Input:[data, label]"; const mxnet::TShape &dshape = in_shape->at(0); - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; // label.shape == data.shape: use probability as label if (dshape != (*in_shape)[softmaxout_enum::kLabel]) { diff --git a/src/operator/spatial_transformer-inl.h b/src/operator/spatial_transformer-inl.h index 660d57d55bab..f9a2a2585683 100644 --- a/src/operator/spatial_transformer-inl.h +++ b/src/operator/spatial_transformer-inl.h @@ -190,10 +190,10 @@ class SpatialTransformerProp : public OperatorProperty { CHECK_EQ(param_.sampler_type, st::kBilinear) << "only supports bilinear sampling currently"; const mxnet::TShape &dshape = (*in_shape)[st::kData]; const mxnet::TShape &lshape = (*in_shape)[st::kLoc]; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; CHECK_EQ(dshape.ndim(), 4U) \ << "input data should be 4D in batch-num_filter-y-x"; - if (!shape_is_known(lshape)) return false; + if (!ndim_is_known(lshape)) return false; CHECK_EQ(lshape.ndim(), 2U) \ << "locolisation paramter should be 4D in batch-num_hidden"; if (param_.transform_type == st::kAffine) { diff --git a/src/operator/tensor/broadcast_reduce_op.h b/src/operator/tensor/broadcast_reduce_op.h index 9fec6cd1255a..9be21534d1f7 100644 --- a/src/operator/tensor/broadcast_reduce_op.h +++ b/src/operator/tensor/broadcast_reduce_op.h @@ -222,7 +222,7 @@ inline bool ReduceAxisShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); mxnet::TShape& ishape = (*in_attrs)[0]; - if (!shape_is_known(ishape)) return false; + if (!ndim_is_known(ishape)) return false; const ReduceAxisParam& param = nnvm::get(attrs.parsed); SHAPE_ASSIGN_CHECK(*out_attrs, 0, @@ -304,7 +304,7 @@ inline bool ReduceAxesShape(const nnvm::NodeAttrs& attrs, mxnet::ShapeVector *out_attrs) { CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); - if (!shape_is_known((*in_attrs)[0])) return false; + if (!ndim_is_known((*in_attrs)[0])) return false; const ReduceAxesParam& param = nnvm::get(attrs.parsed); SHAPE_ASSIGN_CHECK(*out_attrs, 0, ReduceAxesShapeImpl((*in_attrs)[0], param.axis, @@ -317,7 +317,7 @@ inline bool ReduceMinMaxAxesShape(const nnvm::NodeAttrs& attrs, mxnet::ShapeVector *out_attrs) { CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); - if (!shape_is_known((*in_attrs)[0])) return false; + if (!ndim_is_known((*in_attrs)[0])) return false; CHECK_GT((*in_attrs)[0].Size(), 0U) << "Reduction input's size should > 0 " << (*in_attrs)[0]; @@ -351,7 +351,7 @@ inline bool NormShape(const nnvm::NodeAttrs& attrs, mxnet::ShapeVector *out_attrs) { CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); - if (!shape_is_known((*in_attrs)[0])) return false; + if (!ndim_is_known((*in_attrs)[0])) return false; const NormParam& param = nnvm::get(attrs.parsed); SHAPE_ASSIGN_CHECK(*out_attrs, 0, ReduceAxesShapeImpl((*in_attrs)[0], param.axis, @@ -364,7 +364,7 @@ inline bool BroadcastAxesShape(const nnvm::NodeAttrs& attrs, mxnet::ShapeVector *out_attrs) { CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); - if (!shape_is_known((*in_attrs)[0])) return false; + if (!ndim_is_known((*in_attrs)[0])) return false; const BroadcastAxesParam& param = nnvm::get(attrs.parsed); CHECK_EQ(param.axis.ndim() , param.size.ndim()); mxnet::TShape &ishape = (*in_attrs)[0]; diff --git a/src/operator/tensor/dot-inl.h b/src/operator/tensor/dot-inl.h index f81eb9c04f3a..8bbd8df0ab33 100644 --- a/src/operator/tensor/dot-inl.h +++ b/src/operator/tensor/dot-inl.h @@ -1207,6 +1207,7 @@ inline bool DotShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(out_attrs->size(), 1U); mxnet::TShape& lshape = (*in_attrs)[0]; mxnet::TShape& rshape = (*in_attrs)[1]; + if (!ndim_is_known(lshape) || !ndim_is_known(rshape)) return false; if (lshape.ndim() == 1 && rshape.ndim() == 1) { CHECK(!param.transpose_a && !param.transpose_b) << "Cannot transpose vectors"; CHECK_EQ(lshape[0], rshape[0]) << "dot shape error: " << lshape << " X " << rshape; @@ -1479,6 +1480,7 @@ inline bool BatchDotShape(const nnvm::NodeAttrs& attrs, const DotParam& param = nnvm::get(attrs.parsed); mxnet::TShape& lshape = (*in_attrs)[0]; mxnet::TShape& rshape = (*in_attrs)[1]; + if (!ndim_is_known(lshape) || !ndim_is_known(rshape)) return false; if (lshape.ndim() == 3 && rshape.ndim() == 3) { CHECK(lshape[0] == rshape[0]) << "batch_dot shape error(batch_size must be equal): " << lshape << " X " << rshape diff --git a/src/operator/tensor/elemwise_binary_broadcast_op.h b/src/operator/tensor/elemwise_binary_broadcast_op.h index 73019fa8389b..f84767dd4b2f 100644 --- a/src/operator/tensor/elemwise_binary_broadcast_op.h +++ b/src/operator/tensor/elemwise_binary_broadcast_op.h @@ -452,7 +452,7 @@ void BinaryBroadcastComputeSparseEx(const nnvm::NodeAttrs& attrs, CHECK_EQ(inputs.size(), 2U); CHECK_EQ(outputs.size(), 1U); CHECK_EQ(req.size(), 1U); - CHECK_LE(inputs[1].shape().ndim(), 2U) + CHECK_LE(inputs[1].shape().ndim(), 2) << "input dense matrix should have less than or equal to 2 dimensions"; if (req[0] == kNullOp) return; const NDArray& lhs = inputs[0]; @@ -488,7 +488,7 @@ void BinaryBroadcastComputeDenseEx(const nnvm::NodeAttrs& attrs, CHECK_EQ(inputs.size(), 2U); CHECK_EQ(outputs.size(), 1U); CHECK_EQ(req.size(), 1U); - CHECK_LE(inputs[1].shape().ndim(), 2U) + CHECK_LE(inputs[1].shape().ndim(), 2) << "input dense matrix should have less than or equal to 2 dimensions"; if (req[0] == kNullOp) return; const NDArray& lhs = inputs[0]; diff --git a/src/operator/tensor/indexing_op.h b/src/operator/tensor/indexing_op.h index e8c5e884588b..fc1fd41208f2 100644 --- a/src/operator/tensor/indexing_op.h +++ b/src/operator/tensor/indexing_op.h @@ -145,7 +145,7 @@ inline bool EmbeddingOpShape(const nnvm::NodeAttrs& attrs, mxnet::ShapeVector *out_attrs) { using namespace mshadow; const mxnet::TShape &dshape = (*in_attrs)[embedding::kData]; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; const ParamType& param = nnvm::get(attrs.parsed); SHAPE_ASSIGN_CHECK(*in_attrs, embedding::kWeight, Shape2(param.input_dim, param.output_dim)); @@ -682,7 +682,7 @@ inline bool TakeOpShape(const nnvm::NodeAttrs& attrs, using namespace mshadow; const mxnet::TShape &arrshape = (*in_attrs)[take_::kArr]; const mxnet::TShape &idxshape = (*in_attrs)[take_::kIdx]; - if (!shape_is_known(idxshape)) return false; + if (!ndim_is_known(idxshape)) return false; const TakeParam& param = nnvm::get(attrs.parsed); if (param.mode == take_::kRaise) { LOG(FATAL) << "Raise is not supported for the time being..."; @@ -1075,7 +1075,7 @@ inline bool BatchTakeOpShape(const nnvm::NodeAttrs& attrs, SHAPE_ASSIGN_CHECK(*in_attrs, 1, (*out_attrs)[0]); } if ((*in_attrs)[0].ndim() == 0) return false; - CHECK_GE((*in_attrs)[0].ndim(), 2U) << "Data array must have at least 2 dimensional"; + CHECK_GE((*in_attrs)[0].ndim(), 2) << "Data array must have at least 2 dimensional"; if ((*out_attrs)[0].ndim() == 0) return false; CHECK_EQ((*in_attrs)[0].Size()/(*in_attrs)[0][(*in_attrs)[0].ndim()-1], (*out_attrs)[0].Size()) @@ -1170,7 +1170,7 @@ inline bool OneHotOpShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(out_attrs->size(), 1U); // The shape of indices const mxnet::TShape& ishape = (*in_attrs)[0]; - if (!shape_is_known(ishape)) return false; + if (!ndim_is_known(ishape)) return false; int depth = 0; double on_value = 1.0; diff --git a/src/operator/tensor/matrix_op-inl.h b/src/operator/tensor/matrix_op-inl.h index e99741b70bb6..50bfbb90acea 100644 --- a/src/operator/tensor/matrix_op-inl.h +++ b/src/operator/tensor/matrix_op-inl.h @@ -229,7 +229,7 @@ inline bool FlattenShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(in_attrs->size(), 1U) << "Input: [data]"; CHECK_EQ(out_attrs->size(), 1U); const mxnet::TShape &dshape = (*in_attrs)[0]; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; int target_dim = 1; for (int i = 1; i < dshape.ndim(); ++i) { target_dim *= dshape[i]; @@ -337,7 +337,7 @@ inline bool TransposeShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); mxnet::TShape& shp = (*in_attrs)[0]; - CHECK_LE(shp.ndim(), 6U) << "Transpose support at most 6 dimensions"; + CHECK_LE(shp.ndim(), 6) << "Transpose support at most 6 dimensions"; mxnet::TShape ret(shp.ndim(), -1); if (param.axes.ndim() == 0) { for (int i = 0; i < shp.ndim(); ++i) { @@ -1025,7 +1025,7 @@ inline bool SliceAssignScalarOpShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(in_attrs->size(), 1U); CHECK_EQ(out_attrs->size(), 1U); const mxnet::TShape& dshape = (*in_attrs)[0]; - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; SHAPE_ASSIGN_CHECK(*out_attrs, 0, dshape); return true; } @@ -2067,7 +2067,7 @@ inline bool StackOpShape(const nnvm::NodeAttrs& attrs, for (const mxnet::TShape& i : (*in_attrs)) { shape_assign(&dshape, i); } - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; mxnet::TShape oshape(dshape.ndim() + 1, -1); int axis = CheckAxis(param.axis, oshape.ndim()); @@ -2187,7 +2187,7 @@ inline bool SqueezeShape(const nnvm::NodeAttrs& attrs, CHECK_EQ(out_attrs->size(), 1U); const mxnet::TShape& dshape = in_attrs->at(0); const int dndim = dshape.ndim(); - if (!shape_is_known(dshape)) return false; + if (!ndim_is_known(dshape)) return false; mxnet::TShape oshape = dshape; if (param.axis.has_value()) { // preprocess axis diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index e8bfaba4736d..d6c9cad7ac0e 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -1097,6 +1097,18 @@ def test_embedding(): exe_test.backward([grad]) assert_almost_equal(grad_map["embed_weight"].asnumpy(), np.dot(np_onehot.T, np_grad), rtol=rtol, atol=atol) +@with_seed() +def test_embedding_partial_shape(): + # testing embedding with batch size unknown + x = mx.sym.Variable("x") + w = mx.sym.Variable("w") + y = mx.sym.Embedding(data=x, weight=w, input_dim=100, output_dim=10) + _, result_shape, _ = y.infer_shape_partial(x=(0, 5), w=(100, 10)) + assert result_shape == [(0, 5, 10)] + + with mx.np_compat(True): + _, result_shape, _ = y.infer_shape_partial(x=(-1, 5), w=(100, 10)) + assert result_shape == [(-1, 5, 10)] # check ops handle duplicate input correctly. @with_seed() @@ -2573,6 +2585,20 @@ def test_transpose(): y = mx.nd.transpose(x) assert_allclose(np.transpose(x.asnumpy()), y.asnumpy()) +@with_seed() +def test_transpose_partial_shape(): + # test converting tensor shape + # from channels first to channels last + # with batch size unknown + axes = [0, 3, 2, 1] + x = mx.sym.Variable("x") + y = mx.sym.transpose(x, axes=axes) + _, result, _ = y.infer_shape_partial(x=(0, 3, 224, 224)) + assert result == [(0, 224, 224, 3)] + + with mx.np_compat(True): + _, result, _ = y.infer_shape_partial(x=(-1, 3, 224, 224)) + assert result == [(-1, 224, 224, 3)] @with_seed() def test_expand_dims():