


'''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


a_   :      Union[str, Any]				    =      logging.get_logger(__name__)


a_   :      int				    =      {
    "Salesforce/codegen-350M-nl": "https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json",
    "Salesforce/codegen-350M-multi": "https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json",
    "Salesforce/codegen-350M-mono": "https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json",
    "Salesforce/codegen-2B-nl": "https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json",
    "Salesforce/codegen-2B-multi": "https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json",
    "Salesforce/codegen-2B-mono": "https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json",
    "Salesforce/codegen-6B-nl": "https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json",
    "Salesforce/codegen-6B-multi": "https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json",
    "Salesforce/codegen-6B-mono": "https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json",
    "Salesforce/codegen-16B-nl": "https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json",
    "Salesforce/codegen-16B-multi": "https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json",
    "Salesforce/codegen-16B-mono": "https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json",
}




class 	a       (       _UpperCAmelCase    ):
 _lowerCAmelCase	   =		"codegen"
 _lowerCAmelCase	   =		{
     "max_position_embeddings": "n_positions",
     "hidden_size": "n_embd",
     "num_attention_heads": "n_head",
     "num_hidden_layers": "n_layer",
 }
 def __init__(	self ,			__magic_name__=5_04_00 ,			__magic_name__=20_48 ,			__magic_name__=20_48 ,			__magic_name__=40_96 ,			__magic_name__=28 ,			__magic_name__=16 ,			__magic_name__=64 ,			__magic_name__=None ,			__magic_name__="gelu_new" ,			__magic_name__=0.0 ,			__magic_name__=0.0 ,			__magic_name__=0.0 ,			__magic_name__=1e-5 ,			__magic_name__=0.0_2 ,			__magic_name__=True ,			__magic_name__=5_02_56 ,			__magic_name__=5_02_56 ,			__magic_name__=False ,			**__magic_name__ ,			)   ->							Optional[Any]:
      _a      =							vocab_size
      _a      =							n_ctx
      _a      =							n_positions
      _a      =							n_embd
      _a      =							n_layer
      _a      =							n_head
      _a      =							n_inner
      _a      =							rotary_dim
      _a      =							activation_function
      _a      =							resid_pdrop
      _a      =							embd_pdrop
      _a      =							attn_pdrop
      _a      =							layer_norm_epsilon
      _a      =							initializer_range
      _a      =							use_cache

      _a      =							bos_token_id
      _a      =							eos_token_id

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





class 	a       (       _UpperCAmelCase    ):
 def __init__(	self ,			__magic_name__ ,			__magic_name__ = "default" ,			__magic_name__ = None ,			__magic_name__ = False ,			)   ->							Dict:
      super().__init__(_UpperCAmelCase ,			task=_UpperCAmelCase ,			patching_specs=_UpperCAmelCase ,			use_past=_UpperCAmelCase  )
      if not getattr(self._config ,			'pad_token_id' ,			_UpperCAmelCase  ):
           # TODO: how to do that better?
           _a      =							0
 @property
 def 							__UpperCAmelCase						(	self  )   ->							List[Any]:
      _a      =							OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}  )
      if self.use_past:
           self.fill_with_past_key_values_(_UpperCAmelCase ,			direction='inputs'  )
           _a      =							{0: '''batch''', 1: '''past_sequence + sequence'''}
      else:
           _a      =							{0: '''batch''', 1: '''sequence'''}

      return common_inputs
 @property
 def 							__UpperCAmelCase						(	self  )   ->							List[Any]:
      return self._config.n_layer
 @property
 def 							__UpperCAmelCase						(	self  )   ->							int:
      return self._config.n_head
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ = -1 ,			__magic_name__ = -1 ,			__magic_name__ = False ,			__magic_name__ = None ,			)   ->							Any:
      _a      =							super(_UpperCAmelCase ,			self  ).generate_dummy_inputs(
          _UpperCAmelCase ,			batch_size=_UpperCAmelCase ,			seq_length=_UpperCAmelCase ,			is_pair=_UpperCAmelCase ,			framework=_UpperCAmelCase  )

      # We need to order the input in the way they appears in the forward()
      _a      =							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

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

      _a      =							common_inputs['''attention_mask''']
      if self.use_past:
           _a      =							ordered_inputs['''attention_mask'''].dtype
           _a      =							torch.cat(
               [ordered_inputs['attention_mask'], torch.ones(_UpperCAmelCase ,			_UpperCAmelCase ,			dtype=_UpperCAmelCase  )] ,			dim=1  )

      return ordered_inputs
 @property
 def 							__UpperCAmelCase						(	self  )   ->							int:
      return 13





'''simple docstring'''






import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoImageProcessor, ViTImageProcessor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / "utils"))

from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


a_   :      Tuple				    =      get_tests_dir("fixtures")




class 	a       (       unittest.TestCase    ):
 def 							__UpperCAmelCase						(	self  )   ->							str:
      # A mock response for an HTTP head request to emulate server down
      _a      =							mock.Mock()
      _a      =							5_00
      _a      =							{}
      _a      =							HTTPError
      _a      =							{}

      # Download this model to make sure it's in the cache.
      _a      =							ViTImageProcessor.from_pretrained('hf-internal-testing/tiny-random-vit'  )
      # Under the mock environment we get a 500 error when trying to reach the model.
      with mock.patch('requests.Session.request' ,			return_value=__magic_name__  ) as mock_head:
           _a      =							ViTImageProcessor.from_pretrained('hf-internal-testing/tiny-random-vit'  )
           # This check we did call the fake head request
           mock_head.assert_called()
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      # This test is for deprecated behavior and can be removed in v5
      _a      =							ViTImageProcessor.from_pretrained(
          'https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json'  )




 def 							__UpperCAmelCase						(	self  )   ->							Dict:
      with self.assertRaises(__magic_name__  ):
           # config is in subfolder, the following should not work without specifying the subfolder
           _a      =							AutoImageProcessor.from_pretrained('hf-internal-testing/stable-diffusion-all-variants'  )

      _a      =							AutoImageProcessor.from_pretrained(
          'hf-internal-testing/stable-diffusion-all-variants' ,			subfolder='feature_extractor'  )

      self.assertIsNotNone(__magic_name__  )





@is_staging_test
class 	a       (       unittest.TestCase    ):
 @classmethod
 def 							__UpperCAmelCase						(	cls  )   ->							Dict:
      _a      =							TOKEN
      HfFolder.save_token(__magic_name__  )
 @classmethod
 def 							__UpperCAmelCase						(	cls  )   ->							List[Any]:
      try:
           delete_repo(token=cls._token ,			repo_id='test-image-processor'  )
      except HTTPError:
           pass

      try:
           delete_repo(token=cls._token ,			repo_id='valid_org/test-image-processor-org'  )
      except HTTPError:
           pass

      try:
           delete_repo(token=cls._token ,			repo_id='test-dynamic-image-processor'  )
      except HTTPError:
           pass
 def 							__UpperCAmelCase						(	self  )   ->							str:
      _a      =							ViTImageProcessor.from_pretrained(__magic_name__  )
      image_processor.push_to_hub('test-image-processor' ,			use_auth_token=self._token  )

      _a      =							ViTImageProcessor.from_pretrained(f'{USER}/test-image-processor'  )
      for k, v in image_processor.__dict__.items():
           self.assertEqual(__magic_name__ ,			getattr(__magic_name__ ,			__magic_name__  )  )

      # Reset repo
      delete_repo(token=self._token ,			repo_id='test-image-processor'  )

      # Push to hub via save_pretrained
      with tempfile.TemporaryDirectory() as tmp_dir:
           image_processor.save_pretrained(
               __magic_name__ ,			repo_id='test-image-processor' ,			push_to_hub=__magic_name__ ,			use_auth_token=self._token  )

      _a      =							ViTImageProcessor.from_pretrained(f'{USER}/test-image-processor'  )
      for k, v in image_processor.__dict__.items():
           self.assertEqual(__magic_name__ ,			getattr(__magic_name__ ,			__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							Optional[Any]:
      _a      =							ViTImageProcessor.from_pretrained(__magic_name__  )
      image_processor.push_to_hub('valid_org/test-image-processor' ,			use_auth_token=self._token  )

      _a      =							ViTImageProcessor.from_pretrained('valid_org/test-image-processor'  )
      for k, v in image_processor.__dict__.items():
           self.assertEqual(__magic_name__ ,			getattr(__magic_name__ ,			__magic_name__  )  )

      # Reset repo
      delete_repo(token=self._token ,			repo_id='valid_org/test-image-processor'  )

      # Push to hub via save_pretrained
      with tempfile.TemporaryDirectory() as tmp_dir:
           image_processor.save_pretrained(
               __magic_name__ ,			repo_id='valid_org/test-image-processor-org' ,			push_to_hub=__magic_name__ ,			use_auth_token=self._token  )

      _a      =							ViTImageProcessor.from_pretrained('valid_org/test-image-processor-org'  )
      for k, v in image_processor.__dict__.items():
           self.assertEqual(__magic_name__ ,			getattr(__magic_name__ ,			__magic_name__  )  )




 def 							__UpperCAmelCase						(	self  )   ->							Any:
      CustomImageProcessor.register_for_auto_class()
      _a      =							CustomImageProcessor.from_pretrained(__magic_name__  )

      image_processor.push_to_hub('test-dynamic-image-processor' ,			use_auth_token=self._token  )

      # This has added the proper auto_map field to the config
      self.assertDictEqual(
          image_processor.auto_map ,			{'AutoImageProcessor': 'custom_image_processing.CustomImageProcessor'} ,			)

      _a      =							AutoImageProcessor.from_pretrained(
          f'{USER}/test-dynamic-image-processor' ,			trust_remote_code=__magic_name__  )
      # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
      self.assertEqual(new_image_processor.__class__.__name__ ,			'CustomImageProcessor'  )

