


'''simple docstring'''

import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
	from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


_UpperCAmelCase					:			str				     =					get_tests_dir("""fixtures/test_sentencepiece.model""")


@require_torch
@require_torchaudio
@require_sentencepiece
class    a__					(			unittest.TestCase   ):







				"""simple docstring"""



				def        _snake_case	(self		):
										__lowerCAmelCase											=					tempfile.mkdtemp()

										__lowerCAmelCase											=					['''<s>''', '''<pad>''', '''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
										__lowerCAmelCase											=					dict(zip(__lowercase							,      range(len(__lowercase		)		)		)		)
										__lowerCAmelCase											=					Path(self.tmpdirname		)
										save_json(__lowercase							,      save_dir / VOCAB_FILES_NAMES['''vocab_file''']		)
										if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
																copyfile(__lowercase							,      save_dir / VOCAB_FILES_NAMES['''spm_file''']		)

										__lowerCAmelCase											=					SpeechaTextTokenizer.from_pretrained(self.tmpdirname		)
										tokenizer.save_pretrained(self.tmpdirname		)

										__lowerCAmelCase											=					{
										    '''feature_size''': 24,
										    '''num_mel_bins''': 24,
										    '''padding_value''': 0.0,
										    '''sampling_rate''': 1_60_00,
										    '''return_attention_mask''': False,
										    '''do_normalize''': True,
										}
										save_json(__lowercase							,      save_dir / FEATURE_EXTRACTOR_NAME		)




				def        _snake_case	(self							,      **__lowercase		):
										return SpeechaTextTokenizer.from_pretrained(self.tmpdirname							,      **__lowercase		)




				def        _snake_case	(self							,      **__lowercase		):
										return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname							,      **__lowercase		)




				def        _snake_case	(self		):
										shutil.rmtree(self.tmpdirname		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_tokenizer()
										__lowerCAmelCase											=					self.get_feature_extractor()

										__lowerCAmelCase											=					SpeechaTextProcessor(tokenizer=__lowercase							,      feature_extractor=__lowercase		)

										processor.save_pretrained(self.tmpdirname		)
										__lowerCAmelCase											=					SpeechaTextProcessor.from_pretrained(self.tmpdirname		)

										self.assertEqual(processor.tokenizer.get_vocab()							,      tokenizer.get_vocab()		)
										self.assertIsInstance(processor.tokenizer							,      __lowercase		)

										self.assertEqual(processor.feature_extractor.to_json_string()							,      feature_extractor.to_json_string()		)
										self.assertIsInstance(processor.feature_extractor							,      __lowercase		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					SpeechaTextProcessor(
										    tokenizer=self.get_tokenizer()							,      feature_extractor=self.get_feature_extractor()		)
										processor.save_pretrained(self.tmpdirname		)

										__lowerCAmelCase											=					self.get_tokenizer(bos_token='''(BOS)'''							,      eos_token='''(EOS)'''		)
										__lowerCAmelCase											=					self.get_feature_extractor(do_normalize=__lowercase							,      padding_value=1.0		)

										__lowerCAmelCase											=					SpeechaTextProcessor.from_pretrained(
										    self.tmpdirname							,      bos_token='''(BOS)'''							,      eos_token='''(EOS)'''							,      do_normalize=__lowercase							,      padding_value=1.0		)

										self.assertEqual(processor.tokenizer.get_vocab()							,      tokenizer_add_kwargs.get_vocab()		)
										self.assertIsInstance(processor.tokenizer							,      __lowercase		)

										self.assertEqual(processor.feature_extractor.to_json_string()							,      feature_extractor_add_kwargs.to_json_string()		)
										self.assertIsInstance(processor.feature_extractor							,      __lowercase		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_feature_extractor()
										__lowerCAmelCase											=					self.get_tokenizer()

										__lowerCAmelCase											=					SpeechaTextProcessor(tokenizer=__lowercase							,      feature_extractor=__lowercase		)

										__lowerCAmelCase											=					floats_list((3, 10_00)		)

										__lowerCAmelCase											=					feature_extractor(__lowercase							,      return_tensors='''np'''		)
										__lowerCAmelCase											=					processor(__lowercase							,      return_tensors='''np'''		)

										for key in input_feat_extract.keys():
																self.assertAlmostEqual(input_feat_extract[key].sum()							,      input_processor[key].sum()							,      delta=1e-2		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_feature_extractor()
										__lowerCAmelCase											=					self.get_tokenizer()

										__lowerCAmelCase											=					SpeechaTextProcessor(tokenizer=__lowercase							,      feature_extractor=__lowercase		)

										__lowerCAmelCase											=					'''This is a test string'''

										__lowerCAmelCase											=					processor(text=__lowercase		)

										__lowerCAmelCase											=					tokenizer(__lowercase		)

										for key in encoded_tok.keys():
																self.assertListEqual(encoded_tok[key]							,      encoded_processor[key]		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_feature_extractor()
										__lowerCAmelCase											=					self.get_tokenizer()

										__lowerCAmelCase											=					SpeechaTextProcessor(tokenizer=__lowercase							,      feature_extractor=__lowercase		)

										__lowerCAmelCase											=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

										__lowerCAmelCase											=					processor.batch_decode(__lowercase		)
										__lowerCAmelCase											=					tokenizer.batch_decode(__lowercase		)

										self.assertListEqual(__lowercase							,      __lowercase		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					self.get_feature_extractor()
										__lowerCAmelCase											=					self.get_tokenizer()

										__lowerCAmelCase											=					SpeechaTextProcessor(tokenizer=__lowercase							,      feature_extractor=__lowercase		)

										self.assertListEqual(
										    processor.model_input_names							,      feature_extractor.model_input_names							,      msg='''`processor` and `feature_extractor` model input names do not match'''							,      )





'''simple docstring'''

from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


_UpperCAmelCase					:			Optional[int]				     =					logging.get_logger(__name__)

_UpperCAmelCase					:			str				     =					{
    """layoutlmv2-base-uncased""": """https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json""",
    """layoutlmv2-large-uncased""": """https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json""",
    # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
}

# soft dependency
if is_detectrona_available():
	import detectrona


class    a__					(			__A   ):







				"""simple docstring"""



				__UpperCamelCase     :		List[Any]						=						'layoutlmv2'




				def __init__(self							,      __lowercase=3_05_22							,      __lowercase=7_68							,      __lowercase=12							,      __lowercase=12							,      __lowercase=30_72							,      __lowercase="gelu"							,      __lowercase=0.1							,      __lowercase=0.1							,      __lowercase=5_12							,      __lowercase=2							,      __lowercase=0.0_2							,      __lowercase=1e-12							,      __lowercase=0							,      __lowercase=10_24							,      __lowercase=1_28							,      __lowercase=32							,      __lowercase=True							,      __lowercase=2_56							,      __lowercase=64							,      __lowercase=True							,      __lowercase=[7, 7, 2_56]							,      __lowercase=1_28							,      __lowercase=1_28							,      __lowercase=True							,      __lowercase=True							,      __lowercase=False							,      __lowercase=None							,      **__lowercase							,      ):
										super().__init__(
										    vocab_size=__lowercase							,      hidden_size=__lowercase							,      num_hidden_layers=__lowercase							,      num_attention_heads=__lowercase							,      intermediate_size=__lowercase							,      hidden_act=__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							,      pad_token_id=__lowercase							,      **__lowercase							,      )
										__lowerCAmelCase											=					max_ad_position_embeddings
										__lowerCAmelCase											=					max_rel_pos
										__lowerCAmelCase											=					rel_pos_bins
										__lowerCAmelCase											=					fast_qkv
										__lowerCAmelCase											=					max_rel_ad_pos
										__lowerCAmelCase											=					rel_ad_pos_bins
										__lowerCAmelCase											=					convert_sync_batchnorm
										__lowerCAmelCase											=					image_feature_pool_shape
										__lowerCAmelCase											=					coordinate_size
										__lowerCAmelCase											=					shape_size
										__lowerCAmelCase											=					has_relative_attention_bias
										__lowerCAmelCase											=					has_spatial_attention_bias
										__lowerCAmelCase											=					has_visual_segment_embedding
										__lowerCAmelCase											=					(
										    detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
										)




				@classmethod
				def        _snake_case	(self		):
										return {
										    "MODEL.MASK_ON": True,
										    "MODEL.PIXEL_STD": [5_7.3_7_5, 5_7.1_2_0, 5_8.3_9_5],
										    "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
										    "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
										    "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [1_28], [2_56], [5_12]],
										    "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
										    "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 20_00,
										    "MODEL.RPN.PRE_NMS_TOPK_TEST": 10_00,
										    "MODEL.RPN.POST_NMS_TOPK_TRAIN": 10_00,
										    "MODEL.POST_NMS_TOPK_TEST": 10_00,
										    "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
										    "MODEL.ROI_HEADS.NUM_CLASSES": 5,
										    "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
										    "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
										    "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
										    "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
										    "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
										    "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
										    "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
										    "MODEL.RESNETS.DEPTH": 1_01,
										    "MODEL.RESNETS.SIZES": [[32], [64], [1_28], [2_56], [5_12]],
										    "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
										    "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
										    "MODEL.RESNETS.NUM_GROUPS": 32,
										    "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
										    "MODEL.RESNETS.STRIDE_IN_1X1": False,
										}




				def        _snake_case	(self		):
										__lowerCAmelCase											=					detectrona.config.get_cfg()
										for k, v in self.detectrona_config_args.items():
																__lowerCAmelCase											=					k.split('''.'''		)
																__lowerCAmelCase											=					detectrona_config
																for attribute in attributes[:-1]:
																						__lowerCAmelCase											=					getattr(__lowercase							,      __lowercase		)
																setattr(__lowercase							,      attributes[-1]							,      __lowercase		)

										return detectrona_config

