

from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt






def        UpperCamelCase_(							_snake_case   :  Sequence[float]       ,     _snake_case   :  int       ,     _snake_case   :  int	):




			"""simple docstring"""






			if not arr:
						return None, None, 0
			if low == high:
						return low, high, arr[low]

			__a        =(low + high) // 2
			__a      ,    __a      ,    __a        =max_subarray(_snake_case       ,     _snake_case       ,     _snake_case	)
			__a      ,    __a      ,    __a        =max_subarray(_snake_case       ,     mid + 1       ,     _snake_case	)
			__a      ,    __a      ,    __a        =max_cross_sum(_snake_case       ,     _snake_case       ,     _snake_case       ,     _snake_case	)
			if left_sum >= right_sum and left_sum >= cross_sum:
						return left_low, left_high, left_sum
			elif right_sum >= left_sum and right_sum >= cross_sum:
						return right_low, right_high, right_sum
			return cross_left, cross_right, cross_sum






def        UpperCamelCase_(							_snake_case   :  Sequence[float]       ,     _snake_case   :  int       ,     _snake_case   :  int       ,     _snake_case   :  int	):




			"""simple docstring"""






			__a      ,    __a        =float('-inf'	), -1
			__a      ,    __a        =float('-inf'	), -1

			__a        =0
			for i in range(_snake_case       ,     low - 1       ,     -1	):
						summ += arr[i]
						if summ > left_sum:
									__a        =summ
									__a        =i

			__a        =0
			for i in range(mid + 1       ,     high + 1	):
						summ += arr[i]
						if summ > right_sum:
									__a        =summ
									__a        =i

			return max_left, max_right, (left_sum + right_sum)






def        UpperCamelCase_(							_snake_case   :  int	):




			"""simple docstring"""






			__a        =[randint(1       ,     _snake_case	) for _ in range(_snake_case	)]
			__a        =time.time()
			max_subarray(_snake_case       ,     0       ,     input_size - 1	)
			__a        =time.time()
			return end - start






def        UpperCamelCase_(							):




			"""simple docstring"""






			__a        =[10, 100, 1000, 10000, 50000, 100000, 200000, 300000, 400000, 500000]
			__a        =[time_max_subarray(_snake_case	) for input_size in input_sizes]
			print('No of Inputs\t\tTime Taken'	)
			for input_size, runtime in zip(_snake_case       ,     _snake_case	):
						print(_snake_case       ,     '\t\t'       ,     _snake_case	)
			plt.plot(_snake_case       ,     _snake_case	)
			plt.xlabel('Number of Inputs'	)
			plt.ylabel('Time taken in seconds'	)
			plt.show()


if __name__ == "__main__":
						from doctest import testmod

						testmod()









import argparse
from argparse import Namespace

import torch
from torch import nn

from transformers import XGLMConfig, XGLMForCausalLM






def        UpperCamelCase_(							_snake_case   :  Union[str, Any]	):




			"""simple docstring"""






			__a        =[
			    'decoder.version',
			    'decoder.output_projection.weight',
			    '_float_tensor',
			    'decoder.embed_positions._float_tensor',
			]
			for k in ignore_keys:
						state_dict.pop(_snake_case       ,     _snake_case	)






def        UpperCamelCase_(							_snake_case   :  Any	):




			"""simple docstring"""






			__a      ,    __a        =emb.weight.shape
			__a        =nn.Linear(_snake_case       ,     _snake_case       ,     bias=_snake_case	)
			__a        =emb.weight.data
			return lin_layer






def        UpperCamelCase_(							_snake_case   :  str	):




			"""simple docstring"""






			__a        =torch.load(_snake_case       ,     map_location='cpu'	)
			__a        =Namespace(**checkpoint['cfg']['model']	)
			__a        =checkpoint['model']
			remove_ignore_keys_(_snake_case	)
			__a        =state_dict['decoder.embed_tokens.weight'].shape[0]

			__a        ={key.replace('decoder'       ,     'model'	): val for key, val in state_dict.items()}

			__a        =XGLMConfig(
			    vocab_size=_snake_case       ,     max_position_embeddings=args.max_target_positions       ,     num_layers=args.decoder_layers       ,     attention_heads=args.decoder_attention_heads       ,     ffn_dim=args.decoder_ffn_embed_dim       ,     d_model=args.decoder_embed_dim       ,     layerdrop=args.decoder_layerdrop       ,     dropout=args.dropout       ,     attention_dropout=args.attention_dropout       ,     activation_dropout=args.activation_dropout       ,     activation_function='gelu'       ,     scale_embedding=not args.no_scale_embedding       ,     tie_word_embeddings=args.share_decoder_input_output_embed       ,     )

			__a        =XGLMForCausalLM(_snake_case	)
			__a        =model.load_state_dict(_snake_case       ,     strict=_snake_case	)
			print(_snake_case	)
			__a        =make_linear_from_emb(model.model.embed_tokens	)

			return model


if __name__ == "__main__":
						_lowerCAmelCase					:			str  				=			argparse.ArgumentParser()
						# Required parameters
						parser.add_argument("fairseq_path", type=str, help="path to a model.pt on local filesystem.")
						parser.add_argument("pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
						_lowerCAmelCase					:			str  				=			parser.parse_args()
						_lowerCAmelCase					:			Union[str, Any]  				=			convert_fairseq_xglm_checkpoint_from_disk(args.fairseq_path)
						model.save_pretrained(args.pytorch_dump_folder_path)






