





import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin
class      UpperCamelCase_						(					_lowerCamelCase							,					unittest.TestCase   ):
     lowerCAmelCase_							=		DebertaTokenizer
     lowerCAmelCase_							=		True
     lowerCAmelCase_							=		DebertaTokenizerFast




     def 		lowerCAmelCase     (						self					)						->							Dict:
          super().setUp()

          # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
          _snake_case													=			[
              'l',
              'o',
              'w',
              'e',
              'r',
              's',
              't',
              'i',
              'd',
              'n',
              '\u0120',
              '\u0120l',
              '\u0120n',
              '\u0120lo',
              '\u0120low',
              'er',
              '\u0120lowest',
              '\u0120newer',
              '\u0120wider',
              '[UNK]',
          ]
          _snake_case													=			dict(zip(lowerCAmelCase_			,  range(len(lowerCAmelCase_					)					)					)					)
          _snake_case													=			['#version: 0.2', '\u0120 l', '\u0120l o', '\u0120lo w', 'e r', '']
          _snake_case													=			{'unk_token': '[UNK]'}

          _snake_case													=			os.path.join(self.tmpdirname			,  VOCAB_FILES_NAMES['vocab_file']					)
          _snake_case													=			os.path.join(self.tmpdirname			,  VOCAB_FILES_NAMES['merges_file']					)
          with open(self.vocab_file			,  'w'			,  encoding='utf-8'					) as fp:
               fp.write(json.dumps(lowerCAmelCase_					) + '\n'					)
          with open(self.merges_file			,  'w'			,  encoding='utf-8'					) as fp:
               fp.write('\n'.join(lowerCAmelCase_					)					)




     def 		lowerCAmelCase     (						self			,  **lowerCAmelCase_					)						->							Any:
          kwargs.update(self.special_tokens_map					)
          return self.tokenizer_class.from_pretrained(self.tmpdirname			,  **lowerCAmelCase_					)




     def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Union[str, Any]:
          _snake_case													=			'lower newer'
          _snake_case													=			'lower newer'
          return input_text, output_text




     def 		lowerCAmelCase     (						self					)						->							Tuple:
          _snake_case													=			self.get_tokenizer()
          _snake_case													=			'lower newer'
          _snake_case													=			['l', 'o', 'w', 'er', '\u0120', 'n', 'e', 'w', 'er']
          _snake_case													=			tokenizer.tokenize(lowerCAmelCase_					)
          self.assertListEqual(lowerCAmelCase_			,  lowerCAmelCase_					)

          _snake_case													=			tokens + [tokenizer.unk_token]
          _snake_case													=			[0, 1, 2, 15, 10, 9, 3, 2, 15, 19]
          self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase_					)			,  lowerCAmelCase_					)




     def 		lowerCAmelCase     (						self					)						->							Optional[Any]:
          _snake_case													=			self.get_tokenizer()
          _snake_case													=			tokenizer('Hello'			,  'World'					)
          _snake_case													=			[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
          self.assertListEqual(tokd['token_type_ids']			,  lowerCAmelCase_					)




     @slow
     def 		lowerCAmelCase     (						self					)						->							Any:
          _snake_case													=			self.tokenizer_class.from_pretrained('microsoft/deberta-base'					)

          _snake_case													=			tokenizer.encode('sequence builders'			,  add_special_tokens=lowerCAmelCase_					)
          _snake_case													=			tokenizer.encode('multi-sequence build'			,  add_special_tokens=lowerCAmelCase_					)

          _snake_case													=			tokenizer.encode(
              'sequence builders'			,  add_special_tokens=lowerCAmelCase_			,  add_prefix_space=lowerCAmelCase_					)
          _snake_case													=			tokenizer.encode(
              'sequence builders'			,  'multi-sequence build'			,  add_special_tokens=lowerCAmelCase_			,  add_prefix_space=lowerCAmelCase_					)

          _snake_case													=			tokenizer.build_inputs_with_special_tokens(lowerCAmelCase_					)
          _snake_case													=			tokenizer.build_inputs_with_special_tokens(lowerCAmelCase_			,  lowerCAmelCase_					)

          assert encoded_sentence == encoded_text_from_decode
          assert encoded_pair == encoded_pair_from_decode






     @slow
     def 		lowerCAmelCase     (						self					)						->							Optional[Any]:
          _snake_case													=			[self.tokenizer_class]
          if self.test_rust_tokenizer:
               tokenizer_classes.append(self.rust_tokenizer_class					)

          for tokenizer_class in tokenizer_classes:
               _snake_case													=			tokenizer_class.from_pretrained('microsoft/deberta-base'					)

               _snake_case													=			[
                   'ALBERT: A Lite BERT for Self-supervised Learning of Language Representations',
                   'ALBERT incorporates two parameter reduction techniques',
                   'The first one is a factorized embedding parameterization. By decomposing the large vocabulary'
                   ' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'
                   ' vocabulary embedding.',
               ]

               _snake_case													=			tokenizer(lowerCAmelCase_			,  padding=lowerCAmelCase_					)
               _snake_case													=			[tokenizer.decode(lowerCAmelCase_			,  skip_special_tokens=lowerCAmelCase_					) for seq in encoding['input_ids']]

               # fmt: off
               _snake_case													=			{
                   'input_ids': [
                       [1, 2118, 1_1126, 565, 35, 83, 2_5191, 163, 1_8854, 13, 1_2156, 12, 1_6101, 2_5376, 1_3807, 9, 2_2205, 2_7893, 1635, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 2118, 1_1126, 565, 2_4536, 80, 4_3797, 4878, 7373, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 133, 78, 65, 16, 10, 3724, 1538, 3_3183, 1_1303, 4_3797, 1938, 4, 870, 2_4165, 2_9105, 5, 739, 3_2644, 3_3183, 1_1303, 3_6173, 88, 80, 650, 7821, 4_5940, 6, 52, 2559, 5, 1836, 9, 5, 7397, 1_3171, 31, 5, 1836, 9, 3_2644, 3_3183, 1_1303, 4, 2]
                   ],
                   'token_type_ids': [
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
                   ],
                   'attention_mask': [
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
                   ]
               }
               # fmt: on

               _snake_case													=			[
                   'ALBERT: A Lite BERT for Self-supervised Learning of Language Representations',
                   'ALBERT incorporates two parameter reduction techniques',
                   'The first one is a factorized embedding parameterization. By decomposing the large vocabulary'
                   ' embedding matrix into two small matrices, we separate the size of the hidden layers from the size of'
                   ' vocabulary embedding.',
               ]

               self.assertDictEqual(encoding.data			,  lowerCAmelCase_					)

               for expected, decoded in zip(lowerCAmelCase_			,  lowerCAmelCase_					):
                    self.assertEqual(lowerCAmelCase_			,  lowerCAmelCase_					)










import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch
@require_torch
class      UpperCamelCase_						:




					@property
					def 		lowerCAmelCase     (						self					)						->							int:
										return self.get_dummy_input()




					@property
					def 		lowerCAmelCase     (						self					)						->							Optional[Any]:
										if self.block_type == "down":
															return (4, 32, 16, 16)
										elif self.block_type == "mid":
															return (4, 32, 32, 32)
										elif self.block_type == "up":
															return (4, 32, 64, 64)

										raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''					)




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_=True			,  lowerCAmelCase_=False			,  lowerCAmelCase_=False			,  lowerCAmelCase_=False			,  )						->							List[str]:
										_snake_case													=			4
										_snake_case													=			32
										_snake_case													=			(32, 32)

										_snake_case													=			torch.manual_seed(0					)
										_snake_case													=			torch.device(lowerCAmelCase_					)
										_snake_case													=			(batch_size, num_channels) + sizes
										_snake_case													=			randn_tensor(lowerCAmelCase_			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					)
										_snake_case													=			{'hidden_states': hidden_states}

										if include_temb:
															_snake_case													=			128
															_snake_case													=			randn_tensor((batch_size, temb_channels)			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					)

										if include_res_hidden_states_tuple:
															_snake_case													=			torch.manual_seed(1					)
															_snake_case													=			(randn_tensor(lowerCAmelCase_			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					),)

										if include_encoder_hidden_states:
															_snake_case													=			floats_tensor((batch_size, 32, 32)					).to(lowerCAmelCase_					)

										if include_skip_sample:
															_snake_case													=			randn_tensor(((batch_size, 3) + sizes)			,  generator=lowerCAmelCase_			,  device=lowerCAmelCase_					)

										return dummy_input




					def 		lowerCAmelCase     (						self					)						->							Tuple:
										_snake_case													=			{
										    'in_channels': 32,
										    'out_channels': 32,
										    'temb_channels': 128,
										}
										if self.block_type == "up":
															_snake_case													=			32

										if self.block_type == "mid":
															init_dict.pop('out_channels'					)

										_snake_case													=			self.dummy_input
										return init_dict, inputs_dict




					def 		lowerCAmelCase     (						self			,  lowerCAmelCase_					)						->							Optional[int]:
										_snake_case							,       _snake_case													=			self.prepare_init_args_and_inputs_for_common()
										_snake_case													=			self.block_class(**lowerCAmelCase_					)
										unet_block.to(lowerCAmelCase_					)
										unet_block.eval()

										with torch.no_grad():
															_snake_case													=			unet_block(**lowerCAmelCase_					)

										if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					):
															_snake_case													=			output[0]

										self.assertEqual(output.shape			,  self.output_shape					)

										_snake_case													=			output[0, -1, -3:, -3:]
										_snake_case													=			torch.tensor(lowerCAmelCase_					).to(lowerCAmelCase_					)
										assert torch_all_close(output_slice.flatten()			,  lowerCAmelCase_			,  atol=5E-3					)






					@unittest.skipIf(torch_device == 'mps'			,  'Training is not supported in mps'					)
					def 		lowerCAmelCase     (						self					)						->							Tuple:
										_snake_case							,       _snake_case													=			self.prepare_init_args_and_inputs_for_common()
										_snake_case													=			self.block_class(**lowerCAmelCase_					)
										model.to(lowerCAmelCase_					)
										model.train()
										_snake_case													=			model(**lowerCAmelCase_					)

										if isinstance(lowerCAmelCase_			,  lowerCAmelCase_					):
															_snake_case													=			output[0]

										_snake_case													=			torch.device(lowerCAmelCase_					)
										_snake_case													=			randn_tensor(output.shape			,  device=lowerCAmelCase_					)
										_snake_case													=			torch.nn.functional.mse_loss(lowerCAmelCase_			,  lowerCAmelCase_					)
										loss.backward()



