






import logging
import os
import threading
import time


try:
	import warnings
except ImportError:
	a_							=			None

try:
	import msvcrt
except ImportError:
	a_							=			None

try:
	import fcntl
except ImportError:
	a_							=			None


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


# Data
# ------------------------------------------------
a_							=			[
    '''Timeout''',
    '''BaseFileLock''',
    '''WindowsFileLock''',
    '''UnixFileLock''',
    '''SoftFileLock''',
    '''FileLock''',
]

a_							=			'''3.0.12'''


a_							=			None


def 				_a     (   )       ->      Any:
						"""simple docstring"""







						global _logger
						lowerCAmelCase__       					=  _logger or logging.getLogger(__name__      )
						return _logger


class    lowercase__   (	_UpperCAmelCase		):

	def __init__(							self						,			__UpperCAmelCase				)->    List[str]:







							'''simple docstring'''





							lowerCAmelCase__       					=  lock_file
							return None


	def __str__(							self				)->    Any:







							'''simple docstring'''





							lowerCAmelCase__       					=  F"The file lock \'{self.lock_file}\' could not be acquired."
							return temp





class    lowercase__   :

	def __init__(							self						,			__UpperCAmelCase				)->    str:







							'''simple docstring'''





							lowerCAmelCase__       					=  lock
							return None

	def __enter__(							self				)->    Dict:







							'''simple docstring'''





							return self.lock


	def __exit__(							self						,			__UpperCAmelCase						,			__UpperCAmelCase						,			__UpperCAmelCase				)->    Any:







							'''simple docstring'''





							self.lock.release()
							return None





class    lowercase__   :

	def __init__(							self						,			__UpperCAmelCase						,			__UpperCAmelCase=-1						,			__UpperCAmelCase=None				)->    Union[str, Any]:







							'''simple docstring'''





							lowerCAmelCase__       					=  max_filename_length if max_filename_length is not None else 255
							# Hash the filename if it's too long
							lowerCAmelCase__       					=  self.hash_filename_if_too_long(__lowerCAmelCase						,			__lowerCAmelCase				)
							# 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 						UpperCAmelCase     (							self				)->    List[Any]:







							'''simple docstring'''





							return self._lock_file

	@property
	def 						UpperCAmelCase     (							self				)->    int:







							'''simple docstring'''





							return self._timeout

	@timeout.setter
	def 						UpperCAmelCase     (							self						,			__UpperCAmelCase				)->    Any:







							'''simple docstring'''





							lowerCAmelCase__       					=  float(__lowerCAmelCase				)
							return None

	def 						UpperCAmelCase     (							self				)->    int:







							'''simple docstring'''





							raise NotImplementedError()

	def 						UpperCAmelCase     (							self				)->    Union[str, Any]:







							'''simple docstring'''





							raise NotImplementedError()

	@property
	def 						UpperCAmelCase     (							self				)->    List[str]:







							'''simple docstring'''





							return self._lock_file_fd is not None

	def 						UpperCAmelCase     (							self						,			__UpperCAmelCase=None						,			__UpperCAmelCase=0.05				)->    List[str]:







							'''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(__lowerCAmelCase				)
							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 						UpperCAmelCase     (							self						,			__UpperCAmelCase=False				)->    Tuple:







							'''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				)->    Union[str, Any]:







							'''simple docstring'''





							self.acquire()
							return self

	def __exit__(							self						,			__UpperCAmelCase						,			__UpperCAmelCase						,			__UpperCAmelCase				)->    Any:







							'''simple docstring'''





							self.release()
							return None

	def __del__(							self				)->    Union[str, Any]:







							'''simple docstring'''





							self.release(force=__lowerCAmelCase				)
							return None


	def 						UpperCAmelCase     (							self						,			__UpperCAmelCase						,			__UpperCAmelCase				)->    str:







							'''simple docstring'''





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





class    lowercase__   (	_UpperCAmelCase		):

	def __init__(							self						,			__UpperCAmelCase						,			__UpperCAmelCase=-1						,			__UpperCAmelCase=None				)->    List[Any]:







							'''simple docstring'''





							from .file_utils import relative_to_absolute_path

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

	def 						UpperCAmelCase     (							self				)->    Optional[int]:







							'''simple docstring'''





							lowerCAmelCase__       					=  os.O_RDWR | os.O_CREAT | os.O_TRUNC

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


	def 						UpperCAmelCase     (							self				)->    Optional[int]:







							'''simple docstring'''





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

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





class    lowercase__   (	_UpperCAmelCase		):

	def __init__(							self						,			__UpperCAmelCase						,			__UpperCAmelCase=-1						,			__UpperCAmelCase=None				)->    Union[str, Any]:







							'''simple docstring'''





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

	def 						UpperCAmelCase     (							self				)->    Optional[Any]:







							'''simple docstring'''





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

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


	def 						UpperCAmelCase     (							self				)->    Optional[Any]:







							'''simple docstring'''





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





class    lowercase__   (	_UpperCAmelCase		):

	def 						UpperCAmelCase     (							self				)->    str:







							'''simple docstring'''





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


	def 						UpperCAmelCase     (							self				)->    Tuple:







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





a_							=			None

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

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













import unittest

import numpy as np

from transformers import AlbertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
					import jax.numpy as jnp

					from transformers.models.albert.modeling_flax_albert import (
					    FlaxAlbertForMaskedLM,
					    FlaxAlbertForMultipleChoice,
					    FlaxAlbertForPreTraining,
					    FlaxAlbertForQuestionAnswering,
					    FlaxAlbertForSequenceClassification,
					    FlaxAlbertForTokenClassification,
					    FlaxAlbertModel,
					)


class   A			(unittest.TestCase    ):
			'''simple docstring'''




			def __init__(			self   :    List[str]							,	__lowerCAmelCase   :    int							,	__lowerCAmelCase   :    List[str]=13							,	__lowerCAmelCase   :    int=7							,	__lowerCAmelCase   :    Tuple=True							,	__lowerCAmelCase   :    int=True							,	__lowerCAmelCase   :    Dict=True							,	__lowerCAmelCase   :    Union[str, Any]=True							,	__lowerCAmelCase   :    List[Any]=99							,	__lowerCAmelCase   :    Optional[Any]=32							,	__lowerCAmelCase   :    Optional[Any]=5							,	__lowerCAmelCase   :    Tuple=4							,	__lowerCAmelCase   :    Any=37							,	__lowerCAmelCase   :    str="gelu"							,	__lowerCAmelCase   :    Optional[Any]=0.1							,	__lowerCAmelCase   :    Union[str, Any]=0.1							,	__lowerCAmelCase   :    List[str]=5_12							,	__lowerCAmelCase   :    int=16							,	__lowerCAmelCase   :    Optional[int]=2							,	__lowerCAmelCase   :    List[Any]=0.0_2							,	__lowerCAmelCase   :    Tuple=4							,	)	->	Dict:







					"""simple docstring"""






					A__        =   parent
					A__        =   batch_size
					A__        =   seq_length
					A__        =   is_training
					A__        =   use_attention_mask
					A__        =   use_token_type_ids
					A__        =   use_labels
					A__        =   vocab_size
					A__        =   hidden_size
					A__        =   num_hidden_layers
					A__        =   num_attention_heads
					A__        =   intermediate_size
					A__        =   hidden_act
					A__        =   hidden_dropout_prob
					A__        =   attention_probs_dropout_prob
					A__        =   max_position_embeddings
					A__        =   type_vocab_size
					A__        =   type_sequence_label_size
					A__        =   initializer_range
					A__        =   num_choices






			def    a_       (			self   :    Any   )	->	str:







					"""simple docstring"""






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

					A__        =   None
					if self.use_attention_mask:
							A__        =   random_attention_mask([self.batch_size, self.seq_length]   )

					A__        =   None
					if self.use_token_type_ids:
							A__        =   ids_tensor([self.batch_size, self.seq_length]							,	self.type_vocab_size   )

					A__        =   AlbertConfig(
					    vocab_size=self.vocab_size							,	hidden_size=self.hidden_size							,	num_hidden_layers=self.num_hidden_layers							,	num_attention_heads=self.num_attention_heads							,	intermediate_size=self.intermediate_size							,	hidden_act=self.hidden_act							,	hidden_dropout_prob=self.hidden_dropout_prob							,	attention_probs_dropout_prob=self.attention_probs_dropout_prob							,	max_position_embeddings=self.max_position_embeddings							,	type_vocab_size=self.type_vocab_size							,	is_decoder=__lowerCAmelCase							,	initializer_range=self.initializer_range							,	)

					return config, input_ids, token_type_ids, attention_mask







			def    a_       (			self   :    Optional[int]   )	->	Optional[int]:







					"""simple docstring"""






					A__        =   self.prepare_config_and_inputs()
					A__      ,			A__      ,			A__      ,			A__        =   config_and_inputs
					A__        =   {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
					return config, inputs_dict


@require_flax
class   A			(SCREAMING_SNAKE_CASE   ,     unittest.TestCase    ):
			'''simple docstring'''




			__lowerCamelCase	:       str							=    (
			    (
			        FlaxAlbertModel,
			        FlaxAlbertForPreTraining,
			        FlaxAlbertForMaskedLM,
			        FlaxAlbertForMultipleChoice,
			        FlaxAlbertForQuestionAnswering,
			        FlaxAlbertForSequenceClassification,
			        FlaxAlbertForTokenClassification,
			        FlaxAlbertForQuestionAnswering,
			    )
			    if is_flax_available()
			    else ()
			)






			def    a_       (			self   :    str   )	->	Optional[int]:







					"""simple docstring"""






					A__        =   FlaxAlbertModelTester(self   )







			@slow
			def    a_       (			self   :    int   )	->	Tuple:







					"""simple docstring"""






					for model_class_name in self.all_model_classes:
							A__        =   model_class_name.from_pretrained("""albert-base-v2"""   )
							A__        =   model(np.ones((1, 1)   )   )
							self.assertIsNotNone(__lowerCAmelCase   )


@require_flax
class   A			(unittest.TestCase    ):
			'''simple docstring'''




			@slow
			def    a_       (			self   :    Dict   )	->	List[Any]:







					"""simple docstring"""






					A__        =   FlaxAlbertModel.from_pretrained("""albert-base-v2"""   )
					A__        =   np.array([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]   )
					A__        =   np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
					A__        =   model(__lowerCAmelCase							,	attention_mask=__lowerCAmelCase   )[0]
					A__        =   (1, 11, 7_68)
					self.assertEqual(output.shape							,	__lowerCAmelCase   )
					A__        =   np.array(
					    [[[-0.6_5_1_3, 1.5_0_3_5, -0.2_7_6_6], [-0.6_5_1_5, 1.5_0_4_6, -0.2_7_8_0], [-0.6_5_1_2, 1.5_0_4_9, -0.2_7_8_4]]]   )

					self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]							,	__lowerCAmelCase							,	atol=1e-4   )   )



