





'''simple docstring'''
# limitations under the License.

# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works

from .pipelines import DiffusionPipeline, ImagePipelineOutput  # noqa: F401
from .utils import deprecate


deprecate(
    '''pipelines_utils''',
    '''0.22.0''',
    '''Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.''',
    standard_warn=False,
    stacklevel=3,
)








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     _SCREAMING_SNAKE_CASE       (			_a      ):





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





	def 						_A (		self				:					List[str]	):
		UpperCamelCase     :Union[str, Any]          =	ids_tensor([self.batch_size, self.seq_length] ,  self.vocab_size	)
		UpperCamelCase     :Any          =	random_attention_mask([self.batch_size, self.seq_length]	)

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

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

		UpperCamelCase     :Optional[int]          =	None
		UpperCamelCase     :int          =	None
		UpperCamelCase     :List[Any]          =	None
		if self.use_labels:
			UpperCamelCase     :Optional[Any]          =	ids_tensor([self.batch_size] ,  self.type_sequence_label_size	)
			UpperCamelCase     :List[str]          =	ids_tensor([self.batch_size, self.seq_length] ,  self.num_labels	)
			UpperCamelCase     :List[str]          =	ids_tensor([self.batch_size] ,  2	).float()
			UpperCamelCase     :List[str]          =	ids_tensor([self.batch_size] ,  self.num_choices	)

		UpperCamelCase     :Union[str, Any]          =	self.get_config()

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





	def 						_A (		self				:					List[Any]	):
		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 						_A (		self				:					Union[str, Any] ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					int ,  ):
		UpperCamelCase     :Tuple          =	FlaubertModel(config=__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()
		UpperCamelCase     :int          =	model(__lowerCamelCase ,  lengths=__lowerCamelCase ,  langs=__lowerCamelCase	)
		UpperCamelCase     :List[Any]          =	model(__lowerCamelCase ,  langs=__lowerCamelCase	)
		UpperCamelCase     :int          =	model(__lowerCamelCase	)
		self.parent.assertEqual(result.last_hidden_state.shape ,  (self.batch_size, self.seq_length, self.hidden_size)	)





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

		UpperCamelCase     :Dict          =	model(__lowerCamelCase ,  token_type_ids=__lowerCamelCase ,  labels=__lowerCamelCase	)
		self.parent.assertEqual(result.loss.shape ,  ()	)
		self.parent.assertEqual(result.logits.shape ,  (self.batch_size, self.seq_length, self.vocab_size)	)





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

		UpperCamelCase     :Any          =	model(__lowerCamelCase	)

		UpperCamelCase     :int          =	model(__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 						_A (		self				:					str ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					str ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Dict ,  __lowerCamelCase				:					str ,  ):
		UpperCamelCase     :str          =	FlaubertForQuestionAnswering(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Any          =	model(__lowerCamelCase	)

		UpperCamelCase     :Optional[int]          =	model(
		    __lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase ,  cls_index=__lowerCamelCase ,  is_impossible=__lowerCamelCase ,  p_mask=__lowerCamelCase ,  )

		UpperCamelCase     :Union[str, Any]          =	model(
		    __lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase ,  cls_index=__lowerCamelCase ,  is_impossible=__lowerCamelCase ,  )

		((UpperCamelCase)     ,      )     :int          =	result_with_labels.to_tuple()

		UpperCamelCase     :int          =	model(__lowerCamelCase ,  start_positions=__lowerCamelCase ,  end_positions=__lowerCamelCase	)

		((UpperCamelCase)     ,      )     :List[Any]          =	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 						_A (		self				:					List[Any] ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					int ,  __lowerCamelCase				:					Dict ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Any ,  __lowerCamelCase				:					Optional[int] ,  __lowerCamelCase				:					Optional[Any] ,  __lowerCamelCase				:					Tuple ,  ):
		UpperCamelCase     :Optional[int]          =	FlaubertForSequenceClassification(__lowerCamelCase	)
		model.to(__lowerCamelCase	)
		model.eval()

		UpperCamelCase     :Tuple          =	model(__lowerCamelCase	)
		UpperCamelCase     :List[str]          =	model(__lowerCamelCase ,  labels=__lowerCamelCase	)

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





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





	def 						_A (		self				:					str	):
		UpperCamelCase     :List[str]          =	self.prepare_config_and_inputs()
		(
		    (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      (
		    UpperCamelCase
		)     ,      
		)     :List[Any]          =	config_and_inputs
		UpperCamelCase     :Union[str, Any]          =	{
		    """input_ids""": input_ids,
		    """token_type_ids""": token_type_ids,
		    """lengths""": input_lengths,
		    """attention_mask""": input_mask,
		}
		return config, inputs_dict

@require_torch
class     _SCREAMING_SNAKE_CASE       (			_a		,   _a		,   unittest.TestCase      ):
	snake_case__							:     Optional[int]     	=  (
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case__							:     Tuple     	=  (
	    {
	        """feature-extraction""": FlaubertModel,
	        """fill-mask""": FlaubertWithLMHeadModel,
	        """question-answering""": FlaubertForQuestionAnsweringSimple,
	        """text-classification""": FlaubertForSequenceClassification,
	        """token-classification""": FlaubertForTokenClassification,
	        """zero-shot""": FlaubertForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)





	def 						_A (		self				:					int ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					List[str] ,  __lowerCamelCase				:					List[Any] ,  __lowerCamelCase				:					Optional[int]	):
		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 						_A (		self				:					Optional[int] ,  __lowerCamelCase				:					Dict ,  __lowerCamelCase				:					Tuple ,  __lowerCamelCase				:					Tuple=False	):
		UpperCamelCase     :Tuple          =	super()._prepare_for_class(__lowerCamelCase ,  __lowerCamelCase ,  return_labels=__lowerCamelCase	)

		if return_labels:
			if model_class.__name__ == "FlaubertForQuestionAnswering":
				UpperCamelCase     :Tuple          =	torch.zeros(
				    self.model_tester.batch_size ,  dtype=torch.long ,  device=__lowerCamelCase	)
				UpperCamelCase     :List[Any]          =	torch.zeros(
				    self.model_tester.batch_size ,  dtype=torch.long ,  device=__lowerCamelCase	)

		return inputs_dict





	def 						_A (		self				:					str	):
		UpperCamelCase     :List[Any]          =	FlaubertModelTester(self	)
		UpperCamelCase     :Any          =	ConfigTester(self ,  config_class=__lowerCamelCase ,  emb_dim=37	)





	def 						_A (		self				:					Optional[int]	):
		self.config_tester.run_common_tests()





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :List[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_model(*__lowerCamelCase	)





	def 						_A (		self				:					Optional[int]	):
		UpperCamelCase     :Optional[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_lm_head(*__lowerCamelCase	)





	def 						_A (		self				:					List[Any]	):
		UpperCamelCase     :Optional[int]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_simple_qa(*__lowerCamelCase	)





	def 						_A (		self				:					Union[str, Any]	):
		UpperCamelCase     :int          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_qa(*__lowerCamelCase	)





	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :List[Any]          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_sequence_classif(*__lowerCamelCase	)





	def 						_A (		self				:					Tuple	):
		UpperCamelCase     :Tuple          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_token_classif(*__lowerCamelCase	)





	def 						_A (		self				:					int	):
		UpperCamelCase     :Tuple          =	self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_flaubert_multiple_choice(*__lowerCamelCase	)





	@slow
	def 						_A (		self				:					Any	):
		for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			UpperCamelCase     :Optional[int]          =	FlaubertModel.from_pretrained(__lowerCamelCase	)
			self.assertIsNotNone(__lowerCamelCase	)





	@slow
	@require_torch_gpu
	def 						_A (		self				:					Tuple	):
		UpperCamelCase     ,      UpperCamelCase     :Union[str, Any]          =	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     :Optional[Any]          =	True
			UpperCamelCase     :Optional[Any]          =	model_class(config=__lowerCamelCase	)

			UpperCamelCase     :str          =	self._prepare_for_class(__lowerCamelCase ,  __lowerCamelCase	)
			UpperCamelCase     :str          =	torch.jit.trace(
			    __lowerCamelCase ,  (inputs_dict["""input_ids"""].to("""cpu"""	), inputs_dict["""attention_mask"""].to("""cpu"""	))	)

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

@require_torch
class     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	@slow
	def 						_A (		self				:					Optional[Any]	):
		UpperCamelCase     :Union[str, Any]          =	FlaubertModel.from_pretrained("""flaubert/flaubert_base_cased"""	)
		UpperCamelCase     :Optional[Any]          =	torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
		with torch.no_grad():
			UpperCamelCase     :Tuple          =	model(__lowerCamelCase	)[0]
		UpperCamelCase     :Union[str, Any]          =	torch.Size((1, 11, 768)	)
		self.assertEqual(output.shape ,  __lowerCamelCase	)
		UpperCamelCase     :int          =	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] ,  __lowerCamelCase ,  atol=1E-4	)	)
