code: def scaled_dot_product_attention(
      query: Union[ivy.Array, ivy.NativeArray],
      key: Union[ivy.Array, ivy.NativeArray],
      value: Union[ivy.Array, ivy.NativeArray],
      /,
      *,
      scale: Optional[float] = None,
      mask: Optional[Union[ivy.Array, ivy.NativeArray]] = None,
      dropout_p: Optional[float] = 0.0,
      is_causal: Optional[bool] = False,
      training: Optional[bool] = False,
      out: Optional[ivy.Array] = None,
  ) -> ivy.Array:
      """Apply scaled dot product attention to inputs x using optional mask.
  
      Parameters
      ----------
      query
          The queries input array. The shape of queries input array should be in
          *[batch_shape,num_queries,feat_dim]*. The queries input array should have the
          same size as keys and values.
      key
          The keys input array. The shape of keys input array should be in
          *[batch_shape,num_keys,feat_dim]*. The keys input array should have the same
          size as queries and values.
      value
          The values input array. The shape of values input should be in
          *[batch_shape,num_keys,feat_dim]*. The values input array should have the same
          size as queries and keys.
      scale
          The scale float value.
          The scale float value is used to scale the query-key pairs before softmax.
      mask
          The mask input array. The mask to apply to the query-key values. Default is
          None. The shape of mask input should be in *[batch_shape,num_queries,num_keys]*.
      dropout_p
          Specifies the dropout probability, if greater than 0.0, dropout is applied
      is_causal
          If true, assumes causal attention masking
          and errors if both `mask` and `is_causal` are set.
      training
          If True, dropout is used, otherwise dropout is not activated.
      out
          optional output array, for writing the result to. It must have a shape that the
          inputs broadcast to.
  
      Returns
      -------
      ret
          The output following application of scaled dot-product attention.
          The output array is the weighted sum produced by the attention score and value.
          The shape of output array is *[batch_shape,num_queries,feat_dim]* .
  
      Both the description and the type hints above assumes an array input for simplicity,
      but this function is *nestable*, and therefore also accepts :class:`ivy.Container`
      instances in place of any of the arguments.
  
      Examples
      --------
      With :class:`ivy.Array` input:
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.],[4.4, 5.6]]])
      >>> k = ivy.array([[[0.6, 1.5], [2.4, 3.3],[4.2, 5.1]]])
      >>> v = ivy.array([[[0.4, 1.3], [2.2, 3.1],[4.3, 5.3]]])
      >>> result = ivy.scaled_dot_product_attention(q,
      ...                                           k,
      ...                                           v,
      ...                                           scale=1,
      ...                                           dropout_p=0.1,
      ...                                           is_causal=True,
      ...                                           training=True)
      >>> print(result)
  
      ivy.array([[[0.40000001, 1.29999995],
      ...         [2.19994521, 3.09994531],
      ...         [4.30000019, 5.30000019]]])
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.],[4.4, 5.6]]])
      >>> k = ivy.array([[[0.6, 1.5], [2.4, 3.3],[4.2, 5.1]]])
      >>> v = ivy.array([[[0.4, 1.3], [2.2, 3.1],[4.3, 5.3]]])
      >>> mask = ivy.array([[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],[0.0, 0.0, 0.0]]])
      >>> result = ivy.scaled_dot_product_attention(q,k,v,scale=1,mask=mask)
      >>> print(result)
  
      ivy.array([[[0.40000001, 1.29999995],
      ...         [2.19994521, 3.09994531],
      ...         [4.30000019, 5.30000019]]])
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.], [4.4, 5.6]]])
      >>> k = ivy.array([[[0.6, 1.5], [2.4, 3.3], [4.2, 5.1]]])
      >>> v = ivy.array([[[0.4, 1.3], [2.2, 3.1], [4.3, 5.3]]])
      >>> out = ivy.zeros(shape=(1, 3, 2))
      >>> ivy.scaled_dot_product_attention(q,
      ...                                  k,
      ...                                  v,
      ...                                  scale=1,
      ...                                  dropout_p=0.1,
      ...                                  is_causal=True,
      ...                                  training=True,
      ...                                  out=out)
      >>> print(out)
  
      ivy.array([[[0.40000001, 1.29999995],
      ...         [2.19994521, 3.09994531],
      ...         [4.30000019, 5.30000019]]])
  
      >>> q = ivy.native_array([[[0.2, 1.], [2.2, 3.],[4.4, 5.6]]])
      >>> k = ivy.native_array([[[0.6, 1.5], [2.4, 3.3],[4.2, 5.1]]])
      >>> v = ivy.native_array([[[0.4, 1.3], [2.2, 3.1],[4.3, 5.3]]])
      >>> mask = ivy.native_array([[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0],[0.0, 0.0, 0.0]]])
      >>> result = ivy.scaled_dot_product_attention(q,k,v,scale=1,mask=mask)
      >>> print(result)
  
      ivy.array([[[2.30000019, 3.23333359],
      ...         [2.30000019, 3.23333359],
      ...         [2.30000019, 3.23333359]]])
  
      >>> q = ivy.native_array([[[0.2, 1.], [2.2, 3.], [4.4, 5.6]]])
      >>> k = ivy.native_array([[[0.6, 1.5], [2.4, 3.3], [4.2, 5.1]]])
      >>> v = ivy.native_array([[[0.4, 1.3], [2.2, 3.1], [4.3, 5.3]]])
      >>> out = ivy.zeros(shape=(1, 3, 2))
      >>> ivy.scaled_dot_product_attention(q,
      ...                                  k,
      ...                                  v,
      ...                                  scale=1,
      ...                                  dropout_p=0.1,
      ...                                  is_causal=True,
      ...                                  training=True,
      ...                                  out=out)
      >>> print(out)
  
      ivy.array([[[0.40000001, 1.29999995],
      ...         [2.19994521, 3.09994531],
      ...         [4.30000019, 5.30000019]]])
  
      With :class:`ivy.Container` input:
  
      >>> q = ivy.Container(a=ivy.array([[[0.2, 1.], [2.7, 3.], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[1.2, 1.], [2.2, 3.], [4.4, 5.6]]]))
      >>> k = ivy.Container(a=ivy.array([[[4.2, 1.], [2.2, 3.3], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[3.2, 1.], [2.2, 3.6], [4.0, 5.6]]]))
      >>> v = ivy.Container(a=ivy.array([[[5.2, 1.], [2.1, 3.], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[0.2, 1.], [2.2, 3.], [4.4, 5.6]]]))
      >>> result = ivy.scaled_dot_product_attention(q,
      ...                                           k,
      ...                                           v,
      ...                                           scale=1,
      ...                                           dropout_p=0.1,
      ...                                           is_causal=True,
      ...                                           training=True)
      >>> print(result)
      {
          a: ivy.array([[[5.19999981, 1.],
          ...            [2.59249449, 2.68226194],
          ...            [4.4000001, 5.5999999]]]),
          b: ivy.array([[[0.2, 1.],
          ...            [2.19603825, 2.9960382],
          ...            [4.4000001, 5.5999999]]])
      }
  
      >>> q = ivy.Container(a=ivy.array([[[0.2, 1.], [2.7, 3.], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[1.2, 1.], [2.2, 3.], [4.4, 5.6]]]))
      >>> k = ivy.Container(a=ivy.array([[[4.2, 1.], [2.2, 3.3], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[3.2, 1.], [2.2, 3.6], [4.0, 5.6]]]))
      >>> v = ivy.Container(a=ivy.array([[[5.2, 1.], [2.1, 3.], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[0.2, 1.], [2.2, 3.], [4.4, 5.6]]]))
      >>> mask = ivy.Container(
      ...     a=ivy.array([[[1.0, 1.0, 1.0],[1.0, 1.0, 1.0],[1.0, 1.0, 1.0]]]),
      ...     b=ivy.array([[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0,1.0]]])
      ... )
      >>> result = ivy.scaled_dot_product_attention(q,k,v,scale=1,mask=mask)
      >>> print(result)
      {
          a: ivy.array([[[4.26894283, 5.40236187],
          ...            [4.39999437, 5.59999037],
          ...            [4.4000001, 5.5999999]]]),
          b: ivy.array([[[4.35046196, 5.54282808],
          ...            [4.39989519, 5.5998764],
          ...            [4.4000001, 5.5999999]]])
      }
  
      With a mix of :class:`ivy.Array` and :class:`ivy.NativeArray` inputs:
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.],[4.4, 5.6]]])
      >>> k = ivy.native_array([[[0.6, 1.5], [2.4, 3.3],[4.2, 5.1]]])
      >>> v = ivy.native_array([[[0.4, 1.3], [2.2, 3.1],[4.3, 5.3]]])
      >>> result = ivy.scaled_dot_product_attention(q,
      ...                                            k,
      ...                                            v,
      ...                                            scale=1,
      ...                                            dropout_p=0.1,
      ...                                            is_causal=True,
      ...                                            training=True)
      >>> print(result)
  
      ivy.array([[[0.40000001, 1.29999995],
      ...         [2.19994521, 3.09994531],
      ...         [4.30000019, 5.30000019]]])
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.], [4.4, 5.6]]])
      >>> k = ivy.native_array([[[0.6, 1.5], [2.4, 3.3], [4.2, 5.1]]])
      >>> v = ivy.native_array([[[0.4, 1.3], [2.2, 3.1], [4.3, 5.3]]])
      >>> out = ivy.zeros(shape=(1, 3, 2))
      >>> ivy.scaled_dot_product_attention(q,k,v,scale=1,out=out)
      >>> print(out)
      ivy.array([[[4.03946018, 5.0280633 ],
      ...         [4.29981947, 5.29981089],
      ...         [4.30000019, 5.30000019]]])
  
      With a mix of :class:`ivy.Array` and :class:`ivy.Container` inputs:
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.],[4.4, 5.6]]])
      >>> k = ivy.Container(a=ivy.array([[[4.2, 1.], [2.2, 3.3], [4.4, 5.6]]]),
      ...                   b=ivy.array([[[3.2, 1.], [2.2, 3.6], [4.0, 5.6]]]))
      >>> v = ivy.array([[[0.4, 1.3], [2.2, 3.1], [4.3, 5.3]]])
      >>> result = ivy.scaled_dot_product_attention(q,k,v,scale=1,is_causal=True)
      >>> print(result)
      {
          a: ivy.array([[[0.40000001, 1.29999995],
          ...            [2.06345534, 2.9634552],
          ...            [4.30000019, 5.30000019]]]),
          b: ivy.array([[[0.40000001, 1.29999995],
          ...            [2.19336844, 3.09336829],
          ...            [4.30000019, 5.30000019]]])
      }
      With a mix of :class:`ivy.Array` and :class:`ivy.Container` inputs:
  
      >>> q = ivy.array([[[0.2, 1.], [2.2, 3.],[4.4, 5.6]]])
      >>> k = ivy.Container(a=ivy.array([[[4.2, 1.], [2.2, 3.3],[4.4, 5.6]]]),
      ...                   b=ivy.array([[[3.2, 1.], [2.2, 3.6],[4.0, 5.6]]]))
      >>> v = ivy.array([[[0.4, 1.3], [2.2, 3.1],[4.3, 5.3]]])
      >>> mask = ivy.native_array([[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]])
      >>> result = ivy.scaled_dot_product_attention(q,
      ...                                           k,
      ...                                           v,
      ...                                           scale=1,
      ...                                           mask=mask,
      ...                                           dropout_p=0.1,
      ...                                           training=True)
      >>> print(result)
      {
          a: ivy.array([[[2.30000019, 3.23333359],
          ...            [2.30000019, 3.23333359],
          ...            [2.30000019, 3.23333359]]]),
          b: ivy.array([[[2.30000019, 3.23333359],
          ...            [2.30000019, 3.23333359],
          ...            [2.30000019, 3.23333359]]])
      }
      """
      ivy.assertions.check_all(
          (not is_causal) or (is_causal and mask is None),
          "is_causal and attn_mask cannot be set at the same time",
      )
      embed_dim = query.shape[-1]
      scale = scale if scale else 1 / (embed_dim**0.5)
      sim = (
          ivy.einsum("... q f, ... k f -> ... q k", query, key) * scale
      )
      sim = ivy.dropout(sim, dropout_p, training=training)
      if ivy.exists(mask):
          sim = ivy.where(
              ivy.logical_not(mask),
              -ivy.ones_like(sim) * ivy.finfo(ivy.dtype(sim)).max,
              sim,
          )
      elif is_causal:
          L = query.shape[-2]  # Source sequence length
          S = key.shape[-2]  # Target sequence length
          mask = ivy.tril(ivy.ones((L, S)), k=0)
          mask = ivy.astype(mask, ivy.bool)
          sim = ivy.where(
              ivy.logical_not(mask),
              -ivy.ones_like(sim) * ivy.finfo(ivy.dtype(sim)).max,
              sim,
          )
      attn = ivy.softmax(sim, axis=-1)
      result = ivy.einsum("... qk, ...kf -> ...qf", attn, value)
      return (
          ivy.inplace_update(out, result) if ivy.exists(out) else result
      )
  
imports: from typing import Optional, Tuple, Union, Sequence
  import ivy
  from ivy.utils.backend import current_backend
  from ivy.func_wrapper import handle_array_function, handle_partial_mixed_function, inputs_to_ivy_arrays, to_native_arrays_and_back, inputs_to_native_shapes, handle_out_argument, handle_nestable, handle_array_like_without_promotion, handle_device, handle_backend_invalid
  from ivy.utils.exceptions import handle_exceptions
label_desc: Use image node_id 5 for calling a global function with example usage: scaled_dot_product_attention() and returns: unknown