






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowerCamelCase			:					Optional[Any]         =	logging.get_logger(__name__)



class 							_UpperCamelCase			(__lowercase			):
      snake_case_  =							['pixel_values']


      def __init__(	self					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = PILImageResampling.BICUBIC					,						__UpperCamelCase = True					,						__UpperCamelCase = True					,						__UpperCamelCase = 1 / 2_5_5					,						__UpperCamelCase = None					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	None:
           super().__init__(**__A	)
           __lowerCAmelCase								=					size if size is not None else {"height": 2_2_4, "width": 2_2_4}
           __lowerCAmelCase								=					get_size_dict(__A	)
           __lowerCAmelCase								=					crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
           __lowerCAmelCase								=					get_size_dict(__A					,						default_to_square=__A					,						param_name="crop_size"	)

           __lowerCAmelCase								=					do_resize
           __lowerCAmelCase								=					do_rescale
           __lowerCAmelCase								=					do_normalize
           __lowerCAmelCase								=					do_center_crop
           __lowerCAmelCase								=					crop_size
           __lowerCAmelCase								=					size
           __lowerCAmelCase								=					resample
           __lowerCAmelCase								=					rescale_factor
           __lowerCAmelCase								=					image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
           __lowerCAmelCase								=					image_std if image_std is not None else IMAGENET_DEFAULT_STD


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = PILImageResampling.BILINEAR					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           __lowerCAmelCase								=					get_size_dict(__A	)

           if "shortest_edge" in size:
                __lowerCAmelCase								=					get_resize_output_image_size(__A					,						size=size["shortest_edge"]					,						default_to_square=__A	)
                # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
           elif "height" in size and "width" in size:
                __lowerCAmelCase								=					(size["height"], size["width"])
           else:
                raise ValueError(F"""Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}"""	)
           return resize(__A					,						size=__A					,						resample=__A					,						data_format=__A					,						**__A	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           __lowerCAmelCase								=					get_size_dict(__A	)
           if "height" not in size or "width" not in size:
                raise ValueError(F"""The `size` parameter must contain the keys (height, width). Got {size.keys()}"""	)
           return center_crop(__A					,						size=(size["height"], size["width"])					,						data_format=__A					,						**__A	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase	)->	np.ndarray:
           return rescale(__A					,						scale=__A					,						data_format=__A					,						**__A	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           return normalize(__A					,						mean=__A					,						std=__A					,						data_format=__A					,						**__A	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = ChannelDimension.FIRST					,						**__UpperCamelCase					,						)->	BatchFeature:
           __lowerCAmelCase								=					do_resize if do_resize is not None else self.do_resize
           __lowerCAmelCase								=					do_rescale if do_rescale is not None else self.do_rescale
           __lowerCAmelCase								=					do_normalize if do_normalize is not None else self.do_normalize
           __lowerCAmelCase								=					do_center_crop if do_center_crop is not None else self.do_center_crop
           __lowerCAmelCase								=					crop_size if crop_size is not None else self.crop_size
           __lowerCAmelCase								=					get_size_dict(__A					,						param_name="crop_size"					,						default_to_square=__A	)
           __lowerCAmelCase								=					resample if resample is not None else self.resample
           __lowerCAmelCase								=					rescale_factor if rescale_factor is not None else self.rescale_factor
           __lowerCAmelCase								=					image_mean if image_mean is not None else self.image_mean
           __lowerCAmelCase								=					image_std if image_std is not None else self.image_std

           __lowerCAmelCase								=					size if size is not None else self.size
           __lowerCAmelCase								=					get_size_dict(__A	)

           if not is_batched(__A	):
                __lowerCAmelCase								=					[images]

           if not valid_images(__A	):
                raise ValueError(
                    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                    "torch.Tensor, tf.Tensor or jax.ndarray."	)

           if do_resize and size is None:
                raise ValueError("Size must be specified if do_resize is True."	)

           if do_center_crop and crop_size is None:
                raise ValueError("Crop size must be specified if do_center_crop is True."	)

           if do_rescale and rescale_factor is None:
                raise ValueError("Rescale factor must be specified if do_rescale is True."	)

           # All transformations expect numpy arrays.
           __lowerCAmelCase								=					[to_numpy_array(__A	) for image in images]

           if do_resize:
                __lowerCAmelCase								=					[self.resize(image=__A					,						size=__A					,						resample=__A	) for image in images]

           if do_center_crop:
                __lowerCAmelCase								=					[self.center_crop(image=__A					,						size=__A	) for image in images]

           if do_rescale:
                __lowerCAmelCase								=					[self.rescale(image=__A					,						scale=__A	) for image in images]

           if do_normalize:
                __lowerCAmelCase								=					[self.normalize(image=__A					,						mean=__A					,						std=__A	) for image in images]

           __lowerCAmelCase								=					[to_channel_dimension_format(__A					,						__A	) for image in images]

           __lowerCAmelCase								=					{"pixel_values": images}
           return BatchFeature(data=__A					,						tensor_type=__A	)














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

lowerCamelCase			:					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 							_UpperCamelCase			(a_			):
      snake_case_  =							"""bloom"""
      snake_case_  =							["""past_key_values"""]
      snake_case_  =							{
          """num_hidden_layers""": """n_layer""",
          """num_attention_heads""": """n_head""",
      }


      def __init__(	self					,						__UpperCamelCase=2_5_0_8_8_0					,						__UpperCamelCase=6_4					,						__UpperCamelCase=2					,						__UpperCamelCase=8					,						__UpperCamelCase=1e-5					,						__UpperCamelCase=0.0_2					,						__UpperCamelCase=True					,						__UpperCamelCase=1					,						__UpperCamelCase=2					,						__UpperCamelCase=False					,						__UpperCamelCase=0.0					,						__UpperCamelCase=0.0					,						__UpperCamelCase=1					,						__UpperCamelCase=False					,						**__UpperCamelCase					,						)->	Dict:
           __lowerCAmelCase								=					vocab_size
           # Backward compatibility with n_embed kwarg
           __lowerCAmelCase								=					kwargs.pop("n_embed"					,						__UpperCamelCase	)
           __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=__UpperCamelCase					,						eos_token_id=__UpperCamelCase					,						**__UpperCamelCase	)



class 							_UpperCamelCase			(a_			):
      snake_case_  =							version.parse("""1.12"""			)


      def __init__(	self					,						__UpperCamelCase					,						__UpperCamelCase = "default"					,						__UpperCamelCase = None					,						__UpperCamelCase = False					,						)->	str:
           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?
                __lowerCAmelCase								=					0


      @property
      def  __UpperCAmelCase		(	self	)->	Mapping[str, Mapping[int, str]]:
           __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_(__UpperCamelCase					,						direction="inputs"					,						inverted_values_shape=__UpperCamelCase	)
                __lowerCAmelCase								=					{0: "batch", 1: "past_sequence + sequence"}
           else:
                __lowerCAmelCase								=					{0: "batch", 1: "sequence"}

           return common_inputs


      @property
      def  __UpperCAmelCase		(	self	)->	int:
           return self._config.n_layer


      @property
      def  __UpperCAmelCase		(	self	)->	int:
           return self._config.n_head


      @property
      def  __UpperCAmelCase		(	self	)->	float:
           return 1e-3


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = -1					,						__UpperCamelCase = -1					,						__UpperCamelCase = False					,						__UpperCamelCase = None					,						)->	Mapping[str, Any]:
           __lowerCAmelCase								=					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()
           __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(__UpperCamelCase	), torch.zeros(__UpperCamelCase	)) 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(__UpperCamelCase					,						__UpperCamelCase					,						dtype=__UpperCamelCase	)]					,						dim=1	)

           return ordered_inputs


      @property
      def  __UpperCAmelCase		(	self	)->	int:
           return 1_3






