Repository: ivy-llc/ivy

Issue:
alpha_dropout



Discussion:
Assigning myself to this issue. Will start working on it shortly.
Actually I'm working on it, this is just the standard procedure to assign yourself to the sub-task
I apologize for attempting to assign myself. I just realized you're already working on it. Sorry for any confusion caused.

Resolving patch:
diff --git a/ivy/functional/frontends/torch/nn/functional/dropout_functions.py b/ivy/functional/frontends/torch/nn/functional/dropout_functions.py
--- a/ivy/functional/frontends/torch/nn/functional/dropout_functions.py
+++ b/ivy/functional/frontends/torch/nn/functional/dropout_functions.py
@@ -11,6 +11,27 @@ def dropout(input, p=0.5, training=True, inplace=False):
     return ivy.dropout(input, p, training=training)
 
 
+# ToDo: this function will be simplified once ivy.alpha_dropout is implemented
+@to_ivy_arrays_and_back
+@with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch")
+def alpha_dropout(input, p=0.5, training=False, inplace=False):
+    if p == 0.0 or not training or input.shape == () or input.shape == (0,):
+        return input
+    neg_saturation = ivy.log1p(ivy.exp(-ivy.square(input)))
+    mask = ivy.where(
+        ivy.random_uniform(shape=input.shape, device=ivy.dev(input)) < p,
+        0.0,
+        1.0,
+    )
+    if inplace:
+        ivy.inplace_update(input, mask * input + (1 - mask) * neg_saturation)
+        ivy.inplace_update(input, input / ivy.sqrt(1 - p / (1 - p + 1e-5)))
+        return input
+    else:
+        masked = mask * input + (1 - mask) * neg_saturation
+        return masked / ivy.sqrt(1 - p / (1 - p + 1e-5))
+
+
 @to_ivy_arrays_and_back
 @with_unsupported_dtypes({"2.0.1 and below": ("float16",)}, "torch")
 def dropout1d(input, p=0.5, training=True, inplace=False):


Test patch:
diff --git a/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_dropout_functions.py b/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_dropout_functions.py
--- a/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_dropout_functions.py
+++ b/ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_dropout_functions.py
@@ -53,6 +53,52 @@ def test_torch_dropout(
         assert u.shape == x.shape
 
 
+@handle_frontend_test(
+    fn_tree="torch.nn.functional.alpha_dropout",
+    dtype_and_x=helpers.dtype_and_values(
+        available_dtypes=helpers.get_dtypes("float"),
+        min_value=0,
+        max_value=50,
+        allow_inf=False,
+        min_num_dims=1,
+        max_num_dims=1,
+        min_dim_size=2,
+    ),
+    prob=helpers.floats(min_value=0, max_value=0.9),
+    training=st.booleans(),
+    test_inplace=st.just(False),
+)
+def test_torch_alpha_dropout(
+    *,
+    dtype_and_x,
+    prob,
+    training,
+    on_device,
+    fn_tree,
+    frontend,
+    test_flags,
+    backend_fw,
+):
+    input_dtype, x = dtype_and_x
+    ret = helpers.test_frontend_function(
+        input_dtypes=input_dtype,
+        backend_to_test=backend_fw,
+        frontend=frontend,
+        test_flags=test_flags,
+        fn_tree=fn_tree,
+        on_device=on_device,
+        input=x[0],
+        p=prob,
+        training=training,
+        test_values=False,
+    )
+    ret = helpers.flatten_and_to_np(ret=ret, backend=backend_fw)
+    x = np.asarray(x[0], input_dtype[0])
+    for u in ret:
+        # cardinality test
+        assert u.shape == x.shape
+
+
 @handle_frontend_test(
     fn_tree="torch.nn.functional.dropout1d",
     dtype_and_x=helpers.dtype_and_values(