

'''simple docstring'''







import unittest

import numpy as np
import requests

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

      from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
      __lowerCamelCase									=     False

if is_vision_available():
      from PIL import Image

      from transformers import PixaStructImageProcessor


class        A__   (   unittest.TestCase      ):



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__=7		,					UpperCamelCase__=3		,					UpperCamelCase__=18		,					UpperCamelCase__=30		,					UpperCamelCase__=400		,					UpperCamelCase__=None		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__=None		,					)    ->   List[str]:





           '''simple docstring'''
           A_						       =   size if size is not None else {"""height""": 20, """width""": 20}
           A_						       =   parent
           A_						       =   batch_size
           A_						       =   num_channels
           A_						       =   image_size
           A_						       =   min_resolution
           A_						       =   max_resolution
           A_						       =   size
           A_						       =   do_normalize
           A_						       =   do_convert_rgb
           A_						       =   [512, 1024, 2048, 4096]
           A_						       =   patch_size if patch_size is not None else {"""height""": 16, """width""": 16}



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}







    def        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           A_						       =   """https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg"""
           A_						       =   Image.open(requests.get(UpperCamelCase__		,					stream=UpperCamelCase__							).raw							).convert("""RGB"""							)
           return raw_image


@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11    ,    reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."    ,    )
@require_torch
@require_vision
class        A__   (   _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		PixaStructImageProcessor if is_vision_available() else None



    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           A_						       =   PixaStructImageProcessingTester(self							)



    @property
    def        snake_case_      (       self							)    ->   Tuple:





           '''simple docstring'''
           return self.image_processor_tester.prepare_image_processor_dict()



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           self.assertTrue(hasattr(UpperCamelCase__		,					"""do_normalize"""							)							)
           self.assertTrue(hasattr(UpperCamelCase__		,					"""do_convert_rgb"""							)							)



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           A_						       =   self.image_processor_tester.prepare_dummy_image()

           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           A_						       =   2048

           A_						       =   image_processor(UpperCamelCase__		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							)
           self.assertTrue(torch.allclose(inputs.flattened_patches.mean()		,					torch.tensor(0.0606							)		,					atol=1e-3		,					rtol=1e-3							)							)



    def        snake_case_      (       self							)    ->   Tuple:





           '''simple docstring'''
           # Initialize image_processor
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           # create random PIL images
           A_						       =   prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCamelCase__							)
           for image in image_inputs:
                  self.assertIsInstance(UpperCamelCase__		,					Image.Image							)

           # Test not batched input
           A_						       =   (
               (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
               * self.image_processor_tester.num_channels
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                  # Test not batched input
                  A_						       =   image_processor(
                      image_inputs[0]		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(1, max_patch, expected_hidden_dim)		,					)

                  # Test batched
                  A_						       =   image_processor(
                      UpperCamelCase__		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)		,					)



    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           # Initialize image_processor
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           # create random PIL images
           A_						       =   prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCamelCase__							)
           for image in image_inputs:
                  self.assertIsInstance(UpperCamelCase__		,					Image.Image							)

           # Test not batched input
           A_						       =   (
               (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
               * self.image_processor_tester.num_channels
           ) + 2

           A_						       =   True

           for max_patch in self.image_processor_tester.max_patches:
                  # Test not batched input
                  with self.assertRaises(UpperCamelCase__							):
                         A_						       =   image_processor(
                             image_inputs[0]		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches

                  A_						       =   """Hello"""

                  A_						       =   image_processor(
                      image_inputs[0]		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__		,					header_text=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(1, max_patch, expected_hidden_dim)		,					)

                  # Test batched
                  A_						       =   image_processor(
                      UpperCamelCase__		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__		,					header_text=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)		,					)



    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           # Initialize image_processor
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           # create random numpy tensors
           A_						       =   prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCamelCase__		,					numpify=UpperCamelCase__							)
           for image in image_inputs:
                  self.assertIsInstance(UpperCamelCase__		,					np.ndarray							)

           A_						       =   (
               (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
               * self.image_processor_tester.num_channels
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                  # Test not batched input
                  A_						       =   image_processor(
                      image_inputs[0]		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(1, max_patch, expected_hidden_dim)		,					)

                  # Test batched
                  A_						       =   image_processor(
                      UpperCamelCase__		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)		,					)







    def        snake_case_      (       self							)    ->   Union[str, Any]:





           '''simple docstring'''
           # Initialize image_processor
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           # create random PyTorch tensors
           A_						       =   prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCamelCase__		,					torchify=UpperCamelCase__							)
           for image in image_inputs:
                  self.assertIsInstance(UpperCamelCase__		,					torch.Tensor							)

           # Test not batched input
           A_						       =   (
               (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
               * self.image_processor_tester.num_channels
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                  # Test not batched input
                  A_						       =   image_processor(
                      image_inputs[0]		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(1, max_patch, expected_hidden_dim)		,					)

                  # Test batched
                  A_						       =   image_processor(
                      UpperCamelCase__		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)		,					)


@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11    ,    reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`."    ,    )
@require_torch
@require_vision
class        A__   (   _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		PixaStructImageProcessor if is_vision_available() else None



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   PixaStructImageProcessingTester(self		,					num_channels=4							)
           A_						       =   3



    @property
    def        snake_case_      (       self							)    ->   Optional[int]:





           '''simple docstring'''
           return self.image_processor_tester.prepare_image_processor_dict()



    def        snake_case_      (       self							)    ->   List[Any]:





           '''simple docstring'''
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           self.assertTrue(hasattr(UpperCamelCase__		,					"""do_normalize"""							)							)
           self.assertTrue(hasattr(UpperCamelCase__		,					"""do_convert_rgb"""							)							)







    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           # Initialize image_processor
           A_						       =   self.image_processing_class(**self.image_processor_dict							)
           # create random PIL images
           A_						       =   prepare_image_inputs(self.image_processor_tester		,					equal_resolution=UpperCamelCase__							)
           for image in image_inputs:
                  self.assertIsInstance(UpperCamelCase__		,					Image.Image							)

           # Test not batched input
           A_						       =   (
               (self.image_processor_tester.patch_size["""height"""] * self.image_processor_tester.patch_size["""width"""])
               * (self.image_processor_tester.num_channels - 1)
           ) + 2

           for max_patch in self.image_processor_tester.max_patches:
                  # Test not batched input
                  A_						       =   image_processor(
                      image_inputs[0]		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(1, max_patch, expected_hidden_dim)		,					)

                  # Test batched
                  A_						       =   image_processor(
                      UpperCamelCase__		,					return_tensors="""pt"""		,					max_patches=UpperCamelCase__							).flattened_patches
                  self.assertEqual(
                      encoded_images.shape		,					(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)		,					)



'''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									=     logging.get_logger(__name__)


__lowerCamelCase									=     {
    '''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__   (   _snake_case      ):
    lowercase						       =		"codegen"
    lowercase						       =		{
        "max_position_embeddings": "n_positions",
        "hidden_size": "n_embd",
        "num_attention_heads": "n_head",
        "num_hidden_layers": "n_layer",
    }



    def __init__(       self		,					UpperCamelCase__=50400		,					UpperCamelCase__=2048		,					UpperCamelCase__=2048		,					UpperCamelCase__=4096		,					UpperCamelCase__=28		,					UpperCamelCase__=16		,					UpperCamelCase__=64		,					UpperCamelCase__=None		,					UpperCamelCase__="gelu_new"		,					UpperCamelCase__=0.0		,					UpperCamelCase__=0.0		,					UpperCamelCase__=0.0		,					UpperCamelCase__=1e-5		,					UpperCamelCase__=0.02		,					UpperCamelCase__=True		,					UpperCamelCase__=50256		,					UpperCamelCase__=50256		,					UpperCamelCase__=False		,					**UpperCamelCase__		,					)    ->   Tuple:





           '''simple docstring'''
           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__   (   _snake_case      ):



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__ = "default"		,					UpperCamelCase__ = None		,					UpperCamelCase__ = False		,					)    ->   Tuple:





           '''simple docstring'''
           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        snake_case_      (       self							)    ->   Mapping[str, Mapping[int, str]]:





           '''simple docstring'''
           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        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           return self._config.n_layer



    @property
    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           return self._config.n_head



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__ = -1		,					UpperCamelCase__ = -1		,					UpperCamelCase__ = False		,					UpperCamelCase__ = None		,					)    ->   Mapping[str, Any]:





           '''simple docstring'''
           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_       ,      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        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           return 13
