




'''simple docstring'''

from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
							import torch

if is_tf_available():
							import tensorflow as tf






class 						a				(   _lowerCamelCase     ):
				snake_case_      =		["image_processor"]
				snake_case_      =		"SamImageProcessor"


				def __init__(		self    :					List[Any]       ,				lowercase_    :					List[Any]					):
									super().__init__(lowercase_					)
									snake_case_									=				self.image_processor
									snake_case_									=				-10
									snake_case_									=				self.image_processor.size['''longest_edge''']


				def __call__(		self    :					List[str]       ,				lowercase_    :					Tuple=None       ,				lowercase_    :					Tuple=None       ,				lowercase_    :					Optional[int]=None       ,				lowercase_    :					Dict=None       ,				lowercase_    :					Optional[Union[str, TensorType]] = None       ,				**lowercase_    :					Any       ,				):
									snake_case_									=				self.image_processor(
									    lowercase_       ,				return_tensors=lowercase_       ,				**lowercase_       ,				)

									# pop arguments that are not used in the foward but used nevertheless
									snake_case_									=				encoding_image_processor['''original_sizes''']

									if hasattr(lowercase_       ,				'''numpy'''					):  # Checks if Torch or TF tensor
														snake_case_									=				original_sizes.numpy()

									snake_case_			,snake_case_			,snake_case_									=				self._check_and_preprocess_points(
									    input_points=lowercase_       ,				input_labels=lowercase_       ,				input_boxes=lowercase_       ,				)

									snake_case_									=				self._normalize_and_convert(
									    lowercase_       ,				lowercase_       ,				input_points=lowercase_       ,				input_labels=lowercase_       ,				input_boxes=lowercase_       ,				return_tensors=lowercase_       ,				)

									return encoding_image_processor


				def   A_ (		self    :					Dict       ,				lowercase_    :					Union[str, Any]       ,				lowercase_    :					str       ,				lowercase_    :					Optional[Any]=None       ,				lowercase_    :					Tuple=None       ,				lowercase_    :					Optional[Any]=None       ,				lowercase_    :					Optional[Any]="pt"       ,				):
									if input_points is not None:
														if len(lowercase_					) != len(lowercase_					):
																			snake_case_									=				[
																			    self._normalize_coordinates(self.target_size       ,				lowercase_       ,				original_sizes[0]					) for point in input_points
																			]
														else:
																			snake_case_									=				[
																			    self._normalize_coordinates(self.target_size       ,				lowercase_       ,				lowercase_					)
																			    for point, original_size in zip(lowercase_       ,				lowercase_					)
																			]
														# check that all arrays have the same shape
														if not all(point.shape == input_points[0].shape for point in input_points					):
																			if input_labels is not None:
																								snake_case_			,snake_case_									=				self._pad_points_and_labels(lowercase_       ,				lowercase_					)

														snake_case_									=				np.array(lowercase_					)

									if input_labels is not None:
														snake_case_									=				np.array(lowercase_					)

									if input_boxes is not None:
														if len(lowercase_					) != len(lowercase_					):
																			snake_case_									=				[
																			    self._normalize_coordinates(self.target_size       ,				lowercase_       ,				original_sizes[0]       ,				is_bounding_box=lowercase_					)
																			    for box in input_boxes
																			]
														else:
																			snake_case_									=				[
																			    self._normalize_coordinates(self.target_size       ,				lowercase_       ,				lowercase_       ,				is_bounding_box=lowercase_					)
																			    for box, original_size in zip(lowercase_       ,				lowercase_					)
																			]
														snake_case_									=				np.array(lowercase_					)

									if input_boxes is not None:
														if return_tensors == "pt":
																			snake_case_									=				torch.from_numpy(lowercase_					)
																			# boxes batch size of 1 by default
																			snake_case_									=				input_boxes.unsqueeze(1					) if len(input_boxes.shape					) != 3 else input_boxes
														elif return_tensors == "tf":
																			snake_case_									=				tf.convert_to_tensor(lowercase_					)
																			# boxes batch size of 1 by default
																			snake_case_									=				tf.expand_dims(lowercase_       ,				1					) if len(input_boxes.shape					) != 3 else input_boxes
														encoding_image_processor.update({'''input_boxes''': input_boxes}					)
									if input_points is not None:
														if return_tensors == "pt":
																			snake_case_									=				torch.from_numpy(lowercase_					)
																			# point batch size of 1 by default
																			snake_case_									=				input_points.unsqueeze(1					) if len(input_points.shape					) != 4 else input_points
														elif return_tensors == "tf":
																			snake_case_									=				tf.convert_to_tensor(lowercase_					)
																			# point batch size of 1 by default
																			snake_case_									=				tf.expand_dims(lowercase_       ,				1					) if len(input_points.shape					) != 4 else input_points
														encoding_image_processor.update({'''input_points''': input_points}					)
									if input_labels is not None:
														if return_tensors == "pt":
																			snake_case_									=				torch.from_numpy(lowercase_					)
																			# point batch size of 1 by default
																			snake_case_									=				input_labels.unsqueeze(1					) if len(input_labels.shape					) != 3 else input_labels
														elif return_tensors == "tf":
																			snake_case_									=				tf.convert_to_tensor(lowercase_					)
																			# point batch size of 1 by default
																			snake_case_									=				tf.expand_dims(lowercase_       ,				1					) if len(input_labels.shape					) != 3 else input_labels
														encoding_image_processor.update({'''input_labels''': input_labels}					)

									return encoding_image_processor


				def   A_ (		self    :					Union[str, Any]       ,				lowercase_    :					Any       ,				lowercase_    :					Dict					):
									snake_case_									=				max([point.shape[0] for point in input_points]					)
									snake_case_									=				[]
									for i, point in enumerate(lowercase_					):
														if point.shape[0] != expected_nb_points:
																			snake_case_									=				np.concatenate(
																			    [point, np.zeros((expected_nb_points - point.shape[0], 2)					) + self.point_pad_value]       ,				axis=0					)
																			snake_case_									=				np.append(input_labels[i]       ,				[self.point_pad_value]					)
														processed_input_points.append(lowercase_					)
									snake_case_									=				processed_input_points
									return input_points, input_labels


				def   A_ (		self    :					int       ,				lowercase_    :					int       ,				lowercase_    :					np.ndarray       ,				lowercase_    :					List[str]       ,				lowercase_    :					Tuple=False					):
									snake_case_			,snake_case_									=				original_size
									snake_case_			,snake_case_									=				self.image_processor._get_preprocess_shape(lowercase_       ,				longest_edge=lowercase_					)
									snake_case_									=				deepcopy(lowercase_					).astype(lowercase_					)

									if is_bounding_box:
														snake_case_									=				coords.reshape(-1       ,				2       ,				2					)

									snake_case_									=				coords[..., 0] * (new_w / old_w)
									snake_case_									=				coords[..., 1] * (new_h / old_h)

									if is_bounding_box:
														snake_case_									=				coords.reshape(-1       ,				4					)

									return coords


				def   A_ (		self    :					Any       ,				lowercase_    :					int=None       ,				lowercase_    :					int=None       ,				lowercase_    :					Optional[Any]=None       ,				):
									if input_points is not None:
														if hasattr(lowercase_       ,				'''numpy'''					):  # Checks for TF or Torch tensor
																			snake_case_									=				input_points.numpy().tolist()

														if not isinstance(lowercase_       ,				lowercase_					) or not isinstance(input_points[0]       ,				lowercase_					):
																			raise ValueError('''Input points must be a list of list of floating points.'''					)
														snake_case_									=				[np.array(lowercase_					) for input_point in input_points]
									else:
														snake_case_									=				None

									if input_labels is not None:
														if hasattr(lowercase_       ,				'''numpy'''					):
																			snake_case_									=				input_labels.numpy().tolist()

														if not isinstance(lowercase_       ,				lowercase_					) or not isinstance(input_labels[0]       ,				lowercase_					):
																			raise ValueError('''Input labels must be a list of list integers.'''					)
														snake_case_									=				[np.array(lowercase_					) for label in input_labels]
									else:
														snake_case_									=				None

									if input_boxes is not None:
														if hasattr(lowercase_       ,				'''numpy'''					):
																			snake_case_									=				input_boxes.numpy().tolist()

														if (
														    not isinstance(lowercase_       ,				lowercase_					)
														    or not isinstance(input_boxes[0]       ,				lowercase_					)
														    or not isinstance(input_boxes[0][0]       ,				lowercase_					)
														):
																			raise ValueError('''Input boxes must be a list of list of list of floating points.'''					)
														snake_case_									=				[np.array(lowercase_					).astype(np.floataa					) for box in input_boxes]
									else:
														snake_case_									=				None

									return input_points, input_labels, input_boxes


				@property
				def   A_ (		self    :					Optional[int]					):
									snake_case_									=				self.image_processor.model_input_names
									return list(dict.fromkeys(lowercase_					)					)


				def   A_ (		self    :					Optional[Any]       ,				*lowercase_    :					Optional[Any]       ,				**lowercase_    :					List[Any]					):
									return self.image_processor.post_process_masks(*lowercase_       ,				**lowercase_					)







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


a  :				Dict									=  logging.get_logger(__name__)

a  :				List[str]									=  {
    'Helsinki-NLP/opus-mt-en-de': 'https://huggingface.co/Helsinki-NLP/opus-mt-en-de/resolve/main/config.json',
    # See all Marian models at https://huggingface.co/models?filter=marian
}






class 						a				(   _lowerCamelCase     ):
				snake_case_      =		"marian"
				snake_case_      =		["past_key_values"]
				snake_case_      =		{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}


				def __init__(		self    :					List[Any]       ,				lowercase_    :					Optional[Any]=5_8101       ,				lowercase_    :					Dict=None       ,				lowercase_    :					List[str]=1024       ,				lowercase_    :					Optional[Any]=12       ,				lowercase_    :					int=4096       ,				lowercase_    :					Any=16       ,				lowercase_    :					Optional[int]=12       ,				lowercase_    :					str=4096       ,				lowercase_    :					Union[str, Any]=16       ,				lowercase_    :					Dict=0.0       ,				lowercase_    :					Union[str, Any]=0.0       ,				lowercase_    :					Optional[Any]=True       ,				lowercase_    :					Union[str, Any]=True       ,				lowercase_    :					int="gelu"       ,				lowercase_    :					Dict=1024       ,				lowercase_    :					int=0.1       ,				lowercase_    :					Tuple=0.0       ,				lowercase_    :					Tuple=0.0       ,				lowercase_    :					Tuple=0.02       ,				lowercase_    :					int=5_8100       ,				lowercase_    :					Optional[Any]=False       ,				lowercase_    :					Any=5_8100       ,				lowercase_    :					Optional[int]=0       ,				lowercase_    :					Dict=0       ,				lowercase_    :					List[str]=True       ,				**lowercase_    :					Any       ,				):
									snake_case_									=				vocab_size
									snake_case_									=				decoder_vocab_size or vocab_size
									snake_case_									=				max_position_embeddings
									snake_case_									=				d_model
									snake_case_									=				encoder_ffn_dim
									snake_case_									=				encoder_layers
									snake_case_									=				encoder_attention_heads
									snake_case_									=				decoder_ffn_dim
									snake_case_									=				decoder_layers
									snake_case_									=				decoder_attention_heads
									snake_case_									=				dropout
									snake_case_									=				attention_dropout
									snake_case_									=				activation_dropout
									snake_case_									=				activation_function
									snake_case_									=				init_std
									snake_case_									=				encoder_layerdrop
									snake_case_									=				decoder_layerdrop
									snake_case_									=				use_cache
									snake_case_									=				encoder_layers
									snake_case_									=				scale_embedding  # scale factor will be sqrt(d_model) if True
									snake_case_									=				share_encoder_decoder_embeddings
									super().__init__(
									    pad_token_id=lowercase_       ,				eos_token_id=lowercase_       ,				is_encoder_decoder=lowercase_       ,				decoder_start_token_id=lowercase_       ,				forced_eos_token_id=lowercase_       ,				**lowercase_       ,				)







class 						a				(   _lowerCamelCase     ):


				@property
				# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.inputs
				def   A_ (		self    :					Union[str, Any]					):
									if self.task in ["default", "seq2seq-lm"]:
														snake_case_									=				OrderedDict(
														    [
														        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
														        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
														    ]					)

														if self.use_past:
																			snake_case_									=				{0: '''batch'''}
																			snake_case_									=				{0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
														else:
																			snake_case_									=				{0: '''batch''', 1: '''decoder_sequence'''}
																			snake_case_									=				{0: '''batch''', 1: '''decoder_sequence'''}

														if self.use_past:
																			self.fill_with_past_key_values_(lowercase_       ,				direction='''inputs'''					)
									elif self.task == "causal-lm":
														# TODO: figure this case out.
														snake_case_									=				OrderedDict(
														    [
														        ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
														        ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
														    ]					)
														if self.use_past:
																			snake_case_			,snake_case_									=				self.num_layers
																			for i in range(lowercase_					):
																								snake_case_									=				{0: '''batch''', 2: '''past_sequence + sequence'''}
																								snake_case_									=				{0: '''batch''', 2: '''past_sequence + sequence'''}
									else:
														snake_case_									=				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
				# Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
				def   A_ (		self    :					Dict					):
									if self.task in ["default", "seq2seq-lm"]:
														snake_case_									=				super().outputs
									else:
														snake_case_									=				super(lowercase_       ,				self					).outputs
														if self.use_past:
																			snake_case_			,snake_case_									=				self.num_layers
																			for i in range(lowercase_					):
																								snake_case_									=				{0: '''batch''', 2: '''past_sequence + sequence'''}
																								snake_case_									=				{0: '''batch''', 2: '''past_sequence + sequence'''}
									return common_outputs


				def   A_ (		self    :					Dict       ,				lowercase_    :					PreTrainedTokenizer       ,				lowercase_    :					int = -1       ,				lowercase_    :					int = -1       ,				lowercase_    :					bool = False       ,				lowercase_    :					Optional[TensorType] = None       ,				):
									snake_case_									=				self._generate_dummy_inputs_for_encoder_and_decoder(
									    lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_					)

									# Generate decoder inputs
									snake_case_									=				seq_length if not self.use_past else 1
									snake_case_									=				self._generate_dummy_inputs_for_encoder_and_decoder(
									    lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_					)
									snake_case_									=				{F"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
									snake_case_									=				dict(**lowercase_       ,				**lowercase_					)

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

														snake_case_									=				torch.cat(
														    [common_inputs['''decoder_attention_mask'''], torch.ones(lowercase_       ,				lowercase_					)]       ,				dim=1					)

														snake_case_									=				[]
														# If the number of encoder and decoder layers are present in the model configuration, both are considered
														snake_case_			,snake_case_									=				self.num_layers
														snake_case_									=				min(lowercase_       ,				lowercase_					)
														snake_case_									=				max(lowercase_       ,				lowercase_					) - min_num_layers
														snake_case_									=				'''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

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


				def   A_ (		self    :					Union[str, Any]       ,				lowercase_    :					PreTrainedTokenizer       ,				lowercase_    :					int = -1       ,				lowercase_    :					int = -1       ,				lowercase_    :					bool = False       ,				lowercase_    :					Optional[TensorType] = None       ,				):
									snake_case_									=				self._generate_dummy_inputs_for_encoder_and_decoder(
									    lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_					)

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

														snake_case_									=				common_inputs['''attention_mask'''].dtype
														snake_case_									=				torch.cat(
														    [common_inputs['''attention_mask'''], torch.ones(lowercase_       ,				lowercase_       ,				dtype=lowercase_					)]       ,				dim=1					)
														snake_case_									=				[
														    (torch.zeros(lowercase_					), torch.zeros(lowercase_					)) for _ in range(lowercase_					)
														]
									return common_inputs


				def   A_ (		self    :					List[str]       ,				lowercase_    :					PreTrainedTokenizer       ,				lowercase_    :					int = -1       ,				lowercase_    :					int = -1       ,				lowercase_    :					bool = False       ,				lowercase_    :					Optional[TensorType] = None       ,				):
									# Copied from OnnxConfig.generate_dummy_inputs
									# Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
									# If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
									snake_case_									=				compute_effective_axis_dimension(
									    lowercase_       ,				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_									=				tokenizer.num_special_tokens_to_add(lowercase_					)
									snake_case_									=				compute_effective_axis_dimension(
									    lowercase_       ,				fixed_dimension=OnnxConfig.default_fixed_sequence       ,				num_token_to_add=lowercase_					)

									# Generate dummy inputs according to compute batch and sequence
									snake_case_									=				[''' '''.join([tokenizer.unk_token]					) * seq_length] * batch_size
									snake_case_									=				dict(tokenizer(lowercase_       ,				return_tensors=lowercase_					)					)
									return common_inputs


				def   A_ (		self    :					Any       ,				lowercase_    :					PreTrainedTokenizer       ,				lowercase_    :					int = -1       ,				lowercase_    :					int = -1       ,				lowercase_    :					bool = False       ,				lowercase_    :					Optional[TensorType] = None       ,				):
									if self.task in ["default", "seq2seq-lm"]:
														snake_case_									=				self._generate_dummy_inputs_for_default_and_seqaseq_lm(
														    lowercase_       ,				batch_size=lowercase_       ,				seq_length=lowercase_       ,				is_pair=lowercase_       ,				framework=lowercase_					)

									else:
														snake_case_									=				self._generate_dummy_inputs_for_causal_lm(
														    lowercase_       ,				batch_size=lowercase_       ,				seq_length=lowercase_       ,				is_pair=lowercase_       ,				framework=lowercase_					)

									return common_inputs


				def   A_ (		self    :					Dict       ,				lowercase_    :					List[str]       ,				lowercase_    :					List[str]       ,				lowercase_    :					int       ,				lowercase_    :					List[str]					):
									if self.task in ["default", "seq2seq-lm"]:
														snake_case_									=				super()._flatten_past_key_values_(lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_					)
									else:
														snake_case_									=				super(lowercase_       ,				self					)._flatten_past_key_values_(
														    lowercase_       ,				lowercase_       ,				lowercase_       ,				lowercase_					)


				@property
				def   A_ (		self    :					List[str]					):
									return 1e-4

