import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


__a  :					List[Any]          =  namedtuple("""ExampleNamedTuple""", """a b c""")




class        _UpperCamelCase				(						unittest.TestCase			):


			"""simple docstring"""






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

							'''simple docstring'''


							__lowercase			      =							torch.randn(5				,       2						)
							__lowercase			      =							torch.device('''cuda'''						) if torch.cuda.is_available() else torch.device('''cpu'''						)

							__lowercase			      =							send_to_device(__SCREAMING_SNAKE_CASE				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta.cpu()				,       __SCREAMING_SNAKE_CASE						)						)

							__lowercase			      =							send_to_device((tensor, [tensor, tensor], 1)				,       __SCREAMING_SNAKE_CASE						)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta[0].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertIsInstance(resulta[1]				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta[1][0].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertTrue(torch.equal(resulta[1][1].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertEqual(resulta[2]				,       1						)

							__lowercase			      =							send_to_device({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}				,       __SCREAMING_SNAKE_CASE						)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta['''a'''].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertIsInstance(resulta['''b''']				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta['''b'''][0].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertTrue(torch.equal(resulta['''b'''][1].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertEqual(resulta['''c''']				,       1						)

							__lowercase			      =							send_to_device(ExampleNamedTuple(a=__SCREAMING_SNAKE_CASE				,       b=[tensor, tensor]				,       c=1						)				,       __SCREAMING_SNAKE_CASE						)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta.a.cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertIsInstance(resulta.b				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta.b[0].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertTrue(torch.equal(resulta.b[1].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertEqual(resulta.c				,       1						)

							__lowercase			      =							send_to_device(UserDict({'''a''': tensor, '''b''': [tensor, tensor], '''c''': 1}						)				,       __SCREAMING_SNAKE_CASE						)
							self.assertIsInstance(__SCREAMING_SNAKE_CASE				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta['''a'''].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertIsInstance(resulta['''b''']				,       __SCREAMING_SNAKE_CASE						)
							self.assertTrue(torch.equal(resulta['''b'''][0].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertTrue(torch.equal(resulta['''b'''][1].cpu()				,       __SCREAMING_SNAKE_CASE						)						)
							self.assertEqual(resulta['''c''']				,       1						)





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

							'''simple docstring'''


							with self.assertRaises(__SCREAMING_SNAKE_CASE						) as cm:
											__lowercase			      =							recursively_apply(torch.tensor				,       (torch.tensor(1						), 1)				,       error_on_other_type=__SCREAMING_SNAKE_CASE						)
							self.assertEqual(
							    str(cm.exception						)				,       '''Unsupported types (<class \'int\'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed.'''				,       )





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Tuple:

							'''simple docstring'''


							__lowercase			      =							torch.tensor([1, 2, 3, 4, 5]						)
							self.assertEqual(listify(__SCREAMING_SNAKE_CASE						)				,       [1, 2, 3, 4, 5]						)

							__lowercase			      =							torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]						)
							self.assertEqual(listify(__SCREAMING_SNAKE_CASE						)				,       [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]						)

							__lowercase			      =							torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]						)
							self.assertEqual(
							    listify(__SCREAMING_SNAKE_CASE						)				,       [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]						)





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

							'''simple docstring'''


							with patch_environment(aa=1				,       BB=2						):
											self.assertEqual(os.environ.get('''AA'''						)				,       '''1'''						)
											self.assertEqual(os.environ.get('''BB'''						)				,       '''2'''						)

							self.assertNotIn('''AA'''				,       os.environ						)
							self.assertNotIn('''BB'''				,       os.environ						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Tuple:

							'''simple docstring'''


							__lowercase			      =							RegressionModel()
							__lowercase			      =							model.forward
							__lowercase			      =							convert_outputs_to_fpaa(model.forward						)
							__lowercase			      =							extract_model_from_parallel(__SCREAMING_SNAKE_CASE				,       keep_fpaa_wrapper=__SCREAMING_SNAKE_CASE						)
							__lowercase			      =							pickle.dumps(__SCREAMING_SNAKE_CASE						)





			@require_cuda
			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Dict:

							'''simple docstring'''


							__lowercase			      =							RegressionModel()
							__lowercase			      =							model.forward
							__lowercase			      =							torch.cuda.amp.autocast(dtype=torch.floataa						)(model.forward						)
							__lowercase			      =							convert_outputs_to_fpaa(model.forward						)
							__lowercase			      =							extract_model_from_parallel(__SCREAMING_SNAKE_CASE				,       keep_fpaa_wrapper=__SCREAMING_SNAKE_CASE						)
							__lowercase			      =							pickle.dumps(__SCREAMING_SNAKE_CASE						)





			@require_cuda
			@require_torch_min_version(version='''2.0'''						)
			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> List[Any]:

							'''simple docstring'''


							__lowercase			      =							RegressionModel()
							__lowercase			      =							model.forward
							__lowercase			      =							torch.cuda.amp.autocast(dtype=torch.floataa						)(model.forward						)
							__lowercase			      =							convert_outputs_to_fpaa(model.forward						)
							__lowercase			      =							torch.compile(model.forward				,       backend='''inductor'''						)
							__lowercase			      =							torch.randn(4				,       10						).cuda()
							__lowercase			      =							model(__SCREAMING_SNAKE_CASE						)





			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Tuple:

							'''simple docstring'''


							__lowercase			      =							RegressionModel()
							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							__lowercase			      =							torch.nn.parallel.DataParallel(__SCREAMING_SNAKE_CASE						)
							__lowercase			      =							extract_model_from_parallel(__SCREAMING_SNAKE_CASE						)

							self.assertEqual(__SCREAMING_SNAKE_CASE				,       __SCREAMING_SNAKE_CASE						)





			@require_torch_min_version(version='''2.0'''						)
			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> List[Any]:

							'''simple docstring'''


							__lowercase			      =							RegressionModel()
							__lowercase			      =							torch.compile(__SCREAMING_SNAKE_CASE						)

							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							__lowercase			      =							torch.nn.parallel.DataParallel(__SCREAMING_SNAKE_CASE						)
							__lowercase			      =							torch.compile(__SCREAMING_SNAKE_CASE						)
							__lowercase			      =							extract_model_from_parallel(__SCREAMING_SNAKE_CASE						)

							self.assertEqual(compiled_model._orig_mod				,       compiled_model_unwrapped._orig_mod						)





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

							'''simple docstring'''


							self.assertEqual(find_device([1, '''a''', torch.tensor([1, 2, 3]						)]						)				,       torch.device('''cpu'''						)						)
							self.assertEqual(find_device({'''a''': 1, '''b''': torch.tensor([1, 2, 3]						)}						)				,       torch.device('''cpu'''						)						)
							self.assertIsNone(find_device([1, '''a''']						)						)
from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PretrainedConfig, PreTrainedTokenizer
from ...onnx import OnnxConfig, PatchingSpec
from ...utils import TensorType, is_torch_available, logging


__a  :					int          =  logging.get_logger(__name__)

__a  :					Optional[Any]          =  {
    """microsoft/layoutlm-base-uncased""": (
        """https://huggingface.co/microsoft/layoutlm-base-uncased/resolve/main/config.json"""
    ),
    """microsoft/layoutlm-large-uncased""": (
        """https://huggingface.co/microsoft/layoutlm-large-uncased/resolve/main/config.json"""
    ),
}




class        _UpperCamelCase				(						_UpperCAmelCase			):


			"""simple docstring"""






			__a  :			List[Any]      			=						'''layoutlm'''





			def __init__(       self				,       lowerCAmelCase__=3_05_22				,       lowerCAmelCase__=7_68				,       lowerCAmelCase__=12				,       lowerCAmelCase__=12				,       lowerCAmelCase__=30_72				,       lowerCAmelCase__="gelu"				,       lowerCAmelCase__=0.1				,       lowerCAmelCase__=0.1				,       lowerCAmelCase__=5_12				,       lowerCAmelCase__=2				,       lowerCAmelCase__=0.02				,       lowerCAmelCase__=1E-12				,       lowerCAmelCase__=0				,       lowerCAmelCase__="absolute"				,       lowerCAmelCase__=True				,       lowerCAmelCase__=10_24				,       **lowerCAmelCase__				,       )					-> Tuple:

							'''simple docstring'''


							super().__init__(pad_token_id=lowerCAmelCase__				,       **lowerCAmelCase__						)
							__lowercase			      =							vocab_size
							__lowercase			      =							hidden_size
							__lowercase			      =							num_hidden_layers
							__lowercase			      =							num_attention_heads
							__lowercase			      =							hidden_act
							__lowercase			      =							intermediate_size
							__lowercase			      =							hidden_dropout_prob
							__lowercase			      =							attention_probs_dropout_prob
							__lowercase			      =							max_position_embeddings
							__lowercase			      =							type_vocab_size
							__lowercase			      =							initializer_range
							__lowercase			      =							layer_norm_eps
							__lowercase			      =							position_embedding_type
							__lowercase			      =							use_cache
							__lowercase			      =							max_ad_position_embeddings




class        _UpperCamelCase				(						_UpperCAmelCase			):


			"""simple docstring"""






			def __init__(       self				,       lowerCAmelCase__				,       lowerCAmelCase__ = "default"				,       lowerCAmelCase__ = None				,       )					-> str:

							'''simple docstring'''


							super().__init__(lowerCAmelCase__				,       task=lowerCAmelCase__				,       patching_specs=lowerCAmelCase__						)
							__lowercase			      =							config.max_ad_position_embeddings - 1





			@property
			def 	_SCREAMING_SNAKE_CASE      (       self						)					-> Mapping[str, Mapping[int, str]]:

							'''simple docstring'''


							return OrderedDict(
							    [
							        ('''input_ids''', {0: '''batch''', 1: '''sequence'''}),
							        ('''bbox''', {0: '''batch''', 1: '''sequence'''}),
							        ('''attention_mask''', {0: '''batch''', 1: '''sequence'''}),
							        ('''token_type_ids''', {0: '''batch''', 1: '''sequence'''}),
							    ]						)





			def 	_SCREAMING_SNAKE_CASE      (       self				,       lowerCAmelCase__				,       lowerCAmelCase__ = -1				,       lowerCAmelCase__ = -1				,       lowerCAmelCase__ = False				,       lowerCAmelCase__ = None				,       )					-> Mapping[str, Any]:

							'''simple docstring'''


							__lowercase			      =							super().generate_dummy_inputs(
							    lowerCAmelCase__				,       batch_size=lowerCAmelCase__				,       seq_length=lowerCAmelCase__				,       is_pair=lowerCAmelCase__				,       framework=lowerCAmelCase__						)

							# Generate a dummy bbox
							__lowercase			      =							[48, 84, 73, 1_28]

							if not framework == TensorType.PYTORCH:
											raise NotImplementedError('''Exporting LayoutLM to ONNX is currently only supported for PyTorch.'''						)

							if not is_torch_available():
											raise ValueError('''Cannot generate dummy inputs without PyTorch installed.'''						)
							import torch

							__lowercase      ,					__lowercase			      =							input_dict['''input_ids'''].shape
							__lowercase			      =							torch.tensor([*[box] * seq_length]						).tile(lowerCAmelCase__				,       1				,       1						)
							return input_dict