



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


lowercase__					:						Any												=			logging.get_logger(__name__)


lowercase__					:						Dict												=			{
    '''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  _UpperCAmelCase (    lowerCAmelCase__):
 _lowerCAmelCase     :    Any	      =	"""codegen"""
 _lowerCAmelCase     :    Tuple	      =	{
     """max_position_embeddings""": """n_positions""",
     """hidden_size""": """n_embd""",
     """num_attention_heads""": """n_head""",
     """num_hidden_layers""": """n_layer""",
 }

 def __init__(  self	: Optional[int]						,  lowercase_	: Tuple=50400						,  lowercase_	: Any=2048						,  lowercase_	: Tuple=2048						,  lowercase_	: str=4096						,  lowercase_	: Optional[Any]=28						,  lowercase_	: Optional[int]=16						,  lowercase_	: List[Any]=64						,  lowercase_	: Any=None						,  lowercase_	: str="gelu_new"						,  lowercase_	: Optional[Any]=0.0						,  lowercase_	: str=0.0						,  lowercase_	: int=0.0						,  lowercase_	: str=1E-5						,  lowercase_	: Dict=0.02						,  lowercase_	: Optional[int]=True						,  lowercase_	: List[Any]=50256						,  lowercase_	: Dict=50256						,  lowercase_	: Union[str, Any]=False						,  **lowercase_	: Optional[Any]						,  ):
     snake_case_				:  List[str]       =			vocab_size
     snake_case_				:  List[str]       =			n_ctx
     snake_case_				:  int       =			n_positions
     snake_case_				:  Union[str, Any]       =			n_embd
     snake_case_				:  Tuple       =			n_layer
     snake_case_				:  Dict       =			n_head
     snake_case_				:  Union[str, Any]       =			n_inner
     snake_case_				:  int       =			rotary_dim
     snake_case_				:  Optional[Any]       =			activation_function
     snake_case_				:  Any       =			resid_pdrop
     snake_case_				:  List[Any]       =			embd_pdrop
     snake_case_				:  int       =			attn_pdrop
     snake_case_				:  Tuple       =			layer_norm_epsilon
     snake_case_				:  Optional[Any]       =			initializer_range
     snake_case_				:  int       =			use_cache

     snake_case_				:  List[Any]       =			bos_token_id
     snake_case_				:  Optional[Any]       =			eos_token_id

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





class  _UpperCAmelCase (    lowerCAmelCase__):

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

 @property
 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Optional[int]       =			OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}       )
     if self.use_past:
         self.fill_with_past_key_values_(lowercase_						,  direction='''inputs'''       )
         snake_case_				:  List[str]       =			{0: '''batch''', 1: '''past_sequence + sequence'''}
     else:
         snake_case_				:  Any       =			{0: '''batch''', 1: '''sequence'''}

     return common_inputs

 @property
 def 		_snake_case      (  self	: List[Any]       ):
     return self._config.n_layer

 @property
 def 		_snake_case      (  self	: Any       ):
     return self._config.n_head

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

     # We need to order the input in the way they appears in the forward()
     snake_case_				:  Optional[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

             snake_case_,			snake_case_				:  Dict       =			common_inputs['''input_ids'''].shape
             # Not using the same length for past_key_values
             snake_case_				:  Tuple       =			seqlen + 2
             snake_case_				:  Optional[int]       =			(
                 batch,
                 self.num_attention_heads,
                 past_key_values_length,
                 self._config.hidden_size // self.num_attention_heads,
             )
             snake_case_				:  List[Any]       =			[
                 (torch.zeros(lowercase_       ), torch.zeros(lowercase_       )) for _ in range(self.num_layers       )
             ]

     snake_case_				:  int       =			common_inputs['''attention_mask''']
     if self.use_past:
         snake_case_				:  Optional[Any]       =			ordered_inputs['''attention_mask'''].dtype
         snake_case_				:  Any       =			torch.cat(
             [ordered_inputs['''attention_mask'''], torch.ones(lowercase_						,  lowercase_						,  dtype=lowercase_       )]						,  dim=1       )

     return ordered_inputs






 @property
 def 		_snake_case      (  self	: Dict       ):
     return 13










"""simple docstring"""

import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
     import torch

     from transformers import AutoModelForImageClassification

if is_vision_available():
     from transformers import AutoImageProcessor




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

 @slow
 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     snake_case_				:  List[Any]       =			AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     model.to(lowercase_       )

     from datasets import load_dataset

     snake_case_				:  str       =			load_dataset('''nielsr/rvlcdip-demo'''       )

     snake_case_				:  Tuple       =			dataset['''train'''][0]['''image'''].convert('''RGB'''       )

     snake_case_				:  Union[str, Any]       =			image_processor(lowercase_						,  return_tensors='''pt'''       ).to(lowercase_       )

     # forward pass
     with torch.no_grad():
         snake_case_				:  List[Any]       =			model(**lowercase_       )
         snake_case_				:  int       =			outputs.logits

     snake_case_				:  Any       =			torch.Size((1, 16)       )
     self.assertEqual(logits.shape						,  lowercase_       )

     snake_case_				:  Any       =			torch.tensor(
         [-0.41_58, -0.40_92, -0.43_47]						,  device=lowercase_						,  dtype=torch.float						,  )
     self.assertTrue(torch.allclose(logits[0, :3]						,  lowercase_						,  atol=1E-4       )       )





