


'''simple docstring'''


import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
				from PIL import Image

				from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor





@require_vision
class        lowercase__    (		unittest.TestCase   ):





							'''simple docstring'''


							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			List[str]							= tempfile.mkdtemp()

											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= [
											    """[UNK]""",
											    """[CLS]""",
											    """[SEP]""",
											    """[PAD]""",
											    """[MASK]""",
											    """的""",
											    """价""",
											    """格""",
											    """是""",
											    """15""",
											    """便""",
											    """alex""",
											    """##andra""",
											    """，""",
											    """。""",
											    """-""",
											    """t""",
											    """shirt""",
											]
											_SCREAMING_SNAKE_CASE					:			Any							= os.path.join(self.tmpdirname       ,   VOCAB_FILES_NAMES["""vocab_file"""]    )
											with open(self.vocab_file       ,   """w"""       ,   encoding="""utf-8"""    ) as vocab_writer:
															vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]    )    )

											_SCREAMING_SNAKE_CASE					:			List[Any]							= {
											    """do_resize""": True,
											    """size""": {"""height""": 224, """width""": 224},
											    """do_center_crop""": True,
											    """crop_size""": {"""height""": 18, """width""": 18},
											    """do_normalize""": True,
											    """image_mean""": [0.48145466, 0.4578275, 0.40821073],
											    """image_std""": [0.26862954, 0.26130258, 0.27577711],
											    """do_convert_rgb""": True,
											}
											_SCREAMING_SNAKE_CASE					:			Tuple							= os.path.join(self.tmpdirname       ,   __snake_case    )
											with open(self.image_processor_file       ,   """w"""       ,   encoding="""utf-8"""    ) as fp:
															json.dump(__snake_case       ,   __snake_case    )






							def UpperCAmelCase_      (       self       ,   **__snake_case    ):
											return BertTokenizer.from_pretrained(self.tmpdirname       ,   **__snake_case    )






							def UpperCAmelCase_      (       self       ,   **__snake_case    ):
											return BertTokenizerFast.from_pretrained(self.tmpdirname       ,   **__snake_case    )






							def UpperCAmelCase_      (       self       ,   **__snake_case    ):
											return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname       ,   **__snake_case    )






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






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Tuple							= [np.random.randint(255       ,   size=(3, 30, 400)       ,   dtype=np.uinta    )]

											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= [Image.fromarray(np.moveaxis(__snake_case       ,   0       ,   -1    )    ) for x in image_inputs]

											return image_inputs






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			List[Any]							= self.get_tokenizer()
											_SCREAMING_SNAKE_CASE					:			int							= self.get_rust_tokenizer()
											_SCREAMING_SNAKE_CASE					:			str							= self.get_image_processor()

											_SCREAMING_SNAKE_CASE					:			List[Any]							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )
											processor_slow.save_pretrained(self.tmpdirname    )
											_SCREAMING_SNAKE_CASE					:			List[str]							= ChineseCLIPProcessor.from_pretrained(self.tmpdirname       ,   use_fast=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Any							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )
											processor_fast.save_pretrained(self.tmpdirname    )
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= ChineseCLIPProcessor.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       ,   __snake_case    )
											self.assertIsInstance(processor_fast.tokenizer       ,   __snake_case    )

											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       ,   __snake_case    )
											self.assertIsInstance(processor_fast.image_processor       ,   __snake_case    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= ChineseCLIPProcessor(tokenizer=self.get_tokenizer()       ,   image_processor=self.get_image_processor()    )
											processor.save_pretrained(self.tmpdirname    )

											_SCREAMING_SNAKE_CASE					:			Tuple							= self.get_tokenizer(cls_token="""(CLS)"""       ,   sep_token="""(SEP)"""    )
											_SCREAMING_SNAKE_CASE					:			int							= self.get_image_processor(do_normalize=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= ChineseCLIPProcessor.from_pretrained(
											    self.tmpdirname       ,   cls_token="""(CLS)"""       ,   sep_token="""(SEP)"""       ,   do_normalize=__snake_case    )

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

											self.assertEqual(processor.image_processor.to_json_string()       ,   image_processor_add_kwargs.to_json_string()    )
											self.assertIsInstance(processor.image_processor       ,   __snake_case    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			List[str]							= self.get_image_processor()
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= self.get_tokenizer()

											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Dict							= self.prepare_image_inputs()

											_SCREAMING_SNAKE_CASE					:			str							= image_processor(__snake_case       ,   return_tensors="""np"""    )
											_SCREAMING_SNAKE_CASE					:			Tuple							= processor(images=__snake_case       ,   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 UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			List[Any]							= self.get_image_processor()
											_SCREAMING_SNAKE_CASE					:			Dict							= self.get_tokenizer()

											_SCREAMING_SNAKE_CASE					:			List[Any]							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Tuple							= """Alexandra，T-shirt的价格是15便士。"""

											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= processor(text=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			str							= tokenizer(__snake_case    )

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






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= self.get_image_processor()
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= self.get_tokenizer()

											_SCREAMING_SNAKE_CASE					:			List[str]							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Any							= """Alexandra，T-shirt的价格是15便士。"""
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= self.prepare_image_inputs()

											_SCREAMING_SNAKE_CASE					:			Tuple							= processor(text=__snake_case       ,   images=__snake_case    )

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

											# test if it raises when no input is passed
											with pytest.raises(__snake_case    ):
															processor()






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= self.get_image_processor()
											_SCREAMING_SNAKE_CASE					:			List[str]							= self.get_tokenizer()

											_SCREAMING_SNAKE_CASE					:			Dict							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Tuple							= [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											_SCREAMING_SNAKE_CASE					:			Optional[int]							= processor.batch_decode(__snake_case    )
											_SCREAMING_SNAKE_CASE					:			Tuple							= tokenizer.batch_decode(__snake_case    )

											self.assertListEqual(__snake_case       ,   __snake_case    )






							def UpperCAmelCase_      (       self    ):
											_SCREAMING_SNAKE_CASE					:			Tuple							= self.get_image_processor()
											_SCREAMING_SNAKE_CASE					:			Any							= self.get_tokenizer()

											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= ChineseCLIPProcessor(tokenizer=__snake_case       ,   image_processor=__snake_case    )

											_SCREAMING_SNAKE_CASE					:			Tuple							= """Alexandra，T-shirt的价格是15便士。"""
											_SCREAMING_SNAKE_CASE					:			int							= self.prepare_image_inputs()

											_SCREAMING_SNAKE_CASE					:			Tuple							= processor(text=__snake_case       ,   images=__snake_case    )

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








'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


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

UpperCAmelCase_						:  Any				  =						{
    'bigcode/gpt_bigcode-santacoder': 'https://huggingface.co/bigcode/gpt_bigcode-santacoder/resolve/main/config.json',
}





class        lowercase__    (		_snake_case   ):





							'''simple docstring'''


							A_				: Dict       =    """gpt_bigcode"""
							A_				: str       =    ["""past_key_values"""]
							A_				: Tuple       =    {
							    """hidden_size""": """n_embd""",
							    """max_position_embeddings""": """n_positions""",
							    """num_attention_heads""": """n_head""",
							    """num_hidden_layers""": """n_layer""",
							}






							def __init__(       self       ,   __snake_case=5_0257       ,   __snake_case=1024       ,   __snake_case=768       ,   __snake_case=12       ,   __snake_case=12       ,   __snake_case=None       ,   __snake_case="gelu_pytorch_tanh"       ,   __snake_case=0.1       ,   __snake_case=0.1       ,   __snake_case=0.1       ,   __snake_case=1e-5       ,   __snake_case=0.02       ,   __snake_case=True       ,   __snake_case=True       ,   __snake_case=5_0256       ,   __snake_case=5_0256       ,   __snake_case=True       ,   __snake_case=True       ,   __snake_case=True       ,   **__snake_case       ,   ):
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= vocab_size
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= n_positions
											_SCREAMING_SNAKE_CASE					:			List[Any]							= n_embd
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= n_layer
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= n_head
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= n_inner
											_SCREAMING_SNAKE_CASE					:			Optional[Any]							= activation_function
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= resid_pdrop
											_SCREAMING_SNAKE_CASE					:			Union[str, Any]							= embd_pdrop
											_SCREAMING_SNAKE_CASE					:			List[Any]							= attn_pdrop
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= layer_norm_epsilon
											_SCREAMING_SNAKE_CASE					:			Any							= initializer_range
											_SCREAMING_SNAKE_CASE					:			str							= scale_attn_weights
											_SCREAMING_SNAKE_CASE					:			Tuple							= use_cache
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= attention_softmax_in_fpaa
											_SCREAMING_SNAKE_CASE					:			List[Any]							= scale_attention_softmax_in_fpaa
											_SCREAMING_SNAKE_CASE					:			Any							= multi_query

											_SCREAMING_SNAKE_CASE					:			Optional[int]							= bos_token_id
											_SCREAMING_SNAKE_CASE					:			Optional[int]							= eos_token_id

											super().__init__(bos_token_id=__snake_case       ,   eos_token_id=__snake_case       ,   **__snake_case    )




