






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    :      Union[str, Any]  			=		logging.get_logger(__name__)

UpperCamelCase    :      int  			=		{
    '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 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					'bloom'
		_UpperCamelCase				  =					['past_key_values']
		_UpperCamelCase				  =					{
		    'num_hidden_layers': 'n_layer',
		    'num_attention_heads': 'n_head',
		}






		def __init__(      self   ,_lowerCAmelCase=25_08_80   ,_lowerCAmelCase=64   ,_lowerCAmelCase=2   ,_lowerCAmelCase=8   ,_lowerCAmelCase=1E-5   ,_lowerCAmelCase=0.02   ,_lowerCAmelCase=True   ,_lowerCAmelCase=1   ,_lowerCAmelCase=2   ,_lowerCAmelCase=False   ,_lowerCAmelCase=0.0   ,_lowerCAmelCase=0.0   ,_lowerCAmelCase=1   ,_lowerCAmelCase=False   ,**_lowerCAmelCase   ,):
								lowerCamelCase__         =  vocab_size
								# Backward compatibility with n_embed kwarg
								lowerCamelCase__         =  kwargs.pop("""n_embed"""   ,_lowerCAmelCase       )
								lowerCamelCase__         =  hidden_size if n_embed is None else n_embed
								lowerCamelCase__         =  n_layer
								lowerCamelCase__         =  n_head
								lowerCamelCase__         =  layer_norm_epsilon
								lowerCamelCase__         =  initializer_range
								lowerCamelCase__         =  use_cache
								lowerCamelCase__         =  pretraining_tp
								lowerCamelCase__         =  apply_residual_connection_post_layernorm
								lowerCamelCase__         =  hidden_dropout
								lowerCamelCase__         =  attention_dropout

								lowerCamelCase__         =  bos_token_id
								lowerCamelCase__         =  eos_token_id
								lowerCamelCase__         =  slow_but_exact

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




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		_UpperCamelCase				  =					version.parse('1.12'			)






		def __init__(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = "default"   ,_lowerCAmelCase = None   ,_lowerCAmelCase = False   ,):
								super().__init__(_lowerCAmelCase   ,task=_lowerCAmelCase   ,patching_specs=_lowerCAmelCase   ,use_past=_lowerCAmelCase       )
								if not getattr(self._config   ,"""pad_token_id"""   ,_lowerCAmelCase       ):
														# TODO: how to do that better?
														lowerCamelCase__         =  0






		@property
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  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_(_lowerCAmelCase   ,direction="""inputs"""   ,inverted_values_shape=_lowerCAmelCase       )
														lowerCamelCase__         =  {0: """batch""", 1: """past_sequence + sequence"""}
								else:
														lowerCamelCase__         =  {0: """batch""", 1: """sequence"""}

								return common_inputs






		@property
		def 			UpperCamelCase_					(      self       ):
								return self._config.n_layer






		@property
		def 			UpperCamelCase_					(      self       ):
								return self._config.n_head






		@property
		def 			UpperCamelCase_					(      self       ):
								return 1E-3






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = -1   ,_lowerCAmelCase = -1   ,_lowerCAmelCase = False   ,_lowerCAmelCase = None   ,):
								lowerCamelCase__         =  super(_lowerCAmelCase   ,self       ).generate_dummy_inputs(
								    _lowerCAmelCase   ,batch_size=_lowerCAmelCase   ,seq_length=_lowerCAmelCase   ,is_pair=_lowerCAmelCase   ,framework=_lowerCAmelCase       )

								# We need to order the input in the way they appears in the forward()
								lowerCamelCase__         =  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__         =  common_inputs["""input_ids"""].shape
																				# Not using the same length for past_key_values
																				lowerCamelCase__         =  seqlen + 2
																				lowerCamelCase__         =  self._config.hidden_size // self.num_attention_heads
																				lowerCamelCase__         =  (
																				    batch * self.num_attention_heads,
																				    head_dim,
																				    past_key_values_length,
																				)
																				lowerCamelCase__         =  (
																				    batch * self.num_attention_heads,
																				    past_key_values_length,
																				    head_dim,
																				)
																				lowerCamelCase__         =  [
																				    (torch.zeros(_lowerCAmelCase       ), torch.zeros(_lowerCAmelCase       )) for _ in range(self.num_layers       )
																				]

								lowerCamelCase__         =  common_inputs["""attention_mask"""]
								if self.use_past:
														lowerCamelCase__         =  ordered_inputs["""attention_mask"""].dtype
														lowerCamelCase__         =  torch.cat(
														    [ordered_inputs["""attention_mask"""], torch.ones(_lowerCAmelCase   ,_lowerCAmelCase   ,dtype=_lowerCAmelCase       )]   ,dim=1       )

								return ordered_inputs






		@property
		def 			UpperCamelCase_					(      self       ):
								return 13









'''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 		UpperCamelCase__    (unittest.TestCase			):




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase   ,_lowerCAmelCase=7   ,_lowerCAmelCase=3   ,_lowerCAmelCase=18   ,_lowerCAmelCase=30   ,_lowerCAmelCase=4_00   ,_lowerCAmelCase=True   ,_lowerCAmelCase=None   ,_lowerCAmelCase=True   ,_lowerCAmelCase=None   ,_lowerCAmelCase=True   ,_lowerCAmelCase=[0.5, 0.5, 0.5]   ,_lowerCAmelCase=[0.5, 0.5, 0.5]   ,):
								lowerCamelCase__         =  size if size is not None else {"""shortest_edge""": 18}
								lowerCamelCase__         =  crop_size if crop_size is not None else {"""height""": 18, """width""": 18}
								lowerCamelCase__         =  parent
								lowerCamelCase__         =  batch_size
								lowerCamelCase__         =  num_channels
								lowerCamelCase__         =  image_size
								lowerCamelCase__         =  min_resolution
								lowerCamelCase__         =  max_resolution
								lowerCamelCase__         =  do_resize
								lowerCamelCase__         =  size
								lowerCamelCase__         =  do_center_crop
								lowerCamelCase__         =  crop_size
								lowerCamelCase__         =  do_normalize
								lowerCamelCase__         =  image_mean
								lowerCamelCase__         =  image_std






		def 			UpperCamelCase_					(      self       ):
								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 		UpperCamelCase__    (a   ,unittest.TestCase			):




		'''simple docstring'''





		_UpperCamelCase				  =					LevitImageProcessor if is_vision_available() else None






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  LevitImageProcessingTester(self       )






		@property
		def 			UpperCamelCase_					(      self       ):
								return self.image_processor_tester.prepare_image_processor_dict()






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.image_processing_class(**self.image_processor_dict       )
								self.assertTrue(hasattr(_lowerCAmelCase   ,"""image_mean"""       )       )
								self.assertTrue(hasattr(_lowerCAmelCase   ,"""image_std"""       )       )
								self.assertTrue(hasattr(_lowerCAmelCase   ,"""do_normalize"""       )       )
								self.assertTrue(hasattr(_lowerCAmelCase   ,"""do_resize"""       )       )
								self.assertTrue(hasattr(_lowerCAmelCase   ,"""do_center_crop"""       )       )
								self.assertTrue(hasattr(_lowerCAmelCase   ,"""size"""       )       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.image_processing_class.from_dict(self.image_processor_dict       )
								self.assertEqual(image_processor.size   ,{"""shortest_edge""": 18}       )
								self.assertEqual(image_processor.crop_size   ,{"""height""": 18, """width""": 18}       )

								lowerCamelCase__         =  self.image_processing_class.from_dict(self.image_processor_dict   ,size=42   ,crop_size=84       )
								self.assertEqual(image_processor.size   ,{"""shortest_edge""": 42}       )
								self.assertEqual(image_processor.crop_size   ,{"""height""": 84, """width""": 84}       )






		def 			UpperCamelCase_					(      self       ):
								pass






		def 			UpperCamelCase_					(      self       ):
								# Initialize image_processing
								lowerCamelCase__         =  self.image_processing_class(**self.image_processor_dict       )
								# create random PIL images
								lowerCamelCase__         =  prepare_image_inputs(self.image_processor_tester   ,equal_resolution=_lowerCAmelCase       )
								for image in image_inputs:
														self.assertIsInstance(_lowerCAmelCase   ,Image.Image       )

								# Test not batched input
								lowerCamelCase__         =  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__         =  image_processing(_lowerCAmelCase   ,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 			UpperCamelCase_					(      self       ):
								# Initialize image_processing
								lowerCamelCase__         =  self.image_processing_class(**self.image_processor_dict       )
								# create random numpy tensors
								lowerCamelCase__         =  prepare_image_inputs(self.image_processor_tester   ,equal_resolution=_lowerCAmelCase   ,numpify=_lowerCAmelCase       )
								for image in image_inputs:
														self.assertIsInstance(_lowerCAmelCase   ,np.ndarray       )

								# Test not batched input
								lowerCamelCase__         =  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__         =  image_processing(_lowerCAmelCase   ,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 			UpperCamelCase_					(      self       ):
								# Initialize image_processing
								lowerCamelCase__         =  self.image_processing_class(**self.image_processor_dict       )
								# create random PyTorch tensors
								lowerCamelCase__         =  prepare_image_inputs(self.image_processor_tester   ,equal_resolution=_lowerCAmelCase   ,torchify=_lowerCAmelCase       )
								for image in image_inputs:
														self.assertIsInstance(_lowerCAmelCase   ,torch.Tensor       )

								# Test not batched input
								lowerCamelCase__         =  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__         =  image_processing(_lowerCAmelCase   ,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"""],
								    )   ,)

