



'''simple docstring'''



import os
def 					UpperCAmelCase   (						A  :				List[Any]				):
							SCREAMING_SNAKE_CASE					:      Dict     					=			len(grid[0]				)
							SCREAMING_SNAKE_CASE					:      Optional[int]     					=			len(A				)

							SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			0
							SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			0
							SCREAMING_SNAKE_CASE					:      Tuple     					=			0

							# Check vertically, horizontally, diagonally at the same time (only works
							# for nxn grid)
							for i in range(A				):
														for j in range(n_rows - 3				):
																					SCREAMING_SNAKE_CASE					:      List[Any]     					=			grid[j][i] * grid[j + 1][i] * grid[j + 2][i] * grid[j + 3][i]
																					SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			grid[i][j] * grid[i][j + 1] * grid[i][j + 2] * grid[i][j + 3]

																					# Left-to-right diagonal (\) product
																					if i < n_columns - 3:
																												SCREAMING_SNAKE_CASE					:      List[str]     					=			(
																												    grid[i][j]
																												    * grid[i + 1][j + 1]
																												    * grid[i + 2][j + 2]
																												    * grid[i + 3][j + 3]
																												)

																					# Right-to-left diagonal(/) product
																					if i > 2:
																												SCREAMING_SNAKE_CASE					:      int     					=			(
																												    grid[i][j]
																												    * grid[i - 1][j + 1]
																												    * grid[i - 2][j + 2]
																												    * grid[i - 3][j + 3]
																												)

																					SCREAMING_SNAKE_CASE					:      Tuple     					=			max(
																					    A					,		A					,		A					,		A				)
																					if max_product > largest:
																												SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			max_product

							return largest
def 					UpperCAmelCase   (						):
							SCREAMING_SNAKE_CASE					:      int     					=			[]
							with open(os.path.dirname(A				) + '''/grid.txt'''				) as file:
														for line in file:
																					grid.append(line.strip('''\n'''				).split(''' '''				)				)

							SCREAMING_SNAKE_CASE					:      Dict     					=			[[int(A				) for i in grid[j]] for j in range(len(A				)				)]

							return largest_product(A				)


if __name__ == "__main__":
							print(solution())









'''simple docstring'''



import argparse

import torch

from transformers import GPTaLMHeadModel, RobertaForMaskedLM


if __name__ == "__main__":
							lowerCAmelCase_     :       Any								=						argparse.ArgumentParser(
							    description=(
							        'Extraction some layers of the full RobertaForMaskedLM or GPT2LMHeadModel for Transfer Learned'
							        ' Distillation'
							    )
							)
							parser.add_argument('--model_type', default='roberta', choices=['roberta', 'gpt2'])
							parser.add_argument('--model_name', default='roberta-large', type=str)
							parser.add_argument('--dump_checkpoint', default='serialization_dir/tf_roberta_048131723.pth', type=str)
							parser.add_argument('--vocab_transform', action='store_true')
							lowerCAmelCase_     :       Dict								=						parser.parse_args()

							if args.model_type == "roberta":
														lowerCAmelCase_     :       str								=						RobertaForMaskedLM.from_pretrained(args.model_name)
														lowerCAmelCase_     :       Tuple								=						'roberta'
							elif args.model_type == "gpt2":
														lowerCAmelCase_     :       List[Any]								=						GPTaLMHeadModel.from_pretrained(args.model_name)
														lowerCAmelCase_     :       Optional[Any]								=						'transformer'

							lowerCAmelCase_     :       List[Any]								=						model.state_dict()
							lowerCAmelCase_     :       List[Any]								=						{}

							# Embeddings #
							if args.model_type == "gpt2":
														for param_name in ["wte.weight", "wpe.weight"]:
																					lowerCAmelCase_     :       List[str]								=						state_dict[f'{prefix}.{param_name}']
							else:
														for w in ["word_embeddings", "position_embeddings", "token_type_embeddings"]:
																					lowerCAmelCase_     :       Any								=						f'{prefix}.embeddings.{w}.weight'
																					lowerCAmelCase_     :       Any								=						state_dict[param_name]
														for w in ["weight", "bias"]:
																					lowerCAmelCase_     :       str								=						f'{prefix}.embeddings.LayerNorm.{w}'
																					lowerCAmelCase_     :       str								=						state_dict[param_name]

    # Transformer Blocks #
							lowerCAmelCase_     :       List[str]								=						0
							for teacher_idx in [0, 2, 4, 7, 9, 11]:
														if args.model_type == "gpt2":
																					for layer in ["ln_1", "attn.c_attn", "attn.c_proj", "ln_2", "mlp.c_fc", "mlp.c_proj"]:
																												for w in ["weight", "bias"]:
																																			lowerCAmelCase_     :       List[str]								=						state_dict[
																																			    f'{prefix}.h.{teacher_idx}.{layer}.{w}'
																																			]
																					lowerCAmelCase_     :       Any								=						state_dict[f'{prefix}.h.{teacher_idx}.attn.bias']
														else:
																					for layer in [
																					    "attention.self.query",
																					    "attention.self.key",
																					    "attention.self.value",
																					    "attention.output.dense",
																					    "attention.output.LayerNorm",
																					    "intermediate.dense",
																					    "output.dense",
																					    "output.LayerNorm",
																					]:
																												for w in ["weight", "bias"]:
																																			lowerCAmelCase_     :       Any								=						state_dict[
																																			    f'{prefix}.encoder.layer.{teacher_idx}.{layer}.{w}'
																																			]
														std_idx += 1

							# Language Modeling Head ###s
							if args.model_type == "roberta":
														for layer in ["lm_head.decoder.weight", "lm_head.bias"]:
																					lowerCAmelCase_     :       List[Any]								=						state_dict[f'{layer}']
														if args.vocab_transform:
																					for w in ["weight", "bias"]:
																												lowerCAmelCase_     :       str								=						state_dict[f'lm_head.dense.{w}']
																												lowerCAmelCase_     :       Optional[int]								=						state_dict[f'lm_head.layer_norm.{w}']
							elif args.model_type == "gpt2":
														for w in ["weight", "bias"]:
																					lowerCAmelCase_     :       Dict								=						state_dict[f'{prefix}.ln_f.{w}']
														lowerCAmelCase_     :       Any								=						state_dict['lm_head.weight']

							print(f'N layers selected for distillation: {std_idx}')
							print(f'Number of params transferred for distillation: {len(compressed_sd.keys())}')

							print(f'Save transferred checkpoint to {args.dump_checkpoint}.')
							torch.save(compressed_sd, args.dump_checkpoint)




