






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 					lowercase__				(					__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					return sum(param.float().sum() if 'encoder.embeddings' not in key else 0 for key, param in state_dict.items()  )




def 					lowercase__				(					__snake_case   :							Optional[int]					,	__snake_case   :							Union[str, Any]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Tuple						=						{}

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

										UpperCAmelCase_   :				List[Any]						=						key.replace('heads.cmd.mim_head.cls.predictions'					,	'mmm_image_head'  )
										UpperCAmelCase_   :				List[str]						=						key.replace('heads.cmd.mlm_head.cls.predictions'					,	'mmm_text_head'  )
										UpperCAmelCase_   :				Tuple						=						key.replace('heads.cmd.itm_head.cls'					,	'itm_head'  )
										UpperCAmelCase_   :				int						=						key.replace('heads.cmd.itm_head.pooler'					,	'itm_head.pooler'  )
										UpperCAmelCase_   :				Optional[int]						=						key.replace('heads.cmd.clip_head.logit_scale'					,	'flava.logit_scale'  )
										UpperCAmelCase_   :				Tuple						=						key.replace('heads.fairseq_mlm.cls.predictions'					,	'mlm_head'  )
										UpperCAmelCase_   :				List[Any]						=						key.replace('heads.imagenet.mim_head.cls.predictions'					,	'mim_head'  )
										UpperCAmelCase_   :				str						=						key.replace('mm_text_projection'					,	'flava.text_to_mm_projection'  )
										UpperCAmelCase_   :				Any						=						key.replace('mm_image_projection'					,	'flava.image_to_mm_projection'  )
										UpperCAmelCase_   :				Tuple						=						key.replace('image_encoder.module'					,	'flava.image_model'  )
										UpperCAmelCase_   :				Dict						=						key.replace('text_encoder.module'					,	'flava.text_model'  )
										UpperCAmelCase_   :				Tuple						=						key.replace('mm_encoder.module.encoder.cls_token'					,	'flava.multimodal_model.cls_token'  )
										UpperCAmelCase_   :				Any						=						key.replace('mm_encoder.module'					,	'flava.multimodal_model'  )
										UpperCAmelCase_   :				List[str]						=						key.replace('text_projection'					,	'flava.text_projection'  )
										UpperCAmelCase_   :				int						=						key.replace('image_projection'					,	'flava.image_projection'  )

										UpperCAmelCase_   :				Tuple						=						value.float()

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

					return upgrade




@torch.no_grad()
def 					lowercase__				(					__snake_case   :							Optional[Any]					,	__snake_case   :							Union[str, Any]					,	__snake_case   :							Any					,	__snake_case   :							Optional[Any]=None  ):


					'''simple docstring'''


					if config_path is not None:
										UpperCAmelCase_   :				List[Any]						=						FlavaConfig.from_pretrained(__snake_case  )
					else:
										UpperCAmelCase_   :				Optional[int]						=						FlavaConfig()

					UpperCAmelCase_   :				Any						=						FlavaForPreTraining(__snake_case  ).eval()

					UpperCAmelCase_   :				str						=						convert_dalle_checkpoint(__snake_case					,	__snake_case					,	save_checkpoint=__snake_case  )

					if os.path.exists(__snake_case  ):
										UpperCAmelCase_   :				Any						=						torch.load(__snake_case					,	map_location='cpu'  )
					else:
										UpperCAmelCase_   :				Optional[int]						=						torch.hub.load_state_dict_from_url(__snake_case					,	map_location='cpu'  )

					UpperCAmelCase_   :				Any						=						upgrade_state_dict(__snake_case					,	__snake_case  )
					hf_model.load_state_dict(__snake_case  )
					UpperCAmelCase_   :				Optional[Any]						=						hf_model.state_dict()
					UpperCAmelCase_   :				Any						=						count_parameters(__snake_case  )
					UpperCAmelCase_   :				Any						=						count_parameters(__snake_case  ) + count_parameters(__snake_case  )

					assert torch.allclose(__snake_case					,	__snake_case					,	atol=1E-3  )

					hf_model.save_pretrained(__snake_case  )


if __name__ == "__main__":
		__UpperCAmelCase     							=						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')
		__UpperCAmelCase     							=						parser.parse_args()

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














def 					lowercase__				(					__snake_case   :							Tuple					,	__snake_case   :							List[Any]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Any						=						''
					for i in table:
										res += inp[i - 1]
					return res




def 					lowercase__				(					__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					return data[1:] + data[0]




def 					lowercase__				(					__snake_case   :							Union[str, Any]					,	__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Any						=						''
					for i in range(len(__snake_case  )  ):
										if a[i] == b[i]:
															res += "0"
										else:
															res += "1"
					return res




def 					lowercase__				(					__snake_case   :							Dict					,	__snake_case   :							Optional[int]  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Optional[Any]						=						int('0b' + data[0] + data[-1]					,	2  )
					UpperCAmelCase_   :				Any						=						int('0b' + data[1:3]					,	2  )
					return bin(s[row][col]  )[2:]




def 					lowercase__				(					__snake_case   :							Tuple					,	__snake_case   :							List[Any]					,	__snake_case   :							List[Any]					,	__snake_case   :							Optional[int]					,	__snake_case   :							str  ):


					'''simple docstring'''


					UpperCAmelCase_   :				Union[str, Any]						=						message[:4]
					UpperCAmelCase_   :				Optional[int]						=						message[4:]
					UpperCAmelCase_   :				Union[str, Any]						=						apply_table(__snake_case					,	__snake_case  )
					UpperCAmelCase_   :				Union[str, Any]						=						xor(__snake_case					,	__snake_case  )
					UpperCAmelCase_   :				Dict						=						apply_sbox(__snake_case					,	temp[:4]  )  # noqa: E741
					UpperCAmelCase_   :				Any						=						apply_sbox(__snake_case					,	temp[4:]  )
					UpperCAmelCase_   :				str						=						'0' * (2 - len(__snake_case  )) + l  # noqa: E741
					UpperCAmelCase_   :				int						=						'0' * (2 - len(__snake_case  )) + r
					UpperCAmelCase_   :				int						=						apply_table(l + r					,	__snake_case  )
					UpperCAmelCase_   :				int						=						xor(__snake_case					,	__snake_case  )
					return temp + right


if __name__ == "__main__":
		__UpperCAmelCase     							=						input('Enter 10 bit key: ')
		__UpperCAmelCase     							=						input('Enter 8 bit message: ')

		__UpperCAmelCase     							=						[6, 3, 7, 4, 8, 5, 10, 9]
		__UpperCAmelCase     							=						[3, 5, 2, 7, 4, 10, 1, 9, 8, 6]
		__UpperCAmelCase     							=						[2, 4, 3, 1]
		__UpperCAmelCase     							=						[2, 6, 3, 1, 4, 8, 5, 7]
		__UpperCAmelCase     							=						[4, 1, 3, 5, 7, 2, 8, 6]
		__UpperCAmelCase     							=						[4, 1, 2, 3, 2, 3, 4, 1]
		__UpperCAmelCase     							=						[[1, 0, 3, 2], [3, 2, 1, 0], [0, 2, 1, 3], [3, 1, 3, 2]]
		__UpperCAmelCase     							=						[[0, 1, 2, 3], [2, 0, 1, 3], [3, 0, 1, 0], [2, 1, 0, 3]]

		# key generation
		__UpperCAmelCase     							=						apply_table(key, paa_table)
		__UpperCAmelCase     							=						temp[:5]
		__UpperCAmelCase     							=						temp[5:]
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						apply_table(left + right, pa_table)
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						left_shift(left)
		__UpperCAmelCase     							=						left_shift(right)
		__UpperCAmelCase     							=						apply_table(left + right, pa_table)

		# encryption
		__UpperCAmelCase     							=						apply_table(message, IP)
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						temp[4:] + temp[:4]
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						apply_table(temp, IP_inv)
		print('Cipher text is:', CT)

		# decryption
		__UpperCAmelCase     							=						apply_table(CT, IP)
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						temp[4:] + temp[:4]
		__UpperCAmelCase     							=						function(expansion, sa, sa, keya, temp)
		__UpperCAmelCase     							=						apply_table(temp, IP_inv)
		print('Plain text after decypting is:', PT)






