


'''simple docstring'''






import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
  import tensorflow as tf

if is_tensorflow_text_available():
  from transformers.models.bert import TFBertTokenizer


__A :  Any    							=				["bert-base-uncased", "bert-base-cased"]
__A :  Dict    							=				"hf-internal-testing/tiny-bert-tf-only"

if is_tf_available():






  class        __snake_case	(       tf.keras.Model):







      """simple docstring"""



      def __init__(       self   :  List[Any]   ,						lowerCamelCase   :  Dict		)       ->  Dict:
        super().__init__()
        lowerCAmelCase_      :				List[Any]      =       tokenizer
        lowerCAmelCase_      :				List[str]      =       AutoConfig.from_pretrained(_lowerCAmelCase		)
        lowerCAmelCase_      :				Optional[Any]      =       TFAutoModel.from_config(_lowerCAmelCase		)





      def __lowercase       (       self   :  List[str]   ,						lowerCamelCase   :  Optional[Any]		)       ->  Dict:
        lowerCAmelCase_      :				Dict      =       self.tokenizer(_lowerCAmelCase		)
        lowerCAmelCase_      :				Tuple      =       self.bert(**_lowerCAmelCase		)
        return out["pooler_output"]






@require_tf
@require_tensorflow_text
class        __snake_case	(       unittest.TestCase):







    """simple docstring"""



    def __lowercase       (       self   :  List[str]		)       ->  Dict:
      super().setUp()

      lowerCAmelCase_      :				Tuple      =       [
          BertTokenizer.from_pretrained(_lowerCAmelCase		) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
      ]  # repeat for when fast_bert_tokenizer=false
      lowerCAmelCase_      :				List[Any]      =       [TFBertTokenizer.from_pretrained(_lowerCAmelCase		) for checkpoint in TOKENIZER_CHECKPOINTS] + [
          TFBertTokenizer.from_pretrained(_lowerCAmelCase   ,						use_fast_bert_tokenizer=_lowerCAmelCase		)
          for checkpoint in TOKENIZER_CHECKPOINTS
      ]
      assert len(self.tokenizers		) == len(self.tf_tokenizers		)

      lowerCAmelCase_      :				Any      =       [
          """This is a straightforward English test sentence.""",
          """This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.""",
          """Now we're going to add some Chinese: 一 二 三 一二三""",
          """And some much more rare Chinese: 齉 堃 齉堃""",
          """Je vais aussi écrire en français pour tester les accents""",
          """Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ""",
      ]
      lowerCAmelCase_      :				List[str]      =       list(zip(self.test_sentences   ,						self.test_sentences[::-1]		)		)

    def __lowercase       (       self   :  str		)       ->  Optional[Any]:
      for tokenizer, tf_tokenizer in zip(self.tokenizers   ,						self.tf_tokenizers		):
        for test_inputs in (self.test_sentences, self.paired_sentences):
          lowerCAmelCase_      :				Dict      =       tokenizer(_lowerCAmelCase   ,						return_tensors="""tf"""   ,						padding="""longest"""		)
          lowerCAmelCase_      :				Optional[int]      =       tf_tokenizer(_lowerCAmelCase		)

          for key in python_outputs.keys():
            self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape		)		)
            self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key]   ,						tf.intaa		) == tf_outputs[key]		)		)

    @slow
    def __lowercase       (       self   :  Optional[int]		)       ->  Optional[Any]:
      for tf_tokenizer in self.tf_tokenizers:
        lowerCAmelCase_      :				Dict      =       tf_tokenizer(self.paired_sentences		)
        lowerCAmelCase_      :				Dict      =       tf_tokenizer(
            text=[sentence[0] for sentence in self.paired_sentences]   ,						text_pair=[sentence[1] for sentence in self.paired_sentences]   ,						)
        for key in merged_outputs.keys():
          self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key]   ,						tf.intaa		) == separated_outputs[key]		)		)

    @slow
    def __lowercase       (       self   :  Dict		)       ->  int:
      for tf_tokenizer in self.tf_tokenizers:
        lowerCAmelCase_      :				Union[str, Any]      =       tf.function(_lowerCAmelCase		)
        for test_inputs in (self.test_sentences, self.paired_sentences):
          lowerCAmelCase_      :				Optional[int]      =       tf.constant(_lowerCAmelCase		)
          lowerCAmelCase_      :				List[Any]      =       compiled_tokenizer(_lowerCAmelCase		)
          lowerCAmelCase_      :				Optional[int]      =       tf_tokenizer(_lowerCAmelCase		)

          for key in eager_outputs.keys():
            self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]		)		)





    @slow
    def __lowercase       (       self   :  Optional[Any]		)       ->  List[str]:
      for tf_tokenizer in self.tf_tokenizers:
        lowerCAmelCase_      :				int      =       ModelToSave(tokenizer=_lowerCAmelCase		)
        lowerCAmelCase_      :				Union[str, Any]      =       tf.convert_to_tensor(self.test_sentences		)
        lowerCAmelCase_      :				Tuple      =       model(_lowerCAmelCase		)  # Build model with some sample inputs
        with TemporaryDirectory() as tempdir:
          lowerCAmelCase_      :				Optional[Any]      =       Path(_lowerCAmelCase		) / """saved.model"""
          model.save(_lowerCAmelCase		)
          lowerCAmelCase_      :				List[str]      =       tf.keras.models.load_model(_lowerCAmelCase		)
        lowerCAmelCase_      :				Union[str, Any]      =       loaded_model(_lowerCAmelCase		)
        # We may see small differences because the loaded model is compiled, so we need an epsilon for the test
        self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output		)		)   ,						1E-5		)







'''simple docstring'''






from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .embeddings import GaussianFourierProjection, TimestepEmbedding, Timesteps
from .modeling_utils import ModelMixin
from .unet_ad_blocks import get_down_block, get_mid_block, get_out_block, get_up_block






@dataclass
class        __snake_case	(       _SCREAMING_SNAKE_CASE):







    """simple docstring"""



    lowercase       			= 42







class        __snake_case	(       _SCREAMING_SNAKE_CASE     ,_SCREAMING_SNAKE_CASE):







    """simple docstring"""



    @register_to_config
    def __init__(       self   :  Optional[int]   ,						lowerCamelCase   :  int = 6_55_36   ,						lowerCamelCase   :  Optional[int] = None   ,						lowerCamelCase   :  int = 2   ,						lowerCamelCase   :  int = 2   ,						lowerCamelCase   :  int = 0   ,						lowerCamelCase   :  str = "fourier"   ,						lowerCamelCase   :  bool = True   ,						lowerCamelCase   :  bool = False   ,						lowerCamelCase   :  float = 0.0   ,						lowerCamelCase   :  Tuple[str] = ("DownBlock1DNoSkip", "DownBlock1D", "AttnDownBlock1D")   ,						lowerCamelCase   :  Tuple[str] = ("AttnUpBlock1D", "UpBlock1D", "UpBlock1DNoSkip")   ,						lowerCamelCase   :  Tuple[str] = "UNetMidBlock1D"   ,						lowerCamelCase   :  str = None   ,						lowerCamelCase   :  Tuple[int] = (32, 32, 64)   ,						lowerCamelCase   :  str = None   ,						lowerCamelCase   :  int = 8   ,						lowerCamelCase   :  int = 1   ,						lowerCamelCase   :  bool = False   ,						)       ->  List[Any]:
      super().__init__()
      lowerCAmelCase_      :				Optional[Any]      =       sample_size

      # time
      if time_embedding_type == "fourier":
        lowerCAmelCase_      :				int      =       GaussianFourierProjection(
            embedding_size=8   ,						set_W_to_weight=lowerCamelCase   ,						log=lowerCamelCase   ,						flip_sin_to_cos=lowerCamelCase		)
        lowerCAmelCase_      :				List[Any]      =       2 * block_out_channels[0]
      elif time_embedding_type == "positional":
        lowerCAmelCase_      :				Optional[int]      =       Timesteps(
            block_out_channels[0]   ,						flip_sin_to_cos=lowerCamelCase   ,						downscale_freq_shift=lowerCamelCase		)
        lowerCAmelCase_      :				Union[str, Any]      =       block_out_channels[0]

      if use_timestep_embedding:
        lowerCAmelCase_      :				List[str]      =       block_out_channels[0] * 4
        lowerCAmelCase_      :				List[str]      =       TimestepEmbedding(
            in_channels=lowerCamelCase   ,						time_embed_dim=lowerCamelCase   ,						act_fn=lowerCamelCase   ,						out_dim=block_out_channels[0]   ,						)

      lowerCAmelCase_      :				str      =       nn.ModuleList([]		)
      lowerCAmelCase_      :				Tuple      =       None
      lowerCAmelCase_      :				str      =       nn.ModuleList([]		)
      lowerCAmelCase_      :				List[str]      =       None

      # down
      lowerCAmelCase_      :				List[str]      =       in_channels
      for i, down_block_type in enumerate(lowerCamelCase		):
        lowerCAmelCase_      :				Optional[Any]      =       output_channel
        lowerCAmelCase_      :				str      =       block_out_channels[i]

        if i == 0:
          input_channel += extra_in_channels

        lowerCAmelCase_      :				int      =       i == len(lowerCamelCase		) - 1

        lowerCAmelCase_      :				List[str]      =       get_down_block(
            lowerCamelCase   ,						num_layers=lowerCamelCase   ,						in_channels=lowerCamelCase   ,						out_channels=lowerCamelCase   ,						temb_channels=block_out_channels[0]   ,						add_downsample=not is_final_block or downsample_each_block   ,						)
        self.down_blocks.append(lowerCamelCase		)

      # mid
      lowerCAmelCase_      :				Dict      =       get_mid_block(
          lowerCamelCase   ,						in_channels=block_out_channels[-1]   ,						mid_channels=block_out_channels[-1]   ,						out_channels=block_out_channels[-1]   ,						embed_dim=block_out_channels[0]   ,						num_layers=lowerCamelCase   ,						add_downsample=lowerCamelCase   ,						)

      # up
      lowerCAmelCase_      :				List[str]      =       list(reversed(lowerCamelCase		)		)
      lowerCAmelCase_      :				str      =       reversed_block_out_channels[0]
      if out_block_type is None:
        lowerCAmelCase_      :				Optional[int]      =       out_channels
      else:
        lowerCAmelCase_      :				Optional[Any]      =       block_out_channels[0]

      for i, up_block_type in enumerate(lowerCamelCase		):
        lowerCAmelCase_      :				Any      =       output_channel
        lowerCAmelCase_      :				str      =       (
            reversed_block_out_channels[i + 1] if i < len(lowerCamelCase		) - 1 else final_upsample_channels
        )

        lowerCAmelCase_      :				int      =       i == len(lowerCamelCase		) - 1

        lowerCAmelCase_      :				Any      =       get_up_block(
            lowerCamelCase   ,						num_layers=lowerCamelCase   ,						in_channels=lowerCamelCase   ,						out_channels=lowerCamelCase   ,						temb_channels=block_out_channels[0]   ,						add_upsample=not is_final_block   ,						)
        self.up_blocks.append(lowerCamelCase		)
        lowerCAmelCase_      :				Optional[int]      =       output_channel

      # out
      lowerCAmelCase_      :				str      =       norm_num_groups if norm_num_groups is not None else min(block_out_channels[0] // 4   ,						32		)
      lowerCAmelCase_      :				Union[str, Any]      =       get_out_block(
          out_block_type=lowerCamelCase   ,						num_groups_out=lowerCamelCase   ,						embed_dim=block_out_channels[0]   ,						out_channels=lowerCamelCase   ,						act_fn=lowerCamelCase   ,						fc_dim=block_out_channels[-1] // 4   ,						)





    def __lowercase       (       self   :  List[Any]   ,						lowerCamelCase   :  torch.FloatTensor   ,						lowerCamelCase   :  Union[torch.Tensor, float, int]   ,						lowerCamelCase   :  bool = True   ,						)       ->  Union[UNetaDOutput, Tuple]:
      lowerCAmelCase_      :				Optional[Any]      =       timestep
      if not torch.is_tensor(lowerCamelCase		):
        lowerCAmelCase_      :				Any      =       torch.tensor([timesteps]   ,						dtype=torch.long   ,						device=sample.device		)
      elif torch.is_tensor(lowerCamelCase		) and len(timesteps.shape		) == 0:
        lowerCAmelCase_      :				Dict      =       timesteps[None].to(sample.device		)

      lowerCAmelCase_      :				int      =       self.time_proj(lowerCamelCase		)
      if self.config.use_timestep_embedding:
        lowerCAmelCase_      :				int      =       self.time_mlp(lowerCamelCase		)
      else:
        lowerCAmelCase_      :				Tuple      =       timestep_embed[..., None]
        lowerCAmelCase_      :				Dict      =       timestep_embed.repeat([1, 1, sample.shape[2]]		).to(sample.dtype		)
        lowerCAmelCase_      :				Union[str, Any]      =       timestep_embed.broadcast_to((sample.shape[:1] + timestep_embed.shape[1:])		)

      # 2. down
      lowerCAmelCase_      :				Optional[Any]      =       ()
      for downsample_block in self.down_blocks:
        lowerCAmelCase_,							lowerCAmelCase_      :				Any      =       downsample_block(hidden_states=lowerCamelCase   ,						temb=lowerCamelCase		)
        down_block_res_samples += res_samples

      # 3. mid
      if self.mid_block:
        lowerCAmelCase_      :				int      =       self.mid_block(lowerCamelCase   ,						lowerCamelCase		)

      # 4. up
      for i, upsample_block in enumerate(self.up_blocks		):
        lowerCAmelCase_      :				str      =       down_block_res_samples[-1:]
        lowerCAmelCase_      :				Union[str, Any]      =       down_block_res_samples[:-1]
        lowerCAmelCase_      :				List[str]      =       upsample_block(lowerCamelCase   ,						res_hidden_states_tuple=lowerCamelCase   ,						temb=lowerCamelCase		)

      # 5. post-process
      if self.out_block:
        lowerCAmelCase_      :				Union[str, Any]      =       self.out_block(lowerCamelCase   ,						lowerCamelCase		)

      if not return_dict:
        return (sample,)

      return UNetaDOutput(sample=lowerCamelCase		)



