



import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM




@require_torch
class    snake_case__		:






     """simple docstring"""


     def __init__(			self    :    Union[str, Any],					_snake_case    :    Any,					_snake_case    :    Dict=9_9,					_snake_case    :    int=1_3,					_snake_case    :    int=1_6,					_snake_case    :    Any=7,					_snake_case    :    Optional[int]=True,					_snake_case    :    List[Any]=True,					_snake_case    :    Any=True,					_snake_case    :    Optional[Any]=False,					_snake_case    :    Optional[int]=True,					_snake_case    :    str=2,					_snake_case    :    Union[str, Any]=3_2,					_snake_case    :    List[Any]=4,					_snake_case    :    int=4,					_snake_case    :    List[str]=3_0,					_snake_case    :    Any=0,					_snake_case    :    int=1,					_snake_case    :    str=2,					_snake_case    :    Optional[Any]=None,					)  ->Dict:
            snake_case__				:      int        =      parent
            snake_case__				:      Optional[Any]        =      batch_size
            snake_case__				:      Optional[Any]        =      decoder_seq_length
            # For common tests
            snake_case__				:      List[Any]        =      self.decoder_seq_length
            snake_case__				:      List[Any]        =      is_training
            snake_case__				:      Any        =      use_attention_mask
            snake_case__				:      Optional[Any]        =      use_labels

            snake_case__				:      Optional[int]        =      vocab_size
            snake_case__				:      List[Any]        =      d_model
            snake_case__				:      Optional[int]        =      d_model
            snake_case__				:      Tuple        =      decoder_layers
            snake_case__				:      List[Any]        =      decoder_layers
            snake_case__				:      Optional[int]        =      decoder_ffn_dim
            snake_case__				:      Optional[Any]        =      decoder_attention_heads
            snake_case__				:      Any        =      decoder_attention_heads
            snake_case__				:      Optional[int]        =      eos_token_id
            snake_case__				:      Tuple        =      bos_token_id
            snake_case__				:      Dict        =      pad_token_id
            snake_case__				:      Dict        =      decoder_start_token_id
            snake_case__				:      str        =      use_cache
            snake_case__				:      Union[str, Any]        =      max_position_embeddings

            snake_case__				:      Optional[int]        =      None
            snake_case__				:      str        =      decoder_seq_length
            snake_case__				:      Tuple        =      2
            snake_case__				:      Dict        =      1






     def   lowercase_  (			self    :    Any   )  ->Optional[Any]:
            snake_case__				:      str        =      ids_tensor([self.batch_size, self.decoder_seq_length],					self.vocab_size   )

            snake_case__				:      Dict        =      None
            if self.use_attention_mask:
                   snake_case__				:      Union[str, Any]        =      ids_tensor([self.batch_size, self.decoder_seq_length],					vocab_size=2   )

            snake_case__				:      List[Any]        =      None
            if self.use_labels:
                   snake_case__				:      List[Any]        =      ids_tensor([self.batch_size, self.decoder_seq_length],					self.vocab_size   )

            snake_case__				:      Dict        =      TrOCRConfig(
                vocab_size=self.vocab_size,					d_model=self.d_model,					decoder_layers=self.decoder_layers,					decoder_ffn_dim=self.decoder_ffn_dim,					decoder_attention_heads=self.decoder_attention_heads,					eos_token_id=self.eos_token_id,					bos_token_id=self.bos_token_id,					use_cache=self.use_cache,					pad_token_id=self.pad_token_id,					decoder_start_token_id=self.decoder_start_token_id,					max_position_embeddings=self.max_position_embeddings,					)

            return (config, input_ids, attention_mask, lm_labels)






     def   lowercase_  (			self    :    Tuple,					_snake_case    :    Tuple,					_snake_case    :    Optional[Any],					_snake_case    :    Union[str, Any],					_snake_case    :    Tuple,					)  ->Any:
            snake_case__				:      Any        =      True
            snake_case__				:      Dict        =      TrOCRDecoder(config=a__   ).to(a__   ).eval()
            snake_case__				:      List[Any]        =      input_ids[:2]

            input_ids[input_ids == 0] += 1
            # first forward pass
            snake_case__				:      List[str]        =      model(a__,					use_cache=a__   )
            snake_case__				:      List[Any]        =      model(a__   )
            snake_case__				:      List[str]        =      model(a__,					use_cache=a__   )

            self.parent.assertTrue(len(a__   ) == len(a__   )   )
            self.parent.assertTrue(len(a__   ) == len(a__   ) + 1   )

            snake_case__				:      Union[str, Any]        =      outputs['''past_key_values''']

            # create hypothetical next token and extent to next_input_ids
            snake_case__				:      int        =      ids_tensor((2, 1),					config.vocab_size - 1   ) + 1

            # append to next input_ids and
            snake_case__				:      List[Any]        =      torch.cat([input_ids, next_tokens],					dim=-1   )

            snake_case__				:      List[str]        =      model(a__   )['''last_hidden_state''']
            snake_case__				:      Dict        =      model(a__,					past_key_values=a__   )['''last_hidden_state''']

            # select random slice
            snake_case__				:      Any        =      ids_tensor((1,),					output_from_past.shape[-1]   ).item()
            snake_case__				:      Optional[Any]        =      output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
            snake_case__				:      List[Any]        =      output_from_past[:, 0, random_slice_idx].detach()

            # test that outputs are equal for slice
            assert torch.allclose(a__,					a__,					atol=1e-3   )






     def   lowercase_  (			self    :    List[str]   )  ->str:
            snake_case__				:      Tuple        =      self.prepare_config_and_inputs()
            snake_case__				:      Dict        =      config_and_inputs

            snake_case__				:      List[Any]        =      {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
            return config, inputs_dict




@require_torch
class    snake_case__		(			lowerCamelCase__							,	lowerCamelCase__							,	lowerCamelCase__							,	unittest.TestCase	):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE          =      (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
     _SCREAMING_SNAKE_CASE          =      (TrOCRForCausalLM,) if is_torch_available() else ()
     _SCREAMING_SNAKE_CASE          =      {"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
     _SCREAMING_SNAKE_CASE          =      True
     _SCREAMING_SNAKE_CASE          =      False






     def   lowercase_  (			self    :    List[str]   )  ->Union[str, Any]:
            snake_case__				:      List[str]        =      TrOCRStandaloneDecoderModelTester(self,					is_training=a__   )
            snake_case__				:      Optional[Any]        =      ConfigTester(self,					config_class=a__   )






     def   lowercase_  (			self    :    Union[str, Any]   )  ->Optional[Any]:
            pass






     def   lowercase_  (			self    :    Union[str, Any]   )  ->Tuple:
            pass






     def   lowercase_  (			self    :    int   )  ->Any:
            pass






     def   lowercase_  (			self    :    List[Any]   )  ->List[str]:
            self.config_tester.run_common_tests()






     def   lowercase_  (			self    :    List[str]   )  ->List[str]:
            snake_case__				:      List[Any]        =      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_decoder_model_past(*a__   )






     def   lowercase_  (			self    :    Tuple   )  ->Union[str, Any]:
            return






     @unittest.skip('The model doesn\'t support left padding'   )  # and it's not used enough to be worth fixing :)
     def   lowercase_  (			self    :    Optional[Any]   )  ->Optional[int]:
            pass













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




      """simple docstring"""







      def 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  List[str]						=		BlipImageProcessor()
             _lowerCamelCase				:  Union[str, Any]						=		GPTaTokenizer.from_pretrained('''hf-internal-testing/tiny-random-GPT2Model''')
             _lowerCamelCase				:  Any						=		BertTokenizerFast.from_pretrained('''hf-internal-testing/tiny-random-bert''')

             _lowerCamelCase				:  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				:  List[Any]						=		[np.random.randint(255   ,				size=(3, 30, 400)   ,				dtype=np.uinta)]

             _lowerCamelCase				:  List[str]						=		[Image.fromarray(np.moveaxis(a__   ,				0   ,				-1)) for x in image_inputs]

             return image_inputs


      def 					__snake_case      (		self):



             """simple docstring"""





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

             _lowerCamelCase				:  Optional[int]						=		self.get_tokenizer(bos_token='''(BOS)'''   ,				eos_token='''(EOS)''')
             _lowerCamelCase				:  str						=		self.get_image_processor(do_normalize=a__   ,				padding_value=1.0)

             _lowerCamelCase				:  Tuple						=		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				:  List[str]						=		self.get_image_processor()
             _lowerCamelCase				:  Optional[int]						=		self.get_tokenizer()
             _lowerCamelCase				:  Any						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  Dict						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		self.prepare_image_inputs()

             _lowerCamelCase				:  List[str]						=		image_processor(a__   ,				return_tensors='''np''')
             _lowerCamelCase				:  List[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				:  Any						=		self.get_image_processor()
             _lowerCamelCase				:  Dict						=		self.get_tokenizer()
             _lowerCamelCase				:  int						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  Tuple						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		'''lower newer'''

             _lowerCamelCase				:  Any						=		processor(text=a__)

             _lowerCamelCase				:  Optional[int]						=		tokenizer(a__   ,				return_token_type_ids=a__)
             _lowerCamelCase				:  Optional[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[Any]						=		self.get_image_processor()
             _lowerCamelCase				:  Optional[int]						=		self.get_tokenizer()
             _lowerCamelCase				:  Optional[Any]						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  List[Any]						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  List[Any]						=		'''lower newer'''
             _lowerCamelCase				:  Tuple						=		self.prepare_image_inputs()

             _lowerCamelCase				:  Tuple						=		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				:  int						=		self.get_image_processor()
             _lowerCamelCase				:  List[Any]						=		self.get_tokenizer()
             _lowerCamelCase				:  List[Any]						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  Optional[Any]						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

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

             _lowerCamelCase				:  Tuple						=		processor.batch_decode(a__)
             _lowerCamelCase				:  str						=		tokenizer.batch_decode(a__)

             self.assertListEqual(a__   ,				a__)



      def 					__snake_case      (		self):



             """simple docstring"""





             _lowerCamelCase				:  List[str]						=		self.get_image_processor()
             _lowerCamelCase				:  Dict						=		self.get_tokenizer()
             _lowerCamelCase				:  str						=		self.get_qformer_tokenizer()

             _lowerCamelCase				:  str						=		InstructBlipProcessor(
                 tokenizer=a__   ,				image_processor=a__   ,				qformer_tokenizer=a__)

             _lowerCamelCase				:  str						=		'''lower newer'''
             _lowerCamelCase				:  str						=		self.prepare_image_inputs()

             _lowerCamelCase				:  List[Any]						=		processor(text=a__   ,				images=a__)

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

