



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							 =					logging.get_logger(__name__)

UpperCAmelCase							 =					{
    """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  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






							UpperCamelCase__				:      Optional[int]         =					"""marian"""
							UpperCamelCase__				:      Optional[int]         =					["""past_key_values"""]
							UpperCamelCase__				:      Union[str, Any]         =					{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


							def __init__(							self  ,  a_=58_101  ,  a_=None  ,  a_=1_024  ,  a_=12  ,  a_=4_096  ,  a_=16  ,  a_=12  ,  a_=4_096  ,  a_=16  ,  a_=0.0  ,  a_=0.0  ,  a_=True  ,  a_=True  ,  a_="gelu"  ,  a_=1_024  ,  a_=0.1  ,  a_=0.0  ,  a_=0.0  ,  a_=0.02  ,  a_=58_100  ,  a_=False  ,  a_=58_100  ,  a_=0  ,  a_=0  ,  a_=True  ,  **a_  ,  ):
											a__				      =		vocab_size
											a__				      =		decoder_vocab_size or vocab_size
											a__				      =		max_position_embeddings
											a__				      =		d_model
											a__				      =		encoder_ffn_dim
											a__				      =		encoder_layers
											a__				      =		encoder_attention_heads
											a__				      =		decoder_ffn_dim
											a__				      =		decoder_layers
											a__				      =		decoder_attention_heads
											a__				      =		dropout
											a__				      =		attention_dropout
											a__				      =		activation_dropout
											a__				      =		activation_function
											a__				      =		init_std
											a__				      =		encoder_layerdrop
											a__				      =		decoder_layerdrop
											a__				      =		use_cache
											a__				      =		encoder_layers
											a__				      =		scale_embedding  # scale factor will be sqrt(d_model) if True
											a__				      =		share_encoder_decoder_embeddings
											super().__init__(
											    pad_token_id=a_  ,  eos_token_id=a_  ,  is_encoder_decoder=a_  ,  decoder_start_token_id=a_  ,  forced_eos_token_id=a_  ,  **a_  ,  )




class  __snake_case      (   SCREAMING_SNAKE_CASE):







							'''simple docstring'''






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

															if self.use_past:
																			a__				      =		{0: """batch"""}
																			a__				      =		{0: """batch""", 1: """past_decoder_sequence + sequence"""}
															else:
																			a__				      =		{0: """batch""", 1: """decoder_sequence"""}
																			a__				      =		{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.
															a__				      =		OrderedDict(
															    [
															        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
															        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
															    ]		)
															if self.use_past:
																			a__      ,			a__				      =		self.num_layers
																			for i in range(a_		):
																							a__				      =		{0: """batch""", 2: """past_sequence + sequence"""}
																							a__				      =		{0: """batch""", 2: """past_sequence + sequence"""}
											else:
															a__				      =		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		):
											if self.task in ["default", "seq2seq-lm"]:
															a__				      =		super().outputs
											else:
															a__				      =		super(a_  ,  self		).outputs
															if self.use_past:
																			a__      ,			a__				      =		self.num_layers
																			for i in range(a_		):
																							a__				      =		{0: """batch""", 2: """past_sequence + sequence"""}
																							a__				      =		{0: """batch""", 2: """past_sequence + sequence"""}
											return common_outputs


							def _a      (							self  ,  a_  ,  a_ = -1  ,  a_ = -1  ,  a_ = False  ,  a_ = None  ,  ):
											a__				      =		self._generate_dummy_inputs_for_encoder_and_decoder(
											    a_  ,  a_  ,  a_  ,  a_  ,  a_		)

											# Generate decoder inputs
											a__				      =		seq_length if not self.use_past else 1
											a__				      =		self._generate_dummy_inputs_for_encoder_and_decoder(
											    a_  ,  a_  ,  a_  ,  a_  ,  a_		)
											a__				      =		{F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
											a__				      =		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
															a__      ,			a__				      =		common_inputs["""input_ids"""].shape
															a__				      =		common_inputs["""decoder_input_ids"""].shape[1]
															a__      ,			a__				      =		self.num_attention_heads
															a__				      =		(
															    batch,
															    num_encoder_attention_heads,
															    encoder_seq_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)
															a__				      =		decoder_seq_length + 3
															a__				      =		(
															    batch,
															    num_decoder_attention_heads,
															    decoder_past_length,
															    self._config.hidden_size // num_decoder_attention_heads,
															)

															a__				      =		torch.cat(
															    [common_inputs["""decoder_attention_mask"""], torch.ones(a_  ,  a_		)]  ,  dim=1		)

															a__				      =		[]
															# If the number of encoder and decoder layers are present in the model configuration, both are considered
															a__      ,			a__				      =		self.num_layers
															a__				      =		min(a_  ,  a_		)
															a__				      =		max(a_  ,  a_		) - min_num_layers
															a__				      =		"""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.
															a__				      =		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  ,  ):
											a__				      =		self._generate_dummy_inputs_for_encoder_and_decoder(
											    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
															a__      ,			a__				      =		common_inputs["""input_ids"""].shape
															# Not using the same length for past_key_values
															a__				      =		seqlen + 2
															a__      ,			a__				      =		self.num_layers
															a__      ,			a__				      =		self.num_attention_heads
															a__				      =		(
															    batch,
															    num_encoder_attention_heads,
															    past_key_values_length,
															    self._config.hidden_size // num_encoder_attention_heads,
															)

															a__				      =		common_inputs["""attention_mask"""].dtype
															a__				      =		torch.cat(
															    [common_inputs["""attention_mask"""], torch.ones(a_  ,  a_  ,  dtype=a_		)]  ,  dim=1		)
															a__				      =		[
															    (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  ,  ):
											# 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
											a__				      =		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
											a__				      =		tokenizer.num_special_tokens_to_add(a_		)
											a__				      =		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
											a__				      =		[""" """.join([tokenizer.unk_token]		) * seq_length] * batch_size
											a__				      =		dict(tokenizer(a_  ,  return_tensors=a_		)		)
											return common_inputs


							def _a      (							self  ,  a_  ,  a_ = -1  ,  a_ = -1  ,  a_ = False  ,  a_ = None  ,  ):
											if self.task in ["default", "seq2seq-lm"]:
															a__				      =		self._generate_dummy_inputs_for_default_and_seqaseq_lm(
															    a_  ,  batch_size=a_  ,  seq_length=a_  ,  is_pair=a_  ,  framework=a_		)

											else:
															a__				      =		self._generate_dummy_inputs_for_causal_lm(
															    a_  ,  batch_size=a_  ,  seq_length=a_  ,  is_pair=a_  ,  framework=a_		)

											return common_inputs


							def _a      (							self  ,  a_  ,  a_  ,  a_  ,  a_		):
											if self.task in ["default", "seq2seq-lm"]:
															a__				      =		super()._flatten_past_key_values_(a_  ,  a_  ,  a_  ,  a_		)
											else:
															a__				      =		super(a_  ,  self		)._flatten_past_key_values_(
															    a_  ,  a_  ,  a_  ,  a_		)







							@property
							def _a      (							self		):
											return 1E-4






import unittest

from transformers import (
    MODEL_FOR_OBJECT_DETECTION_MAPPING,
    AutoFeatureExtractor,
    AutoModelForObjectDetection,
    ObjectDetectionPipeline,
    is_vision_available,
    pipeline,
)
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_pytesseract,
    require_tf,
    require_timm,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:

	class  __snake_case      :







								'''simple docstring'''






								@staticmethod
								def _a      (							*a_  ,  **a_		):
												pass

@is_pipeline_test
@require_vision
@require_timm
@require_torch
class  __snake_case      (   unittest.TestCase):







							'''simple docstring'''






							UpperCamelCase__				:      Dict         =					MODEL_FOR_OBJECT_DETECTION_MAPPING


							def _a      (							self  ,  a_  ,  a_  ,  a_		):
											a__				      =		ObjectDetectionPipeline(model=a_  ,  image_processor=a_		)
											return object_detector, ["./tests/fixtures/tests_samples/COCO/000000039769.png"]


							def _a      (							self  ,  a_  ,  a_		):
											a__				      =		object_detector("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  ,  threshold=0.0		)

											self.assertGreater(len(a_		)  ,  0		)
											for detected_object in outputs:
															self.assertEqual(
															    a_  ,  {
															        """score""": ANY(a_		),
															        """label""": ANY(a_		),
															        """box""": {"""xmin""": ANY(a_		), """ymin""": ANY(a_		), """xmax""": ANY(a_		), """ymax""": ANY(a_		)},
															    }  ,  )

											import datasets

											a__				      =		datasets.load_dataset("""hf-internal-testing/fixtures_image_utils"""  ,  """image"""  ,  split="""test"""		)

											a__				      =		[
											    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""		),
											    """http://images.cocodataset.org/val2017/000000039769.jpg""",
											    # RGBA
											    dataset[0]["""file"""],
											    # LA
											    dataset[1]["""file"""],
											    # L
											    dataset[2]["""file"""],
											]
											a__				      =		object_detector(a_  ,  threshold=0.0		)

											self.assertEqual(len(a_		)  ,  len(a_		)		)
											for outputs in batch_outputs:
															self.assertGreater(len(a_		)  ,  0		)
															for detected_object in outputs:
																			self.assertEqual(
																			    a_  ,  {
																			        """score""": ANY(a_		),
																			        """label""": ANY(a_		),
																			        """box""": {"""xmin""": ANY(a_		), """ymin""": ANY(a_		), """xmax""": ANY(a_		), """ymax""": ANY(a_		)},
																			    }  ,  )


							@require_tf
							@unittest.skip("""Object detection not implemented in TF"""		)
							def _a      (							self		):
											pass


							@require_torch
							def _a      (							self		):
											a__				      =		"""hf-internal-testing/tiny-detr-mobilenetsv3"""

											a__				      =		AutoModelForObjectDetection.from_pretrained(a_		)
											a__				      =		AutoFeatureExtractor.from_pretrained(a_		)
											a__				      =		ObjectDetectionPipeline(model=a_  ,  feature_extractor=a_		)

											a__				      =		object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""  ,  threshold=0.0		)

											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
											        {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
											    ]  ,  )

											a__				      =		object_detector(
											    [
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											    ]  ,  threshold=0.0  ,  )

											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        [
											            {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
											            {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
											        ],
											        [
											            {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
											            {"""score""": 0.3_376, """label""": """LABEL_0""", """box""": {"""xmin""": 159, """ymin""": 120, """xmax""": 480, """ymax""": 359}},
											        ],
											    ]  ,  )


							@require_torch
							@slow
							def _a      (							self		):
											a__				      =		"""facebook/detr-resnet-50"""

											a__				      =		AutoModelForObjectDetection.from_pretrained(a_		)
											a__				      =		AutoFeatureExtractor.from_pretrained(a_		)
											a__				      =		ObjectDetectionPipeline(model=a_  ,  feature_extractor=a_		)

											a__				      =		object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""		)
											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
											        {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
											        {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
											        {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											        {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											    ]  ,  )

											a__				      =		object_detector(
											    [
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											    ]		)
											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        [
											            {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
											            {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
											            {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
											            {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											            {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											        ],
											        [
											            {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
											            {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
											            {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
											            {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											            {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											        ],
											    ]  ,  )


							@require_torch
							@slow
							def _a      (							self		):
											a__				      =		"""facebook/detr-resnet-50"""

											a__				      =		pipeline("""object-detection"""  ,  model=a_		)

											a__				      =		object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""		)
											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
											        {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
											        {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
											        {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											        {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											    ]  ,  )

											a__				      =		object_detector(
											    [
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											        """http://images.cocodataset.org/val2017/000000039769.jpg""",
											    ]		)
											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        [
											            {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
											            {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
											            {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
											            {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											            {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											        ],
											        [
											            {"""score""": 0.9_982, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 70, """xmax""": 175, """ymax""": 117}},
											            {"""score""": 0.9_960, """label""": """remote""", """box""": {"""xmin""": 333, """ymin""": 72, """xmax""": 368, """ymax""": 187}},
											            {"""score""": 0.9_955, """label""": """couch""", """box""": {"""xmin""": 0, """ymin""": 1, """xmax""": 639, """ymax""": 473}},
											            {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											            {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											        ],
											    ]  ,  )


							@require_torch
							@slow
							def _a      (							self		):
											a__				      =		0.9_985
											a__				      =		"""facebook/detr-resnet-50"""

											a__				      =		pipeline("""object-detection"""  ,  model=a_		)

											a__				      =		object_detector("""http://images.cocodataset.org/val2017/000000039769.jpg"""  ,  threshold=a_		)
											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        {"""score""": 0.9_988, """label""": """cat""", """box""": {"""xmin""": 13, """ymin""": 52, """xmax""": 314, """ymax""": 470}},
											        {"""score""": 0.9_987, """label""": """cat""", """box""": {"""xmin""": 345, """ymin""": 23, """xmax""": 640, """ymax""": 368}},
											    ]  ,  )







							@require_torch
							@require_pytesseract
							@slow
							def _a      (							self		):
											a__				      =		"""Narsil/layoutlmv3-finetuned-funsd"""
											a__				      =		0.9_993

											a__				      =		pipeline("""object-detection"""  ,  model=a_  ,  threshold=a_		)

											a__				      =		object_detector(
											    """https://huggingface.co/spaces/impira/docquery/resolve/2359223c1837a7587402bda0f2643382a6eefeab/invoice.png"""		)
											self.assertEqual(
											    nested_simplify(a_  ,  decimals=4		)  ,  [
											        {"""score""": 0.9_993, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}},
											        {"""score""": 0.9_993, """label""": """I-ANSWER""", """box""": {"""xmin""": 294, """ymin""": 254, """xmax""": 343, """ymax""": 264}},
											    ]  ,  )

