# Path: t5x/examples/t5/layers.py
# def dot_product_attention(query: Array,
#                           key: Array,
#                           value: Array,
#                           bias: Optional[Array] = None,
#                           dropout_rng: Optional[PRNGKey] = None,
#                           dropout_rate: float = 0.,
#                           deterministic: bool = False,
#                           dtype: DType = jnp.float32,
#                           float32_logits: bool = False):
#   def __call__(self,
#                inputs_q: Array,
#                inputs_kv: Array,
#                mask: Optional[Array] = None,
#                bias: Optional[Array] = None,
#                *,
#                decode: bool = False,
#                deterministic: bool = False) -> Array:
# def _normalize_axes(axes: Iterable[int], ndim: int) -> Tuple[int]:
# def _canonicalize_tuple(x):
#   def __call__(self, inputs: Array) -> Array:
# def _convert_to_activation_function(
#     fn_or_string: Union[str, Callable]) -> Callable:
#   def __call__(self, inputs, decode: bool = False, deterministic: bool = False):
#   def setup(self):
#   def __call__(self, inputs: Array) -> Array:
#   def attend(self, query: Array) -> Array:
#   def _relative_position_bucket(relative_position,
#                                 bidirectional=True,
#                                 num_buckets=32,
#                                 max_distance=128):
#   def __call__(self, qlen, klen, bidirectional=True):
#   def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
# def make_attention_mask(query_input: Array,
#                         key_input: Array,
#                         pairwise_fn: Callable = jnp.multiply,
#                         extra_batch_dims: int = 0,
#                         dtype: DType = jnp.float32) -> Array:
# def make_causal_mask(x: Array,
#                      extra_batch_dims: int = 0,
#                      dtype: DType = jnp.float32) -> Array:
# def combine_masks(*masks: Optional[Array], dtype: DType = jnp.float32):
# def combine_biases(*masks: Optional[Array]):
# def make_decoder_mask(decoder_target_tokens: Array,
#                       dtype: DType,
#                       decoder_causal_attention: Optional[Array] = None,
#                       decoder_segment_ids: Optional[Array] = None) -> Array:
# class MultiHeadDotProductAttention(nn.Module):
# class DenseGeneral(nn.Module):
# class MlpBlock(nn.Module):
# class Embed(nn.Module):
# class RelativePositionBiases(nn.Module):
# class LayerNorm(nn.Module):


import dataclasses
import jax
import jax.numpy as jnp
import numpy as np
from typing import Optional
from unittest import mock
from absl.testing import absltest
from absl.testing import parameterized
from flax import linen as nn
from flax.core import freeze
from flax.linen import partitioning as nn_partitioning
from jax import random
from jax.nn import initializers
from t5x.examples.t5 import layers
    decoder_target_tokens = jnp.array([[5, 6, 7, 3, 4, 0]])
    decoder_causal_attention = jnp.array([[1, 1, 1, 0, 0, 0]])
    mask = layers.make_decoder_mask(
        decoder_target_tokens=decoder_target_tokens,
        dtype=jnp.float32,
        decoder_causal_attention=decoder_causal_attention)
    expected_mask = jnp.array(
        [[[[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0],
           [1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0]]]],
        dtype=jnp.float32)
    np.testing.assert_array_equal(mask, expected_mask)

  def test_make_decoder_mask_prefix_lm_packed(self):
    decoder_target_tokens = jnp.array([[5, 6, 7, 8, 3, 4, 0]])
    decoder_segment_ids = jnp.array([[1, 1, 1, 2, 2, 2, 0]])
    decoder_causal_attention = jnp.array([[1, 1, 0, 1, 1, 0, 0]])
    mask = layers.make_decoder_mask(
        decoder_target_tokens=decoder_target_tokens,
        dtype=jnp.float32,
        decoder_causal_attention=decoder_causal_attention,
        decoder_segment_ids=decoder_segment_ids)
    expected_mask = jnp.array([[[[1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0],
                                 [1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0],
                                 [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0],
                                 [0, 0, 0, 0, 0, 0, 0]]]])
    np.testing.assert_array_equal(mask, expected_mask)

  def test_make_decoder_mask_prefix_lm_unpacked_multiple_elements(self):
    decoder_target_tokens = jnp.array([[6, 7, 3, 0], [4, 5, 0, 0]])
    decoder_causal_attention = jnp.array([[1, 1, 0, 0], [1, 0, 0, 0]])

# Path: t5x/examples/t5/layers.py
# def dot_product_attention(query: Array,
#                           key: Array,
#                           value: Array,
#                           bias: Optional[Array] = None,
#                           dropout_rng: Optional[PRNGKey] = None,
#                           dropout_rate: float = 0.,
#                           deterministic: bool = False,
#                           dtype: DType = jnp.float32,
#                           float32_logits: bool = False):
#   def __call__(self,
#                inputs_q: Array,
#                inputs_kv: Array,
#                mask: Optional[Array] = None,
#                bias: Optional[Array] = None,
#                *,
#                decode: bool = False,
#                deterministic: bool = False) -> Array:
# def _normalize_axes(axes: Iterable[int], ndim: int) -> Tuple[int]:
# def _canonicalize_tuple(x):
#   def __call__(self, inputs: Array) -> Array:
# def _convert_to_activation_function(
#     fn_or_string: Union[str, Callable]) -> Callable:
#   def __call__(self, inputs, decode: bool = False, deterministic: bool = False):
#   def setup(self):
#   def __call__(self, inputs: Array) -> Array:
#   def attend(self, query: Array) -> Array:
#   def _relative_position_bucket(relative_position,
#                                 bidirectional=True,
#                                 num_buckets=32,
#                                 max_distance=128):
#   def __call__(self, qlen, klen, bidirectional=True):
#   def __call__(self, x: jnp.ndarray) -> jnp.ndarray:
# def make_attention_mask(query_input: Array,
#                         key_input: Array,
#                         pairwise_fn: Callable = jnp.multiply,
#                         extra_batch_dims: int = 0,
#                         dtype: DType = jnp.float32) -> Array:
# def make_causal_mask(x: Array,
#                      extra_batch_dims: int = 0,
#                      dtype: DType = jnp.float32) -> Array:
# def combine_masks(*masks: Optional[Array], dtype: DType = jnp.float32):
# def combine_biases(*masks: Optional[Array]):
# def make_decoder_mask(decoder_target_tokens: Array,
#                       dtype: DType,
#                       decoder_causal_attention: Optional[Array] = None,
#                       decoder_segment_ids: Optional[Array] = None) -> Array:
# class MultiHeadDotProductAttention(nn.Module):
# class DenseGeneral(nn.Module):
# class MlpBlock(nn.Module):
# class Embed(nn.Module):
# class RelativePositionBiases(nn.Module):
# class LayerNorm(nn.Module):


# Path: t5x/examples/t5/layers_test.py
import dataclasses
import jax
import jax.numpy as jnp
import numpy as np
from typing import Optional
from unittest import mock
from absl.testing import absltest
from absl.testing import parameterized
from flax import linen as nn
from flax.core import freeze
from flax.linen import partitioning as nn_partitioning
from jax import random
from jax.nn import initializers
from t5x.examples.t5 import layers

    decoder_target_tokens = jnp.array([[5, 6, 7, 3, 4, 0]])
    decoder_causal_attention = jnp.array([[1, 1, 1, 0, 0, 0]])
    mask = layers.make_decoder_mask(
        decoder_target_tokens=decoder_target_tokens,
        dtype=jnp.float32,
        decoder_causal_attention=decoder_causal_attention)
    expected_mask = jnp.array(
        [[[[1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0],
           [1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0]]]],
        dtype=jnp.float32)
    np.testing.assert_array_equal(mask, expected_mask)

  def test_make_decoder_mask_prefix_lm_packed(self):
    decoder_target_tokens = jnp.array([[5, 6, 7, 8, 3, 4, 0]])
    decoder_segment_ids = jnp.array([[1, 1, 1, 2, 2, 2, 0]])
    decoder_causal_attention = jnp.array([[1, 1, 0, 1, 1, 0, 0]])
    mask = layers.make_decoder_mask(
        decoder_target_tokens=decoder_target_tokens,
        dtype=jnp.float32,
        decoder_causal_attention=decoder_causal_attention,
        decoder_segment_ids=decoder_segment_ids)
    expected_mask = jnp.array([[[[1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0],
                                 [1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 1, 1, 0, 0],
                                 [0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 1, 1, 0],
                                 [0, 0, 0, 0, 0, 0, 0]]]])
    np.testing.assert_array_equal(mask, expected_mask)

  def test_make_decoder_mask_prefix_lm_unpacked_multiple_elements(self):
    decoder_target_tokens = jnp.array([[6, 7, 3, 0], [4, 5, 0, 0]])
    decoder_causal_attention = jnp.array([[1, 1, 0, 0], [1, 0, 0, 0]])

    mask = layers.make_decoder_mask(