


import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel
class       UpperCAmelCase_   :

   '''simple docstring'''




   def __init__(						self		,							__A		,							__A=13		,							__A=7		,							__A=True		,							__A=True		,							__A=True		,							__A=True		,							__A=99		,							__A=32		,							__A=5		,							__A=4		,							__A=4		,							__A="gelu"		,							__A=0.0		,							__A=0.1		,							__A=True		,							__A=512		,							__A=16		,							__A=2		,							__A=0.02		,							__A=3		,							__A=4		,							__A=None		,							):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							parent
     lowerCamelCase					:   str       =							batch_size
     lowerCamelCase					:   List[Any]       =							seq_length
     lowerCamelCase					:   Tuple       =							is_training
     lowerCamelCase					:   str       =							use_input_mask
     lowerCamelCase					:   Tuple       =							use_token_type_ids
     lowerCamelCase					:   str       =							use_labels
     lowerCamelCase					:   Tuple       =							vocab_size
     lowerCamelCase					:   Optional[Any]       =							hidden_size
     lowerCamelCase					:   Optional[int]       =							num_hidden_layers
     lowerCamelCase					:   Optional[int]       =							num_attention_heads
     lowerCamelCase					:   List[str]       =							intermediate_multiple_size
     lowerCamelCase					:   List[Any]       =							hidden_act
     lowerCamelCase					:   Tuple       =							hidden_dropout
     lowerCamelCase					:   Dict       =							attention_dropout
     lowerCamelCase					:   int       =							weight_tying
     lowerCamelCase					:   Tuple       =							max_position_embeddings
     lowerCamelCase					:   Any       =							type_vocab_size
     lowerCamelCase					:   Union[str, Any]       =							type_sequence_label_size
     lowerCamelCase					:   Optional[int]       =							initializer_range
     lowerCamelCase					:   Optional[int]       =							num_labels
     lowerCamelCase					:   Any       =							num_choices
     lowerCamelCase					:   Optional[int]       =							scope






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							ids_tensor([self.batch_size, self.seq_length]		,							self.vocab_size					)

     lowerCamelCase					:   int       =							None
     if self.use_input_mask:
       lowerCamelCase					:   List[Any]       =							random_attention_mask([self.batch_size, self.seq_length]					)

     lowerCamelCase					:   List[Any]       =							None
     if self.use_labels:
       lowerCamelCase					:   Any       =							ids_tensor([self.batch_size, self.seq_length]		,							self.num_labels					)

     lowerCamelCase					:   List[str]       =							self.get_config()

     return config, input_ids, input_mask, token_labels






   def _snake_case       (						self					):






     """simple docstring"""





     return GPTNeoXJapaneseConfig(
         vocab_size=self.vocab_size		,							hidden_size=self.hidden_size		,							num_hidden_layers=self.num_hidden_layers		,							num_attention_heads=self.num_attention_heads		,							intermediate_multiple_size=self.intermediate_multiple_size		,							hidden_act=self.hidden_act		,							hidden_dropout=self.hidden_dropout		,							attention_dropout=self.attention_dropout		,							weight_tying=self.weight_tying		,							max_position_embeddings=self.max_position_embeddings		,							type_vocab_size=self.type_vocab_size		,							is_decoder=lowerCamelCase_		,							initializer_range=self.initializer_range		,							)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase					:   Tuple       =							self.prepare_config_and_inputs()

     lowerCamelCase					:   List[str]       =							True

     return config, input_ids, input_mask, token_labels






   def _snake_case       (						self		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Union[str, Any]       =							GPTNeoXJapaneseModel(config=lowerCamelCase_					)
     model.to(lowerCamelCase_					)
     model.eval()
     lowerCamelCase					:   List[str]       =							model(lowerCamelCase_		,							attention_mask=lowerCamelCase_					)
     lowerCamelCase					:   str       =							model(lowerCamelCase_					)
     self.parent.assertEqual(result.last_hidden_state.shape		,							(self.batch_size, self.seq_length, self.hidden_size)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   Optional[int]       =							True
     lowerCamelCase					:   str       =							GPTNeoXJapaneseModel(lowerCamelCase_					)
     model.to(lowerCamelCase_					)
     model.eval()
     lowerCamelCase					:   List[str]       =							model(lowerCamelCase_		,							attention_mask=lowerCamelCase_					)
     self.parent.assertEqual(result.last_hidden_state.shape		,							(self.batch_size, self.seq_length, self.hidden_size)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   int       =							GPTNeoXJapaneseForCausalLM(config=lowerCamelCase_					)
     model.to(lowerCamelCase_					)
     model.eval()
     lowerCamelCase					:   List[Any]       =							model(lowerCamelCase_		,							attention_mask=lowerCamelCase_		,							labels=lowerCamelCase_					)
     self.parent.assertEqual(result.logits.shape		,							(self.batch_size, self.seq_length, self.vocab_size)					)






   def _snake_case       (						self		,							__A		,							__A		,							__A					):






     """simple docstring"""





     lowerCamelCase					:   List[Any]       =							True
     lowerCamelCase					:   Union[str, Any]       =							GPTNeoXJapaneseForCausalLM(config=lowerCamelCase_					)
     model.to(lowerCamelCase_					)
     model.eval()

     # first forward pass
     lowerCamelCase					:   Union[str, Any]       =							model(lowerCamelCase_		,							attention_mask=lowerCamelCase_		,							use_cache=lowerCamelCase_					)
     lowerCamelCase					:   Dict       =							outputs.past_key_values

     # create hypothetical multiple next token and extent to next_input_ids
     lowerCamelCase					:   Any       =							ids_tensor((self.batch_size, 3)		,							config.vocab_size					)
     lowerCamelCase					:   Any       =							ids_tensor((self.batch_size, 3)		,							vocab_size=2					)

     # append to next input_ids and
     lowerCamelCase					:   str       =							torch.cat([input_ids, next_tokens]		,							dim=-1					)
     lowerCamelCase					:   int       =							torch.cat([input_mask, next_mask]		,							dim=-1					)

     lowerCamelCase					:   List[Any]       =							model(lowerCamelCase_		,							attention_mask=lowerCamelCase_		,							output_hidden_states=lowerCamelCase_					)
     lowerCamelCase					:   str       =							output_from_no_past["hidden_states"][0]
     lowerCamelCase					:   List[Any]       =							model(
         lowerCamelCase_		,							attention_mask=lowerCamelCase_		,							past_key_values=lowerCamelCase_		,							output_hidden_states=lowerCamelCase_		,							)["hidden_states"][0]

     # select random slice
     lowerCamelCase					:   Union[str, Any]       =							ids_tensor((1,)		,							output_from_past.shape[-1]					).item()
     lowerCamelCase					:   Union[str, Any]       =							output_from_no_past[:, -3:, random_slice_idx].detach()
     lowerCamelCase					:   List[str]       =							output_from_past[:, :, random_slice_idx].detach()

     self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]					)

     # test that outputs are equal for slice
     self.parent.assertTrue(torch.allclose(lowerCamelCase_		,							lowerCamelCase_		,							atol=1e-3					)					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							self.prepare_config_and_inputs()
     lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase					:   Optional[Any]       =							config_and_inputs
     lowerCamelCase					:   Union[str, Any]       =							{"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict



@require_torch
class       UpperCAmelCase_   (   UpperCamelCase						,					UpperCamelCase						,					unittest.TestCase       ):

   '''simple docstring'''




   __A					: Any							=						(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
   __A					: Optional[int]							=						(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
   __A					: Union[str, Any]							=						(
       {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
       if is_torch_available()
       else {}
   )
   __A					: List[str]							=						False
   __A					: Dict							=						False
   __A					: str							=						False
   __A					: Dict							=						False






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   Dict       =							GPTNeoXJapaneseModelTester(self					)
     lowerCamelCase					:   int       =							ConfigTester(self		,							config_class=lowerCamelCase_		,							hidden_size=37					)






   def _snake_case       (						self					):






     """simple docstring"""





     self.config_tester.run_common_tests()






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase					:   int       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(lowerCamelCase_		,							lowerCamelCase_		,							lowerCamelCase_					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase					:   Dict       =							self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(lowerCamelCase_		,							lowerCamelCase_		,							lowerCamelCase_					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase					:   Optional[int]       =							self.model_tester.prepare_config_and_inputs_for_decoder()

     lowerCamelCase					:   Optional[int]       =							None

     self.model_tester.create_and_check_model_as_decoder(lowerCamelCase_		,							lowerCamelCase_		,							lowerCamelCase_					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase       ,      lowerCamelCase					:   Tuple       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past_large_inputs(lowerCamelCase_		,							lowerCamelCase_		,							lowerCamelCase_					)






   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   int       =							self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*lowerCamelCase_					)






   @slow
   def _snake_case       (						self					):






     """simple docstring"""





     lowerCamelCase					:   List[Any]       =							"abeja/gpt-neox-japanese-2.7b"

     lowerCamelCase					:   Tuple       =							["データサイエンティストとは、", "100年後に必要とされる会社は、", "フルリモートの環境で働くために必要なことは、", "国境の長いトンネルを抜けると", "美味しい日本食といえば、"]

     lowerCamelCase					:   Tuple       =							[
         "データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。",
         "100年後に必要とされる会社は、「人」が中心の会社です。",
         "フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。",
         "国境の長いトンネルを抜けると、そこは雪国だった。",
         "美味しい日本食といえば、やっぱりお寿司ですよね。",
     ]

     lowerCamelCase					:   Tuple       =							GPTNeoXJapaneseTokenizer.from_pretrained(lowerCamelCase_					)
     lowerCamelCase					:   Dict       =							GPTNeoXJapaneseForCausalLM.from_pretrained(lowerCamelCase_					)

     lowerCamelCase					:   str       =							[]
     for prompt in prompts:
       lowerCamelCase					:   Any       =							tokenizer(lowerCamelCase_		,							return_tensors="pt"					).input_ids
       lowerCamelCase					:   Union[str, Any]       =							model.generate(lowerCamelCase_		,							max_length=50					)
       lowerCamelCase					:   str       =							tokenizer.batch_decode(lowerCamelCase_		,							skip_special_tokens=lowerCamelCase_					)
       predicted_outputs += generated_string
     self.assertListEqual(lowerCamelCase_		,							lowerCamelCase_					)






"""simple docstring"""


import qiskit



def   _A					(	_a	:						int     ,   _a	:						int				):



      """simple docstring"""






      A				  =		qiskit.Aer.get_backend("""aer_simulator"""				)

      # Create a Quantum Circuit acting on the q register
      A				  =		qiskit.QuantumCircuit(_a     ,   _a				)

      # Apply X (NOT) Gate to Qubits 0 & 1
      circuit.x(0				)
      circuit.x(1				)

      # Map the quantum measurement to the classical bits
      circuit.measure([0, 1]     ,   [0, 1]				)

      # Execute the circuit on the qasm simulator
      A				  =		qiskit.execute(_a     ,   _a     ,   shots=1_0_0_0				)

      # Return the histogram data of the results of the experiment.
      return job.result().get_counts(_a				)


if __name__ == "__main__":
  UpperCAmelCase													=single_qubit_measure(2, 2)
  print(f"""Total count for various states are: {counts}""")




