





import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class     _UpperCAmelCase   (   _UpperCamelCase		,   unittest.TestCase   ):

  """simple docstring"""


  a_      =			MgpstrTokenizer
  a_      =			False
  a_      =			{}
  a_      =			False






  def     lowercase		(    self			:		int					)							->						Any:
     super().setUp()

     # fmt: off
     __lowerCAmelCase							     =     ['[GO]', '[s]', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
     # fmt: on
     __lowerCAmelCase							     =     dict(zip(lowerCAmelCase_ ,  range(len(lowerCAmelCase_					)					)					)					)

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






  def     lowercase		(    self			:		Tuple ,  **lowerCAmelCase_			:		List[str]					)							->						Union[str, Any]:
     return MgpstrTokenizer.from_pretrained(self.tmpdirname ,  **lowerCAmelCase_					)






  def     lowercase		(    self			:		Tuple ,  lowerCAmelCase_			:		Union[str, Any]					)							->						Tuple:
     __lowerCAmelCase							     =     'tester'
     __lowerCAmelCase							     =     'tester'
     return input_text, output_text






  @unittest.skip('MGP-STR always lower cases letters.'					)
  def     lowercase		(    self			:		Optional[int]					)							->						List[Any]:
     pass






  def     lowercase		(    self			:		str					)							->						Dict:
     __lowerCAmelCase							     =     self.get_tokenizers(do_lower_case=lowerCAmelCase_					)
     for tokenizer in tokenizers:
        with self.subTest(f"""{tokenizer.__class__.__name__}"""					):
           __lowerCAmelCase							     =     '[SPECIAL_TOKEN]'

           tokenizer.add_special_tokens({'cls_token': special_token}					)
           __lowerCAmelCase							     =     tokenizer.encode([special_token] ,  add_special_tokens=lowerCAmelCase_					)
           self.assertEqual(len(lowerCAmelCase_					) ,  1					)

           __lowerCAmelCase							     =     tokenizer.decode(lowerCAmelCase_ ,  skip_special_tokens=lowerCAmelCase_					)
           self.assertTrue(special_token not in decoded					)






  def     lowercase		(    self			:		Optional[Any]					)							->						Optional[int]:
     __lowerCAmelCase							     =     self.get_tokenizers()
     for tokenizer in tokenizers:
        with self.subTest(f"""{tokenizer.__class__.__name__}"""					):
           __lowerCAmelCase     ,     __lowerCAmelCase							     =     self.get_input_output_texts(lowerCAmelCase_					)

           __lowerCAmelCase							     =     tokenizer.tokenize(lowerCAmelCase_					)
           __lowerCAmelCase							     =     tokenizer.convert_tokens_to_ids(lowerCAmelCase_					)
           __lowerCAmelCase							     =     tokenizer.encode(lowerCAmelCase_ ,  add_special_tokens=lowerCAmelCase_					)
           self.assertListEqual(lowerCAmelCase_ ,  lowerCAmelCase_					)

           __lowerCAmelCase							     =     tokenizer.convert_ids_to_tokens(lowerCAmelCase_					)
           self.assertNotEqual(len(lowerCAmelCase_					) ,  0					)
           __lowerCAmelCase							     =     tokenizer.decode(lowerCAmelCase_					)
           self.assertIsInstance(lowerCAmelCase_ ,  lowerCAmelCase_					)

           self.assertEqual(text_a.replace(' ' ,  ''					) ,  lowerCAmelCase_					)






  @unittest.skip('MGP-STR tokenizer only handles one sequence.'					)
  def     lowercase		(    self			:		Tuple					)							->						Optional[Any]:
     pass






  @unittest.skip('inputs cannot be pretokenized in MgpstrTokenizer'					)
  def     lowercase		(    self			:		Tuple					)							->						Tuple:
     pass








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     _UpperCAmelCase   (   _UpperCamelCase   ):

  """simple docstring"""


  a_      =			42





class     _UpperCAmelCase   (   _UpperCamelCase		,   _UpperCamelCase   ):

  """simple docstring"""


  @register_to_config
  def __init__(    self			:		Optional[int] ,  lowerCAmelCase_			:		int = 1_6 ,  lowerCAmelCase_			:		int = 8_8 ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		int = 1 ,  lowerCAmelCase_			:		float = 0.0 ,  lowerCAmelCase_			:		int = 3_2 ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		bool = False ,  lowerCAmelCase_			:		Optional[int] = None ,  lowerCAmelCase_			:		str = "geglu" ,  lowerCAmelCase_			:		bool = True ,  lowerCAmelCase_			:		bool = True ,  )							->						Dict:
     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     lowercase		(    self			:		Dict ,  lowerCAmelCase_			:		Tuple ,  lowerCAmelCase_			:		List[Any]=None ,  lowerCAmelCase_			:		Optional[int]=None ,  lowerCAmelCase_			:		Union[str, Any]=None ,  lowerCAmelCase_			:		Optional[int]=1 ,  lowerCAmelCase_			:		Optional[int]=None ,  lowerCAmelCase_			:		bool = True ,  )							->						str:
     __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_					)

