Repository: ivy-llc/ivy

Issue:
adaptive_avg_pool2d



Discussion:

Resolving patch:
diff --git a/ivy/stateful/layers.py b/ivy/stateful/layers.py
--- a/ivy/stateful/layers.py
+++ b/ivy/stateful/layers.py
@@ -1767,3 +1767,43 @@ def _forward(self, x):
             ceil_mode=self._ceil_mode,
             divisor_override=self._divisor_override,
         )
+
+
+class AdaptiveAvgPool2d(Module):
+    def __init__(
+        self,
+        output_size,
+        device=None,
+        dtype=None,
+    ):
+        """
+        Class for applying a 2D adaptive average pooling over mini-batch of inputs.
+
+        Parameters
+        ----------
+        output_size
+            the target output size of the image.
+        device
+            device on which to create the layer's variables 'cuda:0', 'cuda:1', 'cpu'
+        """
+        self._output_size = output_size
+        Module.__init__(self, device=device, dtype=dtype)
+
+    def _forward(self, x):
+        """
+        Forward pass of the layer.
+
+        Parameters
+        ----------
+        x
+            The input array to the layer.
+
+        Returns
+        -------
+            The output array of the layer.
+        """
+        #TODO: test again once adaptive_avg_pool2d is implemnted for the missing backends. 
+        return ivy.adaptive_avg_pool2d(
+            x,
+            self._output_size,
+        )


Test patch:
diff --git a/ivy_tests/test_ivy/test_stateful/test_layers.py b/ivy_tests/test_ivy/test_stateful/test_layers.py
--- a/ivy_tests/test_ivy/test_stateful/test_layers.py
+++ b/ivy_tests/test_ivy/test_stateful/test_layers.py
@@ -1282,3 +1282,65 @@ def test_maxpool3d_layer(
         test_gradients=test_gradients,
         on_device=on_device,
     )
+
+
+# AdaptiveAveragePool2d
+@st.composite
+def array_for_adaptive(
+    draw,
+    num_dims=3,
+    max_dim_size=8,
+    min_dim_size=3,
+    num_out_size=2,
+):
+    dtypes, arrays = draw(
+        helpers.dtype_and_values(
+            available_dtypes=helpers.get_dtypes("float"),
+            min_num_dims=num_dims,
+            max_num_dims=num_dims,
+            min_dim_size=min_dim_size,
+            max_dim_size=max_dim_size,
+        )
+    )
+    size = draw(
+        helpers.list_of_size(
+            x=helpers.ints(min_value=3, max_value=5),
+            size=num_out_size,
+        )
+    )
+    output_size = size[0] if num_out_size == 1 else size
+    return dtypes, arrays, output_size
+
+
+@handle_method(
+    method_tree="AdaptiveAvgPool2d.__call__",
+    dt_arr_size=array_for_adaptive(),
+)
+def test_adaptive_avg_pool2d_layer(
+    *,
+    dt_arr_size,
+    test_gradients,
+    on_device,
+    class_name,
+    method_name,
+    ground_truth_backend,
+    init_flags,
+    method_flags,
+):
+    input_dtype, x, out_size = dt_arr_size
+    helpers.test_method(
+        ground_truth_backend=ground_truth_backend,
+        init_flags=init_flags,
+        method_flags=method_flags,
+        init_all_as_kwargs_np={
+            "output_size": out_size,
+            "device": on_device,
+            "dtype": input_dtype[0],
+        },
+        method_input_dtypes=input_dtype,
+        method_all_as_kwargs_np={"x": x[0]},
+        class_name=class_name,
+        method_name=method_name,
+        test_gradients=test_gradients,
+        on_device=on_device,
+    )