




import argparse
import json
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import Callable, Dict, List, Tuple

import timm
import torch
import torch.nn as nn
from classy_vision.models.regnet import RegNet, RegNetParams, RegNetYaagf, RegNetYaagf, RegNetYaaagf
from huggingface_hub import cached_download, hf_hub_url
from torch import Tensor
from vissl.models.model_helpers import get_trunk_forward_outputs

from transformers import AutoImageProcessor, RegNetConfig, RegNetForImageClassification, RegNetModel
from transformers.utils import logging


logging.set_verbosity_info()
__UpperCamelCase				:  int      					=							logging.get_logger()





@dataclass
class  lowerCAmelCase__:






    '''simple docstring'''





    A_  :     nn.Module
    A_  :     List[nn.Module]			= field(default_factory=snake_case__						)
    A_  :     list			= field(default_factory=snake_case__						)





    def 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   Any       ,   __snake_case				:   Tensor       ,   __snake_case				:   Tensor	):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   len(list(m.modules()	)	) == 1 or isinstance(__snake_case       ,   nn.Convad	) or isinstance(__snake_case       ,   nn.BatchNormad	)
       if has_not_submodules:
          self.traced.append(__snake_case	)





    def __call__(	self				:   Union[str, Any]       ,   __snake_case				:   Tensor	):





       '''simple docstring'''
       for m in self.module.modules():
          self.handles.append(m.register_forward_hook(self._forward_hook	)	)
       self.module(__snake_case	)
       [x.remove() for x in self.handles]
       return self





    @property
    def 					_lowerCamelCase (	self				:   Optional[Any]	):





       '''simple docstring'''
       return list(filter(lambda __snake_case	: len(list(x.state_dict().keys()	)	) > 0       ,   self.traced	)	)





@dataclass
class  lowerCAmelCase__:






    '''simple docstring'''





    A_  :     nn.Module
    A_  :     nn.Module
    A_  :     int			= 1
    A_  :     List			= field(default_factory=snake_case__						)
    A_  :     List			= field(default_factory=snake_case__						)
    A_  :     bool			= True





    def __call__(	self				:   Tuple       ,   __snake_case				:   Tensor	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[int]						=   Tracker(self.dest	)(__snake_case	).parametrized
       UpperCAmelCase_							:      Union[str, Any]						=   Tracker(self.src	)(__snake_case	).parametrized

       UpperCAmelCase_							:      List[Any]						=   list(filter(lambda __snake_case	: type(__snake_case	) not in self.src_skip       ,   __snake_case	)	)
       UpperCAmelCase_							:      int						=   list(filter(lambda __snake_case	: type(__snake_case	) not in self.dest_skip       ,   __snake_case	)	)

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

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





class  lowerCAmelCase__(    nn.Module						):






    '''simple docstring'''





    def __init__(	self				:   Union[str, Any]       ,   __snake_case				:   nn.Module	):





       '''simple docstring'''
       super().__init__()

       UpperCAmelCase_							:      List[Tuple[str, nn.Module]]						=   []
       # - get the stem
       feature_blocks.append(('''conv1''', model.stem)	)
       # - get all the feature blocks
       for k, v in model.trunk_output.named_children():
          assert k.startswith('''block'''	), f'''Unexpected layer name {k}'''
          UpperCAmelCase_							:      Union[str, Any]						=   len(__snake_case	) + 1
          feature_blocks.append((f'''res{block_index}''', v)	)

       UpperCAmelCase_							:      List[Any]						=   nn.ModuleDict(__snake_case	)





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Tensor	):





       '''simple docstring'''
       return get_trunk_forward_outputs(
           __snake_case       ,   out_feat_keys=__snake_case       ,   feature_blocks=self._feature_blocks       ,   )





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    def 					_lowerCamelCase (	self				:   Tuple       ,   __snake_case				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   x.split('''-'''	)
       return x_split[0] + x_split[1] + "_" + "".join(x_split[2:]	)





    def __getitem__(	self				:   Optional[Any]       ,   __snake_case				:   str	):





       '''simple docstring'''
       # default to timm!
       if x not in self:
          UpperCAmelCase_							:      Optional[Any]						=   self.convert_name_to_timm(__snake_case	)
          UpperCAmelCase_							:      str						=   partial(lambda: (timm.create_model(__snake_case       ,   pretrained=__snake_case	).eval(), None)	)

       else:
          UpperCAmelCase_							:      List[Any]						=   super().__getitem__(__snake_case	)

       return val





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    def __getitem__(	self				:   int       ,   __snake_case				:   str	):





       '''simple docstring'''
       if "seer" in x and "in1k" not in x:
          UpperCAmelCase_							:      List[str]						=   RegNetModel
       else:
          UpperCAmelCase_							:      int						=   RegNetForImageClassification
       return val





def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase       ):
   for from_key, to_key in keys:
      UpperCAmelCase_							:      Dict						=   from_state_dict[from_key].clone()
      print(F'''Copied key={from_key} to={to_key}'''       )
   return to_state_dict
def      snake_case_						( __lowercase			,       __lowercase			,       __lowercase			,       __lowercase			,       __lowercase			,       __lowercase = True			,       ):
   print(F'''Converting {name}...'''       )
   with torch.no_grad():
      UpperCAmelCase_							:      int						=   from_model_func()
      UpperCAmelCase_							:      Tuple						=   our_model_func(__lowercase       ).eval()
      UpperCAmelCase_							:      Any						=   ModuleTransfer(src=__lowercase			,       dest=__lowercase			,       raise_if_mismatch=__lowercase       )
      UpperCAmelCase_							:      Union[str, Any]						=   torch.randn((1, 3, 2_2_4, 2_2_4)       )
      module_transfer(__lowercase       )

   if from_state_dict is not None:
      UpperCAmelCase_							:      List[Any]						=   []
      # for seer - in1k finetuned we have to manually copy the head
      if "seer" in name and "in1k" in name:
         UpperCAmelCase_							:      Union[str, Any]						=   [('''0.clf.0.weight''', '''classifier.1.weight'''), ('''0.clf.0.bias''', '''classifier.1.bias''')]
      UpperCAmelCase_							:      Dict						=   manually_copy_vissl_head(__lowercase			,       our_model.state_dict()			,       __lowercase       )
      our_model.load_state_dict(__lowercase       )

   UpperCAmelCase_							:      Any						=   our_model(__lowercase			,       output_hidden_states=__lowercase       )
   UpperCAmelCase_							:      List[Any]						=   (
       our_outputs.logits if isinstance(__lowercase			,       __lowercase       ) else our_outputs.last_hidden_state
   )

   UpperCAmelCase_							:      Tuple						=   from_model(__lowercase       )
   UpperCAmelCase_							:      Optional[Any]						=   from_output[-1] if type(__lowercase       ) is list else from_output

   # now since I don't want to use any config files, vissl seer model doesn't actually have an head, so let's just check the last hidden state
   if "seer" in name and "in1k" in name:
      UpperCAmelCase_							:      Optional[int]						=   our_outputs.hidden_states[-1]

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

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

      UpperCAmelCase_							:      Tuple						=   2_2_4 if '''seer''' not in name else 3_8_4
      # we can use the convnext one
      UpperCAmelCase_							:      Tuple						=   AutoImageProcessor.from_pretrained('''facebook/convnext-base-224-22k-1k'''			,       size=__lowercase       )
      image_processor.push_to_hub(
          repo_path_or_name=save_directory / name			,       commit_message='''Add image processor'''			,       use_temp_dir=__lowercase			,       )

      print(F'''Pushed {name}'''       )
def      snake_case_						( __lowercase			,       __lowercase = None			,       __lowercase = True       ):
   UpperCAmelCase_							:      Optional[Any]						=   '''imagenet-1k-id2label.json'''
   UpperCAmelCase_							:      int						=   1_0_0_0
   UpperCAmelCase_							:      Union[str, Any]						=   (1, num_labels)

   UpperCAmelCase_							:      str						=   '''huggingface/label-files'''
   UpperCAmelCase_							:      List[Any]						=   num_labels
   UpperCAmelCase_							:      Optional[int]						=   json.load(open(cached_download(hf_hub_url(__lowercase			,       __lowercase			,       repo_type='''dataset'''       )       )			,       '''r'''       )       )
   UpperCAmelCase_							:      Any						=   {int(__lowercase       ): v for k, v in idalabel.items()}

   UpperCAmelCase_							:      Optional[Any]						=   idalabel
   UpperCAmelCase_							:      Any						=   {v: k for k, v in idalabel.items()}

   UpperCAmelCase_							:      Dict						=   partial(__lowercase			,       num_labels=__lowercase			,       idalabel=__lowercase			,       labelaid=__lowercase       )

   UpperCAmelCase_							:      Any						=   {
       '''regnet-x-002''': ImageNetPreTrainedConfig(
           depths=[1, 1, 4, 7]			,       hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8]			,       groups_width=8			,       layer_type='''x'''       ),
       '''regnet-x-004''': ImageNetPreTrainedConfig(
           depths=[1, 2, 7, 1_2]			,       hidden_sizes=[3_2, 6_4, 1_6_0, 3_8_4]			,       groups_width=1_6			,       layer_type='''x'''       ),
       '''regnet-x-006''': ImageNetPreTrainedConfig(
           depths=[1, 3, 5, 7]			,       hidden_sizes=[4_8, 9_6, 2_4_0, 5_2_8]			,       groups_width=2_4			,       layer_type='''x'''       ),
       '''regnet-x-008''': ImageNetPreTrainedConfig(
           depths=[1, 3, 7, 5]			,       hidden_sizes=[6_4, 1_2_8, 2_8_8, 6_7_2]			,       groups_width=1_6			,       layer_type='''x'''       ),
       '''regnet-x-016''': ImageNetPreTrainedConfig(
           depths=[2, 4, 1_0, 2]			,       hidden_sizes=[7_2, 1_6_8, 4_0_8, 9_1_2]			,       groups_width=2_4			,       layer_type='''x'''       ),
       '''regnet-x-032''': ImageNetPreTrainedConfig(
           depths=[2, 6, 1_5, 2]			,       hidden_sizes=[9_6, 1_9_2, 4_3_2, 1_0_0_8]			,       groups_width=4_8			,       layer_type='''x'''       ),
       '''regnet-x-040''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_4, 2]			,       hidden_sizes=[8_0, 2_4_0, 5_6_0, 1_3_6_0]			,       groups_width=4_0			,       layer_type='''x'''       ),
       '''regnet-x-064''': ImageNetPreTrainedConfig(
           depths=[2, 4, 1_0, 1]			,       hidden_sizes=[1_6_8, 3_9_2, 7_8_4, 1_6_2_4]			,       groups_width=5_6			,       layer_type='''x'''       ),
       '''regnet-x-080''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_5, 1]			,       hidden_sizes=[8_0, 2_4_0, 7_2_0, 1_9_2_0]			,       groups_width=1_2_0			,       layer_type='''x'''       ),
       '''regnet-x-120''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_1, 1]			,       hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0]			,       groups_width=1_1_2			,       layer_type='''x'''       ),
       '''regnet-x-160''': ImageNetPreTrainedConfig(
           depths=[2, 6, 1_3, 1]			,       hidden_sizes=[2_5_6, 5_1_2, 8_9_6, 2_0_4_8]			,       groups_width=1_2_8			,       layer_type='''x'''       ),
       '''regnet-x-320''': ImageNetPreTrainedConfig(
           depths=[2, 7, 1_3, 1]			,       hidden_sizes=[3_3_6, 6_7_2, 1_3_4_4, 2_5_2_0]			,       groups_width=1_6_8			,       layer_type='''x'''       ),
       # y variant
       '''regnet-y-002''': ImageNetPreTrainedConfig(depths=[1, 1, 4, 7]			,       hidden_sizes=[2_4, 5_6, 1_5_2, 3_6_8]			,       groups_width=8       ),
       '''regnet-y-004''': ImageNetPreTrainedConfig(
           depths=[1, 3, 6, 6]			,       hidden_sizes=[4_8, 1_0_4, 2_0_8, 4_4_0]			,       groups_width=8       ),
       '''regnet-y-006''': ImageNetPreTrainedConfig(
           depths=[1, 3, 7, 4]			,       hidden_sizes=[4_8, 1_1_2, 2_5_6, 6_0_8]			,       groups_width=1_6       ),
       '''regnet-y-008''': ImageNetPreTrainedConfig(
           depths=[1, 3, 8, 2]			,       hidden_sizes=[6_4, 1_2_8, 3_2_0, 7_6_8]			,       groups_width=1_6       ),
       '''regnet-y-016''': ImageNetPreTrainedConfig(
           depths=[2, 6, 1_7, 2]			,       hidden_sizes=[4_8, 1_2_0, 3_3_6, 8_8_8]			,       groups_width=2_4       ),
       '''regnet-y-032''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_3, 1]			,       hidden_sizes=[7_2, 2_1_6, 5_7_6, 1_5_1_2]			,       groups_width=2_4       ),
       '''regnet-y-040''': ImageNetPreTrainedConfig(
           depths=[2, 6, 1_2, 2]			,       hidden_sizes=[1_2_8, 1_9_2, 5_1_2, 1_0_8_8]			,       groups_width=6_4       ),
       '''regnet-y-064''': ImageNetPreTrainedConfig(
           depths=[2, 7, 1_4, 2]			,       hidden_sizes=[1_4_4, 2_8_8, 5_7_6, 1_2_9_6]			,       groups_width=7_2       ),
       '''regnet-y-080''': ImageNetPreTrainedConfig(
           depths=[2, 4, 1_0, 1]			,       hidden_sizes=[1_6_8, 4_4_8, 8_9_6, 2_0_1_6]			,       groups_width=5_6       ),
       '''regnet-y-120''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_1, 1]			,       hidden_sizes=[2_2_4, 4_4_8, 8_9_6, 2_2_4_0]			,       groups_width=1_1_2       ),
       '''regnet-y-160''': ImageNetPreTrainedConfig(
           depths=[2, 4, 1_1, 1]			,       hidden_sizes=[2_2_4, 4_4_8, 1_2_3_2, 3_0_2_4]			,       groups_width=1_1_2       ),
       '''regnet-y-320''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_2, 1]			,       hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2]			,       groups_width=2_3_2       ),
       # models created by SEER -> https://arxiv.org/abs/2202.08360
       '''regnet-y-320-seer''': RegNetConfig(depths=[2, 5, 1_2, 1]			,       hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2]			,       groups_width=2_3_2       ),
       '''regnet-y-640-seer''': RegNetConfig(depths=[2, 5, 1_2, 1]			,       hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0]			,       groups_width=3_2_8       ),
       '''regnet-y-1280-seer''': RegNetConfig(
           depths=[2, 7, 1_7, 1]			,       hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2]			,       groups_width=2_6_4       ),
       '''regnet-y-2560-seer''': RegNetConfig(
           depths=[3, 7, 1_6, 1]			,       hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8]			,       groups_width=6_4_0       ),
       '''regnet-y-10b-seer''': ImageNetPreTrainedConfig(
           depths=[2, 7, 1_7, 1]			,       hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0]			,       groups_width=1_0_1_0       ),
       # finetuned on imagenet
       '''regnet-y-320-seer-in1k''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_2, 1]			,       hidden_sizes=[2_3_2, 6_9_6, 1_3_9_2, 3_7_1_2]			,       groups_width=2_3_2       ),
       '''regnet-y-640-seer-in1k''': ImageNetPreTrainedConfig(
           depths=[2, 5, 1_2, 1]			,       hidden_sizes=[3_2_8, 9_8_4, 1_9_6_8, 4_9_2_0]			,       groups_width=3_2_8       ),
       '''regnet-y-1280-seer-in1k''': ImageNetPreTrainedConfig(
           depths=[2, 7, 1_7, 1]			,       hidden_sizes=[5_2_8, 1_0_5_6, 2_9_0_4, 7_3_9_2]			,       groups_width=2_6_4       ),
       '''regnet-y-2560-seer-in1k''': ImageNetPreTrainedConfig(
           depths=[3, 7, 1_6, 1]			,       hidden_sizes=[6_4_0, 1_6_9_6, 2_5_4_4, 5_0_8_8]			,       groups_width=6_4_0       ),
       '''regnet-y-10b-seer-in1k''': ImageNetPreTrainedConfig(
           depths=[2, 7, 1_7, 1]			,       hidden_sizes=[2_0_2_0, 4_0_4_0, 1_1_1_1_0, 2_8_2_8_0]			,       groups_width=1_0_1_0       ),
   }

   UpperCAmelCase_							:      List[Any]						=   NameToOurModelFuncMap()
   UpperCAmelCase_							:      Optional[int]						=   NameToFromModelFuncMap()
   # add seer weights logic

   def load_using_classy_vision(__lowercase			,       __lowercase       ) -> Tuple[nn.Module, Dict]:
      UpperCAmelCase_							:      str						=   torch.hub.load_state_dict_from_url(__lowercase			,       model_dir=str(__lowercase       )			,       map_location='''cpu'''       )
      UpperCAmelCase_							:      Tuple						=   model_func()
      # check if we have a head, if yes add it
      UpperCAmelCase_							:      Tuple						=   files['''classy_state_dict''']['''base_model''']['''model''']
      UpperCAmelCase_							:      Optional[int]						=   model_state_dict['''trunk''']
      model.load_state_dict(__lowercase       )
      return model.eval(), model_state_dict["heads"]

   # pretrained
   UpperCAmelCase_							:      List[Any]						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet32d/seer_regnet32gf_model_iteration244000.torch'''			,       lambda: FakeRegNetVisslWrapper(RegNetYaagf()       )			,       )

   UpperCAmelCase_							:      Optional[int]						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet64/seer_regnet64gf_model_final_checkpoint_phase0.torch'''			,       lambda: FakeRegNetVisslWrapper(RegNetYaagf()       )			,       )

   UpperCAmelCase_							:      Optional[int]						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/swav_ig1b_regnet128Gf_cnstant_bs32_node16_sinkhorn10_proto16k_syncBN64_warmup8k/model_final_checkpoint_phase0.torch'''			,       lambda: FakeRegNetVisslWrapper(RegNetYaaagf()       )			,       )

   UpperCAmelCase_							:      Union[str, Any]						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_regnet10B/model_iteration124500_conso.torch'''			,       lambda: FakeRegNetVisslWrapper(
           RegNet(RegNetParams(depth=2_7			,       group_width=1_0_1_0			,       w_a=1_7_4_4			,       w_a=6_2_0.8_3			,       w_m=2.5_2       )       )       )			,       )

   # IN1K finetuned
   UpperCAmelCase_							:      str						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet32_finetuned_in1k_model_final_checkpoint_phase78.torch'''			,       lambda: FakeRegNetVisslWrapper(RegNetYaagf()       )			,       )

   UpperCAmelCase_							:      List[Any]						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet64_finetuned_in1k_model_final_checkpoint_phase78.torch'''			,       lambda: FakeRegNetVisslWrapper(RegNetYaagf()       )			,       )

   UpperCAmelCase_							:      int						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_regnet128_finetuned_in1k_model_final_checkpoint_phase78.torch'''			,       lambda: FakeRegNetVisslWrapper(RegNetYaaagf()       )			,       )

   UpperCAmelCase_							:      List[Any]						=   partial(
       __lowercase			,       '''https://dl.fbaipublicfiles.com/vissl/model_zoo/seer_finetuned/seer_10b_finetuned_in1k_model_phase28_conso.torch'''			,       lambda: FakeRegNetVisslWrapper(
           RegNet(RegNetParams(depth=2_7			,       group_width=1_0_1_0			,       w_a=1_7_4_4			,       w_a=6_2_0.8_3			,       w_m=2.5_2       )       )       )			,       )

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


if __name__ == "__main__":
       __UpperCamelCase				:  List[str]      					=							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 regnet* architecture,'
               ' currently: regnetx-*, regnety-*. 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.',
       )

       __UpperCamelCase				:  Optional[Any]      					=							parser.parse_args()

       __UpperCamelCase				:  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 unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
           XLMForMultipleChoice,
           XLMForQuestionAnswering,
           XLMForQuestionAnsweringSimple,
           XLMForSequenceClassification,
           XLMForTokenClassification,
           XLMModel,
           XLMWithLMHeadModel,
       )
       from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST





class  lowerCAmelCase__:






    '''simple docstring'''





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





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   parent
       UpperCAmelCase_							:      Optional[Any]						=   batch_size
       UpperCAmelCase_							:      Union[str, Any]						=   seq_length
       UpperCAmelCase_							:      List[Any]						=   is_training
       UpperCAmelCase_							:      List[Any]						=   use_input_lengths
       UpperCAmelCase_							:      Dict						=   use_token_type_ids
       UpperCAmelCase_							:      Union[str, Any]						=   use_labels
       UpperCAmelCase_							:      int						=   gelu_activation
       UpperCAmelCase_							:      str						=   sinusoidal_embeddings
       UpperCAmelCase_							:      List[str]						=   causal
       UpperCAmelCase_							:      Tuple						=   asm
       UpperCAmelCase_							:      List[Any]						=   n_langs
       UpperCAmelCase_							:      Union[str, Any]						=   vocab_size
       UpperCAmelCase_							:      Any						=   n_special
       UpperCAmelCase_							:      Dict						=   hidden_size
       UpperCAmelCase_							:      Dict						=   num_hidden_layers
       UpperCAmelCase_							:      str						=   num_attention_heads
       UpperCAmelCase_							:      List[str]						=   hidden_dropout_prob
       UpperCAmelCase_							:      Any						=   attention_probs_dropout_prob
       UpperCAmelCase_							:      Tuple						=   max_position_embeddings
       UpperCAmelCase_							:      Union[str, Any]						=   type_sequence_label_size
       UpperCAmelCase_							:      Union[str, Any]						=   initializer_range
       UpperCAmelCase_							:      Tuple						=   num_labels
       UpperCAmelCase_							:      List[Any]						=   num_choices
       UpperCAmelCase_							:      Any						=   summary_type
       UpperCAmelCase_							:      Optional[int]						=   use_proj
       UpperCAmelCase_							:      List[str]						=   scope
       UpperCAmelCase_							:      List[str]						=   bos_token_id





    def 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size	)
       UpperCAmelCase_							:      Union[str, Any]						=   random_attention_mask([self.batch_size, self.seq_length]	)

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

       UpperCAmelCase_							:      int						=   None
       if self.use_token_type_ids:
          UpperCAmelCase_							:      List[str]						=   ids_tensor([self.batch_size, self.seq_length]       ,   self.n_langs	)

       UpperCAmelCase_							:      Optional[int]						=   None
       UpperCAmelCase_							:      Union[str, Any]						=   None
       UpperCAmelCase_							:      str						=   None
       if self.use_labels:
          UpperCAmelCase_							:      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[Any]						=   ids_tensor([self.batch_size]       ,   2	).float()
          UpperCAmelCase_							:      Optional[Any]						=   ids_tensor([self.batch_size]       ,   self.num_choices	)

       UpperCAmelCase_							:      List[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 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       return XLMConfig(
           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       ,   num_labels=self.num_labels       ,   bos_token_id=self.bos_token_id       ,   )





    def 					_lowerCamelCase (	self				:   Optional[Any]       ,   __snake_case				:   int       ,   __snake_case				:   int       ,   __snake_case				:   List[Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Dict       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   XLMModel(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      Optional[int]						=   model(__snake_case       ,   lengths=__snake_case       ,   langs=__snake_case	)
       UpperCAmelCase_							:      Any						=   model(__snake_case       ,   langs=__snake_case	)
       UpperCAmelCase_							:      Any						=   model(__snake_case	)
       self.parent.assertEqual(result.last_hidden_state.shape       ,   (self.batch_size, self.seq_length, self.hidden_size)	)





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Dict       ,   __snake_case				:   Any       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Dict       ,   __snake_case				:   str       ,   __snake_case				:   Tuple       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   int       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   XLMWithLMHeadModel(__snake_case	)
       model.to(__snake_case	)
       model.eval()

       UpperCAmelCase_							:      Optional[Any]						=   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 					_lowerCamelCase (	self				:   Optional[int]       ,   __snake_case				:   Tuple       ,   __snake_case				:   Any       ,   __snake_case				:   Tuple       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   str       ,   __snake_case				:   List[str]       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   XLMForQuestionAnsweringSimple(__snake_case	)
       model.to(__snake_case	)
       model.eval()

       UpperCAmelCase_							:      Optional[int]						=   model(__snake_case	)

       UpperCAmelCase_							:      Any						=   model(__snake_case       ,   start_positions=__snake_case       ,   end_positions=__snake_case	)
       UpperCAmelCase_							:      Optional[Any]						=   outputs
       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 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   int       ,   __snake_case				:   Dict       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Tuple       ,   __snake_case				:   str       ,   __snake_case				:   Tuple       ,   __snake_case				:   int       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   XLMForQuestionAnswering(__snake_case	)
       model.to(__snake_case	)
       model.eval()

       UpperCAmelCase_							:      Union[str, Any]						=   model(__snake_case	)

       UpperCAmelCase_							:      List[str]						=   model(
           __snake_case       ,   start_positions=__snake_case       ,   end_positions=__snake_case       ,   cls_index=__snake_case       ,   is_impossible=__snake_case       ,   p_mask=__snake_case       ,   )

       UpperCAmelCase_							:      Optional[Any]						=   model(
           __snake_case       ,   start_positions=__snake_case       ,   end_positions=__snake_case       ,   cls_index=__snake_case       ,   is_impossible=__snake_case       ,   )

       ((UpperCAmelCase_)		,      )							:      Union[str, Any]						=   result_with_labels.to_tuple()

       UpperCAmelCase_							:      Optional[int]						=   model(__snake_case       ,   start_positions=__snake_case       ,   end_positions=__snake_case	)

       ((UpperCAmelCase_)		,      )							:      str						=   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 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   Dict       ,   __snake_case				:   str       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Dict       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Dict       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Any       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   XLMForSequenceClassification(__snake_case	)
       model.to(__snake_case	)
       model.eval()

       UpperCAmelCase_							:      Union[str, Any]						=   model(__snake_case	)
       UpperCAmelCase_							:      Optional[int]						=   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 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Any       ,   __snake_case				:   List[str]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   Tuple       ,   __snake_case				:   str       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Tuple       ,   __snake_case				:   int       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   self.num_labels
       UpperCAmelCase_							:      Optional[int]						=   XLMForTokenClassification(__snake_case	)
       model.to(__snake_case	)
       model.eval()

       UpperCAmelCase_							:      List[str]						=   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 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   str       ,   __snake_case				:   List[str]       ,   __snake_case				:   Any       ,   __snake_case				:   Tuple       ,   __snake_case				:   Any       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   Dict       ,   __snake_case				:   Dict       ,   __snake_case				:   Optional[int]       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   self.num_choices
       UpperCAmelCase_							:      int						=   XLMForMultipleChoice(config=__snake_case	)
       model.to(__snake_case	)
       model.eval()
       UpperCAmelCase_							:      int						=   input_ids.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
       UpperCAmelCase_							:      int						=   token_type_ids.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
       UpperCAmelCase_							:      List[Any]						=   input_mask.unsqueeze(1	).expand(-1       ,   self.num_choices       ,   -1	).contiguous()
       UpperCAmelCase_							:      Any						=   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 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   self.prepare_config_and_inputs()
       (
           (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      (
           UpperCAmelCase_
       )		,      
       )							:      Union[str, Any]						=   config_and_inputs
       UpperCAmelCase_							:      str						=   {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''lengths''': input_lengths}
       return config, inputs_dict





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






    '''simple docstring'''





    A_  :     Dict			= (
        (
            XLMModel,
            XLMWithLMHeadModel,
            XLMForQuestionAnswering,
            XLMForSequenceClassification,
            XLMForQuestionAnsweringSimple,
            XLMForTokenClassification,
            XLMForMultipleChoice,
        )
        if is_torch_available()
        else ()
    )
    A_  :     List[str]			= (
        (XLMWithLMHeadModel,) if is_torch_available() else ()
    )  # TODO (PVP): Check other models whether language generation is also applicable
    A_  :     Optional[int]			= (
        {
            'feature-extraction': XLMModel,
            'fill-mask': XLMWithLMHeadModel,
            'question-answering': XLMForQuestionAnsweringSimple,
            'text-classification': XLMForSequenceClassification,
            'text-generation': XLMWithLMHeadModel,
            'token-classification': XLMForTokenClassification,
            'zero-shot': XLMForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )





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





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   super()._prepare_for_class(__snake_case       ,   __snake_case       ,   return_labels=__snake_case	)

       if return_labels:
          if model_class.__name__ == "XLMForQuestionAnswering":
             UpperCAmelCase_							:      List[str]						=   torch.zeros(
                 self.model_tester.batch_size       ,   dtype=torch.long       ,   device=__snake_case	)
             UpperCAmelCase_							:      Dict						=   torch.zeros(
                 self.model_tester.batch_size       ,   dtype=torch.long       ,   device=__snake_case	)

       return inputs_dict





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   XLMModelTester(self	)
       UpperCAmelCase_							:      List[Any]						=   ConfigTester(self       ,   config_class=__snake_case       ,   emb_dim=37	)





    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





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





    def 					_lowerCamelCase (	self				:   Any	):





       '''simple docstring'''
       UpperCAmelCase_							:      Any						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_model(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Union[str, Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_lm_head(*__snake_case	)





    def 					_lowerCamelCase (	self				:   Tuple	):





       '''simple docstring'''
       UpperCAmelCase_							:      str						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_simple_qa(*__snake_case	)





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_qa(*__snake_case	)





    def 					_lowerCamelCase (	self				:   List[str]	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case	)





    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_token_classif(*__snake_case	)





    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[int]						=   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case	)





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   str       ,   __snake_case				:   Union[str, Any]       ,   __snake_case				:   List[Any]       ,   __snake_case				:   str       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Union[str, Any]=False       ,   __snake_case				:   Optional[Any]=1	):





       '''simple docstring'''
       self.assertIsInstance(__snake_case       ,   __snake_case	)
       self.assertListEqual(
           [isinstance(__snake_case       ,   __snake_case	) for iter_attentions in attentions]       ,   [True] * len(__snake_case	)	)
       self.assertEqual(len(__snake_case	)       ,   (max_length - min_length) * num_beam_groups	)

       for idx, iter_attentions in enumerate(__snake_case	):
          # adds PAD dummy token
          UpperCAmelCase_							:      Dict						=   min_length + idx + 1
          UpperCAmelCase_							:      List[Any]						=   min_length + idx + 1

          UpperCAmelCase_							:      Optional[int]						=   (
              batch_size * num_beam_groups,
              config.num_attention_heads,
              tgt_len,
              src_len,
          )
          # check attn size
          self.assertListEqual(
              [layer_attention.shape for layer_attention in iter_attentions]       ,   [expected_shape] * len(__snake_case	)	)





    def 					_lowerCamelCase (	self				:   List[Any]       ,   __snake_case				:   List[str]       ,   __snake_case				:   int       ,   __snake_case				:   Optional[int]       ,   __snake_case				:   Dict       ,   __snake_case				:   Any       ,   __snake_case				:   Optional[Any]=False       ,   __snake_case				:   str=1	):





       '''simple docstring'''
       self.assertIsInstance(__snake_case       ,   __snake_case	)
       self.assertListEqual(
           [isinstance(__snake_case       ,   __snake_case	) for iter_hidden_states in hidden_states]       ,   [True] * len(__snake_case	)       ,   )
       self.assertEqual(len(__snake_case	)       ,   (max_length - min_length) * num_beam_groups	)

       for idx, iter_hidden_states in enumerate(__snake_case	):
          # adds PAD dummy token
          UpperCAmelCase_							:      str						=   min_length + idx + 1
          UpperCAmelCase_							:      int						=   (batch_size * num_beam_groups, seq_len, config.hidden_size)
          # check hidden size
          self.assertListEqual(
              [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]       ,   [expected_shape] * len(__snake_case	)       ,   )
       pass





    @slow
    def 					_lowerCamelCase (	self				:   int	):





       '''simple docstring'''
       for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          UpperCAmelCase_							:      Any						=   XLMModel.from_pretrained(__snake_case	)
          self.assertIsNotNone(__snake_case	)





@require_torch
class  lowerCAmelCase__(    unittest.TestCase						):






    '''simple docstring'''





    @slow
    def 					_lowerCamelCase (	self				:   str	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[int]						=   XLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048'''	)
       model.to(__snake_case	)
       UpperCAmelCase_							:      str						=   torch.tensor([[14, 447]]       ,   dtype=torch.long       ,   device=__snake_case	)  # the president
       UpperCAmelCase_							:      Union[str, Any]						=   [
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
           14,
           447,
       ]  # the president the president the president the president the president the president the president the president the president the president
       # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
       UpperCAmelCase_							:      Dict						=   model.generate(__snake_case       ,   do_sample=__snake_case	)
       self.assertListEqual(output_ids[0].cpu().numpy().tolist()       ,   __snake_case	)