




import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







class 		lowercase							(     unittest.TestCase	):

     def       a__			(						self       )		->							List[Any]:
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()

     @property
     def       a__			(						self       )		->							Dict:
          _A							:  Tuple    =		1
          _A							:  List[Any]    =		3
          _A							:  int    =		(32, 32)

          _A							:  Any    =		floats_tensor((batch_size, num_channels) + sizes	,  rng=random.Random(0       )       ).to(__a       )
          return image

     @property
     def       a__			(						self       )		->							str:
          torch.manual_seed(0       )
          _A							:  int    =		UNetaDConditionModel(
              block_out_channels=(32, 64)	,  layers_per_block=2	,  sample_size=32	,  in_channels=4	,  out_channels=4	,  down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")	,  up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")	,  cross_attention_dim=32	,  )
          return model

     @property
     def       a__			(						self       )		->							Optional[Any]:
          torch.manual_seed(0       )
          _A							:  Optional[Any]    =		AutoencoderKL(
              block_out_channels=[32, 64]	,  in_channels=3	,  out_channels=3	,  down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]	,  up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]	,  latent_channels=4	,  )
          return model

     @property
     def       a__			(						self       )		->							str:
          torch.manual_seed(0       )
          _A							:  List[Any]    =		RobertaSeriesConfig(
              hidden_size=32	,  project_dim=32	,  intermediate_size=37	,  layer_norm_eps=1e-05	,  num_attention_heads=4	,  num_hidden_layers=5	,  pad_token_id=1	,  vocab_size=5006	,  )
          return RobertaSeriesModelWithTransformation(__a       )

     @property
     def       a__			(						self       )		->							str:
          def extract(*_a	,  **_a       ):







               class 		lowercase							:

                    def __init__(						self       )		->							List[Any]:
                         _A							:  Union[str, Any]    =		torch.ones([0]       )



                    def       a__			(						self	,  _a       )		->							Optional[Any]:
                         self.pixel_values.to(__a       )
                         return self


               return Out()

          return extract

     def       a__			(						self       )		->							Optional[int]:
          _A							:  Union[str, Any]    =		"""cpu"""  # ensure determinism for the device-dependent torch.Generator
          _A							:  Optional[Any]    =		self.dummy_cond_unet
          _A							:  List[str]    =		PNDMScheduler(skip_prk_steps=__a       )
          _A							:  List[str]    =		self.dummy_vae
          _A							:  Any    =		self.dummy_text_encoder
          _A							:  Union[str, Any]    =		XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""       )
          _A							:  List[Any]    =		77

          _A							:  Dict    =		self.dummy_image.to(__a       )
          _A							:  List[str]    =		init_image / 2 + 0.5

          # make sure here that pndm scheduler skips prk
          _A							:  str    =		AltDiffusionImgaImgPipeline(
              unet=__a	,  scheduler=__a	,  vae=__a	,  text_encoder=__a	,  tokenizer=__a	,  safety_checker=__a	,  feature_extractor=self.dummy_extractor	,  )
          _A							:  int    =		VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor	,  do_normalize=__a       )
          _A							:  Any    =		alt_pipe.to(__a       )
          alt_pipe.set_progress_bar_config(disable=__a       )

          _A							:  Dict    =		"""A painting of a squirrel eating a burger"""
          _A							:  List[Any]    =		torch.Generator(device=__a       ).manual_seed(0       )
          _A							:  Optional[Any]    =		alt_pipe(
              [prompt]	,  generator=__a	,  guidance_scale=6.0	,  num_inference_steps=2	,  output_type="""np"""	,  image=__a	,  )

          _A							:  Optional[int]    =		output.images

          _A							:  Optional[Any]    =		torch.Generator(device=__a       ).manual_seed(0       )
          _A							:  Optional[int]    =		alt_pipe(
              [prompt]	,  generator=__a	,  guidance_scale=6.0	,  num_inference_steps=2	,  output_type="""np"""	,  image=__a	,  return_dict=__a	,  )[0]

          _A							:  Tuple    =		image[0, -3:, -3:, -1]
          _A							:  str    =		image_from_tuple[0, -3:, -3:, -1]

          assert image.shape == (1, 32, 32, 3)
          _A							:  Dict    =		np.array([0.4427, 0.3731, 0.4249, 0.4941, 0.4546, 0.4148, 0.4193, 0.4666, 0.4499]       )

          assert np.abs(image_slice.flatten() - expected_slice       ).max() < 5e-3
          assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 5e-3

     @unittest.skipIf(torch_device != """cuda"""	,  """This test requires a GPU"""       )
     def       a__			(						self       )		->							str:
          _A							:  List[str]    =		self.dummy_cond_unet
          _A							:  List[str]    =		PNDMScheduler(skip_prk_steps=__a       )
          _A							:  Optional[int]    =		self.dummy_vae
          _A							:  Tuple    =		self.dummy_text_encoder
          _A							:  Tuple    =		XLMRobertaTokenizer.from_pretrained("""hf-internal-testing/tiny-xlm-roberta"""       )
          _A							:  Any    =		77

          _A							:  List[str]    =		self.dummy_image.to(__a       )

          # put models in fp16
          _A							:  Union[str, Any]    =		unet.half()
          _A							:  Any    =		vae.half()
          _A							:  Optional[Any]    =		bert.half()

          # make sure here that pndm scheduler skips prk
          _A							:  Union[str, Any]    =		AltDiffusionImgaImgPipeline(
              unet=__a	,  scheduler=__a	,  vae=__a	,  text_encoder=__a	,  tokenizer=__a	,  safety_checker=__a	,  feature_extractor=self.dummy_extractor	,  )
          _A							:  Optional[Any]    =		VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor	,  do_normalize=__a       )
          _A							:  List[Any]    =		alt_pipe.to(__a       )
          alt_pipe.set_progress_bar_config(disable=__a       )

          _A							:  Optional[int]    =		"""A painting of a squirrel eating a burger"""
          _A							:  int    =		torch.manual_seed(0       )
          _A							:  str    =		alt_pipe(
              [prompt]	,  generator=__a	,  num_inference_steps=2	,  output_type="""np"""	,  image=__a	,  ).images

          assert image.shape == (1, 32, 32, 3)



     @unittest.skipIf(torch_device != """cuda"""	,  """This test requires a GPU"""       )
     def       a__			(						self       )		->							int:
          _A							:  Dict    =		load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/img2img/sketch-mountains-input.jpg"""       )
          # resize to resolution that is divisible by 8 but not 16 or 32
          _A							:  Tuple    =		init_image.resize((760, 504)       )

          _A							:  Optional[Any]    =		"""BAAI/AltDiffusion"""
          _A							:  str    =		AltDiffusionImgaImgPipeline.from_pretrained(
              __a	,  safety_checker=__a	,  )
          pipe.to(__a       )
          pipe.set_progress_bar_config(disable=__a       )
          pipe.enable_attention_slicing()

          _A							:  Optional[int]    =		"""A fantasy landscape, trending on artstation"""

          _A							:  Optional[Any]    =		torch.manual_seed(0       )
          _A							:  Optional[Any]    =		pipe(
              prompt=__a	,  image=__a	,  strength=0.75	,  guidance_scale=7.5	,  generator=__a	,  output_type="""np"""	,  )
          _A							:  Tuple    =		output.images[0]

          _A							:  Any    =		image[255:258, 383:386, -1]

          assert image.shape == (504, 760, 3)
          _A							:  Tuple    =		np.array([0.9358, 0.9397, 0.9599, 0.9901, 1.0000, 1.0000, 0.9882, 1.0000, 1.0000]       )

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







@slow
@require_torch_gpu
class 		lowercase							(     unittest.TestCase	):

     def       a__			(						self       )		->							Optional[Any]:
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()



     def       a__			(						self       )		->							List[str]:
          _A							:  str    =		load_image(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
              """/img2img/sketch-mountains-input.jpg"""       )
          _A							:  List[str]    =		init_image.resize((768, 512)       )
          _A							:  int    =		load_numpy(
              """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy"""       )

          _A							:  Any    =		"""BAAI/AltDiffusion"""
          _A							:  List[Any]    =		AltDiffusionImgaImgPipeline.from_pretrained(
              __a	,  safety_checker=__a	,  )
          pipe.to(__a       )
          pipe.set_progress_bar_config(disable=__a       )
          pipe.enable_attention_slicing()

          _A							:  Any    =		"""A fantasy landscape, trending on artstation"""

          _A							:  str    =		torch.manual_seed(0       )
          _A							:  List[str]    =		pipe(
              prompt=__a	,  image=__a	,  strength=0.75	,  guidance_scale=7.5	,  generator=__a	,  output_type="""np"""	,  )
          _A							:  List[str]    =		output.images[0]

          assert image.shape == (512, 768, 3)
          # img2img is flaky across GPUs even in fp32, so using MAE here
          assert np.abs(expected_image - image       ).max() < 1e-2







import os
import re
import shutil
import sys
import tempfile
import unittest

import black


_snake_case            = os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, "utils"))

import check_copies  # noqa: E402


# This is the reference code that will be used in the tests.
# If BertLMPredictionHead is changed in modeling_bert.py, this code needs to be manually updated.
_snake_case            = "    def __init__(self, config):\n        super().__init__()\n        self.transform = BertPredictionHeadTransform(config)\n\n        # The output weights are the same as the input embeddings, but there is\n        # an output-only bias for each token.\n        self.decoder = nn.Linear(config.hidden_size, config.vocab_size, bias=False)\n\n        self.bias = nn.Parameter(torch.zeros(config.vocab_size))\n\n        # Need a link between the two variables so that the bias is correctly resized with `resize_token_embeddings`\n        self.decoder.bias = self.bias\n\n    def forward(self, hidden_states):\n        hidden_states = self.transform(hidden_states)\n        hidden_states = self.decoder(hidden_states)\n        return hidden_states\n"







class 		lowercase							(     unittest.TestCase	):

     def       a__			(						self       )		->							Union[str, Any]:
           _A							:  List[str]    =		tempfile.mkdtemp()
           os.makedirs(os.path.join(self.transformer_dir	,  """models/bert/"""       )       )
           _A							:  str    =		self.transformer_dir
           shutil.copy(
               os.path.join(_a	,  """src/transformers/models/bert/modeling_bert.py"""       )	,  os.path.join(self.transformer_dir	,  """models/bert/modeling_bert.py"""       )	,  )

     def       a__			(						self       )		->							Optional[int]:
           _A							:  List[str]    =		"""src/transformers"""
           shutil.rmtree(self.transformer_dir       )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a=None       )		->							Optional[Any]:
           _A							:  Optional[Any]    =		comment + F'''\nclass {class_name}(nn.Module):\n''' + class_code
           if overwrite_result is not None:
                 _A							:  List[str]    =		comment + F'''\nclass {class_name}(nn.Module):\n''' + overwrite_result
           _A							:  List[str]    =		black.Mode(target_versions={black.TargetVersion.PYaa}	,  line_length=119       )
           _A							:  Optional[int]    =		black.format_str(_a	,  mode=_a       )
           _A							:  Optional[Any]    =		os.path.join(self.transformer_dir	,  """new_code.py"""       )
           with open(_a	,  """w"""	,  newline="""\n"""       ) as f:
                 f.write(_a       )
           if overwrite_result is None:
                 self.assertTrue(len(check_copies.is_copy_consistent(_a       )       ) == 0       )
           else:
                 check_copies.is_copy_consistent(f.name	,  overwrite=_a       )
                 with open(_a	,  """r"""       ) as f:
                       self.assertTrue(f.read()	,  _a       )

     def       a__			(						self       )		->							str:
           _A							:  Union[str, Any]    =		check_copies.find_code_in_transformers("""models.bert.modeling_bert.BertLMPredictionHead"""       )
           self.assertEqual(_a	,  _a       )

     def       a__			(						self       )		->							int:
           # Base copy consistency
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead"""	,  """BertLMPredictionHead"""	,  REFERENCE_CODE + """\n"""	,  )

           # With no empty line at the end
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead"""	,  """BertLMPredictionHead"""	,  _a	,  )

           # Copy consistency with rename
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel"""	,  """TestModelLMPredictionHead"""	,  re.sub("""Bert"""	,  """TestModel"""	,  _a       )	,  )

           # Copy consistency with a really long name
           _A							:  List[str]    =		"""TestModelWithAReallyLongNameBecauseSomePeopleLikeThatForSomeReason"""
           self.check_copy_consistency(
               F'''# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->{long_class_name}'''	,  F'''{long_class_name}LMPredictionHead'''	,  re.sub("""Bert"""	,  _a	,  _a       )	,  )

           # Copy consistency with overwrite
           self.check_copy_consistency(
               """# Copied from transformers.models.bert.modeling_bert.BertLMPredictionHead with Bert->TestModel"""	,  """TestModelLMPredictionHead"""	,  _a	,  overwrite_result=re.sub("""Bert"""	,  """TestModel"""	,  _a       )	,  )



     def       a__			(						self       )		->							Tuple:
           _A							:  Union[str, Any]    =		check_copies.LOCALIZED_READMES["""README_zh-hans.md"""]

           _A							:  str    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the"""
               """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for"""
               """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong"""
               """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.\n1."""
               """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (from HuggingFace),"""
               """ released together with the paper [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and"""
               """ lighter](https://arxiv.org/abs/1910.01108) by Victor Sanh, Lysandre Debut and Thomas Wolf. The same"""
               """ method has been applied to compress GPT2 into"""
               """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into"""
               """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),"""
               """ Multilingual BERT into"""
               """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German"""
               """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)**"""
               """ (from Google Research/Stanford University) released with the paper [ELECTRA: Pre-training text encoders"""
               """ as discriminators rather than generators](https://arxiv.org/abs/2003.10555) by Kevin Clark, Minh-Thang"""
               """ Luong, Quoc V. Le, Christopher D. Manning."""
           )
           _A							:  str    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )
           _A							:  Any    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n1."""
               """ **[DistilBERT](https://huggingface.co/transformers/model_doc/distilbert.html)** (来自 HuggingFace) 伴随论文"""
               """ [DistilBERT, a distilled version of BERT: smaller, faster, cheaper and"""
               """ lighter](https://arxiv.org/abs/1910.01108) 由 Victor Sanh, Lysandre Debut and Thomas Wolf 发布。 The same"""
               """ method has been applied to compress GPT2 into"""
               """ [DistilGPT2](https://github.com/huggingface/transformers/tree/main/examples/distillation), RoBERTa into"""
               """ [DistilRoBERTa](https://github.com/huggingface/transformers/tree/main/examples/distillation),"""
               """ Multilingual BERT into"""
               """ [DistilmBERT](https://github.com/huggingface/transformers/tree/main/examples/distillation) and a German"""
               """ version of DistilBERT.\n1. **[ELECTRA](https://huggingface.co/transformers/model_doc/electra.html)** (来自"""
               """ Google Research/Stanford University) 伴随论文 [ELECTRA: Pre-training text encoders as discriminators rather"""
               """ than generators](https://arxiv.org/abs/2003.10555) 由 Kevin Clark, Minh-Thang Luong, Quoc V. Le,"""
               """ Christopher D. Manning 发布。\n"""
           )

           _A    ,     _A							:  Tuple    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           self.assertFalse(_a       )
           self.assertEqual(_a	,  _a       )

           _A    ,     _A							:  List[str]    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           # Check whether the number of models is equal to README.md after conversion.
           self.assertTrue(_a       )

           _A							:  Tuple    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (from Google Research and the"""
               """ Toyota Technological Institute at Chicago) released with the paper [ALBERT: A Lite BERT for"""
               """ Self-supervised Learning of Language Representations](https://arxiv.org/abs/1909.11942), by Zhenzhong"""
               """ Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut."""
           )
           _A							:  Dict    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/main/model_doc/albert.html)** (来自 Google Research and"""
               """ the Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )
           _A							:  Optional[Any]    =		(
               """1. **[ALBERT](https://huggingface.co/transformers/model_doc/albert.html)** (来自 Google Research and the"""
               """ Toyota Technological Institute at Chicago) 伴随论文 [ALBERT: A Lite BERT for Self-supervised Learning of"""
               """ Language Representations](https://arxiv.org/abs/1909.11942), 由 Zhenzhong Lan, Mingda Chen, Sebastian"""
               """ Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut 发布。\n"""
           )

           _A    ,     _A							:  Optional[int]    =		check_copies.convert_to_localized_md(
               _a	,  _a	,  localized_readme["""format_model_list"""]       )

           # Check if the model link is synchronized.
           self.assertEqual(_a	,  _a       )

