code: 
  
  
  import random
  import unittest
  
  import numpy as np
  
  from transformers.testing_utils import require_torch, require_vision
  from transformers.utils import is_torch_available, is_vision_available
  
  from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs
  
  
  if is_torch_available():
     import torch
  
  if is_vision_available():
     import PIL
  
     from transformers import FlavaImageProcessor
     from transformers.image_utils import PILImageResampling
     from transformers.models.flava.image_processing_flava import (
         FLAVA_CODEBOOK_MEAN,
         FLAVA_CODEBOOK_STD,
         FLAVA_IMAGE_MEAN,
         FLAVA_IMAGE_STD,
     )
  else:
     UpperCamelCase__				       =			UpperCamelCase__				       =			UpperCamelCase__				       =			UpperCamelCase__				       =			None
  
  class 						UpperCAmelCase__   (      unittest.TestCase		):
  
  
  
       '''simple docstring'''
  
  
  
  
       def __init__( self       :       str	,      UpperCamelCase       :       Tuple	,      UpperCamelCase       :       Union[str, Any]=7	,      UpperCamelCase       :       Optional[Any]=3	,      UpperCamelCase       :       List[Any]=30	,      UpperCamelCase       :       List[Any]=4_00	,      UpperCamelCase       :       List[str]=True	,      UpperCamelCase       :       int=None	,      UpperCamelCase       :       Any=True	,      UpperCamelCase       :       Union[str, Any]=None	,      UpperCamelCase       :       int=None	,      UpperCamelCase       :       str=True	,      UpperCamelCase       :       List[str]=1 / 2_55	,      UpperCamelCase       :       List[str]=True	,      UpperCamelCase       :       Dict=FLAVA_IMAGE_MEAN	,      UpperCamelCase       :       Optional[Any]=FLAVA_IMAGE_STD	,      UpperCamelCase       :       Dict=14	,      UpperCamelCase       :       List[Any]=75	,      UpperCamelCase       :       Optional[Any]=None	,      UpperCamelCase       :       Dict=16	,      UpperCamelCase       :       Optional[int]=0.3	,      UpperCamelCase       :       Optional[int]=None	,      UpperCamelCase       :       List[str]=True	,      UpperCamelCase       :       Any=None	,      UpperCamelCase       :       Optional[int]=None	,      UpperCamelCase       :       List[str]=True	,      UpperCamelCase       :       Optional[Any]=None	,      UpperCamelCase       :       List[Any]=True	,      UpperCamelCase       :       str=True	,      UpperCamelCase       :       Any=FLAVA_CODEBOOK_MEAN	,      UpperCamelCase       :       str=FLAVA_CODEBOOK_STD	,      ):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			size if size is not None else {'''height''': 2_24, '''width''': 2_24}
              _lowercase					:				Any           =			crop_size if crop_size is not None else {'''height''': 2_24, '''width''': 2_24}
              _lowercase					:				Dict           =			codebook_size if codebook_size is not None else {'''height''': 1_12, '''width''': 1_12}
              _lowercase					:				Tuple           =			codebook_crop_size if codebook_crop_size is not None else {'''height''': 1_12, '''width''': 1_12}
  
              _lowercase					:				Any           =			parent
              _lowercase					:				Tuple           =			batch_size
              _lowercase					:				int           =			num_channels
              _lowercase					:				Union[str, Any]           =			do_resize
              _lowercase					:				str           =			do_rescale
              _lowercase					:				Any           =			rescale_factor
              _lowercase					:				Tuple           =			min_resolution
              _lowercase					:				str           =			max_resolution
              _lowercase					:				List[Any]           =			size
              _lowercase					:				Optional[Any]           =			resample if resample is not None else PILImageResampling.BICUBIC
              _lowercase					:				Optional[int]           =			do_normalize
              _lowercase					:				Optional[Any]           =			image_mean
              _lowercase					:				int           =			image_std
              _lowercase					:				Dict           =			do_center_crop
              _lowercase					:				List[Any]           =			crop_size
  
              _lowercase					:				Any           =			input_size_patches
              _lowercase					:				Dict           =			total_mask_patches
              _lowercase					:				Optional[int]           =			mask_group_max_patches
              _lowercase					:				Optional[Any]           =			mask_group_min_patches
              _lowercase					:				List[str]           =			mask_group_min_aspect_ratio
              _lowercase					:				Tuple           =			mask_group_max_aspect_ratio
  
              _lowercase					:				int           =			codebook_do_resize
              _lowercase					:				List[str]           =			codebook_size
              _lowercase					:				int           =			codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
              _lowercase					:				Dict           =			codebook_do_center_crop
              _lowercase					:				List[str]           =			codebook_crop_size
              _lowercase					:				Dict           =			codebook_do_map_pixels
              _lowercase					:				Any           =			codebook_do_normalize
              _lowercase					:				Union[str, Any]           =			codebook_image_mean
              _lowercase					:				List[Any]           =			codebook_image_std
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              return {
                  "image_mean": self.image_mean,
                  "image_std": self.image_std,
                  "do_normalize": self.do_normalize,
                  "do_resize": self.do_resize,
                  "size": self.size,
                  "resample": self.resample,
                  "do_rescale": self.do_rescale,
                  "rescale_factor": self.rescale_factor,
                  "do_center_crop": self.do_center_crop,
                  "crop_size": self.crop_size,
                  "input_size_patches": self.input_size_patches,
                  "total_mask_patches": self.total_mask_patches,
                  "mask_group_max_patches": self.mask_group_max_patches,
                  "mask_group_min_patches": self.mask_group_min_patches,
                  "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
                  "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
                  "codebook_do_resize": self.codebook_do_resize,
                  "codebook_size": self.codebook_size,
                  "codebook_resample": self.codebook_resample,
                  "codebook_do_center_crop": self.codebook_do_center_crop,
                  "codebook_crop_size": self.codebook_crop_size,
                  "codebook_do_map_pixels": self.codebook_do_map_pixels,
                  "codebook_do_normalize": self.codebook_do_normalize,
                  "codebook_image_mean": self.codebook_image_mean,
                  "codebook_image_std": self.codebook_image_std,
              }
  
  
  
  
       def   lowerCAmelCase_   ( self       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              return (self.size["height"], self.size["width"])
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              return (
                  (self.input_size_patches, self.input_size_patches)
                  if not isinstance(self.input_size_patches	,      UpperCamelCase		)
                  else self.input_size_patches
              )
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              return (self.codebook_size["height"], self.codebook_size["width"])
  
  
  
  
  @require_torch
  @require_vision
  class 						UpperCAmelCase__   (      A_      ,						unittest.TestCase		):
  
  
  
       '''simple docstring'''
       UpperCAmelCase_         =  FlavaImageProcessor if is_vision_available() else None
       UpperCAmelCase_         =  None
  
  
  
  
       def   lowerCAmelCase_   ( self       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			FlavaImageProcessingTester(self		)
  
  
  
  
       @property
       def   lowerCAmelCase_   ( self       :       Dict		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Tuple           =			self.image_processing_class(**self.image_processor_dict		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''image_mean'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''image_std'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''do_normalize'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''do_resize'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''resample'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''crop_size'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''do_center_crop'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''do_rescale'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''rescale_factor'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''masking_generator'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_do_resize'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_size'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_resample'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_do_center_crop'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_crop_size'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_do_map_pixels'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_do_normalize'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_image_mean'''		)		)
              self.assertTrue(hasattr(UpperCamelCase	,      '''codebook_image_std'''		)		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				int           =			self.image_processing_class.from_dict(self.image_processor_dict		)
              self.assertEqual(image_processor.size	,      {'''height''': 2_24, '''width''': 2_24}		)
              self.assertEqual(image_processor.crop_size	,      {'''height''': 2_24, '''width''': 2_24}		)
              self.assertEqual(image_processor.codebook_size	,      {'''height''': 1_12, '''width''': 1_12}		)
              self.assertEqual(image_processor.codebook_crop_size	,      {'''height''': 1_12, '''width''': 1_12}		)
  
              _lowercase					:				List[str]           =			self.image_processing_class.from_dict(
                  self.image_processor_dict	,      size=42	,      crop_size=84	,      codebook_size=33	,      codebook_crop_size=66		)
              self.assertEqual(image_processor.size	,      {'''height''': 42, '''width''': 42}		)
              self.assertEqual(image_processor.crop_size	,      {'''height''': 84, '''width''': 84}		)
              self.assertEqual(image_processor.codebook_size	,      {'''height''': 33, '''width''': 33}		)
              self.assertEqual(image_processor.codebook_crop_size	,      {'''height''': 66, '''width''': 66}		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       int		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              pass
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				str           =			self.image_processing_class(**self.image_processor_dict		)
              # create random PIL images
              _lowercase					:				Optional[int]           =			prepare_image_inputs(self.image_processor_tester	,      equal_resolution=UpperCamelCase		)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase	,      PIL.Image.Image		)
  
              # Test not batched input
              _lowercase					:				int           =			image_processing(image_inputs[0]	,      return_tensors='''pt'''		)
  
              # Test no bool masked pos
              self.assertFalse('''bool_masked_pos''' in encoded_images		)
  
              _lowercase       ,	_lowercase					:				int           =			self.image_processor_tester.get_expected_image_size()
  
              self.assertEqual(
                  encoded_images.pixel_values.shape	,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)	,      )
  
              # Test batched
              _lowercase					:				str           =			image_processing(UpperCamelCase	,      return_tensors='''pt'''		)
              _lowercase       ,	_lowercase					:				int           =			self.image_processor_tester.get_expected_image_size()
  
              # Test no bool masked pos
              self.assertFalse('''bool_masked_pos''' in encoded_images		)
  
              self.assertEqual(
                  encoded_images.pixel_values.shape	,      (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )	,      )
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Any	,      UpperCamelCase       :       List[str]	,      UpperCamelCase       :       str		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				List[Any]           =			self.image_processing_class(**self.image_processor_dict		)
              # create random tensors
              _lowercase					:				int           =			prepare_image_inputs(self.image_processor_tester	,      equal_resolution=UpperCamelCase	,      **UpperCamelCase		)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase	,      UpperCamelCase		)
  
              # Test not batched input
              _lowercase					:				Dict           =			image_processing(image_inputs[0]	,      return_tensors='''pt'''		)
  
              _lowercase       ,	_lowercase					:				Optional[int]           =			self.image_processor_tester.get_expected_image_size()
              self.assertEqual(
                  encoded_images.pixel_values.shape	,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)	,      )
  
              _lowercase					:				List[Any]           =			image_processing(UpperCamelCase	,      return_image_mask=UpperCamelCase	,      return_tensors='''pt'''		)
  
              _lowercase       ,	_lowercase					:				List[str]           =			self.image_processor_tester.get_expected_image_size()
              self.assertEqual(
                  encoded_images.pixel_values.shape	,      (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )	,      )
  
              _lowercase       ,	_lowercase					:				Tuple           =			self.image_processor_tester.get_expected_mask_size()
              self.assertEqual(
                  encoded_images.bool_masked_pos.shape	,      (
                      self.image_processor_tester.batch_size,
                      expected_height,
                      expected_width,
                  )	,      )
  
              # Test batched
              _lowercase					:				Union[str, Any]           =			image_processing(UpperCamelCase	,      return_tensors='''pt'''		).pixel_values
  
              _lowercase       ,	_lowercase					:				Optional[int]           =			self.image_processor_tester.get_expected_image_size()
              self.assertEqual(
                  encoded_images.shape	,      (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )	,      )
  
              # Test masking
              _lowercase					:				Any           =			image_processing(UpperCamelCase	,      return_image_mask=UpperCamelCase	,      return_tensors='''pt'''		)
  
              _lowercase       ,	_lowercase					:				Any           =			self.image_processor_tester.get_expected_image_size()
              self.assertEqual(
                  encoded_images.pixel_values.shape	,      (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )	,      )
  
              _lowercase       ,	_lowercase					:				int           =			self.image_processor_tester.get_expected_mask_size()
              self.assertEqual(
                  encoded_images.bool_masked_pos.shape	,      (
                      self.image_processor_tester.batch_size,
                      expected_height,
                      expected_width,
                  )	,      )
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Union[str, Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              self._test_call_framework(np.ndarray	,      prepare_kwargs={'''numpify''': True}		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              self._test_call_framework(torch.Tensor	,      prepare_kwargs={'''torchify''': True}		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       List[str]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              random.seed(12_34		)
              _lowercase					:				Union[str, Any]           =			self.image_processing_class(**self.image_processor_dict		)
              _lowercase					:				Union[str, Any]           =			prepare_image_inputs(self.image_processor_tester	,      equal_resolution=UpperCamelCase	,      torchify=UpperCamelCase		)
  
              # Test not batched input
              _lowercase					:				Union[str, Any]           =			image_processing(image_inputs[0]	,      return_image_mask=UpperCamelCase	,      return_tensors='''pt'''		)
              self.assertEqual(encoded_images.bool_masked_pos.sum().item()	,      75		)
  
  
  
  
       def   lowerCAmelCase_   ( self       :       Optional[Any]		):
  
  
  
  
  
              """simple docstring"""
  
  
  
              _lowercase					:				Optional[Any]           =			self.image_processing_class(**self.image_processor_dict		)
              # create random PIL images
              _lowercase					:				Tuple           =			prepare_image_inputs(self.image_processor_tester	,      equal_resolution=UpperCamelCase		)
              for image in image_inputs:
                     self.assertIsInstance(UpperCamelCase	,      PIL.Image.Image		)
  
              # Test not batched input
              _lowercase					:				Union[str, Any]           =			image_processing(image_inputs[0]	,      return_codebook_pixels=UpperCamelCase	,      return_tensors='''pt'''		)
              _lowercase       ,	_lowercase					:				List[Any]           =			self.image_processor_tester.get_expected_codebook_image_size()
              self.assertEqual(
                  encoded_images.codebook_pixel_values.shape	,      (1, self.image_processor_tester.num_channels, expected_height, expected_width)	,      )
  
              # Test batched
              _lowercase					:				List[str]           =			image_processing(UpperCamelCase	,      return_codebook_pixels=UpperCamelCase	,      return_tensors='''pt'''		)
              _lowercase       ,	_lowercase					:				Any           =			self.image_processor_tester.get_expected_codebook_image_size()
              self.assertEqual(
                  encoded_images.codebook_pixel_values.shape	,      (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      expected_height,
                      expected_width,
                  )	,      )
style_context: 
  
  
  import os
  from pathlib import Path
  from typing import Dict, List
  
  import fire
  import torch
  
  from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
  from transformers.utils.logging import get_logger
  
  
  UpperCamelCase__				       =			get_logger(__name__)
  
  def   UpperCamelCase__   (    UpperCAmelCase_    ,     UpperCAmelCase_			)						->							List[str]:
  
  
  
  
         '''simple docstring'''
  
  
  
  
  
  
         if text.startswith(UpperCAmelCase_			):
                return text[len(UpperCAmelCase_			) :]
         return text  # or whatever
  
  def   UpperCamelCase__   (    UpperCAmelCase_			)						->							Optional[int]:
  
  
  
  
         '''simple docstring'''
  
  
  
  
  
  
         return {remove_prefix(UpperCAmelCase_    ,     '''model.'''			): v for k, v in sd.items()}
  
  def   UpperCamelCase__   (    UpperCAmelCase_			)						->							Any:
  
  
  
  
         '''simple docstring'''
  
  
  
  
  
  
         _lowercase					:				Optional[int]           =			{}
         for k in state_dicts[0].keys():
                _lowercase					:				Tuple           =			[sd[k] for sd in state_dicts]
                _lowercase					:				Union[str, Any]           =			sum(UpperCAmelCase_			) / len(UpperCAmelCase_			)
                assert isinstance(UpperCAmelCase_    ,     torch.Tensor			)
                _lowercase					:				Union[str, Any]           =			new_t
         return new_sd
  
  def   UpperCamelCase__   (    UpperCAmelCase_    ,     UpperCAmelCase_    ,     UpperCAmelCase_			)						->							None:
  
  
  
  
         '''simple docstring'''
  
  
  
  
  
  
         _lowercase					:				List[str]           =			AutoModelForSeqaSeqLM.from_pretrained(UpperCAmelCase_			)
         if os.path.isfile(UpperCAmelCase_			):
                _lowercase					:				Optional[Any]           =			[pl_ckpt_path]
         else:
                assert os.path.isdir(UpperCAmelCase_			)
                _lowercase					:				List[str]           =			list(Path(UpperCAmelCase_			).glob('''*.ckpt'''			)			)
                assert ckpt_files, F'could not find any ckpt files inside the {pl_ckpt_path} directory'
  
         if len(UpperCAmelCase_			) > 1:
                logger.info(F'averaging the weights of {ckpt_files}'			)
  
         _lowercase					:				List[Any]           =			[sanitize(torch.load(UpperCAmelCase_    ,     map_location='''cpu'''			)['''state_dict''']			) for x in ckpt_files]
         _lowercase					:				Optional[Any]           =			average_state_dicts(UpperCAmelCase_			)
  
         _lowercase       ,	_lowercase					:				str           =			hf_model.load_state_dict(UpperCAmelCase_    ,     strict=UpperCAmelCase_			)
         assert not missing, F'missing keys: {missing}'
         hf_model.save_pretrained(UpperCAmelCase_			)
         try:
                _lowercase					:				Union[str, Any]           =			AutoTokenizer.from_pretrained(UpperCAmelCase_			)
                tok.save_pretrained(UpperCAmelCase_			)
         except Exception:
                pass
                # dont copy tokenizer if cant
  
  
  if __name__ == "__main__":
     fire.Fire(convert_pl_to_hf)