


import itertools
import json
import os
import unittest

from transformers import AddedToken, LongformerTokenizer, LongformerTokenizerFast
from transformers.models.longformer.tokenization_longformer import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class      UpperCamelCase					(		lowercase__						,      unittest.TestCase     ):


   '''simple docstring'''







   lowercase					:	int									=LongformerTokenizer
   lowercase					:	Tuple									=True
   lowercase					:	Any									=LongformerTokenizerFast
   lowercase					:	List[Any]									=True
   def 		UpperCamelCase  (     self		):
        super().setUp()

        # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
        lowercase_       :Optional[int]     =       [
            '''l''',
            '''o''',
            '''w''',
            '''e''',
            '''r''',
            '''s''',
            '''t''',
            '''i''',
            '''d''',
            '''n''',
            '''\u0120''',
            '''\u0120l''',
            '''\u0120n''',
            '''\u0120lo''',
            '''\u0120low''',
            '''er''',
            '''\u0120lowest''',
            '''\u0120newer''',
            '''\u0120wider''',
            '''<unk>''',
        ]
        lowercase_       :Tuple     =       dict(zip(__UpperCamelCase   ,							range(len(__UpperCamelCase		)		)		)		)
        lowercase_       :str     =       ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
        lowercase_       :List[Any]     =       {'''unk_token''': '''<unk>'''}

        lowercase_       :Tuple     =       os.path.join(self.tmpdirname   ,							VOCAB_FILES_NAMES['''vocab_file''']		)
        lowercase_       :Tuple     =       os.path.join(self.tmpdirname   ,							VOCAB_FILES_NAMES['''merges_file''']		)
        with open(self.vocab_file   ,							'''w'''   ,							encoding='''utf-8'''		) as fp:
             fp.write(json.dumps(__UpperCamelCase		) + '''\n'''		)
        with open(self.merges_file   ,							'''w'''   ,							encoding='''utf-8'''		) as fp:
             fp.write('''\n'''.join(__UpperCamelCase		)		)
   def 		UpperCamelCase  (     self   ,							**UpperCamelCase_		):
        kwargs.update(self.special_tokens_map		)
        return self.tokenizer_class.from_pretrained(self.tmpdirname   ,							**__UpperCamelCase		)
   def 		UpperCamelCase  (     self   ,							**UpperCamelCase_		):
        kwargs.update(self.special_tokens_map		)
        return self.rust_tokenizer_class.from_pretrained(self.tmpdirname   ,							**__UpperCamelCase		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_		):
        lowercase_       :Optional[int]     =       '''lower newer'''
        lowercase_       :Tuple     =       '''lower newer'''
        return input_text, output_text
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[Any]     =       self.tokenizer_class(self.vocab_file   ,							self.merges_file   ,							**self.special_tokens_map		)
        lowercase_       :Tuple     =       '''lower newer'''
        lowercase_       :Optional[Any]     =       ['''l''', '''o''', '''w''', '''er''', '''\u0120''', '''n''', '''e''', '''w''', '''er''']
        lowercase_       :Tuple     =       tokenizer.tokenize(__UpperCamelCase		)  # , add_prefix_space=True)
        self.assertListEqual(__UpperCamelCase   ,							__UpperCamelCase		)

        lowercase_       :Optional[int]     =       tokens + [tokenizer.unk_token]
        lowercase_       :str     =       [0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCamelCase		)   ,							__UpperCamelCase		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       self.get_tokenizer()

        self.assertListEqual(tokenizer.encode('''Hello world!'''   ,							add_special_tokens=__UpperCamelCase		)   ,							[0, 3_1414, 232, 328, 2]		)
        self.assertListEqual(
            tokenizer.encode('''Hello world! cécé herlolip 418'''   ,							add_special_tokens=__UpperCamelCase		)   ,							[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]   ,							)
   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       self.tokenizer_class.from_pretrained('''allenai/longformer-base-4096'''		)

        lowercase_       :Optional[Any]     =       tokenizer.encode('''sequence builders'''   ,							add_special_tokens=__UpperCamelCase		)
        lowercase_       :Tuple     =       tokenizer.encode('''multi-sequence build'''   ,							add_special_tokens=__UpperCamelCase		)

        lowercase_       :Dict     =       tokenizer.encode(
            '''sequence builders'''   ,							add_special_tokens=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase		)
        lowercase_       :List[str]     =       tokenizer.encode(
            '''sequence builders'''   ,							'''multi-sequence build'''   ,							add_special_tokens=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase		)

        lowercase_       :int     =       tokenizer.build_inputs_with_special_tokens(__UpperCamelCase		)
        lowercase_       :List[str]     =       tokenizer.build_inputs_with_special_tokens(__UpperCamelCase   ,							__UpperCamelCase		)

        assert encoded_sentence == encoded_text_from_decode
        assert encoded_pair == encoded_pair_from_decode
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[Any]     =       self.get_tokenizer()

        lowercase_       :int     =       '''Encode this sequence.'''
        lowercase_       :List[Any]     =       tokenizer.byte_encoder[''' '''.encode('''utf-8'''		)[0]]

        # Testing encoder arguments
        lowercase_       :List[str]     =       tokenizer.encode(__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase		)
        lowercase_       :int     =       tokenizer.convert_ids_to_tokens(encoded[0]		)[0]
        self.assertNotEqual(__UpperCamelCase   ,							__UpperCamelCase		)

        lowercase_       :str     =       tokenizer.encode(__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase		)
        lowercase_       :List[Any]     =       tokenizer.convert_ids_to_tokens(encoded[0]		)[0]
        self.assertEqual(__UpperCamelCase   ,							__UpperCamelCase		)

        tokenizer.add_special_tokens({'''bos_token''': '''<s>'''}		)
        lowercase_       :str     =       tokenizer.encode(__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
        lowercase_       :Union[str, Any]     =       tokenizer.convert_ids_to_tokens(encoded[1]		)[0]
        self.assertNotEqual(__UpperCamelCase   ,							__UpperCamelCase		)

        # Testing spaces after special tokens
        lowercase_       :List[str]     =       '''<mask>'''
        tokenizer.add_special_tokens(
            {'''mask_token''': AddedToken(__UpperCamelCase   ,							lstrip=__UpperCamelCase   ,							rstrip=__UpperCamelCase		)}		)  # mask token has a left space
        lowercase_       :Union[str, Any]     =       tokenizer.convert_tokens_to_ids(__UpperCamelCase		)

        lowercase_       :str     =       '''Encode <mask> sequence'''
        lowercase_       :List[str]     =       '''Encode <mask>sequence'''

        lowercase_       :str     =       tokenizer.encode(__UpperCamelCase		)
        lowercase_       :List[Any]     =       encoded.index(__UpperCamelCase		)
        lowercase_       :Tuple     =       tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]		)[0]
        self.assertEqual(__UpperCamelCase   ,							__UpperCamelCase		)

        lowercase_       :str     =       tokenizer.encode(__UpperCamelCase		)
        lowercase_       :Tuple     =       encoded.index(__UpperCamelCase		)
        lowercase_       :Any     =       tokenizer.convert_ids_to_tokens(encoded[mask_loc + 1]		)[0]
        self.assertNotEqual(__UpperCamelCase   ,							__UpperCamelCase		)
   def 		UpperCamelCase  (     self		):
        pass
   def 		UpperCamelCase  (     self		):
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
             with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"		):
                  lowercase_       :Dict     =       self.rust_tokenizer_class.from_pretrained(__UpperCamelCase   ,							**__UpperCamelCase		)
                  lowercase_       :Optional[int]     =       self.tokenizer_class.from_pretrained(__UpperCamelCase   ,							**__UpperCamelCase		)
                  lowercase_       :Union[str, Any]     =       '''A, <mask> AllenNLP sentence.'''
                  lowercase_       :List[Any]     =       tokenizer_r.encode_plus(__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase   ,							return_token_type_ids=__UpperCamelCase		)
                  lowercase_       :List[str]     =       tokenizer_p.encode_plus(__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase   ,							return_token_type_ids=__UpperCamelCase		)

                  # token_type_ids should put 0 everywhere
                  self.assertEqual(sum(tokens_r['''token_type_ids''']		)   ,							sum(tokens_p['''token_type_ids''']		)		)

                  # attention_mask should put 1 everywhere, so sum over length should be 1
                  self.assertEqual(
                      sum(tokens_r['''attention_mask''']		) / len(tokens_r['''attention_mask''']		)   ,							sum(tokens_p['''attention_mask''']		) / len(tokens_p['''attention_mask''']		)   ,							)

                  lowercase_       :Tuple     =       tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids''']		)
                  lowercase_       :Dict     =       tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids''']		)

                  # Rust correctly handles the space before the mask while python doesnt
                  self.assertSequenceEqual(tokens_p['''input_ids''']   ,							[0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2]		)
                  self.assertSequenceEqual(tokens_r['''input_ids''']   ,							[0, 250, 6, 5_0264, 3823, 487, 2_1992, 3645, 4, 2]		)

                  self.assertSequenceEqual(
                      __UpperCamelCase   ,							['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>''']		)
                  self.assertSequenceEqual(
                      __UpperCamelCase   ,							['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>''']		)
   def 		UpperCamelCase  (     self		):
        for trim_offsets, add_prefix_space in itertools.product([True, False]   ,							repeat=2		):
             lowercase_       :Union[str, Any]     =       self.rust_tokenizer_class.from_pretrained(
                 self.tmpdirname   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)

             lowercase_       :Union[str, Any]     =       json.loads(tokenizer_r.backend_tokenizer.pre_tokenizer.__getstate__()		)
             lowercase_       :Dict     =       json.loads(tokenizer_r.backend_tokenizer.post_processor.__getstate__()		)

             self.assertEqual(pre_tokenizer_state['''add_prefix_space''']   ,							__UpperCamelCase		)

             self.assertEqual(post_processor_state['''add_prefix_space''']   ,							__UpperCamelCase		)
             self.assertEqual(post_processor_state['''trim_offsets''']   ,							__UpperCamelCase		)




   def 		UpperCamelCase  (     self		):
        # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space` and
        # `trim_offsets`
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
             with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"		):
                  lowercase_       :Optional[Any]     =       '''hello'''  # `hello` is a token in the vocabulary of `pretrained_name`
                  lowercase_       :int     =       f"{text_of_1_token} {text_of_1_token}"

                  lowercase_       :Dict     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :List[Any]     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(0, len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(len(__UpperCamelCase		) + 1, len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)

                  lowercase_       :str     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :Tuple     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(0, len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(len(__UpperCamelCase		) + 1, len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)

                  lowercase_       :int     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :Union[str, Any]     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(0, len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(len(__UpperCamelCase		), len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)

                  lowercase_       :List[Any]     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :int     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(0, len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(len(__UpperCamelCase		), len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)

                  lowercase_       :Tuple     =       f" {text}"

                  # tokenizer_r = self.rust_tokenizer_class.from_pretrained(
                  #     pretrained_name, use_fast=True, add_prefix_space=True, trim_offsets=True
                  # )
                  # encoding = tokenizer_r(text, return_offsets_mapping=True, add_special_tokens=False)
                  # self.assertEqual(encoding.offset_mapping[0], (1, 1 + len(text_of_1_token)))
                  # self.assertEqual(
                  #     encoding.offset_mapping[1],
                  #     (1 + len(text_of_1_token) + 1, 1 + len(text_of_1_token) + 1 + len(text_of_1_token)),
                  # )

                  lowercase_       :List[str]     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :Any     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(1, 1 + len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(1 + len(__UpperCamelCase		) + 1, 1 + len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)

                  lowercase_       :Optional[Any]     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :Any     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(0, 1 + len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(1 + len(__UpperCamelCase		), 1 + len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)

                  lowercase_       :Dict     =       self.rust_tokenizer_class.from_pretrained(
                      __UpperCamelCase   ,							use_fast=__UpperCamelCase   ,							add_prefix_space=__UpperCamelCase   ,							trim_offsets=__UpperCamelCase		)
                  lowercase_       :List[Any]     =       tokenizer_r(__UpperCamelCase   ,							return_offsets_mapping=__UpperCamelCase   ,							add_special_tokens=__UpperCamelCase		)
                  self.assertEqual(encoding.offset_mapping[0]   ,							(0, 1 + len(__UpperCamelCase		))		)
                  self.assertEqual(
                      encoding.offset_mapping[1]   ,							(1 + len(__UpperCamelCase		), 1 + len(__UpperCamelCase		) + 1 + len(__UpperCamelCase		))   ,							)





import inspect
import unittest

from transformers import YolosConfig
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_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch
   from torch import nn

   from transformers import YolosForObjectDetection, YolosModel
   from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor






class      UpperCamelCase					:


   '''simple docstring'''







   def __init__(     self   ,							UpperCamelCase_   ,							UpperCamelCase_=13   ,							UpperCamelCase_=[30, 30]   ,							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_=3   ,							UpperCamelCase_=None   ,							UpperCamelCase_=8   ,							UpperCamelCase_=10   ,							):
        lowercase_       :Tuple     =       parent
        lowercase_       :Dict     =       batch_size
        lowercase_       :Optional[Any]     =       image_size
        lowercase_       :Optional[int]     =       patch_size
        lowercase_       :List[str]     =       num_channels
        lowercase_       :Dict     =       is_training
        lowercase_       :int     =       use_labels
        lowercase_       :Any     =       hidden_size
        lowercase_       :Optional[Any]     =       num_hidden_layers
        lowercase_       :Any     =       num_attention_heads
        lowercase_       :int     =       intermediate_size
        lowercase_       :List[str]     =       hidden_act
        lowercase_       :int     =       hidden_dropout_prob
        lowercase_       :Optional[Any]     =       attention_probs_dropout_prob
        lowercase_       :Any     =       type_sequence_label_size
        lowercase_       :Union[str, Any]     =       initializer_range
        lowercase_       :Tuple     =       num_labels
        lowercase_       :Dict     =       scope
        lowercase_       :int     =       n_targets
        lowercase_       :int     =       num_detection_tokens
        # we set the expected sequence length (which is used in several tests)
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
        lowercase_       :int     =       (image_size[1] // patch_size) * (image_size[0] // patch_size)
        lowercase_       :int     =       num_patches + 1 + self.num_detection_tokens
   def 		UpperCamelCase  (     self		):
        lowercase_       :Union[str, Any]     =       floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]		)

        lowercase_       :Optional[int]     =       None
        if self.use_labels:
             # labels is a list of Dict (each Dict being the labels for a given example in the batch)
             lowercase_       :Any     =       []
             for i in range(self.batch_size		):
                  lowercase_       :Dict     =       {}
                  lowercase_       :Optional[Any]     =       torch.randint(
                      high=self.num_labels   ,							size=(self.n_targets,)   ,							device=UpperCamelCase_		)
                  lowercase_       :str     =       torch.rand(self.n_targets   ,							4   ,							device=UpperCamelCase_		)
                  labels.append(UpperCamelCase_		)

        lowercase_       :str     =       self.get_config()

        return config, pixel_values, labels
   def 		UpperCamelCase  (     self		):
        return YolosConfig(
            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   ,							num_detection_tokens=self.num_detection_tokens   ,							num_labels=self.num_labels   ,							)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :str     =       YolosModel(config=UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()
        lowercase_       :str     =       model(UpperCamelCase_		)
        self.parent.assertEqual(
            result.last_hidden_state.shape   ,							(self.batch_size, self.expected_seq_len, self.hidden_size)		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Optional[int]     =       YolosForObjectDetection(UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()

        lowercase_       :Dict     =       model(pixel_values=UpperCamelCase_		)
        lowercase_       :Tuple     =       model(UpperCamelCase_		)

        self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_detection_tokens, self.num_labels + 1)		)
        self.parent.assertEqual(result.pred_boxes.shape   ,							(self.batch_size, self.num_detection_tokens, 4)		)

        lowercase_       :Optional[Any]     =       model(pixel_values=UpperCamelCase_   ,							labels=UpperCamelCase_		)

        self.parent.assertEqual(result.loss.shape   ,							()		)
        self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.num_detection_tokens, self.num_labels + 1)		)
        self.parent.assertEqual(result.pred_boxes.shape   ,							(self.batch_size, self.num_detection_tokens, 4)		)




   def 		UpperCamelCase  (     self		):
        lowercase_       :Union[str, Any]     =       self.prepare_config_and_inputs()
        lowercase_    ,      lowercase_    ,      lowercase_       :Tuple     =       config_and_inputs
        lowercase_       :int     =       {'''pixel_values''': pixel_values}
        return config, inputs_dict






@require_torch
class      UpperCamelCase					(		lowercase__						,      lowercase__						,      unittest.TestCase     ):


   '''simple docstring'''







   lowercase					:	Tuple									=(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
   lowercase					:	Union[str, Any]									=(
       {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
   )

   lowercase					:	Optional[int]									=False
   lowercase					:	Optional[int]									=False
   lowercase					:	Dict									=False
   lowercase					:	List[str]									=False
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_=False		):
        lowercase_       :Optional[Any]     =       super()._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_   ,							return_labels=UpperCamelCase_		)

        if return_labels:
             if model_class.__name__ == "YolosForObjectDetection":
                  lowercase_       :Dict     =       []
                  for i in range(self.model_tester.batch_size		):
                       lowercase_       :Optional[int]     =       {}
                       lowercase_       :Tuple     =       torch.ones(
                           size=(self.model_tester.n_targets,)   ,							device=UpperCamelCase_   ,							dtype=torch.long		)
                       lowercase_       :Union[str, Any]     =       torch.ones(
                           self.model_tester.n_targets   ,							4   ,							device=UpperCamelCase_   ,							dtype=torch.float		)
                       labels.append(UpperCamelCase_		)
                  lowercase_       :Optional[int]     =       labels

        return inputs_dict
   def 		UpperCamelCase  (     self		):
        lowercase_       :Union[str, Any]     =       YolosModelTester(self		)
        lowercase_       :List[Any]     =       ConfigTester(self   ,							config_class=UpperCamelCase_   ,							has_text_modality=UpperCamelCase_   ,							hidden_size=37		)
   def 		UpperCamelCase  (     self		):
        self.config_tester.run_common_tests()
   def 		UpperCamelCase  (     self		):
        # YOLOS does not use inputs_embeds
        pass
   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :List[Any]     =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_       :Optional[Any]     =       model_class(UpperCamelCase_		)
             self.assertIsInstance(model.get_input_embeddings()   ,							(nn.Module)		)
             lowercase_       :int     =       model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase_   ,							nn.Linear		)		)
   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :Dict     =       self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase_       :int     =       ['''pixel_values''']
             self.assertListEqual(arg_names[:1]   ,							UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :int     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_    ,      lowercase_       :str     =       self.model_tester.prepare_config_and_inputs_for_common()
        lowercase_       :Tuple     =       True

        # in YOLOS, the seq_len is different
        lowercase_       :Optional[Any]     =       self.model_tester.expected_seq_len
        for model_class in self.all_model_classes:
             lowercase_       :Any     =       True
             lowercase_       :List[str]     =       False
             lowercase_       :List[str]     =       True
             lowercase_       :Any     =       model_class(UpperCamelCase_		)
             model.to(UpperCamelCase_		)
             model.eval()
             with torch.no_grad():
                  lowercase_       :str     =       model(**self._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_		)		)
             lowercase_       :str     =       outputs.attentions
             self.assertEqual(len(UpperCamelCase_		)   ,							self.model_tester.num_hidden_layers		)

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             lowercase_       :Union[str, Any]     =       True
             lowercase_       :int     =       model_class(UpperCamelCase_		)
             model.to(UpperCamelCase_		)
             model.eval()
             with torch.no_grad():
                  lowercase_       :List[Any]     =       model(**self._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_		)		)
             lowercase_       :int     =       outputs.attentions
             self.assertEqual(len(UpperCamelCase_		)   ,							self.model_tester.num_hidden_layers		)

             self.assertListEqual(
                 list(attentions[0].shape[-3:]		)   ,							[self.model_tester.num_attention_heads, seq_len, seq_len]   ,							)
             lowercase_       :str     =       len(UpperCamelCase_		)

             # Check attention is always last and order is fine
             lowercase_       :List[str]     =       True
             lowercase_       :Tuple     =       True
             lowercase_       :List[Any]     =       model_class(UpperCamelCase_		)
             model.to(UpperCamelCase_		)
             model.eval()
             with torch.no_grad():
                  lowercase_       :Any     =       model(**self._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_		)		)

             lowercase_       :List[str]     =       1
             self.assertEqual(out_len + added_hidden_states   ,							len(UpperCamelCase_		)		)

             lowercase_       :List[Any]     =       outputs.attentions

             self.assertEqual(len(UpperCamelCase_		)   ,							self.model_tester.num_hidden_layers		)
             self.assertListEqual(
                 list(self_attentions[0].shape[-3:]		)   ,							[self.model_tester.num_attention_heads, seq_len, seq_len]   ,							)
   def 		UpperCamelCase  (     self		):
        def check_hidden_states_output(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
             lowercase_       :List[Any]     =       model_class(UpperCamelCase_		)
             model.to(UpperCamelCase_		)
             model.eval()

             with torch.no_grad():
                  lowercase_       :Optional[Any]     =       model(**self._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_		)		)

             lowercase_       :str     =       outputs.hidden_states

             lowercase_       :str     =       getattr(
                 self.model_tester   ,							'''expected_num_hidden_layers'''   ,							self.model_tester.num_hidden_layers + 1		)
             self.assertEqual(len(UpperCamelCase_		)   ,							UpperCamelCase_		)

             # YOLOS has a different seq_length
             lowercase_       :Tuple     =       self.model_tester.expected_seq_len

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

        lowercase_    ,      lowercase_       :int     =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_       :Optional[Any]     =       True
             check_hidden_states_output(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		)

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             lowercase_       :Dict     =       True

             check_hidden_states_output(UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[int]     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_object_detection(*UpperCamelCase_		)




   @slow
   def 		UpperCamelCase  (     self		):
        for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_       :Dict     =       YolosModel.from_pretrained(UpperCamelCase_		)
             self.assertIsNotNone(UpperCamelCase_		)




def     UpperCamelCase    (      )					->							List[Any]:
     '''simple docstring'''





     lowercase_       :List[str]     =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''		)
     return image






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


   '''simple docstring'''







   @cached_property
   def 		UpperCamelCase  (     self		):
        return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''		) if is_vision_available() else None




   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :Any     =       YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''		).to(UpperCamelCase_		)

        lowercase_       :Tuple     =       self.default_image_processor
        lowercase_       :str     =       prepare_img()
        lowercase_       :List[str]     =       image_processor(images=UpperCamelCase_   ,							return_tensors='''pt'''		).to(UpperCamelCase_		)

        # forward pass
        with torch.no_grad():
             lowercase_       :Optional[int]     =       model(inputs.pixel_values		)

        # verify outputs
        lowercase_       :List[str]     =       torch.Size((1, 100, 92)		)
        self.assertEqual(outputs.logits.shape   ,							UpperCamelCase_		)

        lowercase_       :int     =       torch.tensor(
            [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]   ,							device=UpperCamelCase_   ,							)
        lowercase_       :Tuple     =       torch.tensor(
            [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]   ,							device=UpperCamelCase_		)
        self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]   ,							UpperCamelCase_   ,							atol=1E-4		)		)
        self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]   ,							UpperCamelCase_   ,							atol=1E-4		)		)

        # verify postprocessing
        lowercase_       :List[str]     =       image_processor.post_process_object_detection(
            UpperCamelCase_   ,							threshold=0.3   ,							target_sizes=[image.size[::-1]]		)[0]
        lowercase_       :str     =       torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]		).to(UpperCamelCase_		)
        lowercase_       :Optional[int]     =       [75, 75, 17, 63, 17]
        lowercase_       :List[str]     =       torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]		).to(UpperCamelCase_		)

        self.assertEqual(len(results['''scores''']		)   ,							5		)
        self.assertTrue(torch.allclose(results['''scores''']   ,							UpperCamelCase_   ,							atol=1E-4		)		)
        self.assertSequenceEqual(results['''labels'''].tolist()   ,							UpperCamelCase_		)
        self.assertTrue(torch.allclose(results['''boxes'''][0, :]   ,							UpperCamelCase_		)		)

