


"""simple docstring"""

import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
       import jax.numpy as jnp

if is_tf_available():
       import tensorflow as tf

if is_torch_available():
       import torch





class  lowerCamelCase__    (		unittest.TestCase     ):
      def SCREAMING_SNAKE_CASE_						(		self						:						Any				):






       '''simple docstring'''


       __lowercase						=						{
           """task_specific_params""": {
               """summarization""": {"""length_penalty""": 1.0, """max_length""": 1_2_8, """min_length""": 1_2, """num_beams""": 4},
               """summarization_cnn""": {"""length_penalty""": 2.0, """max_length""": 1_4_2, """min_length""": 5_6, """num_beams""": 4},
               """summarization_xsum""": {"""length_penalty""": 1.0, """max_length""": 6_2, """min_length""": 1_1, """num_beams""": 6},
           }
       }
       __lowercase						=						{
           """task_specific_params.summarization.length_penalty""": 1.0,
           """task_specific_params.summarization.max_length""": 1_2_8,
           """task_specific_params.summarization.min_length""": 1_2,
           """task_specific_params.summarization.num_beams""": 4,
           """task_specific_params.summarization_cnn.length_penalty""": 2.0,
           """task_specific_params.summarization_cnn.max_length""": 1_4_2,
           """task_specific_params.summarization_cnn.min_length""": 5_6,
           """task_specific_params.summarization_cnn.num_beams""": 4,
           """task_specific_params.summarization_xsum.length_penalty""": 1.0,
           """task_specific_params.summarization_xsum.max_length""": 6_2,
           """task_specific_params.summarization_xsum.min_length""": 1_1,
           """task_specific_params.summarization_xsum.num_beams""": 6,
       }

       self.assertEqual(flatten_dict(A_				)   ,							A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Union[str, Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							x.transpose()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							x.transpose((1, 2, 0)				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							transpose(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							transpose(A_   ,							axes=(1, 2, 0)				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							transpose(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							transpose(A_   ,							axes=(1, 2, 0)				).numpy()				)				)
      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(transpose(A_				)   ,							np.asarray(transpose(A_				)				)				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(transpose(A_   ,							axes=(1, 2, 0)				)   ,							np.asarray(transpose(A_   ,							axes=(1, 2, 0)				)				)				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							np.reshape(A_   ,							(4, 3)				)				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							np.reshape(A_   ,							(1_2, 5)				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							reshape(A_   ,							(4, 3)				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							reshape(A_   ,							(1_2, 5)				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							reshape(A_   ,							(4, 3)				).numpy()				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							reshape(A_   ,							(1_2, 5)				).numpy()				)				)
      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						str				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(4, 3)				)   ,							np.asarray(reshape(A_   ,							(4, 3)				)				)				)				)

       __lowercase						=						np.random.randn(3   ,							4   ,							5				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(reshape(A_   ,							(1_2, 5)				)   ,							np.asarray(reshape(A_   ,							(1_2, 5)				)				)				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							np.squeeze(A_				)				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							np.squeeze(A_   ,							axis=2				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							squeeze(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							squeeze(A_   ,							axis=2				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							squeeze(A_				).numpy()				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							squeeze(A_   ,							axis=2				).numpy()				)				)
      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(1   ,							3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(squeeze(A_				)   ,							np.asarray(squeeze(A_				)				)				)				)

       __lowercase						=						np.random.randn(1   ,							4   ,							1   ,							5				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(squeeze(A_   ,							axis=2				)   ,							np.asarray(squeeze(A_   ,							axis=2				)				)				)				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							np.expand_dims(A_   ,							axis=1				)				)				)
      @require_torch
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						torch.tensor(A_				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							expand_dims(A_   ,							axis=1				).numpy()				)				)
      @require_tf
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						tf.constant(A_				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							expand_dims(A_   ,							axis=1				).numpy()				)				)






      @require_flax
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       __lowercase						=						np.random.randn(3   ,							4				)
       __lowercase						=						jnp.array(A_				)
       self.assertTrue(np.allclose(expand_dims(A_   ,							axis=1				)   ,							np.asarray(expand_dims(A_   ,							axis=1				)				)				)				)




"""simple docstring"""

from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


UpperCAmelCase__					=logging.get_logger(__name__)

UpperCAmelCase__					={
    "EleutherAI/gpt-j-6B": "https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}





class  lowerCamelCase__    (		_a     ):
      a			:  Tuple           =		"""gptj"""
      a			:  Union[str, Any]           =		{
          """max_position_embeddings""": """n_positions""",
          """hidden_size""": """n_embd""",
          """num_attention_heads""": """n_head""",
          """num_hidden_layers""": """n_layer""",
      }
      def __init__(		self						:						int   ,							A_						:						Optional[int]=5_0_4_0_0   ,							A_						:						Optional[Any]=2_0_4_8   ,							A_						:						Optional[Any]=4_0_9_6   ,							A_						:						Any=2_8   ,							A_						:						Union[str, Any]=1_6   ,							A_						:						int=6_4   ,							A_						:						int=None   ,							A_						:						str="gelu_new"   ,							A_						:						str=0.0   ,							A_						:						Optional[Any]=0.0   ,							A_						:						Dict=0.0   ,							A_						:						Dict=1e-5   ,							A_						:						Optional[int]=0.02   ,							A_						:						List[str]=True   ,							A_						:						List[Any]=5_0_2_5_6   ,							A_						:						Optional[int]=5_0_2_5_6   ,							A_						:						List[Any]=False   ,							**A_						:						int   ,							):






       '''simple docstring'''


       __lowercase						=						vocab_size
       __lowercase						=						n_positions
       __lowercase						=						n_embd
       __lowercase						=						n_layer
       __lowercase						=						n_head
       __lowercase						=						n_inner
       __lowercase						=						rotary_dim
       __lowercase						=						activation_function
       __lowercase						=						resid_pdrop
       __lowercase						=						embd_pdrop
       __lowercase						=						attn_pdrop
       __lowercase						=						layer_norm_epsilon
       __lowercase						=						initializer_range
       __lowercase						=						use_cache

       __lowercase						=						bos_token_id
       __lowercase						=						eos_token_id

       super().__init__(
           bos_token_id=A_   ,							eos_token_id=A_   ,							tie_word_embeddings=A_   ,							**A_				)






class  lowerCamelCase__    (		_a     ):
      def __init__(		self						:						int   ,							A_						:						PretrainedConfig   ,							A_						:						str = "default"   ,							A_						:						List[PatchingSpec] = None   ,							A_						:						bool = False   ,							):






       '''simple docstring'''


       super().__init__(A_   ,							task=A_   ,							patching_specs=A_   ,							use_past=A_				)
       if not getattr(self._config   ,							"""pad_token_id"""   ,							A_				):
        # TODO: how to do that better?
        __lowercase						=						0
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]				):






       '''simple docstring'''


       __lowercase						=						OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}				)
       if self.use_past:
        self.fill_with_past_key_values_(A_   ,							direction="""inputs"""				)
        __lowercase						=						{0: """batch""", 1: """past_sequence + sequence"""}
       else:
        __lowercase						=						{0: """batch""", 1: """sequence"""}

       return common_inputs
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       return self._config.n_layer
      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[int]				):






       '''simple docstring'''


       return self._config.n_head
      def SCREAMING_SNAKE_CASE_						(		self						:						Optional[Any]   ,							A_						:						PreTrainedTokenizer   ,							A_						:						int = -1   ,							A_						:						int = -1   ,							A_						:						bool = False   ,							A_						:						Optional[TensorType] = None   ,							):






       '''simple docstring'''


       __lowercase						=						super(A_   ,							self				).generate_dummy_inputs(
           A_   ,							batch_size=A_   ,							seq_length=A_   ,							is_pair=A_   ,							framework=A_				)

       # We need to order the input in the way they appears in the forward()
       __lowercase						=						OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}				)

       # Need to add the past_keys
       if self.use_past:
        if not is_torch_available():
         raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""				)
        else:
         import torch

         __lowercase				,		__lowercase						=						common_inputs["""input_ids"""].shape
         # Not using the same length for past_key_values
         __lowercase						=						seqlen + 2
         __lowercase						=						(
             batch,
             self.num_attention_heads,
             past_key_values_length,
             self._config.hidden_size // self.num_attention_heads,
         )
         __lowercase						=						[
             (torch.zeros(A_				), torch.zeros(A_				)) for _ in range(self.num_layers				)
         ]

       __lowercase						=						common_inputs["""attention_mask"""]
       if self.use_past:
        __lowercase						=						ordered_inputs["""attention_mask"""].dtype
        __lowercase						=						torch.cat(
            [ordered_inputs["""attention_mask"""], torch.ones(A_   ,							A_   ,							dtype=A_				)]   ,							dim=1				)

       return ordered_inputs






      @property
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]				):






       '''simple docstring'''


       return 1_3
