



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowercase						: List[Any]     						={
    "configuration_swinv2": ["SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Swinv2Config"],
}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_lowercase						: Optional[Any]     						=[
		    "SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "Swinv2ForImageClassification",
		    "Swinv2ForMaskedImageModeling",
		    "Swinv2Model",
		    "Swinv2PreTrainedModel",
		]


if TYPE_CHECKING:
		from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_swinva import (
				    SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST,
				    SwinvaForImageClassification,
				    SwinvaForMaskedImageModeling,
				    SwinvaModel,
				    SwinvaPreTrainedModel,
				)


else:
		import sys

		_lowercase						: Union[str, Any]     						=_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





import os
import tempfile
import unittest

from transformers import FlaubertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
					    FlaubertForMultipleChoice,
					    FlaubertForQuestionAnswering,
					    FlaubertForQuestionAnsweringSimple,
					    FlaubertForSequenceClassification,
					    FlaubertForTokenClassification,
					    FlaubertModel,
					    FlaubertWithLMHeadModel,
					)
					from transformers.models.flaubert.modeling_flaubert import FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST
class     lowerCAmelCase_     (       __lowercase ):







							def __init__(   self      :     Union[str, Any] ,			_A      :     Optional[Any] ,			_A      :     Any=13 ,			_A      :     Union[str, Any]=7 ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[str]=True ,			_A      :     List[Any]=True ,			_A      :     Optional[int]=False ,			_A      :     Any=False ,			_A      :     int=False ,			_A      :     Optional[Any]=2 ,			_A      :     Any=99 ,			_A      :     str=0 ,			_A      :     Union[str, Any]=32 ,			_A      :     List[Any]=5 ,			_A      :     Tuple=4 ,			_A      :     List[str]=0.1 ,			_A      :     Union[str, Any]=0.1 ,			_A      :     int=512 ,			_A      :     Union[str, Any]=12 ,			_A      :     List[str]=2 ,			_A      :     int=0.02 ,			_A      :     Optional[Any]=3 ,			_A      :     Any=4 ,			_A      :     Optional[int]="last" ,			_A      :     Any=None ,			_A      :     Dict=None ,			):
												_UpperCamelCase			     =     parent
												_UpperCamelCase			     =     batch_size
												_UpperCamelCase			     =     seq_length
												_UpperCamelCase			     =     is_training
												_UpperCamelCase			     =     use_input_lengths
												_UpperCamelCase			     =     use_token_type_ids
												_UpperCamelCase			     =     use_labels
												_UpperCamelCase			     =     gelu_activation
												_UpperCamelCase			     =     sinusoidal_embeddings
												_UpperCamelCase			     =     causal
												_UpperCamelCase			     =     asm
												_UpperCamelCase			     =     n_langs
												_UpperCamelCase			     =     vocab_size
												_UpperCamelCase			     =     n_special
												_UpperCamelCase			     =     hidden_size
												_UpperCamelCase			     =     num_hidden_layers
												_UpperCamelCase			     =     num_attention_heads
												_UpperCamelCase			     =     hidden_dropout_prob
												_UpperCamelCase			     =     attention_probs_dropout_prob
												_UpperCamelCase			     =     max_position_embeddings
												_UpperCamelCase			     =     type_vocab_size
												_UpperCamelCase			     =     type_sequence_label_size
												_UpperCamelCase			     =     initializer_range
												_UpperCamelCase			     =     num_labels
												_UpperCamelCase			     =     num_choices
												_UpperCamelCase			     =     summary_type
												_UpperCamelCase			     =     use_proj
												_UpperCamelCase			     =     scope







							def  UpperCamelCase_						(   self      :     List[str]		):
												_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size		)
												_UpperCamelCase			     =     random_attention_mask([self.batch_size, self.seq_length]		)

												_UpperCamelCase			     =     None
												if self.use_input_lengths:
																	_UpperCamelCase			     =     (
																	    ids_tensor([self.batch_size] ,			vocab_size=2		) + self.seq_length - 2
																	)  # small variation of seq_length

												_UpperCamelCase			     =     None
												if self.use_token_type_ids:
																	_UpperCamelCase			     =     ids_tensor([self.batch_size, self.seq_length] ,			self.n_langs		)

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

												_UpperCamelCase			     =     self.get_config()

												return (
												    config,
												    input_ids,
												    token_type_ids,
												    input_lengths,
												    sequence_labels,
												    token_labels,
												    is_impossible_labels,
												    choice_labels,
												    input_mask,
												)







							def  UpperCamelCase_						(   self      :     str		):
												return FlaubertConfig(
												    vocab_size=self.vocab_size ,			n_special=self.n_special ,			emb_dim=self.hidden_size ,			n_layers=self.num_hidden_layers ,			n_heads=self.num_attention_heads ,			dropout=self.hidden_dropout_prob ,			attention_dropout=self.attention_probs_dropout_prob ,			gelu_activation=self.gelu_activation ,			sinusoidal_embeddings=self.sinusoidal_embeddings ,			asm=self.asm ,			causal=self.causal ,			n_langs=self.n_langs ,			max_position_embeddings=self.max_position_embeddings ,			initializer_range=self.initializer_range ,			summary_type=self.summary_type ,			use_proj=self.use_proj ,			)







							def  UpperCamelCase_						(   self      :     str ,			_A      :     Union[str, Any] ,			_A      :     Optional[Any] ,			_A      :     str ,			_A      :     Tuple ,			_A      :     List[str] ,			_A      :     List[Any] ,			_A      :     Any ,			_A      :     str ,			_A      :     Optional[int] ,			):
												_UpperCamelCase			     =     FlaubertModel(config=_A		)
												model.to(_A		)
												model.eval()
												_UpperCamelCase			     =     model(_A ,			lengths=_A ,			langs=_A		)
												_UpperCamelCase			     =     model(_A ,			langs=_A		)
												_UpperCamelCase			     =     model(_A		)
												self.parent.assertEqual(result.last_hidden_state.shape ,			(self.batch_size, self.seq_length, self.hidden_size)		)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     List[Any] ,			_A      :     str ,			_A      :     Optional[int] ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     int ,			_A      :     str ,			_A      :     List[Any] ,			_A      :     Any ,			):
												_UpperCamelCase			     =     FlaubertWithLMHeadModel(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A ,			token_type_ids=_A ,			labels=_A		)
												self.parent.assertEqual(result.loss.shape ,			()		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.seq_length, self.vocab_size)		)







							def  UpperCamelCase_						(   self      :     Tuple ,			_A      :     List[str] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     Union[str, Any] ,			_A      :     str ,			_A      :     List[str] ,			_A      :     Tuple ,			_A      :     Optional[int] ,			_A      :     Dict ,			):
												_UpperCamelCase			     =     FlaubertForQuestionAnsweringSimple(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)

												_UpperCamelCase			     =     model(_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      :     Tuple ,			_A      :     str ,			_A      :     Tuple ,			_A      :     Tuple ,			_A      :     Union[str, Any] ,			_A      :     List[str] ,			_A      :     int ,			_A      :     str ,			_A      :     Dict ,			_A      :     List[Any] ,			):
												_UpperCamelCase			     =     FlaubertForQuestionAnswering(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)

												_UpperCamelCase			     =     model(
												    _A ,			start_positions=_A ,			end_positions=_A ,			cls_index=_A ,			is_impossible=_A ,			p_mask=_A ,			)

												_UpperCamelCase			     =     model(
												    _A ,			start_positions=_A ,			end_positions=_A ,			cls_index=_A ,			is_impossible=_A ,			)

												((_UpperCamelCase)     ,    )			     =     result_with_labels.to_tuple()

												_UpperCamelCase			     =     model(_A ,			start_positions=_A ,			end_positions=_A		)

												((_UpperCamelCase)     ,    )			     =     result_with_labels.to_tuple()

												self.parent.assertEqual(result_with_labels.loss.shape ,			()		)
												self.parent.assertEqual(result.start_top_log_probs.shape ,			(self.batch_size, model.config.start_n_top)		)
												self.parent.assertEqual(result.start_top_index.shape ,			(self.batch_size, model.config.start_n_top)		)
												self.parent.assertEqual(
												    result.end_top_log_probs.shape ,			(self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
												self.parent.assertEqual(
												    result.end_top_index.shape ,			(self.batch_size, model.config.start_n_top * model.config.end_n_top)		)
												self.parent.assertEqual(result.cls_logits.shape ,			(self.batch_size,)		)







							def  UpperCamelCase_						(   self      :     List[Any] ,			_A      :     Union[str, Any] ,			_A      :     Tuple ,			_A      :     str ,			_A      :     int ,			_A      :     int ,			_A      :     Optional[int] ,			_A      :     Optional[int] ,			_A      :     int ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     FlaubertForSequenceClassification(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     model(_A		)
												_UpperCamelCase			     =     model(_A ,			labels=_A		)

												self.parent.assertEqual(result.loss.shape ,			()		)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.type_sequence_label_size)		)







							def  UpperCamelCase_						(   self      :     Optional[int] ,			_A      :     List[str] ,			_A      :     Optional[Any] ,			_A      :     str ,			_A      :     Union[str, Any] ,			_A      :     List[Any] ,			_A      :     int ,			_A      :     List[Any] ,			_A      :     str ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     self.num_labels
												_UpperCamelCase			     =     FlaubertForTokenClassification(_A		)
												model.to(_A		)
												model.eval()

												_UpperCamelCase			     =     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      :     Tuple ,			_A      :     Dict ,			_A      :     str ,			_A      :     Optional[Any] ,			_A      :     List[str] ,			_A      :     Any ,			_A      :     Optional[int] ,			_A      :     Optional[Any] ,			_A      :     List[Any] ,			_A      :     List[str] ,			):
												_UpperCamelCase			     =     self.num_choices
												_UpperCamelCase			     =     FlaubertForMultipleChoice(config=_A		)
												model.to(_A		)
												model.eval()
												_UpperCamelCase			     =     input_ids.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     token_type_ids.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     input_mask.unsqueeze(1		).expand(-1 ,			self.num_choices ,			-1		).contiguous()
												_UpperCamelCase			     =     model(
												    _A ,			attention_mask=_A ,			token_type_ids=_A ,			labels=_A ,			)
												self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_choices)		)







							def  UpperCamelCase_						(   self      :     Tuple		):
												_UpperCamelCase			     =     self.prepare_config_and_inputs()
												(
												    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    (
												    _UpperCamelCase
												)     ,    
												)			     =     config_and_inputs
												_UpperCamelCase			     =     {
												    '''input_ids''': input_ids,
												    '''token_type_ids''': token_type_ids,
												    '''lengths''': input_lengths,
												    '''attention_mask''': input_mask,
												}
												return config, inputs_dict




@require_torch
class     lowerCAmelCase_     (       __lowercase,  __lowercase,  unittest.TestCase ):
							UpperCAmelCase				   =     (
							    (
							        FlaubertModel,
							        FlaubertWithLMHeadModel,
							        FlaubertForQuestionAnswering,
							        FlaubertForQuestionAnsweringSimple,
							        FlaubertForSequenceClassification,
							        FlaubertForTokenClassification,
							        FlaubertForMultipleChoice,
							    )
							    if is_torch_available()
							    else ()
							)
							UpperCAmelCase				   =     (
							    {
							        "feature-extraction": FlaubertModel,
							        "fill-mask": FlaubertWithLMHeadModel,
							        "question-answering": FlaubertForQuestionAnsweringSimple,
							        "text-classification": FlaubertForSequenceClassification,
							        "token-classification": FlaubertForTokenClassification,
							        "zero-shot": FlaubertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)







							def  UpperCamelCase_						(   self      :     Union[str, Any] ,			_A      :     Dict ,			_A      :     Dict ,			_A      :     Tuple ,			_A      :     int ,			_A      :     Any		):
												if (
												    pipeline_test_casse_name == "QAPipelineTests"
												    and tokenizer_name is not None
												    and not tokenizer_name.endswith('''Fast'''		)
												):
																	# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
																	# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
																	# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
																	return True

												return False







							def  UpperCamelCase_						(   self      :     str ,			_A      :     Any ,			_A      :     List[str] ,			_A      :     Optional[int]=False		):
												_UpperCamelCase			     =     super()._prepare_for_class(_A ,			_A ,			return_labels=_A		)

												if return_labels:
																	if model_class.__name__ == "FlaubertForQuestionAnswering":
																						_UpperCamelCase			     =     torch.zeros(
																						    self.model_tester.batch_size ,			dtype=torch.long ,			device=_A		)
																						_UpperCamelCase			     =     torch.zeros(
																						    self.model_tester.batch_size ,			dtype=torch.long ,			device=_A		)

												return inputs_dict







							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     FlaubertModelTester(self		)
												_UpperCamelCase			     =     ConfigTester(self ,			config_class=_A ,			emb_dim=37		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												self.config_tester.run_common_tests()







							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_model(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_lm_head(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_simple_qa(*_A		)







							def  UpperCamelCase_						(   self      :     Union[str, Any]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_qa(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_sequence_classif(*_A		)







							def  UpperCamelCase_						(   self      :     Any		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_token_classif(*_A		)







							def  UpperCamelCase_						(   self      :     Optional[int]		):
												_UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_flaubert_multiple_choice(*_A		)







							@slow
							def  UpperCamelCase_						(   self      :     str		):
												for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_UpperCamelCase			     =     FlaubertModel.from_pretrained(_A		)
																	self.assertIsNotNone(_A		)







							@slow
							@require_torch_gpu
							def  UpperCamelCase_						(   self      :     List[Any]		):
												_UpperCamelCase     ,    _UpperCamelCase			     =     self.model_tester.prepare_config_and_inputs_for_common()
												for model_class in self.all_model_classes:
																	# FlauBertForMultipleChoice behaves incorrectly in JIT environments.
																	if model_class == FlaubertForMultipleChoice:
																						return

																	_UpperCamelCase			     =     True
																	_UpperCamelCase			     =     model_class(config=_A		)

																	_UpperCamelCase			     =     self._prepare_for_class(_A ,			_A		)
																	_UpperCamelCase			     =     torch.jit.trace(
																	    _A ,			(inputs_dict['''input_ids'''].to('''cpu'''		), inputs_dict['''attention_mask'''].to('''cpu'''		))		)

																	with tempfile.TemporaryDirectory() as tmp:
																						torch.jit.save(_A ,			os.path.join(_A ,			'''traced_model.pt'''		)		)
																						_UpperCamelCase			     =     torch.jit.load(os.path.join(_A ,			'''traced_model.pt'''		) ,			map_location=_A		)
																						loaded(inputs_dict['''input_ids'''].to(_A		) ,			inputs_dict['''attention_mask'''].to(_A		)		)




@require_torch
class     lowerCAmelCase_     (       unittest.TestCase ):







							@slow
							def  UpperCamelCase_						(   self      :     int		):
												_UpperCamelCase			     =     FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''		)
												_UpperCamelCase			     =     torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]		)
												with torch.no_grad():
																	_UpperCamelCase			     =     model(_A		)[0]
												_UpperCamelCase			     =     torch.Size((1, 11, 768)		)
												self.assertEqual(output.shape ,			_A		)
												_UpperCamelCase			     =     torch.tensor(
												    [[[-2.6251, -1.4298, -0.0227], [-2.8510, -1.6387, 0.2258], [-2.8114, -1.1832, -0.3066]]]		)

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