Repository: ivy-llc/ivy

Issue:
kl_div



Discussion:

Resolving patch:
diff --git a/ivy/functional/frontends/mindspore/ops/function/nn_func.py b/ivy/functional/frontends/mindspore/ops/function/nn_func.py
--- a/ivy/functional/frontends/mindspore/ops/function/nn_func.py
+++ b/ivy/functional/frontends/mindspore/ops/function/nn_func.py
@@ -55,6 +55,43 @@ def softsign(x):
     return ivy.divide(x, ivy.add(1, ivy.abs(x)))
 
 
+@with_supported_dtypes({"2.0 and below": ("float16", "float32")}, "mindspore")
+@to_ivy_arrays_and_back
+def kl_div(logits, labels, reduction="mean"):
+    """
+    Computes the Kullback-Leibler (KL) Divergence between the logits and the labels.
+
+    Parameters:
+        logits (numpy array): The input logits array.
+        labels (numpy array): The label array which has the same shape as logits.
+        reduction (str): Specifies the reduction to be applied to the output.
+                         Its value must be one of 'none', 'mean', 'batchmean',
+                         or 'sum'. Default: 'mean'.
+
+    Returns:
+        float or numpy array: If reduction is 'none', then output is
+        a numpy array and has the same shape as logits.
+                              Otherwise, it is a scalar (float).
+    """
+    assert ivy.shape(logits) == ivy.shape(
+        labels
+    ), "logits and labels must have the same shape."
+    L = labels * (ivy.log(labels) - logits)
+    if reduction == "none":
+        return L
+    elif reduction == "mean":
+        return ivy.mean(L)
+    elif reduction == "batchmean":
+        return ivy.mean(L, axis=0)
+    elif reduction == "sum":
+        return ivy.sum(L)
+    else:
+        raise ValueError(
+            "Invalid reduction mode. Supported values are 'none', 'mean', 'batchmean',"
+            " or 'sum'."
+        )
+
+
 @with_supported_dtypes(
     {
         "2.0.0 and below": (


Test patch:
diff --git a/ivy_tests/array_api_testing/test_array_api b/ivy_tests/array_api_testing/test_array_api
--- a/ivy_tests/array_api_testing/test_array_api
+++ b/ivy_tests/array_api_testing/test_array_api
@@ -1 +1 @@
-Subproject commit d2d43a7261dd496d125dcb7318cae3c95f532a80
+Subproject commit 852a1ddfedb14742ec94a9dfc6b3f3d4e89d0189
diff --git a/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py b/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py
--- a/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py
+++ b/ivy_tests/test_ivy/test_frontends/test_mindspore/test_ops/test_function/test_mindspore_nn_func.py
@@ -80,6 +80,48 @@
 #         x=x[0],
 #     )
 
+# kl_div
+# @handle_frontend_test(
+#     fn_tree="mindspore.ops.function.nn_func.kl_div",
+#     p=helpers.dtype_and_values(
+#         available_dtypes=helpers.get_dtypes("valid"),
+#         num_arrays=1,
+#         shared_dtype=True,
+#         min_value=2,
+#         max_value=5,
+#         min_dim_size=4,
+#     ),
+#     q=helpers.dtype_and_values(
+#         available_dtypes=helpers.get_dtypes("valid"),
+#         num_arrays=1,
+#         shared_dtype=True,
+#         min_value=2,
+#         max_value=5,
+#         min_dim_size=4,
+#     ),
+#     reduction=st.sampled_from(["none", "sum", "mean"]),
+# )
+# def test_mindspore_kl_div(
+#     *,
+#     p,
+#     q,
+#     reduction,
+#     on_device,
+#     fn_tree,
+#     frontend,
+#     test_flags,
+# ):
+#     helpers.test_frontend_function(
+#         input_dtypes=p[0],
+#         frontend=frontend,
+#         test_flags=test_flags,
+#         fn_tree=fn_tree,
+#         on_device=on_device,
+#         p=p[1],
+#         q=q[1],
+#         reduction=reduction,
+#     )
+
 # dropout3d
 # @handle_frontend_test(
 #     fn_tree="mindspore.ops.function.nn_func.dropout3d",