


'''simple docstring'''
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    lowercase	:

		"""simple docstring"""

		def __init__(		self    ,   UpperCamelCase_    ,   UpperCamelCase_=99    ,   UpperCamelCase_=13    ,   UpperCamelCase_=16    ,   UpperCamelCase_=7    ,   UpperCamelCase_=True    ,   UpperCamelCase_=True    ,   UpperCamelCase_=True    ,   UpperCamelCase_=False    ,   UpperCamelCase_=True    ,   UpperCamelCase_=2    ,   UpperCamelCase_=32    ,   UpperCamelCase_=4    ,   UpperCamelCase_=4    ,   UpperCamelCase_=30    ,   UpperCamelCase_=0    ,   UpperCamelCase_=1    ,   UpperCamelCase_=2    ,   UpperCamelCase_=None    ,   ):

							'''simple docstring'''


							UpperCamelCase__   :List[str]												= parent
							UpperCamelCase__   :Optional[int]												= batch_size
							UpperCamelCase__   :Any												= decoder_seq_length
							# For common tests
							UpperCamelCase__   :List[Any]												= self.decoder_seq_length
							UpperCamelCase__   :Union[str, Any]												= is_training
							UpperCamelCase__   :List[str]												= use_attention_mask
							UpperCamelCase__   :Tuple												= use_labels

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

							UpperCamelCase__   :Tuple												= None
							UpperCamelCase__   :List[Any]												= decoder_seq_length
							UpperCamelCase__   :List[str]												= 2
							UpperCamelCase__   :List[str]												= 1




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Union[str, Any]												= ids_tensor([self.batch_size, self.decoder_seq_length]    ,   self.vocab_size							)

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

							UpperCamelCase__   :str												= None
							if self.use_labels:
												UpperCamelCase__   :Tuple												= ids_tensor([self.batch_size, self.decoder_seq_length]    ,   self.vocab_size							)

							UpperCamelCase__   :Tuple												= 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     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   ):

							'''simple docstring'''


							UpperCamelCase__   :int												= True
							UpperCamelCase__   :int												= TrOCRDecoder(config=UpperCamelCase_							).to(UpperCamelCase_							).eval()
							UpperCamelCase__   :Any												= input_ids[:2]

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

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

							UpperCamelCase__   :Optional[int]												= outputs['''past_key_values''']

							# create hypothetical next token and extent to next_input_ids
							UpperCamelCase__   :Union[str, Any]												= ids_tensor((2, 1)    ,   config.vocab_size - 1							) + 1

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

							UpperCamelCase__   :Dict												= model(UpperCamelCase_							)['''last_hidden_state''']
							UpperCamelCase__   :Dict												= model(UpperCamelCase_    ,   past_key_values=UpperCamelCase_							)['''last_hidden_state''']

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

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




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Union[str, Any]												= self.prepare_config_and_inputs()
							UpperCamelCase__					,				UpperCamelCase__					,				UpperCamelCase__					,				UpperCamelCase__   :Union[str, Any]												= config_and_inputs

							UpperCamelCase__   :Optional[int]												= {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
							return config, inputs_dict



@require_torch
class    lowercase	(							A__	,		A__	,		A__	,		unittest.TestCase					):

		"""simple docstring"""

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




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :Union[str, Any]												= TrOCRStandaloneDecoderModelTester(self    ,   is_training=UpperCamelCase_							)
							UpperCamelCase__   :Dict												= ConfigTester(self    ,   config_class=UpperCamelCase_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							pass




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							pass




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							pass




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							self.config_tester.run_common_tests()




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :List[Any]												= self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase_							)




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							return




		@unittest.skip('''The model doesn\'t support left padding'''							)  # and it's not used enough to be worth fixing :)
		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							pass





import unittest

import numpy as np

from diffusers import OnnxStableDiffusionInpaintPipelineLegacy
from diffusers.utils.testing_utils import (
    is_onnx_available,
    load_image,
    load_numpy,
    nightly,
    require_onnxruntime,
    require_torch_gpu,
)


if is_onnx_available():
   import onnxruntime as ort





@nightly
@require_onnxruntime
@require_torch_gpu
class        lowerCamelCase_							(      unittest.TestCase					):


 '''simple docstring'''







 @property
 def       lowerCAmelCase_  (							self  :		Optional[Any]						):
       return (
           "CUDAExecutionProvider",
           {
               "gpu_mem_limit": "15000000000",  # 15GB
               "arena_extend_strategy": "kSameAsRequested",
           },
       )



 @property
 def       lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_							=			ort.SessionOptions()
       SCREAMING_SNAKE_CASE_							=			False
       return options




 def       lowerCAmelCase_  (							self  :		Tuple						):
       SCREAMING_SNAKE_CASE_							=			load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/in_paint/overture-creations-5sI6fQgYIuo.png'						)
       SCREAMING_SNAKE_CASE_							=			load_image(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/in_paint/overture-creations-5sI6fQgYIuo_mask.png'						)
       SCREAMING_SNAKE_CASE_							=			load_numpy(
           'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
           '/in_paint/red_cat_sitting_on_a_park_bench_onnx.npy'						)

       # using the PNDM scheduler by default
       SCREAMING_SNAKE_CASE_							=			OnnxStableDiffusionInpaintPipelineLegacy.from_pretrained(
           'CompVis/stable-diffusion-v1-4'				,				revision='onnx'				,				safety_checker=_lowerCAmelCase				,				feature_extractor=_lowerCAmelCase				,				provider=self.gpu_provider				,				sess_options=self.gpu_options				,				)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase						)

       SCREAMING_SNAKE_CASE_							=			'A red cat sitting on a park bench'

       SCREAMING_SNAKE_CASE_							=			np.random.RandomState(0						)
       SCREAMING_SNAKE_CASE_							=			pipe(
           prompt=_lowerCAmelCase				,				image=_lowerCAmelCase				,				mask_image=_lowerCAmelCase				,				strength=0.75				,				guidance_scale=7.5				,				num_inference_steps=15				,				generator=_lowerCAmelCase				,				output_type='np'				,				)

       SCREAMING_SNAKE_CASE_							=			output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image						).max() < 1E-2