

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


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

__UpperCamelCase    : Optional[Any]				 =						{
    '''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 							SCREAMING_SNAKE_CASE   (						a_			):






					"""simple docstring"""



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







					def __init__(			self     :							int  ,lowercase_     :							Dict=2_5_0_8_8_0  ,lowercase_     :							Dict=6_4  ,lowercase_     :							Dict=2  ,lowercase_     :							Union[str, Any]=8  ,lowercase_     :							Tuple=1E-5  ,lowercase_     :							str=0.02  ,lowercase_     :							List[str]=True  ,lowercase_     :							Optional[Any]=1  ,lowercase_     :							int=2  ,lowercase_     :							List[str]=False  ,lowercase_     :							List[str]=0.0  ,lowercase_     :							int=0.0  ,lowercase_     :							Dict=1  ,lowercase_     :							Optional[int]=False  ,**lowercase_     :							List[Any]  ,):
						lowerCAmelCase__      :		Union[str, Any]    							=      vocab_size
						# Backward compatibility with n_embed kwarg
						lowerCAmelCase__      :		Optional[int]    							=      kwargs.pop('''n_embed'''  ,lowercase_						)
						lowerCAmelCase__      :		Union[str, Any]    							=      hidden_size if n_embed is None else n_embed
						lowerCAmelCase__      :		List[str]    							=      n_layer
						lowerCAmelCase__      :		Any    							=      n_head
						lowerCAmelCase__      :		Union[str, Any]    							=      layer_norm_epsilon
						lowerCAmelCase__      :		Any    							=      initializer_range
						lowerCAmelCase__      :		Union[str, Any]    							=      use_cache
						lowerCAmelCase__      :		Union[str, Any]    							=      pretraining_tp
						lowerCAmelCase__      :		Optional[Any]    							=      apply_residual_connection_post_layernorm
						lowerCAmelCase__      :		Tuple    							=      hidden_dropout
						lowerCAmelCase__      :		Optional[int]    							=      attention_dropout

						lowerCAmelCase__      :		Tuple    							=      bos_token_id
						lowerCAmelCase__      :		Dict    							=      eos_token_id
						lowerCAmelCase__      :		Dict    							=      slow_but_exact

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







class 							SCREAMING_SNAKE_CASE   (						a_			):






					"""simple docstring"""



					lowercase__												=			version.parse("1.12"			)







					def __init__(			self     :							Optional[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?
							lowerCAmelCase__      :		Union[str, Any]    							=      0







					@property
					def 			__lowerCAmelCase					(			self     :							int						):
						lowerCAmelCase__      :		Union[str, 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_(lowercase_  ,direction='''inputs'''  ,inverted_values_shape=lowercase_						)
							lowerCAmelCase__      :		Dict    							=      {0: '''batch''', 1: '''past_sequence + sequence'''}
						else:
							lowerCAmelCase__      :		int    							=      {0: '''batch''', 1: '''sequence'''}

						return common_inputs







					@property
					def 			__lowerCAmelCase					(			self     :							int						):
						return self._config.n_layer







					@property
					def 			__lowerCAmelCase					(			self     :							Optional[int]						):
						return self._config.n_head







					@property
					def 			__lowerCAmelCase					(			self     :							Union[str, Any]						):
						return 1E-3







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							"PreTrainedTokenizer"  ,lowercase_     :							int = -1  ,lowercase_     :							int = -1  ,lowercase_     :							bool = False  ,lowercase_     :							Optional["TensorType"] = None  ,):
						lowerCAmelCase__      :		Optional[int]    							=      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()
						lowerCAmelCase__      :		Dict    							=      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

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

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

						return ordered_inputs







					@property
					def 			__lowerCAmelCase					(			self     :							List[str]						):
						return 1_3







"""simple docstring"""





import unittest

import numpy as np

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

if is_vision_available():
							from PIL import Image

							from transformers import LevitImageProcessor







class 							SCREAMING_SNAKE_CASE   (						unittest.TestCase			):






					"""simple docstring"""



					def __init__(			self     :							Any  ,lowercase_     :							Dict  ,lowercase_     :							List[str]=7  ,lowercase_     :							Tuple=3  ,lowercase_     :							List[str]=1_8  ,lowercase_     :							Optional[Any]=3_0  ,lowercase_     :							List[Any]=4_0_0  ,lowercase_     :							List[Any]=True  ,lowercase_     :							Any=None  ,lowercase_     :							Optional[Any]=True  ,lowercase_     :							str=None  ,lowercase_     :							List[Any]=True  ,lowercase_     :							Dict=[0.5, 0.5, 0.5]  ,lowercase_     :							Dict=[0.5, 0.5, 0.5]  ,):
						lowerCAmelCase__      :		Any    							=      size if size is not None else {'''shortest_edge''': 1_8}
						lowerCAmelCase__      :		Union[str, Any]    							=      crop_size if crop_size is not None else {'''height''': 1_8, '''width''': 1_8}
						lowerCAmelCase__      :		Dict    							=      parent
						lowerCAmelCase__      :		Dict    							=      batch_size
						lowerCAmelCase__      :		List[str]    							=      num_channels
						lowerCAmelCase__      :		Any    							=      image_size
						lowerCAmelCase__      :		Union[str, Any]    							=      min_resolution
						lowerCAmelCase__      :		Dict    							=      max_resolution
						lowerCAmelCase__      :		List[str]    							=      do_resize
						lowerCAmelCase__      :		Optional[Any]    							=      size
						lowerCAmelCase__      :		Tuple    							=      do_center_crop
						lowerCAmelCase__      :		Optional[int]    							=      crop_size
						lowerCAmelCase__      :		List[str]    							=      do_normalize
						lowerCAmelCase__      :		Tuple    							=      image_mean
						lowerCAmelCase__      :		int    							=      image_std







					def 			__lowerCAmelCase					(			self     :							List[str]						):
						return {
						    "image_mean": self.image_mean,
						    "image_std": self.image_std,
						    "do_normalize": self.do_normalize,
						    "do_resize": self.do_resize,
						    "do_center_crop": self.do_center_crop,
						    "size": self.size,
						    "crop_size": self.crop_size,
						}







@require_torch
@require_vision
class 							SCREAMING_SNAKE_CASE   (						a_       ,				unittest.TestCase			):






					"""simple docstring"""



					lowercase__												=			LevitImageProcessor if is_vision_available() else None







					def 			__lowerCAmelCase					(			self     :							List[str]						):
						lowerCAmelCase__      :		Optional[Any]    							=      LevitImageProcessingTester(self						)







					@property
					def 			__lowerCAmelCase					(			self     :							Any						):
						return self.image_processor_tester.prepare_image_processor_dict()







					def 			__lowerCAmelCase					(			self     :							str						):
						lowerCAmelCase__      :		Optional[int]    							=      self.image_processing_class(**self.image_processor_dict						)
						self.assertTrue(hasattr(lowercase_  ,'''image_mean'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''image_std'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''do_normalize'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''do_resize'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''do_center_crop'''						)						)
						self.assertTrue(hasattr(lowercase_  ,'''size'''						)						)







					def 			__lowerCAmelCase					(			self     :							Any						):
						lowerCAmelCase__      :		int    							=      self.image_processing_class.from_dict(self.image_processor_dict						)
						self.assertEqual(image_processor.size  ,{'''shortest_edge''': 1_8}						)
						self.assertEqual(image_processor.crop_size  ,{'''height''': 1_8, '''width''': 1_8}						)

						lowerCAmelCase__      :		List[str]    							=      self.image_processing_class.from_dict(self.image_processor_dict  ,size=4_2  ,crop_size=8_4						)
						self.assertEqual(image_processor.size  ,{'''shortest_edge''': 4_2}						)
						self.assertEqual(image_processor.crop_size  ,{'''height''': 8_4, '''width''': 8_4}						)







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]						):
						pass







					def 			__lowerCAmelCase					(			self     :							Optional[int]						):
						# Initialize image_processing
						lowerCAmelCase__      :		List[str]    							=      self.image_processing_class(**self.image_processor_dict						)
						# create random PIL images
						lowerCAmelCase__      :		Union[str, Any]    							=      prepare_image_inputs(self.image_processor_tester  ,equal_resolution=lowercase_						)
						for image in image_inputs:
							self.assertIsInstance(lowercase_  ,Image.Image						)

						# Test not batched input
						lowerCAmelCase__      :		Any    							=      image_processing(image_inputs[0]  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)

						# Test batched
						lowerCAmelCase__      :		Optional[int]    							=      image_processing(lowercase_  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)







					def 			__lowerCAmelCase					(			self     :							Union[str, Any]						):
						# Initialize image_processing
						lowerCAmelCase__      :		str    							=      self.image_processing_class(**self.image_processor_dict						)
						# create random numpy tensors
						lowerCAmelCase__      :		Optional[Any]    							=      prepare_image_inputs(self.image_processor_tester  ,equal_resolution=lowercase_  ,numpify=lowercase_						)
						for image in image_inputs:
							self.assertIsInstance(lowercase_  ,np.ndarray						)

						# Test not batched input
						lowerCAmelCase__      :		Tuple    							=      image_processing(image_inputs[0]  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)

						# Test batched
						lowerCAmelCase__      :		Union[str, Any]    							=      image_processing(lowercase_  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)







					def 			__lowerCAmelCase					(			self     :							int						):
						# Initialize image_processing
						lowerCAmelCase__      :		Tuple    							=      self.image_processing_class(**self.image_processor_dict						)
						# create random PyTorch tensors
						lowerCAmelCase__      :		Dict    							=      prepare_image_inputs(self.image_processor_tester  ,equal_resolution=lowercase_  ,torchify=lowercase_						)
						for image in image_inputs:
							self.assertIsInstance(lowercase_  ,torch.Tensor						)

						# Test not batched input
						lowerCAmelCase__      :		Dict    							=      image_processing(image_inputs[0]  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        1,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)

						# Test batched
						lowerCAmelCase__      :		Optional[int]    							=      image_processing(lowercase_  ,return_tensors='''pt'''						).pixel_values
						self.assertEqual(
						    encoded_images.shape  ,(
						        self.image_processor_tester.batch_size,
						        self.image_processor_tester.num_channels,
						        self.image_processor_tester.crop_size['''height'''],
						        self.image_processor_tester.crop_size['''width'''],
						    )  ,)




