



import inspect
import unittest

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

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 ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
  from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image



class  __lowerCAmelCase     :




 def __init__(						self	,				__UpperCAmelCase	,				__UpperCAmelCase=13	,				__UpperCAmelCase=64	,				__UpperCAmelCase=2	,				__UpperCAmelCase=3	,				__UpperCAmelCase=True	,				__UpperCAmelCase=True	,				__UpperCAmelCase=32	,				__UpperCAmelCase=5	,				__UpperCAmelCase=4	,				__UpperCAmelCase=37	,				__UpperCAmelCase="gelu"	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=0.1	,				__UpperCAmelCase=10	,				__UpperCAmelCase=0.02	,				__UpperCAmelCase=[1, 16, 4, 4]	,				__UpperCAmelCase=None	,				):



    '''simple docstring'''
    __lowerCamelCase													=    parent
    __lowerCamelCase													=    batch_size
    __lowerCamelCase													=    image_size
    __lowerCamelCase													=    patch_size
    __lowerCamelCase													=    num_channels
    __lowerCamelCase													=    is_training
    __lowerCamelCase													=    use_labels
    __lowerCamelCase													=    hidden_size
    __lowerCamelCase													=    num_hidden_layers
    __lowerCamelCase													=    num_attention_heads
    __lowerCamelCase													=    intermediate_size
    __lowerCamelCase													=    hidden_act
    __lowerCamelCase													=    hidden_dropout_prob
    __lowerCamelCase													=    attention_probs_dropout_prob
    __lowerCamelCase													=    type_sequence_label_size
    __lowerCamelCase													=    initializer_range
    __lowerCamelCase													=    scope
    __lowerCamelCase													=    backbone_featmap_shape

    # in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
    # the number of patches is based on the feature map of the backbone, which by default uses an output stride
    # of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
    __lowerCamelCase													=    (self.image_size // 32) ** 2
    __lowerCamelCase													=    num_patches + 1




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

    __lowerCamelCase													=    None
    if self.use_labels:
       __lowerCamelCase													=    ids_tensor([self.batch_size]	,				self.type_sequence_label_size				)

    __lowerCamelCase													=    self.get_config()

    return config, pixel_values, labels




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    {
        '''global_padding''': '''same''',
        '''layer_type''': '''bottleneck''',
        '''depths''': [3, 4, 9],
        '''out_features''': ['''stage1''', '''stage2''', '''stage3'''],
        '''embedding_dynamic_padding''': True,
        '''hidden_sizes''': [4, 8, 16, 32],
        '''num_groups''': 2,
    }

    return ViTHybridConfig(
        image_size=self.image_size	,				patch_size=self.patch_size	,				num_channels=self.num_channels	,				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	,				is_decoder=__UpperCAmelCase	,				initializer_range=self.initializer_range	,				backbone_featmap_shape=self.backbone_featmap_shape	,				backbone_config=__UpperCAmelCase	,				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    ViTHybridModel(config=__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)
    model.eval()
    __lowerCamelCase													=    model(__UpperCAmelCase				)
    self.parent.assertEqual(result.last_hidden_state.shape	,				(self.batch_size, self.seq_length, self.hidden_size)				)




 def        lowerCamelCase      (						self	,				__UpperCAmelCase	,				__UpperCAmelCase	,				__UpperCAmelCase				):



    '''simple docstring'''
    __lowerCamelCase													=    self.type_sequence_label_size
    __lowerCamelCase													=    ViTHybridForImageClassification(__UpperCAmelCase				)
    model.to(__UpperCAmelCase				)
    model.eval()
    __lowerCamelCase													=    model(__UpperCAmelCase	,				labels=__UpperCAmelCase				)
    self.parent.assertEqual(result.logits.shape	,				(self.batch_size, self.type_sequence_label_size)				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.prepare_config_and_inputs()
    __lowerCamelCase							,__lowerCamelCase							,__lowerCamelCase													=    config_and_inputs
    __lowerCamelCase													=    {'''pixel_values''': pixel_values}
    return config, inputs_dict



@require_torch
class  __lowerCAmelCase     (	lowerCAmelCase__  ,	lowerCAmelCase__  ,	unittest.TestCase   ):
 lowerCAmelCase__       					=  (ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
 lowerCAmelCase__       					=  (
     {"""feature-extraction""": ViTHybridModel, """image-classification""": ViTHybridForImageClassification}
     if is_torch_available()
     else {}
 )
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False
 lowerCAmelCase__       					=  False




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    ViTHybridModelTester(self				)
    __lowerCamelCase													=    ConfigTester(self	,				config_class=__UpperCAmelCase	,				has_text_modality=__UpperCAmelCase	,				hidden_size=37				)




 def        lowerCamelCase      (						self				):



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




 @unittest.skip(reason='''ViT does not use inputs_embeds'''				)
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       self.assertIsInstance(model.get_input_embeddings()	,				(nn.Module)				)
       __lowerCamelCase													=    model.get_output_embeddings()
       self.assertTrue(x is None or isinstance(__UpperCAmelCase	,				nn.Linear				)				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       __lowerCamelCase													=    model_class(__UpperCAmelCase				)
       __lowerCamelCase													=    inspect.signature(model.forward				)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       __lowerCamelCase													=    [*signature.parameters.keys()]

       __lowerCamelCase													=    ['''pixel_values''']
       self.assertListEqual(arg_names[:1]	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase							,__lowerCamelCase													=    self.model_tester.prepare_config_and_inputs_for_common()

    __lowerCamelCase													=    _config_zero_init(__UpperCAmelCase				)
    for model_class in self.all_model_classes:
       __lowerCamelCase													=    model_class(config=__UpperCAmelCase				)
       # Skip the check for the backbone
       for name, module in model.named_modules():
          if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
             __lowerCamelCase													=    [F"""{name}.{key}""" for key in module.state_dict().keys()]
             break

       for name, param in model.named_parameters():
          if param.requires_grad:
             if name in backbone_params:
                continue
             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"""	,				)




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __lowerCamelCase													=    ViTHybridModel.from_pretrained(__UpperCAmelCase				)
       self.assertIsNotNone(__UpperCAmelCase				)





def 		a__			(		):
   __lowerCamelCase													=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
   return image





@require_torch
@require_vision
class  __lowerCAmelCase     (	unittest.TestCase   ):




 @cached_property
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    return (
        ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]				)
        if is_vision_available()
        else None
    )




 @slow
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]				).to(
        __UpperCAmelCase				)

    __lowerCamelCase													=    self.default_image_processor
    __lowerCamelCase													=    prepare_img()
    __lowerCamelCase													=    image_processor(images=__UpperCAmelCase	,				return_tensors='''pt'''				).to(__UpperCAmelCase				)

    # forward pass
    with torch.no_grad():
       __lowerCamelCase													=    model(**__UpperCAmelCase				)

    # verify the logits
    __lowerCamelCase													=    torch.Size((1, 1000)				)
    self.assertEqual(outputs.logits.shape	,				__UpperCAmelCase				)

    __lowerCamelCase													=    torch.tensor([-1.9_090, -0.4_993, -0.2_389]				).to(__UpperCAmelCase				)

    self.assertTrue(torch.allclose(outputs.logits[0, :3]	,				__UpperCAmelCase	,				atol=1E-4				)				)




 @slow
 @require_accelerate
 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''				)
    __lowerCamelCase													=    ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''	,				device_map='''auto'''				)

    __lowerCamelCase													=    prepare_img()

    __lowerCamelCase													=    image_processor(images=__UpperCAmelCase	,				return_tensors='''pt'''				)
    __lowerCamelCase													=    model(**__UpperCAmelCase				)
    __lowerCamelCase													=    outputs.logits
    # model predicts one of the 1000 ImageNet classes
    __lowerCamelCase													=    logits.argmax(-1				).item()

    self.assertTrue(model.config.idalabel[predicted_class_idx]	,				'''tabby, tabby cat'''				)











import unittest

from transformers import RoFormerTokenizer, RoFormerTokenizerFast
from transformers.testing_utils import require_rjieba, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_rjieba
@require_tokenizers
class  __lowerCAmelCase     (	lowerCAmelCase__  ,	unittest.TestCase   ):
 lowerCAmelCase__       					=  RoFormerTokenizer
 lowerCAmelCase__       					=  RoFormerTokenizerFast
 lowerCAmelCase__       					=  True
 lowerCAmelCase__       					=  True




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    super().setUp()




 def        lowerCamelCase      (						self	,				**__UpperCAmelCase				):



    '''simple docstring'''
    return self.tokenizer_class.from_pretrained('''junnyu/roformer_chinese_base'''	,				**__UpperCAmelCase				)




 def        lowerCamelCase      (						self	,				**__UpperCAmelCase				):



    '''simple docstring'''
    return self.rust_tokenizer_class.from_pretrained('''junnyu/roformer_chinese_base'''	,				**__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    '''永和服装饰品有限公司,今天天气非常好'''
    __lowerCamelCase													=    '''永和 服装 饰品 有限公司 , 今 天 天 气 非常 好'''
    return input_text, output_text




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.get_tokenizer()
    __lowerCamelCase							,__lowerCamelCase													=    self.get_chinese_input_output_texts()
    __lowerCamelCase													=    tokenizer.tokenize(__UpperCAmelCase				)

    self.assertListEqual(__UpperCAmelCase	,				output_text.split()				)

    __lowerCamelCase													=    tokens + [tokenizer.unk_token]
    __lowerCamelCase													=    [22943, 21332, 34431, 45904, 117, 306, 1231, 1231, 2653, 33994, 1266, 100]
    self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase				)	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    __lowerCamelCase													=    self.get_rust_tokenizer()
    __lowerCamelCase							,__lowerCamelCase													=    self.get_chinese_input_output_texts()
    __lowerCamelCase													=    tokenizer.tokenize(__UpperCAmelCase				)
    self.assertListEqual(__UpperCAmelCase	,				output_text.split()				)
    __lowerCamelCase													=    tokens + [tokenizer.unk_token]
    __lowerCamelCase													=    [22943, 21332, 34431, 45904, 117, 306, 1231, 1231, 2653, 33994, 1266, 100]
    self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase				)	,				__UpperCAmelCase				)




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass




 def        lowerCamelCase      (						self				):



    '''simple docstring'''
    pass






