





'''simple docstring'''



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 							__lowercase    ( lowerCAmelCase__  ):







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase       ,_lowerCamelCase=13       ,_lowerCamelCase=7       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=False       ,_lowerCamelCase=False       ,_lowerCamelCase=False       ,_lowerCamelCase=2       ,_lowerCamelCase=99       ,_lowerCamelCase=0       ,_lowerCamelCase=32       ,_lowerCamelCase=5       ,_lowerCamelCase=4       ,_lowerCamelCase=0.1       ,_lowerCamelCase=0.1       ,_lowerCamelCase=512       ,_lowerCamelCase=12       ,_lowerCamelCase=2       ,_lowerCamelCase=0.0_2       ,_lowerCamelCase=3       ,_lowerCamelCase=4       ,_lowerCamelCase="last"       ,_lowerCamelCase=None       ,_lowerCamelCase=None       ,)							->							Optional[Any]:







				'''simple docstring'''





				__lowercase			  =							parent
				__lowercase			  =							batch_size
				__lowercase			  =							seq_length
				__lowercase			  =							is_training
				__lowercase			  =							use_input_lengths
				__lowercase			  =							use_token_type_ids
				__lowercase			  =							use_labels
				__lowercase			  =							gelu_activation
				__lowercase			  =							sinusoidal_embeddings
				__lowercase			  =							causal
				__lowercase			  =							asm
				__lowercase			  =							n_langs
				__lowercase			  =							vocab_size
				__lowercase			  =							n_special
				__lowercase			  =							hidden_size
				__lowercase			  =							num_hidden_layers
				__lowercase			  =							num_attention_heads
				__lowercase			  =							hidden_dropout_prob
				__lowercase			  =							attention_probs_dropout_prob
				__lowercase			  =							max_position_embeddings
				__lowercase			  =							type_vocab_size
				__lowercase			  =							type_sequence_label_size
				__lowercase			  =							initializer_range
				__lowercase			  =							num_labels
				__lowercase			  =							num_choices
				__lowercase			  =							summary_type
				__lowercase			  =							use_proj
				__lowercase			  =							scope


	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





				__lowercase			  =							ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size	)
				__lowercase			  =							random_attention_mask([self.batch_size, self.seq_length]	)

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

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

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

				__lowercase			  =							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	)							->							Dict:







				'''simple docstring'''





				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       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							List[str]:







				'''simple docstring'''





				__lowercase			  =							FlaubertModel(config=_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				__lowercase			  =							model(_lowerCamelCase       ,lengths=_lowerCamelCase       ,langs=_lowerCamelCase	)
				__lowercase			  =							model(_lowerCamelCase       ,langs=_lowerCamelCase	)
				__lowercase			  =							model(_lowerCamelCase	)
				self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)	)


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							str:







				'''simple docstring'''





				__lowercase			  =							FlaubertWithLMHeadModel(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()

				__lowercase			  =							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   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							List[str]:







				'''simple docstring'''





				__lowercase			  =							FlaubertForQuestionAnsweringSimple(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()

				__lowercase			  =							model(_lowerCamelCase	)

				__lowercase			  =							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   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							Optional[Any]:







				'''simple docstring'''





				__lowercase			  =							FlaubertForQuestionAnswering(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()

				__lowercase			  =							model(_lowerCamelCase	)

				__lowercase			  =							model(
				    _lowerCamelCase       ,start_positions=_lowerCamelCase       ,end_positions=_lowerCamelCase       ,cls_index=_lowerCamelCase       ,is_impossible=_lowerCamelCase       ,p_mask=_lowerCamelCase       ,)

				__lowercase			  =							model(
				    _lowerCamelCase       ,start_positions=_lowerCamelCase       ,end_positions=_lowerCamelCase       ,cls_index=_lowerCamelCase       ,is_impossible=_lowerCamelCase       ,)

				((__lowercase)  ,						)			  =							result_with_labels.to_tuple()

				__lowercase			  =							model(_lowerCamelCase       ,start_positions=_lowerCamelCase       ,end_positions=_lowerCamelCase	)

				((__lowercase)  ,						)			  =							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       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							FlaubertForSequenceClassification(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()

				__lowercase			  =							model(_lowerCamelCase	)
				__lowercase			  =							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   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							Tuple:







				'''simple docstring'''





				__lowercase			  =							self.num_labels
				__lowercase			  =							FlaubertForTokenClassification(_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()

				__lowercase			  =							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       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							self.num_choices
				__lowercase			  =							FlaubertForMultipleChoice(config=_lowerCamelCase	)
				model.to(_lowerCamelCase	)
				model.eval()
				__lowercase			  =							input_ids.unsqueeze(1	).expand(-1       ,self.num_choices       ,-1	).contiguous()
				__lowercase			  =							token_type_ids.unsqueeze(1	).expand(-1       ,self.num_choices       ,-1	).contiguous()
				__lowercase			  =							input_mask.unsqueeze(1	).expand(-1       ,self.num_choices       ,-1	).contiguous()
				__lowercase			  =							model(
				    _lowerCamelCase       ,attention_mask=_lowerCamelCase       ,token_type_ids=_lowerCamelCase       ,labels=_lowerCamelCase       ,)
				self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.num_choices)	)


	def   _UpperCAmelCase       (self	)							->							Optional[Any]:







				'''simple docstring'''





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






@require_torch
class 							__lowercase    ( lowerCAmelCase__	, lowerCAmelCase__	, unittest.TestCase  ):







	'''simple docstring'''

	a      :			Any     =    (
	    (
	        FlaubertModel,
	        FlaubertWithLMHeadModel,
	        FlaubertForQuestionAnswering,
	        FlaubertForQuestionAnsweringSimple,
	        FlaubertForSequenceClassification,
	        FlaubertForTokenClassification,
	        FlaubertForMultipleChoice,
	    )
	    if is_torch_available()
	    else ()
	)
	a      :			Optional[int]     =    (
	    {
	        "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       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase	)							->							Any:







				'''simple docstring'''





				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       ,_lowerCamelCase       ,_lowerCamelCase       ,_lowerCamelCase=False	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							super()._prepare_for_class(_lowerCamelCase       ,_lowerCamelCase       ,return_labels=_lowerCamelCase	)

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

				return inputs_dict


	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							FlaubertModelTester(self	)
				__lowercase			  =							ConfigTester(self       ,config_class=_lowerCamelCase       ,emb_dim=37	)


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





				self.config_tester.run_common_tests()


	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_model(*_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							Optional[Any]:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_lm_head(*_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_simple_qa(*_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_qa(*_lowerCamelCase	)


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







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_sequence_classif(*_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							Tuple:







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_token_classif(*_lowerCamelCase	)


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







				'''simple docstring'''





				__lowercase			  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_flaubert_multiple_choice(*_lowerCamelCase	)


	@slow
	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							__lowercase			  =							FlaubertModel.from_pretrained(_lowerCamelCase	)
							self.assertIsNotNone(_lowerCamelCase	)


	@slow
	@require_torch_gpu
	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				__lowercase  ,						__lowercase			  =							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

							__lowercase			  =							True
							__lowercase			  =							model_class(config=_lowerCamelCase	)

							__lowercase			  =							self._prepare_for_class(_lowerCamelCase       ,_lowerCamelCase	)
							__lowercase			  =							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'''	)	)
										__lowercase			  =							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 							__lowercase    ( unittest.TestCase  ):







	'''simple docstring'''

	@slow
	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				__lowercase			  =							FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''	)
				__lowercase			  =							torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]	)
				with torch.no_grad():
							__lowercase			  =							model(_lowerCamelCase	)[0]
				__lowercase			  =							torch.Size((1, 11, 768)	)
				self.assertEqual(output.shape       ,_lowerCamelCase	)
				__lowercase			  =							torch.tensor(
				    [[[-2.6_2_5_1, -1.4_2_9_8, -0.0_2_2_7], [-2.8_5_1_0, -1.6_3_8_7, 0.2_2_5_8], [-2.8_1_1_4, -1.1_8_3_2, -0.3_0_6_6]]]	)

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












'''simple docstring'''



_SCREAMING_SNAKE_CASE   =      {
    '''A''': ['''B''', '''C''', '''E'''],
    '''B''': ['''A''', '''D''', '''E'''],
    '''C''': ['''A''', '''F''', '''G'''],
    '''D''': ['''B'''],
    '''E''': ['''A''', '''B''', '''D'''],
    '''F''': ['''C'''],
    '''G''': ['''C'''],
}




def 				_lowerCAmelCase  (			lowerCamelCase_     :					dict						,    lowerCamelCase_     :					Optional[Any]						,    lowerCamelCase_     :					List[str]			):
			__lowercase			  =							set()
			# keep track of all the paths to be checked
			__lowercase			  =							[[start]]

			# return path if start is goal
			if start == goal:
						return [start]

			# keeps looping until all possible paths have been checked
			while queue:
						# pop the first path from the queue
						__lowercase			  =							queue.pop(0			)
						# get the last node from the path
						__lowercase			  =							path[-1]
						if node not in explored:
									__lowercase			  =							graph[node]
									# go through all neighbour nodes, construct a new path and
									# push it into the queue
									for neighbour in neighbours:
												__lowercase			  =							list(lowerCamelCase_			)
												new_path.append(lowerCamelCase_			)
												queue.append(lowerCamelCase_			)
												# return path if neighbour is goal
												if neighbour == goal:
															return new_path

            # mark node as explored
									explored.add(lowerCamelCase_			)

    # in case there's no path between the 2 nodes
			return []




def 				_lowerCAmelCase  (			lowerCamelCase_     :					dict						,    lowerCamelCase_     :					str						,    lowerCamelCase_     :					str			):
			if not graph or start not in graph or target not in graph:
						return -1
			if start == target:
						return 0
			__lowercase			  =							[start]
			__lowercase			  =							set(lowerCamelCase_			)
			# Keep tab on distances from `start` node.
			__lowercase			  =							{start: 0, target: -1}
			while queue:
						__lowercase			  =							queue.pop(0			)
						if node == target:
									__lowercase			  =							(
									    dist[node] if dist[target] == -1 else min(dist[target]						,    dist[node]			)
									)
						for adjacent in graph[node]:
									if adjacent not in visited:
												visited.add(lowerCamelCase_			)
												queue.append(lowerCamelCase_			)
												__lowercase			  =							dist[node] + 1
			return dist[target]


if __name__ == "__main__":
	print(bfs_shortest_path(demo_graph, '''G''', '''D'''))  # returns ['G', 'C', 'A', 'B', 'D']
	print(bfs_shortest_path_distance(demo_graph, '''G''', '''D'''))  # returns 4





