Repository: ivy-llc/ivy

Issue:
local_response_norm



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/torch/nn/functional/non_linear_activation_functions.py b/ivy/functional/frontends/torch/nn/functional/non_linear_activation_functions.py
--- a/ivy/functional/frontends/torch/nn/functional/non_linear_activation_functions.py
+++ b/ivy/functional/frontends/torch/nn/functional/non_linear_activation_functions.py
@@ -282,6 +282,40 @@ def normalize(input, p=2.0, dim=1, eps=1e-12, out=None):
     return ivy.divide(input, max_, out=out)
 
 
+@to_ivy_arrays_and_back
+def local_response_norm(input, size, alpha=0.0001, beta=0.75, k=1.0):
+    dim = len(ivy.shape(input))
+    if dim < 3:
+        raise ValueError(
+            "Expected 3D or higher dimensionality input (got {} dimensions)".format(dim)
+        )
+    if input.size == 0:
+        return input
+    div = ivy.multiply(input, input)
+
+    if dim == 3:
+        div = ivy.expand_dims(div, axis=1)
+        div = ivy.zero_pad(div, ((0, 0), (0, 0), (size // 2, (size - 1) // 2), (0, 0)))
+        div = ivy.avg_pool2d(
+            div, (size, 1), 1, "VALID", count_include_pad=True, data_format="NCHW"
+        )
+        div = ivy.squeeze(div, axis=1)
+    else:
+        sizes = ivy.shape(input)
+        div = ivy.reshape(div, (sizes[0], 1, sizes[1], sizes[2], -1))
+        div = ivy.zero_pad(
+            div, ((0, 0), (0, 0), (size // 2, (size - 1) // 2), (0, 0), (0, 0))
+        )
+        div = ivy.avg_pool3d(
+            div, (size, 1, 1), 1, "VALID", count_include_pad=True, data_format="NCDHW"
+        )
+        div = ivy.squeeze(div, axis=1)
+        div = ivy.reshape(div, sizes)
+
+    div = ivy.pow(ivy.add(ivy.multiply(div, alpha), k), beta)
+    return ivy.divide(input, div)
+
+
 @to_ivy_arrays_and_back
 @with_unsupported_dtypes(
     {


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_non_linear_activation_functions.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_non_linear_activation_functions.py
--- a/ivy_tests/test_ivy/test_frontends/test_torch/test_non_linear_activation_functions.py
+++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_non_linear_activation_functions.py
@@ -1048,6 +1048,48 @@ def test_torch_normalize(
     )
 
 
+# local_response_norm
+@handle_frontend_test(
+    fn_tree="torch.nn.functional.local_response_norm",
+    dtype_x_and_axis=helpers.dtype_values_axis(
+        available_dtypes=helpers.get_dtypes("float"),
+        min_num_dims=3,
+        force_int_axis=True,
+        valid_axis=True,
+    ),
+    size=helpers.ints(min_value=3, max_value=10),
+    alpha=helpers.floats(min_value=1e-4, max_value=1e-3),
+    beta=helpers.floats(min_value=0.5, max_value=2.0),
+    k=helpers.ints(min_value=0, max_value=1),
+)
+def test_torch_local_response_norm(
+    *,
+    dtype_x_and_axis,
+    size,
+    alpha,
+    beta,
+    k,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+):
+    dtype, x, axis = dtype_x_and_axis
+    _filter_dtypes(dtype)
+    helpers.test_frontend_function(
+        input_dtypes=dtype,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        input=x[0],
+        size=size,
+        alpha=alpha,
+        beta=beta,
+        k=k,
+    )
+
+
 # softplus
 @handle_frontend_test(
     fn_tree="torch.nn.functional.softplus",