



'''simple docstring'''



lowerCAmelCase_  :							int             =    range(2, 20 + 1)
lowerCAmelCase_  :							str             =    [10**k for k in range(ks[-1] + 1)]
lowerCAmelCase_  :							dict[int, dict[int, list[list[int]]]]             =    {}
def        UpperCAmelCase (    A	:					Any       ,		A	:					Any       ,		A	:					Optional[int]       ,		A	:					Optional[Any]					):
							SCREAMING_SNAKE_CASE       :   str         =      sum(a_i[j] for j in range(A       ,		len(A					)					)					)
							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      sum(a_i[j] * base[j] for j in range(min(len(A					)       ,		A					)					)					)

							SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Optional[int]         =      0, 0
							SCREAMING_SNAKE_CASE       :   Dict         =      n - i

							SCREAMING_SNAKE_CASE       :   str         =      memo.get(A					)

							if sub_memo is not None:
														SCREAMING_SNAKE_CASE       :   int         =      sub_memo.get(A					)

														if jumps is not None and len(A					) > 0:
																					# find and make the largest jump without going over
																					SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      -1
																					for _k in range(len(A					) - 1       ,		-1       ,		-1					):
																												if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																																			SCREAMING_SNAKE_CASE       :   Tuple         =      _k
																																			break

																					if max_jump >= 0:
																												SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   int         =      jumps[max_jump]
																												# since the difference between jumps is cached, add c
																												SCREAMING_SNAKE_CASE       :   Tuple         =      diff + c
																												for j in range(min(A       ,		len(A					)					)					):
																																			SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Dict         =      divmod(A       ,		10					)
																												if new_c > 0:
																																			add(A       ,		A       ,		A					)

														else:
																					SCREAMING_SNAKE_CASE       :   List[Any]         =      []
							else:
														SCREAMING_SNAKE_CASE       :   Dict         =      {c: []}
														SCREAMING_SNAKE_CASE       :   Dict         =      sub_memo

							if dn >= max_dn or c + diff >= base[k]:
														return diff, dn

							if k > ks[0]:
														while True:
																					# keep doing smaller jumps
																					SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   int         =      next_term(A       ,		k - 1       ,		i + dn       ,		A					)
																					diff += _diff
																					dn += terms_jumped

																					if dn >= max_dn or c + diff >= base[k]:
																												break
							else:
														# would be too small a jump, just compute sequential terms instead
														SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Dict         =      compute(A       ,		A       ,		i + dn       ,		A					)
														diff += _diff
														dn += terms_jumped

							SCREAMING_SNAKE_CASE       :   List[Any]         =      sub_memo[c]

							# keep jumps sorted by # of terms skipped
							SCREAMING_SNAKE_CASE       :   List[Any]         =      0
							while j < len(A					):
														if jumps[j][1] > dn:
																					break
														j += 1

							# cache the jump for this value digitsum(b) and c
							sub_memo[c].insert(A       ,		(diff, dn, k)					)
							return (diff, dn)
def        UpperCAmelCase (    A	:					str       ,		A	:					Dict       ,		A	:					str       ,		A	:					Optional[Any]					):
							if i >= n:
														return 0, i
							if k > len(A					):
														a_i.extend([0 for _ in range(k - len(A					)					)]					)

							# note: a_i -> b * 10^k + c
							# ds_b -> digitsum(b)
							# ds_c -> digitsum(c)
							SCREAMING_SNAKE_CASE       :   Tuple         =      i
							SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Dict         =      0, 0, 0
							for j in range(len(A					)					):
														if j >= k:
																					ds_b += a_i[j]
														else:
																					ds_c += a_i[j]

							while i < n:
														i += 1
														SCREAMING_SNAKE_CASE       :   Dict         =      ds_c + ds_b
														diff += addend
														SCREAMING_SNAKE_CASE       :   int         =      0
														for j in range(A					):
																					SCREAMING_SNAKE_CASE       :   List[str]         =      a_i[j] + addend
																					SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Dict         =      divmod(A       ,		10					)

																					ds_c += a_i[j]

														if addend > 0:
																					break

							if addend > 0:
														add(A       ,		A       ,		A					)
							return diff, i - start_i
def        UpperCAmelCase (    A	:					int       ,		A	:					int       ,		A	:					Dict					):
							for j in range(A       ,		len(A					)					):
														SCREAMING_SNAKE_CASE       :   Any         =      digits[j] + addend
														if s >= 10:
																					SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Tuple         =      divmod(A       ,		10					)
																					SCREAMING_SNAKE_CASE       :   Tuple         =      addend // 10 + quotient
														else:
																					SCREAMING_SNAKE_CASE       :   int         =      s
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      addend // 10

														if addend == 0:
																					break

							while addend > 0:
														SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Tuple         =      divmod(A       ,		10					)
														digits.append(A					)
def        UpperCAmelCase (    A	:					int = 10**15					):
							SCREAMING_SNAKE_CASE       :   Any         =      [1]
							SCREAMING_SNAKE_CASE       :   Optional[int]         =      1
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      0
							while True:
														SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   List[str]         =      next_term(A       ,		20       ,		i + dn       ,		A					)
														dn += terms_jumped
														if dn == n - i:
																					break

							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      0
							for j in range(len(A					)					):
														a_n += digits[j] * 10**j
							return a_n


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









'''simple docstring'''



import argparse
from pathlib import Path

import torch

from transformers import OPTConfig, OPTModel
from transformers.utils import logging


logging.set_verbosity_info()
lowerCAmelCase_  :							List[Any]             =    logging.get_logger(__name__)
def        UpperCAmelCase (    A	:					Dict					):
							SCREAMING_SNAKE_CASE       :   List[str]         =      torch.load(A       ,		map_location='''cpu'''					)
							if "model" in sd.keys():
														SCREAMING_SNAKE_CASE       :   List[Any]         =      torch.load(A       ,		map_location='''cpu'''					)['''model''']

							# pop unnecessary weights
							SCREAMING_SNAKE_CASE       :   Optional[int]         =      [
							    '''decoder.version''',
							    '''decoder.output_projection.weight''',
							]
							for key in keys_to_delete:
														if key in sd:
																					sd.pop(A					)

							SCREAMING_SNAKE_CASE       :   int         =      {
							    '''decoder.project_in_dim.weight''': '''decoder.project_in.weight''',
							    '''decoder.project_out_dim.weight''': '''decoder.project_out.weight''',
							    '''decoder.layer_norm.weight''': '''decoder.final_layer_norm.weight''',
							    '''decoder.layer_norm.bias''': '''decoder.final_layer_norm.bias''',
							}
							for old_key, new_key in keys_to_rename.items():
														if old_key in sd:
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      sd.pop(A					)

							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      list(sd.keys()					)
							for key in keys:
														if ".qkv_proj." in key:
																					SCREAMING_SNAKE_CASE       :   str         =      sd[key]
																					# We split QKV in separate Q,K,V

																					SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      key.replace('''.qkv_proj.'''       ,		'''.q_proj.'''					)
																					SCREAMING_SNAKE_CASE       :   str         =      key.replace('''.qkv_proj.'''       ,		'''.k_proj.'''					)
																					SCREAMING_SNAKE_CASE       :   Any         =      key.replace('''.qkv_proj.'''       ,		'''.v_proj.'''					)

																					SCREAMING_SNAKE_CASE       :   Tuple         =      value.shape[0]
																					assert depth % 3 == 0
																					# `SequeuceParallelTransformerBlock` has QKV weight is separated in K,V,Q despite the naming:
																					# https://cs.github.com/facebookresearch/metaseq/blob/51871bd73cd04c038f239ea2a26db1d7f6b37927/metaseq/modules/sequence_parallel_transformer_layer.py#L97
																					SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       ,SCREAMING_SNAKE_CASE       :   Tuple         =      torch.split(A       ,		depth // 3       ,		dim=0					)

																					SCREAMING_SNAKE_CASE       :   List[str]         =      q
																					SCREAMING_SNAKE_CASE       :   List[str]         =      k
																					SCREAMING_SNAKE_CASE       :   List[Any]         =      v
																					del sd[key]

							return sd
@torch.no_grad()
def        UpperCAmelCase (    A	:					int       ,		A	:					Union[str, Any]       ,		A	:					Dict=None					):
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      load_checkpoint(A					)

							if config is not None:
														SCREAMING_SNAKE_CASE       :   Tuple         =      OPTConfig.from_pretrained(A					)
							else:
														SCREAMING_SNAKE_CASE       :   int         =      OPTConfig()

							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      OPTModel(A					).half().eval()
							model.load_state_dict(A					)

							# Check results
							Path(A					).mkdir(exist_ok=A					)
							model.save_pretrained(A					)


if __name__ == "__main__":
							lowerCAmelCase_  :							List[str]             =    argparse.ArgumentParser()
							# Required parameters
							parser.add_argument(
							    '--fairseq_path',
							    type=str,
							    help=(
							        'path to fairseq checkpoint in correct format. You can find all checkpoints in the correct format here:'
							        ' https://huggingface.co/models?other=opt_metasq'
							    ),
							)
							parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
							parser.add_argument('--hf_config', default=None, type=str, help='Define HF config.')
							lowerCAmelCase_  :							str             =    parser.parse_args()
							convert_opt_checkpoint(args.fairseq_path, args.pytorch_dump_folder_path, config=args.hf_config)




