

'''simple docstring'''

from typing import Optional, Union

from ...file_utils import TensorType
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, TruncationStrategy







class lowerCamelCase   (    __lowerCAmelCase				):
				snake_case_							     = '''MarkupLMFeatureExtractor'''
				snake_case_							     = ('''MarkupLMTokenizer''', '''MarkupLMTokenizerFast''')
				snake_case_							     = True

				def __call__(       self,						lowercase_=None,						lowercase_=None,						lowercase_=None,						lowercase_=None,						lowercase_=None,						lowercase_ = True,						lowercase_ = False,						lowercase_ = None,						lowercase_ = None,						lowercase_ = 0,						lowercase_ = None,						lowercase_ = None,						lowercase_ = None,						lowercase_ = False,						lowercase_ = False,						lowercase_ = False,						lowercase_ = False,						lowercase_ = True,						lowercase_ = None,						**lowercase_,						)    ->							BatchEncoding:
					# first, create nodes and xpaths
					if self.parse_html:
						if html_strings is None:
							raise ValueError('Make sure to pass HTML strings in case `parse_html` is set to `True`' )

						if nodes is not None or xpaths is not None or node_labels is not None:
							raise ValueError(
							    'Please don\'t pass nodes, xpaths nor node labels in case `parse_html` is set to `True`' )

						snake_case							=					self.feature_extractor(lowercase_ )
						snake_case							=					features['nodes']
						snake_case							=					features['xpaths']
					else:
						if html_strings is not None:
							raise ValueError('You have passed HTML strings but `parse_html` is set to `False`.' )
						if nodes is None or xpaths is None:
							raise ValueError('Make sure to pass nodes and xpaths in case `parse_html` is set to `False`' )

        # # second, apply the tokenizer
					if questions is not None and self.parse_html:
						if isinstance(lowercase_,						lowercase_ ):
							snake_case							=					[questions]  # add batch dimension (as the feature extractor always adds a batch dimension)

					snake_case							=					self.tokenizer(
					    text=questions if questions is not None else nodes,						text_pair=nodes if questions is not None else None,						xpaths=lowercase_,						node_labels=lowercase_,						add_special_tokens=lowercase_,						padding=lowercase_,						truncation=lowercase_,						max_length=lowercase_,						stride=lowercase_,						pad_to_multiple_of=lowercase_,						return_token_type_ids=lowercase_,						return_attention_mask=lowercase_,						return_overflowing_tokens=lowercase_,						return_special_tokens_mask=lowercase_,						return_offsets_mapping=lowercase_,						return_length=lowercase_,						verbose=lowercase_,						return_tensors=lowercase_,						**lowercase_,						)

					return encoded_inputs

				def 					_lowerCamelCase    (       self,						*lowercase_,						**lowercase_ )    ->							Optional[int]:
					return self.tokenizer.batch_decode(*lowercase_,						**lowercase_ )

				def 					_lowerCamelCase    (       self,						*lowercase_,						**lowercase_ )    ->							Tuple:
					return self.tokenizer.decode(*lowercase_,						**lowercase_ )






				@property
				def 					_lowerCamelCase    (       self )    ->							Any:
					snake_case							=					self.tokenizer.model_input_names
					return tokenizer_input_names








'''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 lowerCamelCase   (    unittest.TestCase				):

				def 					_lowerCamelCase    (       self,						lowercase_ = False )    ->							Union[str, Any]:
					snake_case							=					10

					if only_cross_attention:
						snake_case							=					12
					else:
						# when only cross attention is not set, the cross attention dim must be the same as the query dim
						snake_case							=					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 					_lowerCamelCase    (       self,						lowercase_,						lowercase_ )    ->							Optional[int]:
					snake_case							=					2

					snake_case							=					torch.rand(lowercase_,						lowercase_,						3,						2 )
					snake_case							=					torch.rand(lowercase_,						4,						lowercase_ )
					snake_case							=					None

					return {
					    "hidden_states": hidden_states,
					    "encoder_hidden_states": encoder_hidden_states,
					    "attention_mask": attention_mask,
					}






				def 					_lowerCamelCase    (       self )    ->							str:
					# self and cross attention

					torch.manual_seed(0 )

					snake_case							=					self.get_constructor_arguments(only_cross_attention=lowercase_ )
					snake_case							=					Attention(**lowercase_ )

					self.assertTrue(attn.to_k is not None )
					self.assertTrue(attn.to_v is not None )

					snake_case							=					self.get_forward_arguments(
					    query_dim=constructor_args['query_dim'],						added_kv_proj_dim=constructor_args['added_kv_proj_dim'] )

					snake_case							=					attn(**lowercase_ )

					# only self attention

					torch.manual_seed(0 )

					snake_case							=					self.get_constructor_arguments(only_cross_attention=lowercase_ )
					snake_case							=					Attention(**lowercase_ )

					self.assertTrue(attn.to_k is None )
					self.assertTrue(attn.to_v is None )

					snake_case							=					self.get_forward_arguments(
					    query_dim=constructor_args['query_dim'],						added_kv_proj_dim=constructor_args['added_kv_proj_dim'] )

					snake_case							=					attn(**lowercase_ )

					self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all() )







class lowerCamelCase   (    unittest.TestCase				):

				def 					_lowerCamelCase    (       self )    ->							List[Any]:
					snake_case							=					DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe',						safety_checker=lowercase_ )

					snake_case							=					pipe(
					    'foo',						num_inference_steps=2,						generator=torch.Generator('cpu' ).manual_seed(0 ),						output_type='np',						).images

					# the initial conversion succeeds
					snake_case							=					DiffusionPipeline.from_pretrained(
					    'hf-internal-testing/tiny-stable-diffusion-pipe',						device_map='sequential',						safety_checker=lowercase_ )

					snake_case							=					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(lowercase_ )

						# can also load the converted weights
						snake_case							=					DiffusionPipeline.from_pretrained(lowercase_,						device_map='sequential',						safety_checker=lowercase_ )

					snake_case							=					pipe(
					    'foo',						num_inference_steps=2,						generator=torch.Generator('cpu' ).manual_seed(0 ),						output_type='np',						).images

					self.assertTrue(np.allclose(lowercase_,						lowercase_,						atol=1E-5 ) )
					self.assertTrue(np.allclose(lowercase_,						lowercase_,						atol=1E-5 ) )





