


import argparse
import re

from flax.traverse_util import flatten_dict, unflatten_dict
from tax import checkpoints

from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration
from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model
from transformers.utils import logging


logging.set_verbosity_info()


# should not include what is already done by the `from_pt` argument
SCREAMING_SNAKE_CASE :   Dict							      ={
    '/attention/': '/0/SelfAttention/',
    '/self_attention/': '/0/SelfAttention/',
    '/encoder_decoder_attention/': '/1/EncDecAttention/',
    'value': 'v',
    'query': 'q',
    'key': 'k',
    'out': 'o',
    'pre_self_attention_layer_norm': '0/layer_norm',
    'pre_cross_attention_layer_norm': '1/layer_norm',
    'pre_attention_layer_norm': '0/layer_norm',  # previously 1, but seems wrong
    'token_embedder': 'shared',
    'encoder_norm': 'final_layer_norm',
    'decoder_norm': 'final_layer_norm',
    'relpos_bias/rel_embedding': 'block/0/layer/0/SelfAttention/relative_attention_bias/weight',
    'router/router_weights/w/': 'router/classifier/',
    'roer/roer_weights/w/': 'router/classifier/',
    'logits_dense': 'lm_head',
}



def        UpperCamelCase_(   lowerCamelCase_				)					->		Any:
    _lowercase :							int   	=	list(s_dict.keys()				)
    for key in keys:
        _lowercase :							List[Any]   	=	r".*/layers_(\d+)"
        _lowercase :							int   	=	key
        if re.match(_lowerCAmelCase							,				_lowerCAmelCase				):
            _lowercase :							Optional[Any]   	=	re.sub(R'layers_(\d+)'							,				R'block/\1/layer'							,				_lowerCAmelCase				)

        _lowercase :							str   	=	r"(encoder|decoder)\/"

        if re.match(_lowerCAmelCase							,				_lowerCAmelCase				):
            _lowercase :							Optional[int]   	=	re.match(_lowerCAmelCase							,				_lowerCAmelCase				).groups()
            if groups[0] == "encoder":
                _lowercase :							Tuple   	=	re.sub(R'/mlp/'							,				R'/1/mlp/'							,				_lowerCAmelCase				)
                _lowercase :							Any   	=	re.sub(R'/pre_mlp_layer_norm/'							,				R'/1/layer_norm/'							,				_lowerCAmelCase				)

            elif groups[0] == "decoder":
                _lowercase :							int   	=	re.sub(R'/mlp/'							,				R'/2/mlp/'							,				_lowerCAmelCase				)
                _lowercase :							int   	=	re.sub(R'/pre_mlp_layer_norm/'							,				R'/2/layer_norm/'							,				_lowerCAmelCase				)

        # 2. Convert other classic mappings
        for old_key, temp_key in MOE_LAYER_NAME_MAPPING.items():
            if old_key in new_key:
                _lowercase :							Optional[int]   	=	new_key.replace(_lowerCAmelCase							,				_lowerCAmelCase				)

        print(F'''{key} -> {new_key}'''				)
        _lowercase :							Any   	=	s_dict.pop(_lowerCAmelCase				)

    if "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict:
        _lowercase :							Dict   	=	s_dict[
            "encoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight"
        ].T
    if "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight" in s_dict:
        _lowercase :							Dict   	=	s_dict[
            "decoder/block/0/layer/0/SelfAttention/relative_attention_bias/weight"
        ].T

    # 3. Take extra care of the EXPERTS layer
    for key in list(s_dict.keys()				):
        if "expert" in key:
            _lowercase :							List[str]   	=	s_dict[key].shape[0]
            _lowercase :							Optional[Any]   	=	s_dict[key]
            for idx in range(_lowerCAmelCase				):
                _lowercase :							int   	=	expert_weihts[idx]
                print(F'''{key} -> {key.replace("expert/"							,				"nested fstring"				)}'''				)

            s_dict.pop(_lowerCAmelCase				)

    return s_dict


SCREAMING_SNAKE_CASE :   Any							      ={
    'NUM_ENCODER_LAYERS': 'num_layers',
    'NUM_DECODER_LAYERS': 'num_decoder_layers',
    'NUM_HEADS': 'num_heads',
    'HEAD_DIM': 'd_kv',
    'EMBED_DIM': 'd_model',
    'MLP_DIM': 'd_ff',
    'NUM_SELECTED_EXPERTS': 'num_selected_experts',
    'NUM_ENCODER_SPARSE_LAYERS': 'num_sparse_encoder_layers',
    'NUM_DECODER_SPARSE_LAYERS': 'num_sparse_decoder_layers',
    'dense.MlpBlock.activations': 'feed_forward_proj',
}



def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_				)					->		Any:
    import regex as re

    with open(_lowerCAmelCase							,				'r'				) as f:
        _lowercase :							int   	=	f.read()

    _lowercase :							Any   	=	re.findall(R'(.*) = ([0-9.]*)'							,				_lowerCAmelCase				)
    _lowercase :							str   	=	{}
    for param, value in regex_match:
        if param in GIN_TO_CONFIG_MAPPING and value != "":
            _lowercase :							List[Any]   	=	float(_lowerCAmelCase				) if "." in value else int(_lowerCAmelCase				)

    _lowercase :							List[str]   	=	re.findall(R'(.*activations) = \(\'(.*)\',\)'							,				_lowerCAmelCase				)[0]
    _lowercase :							Union[str, Any]   	=	str(activation[1]				)

    _lowercase :							Any   	=	num_experts
    _lowercase :							str   	=	SwitchTransformersConfig(**_lowerCAmelCase				)
    return config



def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_							,				lowerCamelCase_=None							,				lowerCamelCase_="./"							,				lowerCamelCase_=8				)					->		Optional[Any]:
    print(F'''Loading flax weights from : {flax_checkpoint_path}'''				)
    _lowercase :							str   	=	checkpoints.load_tax_checkpoint(_lowerCAmelCase				)

    if gin_file is not None:
        _lowercase :							List[Any]   	=	convert_gin_to_config(_lowerCAmelCase							,				_lowerCAmelCase				)
    else:
        _lowercase :							Dict   	=	SwitchTransformersConfig.from_pretrained(_lowerCAmelCase				)

    _lowercase :							int   	=	SwitchTransformersForConditionalGeneration(_lowerCAmelCase				)

    _lowercase :							List[Any]   	=	flax_params["target"]
    _lowercase :							Tuple   	=	flatten_dict(_lowerCAmelCase							,				sep='/'				)
    _lowercase :							Optional[Any]   	=	rename_keys(_lowerCAmelCase				)
    _lowercase :							int   	=	unflatten_dict(_lowerCAmelCase							,				sep='/'				)

    # Load the flax params in the PT model
    load_flax_weights_in_pytorch_model(_lowerCAmelCase							,				_lowerCAmelCase				)

    print(F'''Save PyTorch model to {pytorch_dump_path}'''				)
    pt_model.save_pretrained(_lowerCAmelCase				)


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE :   str							      =argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--switch_t5x_checkpoint_path",
     default=None,
     type=str,
     required=True,
     help=(
         "The config json file corresponding to the pre-trained SwitchTransformers model. \nThis specifies the"
         " model architecture. If not provided, a `gin_file` has to be provided."
     ),
 )
 parser.add_argument(
     "--gin_file",
     default=None,
     type=str,
     required=False,
     help="Path to the gin config file. If not provided, a `config_file` has to be passed   ",
 )
 parser.add_argument(
     "--config_name", default=None, type=str, required=False, help="Config name of SwitchTransformers model."
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output pytorch model."
 )
 parser.add_argument("--num_experts", default=8, type=int, required=False, help="Number of experts")
 SCREAMING_SNAKE_CASE :   int							      =parser.parse_args()
 convert_flax_checkpoint_to_pytorch(
     args.switch_tax_checkpoint_path,
     args.config_name,
     args.gin_file,
     args.pytorch_dump_folder_path,
     args.num_experts,
 )






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 _lowerCamelCase:
       def __init__(      self,   lowerCamelCase,   lowerCamelCase=13,   lowerCamelCase=[30, 30],   lowerCamelCase=2,   lowerCamelCase=3,   lowerCamelCase=True,   lowerCamelCase=True,   lowerCamelCase=32,   lowerCamelCase=5,   lowerCamelCase=4,   lowerCamelCase=37,   lowerCamelCase="gelu",   lowerCamelCase=0.1,   lowerCamelCase=0.1,   lowerCamelCase=10,   lowerCamelCase=0.0_2,   lowerCamelCase=3,   lowerCamelCase=None,   lowerCamelCase=8,   lowerCamelCase=10,   ) ->     Union[str, Any]:




           """simple docstring"""

           _lowercase :							Optional[Any]   	=	parent
           _lowercase :							int   	=	batch_size
           _lowercase :							str   	=	image_size
           _lowercase :							Any   	=	patch_size
           _lowercase :							Optional[Any]   	=	num_channels
           _lowercase :							Union[str, Any]   	=	is_training
           _lowercase :							Dict   	=	use_labels
           _lowercase :							Optional[Any]   	=	hidden_size
           _lowercase :							Optional[int]   	=	num_hidden_layers
           _lowercase :							List[str]   	=	num_attention_heads
           _lowercase :							Optional[Any]   	=	intermediate_size
           _lowercase :							Tuple   	=	hidden_act
           _lowercase :							Union[str, Any]   	=	hidden_dropout_prob
           _lowercase :							str   	=	attention_probs_dropout_prob
           _lowercase :							int   	=	type_sequence_label_size
           _lowercase :							str   	=	initializer_range
           _lowercase :							Tuple   	=	num_labels
           _lowercase :							Any   	=	scope
           _lowercase :							Optional[Any]   	=	n_targets
           _lowercase :							List[Any]   	=	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 :							Tuple   	=	(image_size[1] // patch_size) * (image_size[0] // patch_size)
           _lowercase :							str   	=	num_patches + 1 + self.num_detection_tokens
       def     UpperCamelCase  (      self) ->     Union[str, Any]:




           """simple docstring"""

           _lowercase :							List[str]   	=	floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]])

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

           _lowercase :							Tuple   	=	self.get_config()

           return config, pixel_values, labels
       def     UpperCamelCase  (      self) ->     List[Any]:




           """simple docstring"""

           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=lowerCamelCase,   initializer_range=self.initializer_range,   num_detection_tokens=self.num_detection_tokens,   num_labels=self.num_labels,   )
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Tuple:




           """simple docstring"""

           _lowercase :							Dict   	=	YolosModel(config=lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()
           _lowercase :							Optional[int]   	=	model(lowerCamelCase)
           self.parent.assertEqual(
               result.last_hidden_state.shape,   (self.batch_size, self.expected_seq_len, self.hidden_size))
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase) ->     Optional[Any]:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	YolosForObjectDetection(lowerCamelCase)
           model.to(lowerCamelCase)
           model.eval()

           _lowercase :							List[Any]   	=	model(pixel_values=lowerCamelCase)
           _lowercase :							Union[str, Any]   	=	model(lowerCamelCase)

           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 :							Tuple   	=	model(pixel_values=lowerCamelCase,   labels=lowerCamelCase)

           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) ->     Any:




           """simple docstring"""

           _lowercase :							int   	=	self.prepare_config_and_inputs()
           _lowercase	,       _lowercase	,       _lowercase :							Dict   	=	config_and_inputs
           _lowercase :							Dict   	=	{'pixel_values': pixel_values}
           return config, inputs_dict






@require_torch
class _lowerCamelCase(		_a,				_a,				unittest.TestCase     ):
       lowercase_				:			int									=    (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
       lowercase_				:			Optional[Any]									=    (
           {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
       )

       lowercase_				:			Tuple									=    False
       lowercase_				:			Optional[Any]									=    False
       lowercase_				:			Tuple									=    False
       lowercase_				:			Optional[Any]									=    False
       def     UpperCamelCase  (      self,   lowerCamelCase,   lowerCamelCase,   lowerCamelCase=False) ->     str:




           """simple docstring"""

           _lowercase :							List[Any]   	=	super()._prepare_for_class(lowerCamelCase,   lowerCamelCase,   return_labels=lowerCamelCase)

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

           return inputs_dict
       def     UpperCamelCase  (      self) ->     Tuple:




           """simple docstring"""

           _lowercase :							str   	=	YolosModelTester(self)
           _lowercase :							int   	=	ConfigTester(self,   config_class=lowerCamelCase,   has_text_modality=lowerCamelCase,   hidden_size=37)
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           self.config_tester.run_common_tests()
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           pass
       def     UpperCamelCase  (      self) ->     List[str]:




           """simple docstring"""

           _lowercase	,       _lowercase :							Union[str, Any]   	=	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               _lowercase :							Union[str, Any]   	=	model_class(lowerCamelCase)
               self.assertIsInstance(model.get_input_embeddings(),   (nn.Module))
               _lowercase :							Tuple   	=	model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowerCamelCase,   nn.Linear))
       def     UpperCamelCase  (      self) ->     Tuple:




           """simple docstring"""

           _lowercase	,       _lowercase :							str   	=	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               _lowercase :							Optional[int]   	=	model_class(lowerCamelCase)
               _lowercase :							Optional[Any]   	=	inspect.signature(model.forward)
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               _lowercase :							Union[str, Any]   	=	[*signature.parameters.keys()]

               _lowercase :							Any   	=	['pixel_values']
               self.assertListEqual(arg_names[:1],   lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Any:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowerCamelCase)
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase	,       _lowercase :							Tuple   	=	self.model_tester.prepare_config_and_inputs_for_common()
           _lowercase :							List[str]   	=	True

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

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

               self.assertListEqual(
                   list(attentions[0].shape[-3:]),   [self.model_tester.num_attention_heads, seq_len, seq_len],   )
               _lowercase :							Optional[Any]   	=	len(lowerCamelCase)

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

               _lowercase :							Dict   	=	1
               self.assertEqual(out_len + added_hidden_states,   len(lowerCamelCase))

               _lowercase :							Any   	=	outputs.attentions

               self.assertEqual(len(lowerCamelCase),   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) ->     Optional[int]:




           """simple docstring"""

           def check_hidden_states_output(lowerCamelCase,   lowerCamelCase,   lowerCamelCase):
               _lowercase :							Tuple   	=	model_class(lowerCamelCase)
               model.to(lowerCamelCase)
               model.eval()

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

               _lowercase :							int   	=	outputs.hidden_states

               _lowercase :							Dict   	=	getattr(
                   self.model_tester,   'expected_num_hidden_layers',   self.model_tester.num_hidden_layers + 1)
               self.assertEqual(len(lowerCamelCase),   lowerCamelCase)

               # YOLOS has a different seq_length
               _lowercase :							List[str]   	=	self.model_tester.expected_seq_len

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

           _lowercase	,       _lowercase :							Dict   	=	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               _lowercase :							Any   	=	True
               check_hidden_states_output(lowerCamelCase,   lowerCamelCase,   lowerCamelCase)

               # check that output_hidden_states also work using config
               del inputs_dict["output_hidden_states"]
               _lowercase :							Union[str, Any]   	=	True

               check_hidden_states_output(lowerCamelCase,   lowerCamelCase,   lowerCamelCase)
       def     UpperCamelCase  (      self) ->     Dict:




           """simple docstring"""

           _lowercase :							Optional[int]   	=	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_object_detection(*lowerCamelCase)




       @slow
       def     UpperCamelCase  (      self) ->     Dict:




           """simple docstring"""

           for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _lowercase :							Optional[Any]   	=	YolosModel.from_pretrained(lowerCamelCase)
               self.assertIsNotNone(lowerCamelCase)



def        UpperCamelCase_(   )					->		List[str]:
    _lowercase :							List[Any]   	=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
    return image






@require_torch
@require_vision
class _lowerCamelCase(		unittest.TestCase     ):
       @cached_property
       def     UpperCamelCase  (      self) ->     Dict:




           """simple docstring"""

           return AutoImageProcessor.from_pretrained('hustvl/yolos-small') if is_vision_available() else None




       @slow
       def     UpperCamelCase  (      self) ->     int:




           """simple docstring"""

           _lowercase :							List[str]   	=	YolosForObjectDetection.from_pretrained('hustvl/yolos-small').to(lowerCamelCase)

           _lowercase :							int   	=	self.default_image_processor
           _lowercase :							List[Any]   	=	prepare_img()
           _lowercase :							str   	=	image_processor(images=lowerCamelCase,   return_tensors='pt').to(lowerCamelCase)

           # forward pass
           with torch.no_grad():
               _lowercase :							str   	=	model(inputs.pixel_values)

           # verify outputs
           _lowercase :							Optional[int]   	=	torch.Size((1, 1_00, 92))
           self.assertEqual(outputs.logits.shape,   lowerCamelCase)

           _lowercase :							Tuple   	=	torch.tensor(
               [[-2_4.0_2_4_8, -1_0.3_0_2_4, -1_4.8_2_9_0], [-4_2.0_3_9_2, -1_6.8_2_0_0, -2_7.4_3_3_4], [-2_7.2_7_4_3, -1_1.8_1_5_4, -1_8.7_1_4_8]],   device=lowerCamelCase,   )
           _lowercase :							Dict   	=	torch.tensor(
               [[0.2_5_5_9, 0.5_4_5_5, 0.4_7_0_6], [0.2_9_8_9, 0.7_2_7_9, 0.1_8_7_5], [0.7_7_3_2, 0.4_0_1_7, 0.4_4_6_2]],   device=lowerCamelCase)
           self.assertTrue(torch.allclose(outputs.logits[0, :3, :3],   lowerCamelCase,   atol=1E-4))
           self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3],   lowerCamelCase,   atol=1E-4))

           # verify postprocessing
           _lowercase :							str   	=	image_processor.post_process_object_detection(
               lowerCamelCase,   threshold=0.3,   target_sizes=[image.size[::-1]])[0]
           _lowercase :							Union[str, Any]   	=	torch.tensor([0.9_9_9_4, 0.9_7_9_0, 0.9_9_6_4, 0.9_9_7_2, 0.9_8_6_1]).to(lowerCamelCase)
           _lowercase :							Optional[Any]   	=	[75, 75, 17, 63, 17]
           _lowercase :							Union[str, Any]   	=	torch.tensor([3_3_5.0_6_0_9, 7_9.3_8_4_8, 3_7_5.4_2_1_6, 1_8_7.2_4_9_5]).to(lowerCamelCase)

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


