





"""simple docstring"""



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 		__snake_case					:





 """simple docstring"""


 def __init__(    self					,						__lowerCamelCase					,						__lowerCamelCase=13					,						__lowerCamelCase=64					,						__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=[1, 16, 4, 4]					,						__lowerCamelCase=None					,						):



     '''simple docstring'''




     __A    :						int						 =     parent
     __A    :						List[Any]						 =     batch_size
     __A    :						Dict						 =     image_size
     __A    :						List[str]						 =     patch_size
     __A    :						Tuple						 =     num_channels
     __A    :						int						 =     is_training
     __A    :						List[Any]						 =     use_labels
     __A    :						Optional[int]						 =     hidden_size
     __A    :						Dict						 =     num_hidden_layers
     __A    :						str						 =     num_attention_heads
     __A    :						List[Any]						 =     intermediate_size
     __A    :						List[Any]						 =     hidden_act
     __A    :						Optional[int]						 =     hidden_dropout_prob
     __A    :						List[str]						 =     attention_probs_dropout_prob
     __A    :						int						 =     type_sequence_label_size
     __A    :						Optional[Any]						 =     initializer_range
     __A    :						str						 =     scope
     __A    :						int						 =     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
     __A    :						int						 =     (self.image_size // 32) ** 2
     __A    :						Optional[int]						 =     num_patches + 1

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						str						 =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

     __A    :						Optional[Any]						 =     None
     if self.use_labels:
         __A    :						Union[str, Any]						 =     ids_tensor([self.batch_size]					,						self.type_sequence_label_size  )

     __A    :						str						 =     self.get_config()

     return config, pixel_values, labels

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Optional[int]						 =     {
         '''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=__lowerCamelCase					,						initializer_range=self.initializer_range					,						backbone_featmap_shape=self.backbone_featmap_shape					,						backbone_config=__lowerCamelCase					,						)

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						List[str]						 =     ViTHybridModel(config=__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A    :						Union[str, Any]						 =     model(__lowerCamelCase  )
     self.parent.assertEqual(result.last_hidden_state.shape					,						(self.batch_size, self.seq_length, self.hidden_size)  )

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						List[Any]						 =     self.type_sequence_label_size
     __A    :						Union[str, Any]						 =     ViTHybridForImageClassification(__lowerCamelCase  )
     model.to(__lowerCamelCase  )
     model.eval()
     __A    :						Union[str, Any]						 =     model(__lowerCamelCase					,						labels=__lowerCamelCase  )
     self.parent.assertEqual(result.logits.shape					,						(self.batch_size, self.type_sequence_label_size)  )







 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Any						 =     self.prepare_config_and_inputs()
     __A ,       __A ,       __A    :						List[str]						 =     config_and_inputs
     __A    :						Tuple						 =     {'''pixel_values''': pixel_values}
     return config, inputs_dict






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





 """simple docstring"""


 _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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						int						 =     ViTHybridModelTester(self  )
     __A    :						List[Any]						 =     ConfigTester(self					,						config_class=__lowerCamelCase					,						has_text_modality=__lowerCamelCase					,						hidden_size=37  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     self.config_tester.run_common_tests()

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



     '''simple docstring'''




     pass

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A ,       __A    :						Tuple						 =     self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         __A    :						str						 =     model_class(__lowerCamelCase  )
         self.assertIsInstance(model.get_input_embeddings()					,						(nn.Module)  )
         __A    :						List[Any]						 =     model.get_output_embeddings()
         self.assertTrue(x is None or isinstance(__lowerCamelCase					,						nn.Linear  )  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

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

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

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						str						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Dict						 =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A ,       __A    :						Tuple						 =     self.model_tester.prepare_config_and_inputs_for_common()

     __A    :						Tuple						 =     _config_zero_init(__lowerCamelCase  )
     for model_class in self.all_model_classes:
         __A    :						Optional[Any]						 =     model_class(config=__lowerCamelCase  )
         # Skip the check for the backbone
         for name, module in model.named_modules():
             if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
                 __A    :						Dict						 =     [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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A    :						Optional[int]						 =     ViTHybridModel.from_pretrained(__lowerCamelCase  )
         self.assertIsNotNone(__lowerCamelCase  )







def 		__lowercase     (   )   ->Optional[int]:



    '''simple docstring'''







    __A    :						Tuple						 =     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
    return image






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





 """simple docstring"""


 @cached_property
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




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

 @slow
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Tuple						 =     ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]  ).to(
         __lowerCamelCase  )

     __A    :						List[str]						 =     self.default_image_processor
     __A    :						str						 =     prepare_img()
     __A    :						Optional[Any]						 =     image_processor(images=__lowerCamelCase					,						return_tensors='''pt'''  ).to(__lowerCamelCase  )

     # forward pass
     with torch.no_grad():
         __A    :						List[str]						 =     model(**__lowerCamelCase  )

     # verify the logits
     __A    :						Optional[int]						 =     torch.Size((1, 1000)  )
     self.assertEqual(outputs.logits.shape					,						__lowerCamelCase  )

     __A    :						Union[str, Any]						 =     torch.tensor([-1.9_0_9_0, -0.4_9_9_3, -0.2_3_8_9]  ).to(__lowerCamelCase  )

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







 @slow
 @require_accelerate
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						Dict						 =     ViTHybridImageProcessor.from_pretrained('''google/vit-hybrid-base-bit-384'''  )
     __A    :						List[Any]						 =     ViTHybridForImageClassification.from_pretrained('''google/vit-hybrid-base-bit-384'''					,						device_map='''auto'''  )

     __A    :						Any						 =     prepare_img()

     __A    :						str						 =     image_processor(images=__lowerCamelCase					,						return_tensors='''pt'''  )
     __A    :						Tuple						 =     model(**__lowerCamelCase  )
     __A    :						Tuple						 =     outputs.logits
     # model predicts one of the 1000 ImageNet classes
     __A    :						Optional[Any]						 =     logits.argmax(-1  ).item()

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











"""simple docstring"""



import uuid
from typing import Any, Dict, List, Optional, Union

from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
       import tensorflow as tf

if is_torch_available():
       import torch


a_ =			logging.get_logger(__name__)






class 		__snake_case					:





 """simple docstring"""


 def __init__(    self					,						__lowerCamelCase = None					,						__lowerCamelCase = None					,						__lowerCamelCase=None					,						__lowerCamelCase=None  ):



     '''simple docstring'''




     if not conversation_id:
         __A    :						List[Any]						 =     uuid.uuida()
     if past_user_inputs is None:
         __A    :						List[str]						 =     []
     if generated_responses is None:
         __A    :						Tuple						 =     []

     __A    :						uuid.UUID						 =     conversation_id
     __A    :						List[str]						 =     past_user_inputs
     __A    :						List[str]						 =     generated_responses
     __A    :						Optional[str]						 =     text

 def __eq__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase					,						__lowerCamelCase  ):
         return False
     if self.uuid == other.uuid:
         return True
     return (
         self.new_user_input == other.new_user_input
         and self.past_user_inputs == other.past_user_inputs
         and self.generated_responses == other.generated_responses
     )

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase = False  ):



     '''simple docstring'''




     if self.new_user_input:
         if overwrite:
             logger.warning(
                 F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" was overwritten """
                 F"""with: \"{text}\"."""  )
             __A    :						str						 =     text
         else:
             logger.warning(
                 F"""User input added while unprocessed input was existing: \"{self.new_user_input}\" new input """
                 F"""ignored: \"{text}\". Set `overwrite` to True to overwrite unprocessed user input"""  )
     else:
         __A    :						Union[str, Any]						 =     text

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     if self.new_user_input:
         self.past_user_inputs.append(self.new_user_input  )
     __A    :						List[Any]						 =     None

 def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     self.generated_responses.append(__lowerCamelCase  )

 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     for user_input, generated_response in zip(self.past_user_inputs					,						self.generated_responses  ):
         yield True, user_input
         yield False, generated_response
     if self.new_user_input:
         yield True, self.new_user_input







 def __repr__(    self  ):



     '''simple docstring'''




     __A    :						Optional[Any]						 =     F"""Conversation id: {self.uuid} \n"""
     for is_user, text in self.iter_texts():
         __A    :						Tuple						 =     '''user''' if is_user else '''bot'''
         output += F"""{name} >> {text} \n"""
     return output






@add_end_docstrings(
    SCREAMING_SNAKE_CASE__							,							R"""
        min_length_for_response (`int`, *optional*, defaults to 32):
            The minimum length (in number of tokens) for a response.
        minimum_tokens (`int`, *optional*, defaults to 10):
            The minimum length of tokens to leave for a response.
    """							,							)
class 		__snake_case					(		SCREAMING_SNAKE_CASE__     ):





 """simple docstring"""


 def __init__(    self					,						*__lowerCamelCase					,						**__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(*__lowerCamelCase					,						**__lowerCamelCase  )
     if self.tokenizer.pad_token_id is None:
         __A    :						Union[str, Any]						 =     self.tokenizer.eos_token

 def 					UpperCamelCase__(    self					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						__lowerCamelCase=None					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						str						 =     {}
     __A    :						List[str]						 =     {}
     __A    :						Any						 =     {}

     if min_length_for_response is not None:
         __A    :						int						 =     min_length_for_response
     if minimum_tokens is not None:
         __A    :						Any						 =     minimum_tokens

     if "max_length" in generate_kwargs:
         __A    :						List[Any]						 =     generate_kwargs['''max_length''']
         # self.max_length = generate_kwargs.get("max_length", self.model.config.max_length)
     if clean_up_tokenization_spaces is not None:
         __A    :						str						 =     clean_up_tokenization_spaces

     if generate_kwargs:
         forward_params.update(__lowerCamelCase  )
     return preprocess_params, forward_params, postprocess_params

 def __call__(    self					,						__lowerCamelCase					,						__lowerCamelCase=0					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Any						 =     super().__call__(__lowerCamelCase					,						num_workers=__lowerCamelCase					,						**__lowerCamelCase  )
     if isinstance(__lowerCamelCase					,						__lowerCamelCase  ) and len(__lowerCamelCase  ) == 1:
         return outputs[0]
     return outputs

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=32  ):



     '''simple docstring'''




     if not isinstance(__lowerCamelCase					,						__lowerCamelCase  ):
         raise ValueError('''ConversationalPipeline, expects Conversation as inputs'''  )
     if conversation.new_user_input is None:
         raise ValueError(
             F"""Conversation with UUID {type(conversation.uuid  )} does not contain new user input to process. """
             '''Add user inputs with the conversation\'s `add_user_input` method'''  )
     if hasattr(self.tokenizer					,						'''_build_conversation_input_ids'''  ):
         __A    :						List[Any]						 =     self.tokenizer._build_conversation_input_ids(__lowerCamelCase  )
     else:
         # If the tokenizer cannot handle conversations, we default to only the old version
         __A    :						int						 =     self._legacy_parse_and_tokenize(__lowerCamelCase  )

     if self.framework == "pt":
         __A    :						Union[str, Any]						 =     torch.LongTensor([input_ids]  )
     elif self.framework == "tf":
         __A    :						int						 =     tf.constant([input_ids]  )
     return {"input_ids": input_ids, "conversation": conversation}

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=10					,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Tuple						 =     generate_kwargs.get('''max_length'''					,						self.model.config.max_length  )

     __A    :						str						 =     model_inputs['''input_ids'''].shape[1]
     if max_length - minimum_tokens < n:
         logger.warning(F"""Conversation input is to long ({n}), trimming it to ({max_length} - {minimum_tokens})"""  )
         __A    :						str						 =     max_length - minimum_tokens
         __A    :						Any						 =     model_inputs['''input_ids'''][:, -trim:]
         if "attention_mask" in model_inputs:
             __A    :						Union[str, Any]						 =     model_inputs['''attention_mask'''][:, -trim:]
     __A    :						Dict						 =     model_inputs.pop('''conversation'''  )
     __A    :						List[str]						 =     max_length
     __A    :						Dict						 =     self.model.generate(**__lowerCamelCase					,						**__lowerCamelCase  )
     if self.model.config.is_encoder_decoder:
         __A    :						Any						 =     1
     else:
         __A    :						List[Any]						 =     n
     return {"output_ids": output_ids[:, start_position:], "conversation": conversation}

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase=True  ):



     '''simple docstring'''




     __A    :						int						 =     model_outputs['''output_ids''']
     __A    :						Optional[int]						 =     self.tokenizer.decode(
         output_ids[0]					,						skip_special_tokens=__lowerCamelCase					,						clean_up_tokenization_spaces=__lowerCamelCase					,						)
     __A    :						Dict						 =     model_outputs['''conversation''']
     conversation.mark_processed()
     conversation.append_response(__lowerCamelCase  )
     return conversation







 def 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						Tuple						 =     self.tokenizer.eos_token_id
     __A    :						List[str]						 =     []
     for is_user, text in conversation.iter_texts():
         if eos_token_id is not None:
             input_ids.extend(self.tokenizer.encode(__lowerCamelCase					,						add_special_tokens=__lowerCamelCase  ) + [eos_token_id]  )
         else:
             input_ids.extend(self.tokenizer.encode(__lowerCamelCase					,						add_special_tokens=__lowerCamelCase  )  )

     if len(__lowerCamelCase  ) > self.tokenizer.model_max_length:
         __A    :						List[str]						 =     input_ids[-self.tokenizer.model_max_length :]
     return input_ids




