





'''simple docstring'''





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,
         BertTokenizerFast,
         BlipImageProcessor,
         GPTaTokenizer,
         InstructBlipProcessor,
         PreTrainedTokenizerFast,
     )



@require_vision
class      _SCREAMING_SNAKE_CASE		(			unittest.TestCase					):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=tempfile.mkdtemp()

   lowerCamelCase__:   Optional[Any]    						=BlipImageProcessor()
   lowerCamelCase__:   List[str]    						=GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model")
   lowerCamelCase__:   Optional[int]    						=BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert")

   lowerCamelCase__:   Tuple    						=InstructBlipProcessor(UpperCAmelCase__ ,				UpperCAmelCase__ ,				UpperCAmelCase__)

   processor.save_pretrained(self.tmpdirname)

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any] ,				**UpperCAmelCase_       :     Optional[int]) ->str:



   '''simple docstring'''



   return AutoProcessor.from_pretrained(self.tmpdirname ,				**UpperCAmelCase__).tokenizer

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str] ,				**UpperCAmelCase_       :     Any) ->str:



   '''simple docstring'''



   return AutoProcessor.from_pretrained(self.tmpdirname ,				**UpperCAmelCase__).image_processor

 def 							SCREAMING_SNAKE_CASE_      (self       :     str ,				**UpperCAmelCase_       :     List[Any]) ->Dict:



   '''simple docstring'''



   return AutoProcessor.from_pretrained(self.tmpdirname ,				**UpperCAmelCase__).qformer_tokenizer

 def 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->Any:



   '''simple docstring'''



   shutil.rmtree(self.tmpdirname)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->str:



   '''simple docstring'''



   lowerCamelCase__:   int    						=[np.random.randint(255 ,				size=(3, 30, 400) ,				dtype=np.uinta)]

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

   return image_inputs

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=InstructBlipProcessor(
       tokenizer=self.get_tokenizer() ,				image_processor=self.get_image_processor() ,				qformer_tokenizer=self.get_qformer_tokenizer() ,				)
   processor.save_pretrained(self.tmpdirname)

   lowerCamelCase__:   Dict    						=self.get_tokenizer(bos_token="(BOS)" ,				eos_token="(EOS)")
   lowerCamelCase__:   Tuple    						=self.get_image_processor(do_normalize=UpperCAmelCase__ ,				padding_value=1.0)

   lowerCamelCase__:   Dict    						=InstructBlipProcessor.from_pretrained(
       self.tmpdirname ,				bos_token="(BOS)" ,				eos_token="(EOS)" ,				do_normalize=UpperCAmelCase__ ,				padding_value=1.0)

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

   self.assertEqual(processor.image_processor.to_json_string() ,				image_processor_add_kwargs.to_json_string())
   self.assertIsInstance(processor.image_processor ,				UpperCAmelCase__)
   self.assertIsInstance(processor.qformer_tokenizer ,				UpperCAmelCase__)

 def 							SCREAMING_SNAKE_CASE_      (self       :     Union[str, Any]) ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:   Tuple    						=self.get_image_processor()
   lowerCamelCase__:   Optional[int]    						=self.get_tokenizer()
   lowerCamelCase__:   Union[str, Any]    						=self.get_qformer_tokenizer()

   lowerCamelCase__:   int    						=InstructBlipProcessor(
       tokenizer=UpperCAmelCase__ ,				image_processor=UpperCAmelCase__ ,				qformer_tokenizer=UpperCAmelCase__)

   lowerCamelCase__:   Optional[Any]    						=self.prepare_image_inputs()

   lowerCamelCase__:   Any    						=image_processor(UpperCAmelCase__ ,				return_tensors="np")
   lowerCamelCase__:   Any    						=processor(images=UpperCAmelCase__ ,				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 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[Any]    						=self.get_image_processor()
   lowerCamelCase__:   str    						=self.get_tokenizer()
   lowerCamelCase__:   Any    						=self.get_qformer_tokenizer()

   lowerCamelCase__:   int    						=InstructBlipProcessor(
       tokenizer=UpperCAmelCase__ ,				image_processor=UpperCAmelCase__ ,				qformer_tokenizer=UpperCAmelCase__)

   lowerCamelCase__:   Any    						='''lower newer'''

   lowerCamelCase__:   List[Any]    						=processor(text=UpperCAmelCase__)

   lowerCamelCase__:   int    						=tokenizer(UpperCAmelCase__ ,				return_token_type_ids=UpperCAmelCase__)
   lowerCamelCase__:   Optional[Any]    						=qformer_tokenizer(UpperCAmelCase__ ,				return_token_type_ids=UpperCAmelCase__)

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

   for key in encoded_tokens_qformer.keys():
     self.assertListEqual(encoded_tokens_qformer[key] ,				encoded_processor["qformer_" + key])

 def 							SCREAMING_SNAKE_CASE_      (self       :     Any) ->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:   Optional[int]    						=self.get_image_processor()
   lowerCamelCase__:   Union[str, Any]    						=self.get_tokenizer()
   lowerCamelCase__:   int    						=self.get_qformer_tokenizer()

   lowerCamelCase__:   List[Any]    						=InstructBlipProcessor(
       tokenizer=UpperCAmelCase__ ,				image_processor=UpperCAmelCase__ ,				qformer_tokenizer=UpperCAmelCase__)

   lowerCamelCase__:   int    						='''lower newer'''
   lowerCamelCase__:   Optional[Any]    						=self.prepare_image_inputs()

   lowerCamelCase__:   Union[str, Any]    						=processor(text=UpperCAmelCase__ ,				images=UpperCAmelCase__)

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

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

 def 							SCREAMING_SNAKE_CASE_      (self       :     str) ->int:



   '''simple docstring'''



   lowerCamelCase__:   int    						=self.get_image_processor()
   lowerCamelCase__:   int    						=self.get_tokenizer()
   lowerCamelCase__:   List[Any]    						=self.get_qformer_tokenizer()

   lowerCamelCase__:   List[str]    						=InstructBlipProcessor(
       tokenizer=UpperCAmelCase__ ,				image_processor=UpperCAmelCase__ ,				qformer_tokenizer=UpperCAmelCase__)

   lowerCamelCase__:   Any    						=[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

   lowerCamelCase__:   List[Any]    						=processor.batch_decode(UpperCAmelCase__)
   lowerCamelCase__:   int    						=tokenizer.batch_decode(UpperCAmelCase__)

   self.assertListEqual(UpperCAmelCase__ ,				UpperCAmelCase__)







 def 							SCREAMING_SNAKE_CASE_      (self       :     Optional[int]) ->Dict:



   '''simple docstring'''



   lowerCamelCase__:   Any    						=self.get_image_processor()
   lowerCamelCase__:   Any    						=self.get_tokenizer()
   lowerCamelCase__:   List[str]    						=self.get_qformer_tokenizer()

   lowerCamelCase__:   Union[str, Any]    						=InstructBlipProcessor(
       tokenizer=UpperCAmelCase__ ,				image_processor=UpperCAmelCase__ ,				qformer_tokenizer=UpperCAmelCase__)

   lowerCamelCase__:   str    						='''lower newer'''
   lowerCamelCase__:   List[Any]    						=self.prepare_image_inputs()

   lowerCamelCase__:   Optional[int]    						=processor(text=UpperCAmelCase__ ,				images=UpperCAmelCase__)

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








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


__A             =       logging.get_logger(__name__)

__A             =       {
    "tiiuae/falcon-40b": "https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json",
    "tiiuae/falcon-7b": "https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json",
}



class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    "falcon"
 lowercase_	=    ["past_key_values"]

 def __init__(self       :     str ,				UpperCAmelCase_       :     Optional[Any]=65_024 ,				UpperCAmelCase_       :     Union[str, Any]=4_544 ,				UpperCAmelCase_       :     Any=32 ,				UpperCAmelCase_       :     str=71 ,				UpperCAmelCase_       :     int=1E-5 ,				UpperCAmelCase_       :     int=0.02 ,				UpperCAmelCase_       :     int=True ,				UpperCAmelCase_       :     Tuple=0.0 ,				UpperCAmelCase_       :     str=0.0 ,				UpperCAmelCase_       :     List[Any]=None ,				UpperCAmelCase_       :     Any=False ,				UpperCAmelCase_       :     Tuple=False ,				UpperCAmelCase_       :     List[Any]=True ,				UpperCAmelCase_       :     Dict=True ,				UpperCAmelCase_       :     List[str]=False ,				UpperCAmelCase_       :     str=11 ,				UpperCAmelCase_       :     List[Any]=11 ,				**UpperCAmelCase_       :     str ,				) ->Any:



   '''simple docstring'''



   lowerCamelCase__:   Dict    						=vocab_size
   # Backward compatibility with n_embed kwarg
   lowerCamelCase__:   List[Any]    						=kwargs.pop("n_embed" ,				UpperCAmelCase_)
   lowerCamelCase__:   Dict    						=hidden_size if n_embed is None else n_embed
   lowerCamelCase__:   Tuple    						=num_hidden_layers
   lowerCamelCase__:   Dict    						=num_attention_heads
   lowerCamelCase__:   Optional[Any]    						=layer_norm_epsilon
   lowerCamelCase__:   Union[str, Any]    						=initializer_range
   lowerCamelCase__:   Union[str, Any]    						=use_cache
   lowerCamelCase__:   str    						=hidden_dropout
   lowerCamelCase__:   Tuple    						=attention_dropout

   lowerCamelCase__:   Dict    						=bos_token_id
   lowerCamelCase__:   Union[str, Any]    						=eos_token_id
   lowerCamelCase__:   Any    						=num_attention_heads if num_kv_heads is None else num_kv_heads
   lowerCamelCase__:   Dict    						=alibi
   lowerCamelCase__:   Optional[Any]    						=new_decoder_architecture
   lowerCamelCase__:   List[str]    						=multi_query  # Ignored when new_decoder_architecture is True
   lowerCamelCase__:   Tuple    						=parallel_attn
   lowerCamelCase__:   Optional[Any]    						=bias

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

 @property
 def 							SCREAMING_SNAKE_CASE_      (self       :     List[Any]) ->Dict:



   '''simple docstring'''



   return self.hidden_size // self.num_attention_heads







 @property
 def 							SCREAMING_SNAKE_CASE_      (self       :     Dict) ->List[Any]:



   '''simple docstring'''



   return not self.alibi

