

"""simple docstring"""






import gc
import unittest

import numpy as np
import torch

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
    CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class 	__UpperCamelCase    (    a__							,							unittest.TestCase						):
  _UpperCAmelCase          =		DiTPipeline
  _UpperCAmelCase          =		CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS
  _UpperCAmelCase          =		PipelineTesterMixin.required_optional_params - {
      "latents",
      "num_images_per_prompt",
      "callback",
      "callback_steps",
  }
  _UpperCAmelCase          =		CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS
  _UpperCAmelCase          =		False

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      torch.manual_seed(0      )
      _lowerCAmelCase       :   List[str]  =    TransformeraDModel(
          sample_size=16    ,num_layers=2    ,patch_size=4    ,attention_head_dim=8    ,num_attention_heads=2    ,in_channels=4    ,out_channels=8    ,attention_bias=_A    ,activation_fn='gelu-approximate'    ,num_embeds_ada_norm=1000    ,norm_type='ada_norm_zero'    ,norm_elementwise_affine=_A    ,)
      _lowerCAmelCase       :   Union[str, Any]  =    AutoencoderKL()
      _lowerCAmelCase       :   Union[str, Any]  =    DDIMScheduler()
      _lowerCAmelCase       :   Tuple  =    {'transformer': transformer.eval(), 'vae': vae.eval(), 'scheduler': scheduler}
      return components

  def __lowerCamelCase		(		self    ,_A    ,_A=0      ):
      '''simple docstring'''

      if str(_A      ).startswith('mps'      ):
          _lowerCAmelCase       :   Dict  =    torch.manual_seed(_A      )
      else:
          _lowerCAmelCase       :   int  =    torch.Generator(device=_A      ).manual_seed(_A      )
      _lowerCAmelCase       :   Tuple  =    {
          'class_labels': [1],
          'generator': generator,
          'num_inference_steps': 2,
          'output_type': 'numpy',
      }
      return inputs

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   List[str]  =    'cpu'

      _lowerCAmelCase       :   Optional[int]  =    self.get_dummy_components()
      _lowerCAmelCase       :   Optional[Any]  =    self.pipeline_class(**_A      )
      pipe.to(_A      )
      pipe.set_progress_bar_config(disable=_A      )

      _lowerCAmelCase       :   Tuple  =    self.get_dummy_inputs(_A      )
      _lowerCAmelCase       :   Dict  =    pipe(**_A      ).images
      _lowerCAmelCase       :   Optional[int]  =    image[0, -3:, -3:, -1]

      self.assertEqual(image.shape    ,(1, 16, 16, 3)      )
      _lowerCAmelCase       :   List[str]  =    np.array([0.2_9_4_6, 0.6_6_0_1, 0.4_3_2_9, 0.3_2_9_6, 0.4_1_4_4, 0.5_3_1_9, 0.7_2_7_3, 0.5_0_1_3, 0.4_4_5_7]      )
      _lowerCAmelCase       :   int  =    np.abs(image_slice.flatten() - expected_slice      ).max()
      self.assertLessEqual(_A    ,1E-3      )

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      self._test_inference_batch_single_identical(relax_max_difference=_A    ,expected_max_diff=1E-3      )







  @unittest.skipIf(
      torch_device != 'cuda' or not is_xformers_available()    ,reason='XFormers attention is only available with CUDA and `xformers` installed'    ,)
  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3      )







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

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      super().tearDown()
      gc.collect()
      torch.cuda.empty_cache()

  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Tuple  =    torch.manual_seed(0      )

      _lowerCAmelCase       :   Any  =    DiTPipeline.from_pretrained('facebook/DiT-XL-2-256'      )
      pipe.to('cuda'      )

      _lowerCAmelCase       :   List[Any]  =    ['vase', 'umbrella', 'white shark', 'white wolf']
      _lowerCAmelCase       :   str  =    pipe.get_label_ids(_A      )

      _lowerCAmelCase       :   Dict  =    pipe(_A    ,generator=_A    ,num_inference_steps=40    ,output_type='np'      ).images

      for word, image in zip(_A    ,_A      ):
          _lowerCAmelCase       :   Any  =    load_numpy(
              F"""https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy"""      )
          assert np.abs((expected_image - image).max()      ) < 1E-2







  def __lowerCamelCase		(		self      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Optional[int]  =    DiTPipeline.from_pretrained('facebook/DiT-XL-2-512'      )
      _lowerCAmelCase       :   List[Any]  =    DPMSolverMultistepScheduler.from_config(pipe.scheduler.config      )
      pipe.to('cuda'      )

      _lowerCAmelCase       :   str  =    ['vase', 'umbrella']
      _lowerCAmelCase       :   Any  =    pipe.get_label_ids(_A      )

      _lowerCAmelCase       :   Optional[Any]  =    torch.manual_seed(0      )
      _lowerCAmelCase       :   List[str]  =    pipe(_A    ,generator=_A    ,num_inference_steps=25    ,output_type='np'      ).images

      for word, image in zip(_A    ,_A      ):
          _lowerCAmelCase       :   int  =    load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              F"""/dit/{word}_512.npy"""      )

          assert np.abs((expected_image - image).max()      ) < 1E-1








"""simple docstring"""






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


_lowerCAmelCase						=		logging.get_logger(__name__)

_lowerCAmelCase						=		{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

_lowerCAmelCase						=		{
    """vocab_file""": {
        """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt""",
        """bert-large-uncased""": """https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt""",
        """bert-base-cased""": """https://huggingface.co/bert-base-cased/resolve/main/vocab.txt""",
        """bert-large-cased""": """https://huggingface.co/bert-large-cased/resolve/main/vocab.txt""",
        """bert-base-multilingual-uncased""": (
            """https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt"""
        ),
        """bert-base-multilingual-cased""": """https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt""",
        """bert-base-chinese""": """https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt""",
        """bert-base-german-cased""": """https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt""",
        """bert-large-uncased-whole-word-masking""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt"""
        ),
        """bert-large-cased-whole-word-masking""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt"""
        ),
        """bert-large-uncased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt"""
        ),
        """bert-large-cased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt"""
        ),
        """bert-base-cased-finetuned-mrpc""": (
            """https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt"""
        ),
        """bert-base-german-dbmdz-cased""": """https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt""",
        """bert-base-german-dbmdz-uncased""": (
            """https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt"""
        ),
        """TurkuNLP/bert-base-finnish-cased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt"""
        ),
        """TurkuNLP/bert-base-finnish-uncased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt"""
        ),
        """wietsedv/bert-base-dutch-cased""": (
            """https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """bert-base-uncased""": """https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json""",
        """bert-large-uncased""": """https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json""",
        """bert-base-cased""": """https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json""",
        """bert-large-cased""": """https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json""",
        """bert-base-multilingual-uncased""": (
            """https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json"""
        ),
        """bert-base-multilingual-cased""": (
            """https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json"""
        ),
        """bert-base-chinese""": """https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json""",
        """bert-base-german-cased""": """https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json""",
        """bert-large-uncased-whole-word-masking""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json"""
        ),
        """bert-large-cased-whole-word-masking""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json"""
        ),
        """bert-large-uncased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json"""
        ),
        """bert-large-cased-whole-word-masking-finetuned-squad""": (
            """https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json"""
        ),
        """bert-base-cased-finetuned-mrpc""": (
            """https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json"""
        ),
        """bert-base-german-dbmdz-cased""": (
            """https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json"""
        ),
        """bert-base-german-dbmdz-uncased""": (
            """https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json"""
        ),
        """TurkuNLP/bert-base-finnish-cased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json"""
        ),
        """TurkuNLP/bert-base-finnish-uncased-v1""": (
            """https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json"""
        ),
        """wietsedv/bert-base-dutch-cased""": (
            """https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json"""
        ),
    },
}

_lowerCAmelCase						=		{
    """bert-base-uncased""": 5_1_2,
    """bert-large-uncased""": 5_1_2,
    """bert-base-cased""": 5_1_2,
    """bert-large-cased""": 5_1_2,
    """bert-base-multilingual-uncased""": 5_1_2,
    """bert-base-multilingual-cased""": 5_1_2,
    """bert-base-chinese""": 5_1_2,
    """bert-base-german-cased""": 5_1_2,
    """bert-large-uncased-whole-word-masking""": 5_1_2,
    """bert-large-cased-whole-word-masking""": 5_1_2,
    """bert-large-uncased-whole-word-masking-finetuned-squad""": 5_1_2,
    """bert-large-cased-whole-word-masking-finetuned-squad""": 5_1_2,
    """bert-base-cased-finetuned-mrpc""": 5_1_2,
    """bert-base-german-dbmdz-cased""": 5_1_2,
    """bert-base-german-dbmdz-uncased""": 5_1_2,
    """TurkuNLP/bert-base-finnish-cased-v1""": 5_1_2,
    """TurkuNLP/bert-base-finnish-uncased-v1""": 5_1_2,
    """wietsedv/bert-base-dutch-cased""": 5_1_2,
}

_lowerCAmelCase						=		{
    """bert-base-uncased""": {"""do_lower_case""": True},
    """bert-large-uncased""": {"""do_lower_case""": True},
    """bert-base-cased""": {"""do_lower_case""": False},
    """bert-large-cased""": {"""do_lower_case""": False},
    """bert-base-multilingual-uncased""": {"""do_lower_case""": True},
    """bert-base-multilingual-cased""": {"""do_lower_case""": False},
    """bert-base-chinese""": {"""do_lower_case""": False},
    """bert-base-german-cased""": {"""do_lower_case""": False},
    """bert-large-uncased-whole-word-masking""": {"""do_lower_case""": True},
    """bert-large-cased-whole-word-masking""": {"""do_lower_case""": False},
    """bert-large-uncased-whole-word-masking-finetuned-squad""": {"""do_lower_case""": True},
    """bert-large-cased-whole-word-masking-finetuned-squad""": {"""do_lower_case""": False},
    """bert-base-cased-finetuned-mrpc""": {"""do_lower_case""": False},
    """bert-base-german-dbmdz-cased""": {"""do_lower_case""": False},
    """bert-base-german-dbmdz-uncased""": {"""do_lower_case""": True},
    """TurkuNLP/bert-base-finnish-cased-v1""": {"""do_lower_case""": False},
    """TurkuNLP/bert-base-finnish-uncased-v1""": {"""do_lower_case""": True},
    """wietsedv/bert-base-dutch-cased""": {"""do_lower_case""": False},
}







class 	__UpperCamelCase    (    a__						):
  _UpperCAmelCase          =		VOCAB_FILES_NAMES
  _UpperCAmelCase          =		PRETRAINED_VOCAB_FILES_MAP
  _UpperCAmelCase          =		PRETRAINED_INIT_CONFIGURATION
  _UpperCAmelCase          =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
  _UpperCAmelCase          =		BertTokenizer

  def __init__(		self    ,_A=None    ,_A=None    ,_A=True    ,_A="[UNK]"    ,_A="[SEP]"    ,_A="[PAD]"    ,_A="[CLS]"    ,_A="[MASK]"    ,_A=True    ,_A=None    ,**_A    ,):
      '''simple docstring'''

      super().__init__(
          _A    ,tokenizer_file=_A    ,do_lower_case=_A    ,unk_token=_A    ,sep_token=_A    ,pad_token=_A    ,cls_token=_A    ,mask_token=_A    ,tokenize_chinese_chars=_A    ,strip_accents=_A    ,**_A    ,)

      _lowerCAmelCase       :   Tuple  =    json.loads(self.backend_tokenizer.normalizer.__getstate__()      )
      if (
          normalizer_state.get('lowercase'    ,_A      ) != do_lower_case
          or normalizer_state.get('strip_accents'    ,_A      ) != strip_accents
          or normalizer_state.get('handle_chinese_chars'    ,_A      ) != tokenize_chinese_chars
      ):
          _lowerCAmelCase       :   Dict  =    getattr(_A    ,normalizer_state.pop('type'      )      )
          _lowerCAmelCase       :   Dict  =    do_lower_case
          _lowerCAmelCase       :   Optional[int]  =    strip_accents
          _lowerCAmelCase       :   Union[str, Any]  =    tokenize_chinese_chars
          _lowerCAmelCase       :   Dict  =    normalizer_class(**_A      )

      _lowerCAmelCase       :   Union[str, Any]  =    do_lower_case

  def __lowerCamelCase		(		self    ,_A    ,_A=None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Tuple  =    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

      if token_ids_a:
          output += token_ids_a + [self.sep_token_id]

      return output

  def __lowerCamelCase		(		self    ,_A    ,_A = None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   Union[str, Any]  =    [self.sep_token_id]
      _lowerCAmelCase       :   str  =    [self.cls_token_id]
      if token_ids_a is None:
          return len(cls + token_ids_a + sep      ) * [0]
      return len(cls + token_ids_a + sep      ) * [0] + len(token_ids_a + sep      ) * [1]







  def __lowerCamelCase		(		self    ,_A    ,_A = None      ):
      '''simple docstring'''

      _lowerCAmelCase       :   str  =    self._tokenizer.model.save(_A    ,name=_A      )
      return tuple(_A      )





