Repository: ivy-llc/ivy

Issue:
conv1d

conv1d_transpose



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/paddle/nn/functional/conv.py b/ivy/functional/frontends/paddle/nn/functional/conv.py
--- a/ivy/functional/frontends/paddle/nn/functional/conv.py
+++ b/ivy/functional/frontends/paddle/nn/functional/conv.py
@@ -1 +1,179 @@
 # local
+import ivy
+from ivy.func_wrapper import with_supported_dtypes
+from ivy.functional.frontends.torch.func_wrapper import to_ivy_arrays_and_back
+from ivy.functional.frontends.torch.nn.functional import convolution_functions
+
+
+def _channel_first_input(x, data_format):
+    ndims = len(x.shape)
+    dims = ndims - 2
+    if 1 >= dims >= 3:
+        raise ivy.utils.exceptions.IvyError(f"invalid for input with {dims} dims")
+    # channel first input
+    if data_format not in ["NCL", "NCHW", "NCDHW"]:
+        if data_format in ["NLC", "NHWC", "NDHWC"]:
+            x = ivy.permute_dims(x, axes=(0, ndims - 1, *range(1, ndims - 1)))
+        else:
+            raise ivy.utils.exceptions.IvyError(
+                "data_format should be " + "'NCL' or 'NLC' "
+                if dims == 1
+                else (
+                    "'NCHW' or 'NHWC' "
+                    if dims == 2
+                    else "'NCDHW' or 'NDHWC' " + f"but got data_format '{data_format}'"
+                )
+            )
+    return x
+
+
+def _conv(
+    x, weight, bias=None, stride=1, padding=0, dilation=1, groups=1, data_format="NLC"
+):
+    x = _channel_first_input(x, data_format)
+
+    if padding == 'same':
+        dilation = 1
+    return convolution_functions._conv(
+        x,
+        weight,
+        bias=bias,
+        stride=stride,
+        padding=padding,
+        dilation=dilation,
+        groups=groups,
+    )
+
+
+def _conv_transpose(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    output_padding=0,
+    dilation=1,
+    groups=1,
+    data_format="NLC",
+):
+    x = _channel_first_input(x, data_format)
+    if padding == 'same':
+        dilation = 1
+    return convolution_functions._conv_transpose(
+        x,
+        weight,
+        bias=bias,
+        stride=stride,
+        padding=padding,
+        output_padding=output_padding,
+        groups=groups,
+        dilation=dilation,
+    )
+
+
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+@to_ivy_arrays_and_back
+def conv1d(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    dilation=1,
+    groups=1,
+    data_format="NCL",
+    name=None,
+):
+    return _conv(x, weight, bias, stride, padding, dilation, groups, data_format)
+
+
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+@to_ivy_arrays_and_back
+def conv2d(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    dilation=1,
+    groups=1,
+    data_format="NCHW",
+    name=None,
+):
+    return _conv(x, weight, bias, stride, padding, dilation, groups, data_format)
+
+
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+@to_ivy_arrays_and_back
+def conv3d(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    dilation=1,
+    groups=1,
+    data_format="NCDHW",
+    name=None,
+):
+    return _conv(x, weight, bias, stride, padding, dilation, groups, data_format)
+
+
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+@to_ivy_arrays_and_back
+def conv1d_transpose(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    output_padding=0,
+    groups=1,
+    dilation=1,
+    output_size=None,
+    data_format="NCL",
+    name=None,
+):
+    return _conv_transpose(
+        x, weight, bias, stride, padding, output_padding, dilation, groups, data_format
+    )
+
+
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+@to_ivy_arrays_and_back
+def conv2d_transpose(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    output_padding=0,
+    dilation=1,
+    groups=1,
+    output_size=None,
+    data_format="NCHW",
+    name=None,
+):
+    return _conv_transpose(
+        x, weight, bias, stride, padding, output_padding, dilation, groups, data_format
+    )
+
+
+@with_supported_dtypes({"2.5.1 and below": ("float32", "float64")}, "paddle")
+@to_ivy_arrays_and_back
+def conv3d_transpose(
+    x,
+    weight,
+    bias=None,
+    stride=1,
+    padding=0,
+    output_padding=0,
+    groups=1,
+    dilation=1,
+    output_size=None,
+    data_format="NCDHW",
+    name=None,
+):
+    return _conv_transpose(
+        x, weight, bias, stride, padding, output_padding, dilation, groups, data_format
+    )


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_conv.py b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_conv.py
--- a/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_conv.py
+++ b/ivy_tests/test_ivy/test_frontends/test_paddle/test_nn/test_functional/test_conv.py
@@ -1,3 +1,237 @@
 # global
+from hypothesis import assume
 
 # local
+import ivy_tests.test_ivy.helpers as helpers
+from ivy_tests.test_ivy.helpers import handle_frontend_test
+from ivy_tests.test_ivy.test_frontends.test_torch.test_nn.test_functional.test_convolution_functions import (
+    x_and_filters,
+    _output_shape,
+)
+from ivy_tests.test_ivy.test_functional.test_nn.test_layers import (
+    _assume_tf_dilation_gt_1,
+)
+
+
+# conv1d
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.conv1d",
+    dtype_vals=x_and_filters(dim=1),
+)
+def test_paddle_conv1d(
+    *,
+    dtype_vals,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    dtype, vals, weight, bias, dilations, strides, padding, fc = dtype_vals
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=vals,
+        weight=weight,
+        bias=bias,
+        stride=strides,
+        padding=padding,
+        dilation=dilations,
+        groups=fc,
+    )
+
+
+# conv2d
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.conv2d",
+    dtype_vals=x_and_filters(dim=2),
+)
+def test_paddle_conv2d(
+    *,
+    dtype_vals,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    dtype, vals, weight, bias, dilations, strides, padding, fc = dtype_vals
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=vals,
+        weight=weight,
+        bias=bias,
+        stride=strides,
+        padding=padding,
+        dilation=dilations,
+        groups=fc,
+    )
+
+
+# conv3d
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.conv3d",
+    dtype_vals=x_and_filters(dim=3),
+)
+def test_paddle_conv3d(
+    *,
+    dtype_vals,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    dtype, vals, weight, bias, dilations, strides, padding, fc = dtype_vals
+    # ToDo: Enable gradient tests for dilations > 1 when tensorflow supports it.
+    _assume_tf_dilation_gt_1(backend_fw, on_device, dilations)
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=vals,
+        weight=weight,
+        bias=bias,
+        stride=strides,
+        padding=padding,
+        dilation=dilations,
+        groups=fc,
+    )
+
+
+# conv1d_transpose
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.conv1d_transpose",
+    dtype_vals=x_and_filters(dim=1, transpose=True),
+)
+def test_paddle_conv1d_tranpose(
+    *,
+    dtype_vals,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    dtype, vals, weight, bias, dilations, strides, padding, output_pad, fc = dtype_vals
+    dilations = 1  # ToDo: remove this when support for dilation > 1 is added
+    assume(
+        all(
+            x > 0
+            for x in _output_shape(
+                1, dilations, strides, padding, output_pad, vals.shape, weight.shape
+            )
+        )
+    )
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=vals,
+        weight=weight,
+        bias=bias,
+        stride=strides,
+        padding=padding,
+        output_padding=output_pad,
+        groups=fc,
+        dilation=dilations,
+    )
+
+
+# conv2d_transpose
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.conv2d_transpose",
+    dtype_vals=x_and_filters(dim=2, transpose=True),
+)
+def test_paddle_conv2d_tranpose(
+    *,
+    dtype_vals,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    dtype, vals, weight, bias, dilations, strides, padding, output_pad, fc = dtype_vals
+    dilations = 1  # ToDo: remove this when support for dilation > 1 is added
+    assume(
+        all(
+            x > 0
+            for x in _output_shape(
+                2, dilations, strides, padding, output_pad, vals.shape, weight.shape
+            )
+        )
+    )
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=vals,
+        weight=weight,
+        bias=bias,
+        stride=strides,
+        padding=padding,
+        output_padding=output_pad,
+        dilation=dilations,
+        groups=fc,
+    )
+
+
+# conv3d_transpose
+@handle_frontend_test(
+    fn_tree="paddle.nn.functional.conv3d_transpose",
+    dtype_vals=x_and_filters(dim=3, transpose=True),
+)
+def test_paddle_conv3d_tranpose(
+    *,
+    dtype_vals,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    dtype, vals, weight, bias, dilations, strides, padding, output_pad, fc = dtype_vals
+    dilations = 1  # ToDo: remove this when support for dilation > 1 is added
+    assume(
+        all(
+            x > 0
+            for x in _output_shape(
+                3, dilations, strides, padding, output_pad, vals.shape, weight.shape
+            )
+        )
+    )
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        x=vals,
+        weight=weight,
+        bias=bias,
+        stride=strides,
+        padding=padding,
+        output_padding=output_pad,
+        groups=fc,
+        dilation=dilations,
+    )
diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_convolution_functions.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_convolution_functions.py
--- a/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_convolution_functions.py
+++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_convolution_functions.py
@@ -214,7 +214,7 @@ def test_torch_conv3d(
 ):
     dtype, vals, weight, bias, dilations, strides, padding, fc = dtype_vals
     # ToDo: Enable gradient tests for dilations > 1 when tensorflow supports it.
-    _assume_tf_dilation_gt_1(ivy.current_backend_str(), on_device, dilations)
+    _assume_tf_dilation_gt_1(backend_fw, on_device, dilations)
     helpers.test_frontend_function(
         input_dtypes=dtype,
         backend_to_test=backend_fw,