





'''simple docstring'''
import argparse
import json
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import List

import timm
import torch
import torch.nn as nn
from huggingface_hub import hf_hub_download
from torch import Tensor

from transformers import AutoImageProcessor, ResNetConfig, ResNetForImageClassification
from transformers.utils import logging


logging.set_verbosity_info()
a__							:							Union[str, Any] =logging.get_logger()

@dataclass
class      snake_case     :




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					nn.Module
 SCREAMING_SNAKE_CASE_	:					List[nn.Module]					=field(default_factory=_a       )
 SCREAMING_SNAKE_CASE_	:					list					=field(default_factory=_a       )






 def        _lowerCamelCase					(       self       :							Tuple							,	__A       :							str							,	__A       :							Tensor							,	__A       :							Tensor	):
        __UpperCamelCase       =  len(list(m.modules()	)	) == 1 or isinstance(__lowerCamelCase							,	nn.Convad	) or isinstance(__lowerCamelCase							,	nn.BatchNormad	)
        if has_not_submodules:
               self.traced.append(__lowerCamelCase	)






 def __call__(       self       :							Optional[Any]							,	__A       :							Tensor	):
        for m in self.module.modules():
               self.handles.append(m.register_forward_hook(self._forward_hook	)	)
        self.module(__lowerCamelCase	)
        [x.remove() for x in self.handles]
        return self






 @property
 def        _lowerCamelCase					(       self       :							Optional[Any]	):
        # check the len of the state_dict keys to see if we have learnable params
        return list(filter(lambda __A	: len(list(x.state_dict().keys()	)	) > 0							,	self.traced	)	)






@dataclass
class      snake_case     :




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					nn.Module
 SCREAMING_SNAKE_CASE_	:					nn.Module
 SCREAMING_SNAKE_CASE_	:					int					=0
 SCREAMING_SNAKE_CASE_	:					List					=field(default_factory=_a       )
 SCREAMING_SNAKE_CASE_	:					List					=field(default_factory=_a       )






 def __call__(       self       :							Optional[int]							,	__A       :							Tensor	):
        __UpperCamelCase       =  Tracker(self.dest	)(__lowerCamelCase	).parametrized
        __UpperCamelCase       =  Tracker(self.src	)(__lowerCamelCase	).parametrized

        __UpperCamelCase       =  list(filter(lambda __A	: type(__lowerCamelCase	) not in self.src_skip							,	__lowerCamelCase	)	)
        __UpperCamelCase       =  list(filter(lambda __A	: type(__lowerCamelCase	) not in self.dest_skip							,	__lowerCamelCase	)	)

        if len(__lowerCamelCase	) != len(__lowerCamelCase	):
               raise Exception(
                   f'''Numbers of operations are different. Source module has {len(__lowerCamelCase	)} operations while'''
                   f''' destination module has {len(__lowerCamelCase	)}.'''	)

        for dest_m, src_m in zip(__lowerCamelCase							,	__lowerCamelCase	):
               dest_m.load_state_dict(src_m.state_dict()	)
               if self.verbose == 1:
                      print(f'''Transfered from={src_m} to={dest_m}'''	)






def 				lowercase__   (   __lowercase						:  str				,  __lowercase						:  ResNetConfig				,  __lowercase						:  Path				,  __lowercase						:  bool = True     ) ->      Optional[int]:



       """simple docstring"""






       print(F'''Converting {name}...'''     )
       with torch.no_grad():
              __UpperCamelCase       =  timm.create_model(__lowercase				,  pretrained=__lowercase     ).eval()
              __UpperCamelCase       =  ResNetForImageClassification(__lowercase     ).eval()
              __UpperCamelCase       =  ModuleTransfer(src=__lowercase				,  dest=__lowercase     )
              __UpperCamelCase       =  torch.randn((1, 3, 224, 224)     )
              module_transfer(__lowercase     )

       assert torch.allclose(from_model(__lowercase     )				,  our_model(__lowercase     ).logits     ), "The model logits don't match the original one."

       __UpperCamelCase       =  F'''resnet{'-'.join(name.split('resnet'     )     )}'''
       print(__lowercase     )

       if push_to_hub:
              our_model.push_to_hub(
                  repo_path_or_name=save_directory / checkpoint_name				,  commit_message='Add model'				,  use_temp_dir=__lowercase				,  )

              # we can use the convnext one
              __UpperCamelCase       =  AutoImageProcessor.from_pretrained('facebook/convnext-base-224-22k-1k'     )
              image_processor.push_to_hub(
                  repo_path_or_name=save_directory / checkpoint_name				,  commit_message='Add image processor'				,  use_temp_dir=__lowercase				,  )

              print(F'''Pushed {checkpoint_name}'''     )
def 				lowercase__   (   __lowercase						:  Path				,  __lowercase						:  str = None				,  __lowercase						:  bool = True     ) ->      Dict:



       """simple docstring"""






       __UpperCamelCase       =  """imagenet-1k-id2label.json"""
       __UpperCamelCase       =  1000
       __UpperCamelCase       =  (1, num_labels)

       __UpperCamelCase       =  """huggingface/label-files"""
       __UpperCamelCase       =  num_labels
       __UpperCamelCase       =  json.load(open(hf_hub_download(__lowercase				,  __lowercase				,  repo_type='dataset'     )				,  'r'     )     )
       __UpperCamelCase       =  {int(__lowercase     ): v for k, v in idalabel.items()}

       __UpperCamelCase       =  idalabel
       __UpperCamelCase       =  {v: k for k, v in idalabel.items()}

       __UpperCamelCase       =  partial(__lowercase				,  num_labels=__lowercase				,  idalabel=__lowercase				,  labelaid=__lowercase     )

       __UpperCamelCase       =  {
           """resnet18""": ImageNetPreTrainedConfig(
               depths=[2, 2, 2, 2]				,  hidden_sizes=[64, 128, 256, 512]				,  layer_type='basic'     ),
           """resnet26""": ImageNetPreTrainedConfig(
               depths=[2, 2, 2, 2]				,  hidden_sizes=[256, 512, 1024, 2048]				,  layer_type='bottleneck'     ),
           """resnet34""": ImageNetPreTrainedConfig(
               depths=[3, 4, 6, 3]				,  hidden_sizes=[64, 128, 256, 512]				,  layer_type='basic'     ),
           """resnet50""": ImageNetPreTrainedConfig(
               depths=[3, 4, 6, 3]				,  hidden_sizes=[256, 512, 1024, 2048]				,  layer_type='bottleneck'     ),
           """resnet101""": ImageNetPreTrainedConfig(
               depths=[3, 4, 23, 3]				,  hidden_sizes=[256, 512, 1024, 2048]				,  layer_type='bottleneck'     ),
           """resnet152""": ImageNetPreTrainedConfig(
               depths=[3, 8, 36, 3]				,  hidden_sizes=[256, 512, 1024, 2048]				,  layer_type='bottleneck'     ),
       }

       if model_name:
              convert_weight_and_push(__lowercase				,  names_to_config[model_name]				,  __lowercase				,  __lowercase     )
       else:
              for model_name, config in names_to_config.items():
                     convert_weight_and_push(__lowercase				,  __lowercase				,  __lowercase				,  __lowercase     )
       return config, expected_shape


if __name__ == "__main__":
    a__							:							Optional[Any] =argparse.ArgumentParser()
    # Required parameters
    parser.add_argument(
        '''--model_name''',
        default=None,
        type=str,
        help=(
            '''The name of the model you wish to convert, it must be one of the supported resnet* architecture,'''
            ''' currently: resnet18,26,34,50,101,152. If `None`, all of them will the converted.'''
        ),
    )
    parser.add_argument(
        '''--pytorch_dump_folder_path''',
        default=None,
        type=Path,
        required=True,
        help='''Path to the output PyTorch model directory.''',
    )
    parser.add_argument(
        '''--push_to_hub''',
        default=True,
        type=bool,
        required=False,
        help='''If True, push model and image processor to the hub.''',
    )

    a__							:							str =parser.parse_args()
    a__							:							Path =args.pytorch_dump_folder_path
    pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True)
    convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)








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	)	)
