


"""simple docstring"""







import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       import jax.numpy as jnp

       from transformers.models.albert.modeling_flax_albert import (
           FlaxAlbertForMaskedLM,
           FlaxAlbertForMultipleChoice,
           FlaxAlbertForPreTraining,
           FlaxAlbertForQuestionAnswering,
           FlaxAlbertForSequenceClassification,
           FlaxAlbertForTokenClassification,
           FlaxAlbertModel,
       )







class  UpperCamelCase	(    unittest.TestCase						):
   def __init__(    self  ,__UpperCamelCase  ,__UpperCamelCase=13  ,__UpperCamelCase=7  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=99  ,__UpperCamelCase=32  ,__UpperCamelCase=5  ,__UpperCamelCase=4  ,__UpperCamelCase=37  ,__UpperCamelCase="gelu"  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=512  ,__UpperCamelCase=16  ,__UpperCamelCase=2  ,__UpperCamelCase=0.02  ,__UpperCamelCase=4  ,)			->   List[str]:


        '''simple docstring'''


        lowercase_			:       List[str]									=			parent
        lowercase_			:       Dict									=			batch_size
        lowercase_			:       Optional[int]									=			seq_length
        lowercase_			:       Optional[Any]									=			is_training
        lowercase_			:       Dict									=			use_attention_mask
        lowercase_			:       Dict									=			use_token_type_ids
        lowercase_			:       int									=			use_labels
        lowercase_			:       Union[str, Any]									=			vocab_size
        lowercase_			:       List[str]									=			hidden_size
        lowercase_			:       List[Any]									=			num_hidden_layers
        lowercase_			:       str									=			num_attention_heads
        lowercase_			:       List[str]									=			intermediate_size
        lowercase_			:       List[str]									=			hidden_act
        lowercase_			:       str									=			hidden_dropout_prob
        lowercase_			:       Any									=			attention_probs_dropout_prob
        lowercase_			:       Union[str, Any]									=			max_position_embeddings
        lowercase_			:       List[str]									=			type_vocab_size
        lowercase_			:       Tuple									=			type_sequence_label_size
        lowercase_			:       str									=			initializer_range
        lowercase_			:       List[str]									=			num_choices
   def     _UpperCAmelCase     (    self    )			->   int:


        '''simple docstring'''


        lowercase_			:       str									=			ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size    )

        lowercase_			:       Optional[Any]									=			None
        if self.use_attention_mask:
             lowercase_			:       List[Any]									=			random_attention_mask([self.batch_size, self.seq_length]    )

        lowercase_			:       Dict									=			None
        if self.use_token_type_ids:
             lowercase_			:       Union[str, Any]									=			ids_tensor([self.batch_size, self.seq_length]  ,self.type_vocab_size    )

        lowercase_			:       Union[str, Any]									=			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  ,is_decoder=__UpperCamelCase  ,initializer_range=self.initializer_range  ,)

        return config, input_ids, token_type_ids, attention_mask







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


        '''simple docstring'''


        lowercase_			:       Any									=			self.prepare_config_and_inputs()
        lowercase_			:       Dict									=			config_and_inputs
        lowercase_			:       Dict									=			{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': attention_mask}
        return config, inputs_dict







@require_flax
class  UpperCamelCase	(    lowercase_		,   unittest.TestCase						):
   lowercase							=						(
       (
           FlaxAlbertModel,
           FlaxAlbertForPreTraining,
           FlaxAlbertForMaskedLM,
           FlaxAlbertForMultipleChoice,
           FlaxAlbertForQuestionAnswering,
           FlaxAlbertForSequenceClassification,
           FlaxAlbertForTokenClassification,
           FlaxAlbertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )
   def     _UpperCAmelCase     (    self    )			->   int:


        '''simple docstring'''


        lowercase_			:       Tuple									=			FlaxAlbertModelTester(self    )







   @slow
   def     _UpperCAmelCase     (    self    )			->   Any:


        '''simple docstring'''


        for model_class_name in self.all_model_classes:
             lowercase_			:       int									=			model_class_name.from_pretrained('albert-base-v2'    )
             lowercase_			:       List[str]									=			model(np.ones((1, 1)    )    )
             self.assertIsNotNone(__UpperCamelCase    )







@require_flax
class  UpperCamelCase	(    unittest.TestCase						):
   @slow
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        lowercase_			:       Union[str, Any]									=			FlaxAlbertModel.from_pretrained('albert-base-v2'    )
        lowercase_			:       Optional[int]									=			np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
        lowercase_			:       List[str]									=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
        lowercase_			:       Union[str, Any]									=			model(__UpperCamelCase  ,attention_mask=__UpperCamelCase    )[0]
        lowercase_			:       List[Any]									=			(1, 11, 768)
        self.assertEqual(output.shape  ,__UpperCamelCase    )
        lowercase_			:       Optional[Any]									=			np.array(
            [[[-0.6513, 1.5035, -0.2766], [-0.6515, 1.5046, -0.2780], [-0.6512, 1.5049, -0.2784]]]    )

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


"""simple docstring"""







__SCREAMING_SNAKE_CASE								="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"



def     lowercase__( __SCREAMING_SNAKE_CASE							:  bytes   ):
     # Make sure the supplied data is a bytes-like object
     if not isinstance(__SCREAMING_SNAKE_CASE  ,    __SCREAMING_SNAKE_CASE   ):
          lowercase_			:       Dict									=			F'''a bytes-like object is required, not \'{data.__class__.__name__}\''''
          raise TypeError(__SCREAMING_SNAKE_CASE   )

     lowercase_			:       List[Any]									=			''.join(bin(__SCREAMING_SNAKE_CASE   )[2:].zfill(8   ) for byte in data   )

     lowercase_			:       List[Any]									=			len(__SCREAMING_SNAKE_CASE   ) % 6 != 0

     if padding_needed:
          # The padding that will be added later
          lowercase_			:       Optional[int]									=			b'=' * ((6 - len(__SCREAMING_SNAKE_CASE   ) % 6) // 2)

          # Append binary_stream with arbitrary binary digits (0's by default) to make its
          # length a multiple of 6.
          binary_stream += "0" * (6 - len(__SCREAMING_SNAKE_CASE   ) % 6)
     else:
          lowercase_			:       List[Any]									=			b''

     # Encode every 6 binary digits to their corresponding Base64 character
     return (
         "".join(
             B64_CHARSET[int(binary_stream[index : index + 6]  ,    2   )]
             for index in range(0  ,    len(__SCREAMING_SNAKE_CASE   )  ,    6   )   ).encode()
         + padding
     )



def     lowercase__( __SCREAMING_SNAKE_CASE							:  str   ):
     # Make sure encoded_data is either a string or a bytes-like object
     if not isinstance(__SCREAMING_SNAKE_CASE  ,    __SCREAMING_SNAKE_CASE   ) and not isinstance(__SCREAMING_SNAKE_CASE  ,    __SCREAMING_SNAKE_CASE   ):
          lowercase_			:       Tuple									=			(
              'argument should be a bytes-like object or ASCII string, '
              F'''not \'{encoded_data.__class__.__name__}\''''
          )
          raise TypeError(__SCREAMING_SNAKE_CASE   )

     # In case encoded_data is a bytes-like object, make sure it contains only
     # ASCII characters so we convert it to a string object
     if isinstance(__SCREAMING_SNAKE_CASE  ,    __SCREAMING_SNAKE_CASE   ):
          try:
               lowercase_			:       List[str]									=			encoded_data.decode('utf-8'   )
          except UnicodeDecodeError:
               raise ValueError('base64 encoded data should only contain ASCII characters'   )

     lowercase_			:       List[str]									=			encoded_data.count('='   )

     # Check if the encoded string contains non base64 characters
     if padding:
          assert all(
              char in B64_CHARSET for char in encoded_data[:-padding]   ), "Invalid base64 character(s) found."
     else:
          assert all(
              char in B64_CHARSET for char in encoded_data   ), "Invalid base64 character(s) found."

     # Check the padding
     assert len(__SCREAMING_SNAKE_CASE   ) % 4 == 0 and padding < 3, "Incorrect padding"

     if padding:
          # Remove padding if there is one
          lowercase_			:       Union[str, Any]									=			encoded_data[:-padding]

          lowercase_			:       List[Any]									=			''.join(
              bin(B64_CHARSET.index(__SCREAMING_SNAKE_CASE   )   )[2:].zfill(6   ) for char in encoded_data   )[: -padding * 2]
     else:
          lowercase_			:       int									=			''.join(
              bin(B64_CHARSET.index(__SCREAMING_SNAKE_CASE   )   )[2:].zfill(6   ) for char in encoded_data   )

     lowercase_			:       str									=			[
         int(binary_stream[index : index + 8]  ,    2   )
         for index in range(0  ,    len(__SCREAMING_SNAKE_CASE   )  ,    8   )
     ]

     return bytes(__SCREAMING_SNAKE_CASE   )


if __name__ == "__main__":
       import doctest

       doctest.testmod()

