code: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import os
  from typing import List, Union
  
  import tensorflow as tf
  from tensorflow_text import BertTokenizer as BertTokenizerLayer
  from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs
  
  from .tokenization_bert import BertTokenizer
  
  
  
  class 							__UpperCAmelCase  (       tf.keras.layers.Layer						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def __init__(self						:      Optional[Any]      ,      _lowerCAmelCase						:      List      ,      _lowerCAmelCase						:      bool      ,      _lowerCAmelCase						:      int = None      ,      _lowerCAmelCase						:      int = None      ,      _lowerCAmelCase						:      int = None      ,      _lowerCAmelCase						:      str = "longest"      ,      _lowerCAmelCase						:      bool = True      ,      _lowerCAmelCase						:      int = 512      ,      _lowerCAmelCase						:      int = None      ,      _lowerCAmelCase						:      bool = True      ,      _lowerCAmelCase						:      bool = True      ,      _lowerCAmelCase						:      bool = True      ,      **_lowerCAmelCase						:      Optional[int]      ,      ):
  			super().__init__()
  			if use_fast_bert_tokenizer:
  				A							  =      FastBertTokenizer(
  				    _lowerCAmelCase      ,      token_out_type=tf.intaa      ,      lower_case_nfd_strip_accents=_lowerCAmelCase      ,      **_lowerCAmelCase      )
  			else:
  				A							  =      tf.lookup.StaticVocabularyTable(
  				    tf.lookup.KeyValueTensorInitializer(
  				        keys=_lowerCAmelCase      ,      key_dtype=tf.string      ,      values=tf.range(tf.size(_lowerCAmelCase      ,      out_type=tf.intaa      )      ,      dtype=tf.intaa      )      ,      value_dtype=tf.intaa      ,      )      ,      num_oov_buckets=1      ,      )
  				A							  =      BertTokenizerLayer(
  				    _lowerCAmelCase      ,      token_out_type=tf.intaa      ,      lower_case=_lowerCAmelCase      ,      **_lowerCAmelCase      )
  
  			A							  =      vocab_list
  			A							  =      do_lower_case
  			A							  =      cls_token_id or vocab_list.index("""[CLS]"""      )
  			A							  =      sep_token_id or vocab_list.index("""[SEP]"""      )
  			A							  =      pad_token_id or vocab_list.index("""[PAD]"""      )
  			A							  =      ShrinkLongestTrimmer(max_length - 3      ,      axis=1      )  # Allow room for special tokens
  			A							  =      max_length
  			A							  =      padding
  			A							  =      truncation
  			A							  =      pad_to_multiple_of
  			A							  =      return_token_type_ids
  			A							  =      return_attention_mask
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (cls						:      int      ,      _lowerCAmelCase						:      "PreTrainedTokenizerBase"      ,      **_lowerCAmelCase						:      List[str]      ):  # noqa: F821
  			A							  =      kwargs.pop("""do_lower_case"""      ,      _lowerCAmelCase      )
  			A							  =      tokenizer.do_lower_case if do_lower_case is None else do_lower_case
  			A							  =      kwargs.pop("""cls_token_id"""      ,      _lowerCAmelCase      )
  			A							  =      tokenizer.cls_token_id if cls_token_id is None else cls_token_id
  			A							  =      kwargs.pop("""sep_token_id"""      ,      _lowerCAmelCase      )
  			A							  =      tokenizer.sep_token_id if sep_token_id is None else sep_token_id
  			A							  =      kwargs.pop("""pad_token_id"""      ,      _lowerCAmelCase      )
  			A							  =      tokenizer.pad_token_id if pad_token_id is None else pad_token_id
  
  			A							  =      tokenizer.get_vocab()
  			A							  =      sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]      ,      key=lambda _lowerCAmelCase      : x[1]      )
  			A							  =      [entry[0] for entry in vocab]
  			return cls(
  			    vocab_list=_lowerCAmelCase      ,      do_lower_case=_lowerCAmelCase      ,      cls_token_id=_lowerCAmelCase      ,      sep_token_id=_lowerCAmelCase      ,      pad_token_id=_lowerCAmelCase      ,      **_lowerCAmelCase      ,      )
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (cls						:      Dict      ,      _lowerCAmelCase						:      Union[str, os.PathLike]      ,      *_lowerCAmelCase						:      Optional[Any]      ,      **_lowerCAmelCase						:      Optional[Any]      ):
  			try:
  				A							  =      BertTokenizer.from_pretrained(_lowerCAmelCase      ,      *_lowerCAmelCase      ,      **_lowerCAmelCase      )
  			except:  # noqa: E722
  				from .tokenization_bert_fast import BertTokenizerFast
  
  				A							  =      BertTokenizerFast.from_pretrained(_lowerCAmelCase      ,      *_lowerCAmelCase      ,      **_lowerCAmelCase      )
  			return cls.from_tokenizer(_lowerCAmelCase      ,      **_lowerCAmelCase      )
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ,      _lowerCAmelCase						:      Dict      ):
  			if self.do_lower_case:
  				A							  =      case_fold_utfa(_lowerCAmelCase      )
  			A							  =      self.tf_tokenizer.tokenize(_lowerCAmelCase      )
  			return tokens.merge_dims(1      ,      -1      )
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Union[str, Any]      ,      _lowerCAmelCase						:      Optional[int]=None      ,      _lowerCAmelCase						:      Dict=None      ,      _lowerCAmelCase						:      Dict=None      ,      _lowerCAmelCase						:      str=None      ,      _lowerCAmelCase						:      List[str]=None      ,      _lowerCAmelCase						:      Union[str, Any]=None      ,      _lowerCAmelCase						:      List[str]=None      ,      ):
  			if padding is None:
  				A							  =      self.padding
  			if padding not in ("longest", "max_length"):
  				raise ValueError("""Padding must be either 'longest' or 'max_length'!"""      )
  			if max_length is not None and text_pair is not None:
  				# Because we have to instantiate a Trimmer to do it properly
  				raise ValueError("""max_length cannot be overridden at call time when truncating paired texts!"""      )
  			if max_length is None:
  				A							  =      self.max_length
  			if truncation is None:
  				A							  =      self.truncation
  			if pad_to_multiple_of is None:
  				A							  =      self.pad_to_multiple_of
  			if return_token_type_ids is None:
  				A							  =      self.return_token_type_ids
  			if return_attention_mask is None:
  				A							  =      self.return_attention_mask
  			if not isinstance(_lowerCAmelCase      ,      tf.Tensor      ):
  				A							  =      tf.convert_to_tensor(_lowerCAmelCase      )
  			if text_pair is not None and not isinstance(_lowerCAmelCase      ,      tf.Tensor      ):
  				A							  =      tf.convert_to_tensor(_lowerCAmelCase      )
  			if text_pair is not None:
  				if text.shape.rank > 1:
  					raise ValueError("""text argument should not be multidimensional when a text pair is supplied!"""      )
  				if text_pair.shape.rank > 1:
  					raise ValueError("""text_pair should not be multidimensional!"""      )
  			if text.shape.rank == 2:
  				A  ,    A							  =      text[:, 0], text[:, 1]
  			A							  =      self.unpaired_tokenize(_lowerCAmelCase      )
  			if text_pair is None:  # Unpaired text
  				if truncation:
  					A							  =      text[:, : max_length - 2]  # Allow room for special tokens
  				A  ,    A							  =      combine_segments(
  				    (text,)      ,      start_of_sequence_id=self.cls_token_id      ,      end_of_segment_id=self.sep_token_id      )
  			else:  # Paired text
  				A							  =      self.unpaired_tokenize(_lowerCAmelCase      )
  				if truncation:
  					A  ,    A							  =      self.paired_trimmer.trim([text, text_pair]      )
  				A  ,    A							  =      combine_segments(
  				    (text, text_pair)      ,      start_of_sequence_id=self.cls_token_id      ,      end_of_segment_id=self.sep_token_id      )
  			if padding == "longest":
  				A							  =      input_ids.bounding_shape(axis=1      )
  				if pad_to_multiple_of is not None:
  					# No ceiling division in tensorflow, so we negate floordiv instead
  					A							  =      pad_to_multiple_of * (-tf.math.floordiv(-pad_length      ,      _lowerCAmelCase      ))
  			else:
  				A							  =      max_length
  
  			A  ,    A							  =      pad_model_inputs(_lowerCAmelCase      ,      max_seq_length=_lowerCAmelCase      ,      pad_value=self.pad_token_id      )
  			A							  =      {"""input_ids""": input_ids}
  			if return_attention_mask:
  				A							  =      attention_mask
  			if return_token_type_ids:
  				A  ,    A							  =      pad_model_inputs(
  				    _lowerCAmelCase      ,      max_seq_length=_lowerCAmelCase      ,      pad_value=self.pad_token_id      )
  				A							  =      token_type_ids
  			return output
  
  
  
  
  
  
  
  		def        A     (self						:      List[Any]      ):
  			return {
  			    "vocab_list": self.vocab_list,
  			    "do_lower_case": self.do_lower_case,
  			    "cls_token_id": self.cls_token_id,
  			    "sep_token_id": self.sep_token_id,
  			    "pad_token_id": self.pad_token_id,
  			}
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import tempfile
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import DiffusionPipeline
  from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
  			A							  =      10
  
  			if only_cross_attention:
  				A							  =      12
  			else:
  				# when only cross attention is not set, the cross attention dim must be the same as the query dim
  				A							  =      query_dim
  
  			return {
  			    "query_dim": query_dim,
  			    "cross_attention_dim": cross_attention_dim,
  			    "heads": 2,
  			    "dim_head": 4,
  			    "added_kv_proj_dim": 6,
  			    "norm_num_groups": 1,
  			    "only_cross_attention": only_cross_attention,
  			    "processor": AttnAddedKVProcessor(),
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
  			A							  =      2
  
  			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
  			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
  			A							  =      None
  
  			return {
  			    "hidden_states": hidden_states,
  			    "encoder_hidden_states": encoder_hidden_states,
  			    "attention_mask": attention_mask,
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ):
  			# self and cross attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is not None      )
  			self.assertTrue(attn.to_v is not None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			# only self attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is None      )
  			self.assertTrue(attn.to_v is None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ):
  			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			# the initial conversion succeeds
  			A							  =      DiffusionPipeline.from_pretrained(
  			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			with tempfile.TemporaryDirectory() as tmpdir:
  				# save the converted model
  				pipe.save_pretrained(_lowerCAmelCase      )
  
  				# can also load the converted weights
  				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  
  