






"""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       ):







  '''simple docstring'''


  def 					UpperCamelCase__   (      self:		Tuple				,			UpperCamelCase_:		bool = False					):
     UpperCamelCase_     =10

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

     UpperCamelCase_     =torch.rand(UpperCamelCase_				,			UpperCamelCase_				,			3				,			2					)
     UpperCamelCase_     =torch.rand(UpperCamelCase_				,			4				,			UpperCamelCase_					)
     UpperCamelCase_     =None

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





  def 					UpperCamelCase__   (      self:		Optional[int]					):
     # self and cross attention

     torch.manual_seed(0					)

     UpperCamelCase_     =self.get_constructor_arguments(only_cross_attention=UpperCamelCase_					)
     UpperCamelCase_     =Attention(**UpperCamelCase_					)

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

     UpperCamelCase_     =self.get_forward_arguments(
         query_dim=constructor_args["query_dim"]				,			added_kv_proj_dim=constructor_args["added_kv_proj_dim"]					)

     UpperCamelCase_     =attn(**UpperCamelCase_					)

     # only self attention

     torch.manual_seed(0					)

     UpperCamelCase_     =self.get_constructor_arguments(only_cross_attention=UpperCamelCase_					)
     UpperCamelCase_     =Attention(**UpperCamelCase_					)

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

     UpperCamelCase_     =self.get_forward_arguments(
         query_dim=constructor_args["query_dim"]				,			added_kv_proj_dim=constructor_args["added_kv_proj_dim"]					)

     UpperCamelCase_     =attn(**UpperCamelCase_					)

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




class 						__lowerCAmelCase   (			unittest.TestCase       ):







  '''simple docstring'''


  def 					UpperCamelCase__   (      self:		Tuple					):
     UpperCamelCase_     =DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe"				,			safety_checker=UpperCamelCase_					)

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

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

     UpperCamelCase_     =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(UpperCamelCase_					)

        # can also load the converted weights
        UpperCamelCase_     =DiffusionPipeline.from_pretrained(UpperCamelCase_				,			device_map="sequential"				,			safety_checker=UpperCamelCase_					)

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

     self.assertTrue(np.allclose(UpperCamelCase_				,			UpperCamelCase_				,			atol=1e-5					)					)
     self.assertTrue(np.allclose(UpperCamelCase_				,			UpperCamelCase_				,			atol=1e-5					)					)








"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  __lowerCamelCase       :    Optional[Any]     	=			["image_processor", "tokenizer"]
  __lowerCamelCase       :    List[str]     	=			"VideoMAEImageProcessor"
  __lowerCamelCase       :    Dict     	=			("CLIPTokenizer", "CLIPTokenizerFast")
  def __init__(      self:		str				,			UpperCamelCase_:		Dict=None				,			UpperCamelCase_:		str=None				,			**UpperCamelCase_:		str					):
     UpperCamelCase_     =None
     if "feature_extractor" in kwargs:
        warnings.warn(
            "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
            " instead."				,			UpperCamelCase_				,			)
        UpperCamelCase_     =kwargs.pop("feature_extractor"					)

     UpperCamelCase_     =image_processor if image_processor is not None else feature_extractor
     if image_processor is None:
        raise ValueError("You need to specify an `image_processor`."					)
     if tokenizer is None:
        raise ValueError("You need to specify a `tokenizer`."					)

     super().__init__(UpperCamelCase_				,			UpperCamelCase_					)
     UpperCamelCase_     =self.image_processor
  def __call__(      self:		Tuple				,			UpperCamelCase_:		str=None				,			UpperCamelCase_:		str=None				,			UpperCamelCase_:		Optional[Any]=None				,			**UpperCamelCase_:		int					):

     if text is None and videos is None:
        raise ValueError("You have to specify either text or videos. Both cannot be none."					)

     if text is not None:
        UpperCamelCase_     =self.tokenizer(UpperCamelCase_				,			return_tensors=UpperCamelCase_				,			**UpperCamelCase_					)

     if videos is not None:
        UpperCamelCase_     =self.image_processor(UpperCamelCase_				,			return_tensors=UpperCamelCase_				,			**UpperCamelCase_					)

     if text is not None and videos is not None:
        UpperCamelCase_     =image_features.pixel_values
        return encoding
     elif text is not None:
        return encoding
     else:
        return BatchEncoding(data=dict(**UpperCamelCase_					)				,			tensor_type=UpperCamelCase_					)
  def 					UpperCamelCase__   (      self:		Any				,			*UpperCamelCase_:		List[str]				,			**UpperCamelCase_:		Optional[int]					):
     return self.tokenizer.batch_decode(*UpperCamelCase_				,			**UpperCamelCase_					)
  def 					UpperCamelCase__   (      self:		Optional[Any]				,			*UpperCamelCase_:		List[str]				,			**UpperCamelCase_:		Tuple					):
     return self.tokenizer.decode(*UpperCamelCase_				,			**UpperCamelCase_					)
  @property
  def 					UpperCamelCase__   (      self:		List[Any]					):
     return ["input_ids", "attention_mask", "position_ids", "pixel_values"]
  @property
  def 					UpperCamelCase__   (      self:		str					):
     warnings.warn(
         "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."				,			UpperCamelCase_				,			)
     return self.image_processor_class





  @property
  def 					UpperCamelCase__   (      self:		Optional[Any]					):
     warnings.warn(
         "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."				,			UpperCamelCase_				,			)
     return self.image_processor
