






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    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				def __init__(  self		:					int,						lowerCamelCase		:					int,						lowerCamelCase		:					Any=13,						lowerCamelCase		:					Dict=7,						lowerCamelCase		:					str=True,						lowerCamelCase		:					Any=True,						lowerCamelCase		:					Any=True,						lowerCamelCase		:					Optional[Any]=True,						lowerCamelCase		:					List[str]=True,						lowerCamelCase		:					Tuple=False,						lowerCamelCase		:					Tuple=False,						lowerCamelCase		:					Optional[int]=False,						lowerCamelCase		:					Tuple=2,						lowerCamelCase		:					Union[str, Any]=99,						lowerCamelCase		:					str=0,						lowerCamelCase		:					Optional[int]=32,						lowerCamelCase		:					Union[str, Any]=5,						lowerCamelCase		:					Union[str, Any]=4,						lowerCamelCase		:					Optional[int]=0.1,						lowerCamelCase		:					Any=0.1,						lowerCamelCase		:					Optional[Any]=512,						lowerCamelCase		:					Tuple=12,						lowerCamelCase		:					List[Any]=2,						lowerCamelCase		:					Optional[int]=0.02,						lowerCamelCase		:					Union[str, Any]=3,						lowerCamelCase		:					Any=4,						lowerCamelCase		:					Tuple="last",						lowerCamelCase		:					Optional[int]=None,						lowerCamelCase		:					Optional[Any]=None,						):

								'''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        lowercase__     (  self		:					List[Any]						):

								'''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        lowercase__     (  self		:					Union[str, Any]						):

								'''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        lowercase__     (  self		:					str,						lowerCamelCase		:					str,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Dict,						lowerCamelCase		:					List[str],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Any,						):

								'''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        lowercase__     (  self		:					Optional[Any],						lowerCamelCase		:					List[str],						lowerCamelCase		:					List[str],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					List[str],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					str,						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					Union[str, Any],						):

								'''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        lowercase__     (  self		:					List[str],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Any,						lowerCamelCase		:					int,						lowerCamelCase		:					List[str],						lowerCamelCase		:					Union[str, Any],						):

								'''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        lowercase__     (  self		:					List[str],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					Dict,						lowerCamelCase		:					Dict,						lowerCamelCase		:					int,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Any,						lowerCamelCase		:					str,						):

								'''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        lowercase__     (  self		:					Dict,						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					int,						lowerCamelCase		:					Dict,						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Union[str, Any],						lowerCamelCase		:					str,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					Union[str, Any],						):

								'''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        lowercase__     (  self		:					Union[str, Any],						lowerCamelCase		:					Any,						lowerCamelCase		:					int,						lowerCamelCase		:					Any,						lowerCamelCase		:					Optional[int],						lowerCamelCase		:					Any,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Dict,						lowerCamelCase		:					int,						lowerCamelCase		:					List[Any],						):

								'''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        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					Dict,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Tuple,						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					List[str],						lowerCamelCase		:					int,						lowerCamelCase		:					Optional[Any],						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        lowercase__     (  self		:					Dict						):

								'''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    _UpperCAmelCase      ( A__	,A__	,unittest.TestCase			):

				"""simple docstring"""




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




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Tuple,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Union[str, 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        lowercase__     (  self		:					Any,						lowerCamelCase		:					Any,						lowerCamelCase		:					int,						lowerCamelCase		:					Union[str, Any]=False						):

								'''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        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    FlaubertModelTester(self						)
								lowercase__     				=    ConfigTester(self,						config_class=lowerCamelCase,						emb_dim=37						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								self.config_tester.run_common_tests()




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_model(*lowerCamelCase						)




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_lm_head(*lowerCamelCase						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_simple_qa(*lowerCamelCase						)




				def        lowercase__     (  self		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_qa(*lowerCamelCase						)




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_sequence_classif(*lowerCamelCase						)




				def        lowercase__     (  self		:					Optional[int]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_token_classif(*lowerCamelCase						)




				def        lowercase__     (  self		:					List[str]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_flaubert_multiple_choice(*lowerCamelCase						)




				@slow
				def        lowercase__     (  self		:					Tuple						):

								'''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        lowercase__     (  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    _UpperCAmelCase      ( unittest.TestCase			):

				"""simple docstring"""




				@slow
				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    FlaubertModel.from_pretrained('''flaubert/flaubert_base_cased'''						)
								lowercase__     				=    torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]						)
								with torch.no_grad():
												lowercase__     				=    model(lowerCamelCase						)[0]
								lowercase__     				=    torch.Size((1, 11, 768)						)
								self.assertEqual(output.shape,						lowerCamelCase						)
								lowercase__     				=    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						)						)












from collections import defaultdict
from math import gcd






def 						a    (		lowerCamelCase_ = 150_0000       ):






				'''simple docstring'''
				lowercase__     				=    defaultdict(lowerCamelCase_       )
				lowercase__     				=    2
				while 2 * euclid_m * (euclid_m + 1) <= limit:
								for euclid_n in range((euclid_m % 2) + 1	,     lowerCamelCase_	,     2       ):
												if gcd(lowerCamelCase_	,     lowerCamelCase_       ) > 1:
																continue
												lowercase__     				=    2 * euclid_m * (euclid_m + euclid_n)
												for perimeter in range(lowerCamelCase_	,     limit + 1	,     lowerCamelCase_       ):
																frequencies[perimeter] += 1
								euclid_m += 1
				return sum(1 for frequency in frequencies.values() if frequency == 1       )


if __name__ == "__main__":
							print(F"{solution() = }")




