



"""simple docstring"""






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

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__UpperCAmelCase      :      Union[str, Any]   					=					logging.get_logger(__name__)

__UpperCAmelCase      :      str   					=					{
    'google/bigbird-pegasus-large-arxiv': (
        'https://huggingface.co/google/bigbird-pegasus-large-arxiv/resolve/main/config.json'
    ),
    'google/bigbird-pegasus-large-pubmed': (
        'https://huggingface.co/google/bigbird-pegasus-large-pubmed/resolve/main/config.json'
    ),
    'google/bigbird-pegasus-large-bigpatent': (
        'https://huggingface.co/google/bigbird-pegasus-large-bigpatent/resolve/main/config.json'
    ),
    # See all BigBirdPegasus models at https://huggingface.co/models?filter=bigbird_pegasus
}

class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					a__        =     'bigbird_pegasus'
					a__        =     ['past_key_values']
					a__        =     {
					    'num_attention_heads': 'encoder_attention_heads',
					    'hidden_size': 'd_model',
					    'attention_probs_dropout_prob': 'attention_dropout',
					}







					def __init__(      self    ,       a=9_61_03    ,       a=40_96    ,       a=16    ,       a=40_96    ,       a=16    ,       a=16    ,       a=40_96    ,       a=16    ,       a=0.0    ,       a=0.0    ,       a=True    ,       a=True    ,       a="gelu_new"    ,       a=10_24    ,       a=0.1    ,       a=0.0    ,       a=0.0    ,       a=0.02    ,       a=2    ,       a=0.0    ,       a=True    ,       a=0    ,       a=2    ,       a=1    ,       a="block_sparse"    ,       a=64    ,       a=3    ,       a=False    ,       **a    ,       ):




										"""simple docstring"""





										snake_case_						:Optional[int]								=						vocab_size
										snake_case_						:Any								=						max_position_embeddings
										snake_case_						:List[Any]								=						d_model
										snake_case_						:Any								=						encoder_ffn_dim
										snake_case_						:str								=						encoder_layers
										snake_case_						:Union[str, Any]								=						encoder_attention_heads
										snake_case_						:Any								=						decoder_ffn_dim
										snake_case_						:Dict								=						decoder_layers
										snake_case_						:List[Any]								=						decoder_attention_heads
										snake_case_						:Any								=						dropout
										snake_case_						:Union[str, Any]								=						attention_dropout
										snake_case_						:Optional[int]								=						activation_dropout
										snake_case_						:int								=						activation_function
										snake_case_						:int								=						init_std
										snake_case_						:Union[str, Any]								=						encoder_layerdrop
										snake_case_						:Dict								=						decoder_layerdrop
										snake_case_						:Any								=						classifier_dropout
										snake_case_						:Any								=						use_cache
										snake_case_						:Any								=						encoder_layers
										snake_case_						:int								=						scale_embedding  # scale factor will be sqrt(d_model) if True

										# extra config
										snake_case_						:Optional[int]								=						attention_type
										snake_case_						:int								=						block_size
										snake_case_						:Optional[int]								=						num_random_blocks
										snake_case_						:List[str]								=						use_bias

										super().__init__(
										    pad_token_id=a    ,       bos_token_id=a    ,       eos_token_id=a    ,       is_encoder_decoder=a    ,       decoder_start_token_id=a    ,       **a    ,       )



class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					@property
					def 		_a (      self   ):




										"""simple docstring"""





										if self.task in ["default", "seq2seq-lm"]:
															snake_case_						:int								=						OrderedDict(
															    [
															        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
															        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
															    ]   )

															if self.use_past:
																				snake_case_						:int								=						{0: "batch"}
																				snake_case_						:List[str]								=						{0: "batch", 1: "past_decoder_sequence + sequence"}
															else:
																				snake_case_						:Optional[int]								=						{0: "batch", 1: "decoder_sequence"}
																				snake_case_						:Optional[Any]								=						{0: "batch", 1: "decoder_sequence"}

															if self.use_past:
																				self.fill_with_past_key_values_(a    ,       direction="inputs"   )
										elif self.task == "causal-lm":
															# TODO: figure this case out.
															snake_case_						:List[str]								=						OrderedDict(
															    [
															        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
															        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
															    ]   )
															if self.use_past:
																				snake_case_	,  snake_case_						:Tuple								=						self.num_layers
																				for i in range(a   ):
																									snake_case_						:int								=						{0: "batch", 2: "past_sequence + sequence"}
																									snake_case_						:List[Any]								=						{0: "batch", 2: "past_sequence + sequence"}
										else:
															snake_case_						:List[Any]								=						OrderedDict(
															    [
															        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
															        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
															        ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
															        ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
															    ]   )

										return common_inputs







					@property
					def 		_a (      self   ):




										"""simple docstring"""





										if self.task in ["default", "seq2seq-lm"]:
															snake_case_						:Optional[int]								=						super().outputs
										else:
															snake_case_						:List[str]								=						super(a    ,       self   ).outputs
															if self.use_past:
																				snake_case_	,  snake_case_						:int								=						self.num_layers
																				for i in range(a   ):
																									snake_case_						:Optional[Any]								=						{0: "batch", 2: "past_sequence + sequence"}
																									snake_case_						:Tuple								=						{0: "batch", 2: "past_sequence + sequence"}
										return common_outputs







					def 		_a (      self    ,       a    ,       a = -1    ,       a = -1    ,       a = False    ,       a = None    ,       ):




										"""simple docstring"""





										snake_case_						:int								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    a    ,       a    ,       a    ,       a    ,       a   )

										# Generate decoder inputs
										snake_case_						:List[str]								=						seq_length if not self.use_past else 1
										snake_case_						:str								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    a    ,       a    ,       a    ,       a    ,       a   )
										snake_case_						:Union[str, Any]								=						{F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
										snake_case_						:Tuple								=						dict(**a    ,       **a   )

										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_						:Optional[int]								=						common_inputs["input_ids"].shape
															snake_case_						:Optional[int]								=						common_inputs["decoder_input_ids"].shape[1]
															snake_case_	,  snake_case_						:Optional[int]								=						self.num_attention_heads
															snake_case_						:str								=						(
															    batch,
															    num_encoder_attention_heads,
															    encoder_seq_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)
															snake_case_						:Any								=						decoder_seq_length + 3
															snake_case_						:int								=						(
															    batch,
															    num_decoder_attention_heads,
															    decoder_past_length,
															    self._config.hidden_size // num_decoder_attention_heads,
															)

															snake_case_						:Union[str, Any]								=						torch.cat(
															    [common_inputs["decoder_attention_mask"], torch.ones(a    ,       a   )]    ,       dim=1   )

															snake_case_						:Union[str, Any]								=						[]
															# If the number of encoder and decoder layers are present in the model configuration, both are considered
															snake_case_	,  snake_case_						:Tuple								=						self.num_layers
															snake_case_						:int								=						min(a    ,       a   )
															snake_case_						:Optional[Any]								=						max(a    ,       a   ) - min_num_layers
															snake_case_						:List[Any]								=						"encoder" if num_encoder_layers > num_decoder_layers else "decoder"

															for _ in range(a   ):
																				common_inputs["past_key_values"].append(
																				    (
																				        torch.zeros(a   ),
																				        torch.zeros(a   ),
																				        torch.zeros(a   ),
																				        torch.zeros(a   ),
																				    )   )
															# TODO: test this.
															snake_case_						:Any								=						encoder_shape if remaining_side_name == "encoder" else decoder_shape
															for _ in range(a    ,       a   ):
																				common_inputs["past_key_values"].append((torch.zeros(a   ), torch.zeros(a   ))   )
										return common_inputs







					def 		_a (      self    ,       a    ,       a = -1    ,       a = -1    ,       a = False    ,       a = None    ,       ):




										"""simple docstring"""





										snake_case_						:List[str]								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
										    a    ,       a    ,       a    ,       a    ,       a   )

										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_						:Union[str, Any]								=						common_inputs["input_ids"].shape
															# Not using the same length for past_key_values
															snake_case_						:str								=						seqlen + 2
															snake_case_	,  snake_case_						:Union[str, Any]								=						self.num_layers
															snake_case_	,  snake_case_						:Any								=						self.num_attention_heads
															snake_case_						:List[str]								=						(
															    batch,
															    num_encoder_attention_heads,
															    past_key_values_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)

															snake_case_						:Optional[Any]								=						common_inputs["attention_mask"].dtype
															snake_case_						:Tuple								=						torch.cat(
															    [common_inputs["attention_mask"], torch.ones(a    ,       a    ,       dtype=a   )]    ,       dim=1   )
															snake_case_						:Optional[int]								=						[
															    (torch.zeros(a   ), torch.zeros(a   )) for _ in range(a   )
															]
										return common_inputs







					def 		_a (      self    ,       a    ,       a = -1    ,       a = -1    ,       a = False    ,       a = None    ,       ):




										"""simple docstring"""





										snake_case_						:Optional[int]								=						compute_effective_axis_dimension(
										    a    ,       fixed_dimension=OnnxConfig.default_fixed_batch    ,       num_token_to_add=0   )

										# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
										snake_case_						:List[Any]								=						tokenizer.num_special_tokens_to_add(a   )
										snake_case_						:Tuple								=						compute_effective_axis_dimension(
										    a    ,       fixed_dimension=OnnxConfig.default_fixed_sequence    ,       num_token_to_add=a   )

										# Generate dummy inputs according to compute batch and sequence
										snake_case_						:Any								=						[" ".join([tokenizer.unk_token]   ) * seq_length] * batch_size
										snake_case_						:List[str]								=						dict(tokenizer(a    ,       return_tensors=a   )   )
										return common_inputs







					def 		_a (      self    ,       a    ,       a = -1    ,       a = -1    ,       a = False    ,       a = None    ,       ):




										"""simple docstring"""





										if self.task in ["default", "seq2seq-lm"]:
															snake_case_						:Dict								=						self._generate_dummy_inputs_for_default_and_seqaseq_lm(
															    a    ,       batch_size=a    ,       seq_length=a    ,       is_pair=a    ,       framework=a   )

										elif self.task == "causal-lm":
															snake_case_						:Optional[Any]								=						self._generate_dummy_inputs_for_causal_lm(
															    a    ,       batch_size=a    ,       seq_length=a    ,       is_pair=a    ,       framework=a   )
										else:
															snake_case_						:List[Any]								=						self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
															    a    ,       batch_size=a    ,       seq_length=a    ,       is_pair=a    ,       framework=a   )

										return common_inputs







					def 		_a (      self    ,       a    ,       a    ,       a    ,       a   ):




										"""simple docstring"""





										if self.task in ["default", "seq2seq-lm"]:
															snake_case_						:str								=						super()._flatten_past_key_values_(a    ,       a    ,       a    ,       a   )
										else:
															snake_case_						:Any								=						super(a    ,       self   )._flatten_past_key_values_(
															    a    ,       a    ,       a    ,       a   )






"""simple docstring"""






import warnings
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from PIL import Image

from .configuration_utils import ConfigMixin, register_to_config
from .utils import CONFIG_NAME, PIL_INTERPOLATION, deprecate

class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					a__        =     CONFIG_NAME







					@register_to_config
					def __init__(      self    ,       a = True    ,       a = 8    ,       a = "lanczos"    ,       a = True    ,       a = False    ,       ):




										"""simple docstring"""





										super().__init__()







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:Union[str, Any]								=						images[None, ...]
										snake_case_						:Optional[int]								=						(images * 2_55).round().astype("uint8"   )
										if images.shape[-1] == 1:
															# special case for grayscale (single channel) images
															snake_case_						:List[str]								=						[Image.fromarray(image.squeeze()    ,       mode="L"   ) for image in images]
										else:
															snake_case_						:str								=						[Image.fromarray(a   ) for image in images]

										return pil_images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if not isinstance(a    ,       a   ):
															snake_case_						:Dict								=						[images]
										snake_case_						:Optional[int]								=						[np.array(a   ).astype(np.floataa   ) / 255.0 for image in images]
										snake_case_						:Optional[int]								=						np.stack(a    ,       axis=0   )

										return images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:int								=						images[..., None]

										snake_case_						:str								=						torch.from_numpy(images.transpose(0    ,       3    ,       1    ,       2   )   )
										return images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										snake_case_						:str								=						images.cpu().permute(0    ,       2    ,       3    ,       1   ).float().numpy()
										return images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										return 2.0 * images - 1.0







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										return (images / 2 + 0.5).clamp(0    ,       1   )







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										snake_case_						:List[str]								=						image.convert("RGB"   )
										return image







					def 		_a (      self    ,       a    ,       a = None    ,       a = None    ,       ):




										"""simple docstring"""





										if height is None:
															snake_case_						:Optional[Any]								=						image.height
										if width is None:
															snake_case_						:Dict								=						image.width

										snake_case_	,  snake_case_						:Optional[Any]								=						(
										    x - x % self.config.vae_scale_factor for x in (width, height)
										)  # resize to integer multiple of vae_scale_factor
										snake_case_						:Any								=						image.resize((width, height)    ,       resample=PIL_INTERPOLATION[self.config.resample]   )
										return image







					def 		_a (      self    ,       a    ,       a = None    ,       a = None    ,       ):




										"""simple docstring"""





										snake_case_						:Optional[Any]								=						(PIL.Image.Image, np.ndarray, torch.Tensor)
										if isinstance(a    ,       a   ):
															snake_case_						:str								=						[image]
										elif not (isinstance(a    ,       a   ) and all(isinstance(a    ,       a   ) for i in image   )):
															raise ValueError(
															    F'''Input is in incorrect format: {[type(a   ) for i in image]}. Currently, we only support {', '.join(a   )}'''   )

										if isinstance(image[0]    ,       PIL.Image.Image   ):
															if self.config.do_convert_rgb:
																				snake_case_						:int								=						[self.convert_to_rgb(a   ) for i in image]
															if self.config.do_resize:
																				snake_case_						:Tuple								=						[self.resize(a    ,       a    ,       a   ) for i in image]
															snake_case_						:Dict								=						self.pil_to_numpy(a   )  # to np
															snake_case_						:int								=						self.numpy_to_pt(a   )  # to pt

										elif isinstance(image[0]    ,       np.ndarray   ):
															snake_case_						:Optional[Any]								=						np.concatenate(a    ,       axis=0   ) if image[0].ndim == 4 else np.stack(a    ,       axis=0   )
															snake_case_						:List[Any]								=						self.numpy_to_pt(a   )
															snake_case_	,  snake_case_	,  snake_case_	,  snake_case_						:Optional[int]								=						image.shape
															if self.config.do_resize and (
															    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
															):
																				raise ValueError(
																				    F'''Currently we only support resizing for PIL image - please resize your numpy array to be divisible by {self.config.vae_scale_factor}'''
																				    F'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''   )

										elif isinstance(image[0]    ,       torch.Tensor   ):
															snake_case_						:Optional[Any]								=						torch.cat(a    ,       axis=0   ) if image[0].ndim == 4 else torch.stack(a    ,       axis=0   )
															snake_case_	,  snake_case_	,  snake_case_	,  snake_case_						:int								=						image.shape

															# don't need any preprocess if the image is latents
															if channel == 4:
																				return image

															if self.config.do_resize and (
															    height % self.config.vae_scale_factor != 0 or width % self.config.vae_scale_factor != 0
															):
																				raise ValueError(
																				    F'''Currently we only support resizing for PIL image - please resize your pytorch tensor to be divisible by {self.config.vae_scale_factor}'''
																				    F'''currently the sizes are {height} and {width}. You can also pass a PIL image instead to use resize option in VAEImageProcessor'''   )

        # expected range [0,1], normalize to [-1,1]
										snake_case_						:Union[str, Any]								=						self.config.do_normalize
										if image.min() < 0:
															warnings.warn(
															    "Passing `image` as torch tensor with value range in [-1,1] is deprecated. The expected value range for image tensor is [0,1] "
															    F'''when passing as pytorch tensor or numpy Array. You passed `image` with value range [{image.min()},{image.max()}]'''    ,       a    ,       )
															snake_case_						:Optional[int]								=						False

										if do_normalize:
															snake_case_						:Tuple								=						self.normalize(a   )

										return image







					def 		_a (      self    ,       a    ,       a = "pil"    ,       a = None    ,       ):




										"""simple docstring"""





										if not isinstance(a    ,       torch.Tensor   ):
															raise ValueError(
															    F'''Input for postprocessing is in incorrect format: {type(a   )}. We only support pytorch tensor'''   )
										if output_type not in ["latent", "pt", "np", "pil"]:
															snake_case_						:List[Any]								=						(
															    F'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
															    "`pil`, `np`, `pt`, `latent`"
															)
															deprecate("Unsupported output_type"    ,       "1.0.0"    ,       a    ,       standard_warn=a   )
															snake_case_						:Dict								=						"np"

										if output_type == "latent":
															return image

										if do_denormalize is None:
															snake_case_						:List[Any]								=						[self.config.do_normalize] * image.shape[0]

										snake_case_						:Any								=						torch.stack(
										    [self.denormalize(image[i]   ) if do_denormalize[i] else image[i] for i in range(image.shape[0]   )]   )

										if output_type == "pt":
															return image

										snake_case_						:int								=						self.pt_to_numpy(a   )

										if output_type == "np":
															return image

										if output_type == "pil":
															return self.numpy_to_pil(a   )



class 	__lowerCAmelCase							(__UpperCamelCase  ):


					'''simple docstring'''


					a__        =     CONFIG_NAME







					@register_to_config
					def __init__(      self    ,       a = True    ,       a = 8    ,       a = "lanczos"    ,       a = True    ,       ):




										"""simple docstring"""





										super().__init__()







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:List[Any]								=						images[None, ...]
										snake_case_						:Optional[int]								=						(images * 2_55).round().astype("uint8"   )
										if images.shape[-1] == 1:
															# special case for grayscale (single channel) images
															snake_case_						:str								=						[Image.fromarray(image.squeeze()    ,       mode="L"   ) for image in images]
										else:
															snake_case_						:Any								=						[Image.fromarray(image[:, :, :3]   ) for image in images]

										return pil_images







					@staticmethod
					def 		_a (      a   ):




										"""simple docstring"""





										return image[:, :, 1] * 2**8 + image[:, :, 2]







					def 		_a (      self    ,       a   ):




										"""simple docstring"""





										if images.ndim == 3:
															snake_case_						:Optional[int]								=						images[None, ...]
										snake_case_						:Union[str, Any]								=						images[:, :, :, 3:]
										if images.shape[-1] == 6:
															snake_case_						:Tuple								=						(images_depth * 2_55).round().astype("uint8"   )
															snake_case_						:Optional[Any]								=						[
															    Image.fromarray(self.rgblike_to_depthmap(a   )    ,       mode="I;16"   ) for image_depth in images_depth
															]
										elif images.shape[-1] == 4:
															snake_case_						:Optional[Any]								=						(images_depth * 65535.0).astype(np.uintaa   )
															snake_case_						:List[Any]								=						[Image.fromarray(a    ,       mode="I;16"   ) for image_depth in images_depth]
										else:
															raise Exception("Not supported"   )

										return pil_images







					def 		_a (      self    ,       a    ,       a = "pil"    ,       a = None    ,       ):




										"""simple docstring"""





										if not isinstance(a    ,       torch.Tensor   ):
															raise ValueError(
															    F'''Input for postprocessing is in incorrect format: {type(a   )}. We only support pytorch tensor'''   )
										if output_type not in ["latent", "pt", "np", "pil"]:
															snake_case_						:Any								=						(
															    F'''the output_type {output_type} is outdated and has been set to `np`. Please make sure to set it to one of these instead: '''
															    "`pil`, `np`, `pt`, `latent`"
															)
															deprecate("Unsupported output_type"    ,       "1.0.0"    ,       a    ,       standard_warn=a   )
															snake_case_						:Optional[Any]								=						"np"

										if do_denormalize is None:
															snake_case_						:Any								=						[self.config.do_normalize] * image.shape[0]

										snake_case_						:Tuple								=						torch.stack(
										    [self.denormalize(image[i]   ) if do_denormalize[i] else image[i] for i in range(image.shape[0]   )]   )

										snake_case_						:List[str]								=						self.pt_to_numpy(a   )

										if output_type == "np":
															if image.shape[-1] == 6:
																				snake_case_						:Optional[int]								=						np.stack([self.rgblike_to_depthmap(im[:, :, 3:]   ) for im in image]    ,       axis=0   )
															else:
																				snake_case_						:Any								=						image[:, :, :, 3:]
															return image[:, :, :, :3], image_depth

										if output_type == "pil":
															return self.numpy_to_pil(a   ), self.numpy_to_depth(a   )
										else:
															raise Exception(F'''This type {output_type} is not supported'''   )

