



'''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 				lowercase__      (				lowercase  ):





	def __init__(					self   :							Any					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							List[Any]=13					,lowerCamelCase__   :							List[str]=7					,lowerCamelCase__   :							Union[str, Any]=True					,lowerCamelCase__   :							Union[str, Any]=True					,lowerCamelCase__   :							Union[str, Any]=False					,lowerCamelCase__   :							Optional[int]=True					,lowerCamelCase__   :							int=99					,lowerCamelCase__   :							int=32					,lowerCamelCase__   :							List[Any]=5					,lowerCamelCase__   :							Dict=4					,lowerCamelCase__   :							Tuple=64					,lowerCamelCase__   :							Union[str, Any]="gelu"					,lowerCamelCase__   :							Tuple=0.1					,lowerCamelCase__   :							str=0.1					,lowerCamelCase__   :							Optional[int]=512					,lowerCamelCase__   :							Dict=16					,lowerCamelCase__   :							List[str]=2					,lowerCamelCase__   :							Optional[Any]=0.0_2					,lowerCamelCase__   :							List[Any]=3					,lowerCamelCase__   :							List[str]=4					,lowerCamelCase__   :							Any=None					,lowerCamelCase__   :							Dict=2					,lowerCamelCase__   :							int=2					,lowerCamelCase__   :							Tuple=2					,lowerCamelCase__   :							Union[str, Any]=2					,lowerCamelCase__   :							List[str]=4					,lowerCamelCase__   :							Optional[Any]=1					,):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  parent
							_UpperCamelCase   :						List[str]												=  batch_size
							_UpperCamelCase   :						List[Any]												=  seq_length
							_UpperCamelCase   :						List[str]												=  is_training
							_UpperCamelCase   :						Union[str, Any]												=  use_input_mask
							_UpperCamelCase   :						Tuple												=  use_token_type_ids
							_UpperCamelCase   :						int												=  use_labels
							_UpperCamelCase   :						Any												=  vocab_size
							_UpperCamelCase   :						Tuple												=  hidden_size
							_UpperCamelCase   :						int												=  num_hidden_layers
							_UpperCamelCase   :						Optional[Any]												=  num_attention_heads
							_UpperCamelCase   :						Dict												=  intermediate_size
							_UpperCamelCase   :						int												=  hidden_act
							_UpperCamelCase   :						int												=  hidden_dropout_prob
							_UpperCamelCase   :						Optional[Any]												=  attention_probs_dropout_prob
							_UpperCamelCase   :						Dict												=  max_position_embeddings
							_UpperCamelCase   :						Union[str, Any]												=  type_vocab_size
							_UpperCamelCase   :						Optional[Any]												=  type_sequence_label_size
							_UpperCamelCase   :						Any												=  initializer_range
							_UpperCamelCase   :						Any												=  num_labels
							_UpperCamelCase   :						Any												=  num_choices
							_UpperCamelCase   :						str												=  scope
							_UpperCamelCase   :						Dict												=  q_groups
							_UpperCamelCase   :						int												=  k_groups
							_UpperCamelCase   :						Optional[int]												=  v_groups
							_UpperCamelCase   :						Any												=  post_attention_groups
							_UpperCamelCase   :						Tuple												=  intermediate_groups
							_UpperCamelCase   :						List[str]												=  output_groups





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  ids_tensor([self.batch_size, self.seq_length]					,self.vocab_size					)

							_UpperCamelCase   :						Union[str, Any]												=  None
							if self.use_input_mask:
													_UpperCamelCase   :						Optional[int]												=  random_attention_mask([self.batch_size, self.seq_length]					)

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

							_UpperCamelCase   :						Any												=  self.get_config()

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





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							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   :							List[str]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  SqueezeBertModel(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Union[str, Any]												=  model(lowerCamelCase__					,lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  model(lowerCamelCase__					)
							self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)					)





	def       UpperCamelCase_    (					self   :							Tuple					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							Optional[int]					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  SqueezeBertForMaskedLM(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Any												=  model(lowerCamelCase__					,attention_mask=lowerCamelCase__					,labels=lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)					)





	def       UpperCamelCase_    (					self   :							Union[str, Any]					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							int					,lowerCamelCase__   :							int					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  SqueezeBertForQuestionAnswering(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						List[Any]												=  model(
							    lowerCamelCase__					,attention_mask=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       UpperCamelCase_    (					self   :							Any					,lowerCamelCase__   :							List[Any]					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Union[str, Any]					,lowerCamelCase__   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.num_labels
							_UpperCamelCase   :						List[Any]												=  SqueezeBertForSequenceClassification(lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Any												=  model(lowerCamelCase__					,attention_mask=lowerCamelCase__					,labels=lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels)					)





	def       UpperCamelCase_    (					self   :							str					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							str					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Optional[Any]					,lowerCamelCase__   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  self.num_labels
							_UpperCamelCase   :						Optional[Any]												=  SqueezeBertForTokenClassification(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()

							_UpperCamelCase   :						Optional[Any]												=  model(lowerCamelCase__					,attention_mask=lowerCamelCase__					,labels=lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.num_labels)					)





	def       UpperCamelCase_    (					self   :							Dict					,lowerCamelCase__   :							int					,lowerCamelCase__   :							Tuple					,lowerCamelCase__   :							Any					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							Dict					,lowerCamelCase__   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  self.num_choices
							_UpperCamelCase   :						List[str]												=  SqueezeBertForMultipleChoice(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							_UpperCamelCase   :						Optional[Any]												=  input_ids.unsqueeze(1					).expand(-1					,self.num_choices					,-1					).contiguous()
							_UpperCamelCase   :						int												=  input_mask.unsqueeze(1					).expand(-1					,self.num_choices					,-1					).contiguous()
							_UpperCamelCase   :						Optional[Any]												=  model(
							    lowerCamelCase__					,attention_mask=lowerCamelCase__					,labels=lowerCamelCase__					,)
							self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_choices)					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  self.prepare_config_and_inputs()
							((_UpperCamelCase)							,     (_UpperCamelCase)							,     (_UpperCamelCase)							,     (_UpperCamelCase)							,     (_UpperCamelCase)							,     (_UpperCamelCase))   :						Optional[Any]												=  config_and_inputs
							_UpperCamelCase   :						Tuple												=  {'input_ids': input_ids, 'attention_mask': input_mask}
							return config, inputs_dict






@require_torch
class 				lowercase__      (				lowercase		,   lowercase		,   unittest.TestCase  ):
	lowercase__    							=     (
	    (
	        SqueezeBertModel,
	        SqueezeBertForMaskedLM,
	        SqueezeBertForMultipleChoice,
	        SqueezeBertForQuestionAnswering,
	        SqueezeBertForSequenceClassification,
	        SqueezeBertForTokenClassification,
	    )
	    if is_torch_available()
	    else None
	)
	lowercase__    							=     (
	    {
	        """feature-extraction""": SqueezeBertModel,
	        """fill-mask""": SqueezeBertForMaskedLM,
	        """question-answering""": SqueezeBertForQuestionAnswering,
	        """text-classification""": SqueezeBertForSequenceClassification,
	        """token-classification""": SqueezeBertForTokenClassification,
	        """zero-shot""": SqueezeBertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowercase__    							=     False
	lowercase__    							=     True
	lowercase__    							=     False





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  SqueezeBertModelTester(self					)
							_UpperCamelCase   :						int												=  ConfigTester(self					,config_class=lowerCamelCase__					,dim=37					)





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							self.config_tester.run_common_tests()





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_model(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_masked_lm(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Union[str, Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_question_answering(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_sequence_classification(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_token_classification(*lowerCamelCase__					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Union[str, Any]												=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_multiple_choice(*lowerCamelCase__					)





	@slow
	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													_UpperCamelCase   :						Tuple												=  SqueezeBertModel.from_pretrained(lowerCamelCase__					)
													self.assertIsNotNone(lowerCamelCase__					)






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





	@slow
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  SqueezeBertForSequenceClassification.from_pretrained('squeezebert/squeezebert-mnli'					)

							_UpperCamelCase   :						Optional[int]												=  torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]]					)
							_UpperCamelCase   :						Optional[int]												=  model(lowerCamelCase__					)[0]
							_UpperCamelCase   :						int												=  torch.Size((1, 3)					)
							self.assertEqual(output.shape					,lowerCamelCase__					)
							_UpperCamelCase   :						Any												=  torch.tensor([[0.6_4_0_1, -0.0_3_4_9, -0.6_0_4_1]]					)
							self.assertTrue(torch.allclose(lowerCamelCase__					,lowerCamelCase__					,atol=1E-4					)					)







'''simple docstring'''
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import ViTImageProcessor






class 				lowercase__      (				unittest.TestCase  ):





	def __init__(					self   :							int					,lowerCamelCase__   :							List[str]					,lowerCamelCase__   :							Optional[Any]=13					,lowerCamelCase__   :							List[Any]=3					,lowerCamelCase__   :							Optional[Any]=224					,lowerCamelCase__   :							int=30					,lowerCamelCase__   :							str=400					,lowerCamelCase__   :							str=True					,lowerCamelCase__   :							Any=None					,lowerCamelCase__   :							Tuple=True					,lowerCamelCase__   :							Tuple=[0.5, 0.5, 0.5]					,lowerCamelCase__   :							Optional[int]=[0.5, 0.5, 0.5]					,):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  size if size is not None else {'height': 18, 'width': 18}
							_UpperCamelCase   :						List[Any]												=  parent
							_UpperCamelCase   :						Union[str, Any]												=  batch_size
							_UpperCamelCase   :						Dict												=  num_channels
							_UpperCamelCase   :						List[str]												=  image_size
							_UpperCamelCase   :						Optional[Any]												=  min_resolution
							_UpperCamelCase   :						Dict												=  max_resolution
							_UpperCamelCase   :						Tuple												=  do_resize
							_UpperCamelCase   :						Optional[int]												=  size
							_UpperCamelCase   :						Tuple												=  do_normalize
							_UpperCamelCase   :						str												=  image_mean
							_UpperCamelCase   :						int												=  image_std





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							}






@require_torch
@require_vision
class 				lowercase__      (				lowercase		,   unittest.TestCase  ):
	lowercase__    							=     ViTImageProcessor if is_vision_available() else None





	def       UpperCamelCase_    (					self   :							Optional[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						Dict												=  EfficientFormerImageProcessorTester(self					)





	@property
	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							return self.image_proc_tester.prepare_image_processor_dict()





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[int]												=  self.image_processing_class(**self.image_processor_dict					)
							self.assertTrue(hasattr(lowerCamelCase__					,'image_mean'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'image_std'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'do_normalize'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'do_resize'					)					)
							self.assertTrue(hasattr(lowerCamelCase__					,'size'					)					)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							pass





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							# Initialize image_processor
							_UpperCamelCase   :						str												=  self.image_processing_class(**self.image_processor_dict					)
							# create random PIL images
							_UpperCamelCase   :						Optional[Any]												=  prepare_image_inputs(self.image_proc_tester					,equal_resolution=lowerCamelCase__					)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__					,Image.Image					)

							# Test not batched input
							_UpperCamelCase   :						int												=  image_processor(image_inputs[0]					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)

							# Test batched
							_UpperCamelCase   :						int												=  image_processor(lowerCamelCase__					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)





	def       UpperCamelCase_    (					self   :							List[str]					):







							'''simple docstring'''
							# Initialize image_processor
							_UpperCamelCase   :						Any												=  self.image_processing_class(**self.image_processor_dict					)
							# create random numpy tensors
							_UpperCamelCase   :						Optional[int]												=  prepare_image_inputs(self.image_proc_tester					,equal_resolution=lowerCamelCase__					,numpify=lowerCamelCase__					)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__					,np.ndarray					)

							# Test not batched input
							_UpperCamelCase   :						Union[str, Any]												=  image_processor(image_inputs[0]					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)

							# Test batched
							_UpperCamelCase   :						Optional[Any]												=  image_processor(lowerCamelCase__					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							# Initialize image_processor
							_UpperCamelCase   :						Any												=  self.image_processing_class(**self.image_processor_dict					)
							# create random PyTorch tensors
							_UpperCamelCase   :						Any												=  prepare_image_inputs(self.image_proc_tester					,equal_resolution=lowerCamelCase__					,torchify=lowerCamelCase__					)
							for image in image_inputs:
													self.assertIsInstance(lowerCamelCase__					,torch.Tensor					)

							# Test not batched input
							_UpperCamelCase   :						Dict												=  image_processor(image_inputs[0]					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        1,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)

							# Test batched
							_UpperCamelCase   :						Any												=  image_processor(lowerCamelCase__					,return_tensors='pt'					).pixel_values
							self.assertEqual(
							    encoded_images.shape					,(
							        self.image_proc_tester.batch_size,
							        self.image_proc_tester.num_channels,
							        self.image_proc_tester.size['height'],
							        self.image_proc_tester.size['width'],
							    )					,)


