


"""simple docstring"""

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


UpperCAmelCase__  							=False





class    lowerCamelCase__						(		unittest.TestCase    ):
      pass






@nightly
@require_torch_gpu
class    lowerCamelCase__						(		unittest.TestCase    ):
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


       __lowercase									=	VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion"""      ,       torch_dtype=torch.floataa							)
       pipe.to(A__							)
       pipe.set_progress_bar_config(disable=A__							)

       __lowercase									=	load_image(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"""							)

       __lowercase									=	torch.manual_seed(0							)
       __lowercase									=	pipe.dual_guided(
           prompt="""first prompt"""      ,       image=A__      ,       text_to_image_strength=0.75      ,       generator=A__      ,       guidance_scale=7.5      ,       num_inference_steps=2      ,       output_type="""numpy"""      ,       ).images

       with tempfile.TemporaryDirectory() as tmpdirname:
        pipe.save_pretrained(A__							)
        __lowercase									=	VersatileDiffusionPipeline.from_pretrained(A__      ,       torch_dtype=torch.floataa							)
       pipe.to(A__							)
       pipe.set_progress_bar_config(disable=A__							)

       __lowercase									=	generator.manual_seed(0							)
       __lowercase									=	pipe.dual_guided(
           prompt="""first prompt"""      ,       image=A__      ,       text_to_image_strength=0.75      ,       generator=A__      ,       guidance_scale=7.5      ,       num_inference_steps=2      ,       output_type="""numpy"""      ,       ).images

       assert np.abs(image - new_image							).sum() < 1e-5, "Models don't have the same forward pass"






      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


       __lowercase									=	VersatileDiffusionPipeline.from_pretrained("""shi-labs/versatile-diffusion"""      ,       torch_dtype=torch.floataa							)
       pipe.to(A__							)
       pipe.set_progress_bar_config(disable=A__							)

       __lowercase									=	"""cyberpunk 2077"""
       __lowercase									=	load_image(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg"""							)
       __lowercase									=	torch.manual_seed(0							)
       __lowercase									=	pipe.dual_guided(
           prompt=A__      ,       image=A__      ,       text_to_image_strength=0.75      ,       generator=A__      ,       guidance_scale=7.5      ,       num_inference_steps=5_0      ,       output_type="""numpy"""      ,       ).images

       __lowercase									=	image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

       assert image.shape == (1, 5_1_2, 5_1_2, 3)
       __lowercase									=	np.array([0.14_48, 0.16_19, 0.17_41, 0.10_86, 0.11_47, 0.11_28, 0.11_99, 0.11_65, 0.10_01]							)

       assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-1

       __lowercase									=	"""A painting of a squirrel eating a burger """
       __lowercase									=	torch.manual_seed(0							)
       __lowercase									=	pipe.text_to_image(
           prompt=A__      ,       generator=A__      ,       guidance_scale=7.5      ,       num_inference_steps=5_0      ,       output_type="""numpy"""							).images

       __lowercase									=	image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

       assert image.shape == (1, 5_1_2, 5_1_2, 3)
       __lowercase									=	np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]							)

       assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-1

       __lowercase									=	pipe.image_variation(A__      ,       generator=A__      ,       output_type="""numpy"""							).images

       __lowercase									=	image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

       assert image.shape == (1, 5_1_2, 5_1_2, 3)
       __lowercase									=	np.array([0.30_76, 0.31_23, 0.32_84, 0.37_82, 0.37_70, 0.38_94, 0.42_97, 0.43_31, 0.44_56]							)

       assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-1




"""simple docstring"""

import argparse
import logging
import pickle
from collections import Counter


logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s", datefmt="%m/%d/%Y %H:%M:%S", level=logging.INFO
)
UpperCAmelCase__  							=logging.getLogger(__name__)

if __name__ == "__main__":
       UpperCAmelCase__  							=argparse.ArgumentParser(
           description="Token Counts for smoothing the masking probabilities in MLM (cf XLM/word2vec)"
       )
       parser.add_argument(
           "--data_file", type=str, default="data/dump.bert-base-uncased.pickle", help="The binarized dataset."
       )
       parser.add_argument(
           "--token_counts_dump", type=str, default="data/token_counts.bert-base-uncased.pickle", help="The dump file."
       )
       parser.add_argument("--vocab_size", default=3_0522, type=int)
       UpperCAmelCase__  							=parser.parse_args()

       logger.info(f"""Loading data from {args.data_file}""")
       with open(args.data_file, "rb") as fp:
              UpperCAmelCase__  							=pickle.load(fp)

       logger.info("Counting occurrences for MLM.")
       UpperCAmelCase__  							=Counter()
       for tk_ids in data:
              counter.update(tk_ids)
       UpperCAmelCase__  							=[0] * args.vocab_size
       for k, v in counter.items():
              UpperCAmelCase__  							=v

       logger.info(f"""Dump to {args.token_counts_dump}""")
       with open(args.token_counts_dump, "wb") as handle:
              pickle.dump(counts, handle, protocol=pickle.HIGHEST_PROTOCOL)
