

"""simple docstring"""





import argparse
import pickle

import numpy as np
import torch
from torch import nn

from transformers import ReformerConfig, ReformerModelWithLMHead
from transformers.utils import logging


logging.set_verbosity_info()

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_=None ):
	# set parameter of one layer
	assert torch_layer.weight.shape == weight.shape, f'{torch_layer} layer.weight does not match'
	lowerCAmelCase__	:						Tuple  				=	nn.Parameter(A_ )
	if bias is not None:
		assert torch_layer.bias.shape == bias.shape, f'{torch_layer} layer.bias does not match'
		lowerCAmelCase__	:						List[str]  				=	nn.Parameter(A_ )

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_ ):
	# set torch weights for 1-to-1 comparison
	lowerCAmelCase__	:						Tuple  				=	np.asarray(weights[0] )
	lowerCAmelCase__	:						int  				=	np.asarray(weights[1] )
	lowerCAmelCase__	:						Dict  				=	np.asarray(weights[2] )

	set_param(
	    torch_layer.self_attention.query_key						,     torch.tensor(A_ ).transpose(1						,     2 ).contiguous().view(-1						,     A_ )						,     )
	set_param(
	    torch_layer.self_attention.value						,     torch.tensor(A_ ).transpose(1						,     2 ).contiguous().view(-1						,     A_ )						,     )
	set_param(
	    torch_layer.output.dense						,     torch.tensor(A_ ).view(-1						,     A_ ).contiguous().transpose(0						,     1 )						,     )

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_ ):
	# set torch weights for 1-to-1 comparison
	lowerCAmelCase__	:						Union[str, Any]  				=	np.asarray(weights[0] )
	lowerCAmelCase__	:						Optional[Any]  				=	np.asarray(weights[1] )
	lowerCAmelCase__	:						Optional[int]  				=	np.asarray(weights[2] )
	lowerCAmelCase__	:						int  				=	np.asarray(weights[3] )

	set_param(
	    torch_layer.self_attention.query						,     torch.tensor(A_ ).transpose(1						,     2 ).contiguous().view(-1						,     A_ )						,     )
	set_param(
	    torch_layer.self_attention.key						,     torch.tensor(A_ ).transpose(1						,     2 ).contiguous().view(-1						,     A_ )						,     )
	set_param(
	    torch_layer.self_attention.value						,     torch.tensor(A_ ).transpose(1						,     2 ).contiguous().view(-1						,     A_ )						,     )
	set_param(
	    torch_layer.output.dense						,     torch.tensor(A_ ).view(-1						,     A_ ).contiguous().transpose(0						,     1 )						,     )

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_ ):
	# layernorm 1
	lowerCAmelCase__	:						int  				=	weights[0][0][0]
	lowerCAmelCase__	:						Tuple  				=	np.asarray(layer_norm_a[0] )
	lowerCAmelCase__	:						str  				=	np.asarray(layer_norm_a[1] )
	set_param(
	    torch_block.attention.layer_norm						,     torch.tensor(A_ )						,     torch.tensor(A_ )						,     )

	# lsh weights + output
	lowerCAmelCase__	:						Any  				=	weights[0][1]
	if len(A_ ) < 4:
		set_layer_weights_in_torch_lsh(A_						,     torch_block.attention						,     A_ )
	else:
		set_layer_weights_in_torch_local(A_						,     torch_block.attention						,     A_ )

	# intermediate weighs
	lowerCAmelCase__	:						Optional[int]  				=	weights[2][0][1][2]

	# Chunked Feed Forward
	if len(A_ ) == 4:
		lowerCAmelCase__	:						List[Any]  				=	intermediate_weights[2]

	# layernorm 2
	lowerCAmelCase__	:						Tuple  				=	np.asarray(intermediate_weights[0][0] )
	lowerCAmelCase__	:						Dict  				=	np.asarray(intermediate_weights[0][1] )
	set_param(
	    torch_block.feed_forward.layer_norm						,     torch.tensor(A_ )						,     torch.tensor(A_ )						,     )

	# intermediate dense
	lowerCAmelCase__	:						Union[str, Any]  				=	np.asarray(intermediate_weights[1][0] )
	lowerCAmelCase__	:						Tuple  				=	np.asarray(intermediate_weights[1][1] )
	set_param(
	    torch_block.feed_forward.dense.dense						,     torch.tensor(A_ ).transpose(0						,     1 ).contiguous()						,     torch.tensor(A_ )						,     )

	# intermediate out
	lowerCAmelCase__	:						Union[str, Any]  				=	np.asarray(intermediate_weights[4][0] )
	lowerCAmelCase__	:						Union[str, Any]  				=	np.asarray(intermediate_weights[4][1] )
	set_param(
	    torch_block.feed_forward.output.dense						,     torch.tensor(A_ ).transpose(0						,     1 ).contiguous()						,     torch.tensor(A_ )						,     )

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_ ):
	# reformer model
	lowerCAmelCase__	:						int  				=	torch_model.reformer

	# word embeds
	lowerCAmelCase__	:						str  				=	np.asarray(weights[1] )
	set_param(
	    torch_model_reformer.embeddings.word_embeddings						,     torch.tensor(A_ )						,     )

	if isinstance(weights[3]						,     A_ ):
		lowerCAmelCase__	:						Dict  				=	torch_model_reformer.embeddings.position_embeddings
		for emb_idx in range(len(position_embeddings.weights ) ):
			lowerCAmelCase__	:						Any  				=	np.asarray(weights[3][emb_idx][0] )
			assert (
			    position_embeddings.weights[emb_idx].shape == emb_weights.shape
			), f'{position_embeddings[emb_idx]} emb does not match'
			lowerCAmelCase__	:						List[Any]  				=	nn.Parameter(torch.tensor(A_ ) )

	lowerCAmelCase__	:						List[Any]  				=	weights[5]
	assert len(torch_model_reformer.encoder.layers ) * 4 == len(
	    A_ ), "HF and trax model do not have the same number of layers"
	for layer_idx, layer in enumerate(torch_model_reformer.encoder.layers ):
		lowerCAmelCase__	:						Dict  				=	trax_layer_weights[4 * layer_idx : 4 * (layer_idx + 1)]
		set_block_weights_in_torch(A_						,     A_						,     A_ )

	# output layer norm
	lowerCAmelCase__	:						int  				=	np.asarray(weights[7][0] )
	lowerCAmelCase__	:						int  				=	np.asarray(weights[7][1] )
	set_param(
	    torch_model_reformer.encoder.layer_norm						,     torch.tensor(A_ )						,     torch.tensor(A_ )						,     )

	# output embeddings
	lowerCAmelCase__	:						Any  				=	np.asarray(weights[9][0] )
	lowerCAmelCase__	:						int  				=	np.asarray(weights[9][1] )
	set_param(
	    torch_model.lm_head.decoder						,     torch.tensor(A_ ).transpose(0						,     1 ).contiguous()						,     torch.tensor(A_ )						,     )

def   __SCREAMING_SNAKE_CASE  (      A_						,     A_						,     A_ ):
	# Initialise PyTorch model
	lowerCAmelCase__	:						Union[str, Any]  				=	ReformerConfig.from_json_file(A_ )
	print(f'Building PyTorch model from configuration: {config}' )
	lowerCAmelCase__	:						List[Any]  				=	ReformerModelWithLMHead(A_ )

	with open(A_						,     '''rb''' ) as f:
		lowerCAmelCase__	:						Tuple  				=	pickle.load(A_ )['''weights''']

	set_model_weights_in_torch(A_						,     A_						,     config.hidden_size )

	# Save pytorch-model
	print(f'Save PyTorch model to {pytorch_dump_path}' )
	torch.save(model.state_dict()						,     A_ )


if __name__ == "__main__":
							__UpperCamelCase  :      str							      =		argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '''--trax_model_pkl_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
							)
							parser.add_argument(
							    '''--config_file''',
							    default=None,
							    type=str,
							    required=True,
							    help=(
							        '''The config json file corresponding to the pre-trained Reformer model. \n'''
							        '''This specifies the model architecture.'''
							    ),
							)
							parser.add_argument(
							    '''--pytorch_dump_path''', default=None, type=str, required=True, help='''Path to the output PyTorch model.'''
							)
							__UpperCamelCase  :      Dict							      =		parser.parse_args()
							convert_trax_checkpoint_to_pytorch(args.trax_model_pkl_path, args.config_file, args.pytorch_dump_path)







"""simple docstring"""





from collections import defaultdict
from math import ceil, sqrt

def   __SCREAMING_SNAKE_CASE  (      A_ = 1_00_00_00						,     A_ = 10 ):
	lowerCAmelCase__	:						defaultdict  				=	defaultdict(A_ )

	for outer_width in range(3						,     (t_limit // 4) + 2 ):
		if outer_width * outer_width > t_limit:
			lowerCAmelCase__	:						int  				=	max(
			    ceil(sqrt(outer_width * outer_width - t_limit ) )						,     1 )
		else:
			lowerCAmelCase__	:						Tuple  				=	1

		hole_width_lower_bound += (outer_width - hole_width_lower_bound) % 2

		for hole_width in range(A_						,     outer_width - 1						,     2 ):
			count[outer_width * outer_width - hole_width * hole_width] += 1

	return sum(1 for n in count.values() if 1 <= n <= 10 )


if __name__ == "__main__":
							print(F'''{solution() = }''')




