





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
			from transformers import TvltImageProcessor

if is_speech_available():
			from transformers import TvltFeatureExtractor

from transformers import TvltProcessor






@require_torch
class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	'''ZinengTang/tvlt-base'''
				lowerCamelCase__              =	tempfile.mkdtemp()





			def 	__lowerCamelCase       (					self ,						**__lowerCAmelCase							):



				'''simple docstring'''







				return TvltImageProcessor.from_pretrained(self.checkpoint ,						**__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self ,						**__lowerCAmelCase							):



				'''simple docstring'''







				return TvltFeatureExtractor.from_pretrained(self.checkpoint ,						**__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				shutil.rmtree(self.tmpdirname							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.get_image_processor()
				lowerCamelCase__              =	self.get_feature_extractor()

				lowerCamelCase__              =	TvltProcessor(image_processor=__lowerCAmelCase ,						feature_extractor=__lowerCAmelCase							)
				processor.save_pretrained(self.tmpdirname							)
				lowerCamelCase__              =	TvltProcessor.from_pretrained(self.tmpdirname							)

				self.assertIsInstance(processor.feature_extractor ,						__lowerCAmelCase							)
				self.assertIsInstance(processor.image_processor ,						__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.get_image_processor()
				lowerCamelCase__              =	self.get_feature_extractor()

				lowerCamelCase__              =	TvltProcessor(image_processor=__lowerCAmelCase ,						feature_extractor=__lowerCAmelCase							)

				lowerCamelCase__              =	np.ones([1_2_0_0_0]							)

				lowerCamelCase__              =	feature_extractor(__lowerCAmelCase ,						return_tensors='''np'''							)
				lowerCamelCase__              =	processor(audio=__lowerCAmelCase ,						return_tensors='''np'''							)

				for key in audio_dict.keys():
					self.assertAlmostEqual(audio_dict[key].sum() ,						input_processor[key].sum() ,						delta=1E-2							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.get_image_processor()
				lowerCamelCase__              =	self.get_feature_extractor()

				lowerCamelCase__              =	TvltProcessor(image_processor=__lowerCAmelCase ,						feature_extractor=__lowerCAmelCase							)

				lowerCamelCase__              =	np.ones([3, 2_2_4, 2_2_4]							)

				lowerCamelCase__              =	image_processor(__lowerCAmelCase ,						return_tensors='''np'''							)
				lowerCamelCase__              =	processor(images=__lowerCAmelCase ,						return_tensors='''np'''							)

				for key in image_dict.keys():
					self.assertAlmostEqual(image_dict[key].sum() ,						input_processor[key].sum() ,						delta=1E-2							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.get_image_processor()
				lowerCamelCase__              =	self.get_feature_extractor()

				lowerCamelCase__              =	TvltProcessor(image_processor=__lowerCAmelCase ,						feature_extractor=__lowerCAmelCase							)

				lowerCamelCase__              =	np.ones([1_2_0_0_0]							)
				lowerCamelCase__              =	np.ones([3, 2_2_4, 2_2_4]							)

				lowerCamelCase__              =	processor(audio=__lowerCAmelCase ,						images=__lowerCAmelCase							)

				self.assertListEqual(list(inputs.keys()							) ,						['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']							)

				# test if it raises when no input is passed
				with pytest.raises(__lowerCAmelCase							):
					processor()





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	self.get_image_processor()
				lowerCamelCase__              =	self.get_feature_extractor()

				lowerCamelCase__              =	TvltProcessor(image_processor=__lowerCAmelCase ,						feature_extractor=__lowerCAmelCase							)

				self.assertListEqual(
				    processor.model_input_names ,						image_processor.model_input_names + feature_extractor.model_input_names ,						msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match''' ,						)










from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin






@dataclass
class       __A					(					lowerCAmelCase				):

			'''simple docstring'''







			lowerCAmelCase_							  =		42






class       __A					(					lowerCAmelCase    ,       lowerCAmelCase				):

			'''simple docstring'''







			@register_to_config
			def __init__(					self ,						__lowerCAmelCase = 1_6 ,						__lowerCAmelCase = 8_8 ,						__lowerCAmelCase = None ,						__lowerCAmelCase = None ,						__lowerCAmelCase = 1 ,						__lowerCAmelCase = 0.0 ,						__lowerCAmelCase = 3_2 ,						__lowerCAmelCase = None ,						__lowerCAmelCase = False ,						__lowerCAmelCase = None ,						__lowerCAmelCase = "geglu" ,						__lowerCAmelCase = True ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				super().__init__()
				lowerCamelCase__              =	num_attention_heads
				lowerCamelCase__              =	attention_head_dim
				lowerCamelCase__              =	num_attention_heads * attention_head_dim

				lowerCamelCase__              =	in_channels

				lowerCamelCase__              =	torch.nn.GroupNorm(num_groups=__lowerCAmelCase ,						num_channels=__lowerCAmelCase ,						eps=1E-6 ,						affine=__lowerCAmelCase							)
				lowerCamelCase__              =	nn.Linear(__lowerCAmelCase ,						__lowerCAmelCase							)

				# 3. Define transformers blocks
				lowerCamelCase__              =	nn.ModuleList(
				    [
				        BasicTransformerBlock(
				            __lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						dropout=__lowerCAmelCase ,						cross_attention_dim=__lowerCAmelCase ,						activation_fn=__lowerCAmelCase ,						attention_bias=__lowerCAmelCase ,						double_self_attention=__lowerCAmelCase ,						norm_elementwise_affine=__lowerCAmelCase ,						)
				        for d in range(__lowerCAmelCase							)
				    ]							)

				lowerCamelCase__              =	nn.Linear(__lowerCAmelCase ,						__lowerCAmelCase							)





			def 	__lowerCamelCase       (					self ,						__lowerCAmelCase ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=None ,						__lowerCAmelCase=1 ,						__lowerCAmelCase=None ,						__lowerCAmelCase = True ,						):



				'''simple docstring'''







				lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__              =	hidden_states.shape
				lowerCamelCase__              =	batch_frames // num_frames

				lowerCamelCase__              =	hidden_states

				lowerCamelCase__              =	hidden_states[None, :].reshape(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							)
				lowerCamelCase__              =	hidden_states.permute(0 ,						2 ,						1 ,						3 ,						4							)

				lowerCamelCase__              =	self.norm(__lowerCAmelCase							)
				lowerCamelCase__              =	hidden_states.permute(0 ,						3 ,						4 ,						2 ,						1							).reshape(batch_size * height * width ,						__lowerCAmelCase ,						__lowerCAmelCase							)

				lowerCamelCase__              =	self.proj_in(__lowerCAmelCase							)

				# 2. Blocks
				for block in self.transformer_blocks:
					lowerCamelCase__              =	block(
					    __lowerCAmelCase ,						encoder_hidden_states=__lowerCAmelCase ,						timestep=__lowerCAmelCase ,						cross_attention_kwargs=__lowerCAmelCase ,						class_labels=__lowerCAmelCase ,						)

				# 3. Output
				lowerCamelCase__              =	self.proj_out(__lowerCAmelCase							)
				lowerCamelCase__              =	(
				    hidden_states[None, None, :]
				    .reshape(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							)
				    .permute(0 ,						3 ,						4 ,						1 ,						2							)
				    .contiguous()
				)
				lowerCamelCase__              =	hidden_states.reshape(__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase ,						__lowerCAmelCase							)

				lowerCamelCase__              =	hidden_states + residual

				if not return_dict:
					return (output,)

				return TransformerTemporalModelOutput(sample=__lowerCAmelCase							)



