






'''simple docstring'''







import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch

@require_torch
class _lowercase			:




							@property
							def 	lowerCamelCase_       (							self:       Tuple  ):
														return self.get_dummy_input()




							@property
							def 	lowerCamelCase_       (							self:       Union[str, Any]  ):
														if self.block_type == "down":
																					return (4, 32, 16, 16)
														elif self.block_type == "mid":
																					return (4, 32, 32, 32)
														elif self.block_type == "up":
																					return (4, 32, 64, 64)

														raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''  )




							def 	lowerCamelCase_       (							self:       Optional[Any]    ,							UpperCamelCase__:       List[Any]=True    ,							UpperCamelCase__:       Optional[Any]=False    ,							UpperCamelCase__:       List[Any]=False    ,							UpperCamelCase__:       Tuple=False    ,							):
														lowerCamelCase__      :			List[Any] 						= 4
														lowerCamelCase__      :			int 						= 32
														lowerCamelCase__      :			List[Any] 						= (32, 32)

														lowerCamelCase__      :			Dict 						= torch.manual_seed(0  )
														lowerCamelCase__      :			Union[str, Any] 						= torch.device(UpperCamelCase__  )
														lowerCamelCase__      :			Union[str, Any] 						= (batch_size, num_channels) + sizes
														lowerCamelCase__      :			Any 						= randn_tensor(UpperCamelCase__    ,							generator=UpperCamelCase__    ,							device=UpperCamelCase__  )
														lowerCamelCase__      :			Union[str, Any] 						= {"""hidden_states""": hidden_states}

														if include_temb:
																					lowerCamelCase__      :			Union[str, Any] 						= 128
																					lowerCamelCase__      :			Optional[Any] 						= randn_tensor((batch_size, temb_channels)    ,							generator=UpperCamelCase__    ,							device=UpperCamelCase__  )

														if include_res_hidden_states_tuple:
																					lowerCamelCase__      :			Dict 						= torch.manual_seed(1  )
																					lowerCamelCase__      :			Any 						= (randn_tensor(UpperCamelCase__    ,							generator=UpperCamelCase__    ,							device=UpperCamelCase__  ),)

														if include_encoder_hidden_states:
																					lowerCamelCase__      :			List[Any] 						= floats_tensor((batch_size, 32, 32)  ).to(UpperCamelCase__  )

														if include_skip_sample:
																					lowerCamelCase__      :			Optional[Any] 						= randn_tensor(((batch_size, 3) + sizes)    ,							generator=UpperCamelCase__    ,							device=UpperCamelCase__  )

														return dummy_input




							def 	lowerCamelCase_       (							self:       Any  ):
														lowerCamelCase__      :			Any 						= {
														    """in_channels""": 32,
														    """out_channels""": 32,
														    """temb_channels""": 128,
														}
														if self.block_type == "up":
																					lowerCamelCase__      :			str 						= 32

														if self.block_type == "mid":
																					init_dict.pop("""out_channels"""  )

														lowerCamelCase__      :			Tuple 						= self.dummy_input
														return init_dict, inputs_dict




							def 	lowerCamelCase_       (							self:       Optional[int]    ,							UpperCamelCase__:       Tuple  ):
														lowerCamelCase__						,	lowerCamelCase__      :			int 						= self.prepare_init_args_and_inputs_for_common()
														lowerCamelCase__      :			str 						= self.block_class(**UpperCamelCase__  )
														unet_block.to(UpperCamelCase__  )
														unet_block.eval()

														with torch.no_grad():
																					lowerCamelCase__      :			int 						= unet_block(**UpperCamelCase__  )

														if isinstance(UpperCamelCase__    ,							UpperCamelCase__  ):
																					lowerCamelCase__      :			Optional[int] 						= output[0]

														self.assertEqual(output.shape    ,							self.output_shape  )

														lowerCamelCase__      :			List[str] 						= output[0, -1, -3:, -3:]
														lowerCamelCase__      :			Dict 						= torch.tensor(UpperCamelCase__  ).to(UpperCamelCase__  )
														assert torch_all_close(output_slice.flatten()    ,							UpperCamelCase__    ,							atol=5e-3  )




							@unittest.skipIf(torch_device == """mps"""    ,							"""Training is not supported in mps"""  )
							def 	lowerCamelCase_       (							self:       Tuple  ):
														lowerCamelCase__						,	lowerCamelCase__      :			Tuple 						= self.prepare_init_args_and_inputs_for_common()
														lowerCamelCase__      :			Union[str, Any] 						= self.block_class(**UpperCamelCase__  )
														model.to(UpperCamelCase__  )
														model.train()
														lowerCamelCase__      :			Optional[Any] 						= model(**UpperCamelCase__  )

														if isinstance(UpperCamelCase__    ,							UpperCamelCase__  ):
																					lowerCamelCase__      :			str 						= output[0]

														lowerCamelCase__      :			Optional[int] 						= torch.device(UpperCamelCase__  )
														lowerCamelCase__      :			List[str] 						= randn_tensor(output.shape    ,							device=UpperCamelCase__  )
														lowerCamelCase__      :			Optional[Any] 						= torch.nn.functional.mse_loss(UpperCamelCase__    ,							UpperCamelCase__  )
														loss.backward()









'''simple docstring'''







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


_A			:    Any					  =logging.get_logger(__name__)

_A			:    Dict					  ={
    '''microsoft/trocr-base-handwritten''': (
        '''https://huggingface.co/microsoft/trocr-base-handwritten/resolve/main/config.json'''
    ),
    # See all TrOCR models at https://huggingface.co/models?filter=trocr
}

class _lowercase			(       _lowercase	):
							a          = """trocr"""
							a          = ["""past_key_values"""]
							a          = {
							    """num_attention_heads""": """decoder_attention_heads""",
							    """hidden_size""": """d_model""",
							    """num_hidden_layers""": """decoder_layers""",
							}




							def __init__(							self:       Optional[Any]    ,							UpperCamelCase__:       int=50_265    ,							UpperCamelCase__:       int=1_024    ,							UpperCamelCase__:       Optional[Any]=12    ,							UpperCamelCase__:       Dict=16    ,							UpperCamelCase__:       int=4_096    ,							UpperCamelCase__:       Tuple="gelu"    ,							UpperCamelCase__:       int=512    ,							UpperCamelCase__:       Dict=0.1    ,							UpperCamelCase__:       Tuple=0.0    ,							UpperCamelCase__:       str=0.0    ,							UpperCamelCase__:       Any=2    ,							UpperCamelCase__:       Dict=0.02    ,							UpperCamelCase__:       Optional[Any]=0.0    ,							UpperCamelCase__:       str=True    ,							UpperCamelCase__:       Tuple=False    ,							UpperCamelCase__:       Union[str, Any]=True    ,							UpperCamelCase__:       Tuple=True    ,							UpperCamelCase__:       Dict=1    ,							UpperCamelCase__:       List[str]=0    ,							UpperCamelCase__:       Union[str, Any]=2    ,							**UpperCamelCase__:       str    ,							):
														lowerCamelCase__      :			Any 						= vocab_size
														lowerCamelCase__      :			Tuple 						= d_model
														lowerCamelCase__      :			Any 						= decoder_layers
														lowerCamelCase__      :			Dict 						= decoder_attention_heads
														lowerCamelCase__      :			str 						= decoder_ffn_dim
														lowerCamelCase__      :			Tuple 						= activation_function
														lowerCamelCase__      :			Dict 						= max_position_embeddings
														lowerCamelCase__      :			int 						= dropout
														lowerCamelCase__      :			int 						= attention_dropout
														lowerCamelCase__      :			List[Any] 						= activation_dropout
														lowerCamelCase__      :			Union[str, Any] 						= init_std
														lowerCamelCase__      :			Optional[int] 						= decoder_layerdrop
														lowerCamelCase__      :			Dict 						= use_cache
														lowerCamelCase__      :			Any 						= scale_embedding
														lowerCamelCase__      :			Optional[int] 						= use_learned_position_embeddings
														lowerCamelCase__      :			List[str] 						= layernorm_embedding

														super().__init__(
														    pad_token_id=UpperCamelCase__    ,							bos_token_id=UpperCamelCase__    ,							eos_token_id=UpperCamelCase__    ,							decoder_start_token_id=UpperCamelCase__    ,							**UpperCamelCase__    ,							)

