




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


				"""simple docstring"""





				def __init__(     self					:	Tuple    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Union[str, Any]=1_3    ,   lowercase__					:	Tuple=7    ,   lowercase__					:	Tuple=True    ,   lowercase__					:	Optional[int]=True    ,   lowercase__					:	Optional[int]=True    ,   lowercase__					:	Tuple=True    ,   lowercase__					:	int=True    ,   lowercase__					:	List[str]=False    ,   lowercase__					:	Tuple=False    ,   lowercase__					:	List[str]=False    ,   lowercase__					:	Dict=2    ,   lowercase__					:	Any=9_9    ,   lowercase__					:	int=0    ,   lowercase__					:	Any=3_2    ,   lowercase__					:	Tuple=5    ,   lowercase__					:	str=4    ,   lowercase__					:	Any=0.1    ,   lowercase__					:	Optional[Any]=0.1    ,   lowercase__					:	int=5_1_2    ,   lowercase__					:	List[Any]=1_2    ,   lowercase__					:	Dict=2    ,   lowercase__					:	Union[str, Any]=0.0_2    ,   lowercase__					:	Dict=3    ,   lowercase__					:	Any=4    ,   lowercase__					:	Dict="last"    ,   lowercase__					:	int=None    ,   lowercase__					:	Union[str, Any]=None    ,   ):
						__lowercase				:		Union[str, Any]												=				parent
						__lowercase				:		str												=				batch_size
						__lowercase				:		int												=				seq_length
						__lowercase				:		Dict												=				is_training
						__lowercase				:		Any												=				use_input_lengths
						__lowercase				:		Optional[int]												=				use_token_type_ids
						__lowercase				:		List[str]												=				use_labels
						__lowercase				:		int												=				gelu_activation
						__lowercase				:		Optional[int]												=				sinusoidal_embeddings
						__lowercase				:		Any												=				causal
						__lowercase				:		Any												=				asm
						__lowercase				:		Optional[Any]												=				n_langs
						__lowercase				:		int												=				vocab_size
						__lowercase				:		Union[str, Any]												=				n_special
						__lowercase				:		int												=				hidden_size
						__lowercase				:		Optional[Any]												=				num_hidden_layers
						__lowercase				:		Optional[int]												=				num_attention_heads
						__lowercase				:		Tuple												=				hidden_dropout_prob
						__lowercase				:		Optional[Any]												=				attention_probs_dropout_prob
						__lowercase				:		Any												=				max_position_embeddings
						__lowercase				:		Union[str, Any]												=				type_vocab_size
						__lowercase				:		str												=				type_sequence_label_size
						__lowercase				:		List[Any]												=				initializer_range
						__lowercase				:		List[str]												=				num_labels
						__lowercase				:		str												=				num_choices
						__lowercase				:		List[Any]												=				summary_type
						__lowercase				:		List[Any]												=				use_proj
						__lowercase				:		Optional[int]												=				scope



				def       snake_case    (     self					:	int						):
						__lowercase				:		Tuple												=				ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size						)
						__lowercase				:		Tuple												=				random_attention_mask([self.batch_size, self.seq_length]						)

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

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

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

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



				def       snake_case    (     self					:	str    ,   lowercase__					:	Tuple    ,   lowercase__					:	str    ,   lowercase__					:	List[str]    ,   lowercase__					:	List[str]    ,   lowercase__					:	List[str]    ,   lowercase__					:	Any    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	List[str]    ,   lowercase__					:	List[str]    ,   ):
						__lowercase				:		Optional[int]												=				FlaubertWithLMHeadModel(lowercase__						)
						model.to(lowercase__						)
						model.eval()

						__lowercase				:		Any												=				model(lowercase__    ,   token_type_ids=lowercase__    ,   labels=lowercase__						)
						self.parent.assertEqual(result.loss.shape    ,   ()						)
						self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.seq_length, self.vocab_size)						)



				def       snake_case    (     self					:	Any    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	List[str]    ,   lowercase__					:	int    ,   lowercase__					:	int    ,   lowercase__					:	List[str]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Union[str, Any]    ,   ):
						__lowercase				:		List[str]												=				FlaubertForQuestionAnsweringSimple(lowercase__						)
						model.to(lowercase__						)
						model.eval()

						__lowercase				:		Optional[Any]												=				model(lowercase__						)

						__lowercase				:		str												=				model(lowercase__    ,   start_positions=lowercase__    ,   end_positions=lowercase__						)
						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       snake_case    (     self					:	str    ,   lowercase__					:	Dict    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	List[Any]    ,   lowercase__					:	int    ,   lowercase__					:	str    ,   lowercase__					:	Dict    ,   lowercase__					:	str    ,   lowercase__					:	Dict    ,   lowercase__					:	Dict    ,   ):
						__lowercase				:		Any												=				FlaubertForQuestionAnswering(lowercase__						)
						model.to(lowercase__						)
						model.eval()

						__lowercase				:		Optional[Any]												=				model(lowercase__						)

						__lowercase				:		Tuple												=				model(
						    lowercase__    ,   start_positions=lowercase__    ,   end_positions=lowercase__    ,   cls_index=lowercase__    ,   is_impossible=lowercase__    ,   p_mask=lowercase__    ,   )

						__lowercase				:		str												=				model(
						    lowercase__    ,   start_positions=lowercase__    ,   end_positions=lowercase__    ,   cls_index=lowercase__    ,   is_impossible=lowercase__    ,   )

						((__lowercase)						,)				:		Optional[int]												=				result_with_labels.to_tuple()

						__lowercase				:		Optional[int]												=				model(lowercase__    ,   start_positions=lowercase__    ,   end_positions=lowercase__						)

						((__lowercase)						,)				:		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       snake_case    (     self					:	Dict    ,   lowercase__					:	List[Any]    ,   lowercase__					:	str    ,   lowercase__					:	str    ,   lowercase__					:	str    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Any    ,   lowercase__					:	str    ,   lowercase__					:	List[Any]    ,   lowercase__					:	Tuple    ,   ):
						__lowercase				:		Union[str, Any]												=				FlaubertForSequenceClassification(lowercase__						)
						model.to(lowercase__						)
						model.eval()

						__lowercase				:		Optional[Any]												=				model(lowercase__						)
						__lowercase				:		Optional[int]												=				model(lowercase__    ,   labels=lowercase__						)

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



				def       snake_case    (     self					:	List[Any]    ,   lowercase__					:	int    ,   lowercase__					:	Any    ,   lowercase__					:	List[str]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	str    ,   lowercase__					:	int    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Tuple    ,   ):
						__lowercase				:		List[Any]												=				self.num_labels
						__lowercase				:		Optional[int]												=				FlaubertForTokenClassification(lowercase__						)
						model.to(lowercase__						)
						model.eval()

						__lowercase				:		Any												=				model(lowercase__    ,   attention_mask=lowercase__    ,   labels=lowercase__						)
						self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.seq_length, self.num_labels)						)



				def       snake_case    (     self					:	int    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Any    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Optional[Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	Tuple    ,   lowercase__					:	List[str]    ,   lowercase__					:	Dict    ,   ):
						__lowercase				:		int												=				self.num_choices
						__lowercase				:		Dict												=				FlaubertForMultipleChoice(config=lowercase__						)
						model.to(lowercase__						)
						model.eval()
						__lowercase				:		Tuple												=				input_ids.unsqueeze(1						).expand(-1    ,   self.num_choices    ,   -1						).contiguous()
						__lowercase				:		Optional[Any]												=				token_type_ids.unsqueeze(1						).expand(-1    ,   self.num_choices    ,   -1						).contiguous()
						__lowercase				:		Optional[Any]												=				input_mask.unsqueeze(1						).expand(-1    ,   self.num_choices    ,   -1						).contiguous()
						__lowercase				:		Optional[int]												=				model(
						    lowercase__    ,   attention_mask=lowercase__    ,   token_type_ids=lowercase__    ,   labels=lowercase__    ,   )
						self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_choices)						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		int												=				self.prepare_config_and_inputs()
						(
						    (
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,(
						    __lowercase
						)						,
						)				:		Optional[Any]												=				config_and_inputs
						__lowercase				:		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       lowerCAmelCase__       (			lowerCAmelCase_  ,			lowerCAmelCase_  ,			unittest.TestCase    ):


				"""simple docstring"""





				__UpperCAmelCase :     Optional[Any]       =      (
				    (
				        FlaubertModel,
				        FlaubertWithLMHeadModel,
				        FlaubertForQuestionAnswering,
				        FlaubertForQuestionAnsweringSimple,
				        FlaubertForSequenceClassification,
				        FlaubertForTokenClassification,
				        FlaubertForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				__UpperCAmelCase :     Optional[Any]       =      (
				    {
				        "feature-extraction": FlaubertModel,
				        "fill-mask": FlaubertWithLMHeadModel,
				        "question-answering": FlaubertForQuestionAnsweringSimple,
				        "text-classification": FlaubertForSequenceClassification,
				        "token-classification": FlaubertForTokenClassification,
				        "zero-shot": FlaubertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)



				def       snake_case    (     self					:	Optional[int]    ,   lowercase__					:	Any    ,   lowercase__					:	Union[str, Any]    ,   lowercase__					:	Dict    ,   lowercase__					:	Optional[int]    ,   lowercase__					:	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       snake_case    (     self					:	Dict    ,   lowercase__					:	Dict    ,   lowercase__					:	int    ,   lowercase__					:	str=False						):
						__lowercase				:		Dict												=				super()._prepare_for_class(lowercase__    ,   lowercase__    ,   return_labels=lowercase__						)

						if return_labels:
								if model_class.__name__ == "FlaubertForQuestionAnswering":
										__lowercase				:		Tuple												=				torch.zeros(
										    self.model_tester.batch_size    ,   dtype=torch.long    ,   device=lowercase__						)
										__lowercase				:		Optional[int]												=				torch.zeros(
										    self.model_tester.batch_size    ,   dtype=torch.long    ,   device=lowercase__						)

						return inputs_dict



				def       snake_case    (     self					:	int						):
						__lowercase				:		List[str]												=				FlaubertModelTester(self						)
						__lowercase				:		Optional[Any]												=				ConfigTester(self    ,   config_class=lowercase__    ,   emb_dim=3_7						)



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



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		Optional[int]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_model(*lowercase__						)



				def       snake_case    (     self					:	int						):
						__lowercase				:		Any												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_lm_head(*lowercase__						)



				def       snake_case    (     self					:	Optional[Any]						):
						__lowercase				:		str												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_simple_qa(*lowercase__						)



				def       snake_case    (     self					:	List[Any]						):
						__lowercase				:		Dict												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_qa(*lowercase__						)



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		Optional[int]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_sequence_classif(*lowercase__						)



				def       snake_case    (     self					:	Optional[int]						):
						__lowercase				:		List[str]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_token_classif(*lowercase__						)



				def       snake_case    (     self					:	Tuple						):
						__lowercase				:		List[Any]												=				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_flaubert_multiple_choice(*lowercase__						)



				@slow
				def       snake_case    (     self					:	Optional[Any]						):
						for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								__lowercase				:		Tuple												=				FlaubertModel.from_pretrained(lowercase__						)
								self.assertIsNotNone(lowercase__						)



				@slow
				@require_torch_gpu
				def       snake_case    (     self					:	List[str]						):
						__lowercase						,__lowercase				:		Tuple												=				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				:		Optional[int]												=				True
								__lowercase				:		str												=				model_class(config=lowercase__						)

								__lowercase				:		List[str]												=				self._prepare_for_class(lowercase__    ,   lowercase__						)
								__lowercase				:		Optional[int]												=				torch.jit.trace(
								    lowercase__    ,   (inputs_dict["input_ids"].to("cpu"						), inputs_dict["attention_mask"].to("cpu"						))						)

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





@require_torch
class       lowerCAmelCase__       (			unittest.TestCase    ):


				"""simple docstring"""





				@slow
				def       snake_case    (     self					:	Union[str, Any]						):
						__lowercase				:		Union[str, Any]												=				FlaubertModel.from_pretrained("flaubert/flaubert_base_cased"						)
						__lowercase				:		Tuple												=				torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]						)
						with torch.no_grad():
								__lowercase				:		Union[str, Any]												=				model(lowercase__						)[0]
						__lowercase				:		int												=				torch.Size((1, 1_1, 7_6_8)						)
						self.assertEqual(output.shape    ,   lowercase__						)
						__lowercase				:		Any												=				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]    ,   lowercase__    ,   atol=1e-4						)						)






"""simple docstring"""







class       lowerCAmelCase__       :


				"""simple docstring"""





				def __init__(     self					:	int    ,   lowercase__					:	List[Any]						):
						__lowercase				:		Tuple												=				val
						__lowercase				:		Optional[Any]												=				None
						__lowercase				:		Tuple												=				None



				def       snake_case    (     self					:	Optional[Any]    ,   lowercase__					:	List[str]						):
						if self.val:
								if val < self.val:
										if self.left is None:
												__lowercase				:		Tuple												=				Node(lowercase__						)
										else:
												self.left.insert(lowercase__						)
								elif val > self.val:
										if self.right is None:
												__lowercase				:		Any												=				Node(lowercase__						)
										else:
												self.right.insert(lowercase__						)
						else:
								__lowercase				:		Optional[Any]												=				val





def 						snake_case__       (					_lowerCamelCase,  _lowerCamelCase				)			->Any:






		"""simple docstring"""


		if root:
				inorder(root.left,  _lowerCamelCase				)
				res.append(root.val				)
				inorder(root.right,  _lowerCamelCase				)

def 						snake_case__       (					_lowerCamelCase				)			->Optional[Any]:






		"""simple docstring"""


		if len(_lowerCamelCase				) == 0:
				return arr
		__lowercase				:		int												=				Node(arr[0]				)
		for i in range(1,  len(_lowerCamelCase				)				):
				root.insert(arr[i]				)
		# Traverse BST in order.
		__lowercase				:		Tuple												=				[]
		inorder(_lowerCamelCase,  _lowerCamelCase				)
		return res


if __name__ == "__main__":
						print(tree_sort([1_0, 1, 3, 2, 9, 1_4, 1_3]))
