



'''simple docstring'''





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

import black


SCREAMING_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.
SCREAMING_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 				snake_case (unittest.TestCase   ):



       def 							_a      (				self      )  ->							Dict:
           lowercase__		       =						tempfile.mkdtemp()
           os.makedirs(os.path.join(self.transformer_dir     ,"models/bert/"      )      )
           lowercase__		       =						self.transformer_dir
           shutil.copy(
               os.path.join(UpperCAmelCase_     ,"src/transformers/models/bert/modeling_bert.py"      )     ,os.path.join(self.transformer_dir     ,"models/bert/modeling_bert.py"      )     ,)



       def 							_a      (				self      )  ->							str:
           lowercase__		       =						"src/transformers"
           shutil.rmtree(self.transformer_dir      )



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



       def 							_a      (				self      )  ->							Optional[int]:
           lowercase__		       =						check_copies.find_code_in_transformers("models.bert.modeling_bert.BertLMPredictionHead"      )
           self.assertEqual(UpperCAmelCase_     ,UpperCAmelCase_      )



       def 							_a      (				self      )  ->							Tuple:
           # 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"     ,UpperCAmelCase_     ,)

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

           # Copy consistency with a really long name
           lowercase__		       =						"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"     ,UpperCAmelCase_     ,UpperCAmelCase_      )     ,)

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







       def 							_a      (				self      )  ->							Any:
           lowercase__		       =						check_copies.LOCALIZED_READMES["README_zh-hans.md"]

           lowercase__		       =						(
               "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."
           )
           lowercase__		       =						(
               "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"
           )
           lowercase__		       =						(
               "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"
           )

           lowercase__       ,       lowercase__		       =						check_copies.convert_to_localized_md(
               UpperCAmelCase_     ,UpperCAmelCase_     ,localized_readme["format_model_list"]      )

           self.assertFalse(UpperCAmelCase_      )
           self.assertEqual(UpperCAmelCase_     ,UpperCAmelCase_      )

           lowercase__       ,       lowercase__		       =						check_copies.convert_to_localized_md(
               UpperCAmelCase_     ,UpperCAmelCase_     ,localized_readme["format_model_list"]      )

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

           lowercase__		       =						(
               "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."
           )
           lowercase__		       =						(
               "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"
           )
           lowercase__		       =						(
               "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"
           )

           lowercase__       ,       lowercase__		       =						check_copies.convert_to_localized_md(
               UpperCAmelCase_     ,UpperCAmelCase_     ,localized_readme["format_model_list"]      )

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









'''simple docstring'''





import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


@skip_mps
class 				snake_case (UpperCamelCase		,      unittest.TestCase   ):
       lowerCAmelCase__  :int						   =					TextToVideoSDPipeline
       lowerCAmelCase__  :Union[str, Any]						   =					TEXT_TO_IMAGE_PARAMS
       lowerCAmelCase__  :List[str]						   =					TEXT_TO_IMAGE_BATCH_PARAMS
       # No `output_type`.
       lowerCAmelCase__  :Optional[Any]						   =					frozenset(
           [
               "num_inference_steps",
               "generator",
               "latents",
               "return_dict",
               "callback",
               "callback_steps",
           ]   )



       def 							_a      (				self      )  ->							Optional[int]:
           torch.manual_seed(0      )
           lowercase__		       =						UNetaDConditionModel(
               block_out_channels=(32, 64, 64, 64)     ,layers_per_block=2     ,sample_size=32     ,in_channels=4     ,out_channels=4     ,down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D")     ,up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D")     ,cross_attention_dim=32     ,attention_head_dim=4     ,)
           lowercase__		       =						DDIMScheduler(
               beta_start=0.0_00_85     ,beta_end=0.0_12     ,beta_schedule="scaled_linear"     ,clip_sample=UpperCAmelCase_     ,set_alpha_to_one=UpperCAmelCase_     ,)
           torch.manual_seed(0      )
           lowercase__		       =						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     ,sample_size=128     ,)
           torch.manual_seed(0      )
           lowercase__		       =						CLIPTextConfig(
               bos_token_id=0     ,eos_token_id=2     ,hidden_size=32     ,intermediate_size=37     ,layer_norm_eps=1E-0_5     ,num_attention_heads=4     ,num_hidden_layers=5     ,pad_token_id=1     ,vocab_size=1_000     ,hidden_act="gelu"     ,projection_dim=512     ,)
           lowercase__		       =						CLIPTextModel(UpperCAmelCase_      )
           lowercase__		       =						CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"      )

           lowercase__		       =						{
               "unet": unet,
               "scheduler": scheduler,
               "vae": vae,
               "text_encoder": text_encoder,
               "tokenizer": tokenizer,
           }
           return components



       def 							_a      (				self     ,UpperCAmelCase_     ,UpperCAmelCase_=0      )  ->							int:
           if str(UpperCAmelCase_      ).startswith("mps"      ):
               lowercase__		       =						torch.manual_seed(UpperCAmelCase_      )
           else:
               lowercase__		       =						torch.Generator(device=UpperCAmelCase_      ).manual_seed(UpperCAmelCase_      )
           lowercase__		       =						{
               "prompt": "A painting of a squirrel eating a burger",
               "generator": generator,
               "num_inference_steps": 2,
               "guidance_scale": 6.0,
               "output_type": "pt",
           }
           return inputs



       def 							_a      (				self      )  ->							Tuple:
           lowercase__		       =						"cpu"  # ensure determinism for the device-dependent torch.Generator
           lowercase__		       =						self.get_dummy_components()
           lowercase__		       =						TextToVideoSDPipeline(**UpperCAmelCase_      )
           lowercase__		       =						sd_pipe.to(UpperCAmelCase_      )
           sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_      )

           lowercase__		       =						self.get_dummy_inputs(UpperCAmelCase_      )
           lowercase__		       =						"np"
           lowercase__		       =						sd_pipe(**UpperCAmelCase_      ).frames
           lowercase__		       =						frames[0][-3:, -3:, -1]

           assert frames[0].shape == (64, 64, 3)
           lowercase__		       =						np.array([1_58.0, 1_60.0, 1_53.0, 1_25.0, 1_00.0, 1_21.0, 1_11.0, 93.0, 1_13.0]      )

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



       def 							_a      (				self      )  ->							List[Any]:
           self._test_attention_slicing_forward_pass(test_mean_pixel_difference=UpperCAmelCase_     ,expected_max_diff=3E-3      )



       @unittest.skipIf(
           torch_device != "cuda" or not is_xformers_available()     ,reason="XFormers attention is only available with CUDA and `xformers` installed"     ,)
       def 							_a      (				self      )  ->							Dict:
           self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=UpperCAmelCase_     ,expected_max_diff=1E-2      )



       @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline."      )
       def 							_a      (				self      )  ->							Union[str, Any]:
           pass



       @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline."      )
       def 							_a      (				self      )  ->							Union[str, Any]:
           pass



       @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline."      )
       def 							_a      (				self      )  ->							Union[str, Any]:
           pass







       def 							_a      (				self      )  ->							str:
           return super().test_progress_bar()





@slow
@skip_mps
class 				snake_case (unittest.TestCase   ):



       def 							_a      (				self      )  ->							Optional[int]:
           lowercase__		       =						load_numpy(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy"      )

           lowercase__		       =						TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"      )
           lowercase__		       =						DPMSolverMultistepScheduler.from_config(pipe.scheduler.config      )
           lowercase__		       =						pipe.to("cuda"      )

           lowercase__		       =						"Spiderman is surfing"
           lowercase__		       =						torch.Generator(device="cpu"      ).manual_seed(0      )

           lowercase__		       =						pipe(UpperCAmelCase_     ,generator=UpperCAmelCase_     ,num_inference_steps=25     ,output_type="pt"      ).frames
           lowercase__		       =						video_frames.cpu().numpy()

           assert np.abs(expected_video - video      ).mean() < 5E-2







       def 							_a      (				self      )  ->							Optional[int]:
           lowercase__		       =						load_numpy(
               "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy"      )

           lowercase__		       =						TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"      )
           lowercase__		       =						pipe.to("cuda"      )

           lowercase__		       =						"Spiderman is surfing"
           lowercase__		       =						torch.Generator(device="cpu"      ).manual_seed(0      )

           lowercase__		       =						pipe(UpperCAmelCase_     ,generator=UpperCAmelCase_     ,num_inference_steps=2     ,output_type="pt"      ).frames
           lowercase__		       =						video_frames.cpu().numpy()

           assert np.abs(expected_video - video      ).mean() < 5E-2




