




import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import CLIPImageProcessor, CLIPProcessor


@require_vision
class    lowerCamelCase    (		unittest.TestCase						):






			'''simple docstring'''





			def 							UpperCAmelCase__       ( self			:   List[str]  )						->   Any:
								__magic_name__							:	Union[str, Any]    							=							tempfile.mkdtemp()

								# fmt: off
								__magic_name__							:	Any    							=							['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
								# fmt: on
								__magic_name__							:	Dict    							=							dict(zip(lowerCamelCase_       ,						range(len(lowerCamelCase_  )  )  )  )
								__magic_name__							:	str    							=							['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
								__magic_name__							:	List[Any]    							=							{'''unk_token''': '''<unk>'''}

								__magic_name__							:	Union[str, Any]    							=							os.path.join(self.tmpdirname       ,						VOCAB_FILES_NAMES['''vocab_file''']  )
								__magic_name__							:	Optional[Any]    							=							os.path.join(self.tmpdirname       ,						VOCAB_FILES_NAMES['''merges_file''']  )
								with open(self.vocab_file       ,						'''w'''       ,						encoding='''utf-8'''  ) as fp:
													fp.write(json.dumps(lowerCamelCase_  ) + '''\n'''  )
								with open(self.merges_file       ,						'''w'''       ,						encoding='''utf-8'''  ) as fp:
													fp.write('''\n'''.join(lowerCamelCase_  )  )

								__magic_name__							:	Tuple    							=							{
								    '''do_resize''': True,
								    '''size''': 20,
								    '''do_center_crop''': True,
								    '''crop_size''': 18,
								    '''do_normalize''': True,
								    '''image_mean''': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
								    '''image_std''': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
								}
								__magic_name__							:	Tuple    							=							os.path.join(self.tmpdirname       ,						lowerCamelCase_  )
								with open(self.image_processor_file       ,						'''w'''       ,						encoding='''utf-8'''  ) as fp:
													json.dump(lowerCamelCase_       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   List[str]       ,						**lowerCamelCase_			:   Any  )						->   Optional[int]:
								return CLIPTokenizer.from_pretrained(self.tmpdirname       ,						**lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   int       ,						**lowerCamelCase_			:   List[str]  )						->   Optional[Any]:
								return CLIPTokenizerFast.from_pretrained(self.tmpdirname       ,						**lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Dict       ,						**lowerCamelCase_			:   Tuple  )						->   int:
								return CLIPImageProcessor.from_pretrained(self.tmpdirname       ,						**lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Any:
								shutil.rmtree(self.tmpdirname  )

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   List[Any]:
								__magic_name__							:	List[Any]    							=							[np.random.randint(255       ,						size=(3, 30, 400)       ,						dtype=np.uinta  )]

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

								return image_inputs

			def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   Tuple:
								__magic_name__							:	List[str]    							=							self.get_tokenizer()
								__magic_name__							:	Any    							=							self.get_rust_tokenizer()
								__magic_name__							:	int    							=							self.get_image_processor()

								__magic_name__							:	Union[str, Any]    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )
								processor_slow.save_pretrained(self.tmpdirname  )
								__magic_name__							:	Tuple    							=							CLIPProcessor.from_pretrained(self.tmpdirname       ,						use_fast=lowerCamelCase_  )

								__magic_name__							:	Any    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )
								processor_fast.save_pretrained(self.tmpdirname  )
								__magic_name__							:	Optional[Any]    							=							CLIPProcessor.from_pretrained(self.tmpdirname  )

								self.assertEqual(processor_slow.tokenizer.get_vocab()       ,						tokenizer_slow.get_vocab()  )
								self.assertEqual(processor_fast.tokenizer.get_vocab()       ,						tokenizer_fast.get_vocab()  )
								self.assertEqual(tokenizer_slow.get_vocab()       ,						tokenizer_fast.get_vocab()  )
								self.assertIsInstance(processor_slow.tokenizer       ,						lowerCamelCase_  )
								self.assertIsInstance(processor_fast.tokenizer       ,						lowerCamelCase_  )

								self.assertEqual(processor_slow.image_processor.to_json_string()       ,						image_processor.to_json_string()  )
								self.assertEqual(processor_fast.image_processor.to_json_string()       ,						image_processor.to_json_string()  )
								self.assertIsInstance(processor_slow.image_processor       ,						lowerCamelCase_  )
								self.assertIsInstance(processor_fast.image_processor       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Any  )						->   Tuple:
								__magic_name__							:	Optional[int]    							=							CLIPProcessor(tokenizer=self.get_tokenizer()       ,						image_processor=self.get_image_processor()  )
								processor.save_pretrained(self.tmpdirname  )

								__magic_name__							:	List[Any]    							=							self.get_tokenizer(bos_token='''(BOS)'''       ,						eos_token='''(EOS)'''  )
								__magic_name__							:	Dict    							=							self.get_image_processor(do_normalize=lowerCamelCase_       ,						padding_value=1.0  )

								__magic_name__							:	Any    							=							CLIPProcessor.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 							UpperCAmelCase__       ( self			:   List[str]  )						->   Any:
								__magic_name__							:	Optional[int]    							=							self.get_image_processor()
								__magic_name__							:	Optional[Any]    							=							self.get_tokenizer()

								__magic_name__							:	Union[str, Any]    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )

								__magic_name__							:	Tuple    							=							self.prepare_image_inputs()

								__magic_name__							:	Optional[Any]    							=							image_processor(lowerCamelCase_       ,						return_tensors='''np'''  )
								__magic_name__							:	Any    							=							processor(images=lowerCamelCase_       ,						return_tensors='''np'''  )

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

			def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   List[str]:
								__magic_name__							:	Tuple    							=							self.get_image_processor()
								__magic_name__							:	Union[str, Any]    							=							self.get_tokenizer()

								__magic_name__							:	Union[str, Any]    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )

								__magic_name__							:	Tuple    							=							'''lower newer'''

								__magic_name__							:	int    							=							processor(text=lowerCamelCase_  )

								__magic_name__							:	Optional[Any]    							=							tokenizer(lowerCamelCase_  )

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

			def 							UpperCAmelCase__       ( self			:   str  )						->   int:
								__magic_name__							:	List[Any]    							=							self.get_image_processor()
								__magic_name__							:	Any    							=							self.get_tokenizer()

								__magic_name__							:	List[str]    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )

								__magic_name__							:	Dict    							=							'''lower newer'''
								__magic_name__							:	Any    							=							self.prepare_image_inputs()

								__magic_name__							:	Optional[int]    							=							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 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   List[Any]:
								__magic_name__							:	int    							=							self.get_image_processor()
								__magic_name__							:	Optional[int]    							=							self.get_tokenizer()

								__magic_name__							:	List[str]    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )

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

								__magic_name__							:	Tuple    							=							processor.batch_decode(lowerCamelCase_  )
								__magic_name__							:	Optional[Any]    							=							tokenizer.batch_decode(lowerCamelCase_  )

								self.assertListEqual(lowerCamelCase_       ,						lowerCamelCase_  )


			def 							UpperCAmelCase__       ( self			:   List[Any]  )						->   Optional[Any]:
								__magic_name__							:	Optional[Any]    							=							self.get_image_processor()
								__magic_name__							:	Optional[Any]    							=							self.get_tokenizer()

								__magic_name__							:	List[Any]    							=							CLIPProcessor(tokenizer=lowerCamelCase_       ,						image_processor=lowerCamelCase_  )

								__magic_name__							:	Tuple    							=							'''lower newer'''
								__magic_name__							:	Optional[Any]    							=							self.prepare_image_inputs()

								__magic_name__							:	List[Any]    							=							processor(text=lowerCamelCase_       ,						images=lowerCamelCase_  )

								self.assertListEqual(list(inputs.keys()  )       ,						processor.model_input_names  )









from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


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

__lowerCamelCase   :						Tuple				=			{
    '''shi-labs/nat-mini-in1k-224''': '''https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json''',
    # See all Nat models at https://huggingface.co/models?filter=nat
}


class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase						):






			'''simple docstring'''





			UpperCamelCase__								='''nat'''

			UpperCamelCase__								={
			    '''num_attention_heads''': '''num_heads''',
			    '''num_hidden_layers''': '''num_layers''',
			}

			def __init__( self			:   Union[str, Any]       ,						lowerCamelCase_			:   Dict=4       ,						lowerCamelCase_			:   Union[str, Any]=3       ,						lowerCamelCase_			:   List[str]=64       ,						lowerCamelCase_			:   Union[str, Any]=[3, 4, 6, 5]       ,						lowerCamelCase_			:   List[Any]=[2, 4, 8, 16]       ,						lowerCamelCase_			:   List[Any]=7       ,						lowerCamelCase_			:   Union[str, Any]=3.0       ,						lowerCamelCase_			:   int=True       ,						lowerCamelCase_			:   str=0.0       ,						lowerCamelCase_			:   int=0.0       ,						lowerCamelCase_			:   Dict=0.1       ,						lowerCamelCase_			:   Optional[int]="gelu"       ,						lowerCamelCase_			:   Tuple=0.0_2       ,						lowerCamelCase_			:   Tuple=1E-5       ,						lowerCamelCase_			:   List[Any]=0.0       ,						lowerCamelCase_			:   str=None       ,						lowerCamelCase_			:   Tuple=None       ,						**lowerCamelCase_			:   Any       ,						)						->   Any:
								super().__init__(**lowerCamelCase_  )

								__magic_name__							:	List[Any]    							=							patch_size
								__magic_name__							:	str    							=							num_channels
								__magic_name__							:	Union[str, Any]    							=							embed_dim
								__magic_name__							:	Dict    							=							depths
								__magic_name__							:	str    							=							len(lowerCamelCase_  )
								__magic_name__							:	List[str]    							=							num_heads
								__magic_name__							:	Optional[int]    							=							kernel_size
								__magic_name__							:	Any    							=							mlp_ratio
								__magic_name__							:	Any    							=							qkv_bias
								__magic_name__							:	Union[str, Any]    							=							hidden_dropout_prob
								__magic_name__							:	Any    							=							attention_probs_dropout_prob
								__magic_name__							:	List[str]    							=							drop_path_rate
								__magic_name__							:	str    							=							hidden_act
								__magic_name__							:	Tuple    							=							layer_norm_eps
								__magic_name__							:	Any    							=							initializer_range
								# we set the hidden_size attribute in order to make Nat work with VisionEncoderDecoderModel
								# this indicates the channel dimension after the last stage of the model
								__magic_name__							:	str    							=							int(embed_dim * 2 ** (len(lowerCamelCase_  ) - 1)  )
								__magic_name__							:	str    							=							layer_scale_init_value
								__magic_name__							:	Dict    							=							['''stem'''] + [F'''stage{idx}''' for idx in range(1       ,						len(lowerCamelCase_  ) + 1  )]
								__magic_name__							,    __magic_name__							:	int    							=							get_aligned_output_features_output_indices(
								    out_features=lowerCamelCase_       ,						out_indices=lowerCamelCase_       ,						stage_names=self.stage_names  )



