



import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
						    LiltForQuestionAnswering,
						    LiltForSequenceClassification,
						    LiltForTokenClassification,
						    LiltModel,
						)
						from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST



class 				__snake_case						:




							def __init__(       self     :       Union[str, Any]				,				A_     :       List[Any]				,				A_     :       Dict=1_3				,				A_     :       Any=7				,				A_     :       Tuple=True				,				A_     :       Dict=True				,				A_     :       str=True				,				A_     :       Tuple=True				,				A_     :       int=9_9				,				A_     :       List[Any]=2_4				,				A_     :       str=2				,				A_     :       int=6				,				A_     :       Optional[Any]=3_7				,				A_     :       Dict="gelu"				,				A_     :       Tuple=0.1				,				A_     :       int=0.1				,				A_     :       List[Any]=5_1_2				,				A_     :       List[str]=1_6				,				A_     :       List[Any]=2				,				A_     :       Dict=0.02				,				A_     :       List[Any]=3				,				A_     :       Union[str, Any]=None				,				A_     :       List[Any]=1_0_0_0				,				):
									lowerCAmelCase_			:     int									= parent
									lowerCAmelCase_			:     Optional[int]									= batch_size
									lowerCAmelCase_			:     Dict									= seq_length
									lowerCAmelCase_			:     Dict									= is_training
									lowerCAmelCase_			:     List[str]									= use_input_mask
									lowerCAmelCase_			:     Optional[int]									= use_token_type_ids
									lowerCAmelCase_			:     str									= use_labels
									lowerCAmelCase_			:     List[Any]									= vocab_size
									lowerCAmelCase_			:     Any									= hidden_size
									lowerCAmelCase_			:     Union[str, Any]									= num_hidden_layers
									lowerCAmelCase_			:     Optional[int]									= num_attention_heads
									lowerCAmelCase_			:     int									= intermediate_size
									lowerCAmelCase_			:     Any									= hidden_act
									lowerCAmelCase_			:     Any									= hidden_dropout_prob
									lowerCAmelCase_			:     int									= attention_probs_dropout_prob
									lowerCAmelCase_			:     Union[str, Any]									= max_position_embeddings
									lowerCAmelCase_			:     List[Any]									= type_vocab_size
									lowerCAmelCase_			:     int									= type_sequence_label_size
									lowerCAmelCase_			:     List[str]									= initializer_range
									lowerCAmelCase_			:     List[str]									= num_labels
									lowerCAmelCase_			:     int									= scope
									lowerCAmelCase_			:     Dict									= range_bbox




							def    UpperCAmelCase__			(       self     :       Optional[int]):
									lowerCAmelCase_			:     List[Any]									= ids_tensor([self.batch_size, self.seq_length]				,				self.vocab_size)

									lowerCAmelCase_			:     Tuple									= ids_tensor([self.batch_size, self.seq_length, 4]				,				self.range_bbox)
									# Ensure that bbox is legal
									for i in range(bbox.shape[0]):
											for j in range(bbox.shape[1]):
													if bbox[i, j, 3] < bbox[i, j, 1]:
															lowerCAmelCase_			:     Tuple									= bbox[i, j, 3]
															lowerCAmelCase_			:     str									= bbox[i, j, 1]
															lowerCAmelCase_			:     List[Any]									= t
													if bbox[i, j, 2] < bbox[i, j, 0]:
															lowerCAmelCase_			:     Optional[Any]									= bbox[i, j, 2]
															lowerCAmelCase_			:     Union[str, Any]									= bbox[i, j, 0]
															lowerCAmelCase_			:     str									= t

									lowerCAmelCase_			:     List[Any]									= None
									if self.use_input_mask:
											lowerCAmelCase_			:     Optional[Any]									= ids_tensor([self.batch_size, self.seq_length]				,				vocab_size=2)

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

									lowerCAmelCase_			:     Any									= None
									lowerCAmelCase_			:     Optional[int]									= None
									if self.use_labels:
											lowerCAmelCase_			:     str									= ids_tensor([self.batch_size]				,				self.type_sequence_label_size)
											lowerCAmelCase_			:     str									= ids_tensor([self.batch_size, self.seq_length]				,				self.num_labels)

									lowerCAmelCase_			:     Tuple									= self.get_config()

									return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels




							def    UpperCAmelCase__			(       self     :       List[str]):
									return LiltConfig(
									    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				,				)




							def    UpperCAmelCase__			(       self     :       List[str]				,				A_     :       Optional[int]				,				A_     :       str				,				A_     :       Optional[Any]				,				A_     :       List[str]				,				A_     :       Union[str, Any]				,				A_     :       List[str]				,				A_     :       Any				,				):
									lowerCAmelCase_			:     Optional[Any]									= LiltModel(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     Union[str, Any]									= model(A_				,				bbox=A_				,				attention_mask=A_				,				token_type_ids=A_)
									lowerCAmelCase_			:     Tuple									= model(A_				,				bbox=A_				,				token_type_ids=A_)
									lowerCAmelCase_			:     Optional[int]									= model(A_				,				bbox=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     :       List[str]				,				A_     :       Union[str, Any]				,				A_     :       str				,				A_     :       Any				,				A_     :       Optional[int]				,				A_     :       Optional[int]				,				A_     :       Optional[Any]				,				A_     :       List[Any]				,				):
									lowerCAmelCase_			:     str									= self.num_labels
									lowerCAmelCase_			:     Dict									= LiltForTokenClassification(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     int									= model(
									    A_				,				bbox=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     :       Union[str, Any]				,				A_     :       List[str]				,				A_     :       int				,				A_     :       List[Any]				,				A_     :       List[Any]				,				A_     :       int				,				A_     :       str				,				A_     :       Union[str, Any]				,				):
									lowerCAmelCase_			:     Dict									= LiltForQuestionAnswering(config=A_)
									model.to(A_)
									model.eval()
									lowerCAmelCase_			:     Optional[Any]									= model(
									    A_				,				bbox=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_			:     Tuple									= self.prepare_config_and_inputs()
									(
									    (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  (
									    lowerCAmelCase_
									)					,  
									)			:     List[Any]									= config_and_inputs
									lowerCAmelCase_			:     Union[str, Any]									= {
									    '''input_ids''': input_ids,
									    '''bbox''': bbox,
									    '''token_type_ids''': token_type_ids,
									    '''attention_mask''': input_mask,
									}
									return config, inputs_dict





@require_torch
class 				__snake_case						(     UpperCamelCase_						,UpperCamelCase_						,UpperCamelCase_						,unittest.TestCase						):
							_a			      = (
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_a			      = (
							    {
							        '''feature-extraction''': LiltModel,
							        '''question-answering''': LiltForQuestionAnswering,
							        '''text-classification''': LiltForSequenceClassification,
							        '''token-classification''': LiltForTokenClassification,
							        '''zero-shot''': LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_a			      = False
							_a			      = False




							def    UpperCAmelCase__			(       self     :       Tuple				,				A_     :       Tuple				,				A_     :       List[str]				,				A_     :       Union[str, Any]				,				A_     :       Optional[int]				,				A_     :       Dict):
									return True




							def    UpperCAmelCase__			(       self     :       List[str]):
									lowerCAmelCase_			:     Dict									= LiltModelTester(self)
									lowerCAmelCase_			:     Optional[int]									= ConfigTester(self				,				config_class=A_				,				hidden_size=3_7)




							def    UpperCAmelCase__			(       self     :       List[Any]):
									self.config_tester.run_common_tests()




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									lowerCAmelCase_			:     Optional[int]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*A_)




							def    UpperCAmelCase__			(       self     :       Any):
									lowerCAmelCase_			:     Any									= self.model_tester.prepare_config_and_inputs()
									for type in ["absolute", "relative_key", "relative_key_query"]:
											lowerCAmelCase_			:     int									= type
											self.model_tester.create_and_check_model(*A_)




							def    UpperCAmelCase__			(       self     :       Union[str, Any]):
									lowerCAmelCase_			:     Tuple									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*A_)




							def    UpperCAmelCase__			(       self     :       int):
									lowerCAmelCase_			:     List[str]									= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*A_)







							@slow
							def    UpperCAmelCase__			(       self     :       Tuple):
									for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCAmelCase_			:     Union[str, Any]									= LiltModel.from_pretrained(A_)
											self.assertIsNotNone(A_)





@require_torch
@slow
class 				__snake_case						(     unittest.TestCase						):




							def    UpperCAmelCase__			(       self     :       Tuple):
									lowerCAmelCase_			:     Optional[int]									= LiltModel.from_pretrained('''SCUT-DLVCLab/lilt-roberta-en-base''').to(A_)

									lowerCAmelCase_			:     Optional[int]									= torch.tensor([[1, 2]]				,				device=A_)
									lowerCAmelCase_			:     Any									= torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]				,				device=A_)

									# forward pass
									with torch.no_grad():
											lowerCAmelCase_			:     Dict									= model(input_ids=A_				,				bbox=A_)

									lowerCAmelCase_			:     int									= torch.Size([1, 2, 7_6_8])
									lowerCAmelCase_			:     Any									= torch.tensor(
									    [[-0.0653, 0.0950, -0.0061], [-0.0545, 0.0926, -0.0324]]				,				device=A_				,				)

									self.assertTrue(outputs.last_hidden_state.shape				,				A_)
									self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]				,				A_				,				atol=1e-3))











import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


UpperCAmelCase_									=	get_tests_dir('fixtures')
UpperCAmelCase_									=	get_tests_dir('fixtures/dummy_feature_extractor_config.json')
UpperCAmelCase_									=	get_tests_dir('fixtures/dummy-config.json')





class     lowerCamelCase__(					unittest.TestCase):
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						0
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""   )
             self.assertIsInstance(UpperCamelCase_    ,       UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_   )
             self.assertIsInstance(UpperCamelCase_    ,       UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: int   ):
             with tempfile.TemporaryDirectory() as tmpdirname:
                    __lowerCamelCase   =						WavaVecaConfig()

                    # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_   ).to_dict()

                    config_dict.pop("""feature_extractor_type"""   )
                    __lowerCamelCase   =						WavaVecaFeatureExtractor(**UpperCamelCase_   )

                    # save in new folder
                    model_config.save_pretrained(UpperCamelCase_   )
                    config.save_pretrained(UpperCamelCase_   )

                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_   )

                    # make sure private variable is not incorrectly saved
                    __lowerCamelCase   =						json.loads(config.to_json_string()   )
                    self.assertTrue("""_processor_class""" not in dict_as_saved   )

             self.assertIsInstance(UpperCamelCase_    ,       UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_   )
             self.assertIsInstance(UpperCamelCase_    ,       UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: int   ):
             with self.assertRaisesRegex(
                 UpperCamelCase_    ,       """bert-base is not a local folder and is not a valid model identifier"""   ):
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained("""bert-base"""   )
      def 			lowerCAmelCase__						(				self: Tuple   ):
             with self.assertRaisesRegex(
                 UpperCamelCase_    ,       r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""   ):
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_    ,       revision="""aaaaaa"""   )
      def 			lowerCAmelCase__						(				self: Optional[Any]   ):
             with self.assertRaisesRegex(
                 UpperCamelCase_    ,       """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""    ,       ):
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""   )
      def 			lowerCAmelCase__						(				self: Tuple   ):
             # If remote code is not set, we will time out when asking whether to load the model.
             with self.assertRaises(UpperCamelCase_   ):
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(
                        """hf-internal-testing/test_dynamic_feature_extractor"""   )
             # If remote code is disabled, we can't load this config.
             with self.assertRaises(UpperCamelCase_   ):
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(
                        """hf-internal-testing/test_dynamic_feature_extractor"""    ,       trust_remote_code=UpperCamelCase_   )

             __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(
                 """hf-internal-testing/test_dynamic_feature_extractor"""    ,       trust_remote_code=UpperCamelCase_   )
             self.assertEqual(feature_extractor.__class__.__name__    ,       """NewFeatureExtractor"""   )

             # Test feature extractor can be reloaded.
             with tempfile.TemporaryDirectory() as tmp_dir:
                    feature_extractor.save_pretrained(UpperCamelCase_   )
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_    ,       trust_remote_code=UpperCamelCase_   )
             self.assertEqual(reloaded_feature_extractor.__class__.__name__    ,       """NewFeatureExtractor"""   )
      def 			lowerCAmelCase__						(				self: Any   ):
             try:
                    AutoConfig.register("""custom"""    ,       UpperCamelCase_   )
                    AutoFeatureExtractor.register(UpperCamelCase_    ,       UpperCamelCase_   )
                    # Trying to register something existing in the Transformers library will raise an error
                    with self.assertRaises(UpperCamelCase_   ):
                           AutoFeatureExtractor.register(UpperCamelCase_    ,       UpperCamelCase_   )

                    # Now that the config is registered, it can be used as any other config with the auto-API
                    __lowerCamelCase   =						CustomFeatureExtractor.from_pretrained(UpperCamelCase_   )
                    with tempfile.TemporaryDirectory() as tmp_dir:
                           feature_extractor.save_pretrained(UpperCamelCase_   )
                           __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(UpperCamelCase_   )
                           self.assertIsInstance(UpperCamelCase_    ,       UpperCamelCase_   )

             finally:
                    if "custom" in CONFIG_MAPPING._extra_content:
                           del CONFIG_MAPPING._extra_content["custom"]
                    if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
                           del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]




      def 			lowerCAmelCase__						(				self: Dict   ):





             class     lowerCamelCase__(					__lowerCamelCase):
                   UpperCAmelCase__      :	str    		=   True

             try:
                    AutoConfig.register("""custom"""    ,       UpperCamelCase_   )
                    AutoFeatureExtractor.register(UpperCamelCase_    ,       UpperCamelCase_   )
                    # If remote code is not set, the default is to use local
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(
                        """hf-internal-testing/test_dynamic_feature_extractor"""   )
                    self.assertEqual(feature_extractor.__class__.__name__    ,       """NewFeatureExtractor"""   )
                    self.assertTrue(feature_extractor.is_local   )

                    # If remote code is disabled, we load the local one.
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(
                        """hf-internal-testing/test_dynamic_feature_extractor"""    ,       trust_remote_code=UpperCamelCase_   )
                    self.assertEqual(feature_extractor.__class__.__name__    ,       """NewFeatureExtractor"""   )
                    self.assertTrue(feature_extractor.is_local   )

                    # If remote is enabled, we load from the Hub
                    __lowerCamelCase   =						AutoFeatureExtractor.from_pretrained(
                        """hf-internal-testing/test_dynamic_feature_extractor"""    ,       trust_remote_code=UpperCamelCase_   )
                    self.assertEqual(feature_extractor.__class__.__name__    ,       """NewFeatureExtractor"""   )
                    self.assertTrue(not hasattr(UpperCamelCase_    ,       """is_local"""   )   )

             finally:
                    if "custom" in CONFIG_MAPPING._extra_content:
                           del CONFIG_MAPPING._extra_content["custom"]
                    if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
                           del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]

