




import logging
import os
import threading
import time


try:
					import warnings
except ImportError:
					UpperCamelCase__					    =    None

try:
					import msvcrt
except ImportError:
					UpperCamelCase__					    =    None

try:
					import fcntl
except ImportError:
					UpperCamelCase__					    =    None


# Backward compatibility
# ------------------------------------------------
try:
					TimeoutError
except NameError:
					UpperCamelCase__					    =    OSError


# Data
# ------------------------------------------------
UpperCamelCase__					    =    [
    """Timeout""",
    """BaseFileLock""",
    """WindowsFileLock""",
    """UnixFileLock""",
    """SoftFileLock""",
    """FileLock""",
]

UpperCamelCase__					    =    """3.0.12"""


UpperCamelCase__					    =    None







def 			_a   (		):
					global _logger
					__lowerCAmelCase            = _logger or logging.getLogger(__name__  )
					return _logger
class     a__					(      snake_case__   ):






						def __init__(     self	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = lock_file
											return None






						def __str__(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = f"""The file lock '{self.lock_file}' could not be acquired."""
											return temp






class     a__					:






						def __init__(     self	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = lock
											return None






						def __enter__(     self    ):




											"""simple docstring"""



											return self.lock






						def __exit__(     self	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											self.lock.release()
											return None






class     a__					:






						def __init__(     self	,    _A	,    _A=-1	,    _A=None    ):




											"""simple docstring"""



											__lowerCAmelCase            = max_filename_length if max_filename_length is not None else 2_5_5
											# Hash the filename if it's too long
											__lowerCAmelCase            = self.hash_filename_if_too_long(_A	,    _A    )
											# The path to the lock file.
											__lowerCAmelCase            = lock_file

											# The file descriptor for the *_lock_file* as it is returned by the
											# os.open() function.
											# This file lock is only NOT None, if the object currently holds the
											# lock.
											__lowerCAmelCase            = None

											# The default timeout value.
											__lowerCAmelCase            = timeout

											# We use this lock primarily for the lock counter.
											__lowerCAmelCase            = threading.Lock()

											# The lock counter is used for implementing the nested locking
											# mechanism. Whenever the lock is acquired, the counter is increased and
											# the lock is only released, when this value is 0 again.
											__lowerCAmelCase            = 0
											return None






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return self._lock_file






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return self._timeout






						@timeout.setter
						def        __SCREAMING_SNAKE_CASE(     self	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = float(_A    )
											return None






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											raise NotImplementedError()






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											raise NotImplementedError()






						@property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return self._lock_file_fd is not None






						def        __SCREAMING_SNAKE_CASE(     self	,    _A=None	,    _A=0.05    ):




											"""simple docstring"""



											if timeout is None:
																__lowerCAmelCase            = self.timeout

											# Increment the number right at the beginning.
											# We can still undo it, if something fails.
											with self._thread_lock:
																self._lock_counter += 1

											__lowerCAmelCase            = id(self    )
											__lowerCAmelCase            = self._lock_file
											__lowerCAmelCase            = time.time()
											try:
																while True:
																					with self._thread_lock:
																										if not self.is_locked:
																															logger().debug(f"""Attempting to acquire lock {lock_id} on {lock_filename}"""    )
																															self._acquire()

																					if self.is_locked:
																										logger().debug(f"""Lock {lock_id} acquired on {lock_filename}"""    )
																										break
																					elif timeout >= 0 and time.time() - start_time > timeout:
																										logger().debug(f"""Timeout on acquiring lock {lock_id} on {lock_filename}"""    )
																										raise Timeout(self._lock_file    )
																					else:
																										logger().debug(
																										    f"""Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ..."""    )
																										time.sleep(_A    )
											except:  # noqa
																# Something did go wrong, so decrement the counter.
																with self._thread_lock:
																					__lowerCAmelCase            = max(0	,    self._lock_counter - 1    )

																raise
											return _Acquire_ReturnProxy(lock=self    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A=False    ):




											"""simple docstring"""



											with self._thread_lock:
																if self.is_locked:
																					self._lock_counter -= 1

																					if self._lock_counter == 0 or force:
																										__lowerCAmelCase            = id(self    )
																										__lowerCAmelCase            = self._lock_file

																										logger().debug(f"""Attempting to release lock {lock_id} on {lock_filename}"""    )
																										self._release()
																										__lowerCAmelCase            = 0
																										logger().debug(f"""Lock {lock_id} released on {lock_filename}"""    )

											return None






						def __enter__(     self    ):




											"""simple docstring"""



											self.acquire()
											return self






						def __exit__(     self	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											self.release()
											return None






						def __del__(     self    ):




											"""simple docstring"""



											self.release(force=_A    )
											return None






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = os.path.basename(_A    )
											if len(_A    ) > max_length and max_length > 0:
																__lowerCAmelCase            = os.path.dirname(_A    )
																__lowerCAmelCase            = str(hash(_A    )    )
																__lowerCAmelCase            = filename[: max_length - len(_A    ) - 8] + "..." + hashed_filename + ".lock"
																return os.path.join(_A	,    _A    )
											else:
																return path






class     a__					(      snake_case__   ):






						def __init__(     self	,    _A	,    _A=-1	,    _A=None    ):




											"""simple docstring"""



											from .file_utils import relative_to_absolute_path

											super().__init__(_A	,    timeout=_A	,    max_filename_length=_A    )
											__lowerCAmelCase            = "\\\\?\\" + relative_to_absolute_path(self.lock_file    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = os.O_RDWR | os.O_CREAT | os.O_TRUNC

											try:
																__lowerCAmelCase            = os.open(self._lock_file	,    _A    )
											except OSError:
																pass
											else:
																try:
																					msvcrt.locking(_A	,    msvcrt.LK_NBLCK	,    1    )
																except OSError:
																					os.close(_A    )
																else:
																					__lowerCAmelCase            = fd
											return None






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self._lock_file_fd
											__lowerCAmelCase            = None
											msvcrt.locking(_A	,    msvcrt.LK_UNLCK	,    1    )
											os.close(_A    )

											try:
																os.remove(self._lock_file    )
											# Probably another instance of the application
											# that acquired the file lock.
											except OSError:
																pass
											return None






class     a__					(      snake_case__   ):






						def __init__(     self	,    _A	,    _A=-1	,    _A=None    ):




											"""simple docstring"""



											__lowerCAmelCase            = os.statvfs(os.path.dirname(_A    )    ).f_namemax
											super().__init__(_A	,    timeout=_A	,    max_filename_length=_A    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = os.O_RDWR | os.O_CREAT | os.O_TRUNC
											__lowerCAmelCase            = os.open(self._lock_file	,    _A    )

											try:
																fcntl.flock(_A	,    fcntl.LOCK_EX | fcntl.LOCK_NB    )
											except OSError:
																os.close(_A    )
											else:
																__lowerCAmelCase            = fd
											return None






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self._lock_file_fd
											__lowerCAmelCase            = None
											fcntl.flock(_A	,    fcntl.LOCK_UN    )
											os.close(_A    )
											return None






class     a__					(      snake_case__   ):






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
											try:
																__lowerCAmelCase            = os.open(self._lock_file	,    _A    )
											except OSError:
																pass
											else:
																__lowerCAmelCase            = fd
											return None






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											os.close(self._lock_file_fd    )
											__lowerCAmelCase            = None

											try:
																os.remove(self._lock_file    )
											# The file is already deleted and that's what we want.
											except OSError:
																pass
											return None






UpperCamelCase__					    =    None

if msvcrt:
					UpperCamelCase__					    =    WindowsFileLock
elif fcntl:
					UpperCamelCase__					    =    UnixFileLock
else:
					UpperCamelCase__					    =    SoftFileLock

					if warnings is not None:
										warnings.warn("""only soft file lock is available""")










from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel
@require_tf
class     a__					:
						_a							:					List[str]      =					MBartConfig
						_a							:					Union[str, Any]      =					{}
						_a							:					Tuple      =					"""gelu"""






						def __init__(     self	,    _A	,    _A=1_3	,    _A=7	,    _A=True	,    _A=False	,    _A=9_9	,    _A=3_2	,    _A=2	,    _A=4	,    _A=3_7	,    _A=0.1	,    _A=0.1	,    _A=2_0	,    _A=2	,    _A=1	,    _A=0	,    ):




											"""simple docstring"""



											__lowerCAmelCase            = parent
											__lowerCAmelCase            = batch_size
											__lowerCAmelCase            = seq_length
											__lowerCAmelCase            = is_training
											__lowerCAmelCase            = use_labels
											__lowerCAmelCase            = vocab_size
											__lowerCAmelCase            = hidden_size
											__lowerCAmelCase            = num_hidden_layers
											__lowerCAmelCase            = num_attention_heads
											__lowerCAmelCase            = intermediate_size
											__lowerCAmelCase            = hidden_dropout_prob
											__lowerCAmelCase            = attention_probs_dropout_prob
											__lowerCAmelCase            = max_position_embeddings
											__lowerCAmelCase            = eos_token_id
											__lowerCAmelCase            = pad_token_id
											__lowerCAmelCase            = bos_token_id






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = ids_tensor([self.batch_size, self.seq_length - 1]	,    self.vocab_size    )
											__lowerCAmelCase            = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size    )	,    1    )
											__lowerCAmelCase            = tf.concat([input_ids, eos_tensor]	,    axis=1    )

											__lowerCAmelCase            = ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size    )

											__lowerCAmelCase            = self.config_cls(
											    vocab_size=self.vocab_size	,    d_model=self.hidden_size	,    encoder_layers=self.num_hidden_layers	,    decoder_layers=self.num_hidden_layers	,    encoder_attention_heads=self.num_attention_heads	,    decoder_attention_heads=self.num_attention_heads	,    encoder_ffn_dim=self.intermediate_size	,    decoder_ffn_dim=self.intermediate_size	,    dropout=self.hidden_dropout_prob	,    attention_dropout=self.attention_probs_dropout_prob	,    max_position_embeddings=self.max_position_embeddings	,    eos_token_ids=[2]	,    bos_token_id=self.bos_token_id	,    pad_token_id=self.pad_token_id	,    decoder_start_token_id=self.pad_token_id	,    **self.config_updates	,    )
											__lowerCAmelCase            = prepare_mbart_inputs_dict(_A	,    _A	,    _A    )
											return config, inputs_dict






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = TFMBartModel(config=_A    ).get_decoder()
											__lowerCAmelCase            = inputs_dict["input_ids"]

											__lowerCAmelCase            = input_ids[:1, :]
											__lowerCAmelCase            = inputs_dict["attention_mask"][:1, :]
											__lowerCAmelCase            = inputs_dict["head_mask"]
											__lowerCAmelCase            = 1

											# first forward pass
											__lowerCAmelCase            = model(_A	,    attention_mask=_A	,    head_mask=_A	,    use_cache=_A    )

											__lowerCAmelCase     ,							__lowerCAmelCase            = outputs.to_tuple()
											__lowerCAmelCase            = past_key_values[1]







def 			_a   (		SCREAMING_SNAKE_CASE_							:       Optional[int]   ,   SCREAMING_SNAKE_CASE_							:       List[str]   ,   SCREAMING_SNAKE_CASE_							:       Tuple   ,   SCREAMING_SNAKE_CASE_							:       Any=None   ,   SCREAMING_SNAKE_CASE_							:       int=None   ,   SCREAMING_SNAKE_CASE_							:       List[Any]=None   ,   SCREAMING_SNAKE_CASE_							:       List[Any]=None   ,   SCREAMING_SNAKE_CASE_							:       Optional[Any]=None   ,   ):
					if attention_mask is None:
										__lowerCAmelCase            = tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE_   ,   config.pad_token_id  )   ,   tf.inta  )
					if decoder_attention_mask is None:
										__lowerCAmelCase            = tf.concat(
										    [
										        tf.ones(decoder_input_ids[:, :1].shape   ,   dtype=tf.inta  ),
										        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]   ,   config.pad_token_id  )   ,   tf.inta  ),
										    ]   ,   axis=-1   ,   )
					if head_mask is None:
										__lowerCAmelCase            = tf.ones((config.encoder_layers, config.encoder_attention_heads)  )
					if decoder_head_mask is None:
										__lowerCAmelCase            = tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
					if cross_attn_head_mask is None:
										__lowerCAmelCase            = tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
					return {
					    "input_ids": input_ids,
					    "decoder_input_ids": decoder_input_ids,
					    "attention_mask": attention_mask,
					    "decoder_attention_mask": decoder_attention_mask,
					    "head_mask": head_mask,
					    "decoder_head_mask": decoder_head_mask,
					    "cross_attn_head_mask": cross_attn_head_mask,
					}
@require_tf
class     a__					(      snake_case__				,       snake_case__				,       unittest.TestCase   ):
						_a							:					Optional[Any]      =					(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
						_a							:					Any      =					(TFMBartForConditionalGeneration,) if is_tf_available() else ()
						_a							:					Tuple      =					(
						    {
						        """conversational""": TFMBartForConditionalGeneration,
						        """feature-extraction""": TFMBartModel,
						        """summarization""": TFMBartForConditionalGeneration,
						        """text2text-generation""": TFMBartForConditionalGeneration,
						        """translation""": TFMBartForConditionalGeneration,
						    }
						    if is_tf_available()
						    else {}
						)
						_a							:					Optional[int]      =					True
						_a							:					Optional[int]      =					False
						_a							:					List[Any]      =					False






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
																# Exception encountered when calling layer '...'
																return True

											return False






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = TFMBartModelTester(self    )
											__lowerCAmelCase            = ConfigTester(self	,    config_class=_A    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.check_decoder_model_past_large_inputs(*_A    )






@require_sentencepiece
@require_tokenizers
@require_tf
class     a__					(      unittest.TestCase   ):
						_a							:					Any      =					[
						    """ UN Chief Says There Is No Military Solution in Syria""",
						]
						_a							:					Optional[Any]      =					[
						    """Şeful ONU declară că nu există o soluţie militară în Siria""",
						]
						_a							:					Any      =					"""facebook/mbart-large-en-ro"""






						@cached_property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return AutoTokenizer.from_pretrained(self.model_name    )






						@cached_property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name    )
											return model






						def        __SCREAMING_SNAKE_CASE(     self	,    **_A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.translate_src_text(**_A    )
											self.assertListEqual(self.expected_text	,    _A    )






						def        __SCREAMING_SNAKE_CASE(     self	,    **_A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.tokenizer(self.src_text	,    **_A	,    return_tensors="tf"    )
											__lowerCAmelCase            = self.model.generate(
											    model_inputs.input_ids	,    attention_mask=model_inputs.attention_mask	,    num_beams=2    )
											__lowerCAmelCase            = self.tokenizer.batch_decode(_A	,    skip_special_tokens=_A    )
											return generated_words






						@slow
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											self._assert_generated_batch_equal_expected()




