



import math

import flax.linen as nn
import jax.numpy as jnp







def    _snake_case (       __snake_case					,  __snake_case					,  __snake_case = 1					,  __snake_case = 1					,  __snake_case = 1.0E4					,  __snake_case = False					,  __snake_case = 1.0					,  )     ->     jnp.ndarray:






      '''simple docstring'''







      assert timesteps.ndim == 1, "Timesteps should be a 1d-array"
      assert embedding_dim % 2 == 0, F"""Embedding dimension {embedding_dim} should be even"""
      UpperCAmelCase_  :	Tuple				      =		float(embedding_dim // 2    )
      UpperCAmelCase_  :	List[Any]				      =		math.log(max_timescale / min_timescale    ) / (num_timescales - freq_shift)
      UpperCAmelCase_  :	List[Any]				      =		min_timescale * jnp.exp(jnp.arange(__snake_case					,  dtype=jnp.floataa    ) * -log_timescale_increment    )
      UpperCAmelCase_  :	Dict				      =		jnp.expand_dims(__snake_case					,  1    ) * jnp.expand_dims(__snake_case					,  0    )

      # scale embeddings
      UpperCAmelCase_  :	List[str]				      =		scale * emb

      if flip_sin_to_cos:
            UpperCAmelCase_  :	Any				      =		jnp.concatenate([jnp.cos(__snake_case    ), jnp.sin(__snake_case    )]					,  axis=1    )
      else:
            UpperCAmelCase_  :	Tuple				      =		jnp.concatenate([jnp.sin(__snake_case    ), jnp.cos(__snake_case    )]					,  axis=1    )
      UpperCAmelCase_  :	Any				      =		jnp.reshape(__snake_case					,  [jnp.shape(__snake_case    )[0], embedding_dim]    )
      return signal


class   snake_case_ (nn.Module  ):




       """simple docstring"""






       _lowerCamelCase			    =				32
       _lowerCamelCase			    =				jnp.floataa



       @nn.compact
       def __call__(     self     ,lowercase):

             """simple docstring"""





             UpperCAmelCase_  :	Dict				      =		nn.Dense(self.time_embed_dim     ,dtype=self.dtype     ,name="linear_1")(lowercase)
             UpperCAmelCase_  :	int				      =		nn.silu(lowercase)
             UpperCAmelCase_  :	int				      =		nn.Dense(self.time_embed_dim     ,dtype=self.dtype     ,name="linear_2")(lowercase)
             return temb


class   snake_case_ (nn.Module  ):




       """simple docstring"""






       _lowerCamelCase			    =				32
       _lowerCamelCase			    =				False
       _lowerCamelCase			    =				1



       @nn.compact
       def __call__(     self     ,lowercase):

             """simple docstring"""





             return get_sinusoidal_embeddings(
                 lowercase     ,embedding_dim=self.dim     ,flip_sin_to_cos=self.flip_sin_to_cos     ,freq_shift=self.freq_shift)





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


__lowerCamelCase         =			logging.get_logger(__name__)

__lowerCamelCase         =			{
    '''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   snake_case_ (lowercase__  ):




       """simple docstring"""






       _lowerCamelCase			    =				"""trocr"""
       _lowerCamelCase			    =				["""past_key_values"""]
       _lowerCamelCase			    =				{
           """num_attention_heads""": """decoder_attention_heads""",
           """hidden_size""": """d_model""",
           """num_hidden_layers""": """decoder_layers""",
       }



       def __init__(     self     ,lowercase=50265     ,lowercase=1024     ,lowercase=12     ,lowercase=16     ,lowercase=4096     ,lowercase="gelu"     ,lowercase=512     ,lowercase=0.1     ,lowercase=0.0     ,lowercase=0.0     ,lowercase=2     ,lowercase=0.02     ,lowercase=0.0     ,lowercase=True     ,lowercase=False     ,lowercase=True     ,lowercase=True     ,lowercase=1     ,lowercase=0     ,lowercase=2     ,**lowercase     ,):

             """simple docstring"""





             UpperCAmelCase_  :	int				      =		vocab_size
             UpperCAmelCase_  :	Tuple				      =		d_model
             UpperCAmelCase_  :	List[str]				      =		decoder_layers
             UpperCAmelCase_  :	Union[str, Any]				      =		decoder_attention_heads
             UpperCAmelCase_  :	Optional[Any]				      =		decoder_ffn_dim
             UpperCAmelCase_  :	Tuple				      =		activation_function
             UpperCAmelCase_  :	List[Any]				      =		max_position_embeddings
             UpperCAmelCase_  :	List[Any]				      =		dropout
             UpperCAmelCase_  :	List[str]				      =		attention_dropout
             UpperCAmelCase_  :	Union[str, Any]				      =		activation_dropout
             UpperCAmelCase_  :	Optional[int]				      =		init_std
             UpperCAmelCase_  :	Dict				      =		decoder_layerdrop
             UpperCAmelCase_  :	List[str]				      =		use_cache
             UpperCAmelCase_  :	Dict				      =		scale_embedding
             UpperCAmelCase_  :	Dict				      =		use_learned_position_embeddings
             UpperCAmelCase_  :	Optional[Any]				      =		layernorm_embedding

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