


"""simple docstring"""




import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, 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 (
				    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SqueezeBertForMaskedLM,
				    SqueezeBertForMultipleChoice,
				    SqueezeBertForQuestionAnswering,
				    SqueezeBertForSequenceClassification,
				    SqueezeBertForTokenClassification,
				    SqueezeBertModel,
				)
class 					__lowerCAmelCase					(   __SCREAMING_SNAKE_CASE       ):


		'''simple docstring'''



		def __init__(  self							, _a							, _a=13							, _a=7							, _a=True							, _a=True							, _a=False							, _a=True							, _a=99							, _a=32							, _a=5							, _a=4							, _a=64							, _a="gelu"							, _a=0.1							, _a=0.1							, _a=512							, _a=16							, _a=2							, _a=0.02							, _a=3							, _a=4							, _a=None							, _a=2							, _a=2							, _a=2							, _a=2							, _a=4							, _a=1							, ):
				__a           =			parent
				__a           =			batch_size
				__a           =			seq_length
				__a           =			is_training
				__a           =			use_input_mask
				__a           =			use_token_type_ids
				__a           =			use_labels
				__a           =			vocab_size
				__a           =			hidden_size
				__a           =			num_hidden_layers
				__a           =			num_attention_heads
				__a           =			intermediate_size
				__a           =			hidden_act
				__a           =			hidden_dropout_prob
				__a           =			attention_probs_dropout_prob
				__a           =			max_position_embeddings
				__a           =			type_vocab_size
				__a           =			type_sequence_label_size
				__a           =			initializer_range
				__a           =			num_labels
				__a           =			num_choices
				__a           =			scope
				__a           =			q_groups
				__a           =			k_groups
				__a           =			v_groups
				__a           =			post_attention_groups
				__a           =			intermediate_groups
				__a           =			output_groups

		def 			__UpperCAmelCase       (  self      ):
				__a           =			ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size      )

				__a           =			None
				if self.use_input_mask:
						__a           =			random_attention_mask([self.batch_size, self.seq_length]      )

				__a           =			None
				__a           =			None
				__a           =			None
				if self.use_labels:
						__a           =			ids_tensor([self.batch_size]							, self.type_sequence_label_size      )
						__a           =			ids_tensor([self.batch_size, self.seq_length]							, self.num_labels      )
						__a           =			ids_tensor([self.batch_size]							, self.num_choices      )

				__a           =			self.get_config()

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

		def 			__UpperCAmelCase       (  self      ):
				return SqueezeBertConfig(
				    embedding_size=self.hidden_size							, 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							, attention_probs_dropout_prob=self.hidden_dropout_prob							, attention_dropout=self.attention_probs_dropout_prob							, max_position_embeddings=self.max_position_embeddings							, initializer_range=self.initializer_range							, q_groups=self.q_groups							, k_groups=self.k_groups							, v_groups=self.v_groups							, post_attention_groups=self.post_attention_groups							, intermediate_groups=self.intermediate_groups							, output_groups=self.output_groups							, )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			SqueezeBertModel(config=_a      )
				model.to(_a      )
				model.eval()
				__a           =			model(_a							, _a      )
				__a           =			model(_a      )
				self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)      )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			SqueezeBertForMaskedLM(config=_a      )
				model.to(_a      )
				model.eval()
				__a           =			model(_a							, attention_mask=_a							, labels=_a      )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)      )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			SqueezeBertForQuestionAnswering(config=_a      )
				model.to(_a      )
				model.eval()
				__a           =			model(
				    _a							, attention_mask=_a							, start_positions=_a							, end_positions=_a      )
				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 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			self.num_labels
				__a           =			SqueezeBertForSequenceClassification(_a      )
				model.to(_a      )
				model.eval()
				__a           =			model(_a							, attention_mask=_a							, labels=_a      )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)      )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			self.num_labels
				__a           =			SqueezeBertForTokenClassification(config=_a      )
				model.to(_a      )
				model.eval()

				__a           =			model(_a							, attention_mask=_a							, labels=_a      )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.num_labels)      )

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			self.num_choices
				__a           =			SqueezeBertForMultipleChoice(config=_a      )
				model.to(_a      )
				model.eval()
				__a           =			input_ids.unsqueeze(1      ).expand(-1							, self.num_choices							, -1      ).contiguous()
				__a           =			input_mask.unsqueeze(1      ).expand(-1							, self.num_choices							, -1      ).contiguous()
				__a           =			model(
				    _a							, attention_mask=_a							, labels=_a							, )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_choices)      )





		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.prepare_config_and_inputs()
				((__a)      ,     (__a)      ,     (__a)      ,     (__a)      ,     (__a)      ,     (__a))           =			config_and_inputs
				__a           =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
				return config, inputs_dict







@require_torch
class 					__lowerCAmelCase					(   __SCREAMING_SNAKE_CASE     ,			__SCREAMING_SNAKE_CASE     ,			unittest.TestCase       ):


		'''simple docstring'''



		__UpperCAmelCase		:			Tuple										=				(
		    (
		        SqueezeBertModel,
		        SqueezeBertForMaskedLM,
		        SqueezeBertForMultipleChoice,
		        SqueezeBertForQuestionAnswering,
		        SqueezeBertForSequenceClassification,
		        SqueezeBertForTokenClassification,
		    )
		    if is_torch_available()
		    else None
		)
		__UpperCAmelCase		:			int										=				(
		    {
		        'feature-extraction': SqueezeBertModel,
		        'fill-mask': SqueezeBertForMaskedLM,
		        'question-answering': SqueezeBertForQuestionAnswering,
		        'text-classification': SqueezeBertForSequenceClassification,
		        'token-classification': SqueezeBertForTokenClassification,
		        'zero-shot': SqueezeBertForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		__UpperCAmelCase		:			List[str]										=				False
		__UpperCAmelCase		:			Optional[int]										=				True
		__UpperCAmelCase		:			Dict										=				False

		def 			__UpperCAmelCase       (  self      ):
				__a           =			SqueezeBertModelTester(self      )
				__a           =			ConfigTester(self							, config_class=_a							, dim=37      )

		def 			__UpperCAmelCase       (  self      ):
				self.config_tester.run_common_tests()

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_squeezebert_model(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_squeezebert_for_masked_lm(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_squeezebert_for_question_answering(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_squeezebert_for_token_classification(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_a      )





		@slow
		def 			__UpperCAmelCase       (  self      ):
				for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__a           =			SqueezeBertModel.from_pretrained(_a      )
						self.assertIsNotNone(_a      )







@require_sentencepiece
@require_tokenizers
@require_torch
class 					__lowerCAmelCase					(   unittest.TestCase       ):


		'''simple docstring'''



		@slow
		def 			__UpperCAmelCase       (  self      ):
				__a           =			SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''      )

				__a           =			torch.tensor([[1, 29_414, 232, 328, 740, 1_140, 12_695, 69, 13, 1_588, 2]]      )
				__a           =			model(_a      )[0]
				__a           =			torch.Size((1, 3)      )
				self.assertEqual(output.shape							, _a      )
				__a           =			torch.tensor([[0.6401, -0.0349, -0.6041]]      )
				self.assertTrue(torch.allclose(_a							, _a							, atol=1E-4      )      )










"""simple docstring"""




lowercase_			     =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

def        lowercase							(		lowerCAmelCase__ :     bytes			) ->      bytes:
		# Make sure the supplied data is a bytes-like object
		if not isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			):
				__a           =			f'''a bytes-like object is required, not \'{data.__class__.__name__}\''''
				raise TypeError(lowerCAmelCase__			)

		__a           =			''''''.join(bin(lowerCAmelCase__			)[2:].zfill(8			) for byte in data			)

		__a           =			len(lowerCAmelCase__			) % 6 != 0

		if padding_needed:
				# The padding that will be added later
				__a           =			b'''=''' * ((6 - len(lowerCAmelCase__			) % 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(lowerCAmelCase__			) % 6)
		else:
				__a           =			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(lowerCAmelCase__			)    ,					6			)			).encode()
		    + padding
		)

def        lowercase							(		lowerCAmelCase__ :     str			) ->      bytes:
		# Make sure encoded_data is either a string or a bytes-like object
		if not isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			) and not isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			):
				__a           =			(
				    '''argument should be a bytes-like object or ASCII string, '''
				    f'''not \'{encoded_data.__class__.__name__}\''''
				)
				raise TypeError(lowerCAmelCase__			)

		# 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(lowerCAmelCase__    ,					lowerCAmelCase__			):
				try:
						__a           =			encoded_data.decode('''utf-8'''			)
				except UnicodeDecodeError:
						raise ValueError('''base64 encoded data should only contain ASCII characters'''			)

		__a           =			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(lowerCAmelCase__			) % 4 == 0 and padding < 3, "Incorrect padding"

		if padding:
				# Remove padding if there is one
				__a           =			encoded_data[:-padding]

				__a           =			''''''.join(
				    bin(B64_CHARSET.index(lowerCAmelCase__			)			)[2:].zfill(6			) for char in encoded_data			)[: -padding * 2]
		else:
				__a           =			''''''.join(
				    bin(B64_CHARSET.index(lowerCAmelCase__			)			)[2:].zfill(6			) for char in encoded_data			)

		__a           =			[
		    int(binary_stream[index : index + 8]    ,					2			)
		    for index in range(0    ,					len(lowerCAmelCase__			)    ,					8			)
		]

		return bytes(lowerCAmelCase__			)


if __name__ == "__main__":
				import doctest

				doctest.testmod()






