




import inspect
import unittest
from math import floor

from transformers import CvtConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
		import torch

		from transformers import CvtForImageClassification, CvtModel
		from transformers.models.cvt.modeling_cvt import CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor
class 		UpperCAmelCase   (				UpperCAmelCase__			):







						'''simple docstring'''





						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							snake_case_		    =     self.config_class(**self.inputs_dict						)
							self.parent.assertTrue(hasattr(__lowercase       ,	"embed_dim"						)						)
							self.parent.assertTrue(hasattr(__lowercase       ,	"num_heads"						)						)







class 		UpperCAmelCase   :







						'''simple docstring'''





						def __init__(						self							:     Dict       ,	__lowercase							:     Dict       ,	__lowercase							:     str=13       ,	__lowercase							:     List[Any]=64       ,	__lowercase							:     Any=3       ,	__lowercase							:     Optional[int]=[16, 48, 96]       ,	__lowercase							:     List[Any]=[1, 3, 6]       ,	__lowercase							:     int=[1, 2, 10]       ,	__lowercase							:     Union[str, Any]=[7, 3, 3]       ,	__lowercase							:     Dict=[4, 2, 2]       ,	__lowercase							:     str=[2, 1, 1]       ,	__lowercase							:     List[str]=[2, 2, 2]       ,	__lowercase							:     Union[str, Any]=[False, False, True]       ,	__lowercase							:     str=[0.0, 0.0, 0.0]       ,	__lowercase							:     List[str]=0.02       ,	__lowercase							:     Union[str, Any]=1E-12       ,	__lowercase							:     Dict=True       ,	__lowercase							:     Any=True       ,	__lowercase							:     str=2       ,	):






							"""simple docstring"""






							snake_case_		    =     parent
							snake_case_		    =     batch_size
							snake_case_		    =     image_size
							snake_case_		    =     patch_sizes
							snake_case_		    =     patch_stride
							snake_case_		    =     patch_padding
							snake_case_		    =     is_training
							snake_case_		    =     use_labels
							snake_case_		    =     num_labels
							snake_case_		    =     num_channels
							snake_case_		    =     embed_dim
							snake_case_		    =     num_heads
							snake_case_		    =     stride_kv
							snake_case_		    =     depth
							snake_case_		    =     cls_token
							snake_case_		    =     attention_drop_rate
							snake_case_		    =     initializer_range
							snake_case_		    =     layer_norm_eps






						def        snake_case__						(						self							:     Optional[Any]						):






							"""simple docstring"""






							snake_case_		    =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

							snake_case_		    =     None
							if self.use_labels:
								snake_case_		    =     ids_tensor([self.batch_size]       ,	self.num_labels						)

							snake_case_		    =     self.get_config()
							return config, pixel_values, labels






						def        snake_case__						(						self							:     str						):






							"""simple docstring"""






							return CvtConfig(
							    image_size=self.image_size       ,	num_labels=self.num_labels       ,	num_channels=self.num_channels       ,	embed_dim=self.embed_dim       ,	num_heads=self.num_heads       ,	patch_sizes=self.patch_sizes       ,	patch_padding=self.patch_padding       ,	patch_stride=self.patch_stride       ,	stride_kv=self.stride_kv       ,	depth=self.depth       ,	cls_token=self.cls_token       ,	attention_drop_rate=self.attention_drop_rate       ,	initializer_range=self.initializer_range       ,	)






						def        snake_case__						(						self							:     str       ,	__lowercase							:     Tuple       ,	__lowercase							:     Tuple       ,	__lowercase							:     Any						):






							"""simple docstring"""






							snake_case_		    =     CvtModel(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     model(__lowercase						)
							snake_case_		    =     (self.image_size, self.image_size)
							snake_case_    ,      snake_case_		    =     image_size[0], image_size[1]
							for i in range(len(self.depth						)						):
								snake_case_		    =     floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
								snake_case_		    =     floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
							self.parent.assertEqual(result.last_hidden_state.shape       ,	(self.batch_size, self.embed_dim[-1], height, width)						)






						def        snake_case__						(						self							:     Any       ,	__lowercase							:     int       ,	__lowercase							:     List[str]       ,	__lowercase							:     int						):






							"""simple docstring"""






							snake_case_		    =     self.num_labels
							snake_case_		    =     CvtForImageClassification(__lowercase						)
							model.to(__lowercase						)
							model.eval()
							snake_case_		    =     model(__lowercase       ,	labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.num_labels)						)






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							snake_case_		    =     self.prepare_config_and_inputs()
							snake_case_    ,      snake_case_    ,      snake_case_		    =     config_and_inputs
							snake_case_		    =     {"pixel_values": pixel_values}
							return config, inputs_dict







@require_torch
class 		UpperCAmelCase   (				UpperCAmelCase__       ,   UpperCAmelCase__       ,   unittest.TestCase			):







						'''simple docstring'''





						lowerCAmelCase_     		=   (CvtModel, CvtForImageClassification) if is_torch_available() else ()
						lowerCAmelCase_     		=   (
						    {'''feature-extraction''': CvtModel, '''image-classification''': CvtForImageClassification}
						    if is_torch_available()
						    else {}
						)

						lowerCAmelCase_     		=   False
						lowerCAmelCase_     		=   False
						lowerCAmelCase_     		=   False
						lowerCAmelCase_     		=   False
						lowerCAmelCase_     		=   False






						def        snake_case__						(						self							:     Dict						):






							"""simple docstring"""






							snake_case_		    =     CvtModelTester(self						)
							snake_case_		    =     ConfigTester(self       ,	config_class=__lowercase       ,	has_text_modality=__lowercase       ,	hidden_size=37						)






						def        snake_case__						(						self							:     Optional[Any]						):






							"""simple docstring"""






							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()






						def        snake_case__						(						self							:     Optional[int]						):






							"""simple docstring"""






							return






						@unittest.skip(reason="Cvt does not output attentions"						)
						def        snake_case__						(						self							:     str						):






							"""simple docstring"""






							pass






						@unittest.skip(reason="Cvt does not use inputs_embeds"						)
						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






							pass






						@unittest.skip(reason="Cvt does not support input and output embeddings"						)
						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							pass






						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_    ,      snake_case_		    =     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		    =     model_class(__lowercase						)
								snake_case_		    =     inspect.signature(model.forward						)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								snake_case_		    =     [*signature.parameters.keys()]

								snake_case_		    =     ["pixel_values"]
								self.assertListEqual(arg_names[:1]       ,	__lowercase						)






						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_		    =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowercase						)






						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							def check_hidden_states_output(__lowercase							:     str       ,	__lowercase							:     Tuple       ,	__lowercase							:     str						):
								snake_case_		    =     model_class(__lowercase						)
								model.to(__lowercase						)
								model.eval()

								with torch.no_grad():
									snake_case_		    =     model(**self._prepare_for_class(__lowercase       ,	__lowercase						)						)

								snake_case_		    =     outputs.hidden_states

								snake_case_		    =     len(self.model_tester.depth						)
								self.assertEqual(len(__lowercase						)       ,	__lowercase						)

								# verify the first hidden states (first block)
								self.assertListEqual(
								    list(hidden_states[0].shape[-3:]						)       ,	[
								        self.model_tester.embed_dim[0],
								        self.model_tester.image_size // 4,
								        self.model_tester.image_size // 4,
								    ]       ,	)

							snake_case_    ,      snake_case_		    =     self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
								snake_case_		    =     True
								check_hidden_states_output(__lowercase       ,	__lowercase       ,	__lowercase						)

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								snake_case_		    =     True

								check_hidden_states_output(__lowercase       ,	__lowercase       ,	__lowercase						)






						def        snake_case__						(						self							:     List[str]						):






							"""simple docstring"""






							snake_case_		    =     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__lowercase						)






						@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."						)
						def        snake_case__						(						self							:     List[Any]						):






							"""simple docstring"""






							pass






						@slow
						def        snake_case__						(						self							:     Any						):






							"""simple docstring"""






							for model_name in CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_		    =     CvtModel.from_pretrained(__lowercase						)
								self.assertIsNotNone(__lowercase						)







def 						lowerCamelCase__    (		):







	'''simple docstring'''





	snake_case_		    =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
	return image






@require_torch
@require_vision
class 		UpperCAmelCase   (				unittest.TestCase			):







						'''simple docstring'''





						@cached_property
						def        snake_case__						(						self							:     Any						):






							"""simple docstring"""






							return AutoImageProcessor.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)






						@slow
						def        snake_case__						(						self							:     Union[str, Any]						):






							"""simple docstring"""






							snake_case_		    =     CvtForImageClassification.from_pretrained(CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(__lowercase						)

							snake_case_		    =     self.default_image_processor
							snake_case_		    =     prepare_img()
							snake_case_		    =     image_processor(images=__lowercase       ,	return_tensors="pt"						).to(__lowercase						)

							# forward pass
							with torch.no_grad():
								snake_case_		    =     model(**__lowercase						)

							# verify the logits
							snake_case_		    =     torch.Size((1, 10_00)						)
							self.assertEqual(outputs.logits.shape       ,	__lowercase						)

							snake_case_		    =     torch.tensor([0.9285, 0.9015, -0.3150]						).to(__lowercase						)

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






import json
import os
import re
import unittest

from transformers import CodeGenTokenizer, CodeGenTokenizerFast
from transformers.models.codegen.tokenization_codegen import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin
@require_tokenizers
class 		UpperCAmelCase   (				UpperCAmelCase__       ,   unittest.TestCase			):







						'''simple docstring'''





						lowerCAmelCase_     		=   CodeGenTokenizer
						lowerCAmelCase_     		=   CodeGenTokenizerFast
						lowerCAmelCase_     		=   True
						lowerCAmelCase_     		=   {'''add_prefix_space''': True}
						lowerCAmelCase_     		=   False






						def        snake_case__						(						self							:     List[Any]						):






							"""simple docstring"""






							super().setUp()

							# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
							snake_case_		    =     [
							    "l",
							    "o",
							    "w",
							    "e",
							    "r",
							    "s",
							    "t",
							    "i",
							    "d",
							    "n",
							    "\u0120",
							    "\u0120l",
							    "\u0120n",
							    "\u0120lo",
							    "\u0120low",
							    "er",
							    "\u0120lowest",
							    "\u0120newer",
							    "\u0120wider",
							    "<unk>",
							    "<|endoftext|>",
							]
							snake_case_		    =     dict(zip(__lowercase       ,	range(len(__lowercase						)						)						)						)
							snake_case_		    =     ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
							snake_case_		    =     {"unk_token": "<unk>"}

							snake_case_		    =     os.path.join(self.tmpdirname       ,	VOCAB_FILES_NAMES["vocab_file"]						)
							snake_case_		    =     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(__lowercase						) + "\n"						)
							with open(self.merges_file       ,	"w"       ,	encoding="utf-8"						) as fp:
								fp.write("\n".join(__lowercase						)						)






						def        snake_case__						(						self							:     Union[str, Any]       ,	**__lowercase							:     List[str]						):






							"""simple docstring"""






							kwargs.update(self.special_tokens_map						)
							return CodeGenTokenizer.from_pretrained(self.tmpdirname       ,	**__lowercase						)






						def        snake_case__						(						self							:     Optional[Any]       ,	**__lowercase							:     Union[str, Any]						):






							"""simple docstring"""






							kwargs.update(self.special_tokens_map						)
							return CodeGenTokenizerFast.from_pretrained(self.tmpdirname       ,	**__lowercase						)






						def        snake_case__						(						self							:     Optional[int]       ,	__lowercase							:     List[str]						):






							"""simple docstring"""






							snake_case_		    =     "lower newer"
							snake_case_		    =     "lower newer"
							return input_text, output_text






						def        snake_case__						(						self							:     Optional[Any]						):






							"""simple docstring"""






							snake_case_		    =     CodeGenTokenizer(self.vocab_file       ,	self.merges_file       ,	**self.special_tokens_map						)
							snake_case_		    =     "lower newer"
							snake_case_		    =     ["\u0120low", "er", "\u0120", "n", "e", "w", "er"]
							snake_case_		    =     tokenizer.tokenize(__lowercase       ,	add_prefix_space=__lowercase						)
							self.assertListEqual(__lowercase       ,	__lowercase						)

							snake_case_		    =     tokens + [tokenizer.unk_token]
							snake_case_		    =     [14, 15, 10, 9, 3, 2, 15, 19]
							self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase						)       ,	__lowercase						)






						def        snake_case__						(						self							:     Optional[int]						):






							"""simple docstring"""






							if not self.test_rust_tokenizer:
								return

							snake_case_		    =     self.get_tokenizer()
							snake_case_		    =     self.get_rust_tokenizer(add_prefix_space=__lowercase						)

							snake_case_		    =     "lower newer"

							# Testing tokenization
							snake_case_		    =     tokenizer.tokenize(__lowercase       ,	add_prefix_space=__lowercase						)
							snake_case_		    =     rust_tokenizer.tokenize(__lowercase						)
							self.assertListEqual(__lowercase       ,	__lowercase						)

							# Testing conversion to ids without special tokens
							snake_case_		    =     tokenizer.encode(__lowercase       ,	add_special_tokens=__lowercase       ,	add_prefix_space=__lowercase						)
							snake_case_		    =     rust_tokenizer.encode(__lowercase       ,	add_special_tokens=__lowercase						)
							self.assertListEqual(__lowercase       ,	__lowercase						)

							# Testing conversion to ids with special tokens
							snake_case_		    =     self.get_rust_tokenizer(add_prefix_space=__lowercase						)
							snake_case_		    =     tokenizer.encode(__lowercase       ,	add_prefix_space=__lowercase						)
							snake_case_		    =     rust_tokenizer.encode(__lowercase						)
							self.assertListEqual(__lowercase       ,	__lowercase						)

							# Testing the unknown token
							snake_case_		    =     tokens + [rust_tokenizer.unk_token]
							snake_case_		    =     [14, 15, 10, 9, 3, 2, 15, 19]
							self.assertListEqual(rust_tokenizer.convert_tokens_to_ids(__lowercase						)       ,	__lowercase						)






						def        snake_case__						(						self							:     Any       ,	*__lowercase							:     Union[str, Any]       ,	**__lowercase							:     Tuple						):






							"""simple docstring"""






							pass






						def        snake_case__						(						self							:     int       ,	__lowercase							:     str=15						):






							"""simple docstring"""






							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
								with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"						):
									snake_case_		    =     self.rust_tokenizer_class.from_pretrained(__lowercase       ,	**__lowercase						)

									# Simple input
									snake_case_		    =     "This is a simple input"
									snake_case_		    =     ["This is a simple input 1", "This is a simple input 2"]
									snake_case_		    =     ("This is a simple input", "This is a pair")
									snake_case_		    =     [
									    ("This is a simple input 1", "This is a simple input 2"),
									    ("This is a simple pair 1", "This is a simple pair 2"),
									]

									# Simple input tests
									self.assertRaises(__lowercase       ,	tokenizer_r.encode       ,	__lowercase       ,	max_length=__lowercase       ,	padding="max_length"						)

									# Simple input
									self.assertRaises(__lowercase       ,	tokenizer_r.encode_plus       ,	__lowercase       ,	max_length=__lowercase       ,	padding="max_length"						)

									# Simple input
									self.assertRaises(
									    __lowercase       ,	tokenizer_r.batch_encode_plus       ,	__lowercase       ,	max_length=__lowercase       ,	padding="max_length"       ,	)

									# Pair input
									self.assertRaises(__lowercase       ,	tokenizer_r.encode       ,	__lowercase       ,	max_length=__lowercase       ,	padding="max_length"						)

									# Pair input
									self.assertRaises(__lowercase       ,	tokenizer_r.encode_plus       ,	__lowercase       ,	max_length=__lowercase       ,	padding="max_length"						)

									# Pair input
									self.assertRaises(
									    __lowercase       ,	tokenizer_r.batch_encode_plus       ,	__lowercase       ,	max_length=__lowercase       ,	padding="max_length"       ,	)






						def        snake_case__						(						self							:     str						):






							"""simple docstring"""






							snake_case_		    =     CodeGenTokenizer.from_pretrained(self.tmpdirname       ,	pad_token="<pad>"						)

							# Simple input
							snake_case_		    =     "This is a simple input"
							snake_case_		    =     ["This is a simple input looooooooong", "This is a simple input"]
							snake_case_		    =     ("This is a simple input", "This is a pair")
							snake_case_		    =     [
							    ("This is a simple input loooooong", "This is a simple input"),
							    ("This is a simple pair loooooong", "This is a simple pair"),
							]

							snake_case_		    =     tokenizer.pad_token_id

							snake_case_		    =     tokenizer(__lowercase       ,	padding="max_length"       ,	max_length=30       ,	return_tensors="np"						)
							snake_case_		    =     tokenizer(__lowercase       ,	padding=__lowercase       ,	truncate=__lowercase       ,	return_tensors="np"						)
							snake_case_		    =     tokenizer(*__lowercase       ,	padding="max_length"       ,	max_length=60       ,	return_tensors="np"						)
							snake_case_		    =     tokenizer(__lowercase       ,	padding=__lowercase       ,	truncate=__lowercase       ,	return_tensors="np"						)

							# s
							# test single string max_length padding
							self.assertEqual(out_s["input_ids"].shape[-1]       ,	30						)
							self.assertTrue(pad_token_id in out_s["input_ids"]						)
							self.assertTrue(0 in out_s["attention_mask"]						)

							# s2
							# test automatic padding
							self.assertEqual(out_sa["input_ids"].shape[-1]       ,	33						)
							# long slice doesn't have padding
							self.assertFalse(pad_token_id in out_sa["input_ids"][0]						)
							self.assertFalse(0 in out_sa["attention_mask"][0]						)
							# short slice does have padding
							self.assertTrue(pad_token_id in out_sa["input_ids"][1]						)
							self.assertTrue(0 in out_sa["attention_mask"][1]						)

							# p
							# test single pair max_length padding
							self.assertEqual(out_p["input_ids"].shape[-1]       ,	60						)
							self.assertTrue(pad_token_id in out_p["input_ids"]						)
							self.assertTrue(0 in out_p["attention_mask"]						)

							# p2
							# test automatic padding pair
							self.assertEqual(out_pa["input_ids"].shape[-1]       ,	52						)
							# long slice pair doesn't have padding
							self.assertFalse(pad_token_id in out_pa["input_ids"][0]						)
							self.assertFalse(0 in out_pa["attention_mask"][0]						)
							# short slice pair does have padding
							self.assertTrue(pad_token_id in out_pa["input_ids"][1]						)
							self.assertTrue(0 in out_pa["attention_mask"][1]						)






						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_		    =     "$$$"
							snake_case_		    =     CodeGenTokenizer.from_pretrained(self.tmpdirname       ,	bos_token=__lowercase       ,	add_bos_token=__lowercase						)

							snake_case_		    =     "This is a simple input"
							snake_case_		    =     ["This is a simple input 1", "This is a simple input 2"]

							snake_case_		    =     tokenizer.bos_token_id

							snake_case_		    =     tokenizer(__lowercase						)
							snake_case_		    =     tokenizer(__lowercase						)

							self.assertEqual(out_s.input_ids[0]       ,	__lowercase						)
							self.assertTrue(all(o[0] == bos_token_id for o in out_sa.input_ids						)						)

							snake_case_		    =     tokenizer.decode(out_s.input_ids						)
							snake_case_		    =     tokenizer.batch_decode(out_sa.input_ids						)

							self.assertEqual(decode_s.split()[0]       ,	__lowercase						)
							self.assertTrue(all(d.split()[0] == bos_token for d in decode_sa						)						)






						@slow
						def        snake_case__						(						self							:     Tuple						):






							"""simple docstring"""






							snake_case_		    =     CodeGenTokenizer.from_pretrained("Salesforce/codegen-350M-mono"						)

							snake_case_		    =     "\nif len_a > len_b:\n    result = a\nelse:\n    result = b\n\n\n\n#"
							snake_case_		    =     "\nif len_a > len_b:      result = a\nelse:      result = b"

							snake_case_		    =     tokenizer.encode(__lowercase						)
							snake_case_		    =     ["^#", re.escape("<|endoftext|>"						), "^'''", "^\"\"\"", "\n\n\n"]
							snake_case_		    =     tokenizer.decode(__lowercase       ,	truncate_before_pattern=__lowercase						)
							self.assertEqual(__lowercase       ,	__lowercase						)






						def        snake_case__						(						self							:     Dict						):






							"""simple docstring"""






							pass
