"""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 				snake_case__     :


 def __init__(				self  ,						lowerCamelCase  ,						lowerCamelCase=99  ,						lowerCamelCase=13  ,						lowerCamelCase=16  ,						lowerCamelCase=7  ,						lowerCamelCase=True  ,						lowerCamelCase=True  ,						lowerCamelCase=True  ,						lowerCamelCase=False  ,						lowerCamelCase=True  ,						lowerCamelCase=2  ,						lowerCamelCase=32  ,						lowerCamelCase=4  ,						lowerCamelCase=4  ,						lowerCamelCase=30  ,						lowerCamelCase=0  ,						lowerCamelCase=1  ,						lowerCamelCase=2  ,						lowerCamelCase=None  ,						):
        __a     				=  parent
        __a     				=  batch_size
        __a     				=  decoder_seq_length
        # For common tests
        __a     				=  self.decoder_seq_length
        __a     				=  is_training
        __a     				=  use_attention_mask
        __a     				=  use_labels

        __a     				=  vocab_size
        __a     				=  d_model
        __a     				=  d_model
        __a     				=  decoder_layers
        __a     				=  decoder_layers
        __a     				=  decoder_ffn_dim
        __a     				=  decoder_attention_heads
        __a     				=  decoder_attention_heads
        __a     				=  eos_token_id
        __a     				=  bos_token_id
        __a     				=  pad_token_id
        __a     				=  decoder_start_token_id
        __a     				=  use_cache
        __a     				=  max_position_embeddings

        __a     				=  None
        __a     				=  decoder_seq_length
        __a     				=  2
        __a     				=  1


 def 		a__ (				self		):
        __a     				=  ids_tensor([self.batch_size, self.decoder_seq_length]  ,						self.vocab_size		)

        __a     				=  None
        if self.use_attention_mask:
               __a     				=  ids_tensor([self.batch_size, self.decoder_seq_length]  ,						vocab_size=2		)

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

        __a     				=  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 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						lowerCamelCase  ,						):
        __a     				=  True
        __a     				=  TrOCRDecoder(config=lowerCamelCase		).to(lowerCamelCase		).eval()
        __a     				=  input_ids[:2]

        input_ids[input_ids == 0] += 1
        # first forward pass
        __a     				=  model(lowerCamelCase  ,						use_cache=lowerCamelCase		)
        __a     				=  model(lowerCamelCase		)
        __a     				=  model(lowerCamelCase  ,						use_cache=lowerCamelCase		)

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

        __a     				=  outputs["past_key_values"]

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

        # append to next input_ids and
        __a     				=  torch.cat([input_ids, next_tokens]  ,						dim=-1		)

        __a     				=  model(lowerCamelCase		)["last_hidden_state"]
        __a     				=  model(lowerCamelCase  ,						past_key_values=lowerCamelCase		)["last_hidden_state"]

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

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






 def 		a__ (				self		):
        __a     				=  self.prepare_config_and_inputs()
        __a				, __a				, __a				, __a     				=  config_and_inputs

        __a     				=  {"input_ids": input_ids, "attention_mask": attention_mask}
        return config, inputs_dict






@require_torch
class 				snake_case__     (   snake_case_,		snake_case_,		snake_case_,		unittest.TestCase   ):
 _snake_case   :			List[str]           =		(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
 _snake_case   :			Optional[int]           =		(TrOCRForCausalLM,) if is_torch_available() else ()
 _snake_case   :			str           =		{"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
 _snake_case   :			Optional[Any]           =		True
 _snake_case   :			Any           =		False


 def 		a__ (				self		):
        __a     				=  TrOCRStandaloneDecoderModelTester(self  ,						is_training=lowerCamelCase		)
        __a     				=  ConfigTester(self  ,						config_class=lowerCamelCase		)


 def 		a__ (				self		):
        pass


 def 		a__ (				self		):
        pass


 def 		a__ (				self		):
        pass


 def 		a__ (				self		):
        self.config_tester.run_common_tests()


 def 		a__ (				self		):
        __a     				=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_decoder_model_past(*lowerCamelCase		)


 def 		a__ (				self		):
        return






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

"""simple docstring"""



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


SCREAMING_SNAKE_CASE__:List[str]			=	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__:int			=	{
    """microsoft/focalnet-tiny""": """https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json""",
}






class 				snake_case__     (   snake_case_,		snake_case_   ):
 _snake_case   :			List[Any]           =		"""focalnet"""


 def __init__(				self  ,						lowerCamelCase=224  ,						lowerCamelCase=4  ,						lowerCamelCase=3  ,						lowerCamelCase=96  ,						lowerCamelCase=False  ,						lowerCamelCase=[192, 384, 768, 768]  ,						lowerCamelCase=[2, 2, 6, 2]  ,						lowerCamelCase=[2, 2, 2, 2]  ,						lowerCamelCase=[3, 3, 3, 3]  ,						lowerCamelCase="gelu"  ,						lowerCamelCase=4.0  ,						lowerCamelCase=0.0  ,						lowerCamelCase=0.1  ,						lowerCamelCase=False  ,						lowerCamelCase=1E-4  ,						lowerCamelCase=False  ,						lowerCamelCase=False  ,						lowerCamelCase=False  ,						lowerCamelCase=0.02  ,						lowerCamelCase=1E-5  ,						lowerCamelCase=32  ,						lowerCamelCase=None  ,						lowerCamelCase=None  ,						**lowerCamelCase  ,						):
        super().__init__(**lowerCamelCase		)

        __a     				=  image_size
        __a     				=  patch_size
        __a     				=  num_channels
        __a     				=  embed_dim
        __a     				=  use_conv_embed
        __a     				=  hidden_sizes
        __a     				=  depths
        __a     				=  focal_levels
        __a     				=  focal_windows
        __a     				=  hidden_act
        __a     				=  mlp_ratio
        __a     				=  hidden_dropout_prob
        __a     				=  drop_path_rate
        __a     				=  use_layerscale
        __a     				=  layerscale_value
        __a     				=  use_post_layernorm
        __a     				=  use_post_layernorm_in_modulation
        __a     				=  normalize_modulator
        __a     				=  initializer_range
        __a     				=  layer_norm_eps
        __a     				=  encoder_stride
        __a     				=  ["stem"] + [F"stage{idx}" for idx in range(1  ,						len(self.depths		) + 1		)]
        __a				, __a     				=  get_aligned_output_features_output_indices(
            out_features=lowerCamelCase  ,						out_indices=lowerCamelCase  ,						stage_names=self.stage_names		)
