diff --git a/paddle/fluid/operators/bilateral_slice_op.cc b/paddle/fluid/operators/bilateral_slice_op.cc index b742b4c0deea89dacd29a02588236b81ac13f6af..b00604155d67e1323a1f38fb8f13f34d706d6db0 100644 --- a/paddle/fluid/operators/bilateral_slice_op.cc +++ b/paddle/fluid/operators/bilateral_slice_op.cc @@ -50,20 +50,25 @@ class BilateralSliceOp : public framework::OperatorWithKernel { int64_t input_chans = input_dims[1]; int64_t output_chans; - if (has_offset) { - PADDLE_ENFORCE_EQ((coeffs_chans % (input_chans + 1)), 0, - platform::errors::InvalidArgument( - "Slicing with affine offset, coefficients grid " - "should have n_out*(n_in+1) channels, but got %d", - coeffs_chans)); - output_chans = coeffs_chans / (input_chans + 1); + if ((!ctx->IsRuntime()) && ((coeffs_chans < 0) || (input_chans < 0))) { + output_chans = -1; } else { - PADDLE_ENFORCE_EQ((coeffs_chans % input_chans), 0, - platform::errors::InvalidArgument( - "Slicing without affine offset, coefficients grid " - "should have n_out*n_in channels, but got %d .", - coeffs_chans)); - output_chans = coeffs_chans / input_chans; + if (has_offset) { + PADDLE_ENFORCE_EQ((coeffs_chans % (input_chans + 1)), 0, + platform::errors::InvalidArgument( + "Slicing with affine offset, coefficients grid " + "should have n_out*(n_in+1) channels, but got %d", + coeffs_chans)); + output_chans = coeffs_chans / (input_chans + 1); + } else { + PADDLE_ENFORCE_EQ( + (coeffs_chans % input_chans), 0, + platform::errors::InvalidArgument( + "Slicing without affine offset, coefficients grid " + "should have n_out*n_in channels, but got %d .", + coeffs_chans)); + output_chans = coeffs_chans / input_chans; + } } std::vector output_dims; diff --git a/python/paddle/fluid/contrib/layers/nn.py b/python/paddle/fluid/contrib/layers/nn.py index ac6493b1c2969a8c3319bc8d29983b0ccc3a67d9..d0543bb90dd14b484ce2fb36206885d2de79621e 100644 --- a/python/paddle/fluid/contrib/layers/nn.py +++ b/python/paddle/fluid/contrib/layers/nn.py @@ -1525,10 +1525,10 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): grid = fluid.data(name='grid', shape=[None, 12, 8, 10, 6], dtype='float32') # without offset - output = fluid.layers.bilateral_slice(x, guide, grid, has_offset=False) + output = fluid.contrib.bilateral_slice(x, guide, grid, has_offset=False) # has offset - output = fluid.layers.bilateral_slice(x, guide, grid, has_offset=True) + output = fluid.contrib.bilateral_slice(x, guide, grid, has_offset=True) """ helper = LayerHelper("bilateral_slice", **locals()) @@ -1541,7 +1541,9 @@ def bilateral_slice(x, guide, grid, has_offset, name=None): out = helper.create_variable_for_type_inference(x.dtype) inputs = {'X': x, 'Guide': guide, 'Grid': grid} - + if paddle.fluid.in_dygraph_mode(): + attrs = ('has_offset', has_offset) + return getattr(core.ops, "bilateral_slice")(x, grid, guide, *attrs) helper.append_op( type='bilateral_slice', inputs=inputs, diff --git a/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py b/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py index 51e447dba725c03ad7eea5c94c2be70cc8ea9a7a..c0d622d7ea18753c1e1fba2f81bd883354586be6 100644 --- a/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py +++ b/python/paddle/fluid/tests/unittests/test_bilateral_slice_op.py @@ -178,16 +178,25 @@ class TestBilateralSliceOp1(TestBilateralSliceOp): self.data_type = 'float32' -class TestBilateralSliceApi(TestBilateralSliceOp): +class TestBilateralSliceApi(unittest.TestCase): def test_api(self): x = paddle.fluid.data( name='x', shape=[None, 3, 25, 15], dtype='float32') guide = paddle.fluid.data( name='guide', shape=[None, 25, 15], dtype='float32') grid = paddle.fluid.data( - name='grid', shape=[None, 12, 8, 5, 3], dtype='float32') - paddle.fluid.contrib.layers.bilateral_slice(x, guide, grid, - self.has_offset) + name='grid', shape=[None, None, 8, 5, 3], dtype='float32') + paddle.fluid.contrib.layers.bilateral_slice(x, guide, grid, False) + + if not paddle.fluid.is_compiled_with_cuda(): + return + + with paddle.fluid.dygraph.guard(): + x1 = paddle.rand([3, 1, 50, 30]) + guide1 = paddle.rand([3, 50, 30]) + grid1 = paddle.rand([3, 2, 2, 5, 3]) + + paddle.fluid.contrib.bilateral_slice(x1, guide1, grid1, False) if __name__ == "__main__":