


import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast

@require_vision
class      __snake_case				(   unittest.TestCase):
					def 	a			(						self       :						str  )							->					Any:



											'''simple docstring'''





											__magic_name__					=      tempfile.mkdtemp()

											__magic_name__					=      CLIPImageProcessor()
											__magic_name__					=      BertTokenizer.from_pretrained(
											    'hf-internal-testing/tiny-random-BertModel'	,      model_input_names=['input_ids', 'attention_mask']  )

											__magic_name__					=      GitProcessor(_lowerCAmelCase	,      _lowerCAmelCase  )

											processor.save_pretrained(self.tmpdirname  )
					def 	a			(						self       :						List[str]	,      **_lowerCAmelCase       :						Union[str, Any]  )							->					Optional[int]:



											'''simple docstring'''





											return AutoProcessor.from_pretrained(self.tmpdirname	,      **_lowerCAmelCase  ).tokenizer
					def 	a			(						self       :						int	,      **_lowerCAmelCase       :						Any  )							->					int:



											'''simple docstring'''





											return AutoProcessor.from_pretrained(self.tmpdirname	,      **_lowerCAmelCase  ).image_processor
					def 	a			(						self       :						Optional[Any]  )							->					Union[str, Any]:



											'''simple docstring'''





											shutil.rmtree(self.tmpdirname  )
					def 	a			(						self       :						Tuple  )							->					Tuple:



											'''simple docstring'''





											__magic_name__					=      [np.random.randint(2_5_5	,      size=(3, 3_0, 4_0_0)	,      dtype=np.uinta  )]

											__magic_name__					=      [Image.fromarray(np.moveaxis(_lowerCAmelCase	,      0	,      -1  )  ) for x in image_inputs]

											return image_inputs
					def 	a			(						self       :						int  )							->					int:



											'''simple docstring'''





											__magic_name__					=      GitProcessor(tokenizer=self.get_tokenizer()	,      image_processor=self.get_image_processor()  )
											processor.save_pretrained(self.tmpdirname  )

											__magic_name__					=      self.get_tokenizer(bos_token='(BOS)'	,      eos_token='(EOS)'  )
											__magic_name__					=      self.get_image_processor(do_normalize=_lowerCAmelCase	,      padding_value=1.0  )

											__magic_name__					=      GitProcessor.from_pretrained(
											    self.tmpdirname	,      bos_token='(BOS)'	,      eos_token='(EOS)'	,      do_normalize=_lowerCAmelCase	,      padding_value=1.0  )

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

											self.assertEqual(processor.image_processor.to_json_string()	,      image_processor_add_kwargs.to_json_string()  )
											self.assertIsInstance(processor.image_processor	,      _lowerCAmelCase  )
					def 	a			(						self       :						Optional[Any]  )							->					Union[str, Any]:



											'''simple docstring'''





											__magic_name__					=      self.get_image_processor()
											__magic_name__					=      self.get_tokenizer()

											__magic_name__					=      GitProcessor(tokenizer=_lowerCAmelCase	,      image_processor=_lowerCAmelCase  )

											__magic_name__					=      self.prepare_image_inputs()

											__magic_name__					=      image_processor(_lowerCAmelCase	,      return_tensors='np'  )
											__magic_name__					=      processor(images=_lowerCAmelCase	,      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 	a			(						self       :						Any  )							->					Tuple:



											'''simple docstring'''





											__magic_name__					=      self.get_image_processor()
											__magic_name__					=      self.get_tokenizer()

											__magic_name__					=      GitProcessor(tokenizer=_lowerCAmelCase	,      image_processor=_lowerCAmelCase  )

											__magic_name__					=      'lower newer'

											__magic_name__					=      processor(text=_lowerCAmelCase  )

											__magic_name__					=      tokenizer(_lowerCAmelCase	,      return_token_type_ids=_lowerCAmelCase  )

											for key in encoded_tok.keys():
																	self.assertListEqual(encoded_tok[key]	,      encoded_processor[key]  )
					def 	a			(						self       :						Union[str, Any]  )							->					int:



											'''simple docstring'''





											__magic_name__					=      self.get_image_processor()
											__magic_name__					=      self.get_tokenizer()

											__magic_name__					=      GitProcessor(tokenizer=_lowerCAmelCase	,      image_processor=_lowerCAmelCase  )

											__magic_name__					=      'lower newer'
											__magic_name__					=      self.prepare_image_inputs()

											__magic_name__					=      processor(text=_lowerCAmelCase	,      images=_lowerCAmelCase  )

											self.assertListEqual(list(inputs.keys()  )	,      ['input_ids', 'attention_mask', 'pixel_values']  )

											# test if it raises when no input is passed
											with pytest.raises(_lowerCAmelCase  ):
																	processor()
					def 	a			(						self       :						List[Any]  )							->					Tuple:



											'''simple docstring'''





											__magic_name__					=      self.get_image_processor()
											__magic_name__					=      self.get_tokenizer()

											__magic_name__					=      GitProcessor(tokenizer=_lowerCAmelCase	,      image_processor=_lowerCAmelCase  )

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

											__magic_name__					=      processor.batch_decode(_lowerCAmelCase  )
											__magic_name__					=      tokenizer.batch_decode(_lowerCAmelCase  )

											self.assertListEqual(_lowerCAmelCase	,      _lowerCAmelCase  )







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



											'''simple docstring'''





											__magic_name__					=      self.get_image_processor()
											__magic_name__					=      self.get_tokenizer()

											__magic_name__					=      GitProcessor(tokenizer=_lowerCAmelCase	,      image_processor=_lowerCAmelCase  )

											__magic_name__					=      'lower newer'
											__magic_name__					=      self.prepare_image_inputs()

											__magic_name__					=      processor(text=_lowerCAmelCase	,      images=_lowerCAmelCase  )

											# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
											self.assertListEqual(list(inputs.keys()  )	,      ['input_ids', 'attention_mask', 'pixel_values']  )





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase			:					List[Any]         =							logging.get_logger(__name__)

UpperCamelCase			:					Tuple         =							{
    '''mnaylor/mega-base-wikitext''': '''https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json''',
}

class      __snake_case				(   _UpperCAmelCase):
					SCREAMING_SNAKE_CASE_		:       str								=			"mega"
					def __init__(						self       :						Optional[int]	,      _lowerCAmelCase       :						List[Any]=3_0_5_2_2	,      _lowerCAmelCase       :						List[str]=1_2_8	,      _lowerCAmelCase       :						Dict=4	,      _lowerCAmelCase       :						Optional[Any]=2_5_6	,      _lowerCAmelCase       :						Optional[Any]=1_6	,      _lowerCAmelCase       :						Dict=True	,      _lowerCAmelCase       :						Optional[Any]=6_4	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						Optional[int]=-1	,      _lowerCAmelCase       :						Any=None	,      _lowerCAmelCase       :						List[Any]=True	,      _lowerCAmelCase       :						List[Any]="scalenorm"	,      _lowerCAmelCase       :						str=True	,      _lowerCAmelCase       :						Union[str, Any]="silu"	,      _lowerCAmelCase       :						Optional[Any]="softmax"	,      _lowerCAmelCase       :						Optional[Any]=0.1	,      _lowerCAmelCase       :						Dict=0.1	,      _lowerCAmelCase       :						Any=0.1	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						Any=True	,      _lowerCAmelCase       :						int=2_5_6	,      _lowerCAmelCase       :						List[str]=True	,      _lowerCAmelCase       :						str=0.1	,      _lowerCAmelCase       :						List[str]=2_0_4_8	,      _lowerCAmelCase       :						Optional[Any]=False	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Dict=0.02	,      _lowerCAmelCase       :						str=0.2	,      _lowerCAmelCase       :						List[str]=0.02	,      _lowerCAmelCase       :						int=1.0	,      _lowerCAmelCase       :						Optional[int]=1	,      _lowerCAmelCase       :						List[str]=0	,      _lowerCAmelCase       :						str=2	,      _lowerCAmelCase       :						Optional[Any]="rotary"	,      _lowerCAmelCase       :						int=None	,      _lowerCAmelCase       :						int=True	,      _lowerCAmelCase       :						Optional[Any]=True	,      **_lowerCAmelCase       :						str	,      )							->					Any:



											'''simple docstring'''





											super().__init__(pad_token_id=_lowerCAmelCase	,      bos_token_id=_lowerCAmelCase	,      eos_token_id=_lowerCAmelCase	,      **_lowerCAmelCase  )

											__magic_name__					=      vocab_size
											__magic_name__					=      hidden_size
											__magic_name__					=      num_hidden_layers
											__magic_name__					=      activation
											__magic_name__					=      attention_activation
											__magic_name__					=      intermediate_size
											__magic_name__					=      ema_projection_size
											__magic_name__					=      bidirectional
											__magic_name__					=      shared_representation_size
											__magic_name__					=      use_chunking
											__magic_name__					=      chunk_size
											__magic_name__					=      truncation
											__magic_name__					=      normalize_before_mega
											__magic_name__					=      normalization_type
											__magic_name__					=      norm_affine
											__magic_name__					=      dropout_prob
											__magic_name__					=      hidden_dropout_prob
											__magic_name__					=      attention_probs_dropout_prob
											__magic_name__					=      use_feature_dropout
											__magic_name__					=      use_normalized_ffn
											__magic_name__					=      nffn_hidden_size
											__magic_name__					=      normalize_before_ffn
											__magic_name__					=      nffn_activation_dropout_prob
											__magic_name__					=      max_positions
											__magic_name__					=      add_token_type_embeddings
											__magic_name__					=      type_vocab_size
											__magic_name__					=      initializer_range
											__magic_name__					=      ema_delta_alpha_range
											__magic_name__					=      ema_beta_range
											__magic_name__					=      ema_gamma_omega_range
											__magic_name__					=      relative_positional_bias
											__magic_name__					=      use_cache
											__magic_name__					=      classifier_dropout
											__magic_name__					=      add_lm_hidden_dense_layer
											__magic_name__					=      1  # not used but required by Hugging Face







class      __snake_case				(   _UpperCAmelCase):
					@property
					def 	a			(						self       :						int  )							->					Mapping[str, Mapping[int, str]]:



											'''simple docstring'''





											if self.task == "multiple-choice":
																	__magic_name__					=      {0: 'batch', 1: 'choice', 2: 'sequence'}
											else:
																	__magic_name__					=      {0: 'batch', 1: 'sequence'}
											return OrderedDict(
											    [
											        ('input_ids', dynamic_axis),
											        ('attention_mask', dynamic_axis),
											    ]  )

