



"""simple docstring"""


import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						BertModel					,     _SCREAMING_SNAKE_CASE   :						str					,     _SCREAMING_SNAKE_CASE   :						str				):






				'''simple docstring'''



				_UpperCAmelCase					=    ('''dense.weight''', '''attention.self.query''', '''attention.self.key''', '''attention.self.value''')

				_UpperCAmelCase					=    (
				    ('''layer.''', '''layer_'''),
				    ('''word_embeddings.weight''', '''word_embeddings'''),
				    ('''position_embeddings.weight''', '''position_embeddings'''),
				    ('''token_type_embeddings.weight''', '''token_type_embeddings'''),
				    ('''.''', '''/'''),
				    ('''LayerNorm/weight''', '''LayerNorm/gamma'''),
				    ('''LayerNorm/bias''', '''LayerNorm/beta'''),
				    ('''weight''', '''kernel'''),
				)

				if not os.path.isdir(_SCREAMING_SNAKE_CASE				):
								os.makedirs(_SCREAMING_SNAKE_CASE				)

				_UpperCAmelCase					=    model.state_dict()

				def to_tf_var_name(_SCREAMING_SNAKE_CASE   :						str				):
								for patt, repl in iter(_SCREAMING_SNAKE_CASE				):
												_UpperCAmelCase					=    name.replace(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
								return f'bert/{name}'

				def create_tf_var(_SCREAMING_SNAKE_CASE   :						np.ndarray					,     _SCREAMING_SNAKE_CASE   :						str					,     _SCREAMING_SNAKE_CASE   :						tf.Session				):
								_UpperCAmelCase					=    tf.dtypes.as_dtype(tensor.dtype				)
								_UpperCAmelCase					=    tf.get_variable(dtype=_SCREAMING_SNAKE_CASE					,     shape=tensor.shape					,     name=_SCREAMING_SNAKE_CASE					,     initializer=tf.zeros_initializer()				)
								session.run(tf.variables_initializer([tf_var]				)				)
								session.run(_SCREAMING_SNAKE_CASE				)
								return tf_var

				tf.reset_default_graph()
				with tf.Session() as session:
								for var_name in state_dict:
												_UpperCAmelCase					=    to_tf_var_name(_SCREAMING_SNAKE_CASE				)
												_UpperCAmelCase					=    state_dict[var_name].numpy()
												if any(x in var_name for x in tensors_to_transpose				):
																_UpperCAmelCase					=    torch_tensor.T
												_UpperCAmelCase					=    create_tf_var(tensor=_SCREAMING_SNAKE_CASE					,     name=_SCREAMING_SNAKE_CASE					,     session=_SCREAMING_SNAKE_CASE				)
												tf.keras.backend.set_value(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)
												_UpperCAmelCase					=    session.run(_SCREAMING_SNAKE_CASE				)
												print(f'Successfully created {tf_name}: {np.allclose(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				)}'				)

								_UpperCAmelCase					=    tf.train.Saver(tf.trainable_variables()				)
								saver.save(_SCREAMING_SNAKE_CASE					,     os.path.join(_SCREAMING_SNAKE_CASE					,     model_name.replace('''-'''					,     '''_'''				) + '''.ckpt'''				)				)







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						List[Any]=None				):






				'''simple docstring'''



				_UpperCAmelCase					=    argparse.ArgumentParser()
				parser.add_argument('''--model_name'''					,     type=_SCREAMING_SNAKE_CASE					,     required=_SCREAMING_SNAKE_CASE					,     help='''model name e.g. bert-base-uncased'''				)
				parser.add_argument(
				    '''--cache_dir'''					,     type=_SCREAMING_SNAKE_CASE					,     default=_SCREAMING_SNAKE_CASE					,     required=_SCREAMING_SNAKE_CASE					,     help='''Directory containing pytorch model'''				)
				parser.add_argument('''--pytorch_model_path'''					,     type=_SCREAMING_SNAKE_CASE					,     required=_SCREAMING_SNAKE_CASE					,     help='''/path/to/<pytorch-model-name>.bin'''				)
				parser.add_argument('''--tf_cache_dir'''					,     type=_SCREAMING_SNAKE_CASE					,     required=_SCREAMING_SNAKE_CASE					,     help='''Directory in which to save tensorflow model'''				)
				_UpperCAmelCase					=    parser.parse_args(_SCREAMING_SNAKE_CASE				)

				_UpperCAmelCase					=    BertModel.from_pretrained(
				    pretrained_model_name_or_path=args.model_name					,     state_dict=torch.load(args.pytorch_model_path				)					,     cache_dir=args.cache_dir					,     )

				convert_pytorch_checkpoint_to_tf(model=_SCREAMING_SNAKE_CASE					,     ckpt_dir=args.tf_cache_dir					,     model_name=args.model_name				)


if __name__ == "__main__":
					main()








"""simple docstring"""


from typing import List, Optional, Union

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


__A   :							Any										=   logging.get_logger(__name__)

__A   :							Dict										=   {
    "huggingface/informer-tourism-monthly": (
        "https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json"
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}
class 			_a (					lowerCAmelCase):
			"""simple docstring"""
			UpperCamelCase__			      =	"""informer"""
			UpperCamelCase__			      =	{
			    """hidden_size""": """d_model""",
			    """num_attention_heads""": """encoder_attention_heads""",
			    """num_hidden_layers""": """encoder_layers""",
			}



			def __init__(		self     :    List[str]				,   __UpperCamelCase     :    Optional[int] = None				,   __UpperCamelCase     :    Optional[int] = None				,   __UpperCamelCase     :    str = "student_t"				,   __UpperCamelCase     :    str = "nll"				,   __UpperCamelCase     :    int = 1				,   __UpperCamelCase     :    List[int] = None				,   __UpperCamelCase     :    Optional[Union[str, bool]] = "mean"				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    int = 0				,   __UpperCamelCase     :    Optional[List[int]] = None				,   __UpperCamelCase     :    Optional[List[int]] = None				,   __UpperCamelCase     :    int = 6_4				,   __UpperCamelCase     :    int = 3_2				,   __UpperCamelCase     :    int = 3_2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    int = 2				,   __UpperCamelCase     :    bool = True				,   __UpperCamelCase     :    str = "gelu"				,   __UpperCamelCase     :    float = 0.0_5				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    float = 0.1				,   __UpperCamelCase     :    int = 1_0_0				,   __UpperCamelCase     :    float = 0.0_2				,   __UpperCamelCase     :    Optional[Any]=True				,   __UpperCamelCase     :    str = "prob"				,   __UpperCamelCase     :    int = 5				,   __UpperCamelCase     :    bool = True				,   **__UpperCamelCase     :    Union[str, Any]				,   )->Dict:
							# time series specific configuration
							_UpperCAmelCase					=    prediction_length
							_UpperCAmelCase					=    context_length or prediction_length
							_UpperCAmelCase					=    distribution_output
							_UpperCAmelCase					=    loss
							_UpperCAmelCase					=    input_size
							_UpperCAmelCase					=    num_time_features
							_UpperCAmelCase					=    lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
							_UpperCAmelCase					=    scaling
							_UpperCAmelCase					=    num_dynamic_real_features
							_UpperCAmelCase					=    num_static_real_features
							_UpperCAmelCase					=    num_static_categorical_features

							# set cardinality
							if cardinality and num_static_categorical_features > 0:
											if len(__UpperCamelCase   ) != num_static_categorical_features:
															raise ValueError(
															    '''The cardinality should be a list of the same length as `num_static_categorical_features`'''   )
											_UpperCAmelCase					=    cardinality
							else:
											_UpperCAmelCase					=    [0]

							# set embedding_dimension
							if embedding_dimension and num_static_categorical_features > 0:
											if len(__UpperCamelCase   ) != num_static_categorical_features:
															raise ValueError(
															    '''The embedding dimension should be a list of the same length as `num_static_categorical_features`'''   )
											_UpperCAmelCase					=    embedding_dimension
							else:
											_UpperCAmelCase					=    [min(5_0				,   (cat + 1) // 2   ) for cat in self.cardinality]

							_UpperCAmelCase					=    num_parallel_samples

							# Transformer architecture configuration
							_UpperCAmelCase					=    input_size * len(self.lags_sequence   ) + self._number_of_features
							_UpperCAmelCase					=    d_model
							_UpperCAmelCase					=    encoder_attention_heads
							_UpperCAmelCase					=    decoder_attention_heads
							_UpperCAmelCase					=    encoder_ffn_dim
							_UpperCAmelCase					=    decoder_ffn_dim
							_UpperCAmelCase					=    encoder_layers
							_UpperCAmelCase					=    decoder_layers

							_UpperCAmelCase					=    dropout
							_UpperCAmelCase					=    attention_dropout
							_UpperCAmelCase					=    activation_dropout
							_UpperCAmelCase					=    encoder_layerdrop
							_UpperCAmelCase					=    decoder_layerdrop

							_UpperCAmelCase					=    activation_function
							_UpperCAmelCase					=    init_std

							_UpperCAmelCase					=    use_cache

							# Informer
							_UpperCAmelCase					=    attention_type
							_UpperCAmelCase					=    sampling_factor
							_UpperCAmelCase					=    distil

							super().__init__(is_encoder_decoder=__UpperCamelCase				,   **__UpperCamelCase   )



			@property
			def  lowercase__						(		self     :    Optional[int]   )->int:
							return (
							    sum(self.embedding_dimension   )
							    + self.num_dynamic_real_features
							    + self.num_time_features
							    + self.num_static_real_features
							    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
							)



