
import unittest

from transformers import BarthezTokenizer, BarthezTokenizerFast, BatchEncoding
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
@require_sentencepiece
@slow  # see https://github.com/huggingface/transformers/issues/11457
class 		UpperCamelCase__  (	__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							int     	=				BarthezTokenizer
   _SCREAMING_SNAKE_CASE				:							List[str]     	=				BarthezTokenizerFast
   _SCREAMING_SNAKE_CASE				:							List[str]     	=				True
   _SCREAMING_SNAKE_CASE				:							Dict     	=				True
   def 				lowerCAmelCase      (self			: Dict       ):
    super().setUp()

    __a			:  Union[str, Any]		    =  BarthezTokenizerFast.from_pretrained('''moussaKam/mbarthez'''       )
    tokenizer.save_pretrained(self.tmpdirname       )
    tokenizer.save_pretrained(self.tmpdirname ,    legacy_format=snake_case_       )
    __a			:  Any		    =  tokenizer
   def 				lowerCAmelCase      (self			: Dict       ):
    __a			:  Optional[int]		    =  '''<pad>'''
    __a			:  Any		    =  1

    self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case_       ) ,    snake_case_       )
    self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case_       ) ,    snake_case_       )
   def 				lowerCAmelCase      (self			: Optional[int]       ):
    __a			:  List[Any]		    =  list(self.get_tokenizer().get_vocab().keys()       )

    self.assertEqual(vocab_keys[0] ,    '''<s>'''       )
    self.assertEqual(vocab_keys[1] ,    '''<pad>'''       )
    self.assertEqual(vocab_keys[-1] ,    '''<mask>'''       )
    self.assertEqual(len(snake_case_       ) ,    1_0_1_1_2_2       )
   def 				lowerCAmelCase      (self			: Optional[Any]       ):
    self.assertEqual(self.get_tokenizer().vocab_size ,    1_0_1_1_2_2       )
   @require_torch
   def 				lowerCAmelCase      (self			: List[str]       ):
    __a			:  List[str]		    =  ['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
    __a			:  Optional[Any]		    =  [0, 5_7, 3_0_1_8, 7_0_3_0_7, 9_1, 2]

    __a			:  Optional[int]		    =  self.tokenizer(
        snake_case_ ,    max_length=len(snake_case_       ) ,    padding=snake_case_ ,    truncation=snake_case_ ,    return_tensors='''pt'''       )
    self.assertIsInstance(snake_case_ ,    snake_case_       )

    self.assertEqual((2, 6) ,    batch.input_ids.shape       )
    self.assertEqual((2, 6) ,    batch.attention_mask.shape       )
    __a			:  str		    =  batch.input_ids.tolist()[0]
    self.assertListEqual(snake_case_ ,    snake_case_       )
   def 				lowerCAmelCase      (self			: Dict       ):
    if not self.test_rust_tokenizer:
     return

    __a			:  Dict		    =  self.get_tokenizer()
    __a			:  List[str]		    =  self.get_rust_tokenizer()

    __a			:  Tuple		    =  '''I was born in 92000, and this is falsé.'''

    __a			:  Optional[int]		    =  tokenizer.tokenize(snake_case_       )
    __a			:  Optional[int]		    =  rust_tokenizer.tokenize(snake_case_       )
    self.assertListEqual(snake_case_ ,    snake_case_       )

    __a			:  str		    =  tokenizer.encode(snake_case_ ,    add_special_tokens=snake_case_       )
    __a			:  Any		    =  rust_tokenizer.encode(snake_case_ ,    add_special_tokens=snake_case_       )
    self.assertListEqual(snake_case_ ,    snake_case_       )

    __a			:  Any		    =  self.get_rust_tokenizer()
    __a			:  str		    =  tokenizer.encode(snake_case_       )
    __a			:  List[str]		    =  rust_tokenizer.encode(snake_case_       )
    self.assertListEqual(snake_case_ ,    snake_case_       )







   @slow
   def 				lowerCAmelCase      (self			: Optional[int]       ):
    # fmt: off
    __a			:  Optional[int]		    =  {'''input_ids''': [[0, 4_9_0, 1_4_3_2_8, 4_5_0_7, 3_5_4, 4_7, 4_3_6_6_9, 9_5, 2_5, 7_8_1_1_7, 2_0_2_1_5, 1_9_7_7_9, 1_9_0, 2_2, 4_0_0, 4, 3_5_3_4_3, 8_0_3_1_0, 6_0_3, 8_6, 2_4_9_3_7, 1_0_5, 3_3_4_3_8, 9_4_7_6_2, 1_9_6, 3_9_6_4_2, 7, 1_5, 1_5_9_3_3, 1_7_3, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1_0_5_3_4, 8_7, 2_5, 6_6, 3_3_5_8, 1_9_6, 5_5_2_8_9, 8, 8_2_9_6_1, 8_1, 2_2_0_4, 7_5_2_0_3, 7, 1_5, 7_6_3, 1_2_9_5_6, 2_1_6, 1_7_8, 1_4_3_2_8, 9_5_9_5, 1_3_7_7, 6_9_6_9_3, 7, 4_4_8, 7_1_0_2_1, 1_9_6, 1_8_1_0_6, 1_4_3_7, 1_3_9_7_4, 1_0_8, 9_0_8_3, 4, 4_9_3_1_5, 7, 3_9, 8_6, 1_3_2_6, 2_7_9_3, 4_6_3_3_3, 4, 4_4_8, 1_9_6, 7_4_5_8_8, 7, 4_9_3_1_5, 7, 3_9, 2_1, 8_2_2, 3_8_4_7_0, 7_4, 2_1, 6_6_7_2_3, 6_2_4_8_0, 8, 2_2_0_5_0, 5, 2]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
    # fmt: on

    # moussaKam/mbarthez is a french model. So we also use french texts.
    __a			:  Optional[int]		    =  [
        '''Le transformeur est un modèle d\'apprentissage profond introduit en 2017, '''
        '''utilisé principalement dans le domaine du traitement automatique des langues (TAL).''',
        '''À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus '''
        '''pour gérer des données séquentielles, telles que le langage naturel, pour des tâches '''
        '''telles que la traduction et la synthèse de texte.''',
    ]

    self.tokenizer_integration_test_util(
        expected_encoding=snake_case_ ,    model_name='''moussaKam/mbarthez''' ,    revision='''c2e4ecbca5e3cd2c37fe1ac285ca4fbdf1366fb6''' ,    sequences=snake_case_ ,    )


import collections
import inspect
import unittest

from transformers import FocalNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch
      from torch import nn

      from transformers import (
          FocalNetBackbone,
          FocalNetForImageClassification,
          FocalNetForMaskedImageModeling,
          FocalNetModel,
      )
      from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor



class 		UpperCamelCase__  :
   def __init__(self			: Optional[Any] ,    snake_case_			: Union[str, Any] ,    snake_case_			: Optional[Any]=1_3 ,    snake_case_			: str=3_2 ,    snake_case_			: Any=2 ,    snake_case_			: Union[str, Any]=3 ,    snake_case_			: int=1_6 ,    snake_case_			: Optional[int]=[3_2, 6_4, 1_2_8] ,    snake_case_			: str=[1, 2, 1] ,    snake_case_			: str=[2, 2, 4] ,    snake_case_			: List[str]=2 ,    snake_case_			: List[str]=2.0 ,    snake_case_			: List[Any]=True ,    snake_case_			: Tuple=0.0 ,    snake_case_			: Optional[Any]=0.0 ,    snake_case_			: int=0.1 ,    snake_case_			: Optional[int]="gelu" ,    snake_case_			: List[str]=False ,    snake_case_			: Optional[int]=True ,    snake_case_			: Optional[int]=0.02 ,    snake_case_			: List[str]=1E-5 ,    snake_case_			: List[Any]=True ,    snake_case_			: int=None ,    snake_case_			: List[Any]=True ,    snake_case_			: Optional[Any]=1_0 ,    snake_case_			: Union[str, Any]=8 ,    snake_case_			: Optional[Any]=["stage1", "stage2"] ,    snake_case_			: List[Any]=[1, 2] ,    ):
    __a			:  Tuple		    =  parent
    __a			:  str		    =  batch_size
    __a			:  Any		    =  image_size
    __a			:  List[Any]		    =  patch_size
    __a			:  List[Any]		    =  num_channels
    __a			:  List[str]		    =  embed_dim
    __a			:  str		    =  hidden_sizes
    __a			:  Any		    =  depths
    __a			:  List[str]		    =  num_heads
    __a			:  Any		    =  window_size
    __a			:  List[str]		    =  mlp_ratio
    __a			:  Optional[int]		    =  qkv_bias
    __a			:  Any		    =  hidden_dropout_prob
    __a			:  List[str]		    =  attention_probs_dropout_prob
    __a			:  str		    =  drop_path_rate
    __a			:  Optional[Any]		    =  hidden_act
    __a			:  Optional[int]		    =  use_absolute_embeddings
    __a			:  List[str]		    =  patch_norm
    __a			:  int		    =  layer_norm_eps
    __a			:  Optional[Any]		    =  initializer_range
    __a			:  List[str]		    =  is_training
    __a			:  Dict		    =  scope
    __a			:  Optional[Any]		    =  use_labels
    __a			:  Union[str, Any]		    =  type_sequence_label_size
    __a			:  Optional[int]		    =  encoder_stride
    __a			:  str		    =  out_features
    __a			:  Optional[int]		    =  out_indices
   def 				lowerCAmelCase      (self			: Dict       ):
    __a			:  Tuple		    =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

    __a			:  Dict		    =  None
    if self.use_labels:
     __a			:  List[Any]		    =  ids_tensor([self.batch_size] ,    self.type_sequence_label_size       )

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

    return config, pixel_values, labels
   def 				lowerCAmelCase      (self			: Optional[Any]       ):
    return FocalNetConfig(
        image_size=self.image_size ,    patch_size=self.patch_size ,    num_channels=self.num_channels ,    embed_dim=self.embed_dim ,    hidden_sizes=self.hidden_sizes ,    depths=self.depths ,    num_heads=self.num_heads ,    window_size=self.window_size ,    mlp_ratio=self.mlp_ratio ,    qkv_bias=self.qkv_bias ,    hidden_dropout_prob=self.hidden_dropout_prob ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob ,    drop_path_rate=self.drop_path_rate ,    hidden_act=self.hidden_act ,    use_absolute_embeddings=self.use_absolute_embeddings ,    path_norm=self.patch_norm ,    layer_norm_eps=self.layer_norm_eps ,    initializer_range=self.initializer_range ,    encoder_stride=self.encoder_stride ,    out_features=self.out_features ,    out_indices=self.out_indices ,    )
   def 				lowerCAmelCase      (self			: Dict ,    snake_case_			: int ,    snake_case_			: Tuple ,    snake_case_			: str       ):
    __a			:  int		    =  FocalNetModel(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Dict		    =  model(snake_case_       )

    __a			:  Union[str, Any]		    =  ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
    __a			:  Dict		    =  int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

    self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, expected_seq_len, expected_dim)       )
   def 				lowerCAmelCase      (self			: Tuple ,    snake_case_			: Any ,    snake_case_			: Union[str, Any] ,    snake_case_			: Optional[int]       ):
    __a			:  List[str]		    =  FocalNetBackbone(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  List[str]		    =  model(snake_case_       )

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps       ) ,    len(config.out_features       )       )
    self.parent.assertListEqual(list(result.feature_maps[0].shape       ) ,    [self.batch_size, self.image_size, 8, 8]       )

    # verify channels
    self.parent.assertEqual(len(model.channels       ) ,    len(config.out_features       )       )
    self.parent.assertListEqual(model.channels ,    config.hidden_sizes[:-1]       )

    # verify backbone works with out_features=None
    __a			:  Union[str, Any]		    =  None
    __a			:  Tuple		    =  FocalNetBackbone(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  int		    =  model(snake_case_       )

    # verify feature maps
    self.parent.assertEqual(len(result.feature_maps       ) ,    1       )
    self.parent.assertListEqual(list(result.feature_maps[0].shape       ) ,    [self.batch_size, self.image_size * 2, 4, 4]       )

    # verify channels
    self.parent.assertEqual(len(model.channels       ) ,    1       )
    self.parent.assertListEqual(model.channels ,    [config.hidden_sizes[-1]]       )
   def 				lowerCAmelCase      (self			: Optional[Any] ,    snake_case_			: Optional[int] ,    snake_case_			: Dict ,    snake_case_			: Optional[Any]       ):
    __a			:  List[str]		    =  FocalNetForMaskedImageModeling(config=snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  int		    =  model(snake_case_       )
    self.parent.assertEqual(
        result.reconstruction.shape ,    (self.batch_size, self.num_channels, self.image_size, self.image_size)       )

    # test greyscale images
    __a			:  str		    =  1
    __a			:  Optional[Any]		    =  FocalNetForMaskedImageModeling(snake_case_       )
    model.to(snake_case_       )
    model.eval()

    __a			:  Any		    =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
    __a			:  Union[str, Any]		    =  model(snake_case_       )
    self.parent.assertEqual(result.reconstruction.shape ,    (self.batch_size, 1, self.image_size, self.image_size)       )
   def 				lowerCAmelCase      (self			: Union[str, Any] ,    snake_case_			: List[Any] ,    snake_case_			: Any ,    snake_case_			: int       ):
    __a			:  Dict		    =  self.type_sequence_label_size
    __a			:  Optional[Any]		    =  FocalNetForImageClassification(snake_case_       )
    model.to(snake_case_       )
    model.eval()
    __a			:  Union[str, Any]		    =  model(snake_case_ ,    labels=snake_case_       )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.type_sequence_label_size)       )

    # test greyscale images
    __a			:  Optional[int]		    =  1
    __a			:  str		    =  FocalNetForImageClassification(snake_case_       )
    model.to(snake_case_       )
    model.eval()

    __a			:  str		    =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
    __a			:  List[Any]		    =  model(snake_case_       )
    self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.type_sequence_label_size)       )







   def 				lowerCAmelCase      (self			: List[Any]       ):
    __a			:  Any		    =  self.prepare_config_and_inputs()

    __a    ,	__a    ,	__a			:  Any		    =  config_and_inputs
    __a			:  Tuple		    =  {'''pixel_values''': pixel_values}
    return config, inputs_dict





@require_torch
class 		UpperCamelCase__  (	__lowercase   ,__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							Optional[Any]     	=				(
       (
           FocalNetModel,
           FocalNetForImageClassification,
           FocalNetForMaskedImageModeling,
           FocalNetBackbone,
       )
       if is_torch_available()
       else ()
   )
   _SCREAMING_SNAKE_CASE				:							Optional[Any]     	=				(
       {"feature-extraction": FocalNetModel, "image-classification": FocalNetForImageClassification}
       if is_torch_available()
       else {}
   )
   _SCREAMING_SNAKE_CASE				:							str     	=				False

   _SCREAMING_SNAKE_CASE				:							str     	=				False
   _SCREAMING_SNAKE_CASE				:							Optional[Any]     	=				False
   _SCREAMING_SNAKE_CASE				:							Any     	=				False
   _SCREAMING_SNAKE_CASE				:							Dict     	=				False
   def 				lowerCAmelCase      (self			: List[Any]       ):
    __a			:  Union[str, Any]		    =  FocalNetModelTester(self       )
    __a			:  Dict		    =  ConfigTester(self ,    config_class=snake_case_ ,    embed_dim=3_7 ,    has_text_modality=snake_case_       )
   def 				lowerCAmelCase      (self			: Any       ):
    self.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()
   def 				lowerCAmelCase      (self			: Dict       ):
    return
   def 				lowerCAmelCase      (self			: Dict       ):
    __a			:  Union[str, Any]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*snake_case_       )
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  Dict		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_backbone(*snake_case_       )
   def 				lowerCAmelCase      (self			: Any       ):
    __a			:  Dict		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_image_modeling(*snake_case_       )
   def 				lowerCAmelCase      (self			: Optional[int]       ):
    __a			:  Union[str, Any]		    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*snake_case_       )
   @unittest.skip(reason='''FocalNet does not use inputs_embeds'''       )
   def 				lowerCAmelCase      (self			: Optional[Any]       ):
    pass
   @unittest.skip(reason='''FocalNet does not use feedforward chunking'''       )
   def 				lowerCAmelCase      (self			: str       ):
    pass
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a    ,	__a			:  Dict		    =  self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes[:-1]:
     __a			:  int		    =  model_class(snake_case_       )
     self.assertIsInstance(model.get_input_embeddings() ,    (nn.Module)       )
     __a			:  List[Any]		    =  model.get_output_embeddings()
     self.assertTrue(x is None or isinstance(snake_case_ ,    nn.Linear       )       )
   def 				lowerCAmelCase      (self			: Optional[int]       ):
    __a    ,	__a			:  Optional[int]		    =  self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes[:-1]:
     __a			:  str		    =  model_class(snake_case_       )
     __a			:  int		    =  inspect.signature(model.forward       )
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     __a			:  Dict		    =  [*signature.parameters.keys()]

     __a			:  str		    =  ['''pixel_values''']
     self.assertListEqual(arg_names[:1] ,    snake_case_       )
   def 				lowerCAmelCase      (self			: Tuple ,    snake_case_			: Tuple ,    snake_case_			: Dict ,    snake_case_			: Any ,    snake_case_			: Optional[Any]       ):
    __a			:  Any		    =  model_class(snake_case_       )
    model.to(snake_case_       )
    model.eval()

    with torch.no_grad():
     __a			:  Dict		    =  model(**self._prepare_for_class(snake_case_ ,    snake_case_       )       )

    __a			:  Union[str, Any]		    =  outputs.hidden_states

    __a			:  Dict		    =  getattr(
        self.model_tester ,    '''expected_num_hidden_layers''' ,    len(self.model_tester.depths       ) + 1       )
    self.assertEqual(len(snake_case_       ) ,    snake_case_       )

    # FocalNet has a different seq_length
    __a			:  int		    =  (
        config.patch_size
        if isinstance(config.patch_size ,    collections.abc.Iterable       )
        else (config.patch_size, config.patch_size)
    )

    __a			:  Dict		    =  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

    self.assertListEqual(
        list(hidden_states[0].shape[-2:]       ) ,    [num_patches, self.model_tester.embed_dim] ,    )

    __a			:  Optional[Any]		    =  outputs.reshaped_hidden_states
    self.assertEqual(len(snake_case_       ) ,    snake_case_       )

    __a    ,	__a    ,	__a    ,	__a			:  List[Any]		    =  reshaped_hidden_states[0].shape
    __a			:  List[str]		    =  (
        reshaped_hidden_states[0].view(snake_case_ ,    snake_case_ ,    height * width       ).permute(0 ,    2 ,    1       )
    )
    self.assertListEqual(
        list(reshaped_hidden_states.shape[-2:]       ) ,    [num_patches, self.model_tester.embed_dim] ,    )
   def 				lowerCAmelCase      (self			: Optional[int]       ):
    __a    ,	__a			:  Dict		    =  self.model_tester.prepare_config_and_inputs_for_common()

    __a			:  Dict		    =  (
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size ,    collections.abc.Iterable       )
        else (self.model_tester.image_size, self.model_tester.image_size)
    )

    for model_class in self.all_model_classes[:-1]:
     __a			:  Any		    =  True
     self.check_hidden_states_output(snake_case_ ,    snake_case_ ,    snake_case_ ,    snake_case_       )

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     __a			:  Optional[int]		    =  True

     self.check_hidden_states_output(snake_case_ ,    snake_case_ ,    snake_case_ ,    snake_case_       )
   def 				lowerCAmelCase      (self			: List[Any]       ):
    __a    ,	__a			:  List[Any]		    =  self.model_tester.prepare_config_and_inputs_for_common()
    __a			:  List[str]		    =  3

    __a			:  Optional[int]		    =  (
        self.model_tester.image_size
        if isinstance(self.model_tester.image_size ,    collections.abc.Iterable       )
        else (self.model_tester.image_size, self.model_tester.image_size)
    )
    __a			:  List[str]		    =  (
        config.patch_size
        if isinstance(config.patch_size ,    collections.abc.Iterable       )
        else (config.patch_size, config.patch_size)
    )

    __a			:  List[Any]		    =  image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
    __a			:  Union[str, Any]		    =  image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

    for model_class in self.all_model_classes[:-1]:
     __a			:  int		    =  True
     self.check_hidden_states_output(snake_case_ ,    snake_case_ ,    snake_case_ ,    (padded_height, padded_width)       )

     # check that output_hidden_states also work using config
     del inputs_dict["output_hidden_states"]
     __a			:  List[str]		    =  True
     self.check_hidden_states_output(snake_case_ ,    snake_case_ ,    snake_case_ ,    (padded_height, padded_width)       )
   @slow
   def 				lowerCAmelCase      (self			: str       ):
    for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __a			:  Dict		    =  FocalNetModel.from_pretrained(snake_case_       )
     self.assertIsNotNone(snake_case_       )







   def 				lowerCAmelCase      (self			: List[Any]       ):
    __a    ,	__a			:  Optional[int]		    =  self.model_tester.prepare_config_and_inputs_for_common()

    __a			:  Optional[int]		    =  _config_zero_init(snake_case_       )
    for model_class in self.all_model_classes:
     __a			:  str		    =  model_class(config=snake_case_       )
     for name, param in model.named_parameters():
      if "embeddings" not in name and param.requires_grad:
       self.assertIn(
           ((param.data.mean() * 1E9).round() / 1E9).item() ,    [0.0, 1.0] ,    msg=f"Parameter {name} of model {model_class} seems not properly initialized" ,    )





@require_vision
@require_torch
class 		UpperCamelCase__  (	unittest.TestCase   ):
   @cached_property
   def 				lowerCAmelCase      (self			: str       ):
    # TODO update organization
    return AutoImageProcessor.from_pretrained('''microsoft/focalnet-tiny'''       ) if is_vision_available() else None







   @slow
   def 				lowerCAmelCase      (self			: str       ):
    __a			:  int		    =  FocalNetForImageClassification.from_pretrained('''microsoft/focalnet-tiny'''       ).to(snake_case_       )
    __a			:  Optional[Any]		    =  self.default_image_processor

    __a			:  Dict		    =  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
    __a			:  Optional[Any]		    =  image_processor(images=snake_case_ ,    return_tensors='''pt'''       ).to(snake_case_       )

    # forward pass
    with torch.no_grad():
     __a			:  Any		    =  model(**snake_case_       )

    # verify the logits
    __a			:  List[Any]		    =  torch.Size((1, 1_0_0_0)       )
    self.assertEqual(outputs.logits.shape ,    snake_case_       )
    __a			:  Union[str, Any]		    =  torch.tensor([0.2166, -0.4368, 0.2191]       ).to(snake_case_       )
    self.assertTrue(torch.allclose(outputs.logits[0, :3] ,    snake_case_ ,    atol=1E-4       )       )
    self.assertTrue(outputs.logits.argmax(dim=-1       ).item() ,    2_8_1       )





@require_torch
class 		UpperCamelCase__  (	__lowercase   ,unittest.TestCase   ):
   _SCREAMING_SNAKE_CASE				:							Tuple     	=				(FocalNetBackbone,) if is_torch_available() else ()
   _SCREAMING_SNAKE_CASE				:							int     	=				FocalNetConfig

   _SCREAMING_SNAKE_CASE				:							Any     	=				False
   def 				lowerCAmelCase      (self			: Tuple       ):
    __a			:  Union[str, Any]		    =  FocalNetModelTester(self       )
