






import argparse
import os

import torch

from transformers import FlavaImageCodebook, FlavaImageCodebookConfig

def      _lowerCamelCase	(						__A    :							Tuple		,		__A    :							str		,		__A    :							Any		,		__A    :							Optional[int] )      ->					List[Any]:
    _UpperCAmelCase				:      Optional[Any]			  =				s.rsplit(A__		,		A__ )
    return new.join(A__ )

def      _lowerCamelCase	(						__A    :							str )      ->					Optional[Any]:
    return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items() )

def      _lowerCamelCase	(						__A    :							Union[str, Any] )      ->					List[Any]:
    _UpperCAmelCase				:      Tuple			  =				{}

    _UpperCAmelCase				:      List[Any]			  =				['''group_1''', '''group_2''', '''group_3''', '''group_4''']
    for key, value in state_dict.items():
        for group_key in group_keys:
            if group_key in key:
                _UpperCAmelCase				:      List[str]			  =				key.replace(f'''{group_key}.'''		,		f'''{group_key}.group.''' )

        if "res_path" in key:
            _UpperCAmelCase				:      int			  =				key.replace('''res_path.'''		,		'''res_path.path.''' )

        if key.endswith('''.w''' ):
            _UpperCAmelCase				:      Tuple			  =				rreplace(A__		,		'''.w'''		,		'''.weight'''		,		1 )
        if key.endswith('''.b''' ):
            _UpperCAmelCase				:      Optional[int]			  =				rreplace(A__		,		'''.b'''		,		'''.bias'''		,		1 )

        _UpperCAmelCase				:      Dict			  =				value.float()

    return upgrade

@torch.no_grad()
def      _lowerCamelCase	(						__A    :							List[str]		,		__A    :							List[str]		,		__A    :							int=None		,		__A    :							Optional[Any]=True )      ->					Optional[int]:
    from dall_e import Encoder

    _UpperCAmelCase				:      Optional[Any]			  =				Encoder()
    if os.path.exists(A__ ):
        _UpperCAmelCase				:      Optional[Any]			  =				torch.load(A__ )
    else:
        _UpperCAmelCase				:      str			  =				torch.hub.load_state_dict_from_url(A__ )

    if isinstance(A__		,		A__ ):
        _UpperCAmelCase				:      List[str]			  =				ckpt.state_dict()
    encoder.load_state_dict(A__ )

    if config_path is not None:
        _UpperCAmelCase				:      str			  =				FlavaImageCodebookConfig.from_pretrained(A__ )
    else:
        _UpperCAmelCase				:      Any			  =				FlavaImageCodebookConfig()

    _UpperCAmelCase				:      Optional[Any]			  =				FlavaImageCodebook(A__ ).eval()
    _UpperCAmelCase				:      Any			  =				encoder.state_dict()

    _UpperCAmelCase				:      Union[str, Any]			  =				upgrade_state_dict(A__ )
    hf_model.load_state_dict(A__ )
    _UpperCAmelCase				:      Any			  =				hf_model.state_dict()
    _UpperCAmelCase				:      Union[str, Any]			  =				count_parameters(A__ )
    _UpperCAmelCase				:      Tuple			  =				count_parameters(A__ )

    assert torch.allclose(A__		,		A__		,		atol=1e-3 )

    if save_checkpoint:
        hf_model.save_pretrained(A__ )
    else:
        return hf_state_dict


if __name__ == "__main__":
  SCREAMING_SNAKE_CASE 							=						argparse.ArgumentParser()
  parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
  parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to flava checkpoint')
  parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
  SCREAMING_SNAKE_CASE 							=						parser.parse_args()

  convert_dalle_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path)












import math

def      _lowerCamelCase	(						__A    :							int )      ->					int:

    if not isinstance(__A		,		__A ):
        _UpperCAmelCase				:      List[Any]			  =				f'''Input value of [number={number}] must be an integer'''
        raise TypeError(__A )

    if number < 1:
        _UpperCAmelCase				:      Optional[Any]			  =				f'''Input value of [number={number}] must be > 0'''
        raise ValueError(__A )
    elif number == 1:
        return 3
    elif number == 2:
        return 5
    else:
        _UpperCAmelCase				:      Any			  =				int(math.log(number // 3		,		2 ) ) + 2

        _UpperCAmelCase				:      Dict			  =				[3, 5]
        _UpperCAmelCase				:      Tuple			  =				2
        _UpperCAmelCase				:      Dict			  =				3
        for block in range(1		,		__A ):
            for _ in range(__A ):
                proth_list.append(2 ** (block + 1) + proth_list[proth_index - 1] )
                proth_index += 1
            increment *= 2

    return proth_list[number - 1]


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  for number in range(11):
    SCREAMING_SNAKE_CASE 							=						0
    try:
      SCREAMING_SNAKE_CASE 							=						proth(number)
    except ValueError:
      print(F'ValueError: there is no {number}th Proth number')
      continue

    print(F'The {number}th Proth number: {value}')




