



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   UpperCAmelCase_    (       unittest.TestCase):







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Any    =    tempfile.mkdtemp()

														_lowerCAmelCase     :							int    =    BlipImageProcessor()
														_lowerCAmelCase     :							Dict    =    GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model")
														_lowerCAmelCase     :							Tuple    =    BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert")

														_lowerCAmelCase     :							Optional[Any]    =    InstructBlipProcessor(__a,			__a,			__a)

														processor.save_pretrained(self.tmpdirname)







							def    snake_case__      (   self,			**__a):

														'''simple docstring'''

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







							def    snake_case__      (   self,			**__a):

														'''simple docstring'''

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







							def    snake_case__      (   self,			**__a):

														'''simple docstring'''

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







							def    snake_case__      (   self):

														'''simple docstring'''

														shutil.rmtree(self.tmpdirname)







							def    snake_case__      (   self):

														'''simple docstring'''

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

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

														return image_inputs







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							List[Any]    =    InstructBlipProcessor(
														    tokenizer=self.get_tokenizer(),			image_processor=self.get_image_processor(),			qformer_tokenizer=self.get_qformer_tokenizer(),			)
														processor.save_pretrained(self.tmpdirname)

														_lowerCAmelCase     :							str    =    self.get_tokenizer(bos_token="(BOS)",			eos_token="(EOS)")
														_lowerCAmelCase     :							int    =    self.get_image_processor(do_normalize=__a,			padding_value=1.0)

														_lowerCAmelCase     :							str    =    InstructBlipProcessor.from_pretrained(
														    self.tmpdirname,			bos_token="(BOS)",			eos_token="(EOS)",			do_normalize=__a,			padding_value=1.0)

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

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







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    self.get_image_processor()
														_lowerCAmelCase     :							int    =    self.get_tokenizer()
														_lowerCAmelCase     :							int    =    self.get_qformer_tokenizer()

														_lowerCAmelCase     :							Dict    =    InstructBlipProcessor(
														    tokenizer=__a,			image_processor=__a,			qformer_tokenizer=__a)

														_lowerCAmelCase     :							int    =    self.prepare_image_inputs()

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

														'''simple docstring'''

														_lowerCAmelCase     :							Optional[int]    =    self.get_image_processor()
														_lowerCAmelCase     :							Optional[Any]    =    self.get_tokenizer()
														_lowerCAmelCase     :							List[str]    =    self.get_qformer_tokenizer()

														_lowerCAmelCase     :							Union[str, Any]    =    InstructBlipProcessor(
														    tokenizer=__a,			image_processor=__a,			qformer_tokenizer=__a)

														_lowerCAmelCase     :							List[str]    =    "lower newer"

														_lowerCAmelCase     :							str    =    processor(text=__a)

														_lowerCAmelCase     :							Dict    =    tokenizer(__a,			return_token_type_ids=__a)
														_lowerCAmelCase     :							Union[str, Any]    =    qformer_tokenizer(__a,			return_token_type_ids=__a)

														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    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							List[str]    =    self.get_image_processor()
														_lowerCAmelCase     :							List[str]    =    self.get_tokenizer()
														_lowerCAmelCase     :							Optional[int]    =    self.get_qformer_tokenizer()

														_lowerCAmelCase     :							List[str]    =    InstructBlipProcessor(
														    tokenizer=__a,			image_processor=__a,			qformer_tokenizer=__a)

														_lowerCAmelCase     :							int    =    "lower newer"
														_lowerCAmelCase     :							Optional[Any]    =    self.prepare_image_inputs()

														_lowerCAmelCase     :							str    =    processor(text=__a,			images=__a)

														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(__a):
																					processor()







							def    snake_case__      (   self):

														'''simple docstring'''

														_lowerCAmelCase     :							Tuple    =    self.get_image_processor()
														_lowerCAmelCase     :							Union[str, Any]    =    self.get_tokenizer()
														_lowerCAmelCase     :							Dict    =    self.get_qformer_tokenizer()

														_lowerCAmelCase     :							Dict    =    InstructBlipProcessor(
														    tokenizer=__a,			image_processor=__a,			qformer_tokenizer=__a)

														_lowerCAmelCase     :							Union[str, Any]    =    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

														_lowerCAmelCase     :							str    =    processor.batch_decode(__a)
														_lowerCAmelCase     :							List[str]    =    tokenizer.batch_decode(__a)

														self.assertListEqual(__a,			__a)







							def    snake_case__      (   self):

														'''simple docstring'''

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

														_lowerCAmelCase     :							Dict    =    InstructBlipProcessor(
														    tokenizer=__a,			image_processor=__a,			qformer_tokenizer=__a)

														_lowerCAmelCase     :							Optional[Any]    =    "lower newer"
														_lowerCAmelCase     :							Tuple    =    self.prepare_image_inputs()

														_lowerCAmelCase     :							List[str]    =    processor(text=__a,			images=__a)

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




'''simple docstring'''


from typing import Dict

import numpy as np
import torch

from . import residue_constants as rc
from .tensor_utils import tensor_tree_map, tree_map







def 			snake_case_   (  SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Tuple     		=      []
				_SCREAMING_SNAKE_CASE :       Dict     		=      []
				_SCREAMING_SNAKE_CASE :       str     		=      []

				for rt in rc.restypes:
								_SCREAMING_SNAKE_CASE :       Optional[int]     		=      rc.restype_name_to_atomaa_names[rc.restype_atoa[rt]]
								restype_atomaa_to_atomaa_list.append([(rc.atom_order[name] if name else 0) for name in atom_names]     )
								_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      {name: i for i, name in enumerate(SCREAMING_SNAKE_CASE__     )}
								restype_atomaa_to_atomaa_list.append(
								    [(atom_name_to_idxaa[name] if name in atom_name_to_idxaa else 0) for name in rc.atom_types]     )

								restype_atomaa_mask_list.append([(1.0 if name else 0.0) for name in atom_names]     )

				# Add dummy mapping for restype 'UNK'
				restype_atomaa_to_atomaa_list.append([0] * 14     )
				restype_atomaa_to_atomaa_list.append([0] * 37     )
				restype_atomaa_mask_list.append([0.0] * 14     )

				_SCREAMING_SNAKE_CASE :       Dict     		=      torch.tensor(
				    SCREAMING_SNAKE_CASE__							,      dtype=torch.intaa							,      device=protein["""aatype"""].device							,      )
				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      torch.tensor(
				    SCREAMING_SNAKE_CASE__							,      dtype=torch.intaa							,      device=protein["""aatype"""].device							,      )
				_SCREAMING_SNAKE_CASE :       str     		=      torch.tensor(
				    SCREAMING_SNAKE_CASE__							,      dtype=torch.floataa							,      device=protein["""aatype"""].device							,      )
				_SCREAMING_SNAKE_CASE :       int     		=      protein["""aatype"""].to(torch.long     )

				# create the mapping for (residx, atom14) --> atom37, i.e. an array
				# with shape (num_res, 14) containing the atom37 indices for this protein
				_SCREAMING_SNAKE_CASE :       List[str]     		=      restype_atomaa_to_atomaa[protein_aatype]
				_SCREAMING_SNAKE_CASE :       str     		=      restype_atomaa_mask[protein_aatype]

				_SCREAMING_SNAKE_CASE :       List[Any]     		=      residx_atomaa_mask
				_SCREAMING_SNAKE_CASE :       List[str]     		=      residx_atomaa_to_atomaa.long()

				# create the gather indices for mapping back
				_SCREAMING_SNAKE_CASE :       int     		=      restype_atomaa_to_atomaa[protein_aatype]
				_SCREAMING_SNAKE_CASE :       Dict     		=      residx_atomaa_to_atomaa.long()

				# create the corresponding mask
				_SCREAMING_SNAKE_CASE :       str     		=      torch.zeros([21, 37]							,      dtype=torch.floataa							,      device=protein["""aatype"""].device     )
				for restype, restype_letter in enumerate(rc.restypes     ):
								_SCREAMING_SNAKE_CASE :       int     		=      rc.restype_atoa[restype_letter]
								_SCREAMING_SNAKE_CASE :       Dict     		=      rc.residue_atoms[restype_name]
								for atom_name in atom_names:
												_SCREAMING_SNAKE_CASE :       List[Any]     		=      rc.atom_order[atom_name]
												_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      1

				_SCREAMING_SNAKE_CASE :       int     		=      restype_atomaa_mask[protein_aatype]
				_SCREAMING_SNAKE_CASE :       int     		=      residx_atomaa_mask

				return protein







def 			snake_case_   (  SCREAMING_SNAKE_CASE__     ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      tree_map(lambda SCREAMING_SNAKE_CASE__     : torch.tensor(SCREAMING_SNAKE_CASE__							,      device=batch["""aatype"""].device     )							,      SCREAMING_SNAKE_CASE__							,      np.ndarray     )
				_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      tensor_tree_map(lambda SCREAMING_SNAKE_CASE__     : np.array(SCREAMING_SNAKE_CASE__     )							,      make_atomaa_masks(SCREAMING_SNAKE_CASE__     )     )
				return out




