

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				(       lowercase_			):




				def __init__(					self      ,   snake_case      ,   snake_case=13      ,   snake_case=7      ,   snake_case=True      ,   snake_case=True      ,   snake_case=True      ,   snake_case=True      ,   snake_case=True      ,   snake_case=False      ,   snake_case=False      ,   snake_case=False      ,   snake_case=2      ,   snake_case=99      ,   snake_case=0      ,   snake_case=32      ,   snake_case=5      ,   snake_case=4      ,   snake_case=0.1      ,   snake_case=0.1      ,   snake_case=512      ,   snake_case=12      ,   snake_case=2      ,   snake_case=0.02      ,   snake_case=3      ,   snake_case=4      ,   snake_case="last"      ,   snake_case=None      ,   snake_case=None      ,   ):
											snake_case_							=    parent
											snake_case_							=    batch_size
											snake_case_							=    seq_length
											snake_case_							=    is_training
											snake_case_							=    use_input_lengths
											snake_case_							=    use_token_type_ids
											snake_case_							=    use_labels
											snake_case_							=    gelu_activation
											snake_case_							=    sinusoidal_embeddings
											snake_case_							=    causal
											snake_case_							=    asm
											snake_case_							=    n_langs
											snake_case_							=    vocab_size
											snake_case_							=    n_special
											snake_case_							=    hidden_size
											snake_case_							=    num_hidden_layers
											snake_case_							=    num_attention_heads
											snake_case_							=    hidden_dropout_prob
											snake_case_							=    attention_probs_dropout_prob
											snake_case_							=    max_position_embeddings
											snake_case_							=    type_vocab_size
											snake_case_							=    type_sequence_label_size
											snake_case_							=    initializer_range
											snake_case_							=    num_labels
											snake_case_							=    num_choices
											snake_case_							=    summary_type
											snake_case_							=    use_proj
											snake_case_							=    scope




				def     a							(					self       ):
											snake_case_							=    ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size       )
											snake_case_							=    random_attention_mask([self.batch_size, self.seq_length]       )

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

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

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

											snake_case_							=    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       ):
											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      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   ):
											snake_case_							=    FlaubertModel(config=snake_case       )
											model.to(snake_case       )
											model.eval()
											snake_case_							=    model(snake_case      ,   lengths=snake_case      ,   langs=snake_case       )
											snake_case_							=    model(snake_case      ,   langs=snake_case       )
											snake_case_							=    model(snake_case       )
											self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)       )




				def     a							(					self      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   ):
											snake_case_							=    FlaubertWithLMHeadModel(snake_case       )
											model.to(snake_case       )
											model.eval()

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




				def     a							(					self      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   ):
											snake_case_							=    FlaubertForQuestionAnsweringSimple(snake_case       )
											model.to(snake_case       )
											model.eval()

											snake_case_							=    model(snake_case       )

											snake_case_							=    model(snake_case      ,   start_positions=snake_case      ,   end_positions=snake_case       )
											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      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   ):
											snake_case_							=    FlaubertForQuestionAnswering(snake_case       )
											model.to(snake_case       )
											model.eval()

											snake_case_							=    model(snake_case       )

											snake_case_							=    model(
											    snake_case      ,   start_positions=snake_case      ,   end_positions=snake_case      ,   cls_index=snake_case      ,   is_impossible=snake_case      ,   p_mask=snake_case      ,   )

											snake_case_							=    model(
											    snake_case      ,   start_positions=snake_case      ,   end_positions=snake_case      ,   cls_index=snake_case      ,   is_impossible=snake_case      ,   )

											((snake_case_)				,    )							=    result_with_labels.to_tuple()

											snake_case_							=    model(snake_case      ,   start_positions=snake_case      ,   end_positions=snake_case       )

											((snake_case_)				,    )							=    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      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   ):
											snake_case_							=    FlaubertForSequenceClassification(snake_case       )
											model.to(snake_case       )
											model.eval()

											snake_case_							=    model(snake_case       )
											snake_case_							=    model(snake_case      ,   labels=snake_case       )

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




				def     a							(					self      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   ):
											snake_case_							=    self.num_labels
											snake_case_							=    FlaubertForTokenClassification(snake_case       )
											model.to(snake_case       )
											model.eval()

											snake_case_							=    model(snake_case      ,   attention_mask=snake_case      ,   labels=snake_case       )
											self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.seq_length, self.num_labels)       )




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




				def     a							(					self       ):
											snake_case_							=    self.prepare_config_and_inputs()
											(
											    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    (
											    snake_case_
											)				,    
											)							=    config_and_inputs
											snake_case_							=    {
											    '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				(       lowercase_	,  lowercase_	,  unittest.TestCase			):
				__SCREAMING_SNAKE_CASE					: List[str]       			=			(
				    (
				        FlaubertModel,
				        FlaubertWithLMHeadModel,
				        FlaubertForQuestionAnswering,
				        FlaubertForQuestionAnsweringSimple,
				        FlaubertForSequenceClassification,
				        FlaubertForTokenClassification,
				        FlaubertForMultipleChoice,
				    )
				    if is_torch_available()
				    else ()
				)
				__SCREAMING_SNAKE_CASE					: 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     a							(					self      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case      ,   snake_case       ):
											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      ,   snake_case      ,   snake_case      ,   snake_case=False       ):
											snake_case_							=    super()._prepare_for_class(snake_case      ,   snake_case      ,   return_labels=snake_case       )

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

											return inputs_dict




				def     a							(					self       ):
											snake_case_							=    FlaubertModelTester(self       )
											snake_case_							=    ConfigTester(self      ,   config_class=snake_case      ,   emb_dim=37       )




				def     a							(					self       ):
											self.config_tester.run_common_tests()




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_model(*snake_case       )




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_lm_head(*snake_case       )




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_simple_qa(*snake_case       )




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_qa(*snake_case       )




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_sequence_classif(*snake_case       )




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_token_classif(*snake_case       )




				def     a							(					self       ):
											snake_case_							=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_flaubert_multiple_choice(*snake_case       )




				@slow
				def     a							(					self       ):
											for model_name in FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		snake_case_							=    FlaubertModel.from_pretrained(snake_case       )
																		self.assertIsNotNone(snake_case       )




				@slow
				@require_torch_gpu
				def     a							(					self       ):
											snake_case_				,    snake_case_							=    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

																		snake_case_							=    True
																		snake_case_							=    model_class(config=snake_case       )

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

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







@require_torch
class  lowercase				(       unittest.TestCase			):




				@slow
				def     a							(					self       ):
											snake_case_							=    FlaubertModel.from_pretrained('flaubert/flaubert_base_cased'       )
											snake_case_							=    torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
											with torch.no_grad():
																		snake_case_							=    model(snake_case       )[0]
											snake_case_							=    torch.Size((1, 11, 768)       )
											self.assertEqual(output.shape      ,   snake_case       )
											snake_case_							=    torch.tensor(
											    [[[-2.62_51, -1.42_98, -0.02_27], [-2.85_10, -1.63_87, 0.22_58], [-2.81_14, -1.18_32, -0.30_66]]]       )

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







class  lowercase				:




				def __init__(					self      ,   snake_case      ,   snake_case      ,   snake_case       ):
											snake_case_							=    name
											snake_case_							=    value
											snake_case_							=    weight




				def __repr__(					self       ):
											return F'''{self.__class__.__name__}({self.name}, {self.value}, {self.weight})'''




				def     a							(					self       ):
											return self.value




				def     a							(					self       ):
											return self.name




				def     a							(					self       ):
											return self.weight




				def     a							(					self       ):
											return self.value / self.weight





def 					__lowerCamelCase	( UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    []
							for i in range(len(UpperCamelCase__      )      ):
														menu.append(Things(name[i]   ,							value[i]   ,							weight[i]      )      )
							return menu





def 					__lowerCamelCase	( UpperCamelCase__   ,							UpperCamelCase__   ,							UpperCamelCase__      ):







							'''simple docstring'''






							snake_case_							=    sorted(UpperCamelCase__   ,							key=UpperCamelCase__   ,							reverse=UpperCamelCase__      )
							snake_case_							=    []
							snake_case_				,    snake_case_							=    0.0, 0.0
							for i in range(len(UpperCamelCase__      )      ):
														if (total_cost + items_copy[i].get_weight()) <= max_cost:
																					result.append(items_copy[i]      )
																					total_cost += items_copy[i].get_weight()
																					total_value += items_copy[i].get_value()
							return (result, total_value)





def 					__lowerCamelCase	( ):







							'''simple docstring'''








if __name__ == "__main__":
					import doctest

					doctest.testmod()




