"""simple docstring"""





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

from packaging import version


if TYPE_CHECKING:
			from ... import PreTrainedTokenizer, TensorType

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


lowerCAmelCase_     =	logging.get_logger(__name__)

lowerCAmelCase_     =	{
    '''bigscience/bloom''': '''https://huggingface.co/bigscience/bloom/resolve/main/config.json''',
    '''bigscience/bloom-560m''': '''https://huggingface.co/bigscience/bloom-560m/blob/main/config.json''',
    '''bigscience/bloom-1b1''': '''https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json''',
    '''bigscience/bloom-1b7''': '''https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json''',
    '''bigscience/bloom-3b''': '''https://huggingface.co/bigscience/bloom-3b/blob/main/config.json''',
    '''bigscience/bloom-7b1''': '''https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json''',
}




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						"bloom"
				a			=						["past_key_values"]
				a			=						{
				    "num_hidden_layers": "n_layer",
				    "num_attention_heads": "n_head",
				}





				def __init__(      self							:							Union[str, Any]      ,    _A							:							List[Any]=2_5_0_8_8_0      ,    _A							:							Optional[int]=6_4      ,    _A							:							List[Any]=2      ,    _A							:							int=8      ,    _A							:							int=1e-5      ,    _A							:							Tuple=0.02      ,    _A							:							List[str]=True      ,    _A							:							Dict=1      ,    _A							:							List[Any]=2      ,    _A							:							Any=False      ,    _A							:							Dict=0.0      ,    _A							:							Tuple=0.0      ,    _A							:							Any=1      ,    _A							:							List[str]=False      ,    **_A							:							str      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    vocab_size
											# Backward compatibility with n_embed kwarg
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    kwargs.pop("""n_embed"""      ,    _A)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    hidden_size if n_embed is None else n_embed
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    n_layer
											_SCREAMING_SNAKE_CASE		:						Dict			  =    n_head
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    layer_norm_epsilon
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    initializer_range
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    use_cache
											_SCREAMING_SNAKE_CASE		:						Any			  =    pretraining_tp
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    apply_residual_connection_post_layernorm
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    hidden_dropout
											_SCREAMING_SNAKE_CASE		:						str			  =    attention_dropout

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    bos_token_id
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    eos_token_id
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    slow_but_exact

											super().__init__(bos_token_id=_A      ,    eos_token_id=_A      ,    **_A)




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						version.parse("1.12" )





				def __init__(      self							:							int      ,    _A							:							PretrainedConfig      ,    _A							:							str = "default"      ,    _A							:							List[PatchingSpec] = None      ,    _A							:							bool = False      ,    ):



											"""simple docstring"""







											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?
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    0





				@property
				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}})
											if self.use_past:
																		# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
																		self.fill_with_past_key_values_(_A      ,    direction="""inputs"""      ,    inverted_values_shape=_A)
																		_SCREAMING_SNAKE_CASE		:						Any			  =    {0: """batch""", 1: """past_sequence + sequence"""}
											else:
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    {0: """batch""", 1: """sequence"""}

											return common_inputs





				@property
				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											return self._config.n_layer





				@property
				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											return self._config.n_head





				@property
				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											return 1e-3





				def 		_lowerCAmelCase		(      self							:							List[str]      ,    _A							:							"PreTrainedTokenizer"      ,    _A							:							int = -1      ,    _A							:							int = -1      ,    _A							:							bool = False      ,    _A							:							Optional["TensorType"] = None      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    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()
											_SCREAMING_SNAKE_CASE		:						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

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

											_SCREAMING_SNAKE_CASE		:						Any			  =    common_inputs["""attention_mask"""]
											if self.use_past:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    ordered_inputs["""attention_mask"""].dtype
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    torch.cat(
																		    [ordered_inputs["""attention_mask"""], torch.ones(_A      ,    _A      ,    dtype=_A)]      ,    dim=1)

											return ordered_inputs





				@property
				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											return 1_3



"""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  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				@slow
				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip""")
											_SCREAMING_SNAKE_CASE		:						str			  =    AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip""")
											model.to(_A)

											from datasets import load_dataset

											_SCREAMING_SNAKE_CASE		:						Any			  =    load_dataset("""nielsr/rvlcdip-demo""")

											_SCREAMING_SNAKE_CASE		:						Any			  =    dataset["""train"""][0]["""image"""].convert("""RGB""")

											_SCREAMING_SNAKE_CASE		:						str			  =    image_processor(_A      ,    return_tensors="""pt""").to(_A)

											# forward pass
											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						Any			  =    model(**_A)
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    outputs.logits

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    torch.Size((1, 1_6))
											self.assertEqual(logits.shape      ,    _A)

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    torch.tensor(
											    [-0.4_158, -0.4_092, -0.4_347]      ,    device=_A      ,    dtype=torch.float      ,    )
											self.assertTrue(torch.allclose(logits[0, :3]      ,    _A      ,    atol=1e-4))


