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

import torch
import torch.nn as nn
from huggingface_hub import cached_download, hf_hub_download
from torch import Tensor

from transformers import AutoImageProcessor, VanConfig, VanForImageClassification
from transformers.models.deprecated.van.modeling_van import VanLayerScaling
from transformers.utils import logging


logging.set_verbosity_info()
A__ :		Optional[Any]					=logging.get_logger(__name__)







@dataclass
class 			__A					:
 lowerCamelCase												=42
 lowerCamelCase												=field(default_factory=__lowerCamelCase      )
 lowerCamelCase												=field(default_factory=__lowerCamelCase      )
 def 		lowercase_( self      :     Any					, lowerCamelCase      :     str					, lowerCamelCase      :     Tensor					, lowerCamelCase      :     Tensor							):



   """simple docstring"""





   __A		:  List[Any]       				=		len(list(m.modules()							)							) == 1 or isinstance(lowerCamelCase					, nn.Convad							) or isinstance(lowerCamelCase					, nn.BatchNormad							)
   if has_not_submodules:
     if not isinstance(lowerCamelCase					, lowerCamelCase							):
       self.traced.append(lowerCamelCase							)
 def __call__( self      :     Any					, lowerCamelCase      :     Tensor							):



   """simple docstring"""





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



   """simple docstring"""





   return list(filter(lambda lowerCamelCase							: len(list(x.state_dict().keys()							)							) > 0					, self.traced							)							)







@dataclass
class 			__A					:
 lowerCamelCase												=42
 lowerCamelCase												=42
 lowerCamelCase												=0
 lowerCamelCase												=field(default_factory=__lowerCamelCase      )
 lowerCamelCase												=field(default_factory=__lowerCamelCase      )
 def __call__( self      :     Tuple					, lowerCamelCase      :     Tensor							):



   """simple docstring"""





   __A		:  Tuple       				=		Tracker(self.dest							)(lowerCamelCase							).parametrized
   __A		:  List[Any]       				=		Tracker(self.src							)(lowerCamelCase							).parametrized

   __A		:  Tuple       				=		list(filter(lambda lowerCamelCase							: type(lowerCamelCase							) not in self.src_skip					, lowerCamelCase							)							)
   __A		:  str       				=		list(filter(lambda lowerCamelCase							: 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 							A_  (    __SCREAMING_SNAKE_CASE    :	Any      ,  __SCREAMING_SNAKE_CASE    :	Union[str, Any]			)					->							nn.Module:





  """simple docstring"""

  __A		:  Union[str, Any]       				=		from_model.state_dict()
  __A		:  int       				=		our_model.state_dict()
  __A		:  Union[str, Any]       				=		our_model.config
  __A		:  Optional[int]       				=		[]
  for stage_idx in range(len(config.hidden_sizes			)			):
    for block_id in range(config.depths[stage_idx]			):
      __A		:  Tuple       				=		F"block{stage_idx + 1}.{block_id}.layer_scale_1"
      __A		:  Tuple       				=		F"van.encoder.stages.{stage_idx}.layers.{block_id}.attention_scaling.weight"

      all_keys.append((from_key, to_key)			)
      __A		:  Optional[int]       				=		F"block{stage_idx + 1}.{block_id}.layer_scale_2"
      __A		:  Any       				=		F"van.encoder.stages.{stage_idx}.layers.{block_id}.mlp_scaling.weight"

      all_keys.append((from_key, to_key)			)

  for from_key, to_key in all_keys:
    __A		:  List[str]       				=		from_state_dict.pop(SCREAMING_SNAKE_CASE__			)

  our_model.load_state_dict(SCREAMING_SNAKE_CASE__			)
  return our_model







def 							A_  (    __SCREAMING_SNAKE_CASE    :	Optional[int]      ,  __SCREAMING_SNAKE_CASE    :	Union[str, Any]      ,  __SCREAMING_SNAKE_CASE    :	Union[str, Any]      ,  __SCREAMING_SNAKE_CASE    :	Optional[Any]      ,  __SCREAMING_SNAKE_CASE    :	List[str]      ,  __SCREAMING_SNAKE_CASE    :	Union[str, Any] = True      ,  )					->							List[Any]:





  """simple docstring"""

  print(F"Downloading weights for {name}..."			)
  __A		:  List[Any]       				=		cached_download(SCREAMING_SNAKE_CASE__			)
  print(F"Converting {name}..."			)
  __A		:  List[str]       				=		torch.load(SCREAMING_SNAKE_CASE__			)["""state_dict"""]
  from_model.load_state_dict(SCREAMING_SNAKE_CASE__			)
  from_model.eval()
  with torch.no_grad():
    __A		:  List[Any]       				=		VanForImageClassification(SCREAMING_SNAKE_CASE__			).eval()
    __A		:  Optional[Any]       				=		ModuleTransfer(src=SCREAMING_SNAKE_CASE__      ,  dest=SCREAMING_SNAKE_CASE__			)
    __A		:  List[Any]       				=		torch.randn((1, 3, 224, 224)			)
    module_transfer(SCREAMING_SNAKE_CASE__			)
    __A		:  List[str]       				=		copy_parameters(SCREAMING_SNAKE_CASE__      ,  SCREAMING_SNAKE_CASE__			)

  if not torch.allclose(from_model(SCREAMING_SNAKE_CASE__			)      ,  our_model(SCREAMING_SNAKE_CASE__			).logits			):
    raise ValueError("""The model logits don't match the original one."""			)

  __A		:  Tuple       				=		name
  print(SCREAMING_SNAKE_CASE__			)

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

    # we can use the convnext one
    __A		:  Tuple       				=		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=SCREAMING_SNAKE_CASE__      ,  )

    print(F"Pushed {checkpoint_name}"			)







def 							A_  (    __SCREAMING_SNAKE_CASE    :	str      ,  __SCREAMING_SNAKE_CASE    :	Union[str, Any] = None      ,  __SCREAMING_SNAKE_CASE    :	int = True			)					->							int:





  """simple docstring"""

  __A		:  Dict       				=		"""imagenet-1k-id2label.json"""
  __A		:  Optional[int]       				=		1000

  __A		:  Dict       				=		"""huggingface/label-files"""
  __A		:  Any       				=		num_labels
  __A		:  Optional[int]       				=		json.load(open(hf_hub_download(SCREAMING_SNAKE_CASE__      ,  SCREAMING_SNAKE_CASE__      ,  repo_type="""dataset"""			)      ,  """r"""			)			)
  __A		:  List[str]       				=		{int(SCREAMING_SNAKE_CASE__			): v for k, v in idalabel.items()}

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

  __A		:  List[Any]       				=		partial(SCREAMING_SNAKE_CASE__      ,  num_labels=SCREAMING_SNAKE_CASE__      ,  idalabel=SCREAMING_SNAKE_CASE__      ,  labelaid=SCREAMING_SNAKE_CASE__			)

  __A		:  str       				=		{
      """van-tiny""": ImageNetPreTrainedConfig(
          hidden_sizes=[32, 64, 160, 256]      ,  depths=[3, 3, 5, 2]      ,  mlp_ratios=[8, 8, 4, 4]      ,  ),
      """van-small""": ImageNetPreTrainedConfig(
          hidden_sizes=[64, 128, 320, 512]      ,  depths=[2, 2, 4, 2]      ,  mlp_ratios=[8, 8, 4, 4]      ,  ),
      """van-base""": ImageNetPreTrainedConfig(
          hidden_sizes=[64, 128, 320, 512]      ,  depths=[3, 3, 12, 3]      ,  mlp_ratios=[8, 8, 4, 4]      ,  ),
      """van-large""": ImageNetPreTrainedConfig(
          hidden_sizes=[64, 128, 320, 512]      ,  depths=[3, 5, 27, 3]      ,  mlp_ratios=[8, 8, 4, 4]      ,  ),
  }

  __A		:  str       				=		{
      """van-tiny""": van_tiny,
      """van-small""": van_small,
      """van-base""": van_base,
      """van-large""": van_large,
  }

  __A		:  Tuple       				=		{
      """van-tiny""": (
          """https://huggingface.co/Visual-Attention-Network/VAN-Tiny-original/resolve/main/van_tiny_754.pth.tar"""
      ),
      """van-small""": (
          """https://huggingface.co/Visual-Attention-Network/VAN-Small-original/resolve/main/van_small_811.pth.tar"""
      ),
      """van-base""": (
          """https://huggingface.co/Visual-Attention-Network/VAN-Base-original/resolve/main/van_base_828.pth.tar"""
      ),
      """van-large""": (
          """https://huggingface.co/Visual-Attention-Network/VAN-Large-original/resolve/main/van_large_839.pth.tar"""
      ),
  }

  if model_name:
    convert_weight_and_push(
        SCREAMING_SNAKE_CASE__      ,  names_to_config[model_name]      ,  checkpoint=names_to_original_checkpoints[model_name]      ,  from_model=names_to_original_models[model_name]()      ,  save_directory=SCREAMING_SNAKE_CASE__      ,  push_to_hub=SCREAMING_SNAKE_CASE__      ,  )
  else:
    for model_name, config in names_to_config.items():
      convert_weight_and_push(
          SCREAMING_SNAKE_CASE__      ,  SCREAMING_SNAKE_CASE__      ,  checkpoint=names_to_original_checkpoints[model_name]      ,  from_model=names_to_original_models[model_name]()      ,  save_directory=SCREAMING_SNAKE_CASE__      ,  push_to_hub=SCREAMING_SNAKE_CASE__      ,  )


if __name__ == "__main__":
     A__ :		List[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: van-tiny/small/base/large. 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(
         '--van_dir',
         required=True,
         type=Path,
         help=(
             'A path to VAN\'s original implementation directory. You can download from here:'
             ' https://github.com/Visual-Attention-Network/VAN-Classification'
         ),
     )
     parser.add_argument(
         '--push_to_hub',
         default=True,
         type=bool,
         required=False,
         help='If True, push model and image processor to the hub.',
     )

     A__ :		Dict					=parser.parse_args()
     A__ :		Path					=args.pytorch_dump_folder_path
     pytorch_dump_folder_path.mkdir(exist_ok=True, parents=True)
     A__ :		List[str]					=args.van_dir
     # append the path to the parents to maskformer dir
     sys.path.append(str(van_dir.parent))
     from van.models.van import van_base, van_large, van_small, van_tiny

     convert_weights_and_push(pytorch_dump_folder_path, args.model_name, args.push_to_hub)










import unittest

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          YosoForMaskedLM,
          YosoForMultipleChoice,
          YosoForQuestionAnswering,
          YosoForSequenceClassification,
          YosoForTokenClassification,
          YosoModel,
      )
      from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST







class        __snake_case			:





       '''simple docstring'''







       def __init__(    self					:       Union[str, Any]				,							A					:       Optional[Any]				,							A					:       str=13				,							A					:       List[Any]=7				,							A					:       str=True				,							A					:       Optional[int]=True				,							A					:       Any=True				,							A					:       List[str]=True				,							A					:       str=99				,							A					:       str=32				,							A					:       Any=5				,							A					:       Any=4				,							A					:       str=37				,							A					:       List[Any]="gelu"				,							A					:       Union[str, Any]=0.1				,							A					:       str=0.1				,							A					:       List[Any]=512				,							A					:       Optional[int]=16				,							A					:       List[str]=2				,							A					:       Optional[int]=0.02				,							A					:       Optional[int]=3				,							A					:       int=4				,							A					:       List[Any]=None				,							):
         __snake_case:		Any					    =		parent
         __snake_case:		Optional[int]					    =		batch_size
         __snake_case:		Optional[int]					    =		seq_length
         __snake_case:		Any					    =		is_training
         __snake_case:		int					    =		use_input_mask
         __snake_case:		Tuple					    =		use_token_type_ids
         __snake_case:		Optional[Any]					    =		use_labels
         __snake_case:		Optional[Any]					    =		vocab_size
         __snake_case:		str					    =		hidden_size
         __snake_case:		int					    =		num_hidden_layers
         __snake_case:		Optional[int]					    =		num_attention_heads
         __snake_case:		Tuple					    =		intermediate_size
         __snake_case:		Optional[int]					    =		hidden_act
         __snake_case:		Union[str, Any]					    =		hidden_dropout_prob
         __snake_case:		Any					    =		attention_probs_dropout_prob
         __snake_case:		List[str]					    =		max_position_embeddings
         __snake_case:		Any					    =		type_vocab_size
         __snake_case:		Dict					    =		type_sequence_label_size
         __snake_case:		int					    =		initializer_range
         __snake_case:		Union[str, Any]					    =		num_labels
         __snake_case:		int					    =		num_choices
         __snake_case:		List[Any]					    =		scope




       def 				UpperCAmelCase__					(    self					:       List[Any]						):
         __snake_case:		Any					    =		ids_tensor([self.batch_size, self.seq_length]				,							self.vocab_size						)

         __snake_case:		Any					    =		None
         if self.use_input_mask:
           __snake_case:		str					    =		random_attention_mask([self.batch_size, self.seq_length]						)

         __snake_case:		Tuple					    =		None
         if self.use_token_type_ids:
           __snake_case:		Tuple					    =		ids_tensor([self.batch_size, self.seq_length]				,							self.type_vocab_size						)

         __snake_case:		int					    =		None
         __snake_case:		Union[str, Any]					    =		None
         __snake_case:		str					    =		None
         if self.use_labels:
           __snake_case:		str					    =		ids_tensor([self.batch_size]				,							self.type_sequence_label_size						)
           __snake_case:		int					    =		ids_tensor([self.batch_size, self.seq_length]				,							self.num_labels						)
           __snake_case:		Tuple					    =		ids_tensor([self.batch_size]				,							self.num_choices						)

         __snake_case:		List[str]					    =		self.get_config()

         return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




       def 				UpperCAmelCase__					(    self					:       Union[str, Any]						):
         return YosoConfig(
             vocab_size=self.vocab_size				,							hidden_size=self.hidden_size				,							num_hidden_layers=self.num_hidden_layers				,							num_attention_heads=self.num_attention_heads				,							intermediate_size=self.intermediate_size				,							hidden_act=self.hidden_act				,							hidden_dropout_prob=self.hidden_dropout_prob				,							attention_probs_dropout_prob=self.attention_probs_dropout_prob				,							max_position_embeddings=self.max_position_embeddings				,							type_vocab_size=self.type_vocab_size				,							is_decoder=A				,							initializer_range=self.initializer_range				,							)




       def 				UpperCAmelCase__					(    self					:       Any						):
         __snake_case:		str					    =		self.get_config()
         __snake_case:		Optional[Any]					    =		300
         return config




       def 				UpperCAmelCase__					(    self					:       Optional[Any]						):
         (
             (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  
         ):		List[str]					    =		self.prepare_config_and_inputs()

         __snake_case:		Optional[Any]					    =		True
         __snake_case:		Optional[Any]					    =		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
         __snake_case:		Tuple					    =		ids_tensor([self.batch_size, self.seq_length]				,							vocab_size=2						)

         return (
             config,
             input_ids,
             token_type_ids,
             input_mask,
             sequence_labels,
             token_labels,
             choice_labels,
             encoder_hidden_states,
             encoder_attention_mask,
         )




       def 				UpperCAmelCase__					(    self					:       Any				,							A					:       Dict				,							A					:       str				,							A					:       Tuple				,							A					:       Dict				,							A					:       List[str]				,							A					:       List[str]				,							A					:       Optional[int]						):
         __snake_case:		Optional[int]					    =		YosoModel(config=A						)
         model.to(A						)
         model.eval()
         __snake_case:		List[Any]					    =		model(A				,							attention_mask=A				,							token_type_ids=A						)
         __snake_case:		str					    =		model(A				,							token_type_ids=A						)
         __snake_case:		int					    =		model(A						)
         self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)						)




       def 				UpperCAmelCase__					(    self					:       Optional[int]				,							A					:       Dict				,							A					:       int				,							A					:       str				,							A					:       List[str]				,							A					:       int				,							A					:       Optional[int]				,							A					:       str				,							A					:       Optional[int]				,							A					:       str				,							):
         __snake_case:		Optional[Any]					    =		True
         __snake_case:		List[str]					    =		YosoModel(A						)
         model.to(A						)
         model.eval()
         __snake_case:		Optional[int]					    =		model(
             A				,							attention_mask=A				,							token_type_ids=A				,							encoder_hidden_states=A				,							encoder_attention_mask=A				,							)
         __snake_case:		Optional[int]					    =		model(
             A				,							attention_mask=A				,							token_type_ids=A				,							encoder_hidden_states=A				,							)
         __snake_case:		Optional[int]					    =		model(A				,							attention_mask=A				,							token_type_ids=A						)
         self.parent.assertEqual(result.last_hidden_state.shape				,							(self.batch_size, self.seq_length, self.hidden_size)						)




       def 				UpperCAmelCase__					(    self					:       Optional[Any]				,							A					:       str				,							A					:       Optional[int]				,							A					:       List[Any]				,							A					:       Optional[Any]				,							A					:       Dict				,							A					:       int				,							A					:       Optional[int]						):
         __snake_case:		List[str]					    =		YosoForMaskedLM(config=A						)
         model.to(A						)
         model.eval()
         __snake_case:		int					    =		model(A				,							attention_mask=A				,							token_type_ids=A				,							labels=A						)
         self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length, self.vocab_size)						)




       def 				UpperCAmelCase__					(    self					:       int				,							A					:       Any				,							A					:       Union[str, Any]				,							A					:       Optional[int]				,							A					:       Any				,							A					:       int				,							A					:       Any				,							A					:       Optional[int]						):
         __snake_case:		int					    =		YosoForQuestionAnswering(config=A						)
         model.to(A						)
         model.eval()
         __snake_case:		Any					    =		model(
             A				,							attention_mask=A				,							token_type_ids=A				,							start_positions=A				,							end_positions=A				,							)
         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 				UpperCAmelCase__					(    self					:       str				,							A					:       Union[str, Any]				,							A					:       Tuple				,							A					:       Union[str, Any]				,							A					:       int				,							A					:       Optional[int]				,							A					:       int				,							A					:       List[Any]						):
         __snake_case:		Union[str, Any]					    =		self.num_labels
         __snake_case:		Union[str, Any]					    =		YosoForSequenceClassification(A						)
         model.to(A						)
         model.eval()
         __snake_case:		Optional[int]					    =		model(A				,							attention_mask=A				,							token_type_ids=A				,							labels=A						)
         self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.num_labels)						)




       def 				UpperCAmelCase__					(    self					:       Dict				,							A					:       Optional[int]				,							A					:       Dict				,							A					:       Any				,							A					:       Dict				,							A					:       str				,							A					:       int				,							A					:       Optional[int]						):
         __snake_case:		List[Any]					    =		self.num_labels
         __snake_case:		Union[str, Any]					    =		YosoForTokenClassification(config=A						)
         model.to(A						)
         model.eval()
         __snake_case:		List[Any]					    =		model(A				,							attention_mask=A				,							token_type_ids=A				,							labels=A						)
         self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.seq_length, self.num_labels)						)




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






       def 				UpperCAmelCase__					(    self					:       List[Any]						):
         __snake_case:		List[str]					    =		self.prepare_config_and_inputs()
         (
             (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  (
             __snake_case
         )    ,  
         ):		int					    =		config_and_inputs
         __snake_case:		str					    =		{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
         return config, inputs_dict







@require_torch
class        __snake_case			(				__lowerCamelCase  ,     __lowerCamelCase  ,     unittest.TestCase     ):





       '''simple docstring'''







       lowerCAmelCase__ 						=	(
           (
               YosoModel,
               YosoForMaskedLM,
               YosoForMultipleChoice,
               YosoForQuestionAnswering,
               YosoForSequenceClassification,
               YosoForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       lowerCAmelCase__ 						=	False
       lowerCAmelCase__ 						=	False
       lowerCAmelCase__ 						=	False

       lowerCAmelCase__ 						=	()
       lowerCAmelCase__ 						=	(
           {
               """feature-extraction""": YosoModel,
               """fill-mask""": YosoForMaskedLM,
               """question-answering""": YosoForQuestionAnswering,
               """text-classification""": YosoForSequenceClassification,
               """token-classification""": YosoForTokenClassification,
               """zero-shot""": YosoForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )




       def 				UpperCAmelCase__					(    self					:       str						):
         __snake_case:		List[str]					    =		YosoModelTester(self						)
         __snake_case:		Optional[int]					    =		ConfigTester(self				,							config_class=A				,							hidden_size=37						)




       def 				UpperCAmelCase__					(    self					:       List[str]						):
         self.config_tester.run_common_tests()




       def 				UpperCAmelCase__					(    self					:       Optional[int]						):
         __snake_case:		Union[str, Any]					    =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*A						)




       def 				UpperCAmelCase__					(    self					:       Union[str, Any]						):
         __snake_case:		Optional[Any]					    =		self.model_tester.prepare_config_and_inputs()
         for type in ["absolute", "relative_key", "relative_key_query"]:
           __snake_case:		Optional[Any]					    =		type
           self.model_tester.create_and_check_model(*A						)




       def 				UpperCAmelCase__					(    self					:       str						):
         __snake_case:		List[Any]					    =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*A						)




       def 				UpperCAmelCase__					(    self					:       List[Any]						):
         __snake_case:		List[str]					    =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_multiple_choice(*A						)




       def 				UpperCAmelCase__					(    self					:       int						):
         __snake_case:		Any					    =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*A						)




       def 				UpperCAmelCase__					(    self					:       Optional[Any]						):
         __snake_case:		List[Any]					    =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*A						)




       def 				UpperCAmelCase__					(    self					:       List[str]						):
         __snake_case:		Optional[int]					    =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*A						)




       @slow
       def 				UpperCAmelCase__					(    self					:       Any						):
         for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           __snake_case:		str					    =		YosoModel.from_pretrained(A						)
           self.assertIsNotNone(A						)






       def 				UpperCAmelCase__					(    self					:       Tuple						):
         return







@require_torch
class        __snake_case			(				unittest.TestCase     ):





       '''simple docstring'''







       @slow
       def 				UpperCAmelCase__					(    self					:       Tuple						):
         __snake_case:		Any					    =		YosoModel.from_pretrained("""uw-madison/yoso-4096"""						)
         __snake_case:		Union[str, Any]					    =		torch.tensor([[0, 1, 2, 3, 4, 5]]						)

         with torch.no_grad():
           __snake_case:		Optional[Any]					    =		model(A						)[0]

         __snake_case:		List[str]					    =		torch.Size((1, 6, 768)						)
         self.assertEqual(output.shape				,							A						)

         __snake_case:		List[Any]					    =		torch.tensor(
             [[[-0.0611, 0.1242, 0.0840], [0.0280, -0.0048, 0.1125], [0.0106, 0.0226, 0.0751]]]						)

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




       @slow
       def 				UpperCAmelCase__					(    self					:       Optional[Any]						):
         __snake_case:		Optional[Any]					    =		YosoForMaskedLM.from_pretrained("""uw-madison/yoso-4096"""						)
         __snake_case:		Tuple					    =		torch.tensor([[0, 1, 2, 3, 4, 5]]						)

         with torch.no_grad():
           __snake_case:		Optional[int]					    =		model(A						)[0]

         __snake_case:		Tuple					    =		50_265

         __snake_case:		List[str]					    =		torch.Size((1, 6, vocab_size)						)
         self.assertEqual(output.shape				,							A						)

         __snake_case:		Any					    =		torch.tensor(
             [[[-2.1313, -3.7285, -2.2407], [-2.7047, -3.3314, -2.6408], [0.0629, -2.5166, -0.3356]]]						)

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






       @slow
       def 				UpperCAmelCase__					(    self					:       Any						):
         __snake_case:		int					    =		YosoForMaskedLM.from_pretrained("""uw-madison/yoso-4096"""						)
         __snake_case:		Optional[Any]					    =		torch.arange(4_096						).unsqueeze(0						)

         with torch.no_grad():
           __snake_case:		Tuple					    =		model(A						)[0]

         __snake_case:		List[str]					    =		50_265

         __snake_case:		Union[str, Any]					    =		torch.Size((1, 4_096, vocab_size)						)
         self.assertEqual(output.shape				,							A						)

         __snake_case:		List[str]					    =		torch.tensor(
             [[[-2.3914, -4.3742, -5.0956], [-4.0988, -4.2384, -7.0406], [-3.1427, -3.7192, -6.6800]]]						)

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






