# Path: t5x/losses.py
# def cross_entropy_with_logits(logits: jnp.ndarray, targets: jnp.ndarray,
#                               z_loss: float) -> jnp.ndarray:
# def _cross_entropy_with_logits_fwd(
#     logits: jnp.ndarray,
#     targets: jnp.ndarray,
#     z_loss: float = 0.0
# ) -> Tuple[jnp.ndarray, Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp
# def _cross_entropy_with_logits_bwd(
#     res: Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray,
#                jnp.ndarray, jnp.ndarray], g: Tuple[jnp.ndarray, jnp.ndarray]
# ) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
# def compute_weighted_cross_entropy(
#     logits: jnp.ndarray,
#     targets: jnp.ndarray,
#     weights: Optional[jnp.ndarray] = None,
#     label_smoothing: float = 0.0,
#     z_loss: float = 0.0,
#     loss_normalizing_factor: Optional[float] = None
# ) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
# def convert_special_loss_normalizing_factor_to_enum(
#     x: str) -> SpecialLossNormalizingFactor:
# def get_loss_normalizing_factor_and_weights(
#     loss_normalizing_factor: Optional[Union[float, int, str,
#                                             SpecialLossNormalizingFactor]],
#     batch: Mapping[str, jnp.ndarray]):
# class SpecialLossNormalizingFactor(enum.Enum):
#   NUM_REAL_TARGET_TOKENS = 1
#   NUM_TOTAL_TARGET_TOKENS = 2
#   AVERAGE_PER_SEQUENCE = 3


from absl.testing import absltest
from t5x import losses
import jax
import jax.numpy as jnp
import numpy as np
  def test_average_per_sequence(self):
    batch = {
        'decoder_target_tokens':
            jnp.asarray([[1, 2, 3, 4, 0], [5, 6, 0, 0, 0]], jnp.int32)
    }

    (output_lnf,
     output_loss_weights) = losses.get_loss_normalizing_factor_and_weights(
         loss_normalizing_factor=losses.SpecialLossNormalizingFactor
         .AVERAGE_PER_SEQUENCE,
         batch=batch)

    np.testing.assert_allclose(output_lnf, 2.0, rtol=1e-3)
    np.testing.assert_allclose(
        output_loss_weights,
        jnp.asarray([[0.25, 0.25, 0.25, 0.25, 0.0], [0.5, 0.5, 0.0, 0.0, 0.0]],
                    jnp.float32),
        rtol=1e-3)

  def test_average_per_sequence_with_weights(self):
    batch = {
        'decoder_target_tokens':
            jnp.asarray([[1, 2, 3, 4, 0], [5, 6, 0, 0, 0]], jnp.int32),
        'decoder_loss_weights':
            jnp.asarray([[0.5, 1.0, 0.25, 2.0, 0.0], [1.0, 1.0, 0.0, 0.0, 0.0]],
                        jnp.float32)
    }

    (output_lnf,
     output_loss_weights) = losses.get_loss_normalizing_factor_and_weights(

# Path: t5x/losses.py
# def cross_entropy_with_logits(logits: jnp.ndarray, targets: jnp.ndarray,
#                               z_loss: float) -> jnp.ndarray:
# def _cross_entropy_with_logits_fwd(
#     logits: jnp.ndarray,
#     targets: jnp.ndarray,
#     z_loss: float = 0.0
# ) -> Tuple[jnp.ndarray, Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp
# def _cross_entropy_with_logits_bwd(
#     res: Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray, jnp.ndarray,
#                jnp.ndarray, jnp.ndarray], g: Tuple[jnp.ndarray, jnp.ndarray]
# ) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
# def compute_weighted_cross_entropy(
#     logits: jnp.ndarray,
#     targets: jnp.ndarray,
#     weights: Optional[jnp.ndarray] = None,
#     label_smoothing: float = 0.0,
#     z_loss: float = 0.0,
#     loss_normalizing_factor: Optional[float] = None
# ) -> Tuple[jnp.ndarray, jnp.ndarray, jnp.ndarray]:
# def convert_special_loss_normalizing_factor_to_enum(
#     x: str) -> SpecialLossNormalizingFactor:
# def get_loss_normalizing_factor_and_weights(
#     loss_normalizing_factor: Optional[Union[float, int, str,
#                                             SpecialLossNormalizingFactor]],
#     batch: Mapping[str, jnp.ndarray]):
# class SpecialLossNormalizingFactor(enum.Enum):
#   NUM_REAL_TARGET_TOKENS = 1
#   NUM_TOTAL_TARGET_TOKENS = 2
#   AVERAGE_PER_SEQUENCE = 3


# Path: t5x/losses_test.py
from absl.testing import absltest
from t5x import losses
import jax
import jax.numpy as jnp
import numpy as np

  def test_average_per_sequence(self):
    batch = {
        'decoder_target_tokens':
            jnp.asarray([[1, 2, 3, 4, 0], [5, 6, 0, 0, 0]], jnp.int32)
    }

    (output_lnf,
     output_loss_weights) = losses.get_loss_normalizing_factor_and_weights(
         loss_normalizing_factor=losses.SpecialLossNormalizingFactor
         .AVERAGE_PER_SEQUENCE,
         batch=batch)

    np.testing.assert_allclose(output_lnf, 2.0, rtol=1e-3)
    np.testing.assert_allclose(
        output_loss_weights,
        jnp.asarray([[0.25, 0.25, 0.25, 0.25, 0.0], [0.5, 0.5, 0.0, 0.0, 0.0]],
                    jnp.float32),
        rtol=1e-3)

  def test_average_per_sequence_with_weights(self):
    batch = {
        'decoder_target_tokens':
            jnp.asarray([[1, 2, 3, 4, 0], [5, 6, 0, 0, 0]], jnp.int32),
        'decoder_loss_weights':
            jnp.asarray([[0.5, 1.0, 0.25, 2.0, 0.0], [1.0, 1.0, 0.0, 0.0, 0.0]],
                        jnp.float32)
    }

    (output_lnf,
     output_loss_weights) = losses.get_loss_normalizing_factor_and_weights(

         loss_normalizing_factor=losses.SpecialLossNormalizingFactor