'''simple docstring'''

import unittest

from transformers import MarkupLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    MarkupLMForQuestionAnswering,
							    MarkupLMForSequenceClassification,
							    MarkupLMForTokenClassification,
							    MarkupLMModel,
							)

# TODO check dependencies
from transformers import MarkupLMFeatureExtractor, MarkupLMProcessor, MarkupLMTokenizer
class 	SCREAMING_SNAKE_CASE      :







		"""simple docstring"""






		def __init__( self		,    __A		,    __A=13		,    __A=7		,    __A=True		,    __A=True		,    __A=True		,    __A=True		,    __A=99		,    __A=32		,    __A=5		,    __A=4		,    __A=37		,    __A="gelu"		,    __A=0.1		,    __A=0.1		,    __A=512		,    __A=16		,    __A=2		,    __A=0.02		,    __A=3		,    __A=None		,    __A=20		,    __A=30		,    __A=2		,    __A=2		,    __A=10		,    )			->  Optional[Any]:
								_lowerCAmelCase					    =parent
								_lowerCAmelCase					    =batch_size
								_lowerCAmelCase					    =seq_length
								_lowerCAmelCase					    =is_training
								_lowerCAmelCase					    =use_input_mask
								_lowerCAmelCase					    =use_token_type_ids
								_lowerCAmelCase					    =use_labels
								_lowerCAmelCase					    =vocab_size
								_lowerCAmelCase					    =hidden_size
								_lowerCAmelCase					    =num_hidden_layers
								_lowerCAmelCase					    =num_attention_heads
								_lowerCAmelCase					    =intermediate_size
								_lowerCAmelCase					    =hidden_act
								_lowerCAmelCase					    =hidden_dropout_prob
								_lowerCAmelCase					    =attention_probs_dropout_prob
								_lowerCAmelCase					    =max_position_embeddings
								_lowerCAmelCase					    =type_vocab_size
								_lowerCAmelCase					    =type_sequence_label_size
								_lowerCAmelCase					    =initializer_range
								_lowerCAmelCase					    =num_labels
								_lowerCAmelCase					    =scope
								_lowerCAmelCase					    =max_xpath_tag_unit_embeddings
								_lowerCAmelCase					    =max_xpath_subs_unit_embeddings
								_lowerCAmelCase					    =tag_pad_id
								_lowerCAmelCase					    =subs_pad_id
								_lowerCAmelCase					    =max_depth






		def 		UpperCamelCase__							( self					)			->  str:
								_lowerCAmelCase					    =ids_tensor([self.batch_size, self.seq_length]		,    self.vocab_size					)

								_lowerCAmelCase					    =ids_tensor(
								    [self.batch_size, self.seq_length, self.max_depth]		,    self.max_xpath_tag_unit_embeddings					)

								_lowerCAmelCase					    =ids_tensor(
								    [self.batch_size, self.seq_length, self.max_depth]		,    self.max_xpath_subs_unit_embeddings					)

								_lowerCAmelCase					    =None
								if self.use_input_mask:
														_lowerCAmelCase					    =ids_tensor([self.batch_size, self.seq_length]		,    vocab_size=2					)

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

								_lowerCAmelCase					    =None
								_lowerCAmelCase					    =None
								if self.use_labels:
														_lowerCAmelCase					    =ids_tensor([self.batch_size]		,    self.type_sequence_label_size					)
														_lowerCAmelCase					    =ids_tensor([self.batch_size, self.seq_length]		,    self.num_labels					)

								_lowerCAmelCase					    =self.get_config()

								return (
								    config,
								    input_ids,
								    xpath_tags_seq,
								    xpath_subs_seq,
								    token_type_ids,
								    input_mask,
								    sequence_labels,
								    token_labels,
								)






		def 		UpperCamelCase__							( self					)			->  Dict:
								return MarkupLMConfig(
								    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		,    initializer_range=self.initializer_range		,    max_xpath_tag_unit_embeddings=self.max_xpath_tag_unit_embeddings		,    max_xpath_subs_unit_embeddings=self.max_xpath_subs_unit_embeddings		,    tag_pad_id=self.tag_pad_id		,    subs_pad_id=self.subs_pad_id		,    max_depth=self.max_depth		,    )






		def 		UpperCamelCase__							( self		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    )			->  Tuple:
								_lowerCAmelCase					    =MarkupLMModel(config=__A					)
								model.to(__A					)
								model.eval()
								print('Configs:'		,    model.config.tag_pad_id		,    model.config.subs_pad_id					)
								_lowerCAmelCase					    =model(__A		,    attention_mask=__A		,    token_type_ids=__A					)
								_lowerCAmelCase					    =model(__A		,    token_type_ids=__A					)
								_lowerCAmelCase					    =model(__A					)
								self.parent.assertEqual(result.last_hidden_state.shape		,    (self.batch_size, self.seq_length, self.hidden_size)					)
								self.parent.assertEqual(result.pooler_output.shape		,    (self.batch_size, self.hidden_size)					)






		def 		UpperCamelCase__							( self		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    )			->  Tuple:
								_lowerCAmelCase					    =self.num_labels
								_lowerCAmelCase					    =MarkupLMForSequenceClassification(__A					)
								model.to(__A					)
								model.eval()
								_lowerCAmelCase					    =model(
								    __A		,    xpath_tags_seq=__A		,    xpath_subs_seq=__A		,    attention_mask=__A		,    token_type_ids=__A		,    labels=__A		,    )
								self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_labels)					)






		def 		UpperCamelCase__							( self		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    )			->  List[Any]:
								_lowerCAmelCase					    =self.num_labels
								_lowerCAmelCase					    =MarkupLMForTokenClassification(config=__A					)
								model.to(__A					)
								model.eval()
								_lowerCAmelCase					    =model(
								    __A		,    xpath_tags_seq=__A		,    xpath_subs_seq=__A		,    attention_mask=__A		,    token_type_ids=__A		,    labels=__A		,    )
								self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.seq_length, self.num_labels)					)






		def 		UpperCamelCase__							( self		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    __A		,    )			->  Union[str, Any]:
								_lowerCAmelCase					    =MarkupLMForQuestionAnswering(config=__A					)
								model.to(__A					)
								model.eval()
								_lowerCAmelCase					    =model(
								    __A		,    xpath_tags_seq=__A		,    xpath_subs_seq=__A		,    attention_mask=__A		,    token_type_ids=__A		,    start_positions=__A		,    end_positions=__A		,    )
								self.parent.assertEqual(result.start_logits.shape		,    (self.batch_size, self.seq_length)					)
								self.parent.assertEqual(result.end_logits.shape		,    (self.batch_size, self.seq_length)					)






		def 		UpperCamelCase__							( self					)			->  List[str]:
								_lowerCAmelCase					    =self.prepare_config_and_inputs()
								(
								    (
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					(
								    _lowerCAmelCase
								)    ,					
								)					    =config_and_inputs
								_lowerCAmelCase					    ={
								    'input_ids': input_ids,
								    'xpath_tags_seq': xpath_tags_seq,
								    'xpath_subs_seq': xpath_subs_seq,
								    'token_type_ids': token_type_ids,
								    'attention_mask': input_mask,
								}
								return config, inputs_dict

@require_torch
class 	SCREAMING_SNAKE_CASE      (    __lowercase     ,      __lowercase     ,      unittest.TestCase):







		"""simple docstring"""
		lowercase :   Union[str, Any]							=				(
		    (
		        MarkupLMModel,
		        MarkupLMForSequenceClassification,
		        MarkupLMForTokenClassification,
		        MarkupLMForQuestionAnswering,
		    )
		    if is_torch_available()
		    else None
		)
		lowercase :   Optional[Any]							=				(
		    {
		        'feature-extraction': MarkupLMModel,
		        'question-answering': MarkupLMForQuestionAnswering,
		        'text-classification': MarkupLMForSequenceClassification,
		        'token-classification': MarkupLMForTokenClassification,
		        'zero-shot': MarkupLMForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)






		def 		UpperCamelCase__							( self		,    __A		,    __A		,    __A		,    __A		,    __A					)			->  Tuple:
								# ValueError: Nodes must be of type `List[str]` (single pretokenized example), or `List[List[str]]`
								# (batch of pretokenized examples).
								return True






		def 		UpperCamelCase__							( self					)			->  List[str]:
								_lowerCAmelCase					    =MarkupLMModelTester(self					)
								_lowerCAmelCase					    =ConfigTester(self		,    config_class=__A		,    hidden_size=37					)






		def 		UpperCamelCase__							( self					)			->  Any:
								self.config_tester.run_common_tests()






		def 		UpperCamelCase__							( self					)			->  List[str]:
								_lowerCAmelCase					    =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__A					)






		def 		UpperCamelCase__							( self					)			->  str:
								_lowerCAmelCase					    =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*__A					)






		def 		UpperCamelCase__							( self					)			->  Optional[int]:
								_lowerCAmelCase					    =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__A					)






		def 		UpperCamelCase__							( self					)			->  Dict:
								_lowerCAmelCase					    =self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__A					)



def 				UpperCamelCase__      (      ):


						'''simple docstring'''






						_lowerCAmelCase					    ='\n    <!DOCTYPE html>\n    <html>\n    <head>\n    <title>Page Title</title>\n    </head>\n    <body>\n\n    <h1>This is a Heading</h1>\n    <p>This is a paragraph.</p>\n\n    </body>\n    </html>\n    '

						return html_string


@require_torch
class 	SCREAMING_SNAKE_CASE      (    unittest.TestCase):







		"""simple docstring"""






		@cached_property
		def 		UpperCamelCase__							( self					)			->  Any:
								# TODO use from_pretrained here
								_lowerCAmelCase					    =MarkupLMFeatureExtractor()
								_lowerCAmelCase					    =MarkupLMTokenizer.from_pretrained('microsoft/markuplm-base'					)

								return MarkupLMProcessor(__A		,    __A					)






		@slow
		def 		UpperCamelCase__							( self					)			->  Any:
								_lowerCAmelCase					    =MarkupLMModel.from_pretrained('microsoft/markuplm-base'					).to(__A					)

								_lowerCAmelCase					    =self.default_processor

								_lowerCAmelCase					    =processor(prepare_html_string()		,    return_tensors='pt'					)
								_lowerCAmelCase					    =inputs.to(__A					)

								# forward pass
								with torch.no_grad():
														_lowerCAmelCase					    =model(**__A					)

								# verify the last hidden states
								_lowerCAmelCase					    =torch.Size([1, 14, 768]					)
								self.assertEqual(outputs.last_hidden_state.shape		,    __A					)

								_lowerCAmelCase					    =torch.tensor(
								    [[0.0_675, -0.0_052, 0.5_001], [-0.2_281, 0.0_802, 0.2_192], [-0.0_583, -0.3_311, 0.1_185]]					).to(__A					)

								self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]		,    __A		,    atol=1E-4					)					)






'''simple docstring'''

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,
)


lowercase_  		=			namedtuple('''ExampleNamedTuple''', '''a b c''')
class 	SCREAMING_SNAKE_CASE      (    unittest.TestCase):







		"""simple docstring"""






		def 		UpperCamelCase__							( self					)			->  int:
								_lowerCAmelCase					    =torch.randn(5		,    2					)
								_lowerCAmelCase					    =torch.device('cuda'					) if torch.cuda.is_available() else torch.device('cpu'					)

								_lowerCAmelCase					    =send_to_device(__A		,    __A					)
								self.assertTrue(torch.equal(resulta.cpu()		,    __A					)					)

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

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

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

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






		def 		UpperCamelCase__							( self					)			->  Optional[int]:
								with self.assertRaises(__A					) as cm:
														_lowerCAmelCase					    =recursively_apply(torch.tensor		,    (torch.tensor(1					), 1)		,    error_on_other_type=__A					)
								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 		UpperCamelCase__							( self					)			->  int:
								_lowerCAmelCase					    =torch.tensor([1, 2, 3, 4, 5]					)
								self.assertEqual(listify(__A					)		,    [1, 2, 3, 4, 5]					)

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

								_lowerCAmelCase					    =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(__A					)		,    [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]					)






		def 		UpperCamelCase__							( self					)			->  List[Any]:
								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 		UpperCamelCase__							( self					)			->  Union[str, Any]:
								_lowerCAmelCase					    =RegressionModel()
								_lowerCAmelCase					    =model.forward
								_lowerCAmelCase					    =convert_outputs_to_fpaa(model.forward					)
								_lowerCAmelCase					    =extract_model_from_parallel(__A		,    keep_fpaa_wrapper=__A					)
								_lowerCAmelCase					    =pickle.dumps(__A					)






		@require_cuda
		def 		UpperCamelCase__							( self					)			->  Dict:
								_lowerCAmelCase					    =RegressionModel()
								_lowerCAmelCase					    =model.forward
								_lowerCAmelCase					    =torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
								_lowerCAmelCase					    =convert_outputs_to_fpaa(model.forward					)
								_lowerCAmelCase					    =extract_model_from_parallel(__A		,    keep_fpaa_wrapper=__A					)
								_lowerCAmelCase					    =pickle.dumps(__A					)






		@require_cuda
		@require_torch_min_version(version='2.0'					)
		def 		UpperCamelCase__							( self					)			->  int:
								_lowerCAmelCase					    =RegressionModel()
								_lowerCAmelCase					    =model.forward
								_lowerCAmelCase					    =torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
								_lowerCAmelCase					    =convert_outputs_to_fpaa(model.forward					)
								_lowerCAmelCase					    =torch.compile(model.forward		,    backend='inductor'					)
								_lowerCAmelCase					    =torch.randn(4		,    10					).cuda()
								_lowerCAmelCase					    =model(__A					)






		def 		UpperCamelCase__							( self					)			->  Optional[int]:
								_lowerCAmelCase					    =RegressionModel()
								# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
								_lowerCAmelCase					    =torch.nn.parallel.DataParallel(__A					)
								_lowerCAmelCase					    =extract_model_from_parallel(__A					)

								self.assertEqual(__A		,    __A					)






		@require_torch_min_version(version='2.0'					)
		def 		UpperCamelCase__							( self					)			->  str:
								_lowerCAmelCase					    =RegressionModel()
								_lowerCAmelCase					    =torch.compile(__A					)

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

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






		def 		UpperCamelCase__							( self					)			->  Tuple:
								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']					)					)





