"""simple docstring"""







__SCREAMING_SNAKE_CASE             =range(2, 20 + 1)
__SCREAMING_SNAKE_CASE             =[10**k for k in range(ks[-1] + 1)]
__SCREAMING_SNAKE_CASE             ={}



def      lowercase__(			__SCREAMING_SNAKE_CASE				:	Any	,     __SCREAMING_SNAKE_CASE				:	Any	,     __SCREAMING_SNAKE_CASE				:	Optional[int]	,     __SCREAMING_SNAKE_CASE				:	Optional[Any] ):
     lowercase_      :							str						=      sum(a_i[j] for j in range(__SCREAMING_SNAKE_CASE	,     len(__SCREAMING_SNAKE_CASE ) ) )
     lowercase_      :							Union[str, Any]						=      sum(a_i[j] * base[j] for j in range(min(len(__SCREAMING_SNAKE_CASE )	,     __SCREAMING_SNAKE_CASE ) ) )

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

     lowercase_      :							str						=      memo.get(__SCREAMING_SNAKE_CASE )

     if sub_memo is not None:
          lowercase_      :							int						=      sub_memo.get(__SCREAMING_SNAKE_CASE )

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

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

          else:
               lowercase_      :							List[Any]						=      []
     else:
          lowercase_      :							Dict						=      {c: []}
          lowercase_      :							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
               lowercase_      :							int						=      next_term(__SCREAMING_SNAKE_CASE	,     k - 1	,     i + dn	,     __SCREAMING_SNAKE_CASE )
               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
          lowercase_      :							Dict						=      compute(__SCREAMING_SNAKE_CASE	,     __SCREAMING_SNAKE_CASE	,     i + dn	,     __SCREAMING_SNAKE_CASE )
          diff += _diff
          dn += terms_jumped

     lowercase_      :							List[Any]						=      sub_memo[c]

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

     # cache the jump for this value digitsum(b) and c
     sub_memo[c].insert(__SCREAMING_SNAKE_CASE	,     (diff, dn, k) )
     return (diff, dn)



def      lowercase__(			__SCREAMING_SNAKE_CASE				:	str	,     __SCREAMING_SNAKE_CASE				:	Dict	,     __SCREAMING_SNAKE_CASE				:	str	,     __SCREAMING_SNAKE_CASE				:	Optional[Any] ):
     if i >= n:
          return 0, i
     if k > len(__SCREAMING_SNAKE_CASE ):
          a_i.extend([0 for _ in range(k - len(__SCREAMING_SNAKE_CASE ) )] )

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

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

               ds_c += a_i[j]

          if addend > 0:
               break

     if addend > 0:
          add(__SCREAMING_SNAKE_CASE	,     __SCREAMING_SNAKE_CASE	,     __SCREAMING_SNAKE_CASE )
     return diff, i - start_i



def      lowercase__(			__SCREAMING_SNAKE_CASE				:	int	,     __SCREAMING_SNAKE_CASE				:	int	,     __SCREAMING_SNAKE_CASE				:	Dict ):
     for j in range(__SCREAMING_SNAKE_CASE	,     len(__SCREAMING_SNAKE_CASE ) ):
          lowercase_      :							Any						=      digits[j] + addend
          if s >= 10:
               lowercase_      :							Tuple						=      divmod(__SCREAMING_SNAKE_CASE	,     10 )
               lowercase_      :							Tuple						=      addend // 10 + quotient
          else:
               lowercase_      :							int						=      s
               lowercase_      :							Optional[int]						=      addend // 10

          if addend == 0:
               break

     while addend > 0:
          lowercase_      :							Tuple						=      divmod(__SCREAMING_SNAKE_CASE	,     10 )
          digits.append(__SCREAMING_SNAKE_CASE )



def      lowercase__(			__SCREAMING_SNAKE_CASE				:	int = 10**15 ):
     lowercase_      :							Any						=      [1]
     lowercase_      :							Optional[int]						=      1
     lowercase_      :							Optional[Any]						=      0
     while True:
          lowercase_      :							List[str]						=      next_term(__SCREAMING_SNAKE_CASE	,     20	,     i + dn	,     __SCREAMING_SNAKE_CASE )
          dn += terms_jumped
          if dn == n - i:
               break

     lowercase_      :							Optional[Any]						=      0
     for j in range(len(__SCREAMING_SNAKE_CASE ) ):
          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)




