



'''simple docstring'''

import os
import unittest

from transformers import MobileBertTokenizer, MobileBertTokenizerFast
from transformers.models.bert.tokenization_bert import (
    VOCAB_FILES_NAMES,
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin, filter_non_english

@require_tokenizers
class       __magic_name__		(       _UpperCamelCase     ,		unittest.TestCase  ):
							lowerCAmelCase			:       Optional[Any]  							=   MobileBertTokenizer
							lowerCAmelCase			:       Optional[Any]  							=   MobileBertTokenizerFast
							lowerCAmelCase			:       Optional[int]  							=   True
							lowerCAmelCase			:       Any  							=   True
							lowerCAmelCase			:       Dict  							=   filter_non_english
							lowerCAmelCase			:       Optional[int]  							=   'google/mobilebert-uncased'







							def 				__lowercase     (				self   :			Tuple						):
									super().setUp()

									_a	: Dict								=				[
									    '[UNK]',
									    '[CLS]',
									    '[SEP]',
									    '[PAD]',
									    '[MASK]',
									    'want',
									    '##want',
									    '##ed',
									    'wa',
									    'un',
									    'runn',
									    '##ing',
									    ',',
									    'low',
									    'lowest',
									]
									_a	: List[Any]								=				os.path.join(self.tmpdirname      ,VOCAB_FILES_NAMES['vocab_file']						)
									with open(self.vocab_file      ,'w'      ,encoding='utf-8'						) as vocab_writer:
											vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]						)						)

									_a	: str								=				[
									    (tokenizer_def[0], self.pre_trained_model_path, tokenizer_def[2])  # else the 'google/' prefix is stripped
									    for tokenizer_def in self.tokenizers_list
									]







							def 				__lowercase     (				self   :			List[Any]      ,_UpperCAmelCase   :			int						):
									_a	: str								=				'UNwant\u00E9d,running'
									_a	: Optional[int]								=				'unwanted, running'
									return input_text, output_text







							def 				__lowercase     (				self   :			Union[str, Any]						):
									_a	: Optional[int]								=				self.tokenizer_class(self.vocab_file						)

									_a	: Optional[Any]								=				tokenizer.tokenize('UNwant\u00E9d,running'						)
									self.assertListEqual(_UpperCAmelCase      ,['un', '##want', '##ed', ',', 'runn', '##ing']						)
									self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase						)      ,[9, 6, 7, 12, 10, 11]						)







							def 				__lowercase     (				self   :			Optional[Any]						):
									if not self.test_rust_tokenizer:
											return

									_a	: int								=				self.get_tokenizer()
									_a	: str								=				self.get_rust_tokenizer()

									_a	: int								=				'UNwant\u00E9d,running'

									_a	: Dict								=				tokenizer.tokenize(_UpperCAmelCase						)
									_a	: int								=				rust_tokenizer.tokenize(_UpperCAmelCase						)
									self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)

									_a	: Optional[Any]								=				tokenizer.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)
									_a	: Any								=				rust_tokenizer.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)
									self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)

									_a	: Dict								=				self.get_rust_tokenizer()
									_a	: Any								=				tokenizer.encode(_UpperCAmelCase						)
									_a	: Union[str, Any]								=				rust_tokenizer.encode(_UpperCAmelCase						)
									self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)

									# With lower casing
									_a	: Tuple								=				self.get_tokenizer(do_lower_case=_UpperCAmelCase						)
									_a	: Tuple								=				self.get_rust_tokenizer(do_lower_case=_UpperCAmelCase						)

									_a	: Dict								=				'UNwant\u00E9d,running'

									_a	: int								=				tokenizer.tokenize(_UpperCAmelCase						)
									_a	: Optional[Any]								=				rust_tokenizer.tokenize(_UpperCAmelCase						)
									self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)

									_a	: int								=				tokenizer.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)
									_a	: Union[str, Any]								=				rust_tokenizer.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)
									self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)

									_a	: Any								=				self.get_rust_tokenizer()
									_a	: Tuple								=				tokenizer.encode(_UpperCAmelCase						)
									_a	: Any								=				rust_tokenizer.encode(_UpperCAmelCase						)
									self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)







							def 				__lowercase     (				self   :			List[Any]						):
									_a	: Any								=				BasicTokenizer()

									self.assertListEqual(tokenizer.tokenize('ah\u535A\u63A8zz'						)      ,['ah', '\u535A', '\u63A8', 'zz']						)







							def 				__lowercase     (				self   :			List[Any]						):
									_a	: Dict								=				BasicTokenizer(do_lower_case=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHeLLo!how  \n Are yoU?  '						)      ,['hello', '!', 'how', 'are', 'you', '?']						)
									self.assertListEqual(tokenizer.tokenize('H\u00E9llo'						)      ,['hello']						)







							def 				__lowercase     (				self   :			str						):
									_a	: List[Any]								=				BasicTokenizer(do_lower_case=_UpperCAmelCase      ,strip_accents=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '						)      ,['hällo', '!', 'how', 'are', 'you', '?']						)
									self.assertListEqual(tokenizer.tokenize('H\u00E9llo'						)      ,['h\u00E9llo']						)







							def 				__lowercase     (				self   :			List[str]						):
									_a	: Optional[Any]								=				BasicTokenizer(do_lower_case=_UpperCAmelCase      ,strip_accents=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '						)      ,['hallo', '!', 'how', 'are', 'you', '?']						)
									self.assertListEqual(tokenizer.tokenize('H\u00E9llo'						)      ,['hello']						)







							def 				__lowercase     (				self   :			str						):
									_a	: Tuple								=				BasicTokenizer(do_lower_case=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '						)      ,['hallo', '!', 'how', 'are', 'you', '?']						)
									self.assertListEqual(tokenizer.tokenize('H\u00E9llo'						)      ,['hello']						)







							def 				__lowercase     (				self   :			Union[str, Any]						):
									_a	: Any								=				BasicTokenizer(do_lower_case=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHeLLo!how  \n Are yoU?  '						)      ,['HeLLo', '!', 'how', 'Are', 'yoU', '?']						)







							def 				__lowercase     (				self   :			Any						):
									_a	: Any								=				BasicTokenizer(do_lower_case=_UpperCAmelCase      ,strip_accents=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '						)      ,['HäLLo', '!', 'how', 'Are', 'yoU', '?']						)







							def 				__lowercase     (				self   :			List[Any]						):
									_a	: Union[str, Any]								=				BasicTokenizer(do_lower_case=_UpperCAmelCase      ,strip_accents=_UpperCAmelCase						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHäLLo!how  \n Are yoU?  '						)      ,['HaLLo', '!', 'how', 'Are', 'yoU', '?']						)







							def 				__lowercase     (				self   :			List[Any]						):
									_a	: Optional[int]								=				BasicTokenizer(do_lower_case=_UpperCAmelCase      ,never_split=['[UNK]']						)

									self.assertListEqual(
									    tokenizer.tokenize(' \tHeLLo!how  \n Are yoU? [UNK]'						)      ,['HeLLo', '!', 'how', 'Are', 'yoU', '?', '[UNK]']						)







							def 				__lowercase     (				self   :			Optional[int]						):
									_a	: Optional[Any]								=				['[UNK]', '[CLS]', '[SEP]', 'want', '##want', '##ed', 'wa', 'un', 'runn', '##ing']

									_a	: str								=				{}
									for i, token in enumerate(_UpperCAmelCase						):
											_a	: Dict								=				i
									_a	: Tuple								=				WordpieceTokenizer(vocab=_UpperCAmelCase      ,unk_token='[UNK]'						)

									self.assertListEqual(tokenizer.tokenize(''						)      ,[]						)

									self.assertListEqual(tokenizer.tokenize('unwanted running'						)      ,['un', '##want', '##ed', 'runn', '##ing']						)

									self.assertListEqual(tokenizer.tokenize('unwantedX running'						)      ,['[UNK]', 'runn', '##ing']						)







							def 				__lowercase     (				self   :			Optional[Any]						):
									self.assertTrue(_is_whitespace(' '						)						)
									self.assertTrue(_is_whitespace('\t'						)						)
									self.assertTrue(_is_whitespace('\r'						)						)
									self.assertTrue(_is_whitespace('\n'						)						)
									self.assertTrue(_is_whitespace('\u00A0'						)						)

									self.assertFalse(_is_whitespace('A'						)						)
									self.assertFalse(_is_whitespace('-'						)						)







							def 				__lowercase     (				self   :			Optional[int]						):
									self.assertTrue(_is_control('\u0005'						)						)

									self.assertFalse(_is_control('A'						)						)
									self.assertFalse(_is_control(' '						)						)
									self.assertFalse(_is_control('\t'						)						)
									self.assertFalse(_is_control('\r'						)						)







							def 				__lowercase     (				self   :			Optional[Any]						):
									self.assertTrue(_is_punctuation('-'						)						)
									self.assertTrue(_is_punctuation('$'						)						)
									self.assertTrue(_is_punctuation('`'						)						)
									self.assertTrue(_is_punctuation('.'						)						)

									self.assertFalse(_is_punctuation('A'						)						)
									self.assertFalse(_is_punctuation(' '						)						)







							def 				__lowercase     (				self   :			List[str]						):
									_a	: List[str]								=				self.get_tokenizer()
									_a	: Optional[Any]								=				self.get_rust_tokenizer()

									# Example taken from the issue https://github.com/huggingface/tokenizers/issues/340
									self.assertListEqual([tokenizer.tokenize(_UpperCAmelCase						) for t in ['Test', '\xad', 'test']]      ,[['[UNK]'], [], ['[UNK]']]						)

									self.assertListEqual(
									    [rust_tokenizer.tokenize(_UpperCAmelCase						) for t in ['Test', '\xad', 'test']]      ,[['[UNK]'], [], ['[UNK]']]						)







							@slow
							def 				__lowercase     (				self   :			List[str]						):
									_a	: List[Any]								=				self.tokenizer_class.from_pretrained('google/mobilebert-uncased'						)

									_a	: Optional[Any]								=				tokenizer.encode('sequence builders'      ,add_special_tokens=_UpperCAmelCase						)
									_a	: str								=				tokenizer.encode('multi-sequence build'      ,add_special_tokens=_UpperCAmelCase						)

									_a	: Any								=				tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase						)
									_a	: Any								=				tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase      ,_UpperCAmelCase						)

									assert encoded_sentence == [101] + text + [102]
									assert encoded_pair == [101] + text + [102] + text_a + [102]







							def 				__lowercase     (				self   :			Dict						):
									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""						):
													_a	: Dict								=				self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase      ,**_UpperCAmelCase						)

													_a	: str								=				F"""A, naïve {tokenizer_r.mask_token} AllenNLP sentence."""
													_a	: List[str]								=				tokenizer_r.encode_plus(
													    _UpperCAmelCase      ,return_attention_mask=_UpperCAmelCase      ,return_token_type_ids=_UpperCAmelCase      ,return_offsets_mapping=_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase      ,)

													_a	: Optional[Any]								=				tokenizer_r.do_lower_case if hasattr(_UpperCAmelCase      ,'do_lower_case'						) else False
													_a	: Tuple								=				(
													    [
													        ((0, 0), tokenizer_r.cls_token),
													        ((0, 1), 'A'),
													        ((1, 2), ','),
													        ((3, 5), 'na'),
													        ((5, 6), '##ï'),
													        ((6, 8), '##ve'),
													        ((9, 15), tokenizer_r.mask_token),
													        ((16, 21), 'Allen'),
													        ((21, 23), '##NL'),
													        ((23, 24), '##P'),
													        ((25, 33), 'sentence'),
													        ((33, 34), '.'),
													        ((0, 0), tokenizer_r.sep_token),
													    ]
													    if not do_lower_case
													    else [
													        ((0, 0), tokenizer_r.cls_token),
													        ((0, 1), 'a'),
													        ((1, 2), ','),
													        ((3, 8), 'naive'),
													        ((9, 15), tokenizer_r.mask_token),
													        ((16, 21), 'allen'),
													        ((21, 23), '##nl'),
													        ((23, 24), '##p'),
													        ((25, 33), 'sentence'),
													        ((33, 34), '.'),
													        ((0, 0), tokenizer_r.sep_token),
													    ]
													)

													self.assertEqual(
													    [e[1] for e in expected_results]      ,tokenizer_r.convert_ids_to_tokens(tokens['input_ids']						)						)
													self.assertEqual([e[0] for e in expected_results]      ,tokens['offset_mapping']						)







							def 				__lowercase     (				self   :			int						):
									_a	: Any								=				['的', '人', '有']
									_a	: List[Any]								=				''.join(_UpperCAmelCase						)
									for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
											with self.subTest(F"""{tokenizer.__class__.__name__} ({pretrained_name})"""						):
													_a	: str								=				True
													_a	: Dict								=				self.tokenizer_class.from_pretrained(_UpperCAmelCase      ,**_UpperCAmelCase						)
													_a	: Optional[int]								=				self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase      ,**_UpperCAmelCase						)

													_a	: Optional[int]								=				tokenizer_p.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)
													_a	: Tuple								=				tokenizer_r.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)

													_a	: Union[str, Any]								=				tokenizer_r.convert_ids_to_tokens(_UpperCAmelCase						)
													_a	: Optional[int]								=				tokenizer_p.convert_ids_to_tokens(_UpperCAmelCase						)

													# it is expected that each Chinese character is not preceded by "##"
													self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)
													self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)

													_a	: str								=				False
													_a	: List[Any]								=				self.rust_tokenizer_class.from_pretrained(_UpperCAmelCase      ,**_UpperCAmelCase						)
													_a	: int								=				self.tokenizer_class.from_pretrained(_UpperCAmelCase      ,**_UpperCAmelCase						)

													_a	: Tuple								=				tokenizer_r.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)
													_a	: Any								=				tokenizer_p.encode(_UpperCAmelCase      ,add_special_tokens=_UpperCAmelCase						)

													_a	: List[Any]								=				tokenizer_r.convert_ids_to_tokens(_UpperCAmelCase						)
													_a	: Union[str, Any]								=				tokenizer_p.convert_ids_to_tokens(_UpperCAmelCase						)

													# it is expected that only the first Chinese character is not preceded by "##".
													_a	: Any								=				[
													    F"""##{token}""" if idx != 0 else token for idx, token in enumerate(_UpperCAmelCase						)
													]
													self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)
													self.assertListEqual(_UpperCAmelCase      ,_UpperCAmelCase						)









'''simple docstring'''

import unittest

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

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()

class       __magic_name__		(       unittest.TestCase  ):







							@property
							def 				__lowercase     (				self   :			int						):
									torch.manual_seed(0						)
									_a	: int								=				UNetaDModel(
									    block_out_channels=(32, 64)      ,layers_per_block=2      ,sample_size=32      ,in_channels=3      ,out_channels=3      ,down_block_types=('DownBlock2D', 'AttnDownBlock2D')      ,up_block_types=('AttnUpBlock2D', 'UpBlock2D')      ,)
									return model







							@property
							def 				__lowercase     (				self   :			Any						):
									torch.manual_seed(0						)
									_a	: int								=				VQModel(
									    block_out_channels=[32, 64]      ,in_channels=3      ,out_channels=3      ,down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']      ,up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']      ,latent_channels=3      ,)
									return model







							@property
							def 				__lowercase     (				self   :			Union[str, Any]						):
									torch.manual_seed(0						)
									_a	: Dict								=				CLIPTextConfig(
									    bos_token_id=0      ,eos_token_id=2      ,hidden_size=32      ,intermediate_size=37      ,layer_norm_eps=1E-05      ,num_attention_heads=4      ,num_hidden_layers=5      ,pad_token_id=1      ,vocab_size=1000      ,)
									return CLIPTextModel(_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Dict						):
									_a	: Union[str, Any]								=				self.dummy_uncond_unet
									_a	: Union[str, Any]								=				DDIMScheduler()
									_a	: Union[str, Any]								=				self.dummy_vq_model

									_a	: List[Any]								=				LDMPipeline(unet=_UpperCAmelCase      ,vqvae=_UpperCAmelCase      ,scheduler=_UpperCAmelCase						)
									ldm.to(_UpperCAmelCase						)
									ldm.set_progress_bar_config(disable=_UpperCAmelCase						)

									_a	: Dict								=				torch.manual_seed(0						)
									_a	: int								=				ldm(generator=_UpperCAmelCase      ,num_inference_steps=2      ,output_type='numpy'						).images

									_a	: Tuple								=				torch.manual_seed(0						)
									_a	: List[Any]								=				ldm(generator=_UpperCAmelCase      ,num_inference_steps=2      ,output_type='numpy'      ,return_dict=_UpperCAmelCase						)[0]

									_a	: Any								=				image[0, -3:, -3:, -1]
									_a	: List[Any]								=				image_from_tuple[0, -3:, -3:, -1]

									assert image.shape == (1, 64, 64, 3)
									_a	: Union[str, Any]								=				np.array([0.85_12, 0.8_18, 0.64_11, 0.68_08, 0.44_65, 0.56_18, 0.46, 0.62_31, 0.51_72]						)
									_a	: Any								=				1E-2 if torch_device != 'mps' else 3E-2

									assert np.abs(image_slice.flatten() - expected_slice						).max() < tolerance
									assert np.abs(image_from_tuple_slice.flatten() - expected_slice						).max() < tolerance

@slow
@require_torch
class       __magic_name__		(       unittest.TestCase  ):







							def 				__lowercase     (				self   :			Optional[int]						):
									_a	: Dict								=				LDMPipeline.from_pretrained('CompVis/ldm-celebahq-256'						)
									ldm.to(_UpperCAmelCase						)
									ldm.set_progress_bar_config(disable=_UpperCAmelCase						)

									_a	: str								=				torch.manual_seed(0						)
									_a	: Optional[Any]								=				ldm(generator=_UpperCAmelCase      ,num_inference_steps=5      ,output_type='numpy'						).images

									_a	: Optional[Any]								=				image[0, -3:, -3:, -1]

									assert image.shape == (1, 256, 256, 3)
									_a	: Union[str, Any]								=				np.array([0.43_99, 0.4_49_75, 0.4_68_25, 0.4_74, 0.43_59, 0.45_81, 0.4_50_95, 0.43_41, 0.44_47]						)
									_a	: List[str]								=				1E-2 if torch_device != 'mps' else 3E-2

									assert np.abs(image_slice.flatten() - expected_slice						).max() < tolerance




