

import unittest

from transformers import AlbertConfig, is_torch_available
from transformers.models.auto import get_values
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 (
       MODEL_FOR_PRETRAINING_MAPPING,
       AlbertForMaskedLM,
       AlbertForMultipleChoice,
       AlbertForPreTraining,
       AlbertForQuestionAnswering,
       AlbertForSequenceClassification,
       AlbertForTokenClassification,
       AlbertModel,
   )
   from transformers.models.albert.modeling_albert import ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST



class SCREAMING_SNAKE_CASE     :
  """simple docstring"""






  def __init__(      self			:   Tuple	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   List[str]=13	,							lowerCAmelCase			:   List[str]=7	,							lowerCAmelCase			:   Optional[int]=True	,							lowerCAmelCase			:   Union[str, Any]=True	,							lowerCAmelCase			:   List[Any]=True	,							lowerCAmelCase			:   Tuple=True	,							lowerCAmelCase			:   str=99	,							lowerCAmelCase			:   Tuple=16	,							lowerCAmelCase			:   str=36	,							lowerCAmelCase			:   int=6	,							lowerCAmelCase			:   Optional[Any]=6	,							lowerCAmelCase			:   str=6	,							lowerCAmelCase			:   List[str]=37	,							lowerCAmelCase			:   List[str]="gelu"	,							lowerCAmelCase			:   Tuple=0.1	,							lowerCAmelCase			:   Tuple=0.1	,							lowerCAmelCase			:   List[str]=5_12	,							lowerCAmelCase			:   Tuple=16	,							lowerCAmelCase			:   List[Any]=2	,							lowerCAmelCase			:   Dict=0.02	,							lowerCAmelCase			:   List[str]=3	,							lowerCAmelCase			:   Union[str, Any]=4	,							lowerCAmelCase			:   List[Any]=None	,							)							->     Dict:






     """simple docstring"""







     __lowerCAmelCase  :      str    =						parent
     __lowerCAmelCase  :      Any    =						batch_size
     __lowerCAmelCase  :      List[Any]    =						seq_length
     __lowerCAmelCase  :      Optional[int]    =						is_training
     __lowerCAmelCase  :      Optional[int]    =						use_input_mask
     __lowerCAmelCase  :      Optional[int]    =						use_token_type_ids
     __lowerCAmelCase  :      Any    =						use_labels
     __lowerCAmelCase  :      Optional[Any]    =						vocab_size
     __lowerCAmelCase  :      str    =						embedding_size
     __lowerCAmelCase  :      Tuple    =						hidden_size
     __lowerCAmelCase  :      Optional[Any]    =						num_hidden_layers
     __lowerCAmelCase  :      Any    =						num_hidden_groups
     __lowerCAmelCase  :      str    =						num_attention_heads
     __lowerCAmelCase  :      str    =						intermediate_size
     __lowerCAmelCase  :      List[str]    =						hidden_act
     __lowerCAmelCase  :      Optional[int]    =						hidden_dropout_prob
     __lowerCAmelCase  :      Optional[Any]    =						attention_probs_dropout_prob
     __lowerCAmelCase  :      Optional[int]    =						max_position_embeddings
     __lowerCAmelCase  :      Optional[Any]    =						type_vocab_size
     __lowerCAmelCase  :      int    =						type_sequence_label_size
     __lowerCAmelCase  :      int    =						initializer_range
     __lowerCAmelCase  :      Union[str, Any]    =						num_labels
     __lowerCAmelCase  :      str    =						num_choices
     __lowerCAmelCase  :      int    =						scope







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple   )							->     List[Any]:






     """simple docstring"""







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

     __lowerCAmelCase  :      Tuple    =						None
     if self.use_input_mask:
        __lowerCAmelCase  :      Optional[Any]    =						random_attention_mask([self.batch_size, self.seq_length]   )

     __lowerCAmelCase  :      Union[str, Any]    =						None
     if self.use_token_type_ids:
        __lowerCAmelCase  :      Optional[int]    =						ids_tensor([self.batch_size, self.seq_length]	,							self.type_vocab_size   )

     __lowerCAmelCase  :      str    =						None
     __lowerCAmelCase  :      Optional[Any]    =						None
     __lowerCAmelCase  :      List[str]    =						None
     if self.use_labels:
        __lowerCAmelCase  :      Optional[Any]    =						ids_tensor([self.batch_size]	,							self.type_sequence_label_size   )
        __lowerCAmelCase  :      Tuple    =						ids_tensor([self.batch_size, self.seq_length]	,							self.num_labels   )
        __lowerCAmelCase  :      Tuple    =						ids_tensor([self.batch_size]	,							self.num_choices   )

     __lowerCAmelCase  :      str    =						self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







  def 	SCREAMING_SNAKE_CASE   (      self			:   Union[str, Any]   )							->     Tuple:






     """simple docstring"""







     return AlbertConfig(
         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_size=self.intermediate_size	,							hidden_act=self.hidden_act	,							hidden_dropout_prob=self.hidden_dropout_prob	,							attention_probs_dropout_prob=self.attention_probs_dropout_prob	,							max_position_embeddings=self.max_position_embeddings	,							type_vocab_size=self.type_vocab_size	,							initializer_range=self.initializer_range	,							num_hidden_groups=self.num_hidden_groups	,							)







  def 	SCREAMING_SNAKE_CASE   (      self			:   int	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Any   )							->     Union[str, Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						AlbertModel(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      Optional[int]    =						model(lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase   )
     __lowerCAmelCase  :      Dict    =						model(lowerCAmelCase	,							token_type_ids=lowerCAmelCase   )
     __lowerCAmelCase  :      Dict    =						model(lowerCAmelCase   )
     self.parent.assertEqual(result.last_hidden_state.shape	,							(self.batch_size, self.seq_length, self.hidden_size)   )
     self.parent.assertEqual(result.pooler_output.shape	,							(self.batch_size, self.hidden_size)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   str	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   Optional[Any]   )							->     int:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						AlbertForPreTraining(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      Optional[int]    =						model(
         lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							labels=lowerCAmelCase	,							sentence_order_label=lowerCAmelCase	,							)
     self.parent.assertEqual(result.prediction_logits.shape	,							(self.batch_size, self.seq_length, self.vocab_size)   )
     self.parent.assertEqual(result.sop_logits.shape	,							(self.batch_size, config.num_labels)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   Any   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						AlbertForMaskedLM(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      List[Any]    =						model(lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							labels=lowerCAmelCase   )
     self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.seq_length, self.vocab_size)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   str   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      List[Any]    =						AlbertForQuestionAnswering(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      Any    =						model(
         lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							start_positions=lowerCAmelCase	,							end_positions=lowerCAmelCase	,							)
     self.parent.assertEqual(result.start_logits.shape	,							(self.batch_size, self.seq_length)   )
     self.parent.assertEqual(result.end_logits.shape	,							(self.batch_size, self.seq_length)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   str	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Union[str, Any]   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[int]    =						self.num_labels
     __lowerCAmelCase  :      Optional[int]    =						AlbertForSequenceClassification(lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      Optional[Any]    =						model(lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							labels=lowerCAmelCase   )
     self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_labels)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   int	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Tuple	,							lowerCAmelCase			:   Any	,							lowerCAmelCase			:   Tuple   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						self.num_labels
     __lowerCAmelCase  :      Optional[Any]    =						AlbertForTokenClassification(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      Dict    =						model(lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							labels=lowerCAmelCase   )
     self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.seq_length, self.num_labels)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   Union[str, Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   List[Any]	,							lowerCAmelCase			:   List[str]   )							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						self.num_choices
     __lowerCAmelCase  :      Any    =						AlbertForMultipleChoice(config=lowerCAmelCase   )
     model.to(lowerCAmelCase   )
     model.eval()
     __lowerCAmelCase  :      List[Any]    =						input_ids.unsqueeze(1   ).expand(-1	,							self.num_choices	,							-1   ).contiguous()
     __lowerCAmelCase  :      Any    =						token_type_ids.unsqueeze(1   ).expand(-1	,							self.num_choices	,							-1   ).contiguous()
     __lowerCAmelCase  :      Optional[int]    =						input_mask.unsqueeze(1   ).expand(-1	,							self.num_choices	,							-1   ).contiguous()
     __lowerCAmelCase  :      List[str]    =						model(
         lowerCAmelCase	,							attention_mask=lowerCAmelCase	,							token_type_ids=lowerCAmelCase	,							labels=lowerCAmelCase	,							)
     self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_choices)   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     str:






     """simple docstring"""







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






@require_torch
class SCREAMING_SNAKE_CASE     (  a_       ,							a_       ,							unittest.TestCase			):
  """simple docstring"""






  lowerCamelCase   :      Optional[Any]										=(
      (
          AlbertModel,
          AlbertForPreTraining,
          AlbertForMaskedLM,
          AlbertForMultipleChoice,
          AlbertForSequenceClassification,
          AlbertForTokenClassification,
          AlbertForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase   :      Any										=(
      {
          "feature-extraction": AlbertModel,
          "fill-mask": AlbertForMaskedLM,
          "question-answering": AlbertForQuestionAnswering,
          "text-classification": AlbertForSequenceClassification,
          "token-classification": AlbertForTokenClassification,
          "zero-shot": AlbertForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowerCamelCase   :      List[Any]										=True







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]	,							lowerCAmelCase			:   Dict	,							lowerCAmelCase			:   Optional[int]	,							lowerCAmelCase			:   Optional[Any]=False   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      List[Any]    =						super()._prepare_for_class(lowerCAmelCase	,							lowerCAmelCase	,							return_labels=lowerCAmelCase   )

     if return_labels:
        if model_class in get_values(lowerCAmelCase   ):
           __lowerCAmelCase  :      Optional[Any]    =						torch.zeros(
               (self.model_tester.batch_size, self.model_tester.seq_length)	,							dtype=torch.long	,							device=lowerCAmelCase   )
           __lowerCAmelCase  :      List[str]    =						torch.zeros(
               self.model_tester.batch_size	,							dtype=torch.long	,							device=lowerCAmelCase   )
     return inputs_dict







  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						AlbertModelTester(self   )
     __lowerCAmelCase  :      Union[str, Any]    =						ConfigTester(self	,							config_class=lowerCAmelCase	,							hidden_size=37   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     List[Any]:






     """simple docstring"""







     self.config_tester.run_common_tests()







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      int    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      Optional[Any]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      Union[str, Any]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      List[Any]    =						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict   )							->     Tuple:






     """simple docstring"""







     __lowerCAmelCase  :      List[str]    =						self.model_tester.prepare_config_and_inputs()
     for type in ["absolute", "relative_key", "relative_key_query"]:
        __lowerCAmelCase  :      Tuple    =						type
        self.model_tester.create_and_check_model(*lowerCAmelCase   )







  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[Any]   )							->     Dict:






     """simple docstring"""







     for model_name in ALBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowerCAmelCase  :      Tuple    =						AlbertModel.from_pretrained(lowerCAmelCase   )
        self.assertIsNotNone(lowerCAmelCase   )






@require_torch
class SCREAMING_SNAKE_CASE     (  unittest.TestCase			):
  """simple docstring"""






  @slow
  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     str:






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						AlbertModel.from_pretrained("""albert-base-v2"""   )
     __lowerCAmelCase  :      Dict    =						torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
     __lowerCAmelCase  :      Dict    =						torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
     with torch.no_grad():
        __lowerCAmelCase  :      Any    =						model(lowerCAmelCase	,							attention_mask=lowerCAmelCase   )[0]
     __lowerCAmelCase  :      Union[str, Any]    =						torch.Size((1, 11, 7_68)   )
     self.assertEqual(output.shape	,							lowerCAmelCase   )
     __lowerCAmelCase  :      List[Any]    =						torch.tensor(
         [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]   )

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,							lowerCAmelCase	,							atol=1e-4   )   )





import numpy as np
import qiskit





def      snake_case_				(__A					:						int = 8 ,       __A					:						int | None = None					)      ->     str:
   __lowerCAmelCase  :      List[Any]    =						np.random.default_rng(seed=__A					)

   # Roughly 25% of the qubits will contribute to the key.
   # So we take more than we need.
   __lowerCAmelCase  :      Tuple    =						6 * key_len
   # Measurement basis for Alice's qubits.
   __lowerCAmelCase  :      List[Any]    =						rng.integers(2 ,       size=__A					)
   # The set of states Alice will prepare.
   __lowerCAmelCase  :      List[str]    =						rng.integers(2 ,       size=__A					)
   # Measurement basis for Bob's qubits.
   __lowerCAmelCase  :      List[Any]    =						rng.integers(2 ,       size=__A					)

   # Quantum Circuit to simulate BB84
   __lowerCAmelCase  :      int    =						qiskit.QuantumCircuit(__A ,       name="""BB84"""					)

   # Alice prepares her qubits according to rules above.
   for index, _ in enumerate(__A					):
      if alice_state[index] == 1:
         bbaa_circ.x(__A					)
      if alice_basis[index] == 1:
         bbaa_circ.h(__A					)
   bbaa_circ.barrier()

   # Bob measures the received qubits according to rules above.
   for index, _ in enumerate(__A					):
      if bob_basis[index] == 1:
         bbaa_circ.h(__A					)

   bbaa_circ.barrier()
   bbaa_circ.measure_all()

   # Simulate the quantum circuit.
   __lowerCAmelCase  :      Optional[Any]    =						qiskit.Aer.get_backend("""aer_simulator"""					)
   # We only need to run one shot because the key is unique.
   # Multiple shots will produce the same key.
   __lowerCAmelCase  :      Optional[Any]    =						qiskit.execute(__A ,       __A ,       shots=1 ,       seed_simulator=__A					)
   # Returns the result of measurement.
   __lowerCAmelCase  :      List[Any]    =						job.result().get_counts(__A					).most_frequent()

   # Extracting the generated key from the simulation results.
   # Only keep measurement results where Alice and Bob chose the same basis.
   __lowerCAmelCase  :      Optional[int]    =						"""""".join(
       [
           result_bit
           for alice_basis_bit, bob_basis_bit, result_bit in zip(
               __A ,       __A ,       __A					)
           if alice_basis_bit == bob_basis_bit
       ]					)

   # Get final key. Pad with 0 if too short, otherwise truncate.
   __lowerCAmelCase  :      Tuple    =						gen_key[:key_len] if len(__A					) >= key_len else gen_key.ljust(__A ,       """0"""					)
   return key


if __name__ == "__main__":
   print(F'The generated key is : {bbaa(8, seed=0)}')
   from doctest import testmod

   testmod()


