



"""simple docstring"""




from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


_lowerCAmelCase							:   List[str]				=				logging.get_logger(__name__)

_lowerCAmelCase							:   List[Any]				=				{
    "EleutherAI/gpt-j-6B": "https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}






class       UpperCAmelCase_    (							_UpperCamelCase		):
      __SCREAMING_SNAKE_CASE		:  str						=					'gptj'
      __SCREAMING_SNAKE_CASE		:  List[Any]						=					{
          'max_position_embeddings': 'n_positions',
          'hidden_size': 'n_embd',
          'num_attention_heads': 'n_head',
          'num_hidden_layers': 'n_layer',
      }



      def __init__(	self	:      Dict	,      A	:      Any=5_0_4_0_0	,      A	:      Optional[Any]=2_0_4_8	,      A	:      List[Any]=4_0_9_6	,      A	:      Any=2_8	,      A	:      str=1_6	,      A	:      Optional[int]=6_4	,      A	:      Any=None	,      A	:      str="gelu_new"	,      A	:      List[Any]=0.0	,      A	:      Union[str, Any]=0.0	,      A	:      int=0.0	,      A	:      List[str]=1e-5	,      A	:      str=0.02	,      A	:      Optional[Any]=True	,      A	:      Optional[int]=5_0_2_5_6	,      A	:      Any=5_0_2_5_6	,      A	:      str=False	,      **A	:      int	,      ):
           _UpperCAmelCase     :      Dict														=       vocab_size
           _UpperCAmelCase     :      List[Any]														=       n_positions
           _UpperCAmelCase     :      List[str]														=       n_embd
           _UpperCAmelCase     :      str														=       n_layer
           _UpperCAmelCase     :      Optional[Any]														=       n_head
           _UpperCAmelCase     :      Dict														=       n_inner
           _UpperCAmelCase     :      Optional[int]														=       rotary_dim
           _UpperCAmelCase     :      Tuple														=       activation_function
           _UpperCAmelCase     :      int														=       resid_pdrop
           _UpperCAmelCase     :      Any														=       embd_pdrop
           _UpperCAmelCase     :      List[str]														=       attn_pdrop
           _UpperCAmelCase     :      Dict														=       layer_norm_epsilon
           _UpperCAmelCase     :      int														=       initializer_range
           _UpperCAmelCase     :      Tuple														=       use_cache

           _UpperCAmelCase     :      Optional[int]														=       bos_token_id
           _UpperCAmelCase     :      Any														=       eos_token_id

           super().__init__(
               bos_token_id=A	,      eos_token_id=A	,      tie_word_embeddings=A	,      **A		)







class       UpperCAmelCase_    (							_UpperCamelCase		):



      def __init__(	self	:      Any	,      A	:      PretrainedConfig	,      A	:      str = "default"	,      A	:      List[PatchingSpec] = None	,      A	:      bool = False	,      ):
           super().__init__(A	,      task=A	,      patching_specs=A	,      use_past=A		)
           if not getattr(self._config	,      "pad_token_id"	,      A		):
                # TODO: how to do that better?
                _UpperCAmelCase     :      Tuple														=       0



      @property
      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase     :      Union[str, Any]														=       OrderedDict({"input_ids": {0: "batch", 1: "sequence"}}		)
           if self.use_past:
                self.fill_with_past_key_values_(A	,      direction="inputs"		)
                _UpperCAmelCase     :      Union[str, Any]														=       {0: "batch", 1: "past_sequence + sequence"}
           else:
                _UpperCAmelCase     :      List[str]														=       {0: "batch", 1: "sequence"}

           return common_inputs



      @property
      def 		snake_case_   (	self	:      List[str]		):
           return self._config.n_layer



      @property
      def 		snake_case_   (	self	:      Union[str, Any]		):
           return self._config.n_head



      def 		snake_case_   (	self	:      Any	,      A	:      PreTrainedTokenizer	,      A	:      int = -1	,      A	:      int = -1	,      A	:      bool = False	,      A	:      Optional[TensorType] = None	,      ):
           _UpperCAmelCase     :      Optional[int]														=       super(A	,      self		).generate_dummy_inputs(
               A	,      batch_size=A	,      seq_length=A	,      is_pair=A	,      framework=A		)

           # We need to order the input in the way they appears in the forward()
           _UpperCAmelCase     :      Union[str, Any]														=       OrderedDict({"input_ids": common_inputs["input_ids"]}		)

           # Need to add the past_keys
           if self.use_past:
                if not is_torch_available():
                     raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."		)
                else:
                     import torch

                     _UpperCAmelCase							,			_UpperCAmelCase     :      Tuple														=       common_inputs["input_ids"].shape
                     # Not using the same length for past_key_values
                     _UpperCAmelCase     :      str														=       seqlen + 2
                     _UpperCAmelCase     :      str														=       (
                         batch,
                         self.num_attention_heads,
                         past_key_values_length,
                         self._config.hidden_size // self.num_attention_heads,
                     )
                     _UpperCAmelCase     :      List[str]														=       [
                         (torch.zeros(A		), torch.zeros(A		)) for _ in range(self.num_layers		)
                     ]

           _UpperCAmelCase     :      Union[str, Any]														=       common_inputs["attention_mask"]
           if self.use_past:
                _UpperCAmelCase     :      Any														=       ordered_inputs["attention_mask"].dtype
                _UpperCAmelCase     :      List[str]														=       torch.cat(
                    [ordered_inputs["attention_mask"], torch.ones(A	,      A	,      dtype=A		)]	,      dim=1		)

           return ordered_inputs



      @property
      def 		snake_case_   (	self	:      Union[str, Any]		):
           return 1_3





"""simple docstring"""




import unittest

from transformers import is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
 from PIL import Image
else:






 class       UpperCAmelCase_    :



       @staticmethod
       def 		snake_case_   (	*A	:      Union[str, Any]	,      **A	:      Dict		):
            pass






@is_pipeline_test
@require_vision
class       UpperCAmelCase_    (							unittest.TestCase		):



      @require_torch
      def 		snake_case_   (	self	:      Union[str, Any]		):
           _UpperCAmelCase     :      Union[str, Any]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      )
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Tuple														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
           # python and torch versions.
           self.assertIn(
               nested_simplify(A		)	,      [
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}],
                   [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "c"}, {"score": 0.333, "label": "b"}],
               ]	,      )

           _UpperCAmelCase     :      str														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[str]														=       pipeline(
               model="hf-internal-testing/tiny-random-clip-zero-shot-image-classification"	,      framework="tf"		)
           _UpperCAmelCase     :      int														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["a", "b", "c"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [{"score": 0.333, "label": "a"}, {"score": 0.333, "label": "b"}, {"score": 0.333, "label": "c"}]	,      )

           _UpperCAmelCase     :      Optional[Any]														=       image_classifier([image] * 5	,      candidate_labels=["A", "B", "C"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
                   [
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                       {"score": 0.333, "label": ANY(A		)},
                   ],
               ]	,      )



      @slow
      @require_torch
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      Optional[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      )
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Dict														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      List[Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)

           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Tuple														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )



      @slow
      @require_tf
      def 		snake_case_   (	self	:      Any		):
           _UpperCAmelCase     :      List[Any]														=       pipeline(
               task="zero-shot-image-classification"	,      model="openai/clip-vit-base-patch32"	,      framework="tf"		)
           # This is an image of 2 cats with remotes and no planes
           _UpperCAmelCase     :      Union[str, Any]														=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier(A	,      candidate_labels=["cat", "plane", "remote"]		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   {"score": 0.511, "label": "remote"},
                   {"score": 0.485, "label": "cat"},
                   {"score": 0.004, "label": "plane"},
               ]	,      )

           _UpperCAmelCase     :      Union[str, Any]														=       image_classifier([image] * 5	,      candidate_labels=["cat", "plane", "remote"]	,      batch_size=2		)
           self.assertEqual(
               nested_simplify(A		)	,      [
                   [
                       {"score": 0.511, "label": "remote"},
                       {"score": 0.485, "label": "cat"},
                       {"score": 0.004, "label": "plane"},
                   ],
               ]
               * 5	,      )
