code: def _dot_product_attention(
      scope: Scope,
      query: Array,
      key: Array,
      value: Array,
      bias: Optional[Array] = None,
      attn_fn: Callable = softmax_attn,
      dtype=jnp.float32,
  ):
      assert key.ndim == query.ndim
      assert key.ndim == value.ndim
  
      n = query.ndim
      attn_weights = lax.dot_general(
          query, key, (((n - 1,), (n - 1,)), ((), ()))
      )
      if bias is not None:
          attn_weights += bias
      attn_weights = attn_fn(scope, attn_weights)
      attn_weights = attn_weights.astype(dtype)
  
      contract_dims = (
          tuple(range(n - 1, attn_weights.ndim)),
          tuple(range(0, n - 1)),
      )
      y = lax.dot_general(
          attn_weights, value, (contract_dims, ((), ()))
      )
      return y
  
file_path: flax/tests/core/design/core_attention_test.py
imports: from functools import partial
  from typing import Callable, Optional, Sequence
  import jax
  from absl.testing import absltest
  from jax import lax, random
  from jax import numpy
  from flax.core import Array, Scope, init, lift, nn, unfreeze
label_desc: Use image node_id 3 for calling a global function with example usage: _dot_product_attention(scope, query, key, value, bias, attn_fn, dtype) and returns: y