

import os
import sys
import unittest


lowercase_									=   os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
lowercase_									=   os.path.join(git_repo_path, 'src', 'transformers')

lowercase_									=   '\n{0} = None\n'

lowercase_									=   '\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n'


lowercase_									=   '\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n'





class  __lowerCAmelCase     (					unittest.TestCase							):




  def   A__     (				self					)  -> List[Any]:
        '''simple docstring'''






        _lowercase	       =find_backend('    _import_structure["models.albert"].append("AlbertTokenizerFast")'					)
        self.assertIsNone(lowerCAmelCase					)

        _lowercase	       =find_backend('    if not is_tokenizers_available():'					)
        self.assertEqual(lowerCAmelCase							,   'tokenizers'					)

        _lowercase	       =find_backend('    if not is_tensorflow_text_available():'					)
        self.assertEqual(lowerCAmelCase							,   'tensorflow_text'					)

        _lowercase	       =find_backend('    if not (is_sentencepiece_available() and is_tokenizers_available()):'					)
        self.assertEqual(lowerCAmelCase							,   'sentencepiece_and_tokenizers'					)

        _lowercase	       =find_backend(
            '    if not (is_sentencepiece_available() and is_tensorflow_text_available()):'					)
        self.assertEqual(lowerCAmelCase							,   'sentencepiece_and_tensorflow_text'					)

        _lowercase	       =find_backend(
            '    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):'					)
        self.assertEqual(lowerCAmelCase							,   'sentencepiece_and_tokenizers_and_vision'					)




  def   A__     (				self					)  -> Union[str, Any]:
        '''simple docstring'''






        _lowercase	       =read_init()
        # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
        self.assertIn('torch'							,   lowerCAmelCase					)
        self.assertIn('tensorflow_text'							,   lowerCAmelCase					)
        self.assertIn('sentencepiece_and_tokenizers'							,   lowerCAmelCase					)

        # Likewise, we can't assert on the exact content of a key
        self.assertIn('BertModel'							,   objects['torch']					)
        self.assertIn('TFBertModel'							,   objects['tf']					)
        self.assertIn('FlaxBertModel'							,   objects['flax']					)
        self.assertIn('BertModel'							,   objects['torch']					)
        self.assertIn('TFBertTokenizer'							,   objects['tensorflow_text']					)
        self.assertIn('convert_slow_tokenizer'							,   objects['sentencepiece_and_tokenizers']					)




  def   A__     (				self					)  -> Tuple:
        '''simple docstring'''






        _lowercase	       =create_dummy_object('CONSTANT'							,   '\'torch\''					)
        self.assertEqual(lowerCAmelCase							,   '\nCONSTANT = None\n'					)

        _lowercase	       =create_dummy_object('function'							,   '\'torch\''					)
        self.assertEqual(
            lowerCAmelCase							,   '\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n'					)

        _lowercase	       ='\nclass FakeClass(metaclass=DummyObject):\n    _backends = \'torch\'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, \'torch\')\n'
        _lowercase	       =create_dummy_object('FakeClass'							,   '\'torch\''					)
        self.assertEqual(lowerCAmelCase							,   lowerCAmelCase					)




  def   A__     (				self					)  -> int:
        '''simple docstring'''






        _lowercase	       ='# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = ["torch"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, ["torch"])\n'
        _lowercase	       =create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']}					)
        self.assertEqual(dummy_files['torch']							,   lowerCAmelCase					)





import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin





class  __lowerCAmelCase     (					SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE			,			SCREAMING_SNAKE_CASE							):




  @register_to_config
  def __init__(				self							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase							,   lowerCAmelCase = False							,   )  -> Tuple:
        '''simple docstring'''






        super().__init__()

        _lowercase	       =nn.Embedding(lowerCAmelCase							,   lowerCAmelCase					)

        _lowercase	       =nn.Embedding(lowerCAmelCase							,   lowerCAmelCase					)
        _lowercase	       =False

        _lowercase	       =nn.Dropout(p=lowerCAmelCase					)

        _lowercase	       =TaConfig(
            vocab_size=lowerCAmelCase							,   d_model=lowerCAmelCase							,   num_heads=lowerCAmelCase							,   d_kv=lowerCAmelCase							,   d_ff=lowerCAmelCase							,   dropout_rate=lowerCAmelCase							,   feed_forward_proj=lowerCAmelCase							,   is_decoder=lowerCAmelCase							,   is_encoder_decoder=lowerCAmelCase							,   )

        _lowercase	       =nn.ModuleList()
        for lyr_num in range(lowerCAmelCase					):
              _lowercase	       =TaBlock(lowerCAmelCase					)
              self.encoders.append(lowerCAmelCase					)

        _lowercase	       =TaLayerNorm(lowerCAmelCase					)
        _lowercase	       =nn.Dropout(p=lowerCAmelCase					)




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase					)  -> Dict:
        '''simple docstring'''






        _lowercase	       =self.token_embedder(lowerCAmelCase					)

        _lowercase	       =encoder_input_tokens.shape[1]
        _lowercase	       =torch.arange(lowerCAmelCase							,   device=encoder_input_tokens.device					)
        x += self.position_encoding(lowerCAmelCase					)

        _lowercase	       =self.dropout_pre(lowerCAmelCase					)

        # inverted the attention mask
        _lowercase	       =encoder_input_tokens.size()
        _lowercase	       =self.get_extended_attention_mask(lowerCAmelCase							,   lowerCAmelCase					)

        for lyr in self.encoders:
              _lowercase	       =lyr(lowerCAmelCase							,   lowerCAmelCase					)[0]
        _lowercase	       =self.layer_norm(lowerCAmelCase					)

        return self.dropout_post(lowerCAmelCase					), encoder_inputs_mask


