





'''simple docstring'''







import argparse
import os

import torch

from transformers import FlavaConfig, FlavaForPreTraining
from transformers.models.flava.convert_dalle_to_flava_codebook import convert_dalle_checkpoint

def    __UpperCamelCase	(	a    : Optional[int]     ) ->Tuple:
 # encoder.embeddings are double copied in original FLAVA
 return sum(param.float().sum() if '''encoder.embeddings''' not in key else 0 for key, param in state_dict.items()     )

def    __UpperCamelCase	(	a    : Dict					,   a    : Optional[int]     ) ->str:
 snake_case				=			{}

 for key, value in state_dict.items():
  if "text_encoder.embeddings" in key or "image_encoder.embeddings" in key:
   continue

  snake_case				=			key.replace('''heads.cmd.mim_head.cls.predictions'''					,   '''mmm_image_head'''     )
  snake_case				=			key.replace('''heads.cmd.mlm_head.cls.predictions'''					,   '''mmm_text_head'''     )
  snake_case				=			key.replace('''heads.cmd.itm_head.cls'''					,   '''itm_head'''     )
  snake_case				=			key.replace('''heads.cmd.itm_head.pooler'''					,   '''itm_head.pooler'''     )
  snake_case				=			key.replace('''heads.cmd.clip_head.logit_scale'''					,   '''flava.logit_scale'''     )
  snake_case				=			key.replace('''heads.fairseq_mlm.cls.predictions'''					,   '''mlm_head'''     )
  snake_case				=			key.replace('''heads.imagenet.mim_head.cls.predictions'''					,   '''mim_head'''     )
  snake_case				=			key.replace('''mm_text_projection'''					,   '''flava.text_to_mm_projection'''     )
  snake_case				=			key.replace('''mm_image_projection'''					,   '''flava.image_to_mm_projection'''     )
  snake_case				=			key.replace('''image_encoder.module'''					,   '''flava.image_model'''     )
  snake_case				=			key.replace('''text_encoder.module'''					,   '''flava.text_model'''     )
  snake_case				=			key.replace('''mm_encoder.module.encoder.cls_token'''					,   '''flava.multimodal_model.cls_token'''     )
  snake_case				=			key.replace('''mm_encoder.module'''					,   '''flava.multimodal_model'''     )
  snake_case				=			key.replace('''text_projection'''					,   '''flava.text_projection'''     )
  snake_case				=			key.replace('''image_projection'''					,   '''flava.image_projection'''     )

  snake_case				=			value.float()

 for key, value in codebook_state_dict.items():
  snake_case				=			value

 return upgrade

@torch.no_grad()
def    __UpperCamelCase	(	a    : Any					,   a    : Dict					,   a    : Dict					,   a    : Any=None     ) ->int:
 if config_path is not None:
  snake_case				=			FlavaConfig.from_pretrained(a     )
 else:
  snake_case				=			FlavaConfig()

 snake_case				=			FlavaForPreTraining(a     ).eval()

 snake_case				=			convert_dalle_checkpoint(a					,   a					,   save_checkpoint=a     )

 if os.path.exists(a     ):
  snake_case				=			torch.load(a					,   map_location='''cpu'''     )
 else:
  snake_case				=			torch.hub.load_state_dict_from_url(a					,   map_location='''cpu'''     )

 snake_case				=			upgrade_state_dict(a					,   a     )
 hf_model.load_state_dict(a     )
 snake_case				=			hf_model.state_dict()
 snake_case				=			count_parameters(a     )
 snake_case				=			count_parameters(a     ) + count_parameters(a     )

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

 hf_model.save_pretrained(a     )


if __name__ == "__main__":
   _lowercase       						=			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('--codebook_path', default=None, type=str, help='Path to flava codebook checkpoint')
   parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
   _lowercase       						=			parser.parse_args()

   convert_flava_checkpoint(args.checkpoint_path, args.codebook_path, args.pytorch_dump_folder_path, args.config_path)








'''simple docstring'''







from collections import Counter
from pathlib import Path
from typing import Optional, Tuple

import yaml
class 			_lowercase						(       yaml.SafeLoader					):







     def     UpperCamelCase			( self    ,   A__    ) ->	List[str]:
      snake_case				=			[self.constructed_objects[key_node] for key_node, _ in node.value]
      snake_case				=			[tuple(A__    ) if isinstance(A__    ,   A__    ) else key for key in keys]
      snake_case				=			Counter(A__    )
      snake_case				=			[key for key in counter if counter[key] > 1]
      if duplicate_keys:
       raise TypeError(F"""Got duplicate yaml keys: {duplicate_keys}"""    )







     def     UpperCamelCase			( self    ,   A__    ,   A__=False    ) ->	List[Any]:
      snake_case				=			super().construct_mapping(A__    ,   deep=A__    )
      self._check_no_duplicates_on_constructed_node(A__    )
      return mapping




def    __UpperCamelCase	(	a    : str     ) ->Tuple[Optional[str], str]:
 snake_case				=			list(readme_content.splitlines()     )
 if full_content and full_content[0] == "---" and "---" in full_content[1:]:
  snake_case				=			full_content[1:].index('''---'''     ) + 1
  snake_case				=			'''\n'''.join(full_content[1:sep_idx]     )
  return yamlblock, "\n".join(full_content[sep_idx + 1 :]     )

 return None, "\n".join(a     )
class 			_lowercase						(       __a					):
     # class attributes
     _UpperCAmelCase										=					{'''train_eval_index'''}  # train-eval-index in the YAML metadata







     @classmethod
     def     UpperCamelCase			( cls    ,   A__    ) ->	"DatasetMetadata":
      with open(A__    ,   encoding='''utf-8'''    ) as readme_file:
       snake_case				,		snake_case				=			_split_yaml_from_readme(readme_file.read()    )
      if yaml_string is not None:
       return cls.from_yaml_string(A__    )
      else:
       return cls()







     def     UpperCamelCase			( self    ,   A__    ) ->	str:
      if path.exists():
       with open(A__    ,   encoding='''utf-8'''    ) as readme_file:
        snake_case				=			readme_file.read()
      else:
       snake_case				=			None
      snake_case				=			self._to_readme(A__    )
      with open(A__    ,   '''w'''    ,   encoding='''utf-8'''    ) as readme_file:
       readme_file.write(A__    )







     def     UpperCamelCase			( self    ,   A__ = None    ) ->	str:
      if readme_content is not None:
       snake_case				,		snake_case				=			_split_yaml_from_readme(A__    )
       snake_case				=			'''---\n''' + self.to_yaml_string() + '''---\n''' + content
      else:
       snake_case				=			'''---\n''' + self.to_yaml_string() + '''---\n'''
      return full_content







     @classmethod
     def     UpperCamelCase			( cls    ,   A__    ) ->	"DatasetMetadata":
      snake_case				=			yaml.load(A__    ,   Loader=_NoDuplicateSafeLoader    ) or {}

      # Convert the YAML keys to DatasetMetadata fields
      snake_case				=			{
          (key.replace('''-'''    ,   '''_'''    ) if key.replace('''-'''    ,   '''_'''    ) in cls._FIELDS_WITH_DASHES else key): value
          for key, value in metadata_dict.items()
      }
      return cls(**A__    )







     def     UpperCamelCase			( self    ) ->	str:
      return yaml.safe_dump(
          {
              (key.replace('''_'''    ,   '''-'''    ) if key in self._FIELDS_WITH_DASHES else key): value
              for key, value in self.items()
          }    ,   sort_keys=A__    ,   allow_unicode=A__    ,   encoding='''utf-8'''    ,   ).decode('''utf-8'''    )




_lowercase       						=			{
    'image-classification': [],
    'translation': [],
    'image-segmentation': [],
    'fill-mask': [],
    'automatic-speech-recognition': [],
    'token-classification': [],
    'sentence-similarity': [],
    'audio-classification': [],
    'question-answering': [],
    'summarization': [],
    'zero-shot-classification': [],
    'table-to-text': [],
    'feature-extraction': [],
    'other': [],
    'multiple-choice': [],
    'text-classification': [],
    'text-to-image': [],
    'text2text-generation': [],
    'zero-shot-image-classification': [],
    'tabular-classification': [],
    'tabular-regression': [],
    'image-to-image': [],
    'tabular-to-text': [],
    'unconditional-image-generation': [],
    'text-retrieval': [],
    'text-to-speech': [],
    'object-detection': [],
    'audio-to-audio': [],
    'text-generation': [],
    'conversational': [],
    'table-question-answering': [],
    'visual-question-answering': [],
    'image-to-text': [],
    'reinforcement-learning': [],
    'voice-activity-detection': [],
    'time-series-forecasting': [],
    'document-question-answering': [],
}


if __name__ == "__main__":
   from argparse import ArgumentParser

   _lowercase       						=			ArgumentParser(usage='Validate the yaml metadata block of a README.md file.')
   ap.add_argument('readme_filepath')
   _lowercase       						=			ap.parse_args()

   _lowercase       						=			Path(args.readme_filepath)
   _lowercase       						=			DatasetMetadata.from_readme(readme_filepath)
   print(dataset_metadata)
   dataset_metadata.to_readme(readme_filepath)

