





from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers import (
        TFDebertaForMaskedLM,
        TFDebertaForQuestionAnswering,
        TFDebertaForSequenceClassification,
        TFDebertaForTokenClassification,
        TFDebertaModel,
    )




class    _a  :

      """simple docstring"""


      def __init__(						self:	List[str]					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	Union[str, Any]=13					,  __lowerCamelCase:	str=7					,  __lowerCamelCase:	List[str]=True					,  __lowerCamelCase:	int=True					,  __lowerCamelCase:	Optional[Any]=True					,  __lowerCamelCase:	Union[str, Any]=True					,  __lowerCamelCase:	Dict=99					,  __lowerCamelCase:	Dict=32					,  __lowerCamelCase:	Optional[int]=2					,  __lowerCamelCase:	Tuple=4					,  __lowerCamelCase:	List[str]=37					,  __lowerCamelCase:	Tuple="gelu"					,  __lowerCamelCase:	str=0.1					,  __lowerCamelCase:	Union[str, Any]=0.1					,  __lowerCamelCase:	List[Any]=512					,  __lowerCamelCase:	Any=16					,  __lowerCamelCase:	int=2					,  __lowerCamelCase:	Any=0.02					,  __lowerCamelCase:	Optional[int]=3					,  __lowerCamelCase:	Dict=4					,  __lowerCamelCase:	str=None					,  ):
          '''simple docstring'''


          UpperCamelCase__:					str												=      parent
          UpperCamelCase__:					List[str]												=      13
          UpperCamelCase__:					Optional[Any]												=      7
          UpperCamelCase__:					Optional[Any]												=      True
          UpperCamelCase__:					Tuple												=      True
          UpperCamelCase__:					Dict												=      True
          UpperCamelCase__:					Dict												=      True
          UpperCamelCase__:					Union[str, Any]												=      99
          UpperCamelCase__:					str												=      32
          UpperCamelCase__:					Union[str, Any]												=      2
          UpperCamelCase__:					Dict												=      4
          UpperCamelCase__:					Tuple												=      37
          UpperCamelCase__:					str												=      'gelu'
          UpperCamelCase__:					Dict												=      0.1
          UpperCamelCase__:					Optional[Any]												=      0.1
          UpperCamelCase__:					Optional[Any]												=      512
          UpperCamelCase__:					Optional[int]												=      16
          UpperCamelCase__:					Any												=      False
          UpperCamelCase__:					Optional[Any]												=      -1
          UpperCamelCase__:					List[str]												=      True
          UpperCamelCase__:					Optional[int]												=      2
          UpperCamelCase__:					Dict												=      0.02
          UpperCamelCase__:					Any												=      3
          UpperCamelCase__:					List[Any]												=      4
          UpperCamelCase__:					Union[str, Any]												=      None






      def 	UpperCAmelCase_   (						self:	Tuple			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      ids_tensor([self.batch_size, self.seq_length]					,  self.vocab_size			)

          UpperCamelCase__:					int												=      None
          if self.use_input_mask:
              UpperCamelCase__:					Dict												=      random_attention_mask([self.batch_size, self.seq_length]			)

          UpperCamelCase__:					Optional[int]												=      None
          if self.use_token_type_ids:
              UpperCamelCase__:					Union[str, Any]												=      ids_tensor([self.batch_size, self.seq_length]					,  self.type_vocab_size			)

          UpperCamelCase__:					str												=      None
          UpperCamelCase__:					Tuple												=      None
          UpperCamelCase__:					Union[str, Any]												=      None
          if self.use_labels:
              UpperCamelCase__:					Union[str, Any]												=      ids_tensor([self.batch_size]					,  self.type_sequence_label_size			)
              UpperCamelCase__:					Optional[Any]												=      ids_tensor([self.batch_size, self.seq_length]					,  self.num_labels			)

          UpperCamelCase__:					str												=      DebertaConfig(
              vocab_size=self.vocab_size					,  hidden_size=self.hidden_size					,  num_hidden_layers=self.num_hidden_layers					,  num_attention_heads=self.num_attention_heads					,  intermediate_size=self.intermediate_size					,  hidden_act=self.hidden_act					,  hidden_dropout_prob=self.hidden_dropout_prob					,  attention_probs_dropout_prob=self.attention_probs_dropout_prob					,  max_position_embeddings=self.max_position_embeddings					,  type_vocab_size=self.type_vocab_size					,  relative_attention=self.relative_attention					,  max_relative_positions=self.max_relative_positions					,  position_biased_input=self.position_biased_input					,  initializer_range=self.initializer_range					,  return_dict=__UpperCamelCase					,  )

          return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






      def 	UpperCAmelCase_   (						self:	List[str]					,  __lowerCamelCase:	Optional[Any]					,  __lowerCamelCase:	int					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	int					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	Tuple			):
          '''simple docstring'''


          UpperCamelCase__:					List[str]												=      TFDebertaModel(config=__UpperCamelCase			)
          UpperCamelCase__:					List[str]												=      {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

          UpperCamelCase__:					Dict												=      [input_ids, input_mask]
          UpperCamelCase__:					Dict												=      model(__UpperCamelCase			)

          UpperCamelCase__:					List[Any]												=      model(__UpperCamelCase			)

          self.parent.assertEqual(result.last_hidden_state.shape					,  (self.batch_size, self.seq_length, self.hidden_size)			)






      def 	UpperCAmelCase_   (						self:	str					,  __lowerCamelCase:	int					,  __lowerCamelCase:	Optional[Any]					,  __lowerCamelCase:	int					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	Dict			):
          '''simple docstring'''


          UpperCamelCase__:					Tuple												=      TFDebertaForMaskedLM(config=__UpperCamelCase			)
          UpperCamelCase__:					int												=      {
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          UpperCamelCase__:					int												=      model(__UpperCamelCase			)
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.vocab_size)			)






      def 	UpperCAmelCase_   (						self:	Any					,  __lowerCamelCase:	Optional[int]					,  __lowerCamelCase:	Optional[Any]					,  __lowerCamelCase:	Optional[int]					,  __lowerCamelCase:	Dict					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	str					,  __lowerCamelCase:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					str												=      self.num_labels
          UpperCamelCase__:					str												=      TFDebertaForSequenceClassification(config=__UpperCamelCase			)
          UpperCamelCase__:					Dict												=      {
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          UpperCamelCase__:					Dict												=      model(__UpperCamelCase			)
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.num_labels)			)






      def 	UpperCAmelCase_   (						self:	Union[str, Any]					,  __lowerCamelCase:	Any					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	Any					,  __lowerCamelCase:	Union[str, Any]					,  __lowerCamelCase:	Optional[int]					,  __lowerCamelCase:	str					,  __lowerCamelCase:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					List[str]												=      self.num_labels
          UpperCamelCase__:					List[Any]												=      TFDebertaForTokenClassification(config=__UpperCamelCase			)
          UpperCamelCase__:					List[str]												=      {
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }
          UpperCamelCase__:					Optional[int]												=      model(__UpperCamelCase			)
          self.parent.assertEqual(result.logits.shape					,  (self.batch_size, self.seq_length, self.num_labels)			)






      def 	UpperCAmelCase_   (						self:	List[str]					,  __lowerCamelCase:	Union[str, Any]					,  __lowerCamelCase:	Dict					,  __lowerCamelCase:	List[str]					,  __lowerCamelCase:	List[Any]					,  __lowerCamelCase:	Tuple					,  __lowerCamelCase:	Optional[Any]					,  __lowerCamelCase:	List[str]			):
          '''simple docstring'''


          UpperCamelCase__:					int												=      TFDebertaForQuestionAnswering(config=__UpperCamelCase			)
          UpperCamelCase__:					Optional[Any]												=      {
              'input_ids': input_ids,
              'attention_mask': input_mask,
              'token_type_ids': token_type_ids,
          }

          UpperCamelCase__:					List[str]												=      model(__UpperCamelCase			)
          self.parent.assertEqual(result.start_logits.shape					,  (self.batch_size, self.seq_length)			)
          self.parent.assertEqual(result.end_logits.shape					,  (self.batch_size, self.seq_length)			)






      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          UpperCamelCase__:					List[Any]												=      self.prepare_config_and_inputs()
          (
              UpperCamelCase__  
          ):					List[Any]												=      config_and_inputs
          UpperCamelCase__:					Union[str, Any]												=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
          return config, inputs_dict




@require_tf
class    _a  (  _lowerCamelCase		,	_lowerCamelCase		,	unittest.TestCase):

      """simple docstring"""


      UpperCamelCase__       						=   (
          (
              TFDebertaModel,
              TFDebertaForMaskedLM,
              TFDebertaForQuestionAnswering,
              TFDebertaForSequenceClassification,
              TFDebertaForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      UpperCamelCase__       						=   (
          {
              """feature-extraction""": TFDebertaModel,
              """fill-mask""": TFDebertaForMaskedLM,
              """question-answering""": TFDebertaForQuestionAnswering,
              """text-classification""": TFDebertaForSequenceClassification,
              """token-classification""": TFDebertaForTokenClassification,
              """zero-shot""": TFDebertaForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )

      UpperCamelCase__       						=   False
      UpperCamelCase__       						=   False






      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          UpperCamelCase__:					Tuple												=      TFDebertaModelTester(self			)
          UpperCamelCase__:					Optional[Any]												=      ConfigTester(self					,  config_class=__UpperCamelCase					,  hidden_size=37			)






      def 	UpperCAmelCase_   (						self:	Optional[int]			):
          '''simple docstring'''


          self.config_tester.run_common_tests()






      def 	UpperCAmelCase_   (						self:	int			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[int]												=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__UpperCamelCase			)






      def 	UpperCAmelCase_   (						self:	Optional[Any]			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase			)






      def 	UpperCAmelCase_   (						self:	Dict			):
          '''simple docstring'''


          UpperCamelCase__:					str												=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase			)






      def 	UpperCAmelCase_   (						self:	Dict			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[int]												=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase			)






      def 	UpperCAmelCase_   (						self:	Tuple			):
          '''simple docstring'''


          UpperCamelCase__:					Dict												=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase			)






      @slow
      def 	UpperCAmelCase_   (						self:	Optional[int]			):
          '''simple docstring'''


          UpperCamelCase__:					Any												=      TFDebertaModel.from_pretrained("kamalkraj/deberta-base"			)
          self.assertIsNotNone(__UpperCamelCase			)




@require_tf
class    _a  (  unittest.TestCase):

      """simple docstring"""


      @unittest.skip(reason="Model not available yet"			)
      def 	UpperCAmelCase_   (						self:	Optional[int]			):
          '''simple docstring'''


          pass






      @slow
      def 	UpperCAmelCase_   (						self:	Optional[int]			):
          '''simple docstring'''


          UpperCamelCase__:					List[str]												=      TFDebertaModel.from_pretrained("kamalkraj/deberta-base"			)
          UpperCamelCase__:					Any												=      tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]			)
          UpperCamelCase__:					str												=      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]			)
          UpperCamelCase__:					Optional[int]												=      model(__UpperCamelCase					,  attention_mask=__UpperCamelCase			)[0]

          UpperCamelCase__:					List[Any]												=      tf.constant(
              [
                  [
                      [-0.59_855_896, -0.80_552_566, -0.8_462_135],
                      [1.4_484_025, -0.93_483_794, -0.80_593_085],
                      [0.3_122_741, 0.00_316_059, -1.4_131_377],
                  ]
              ]			)
          tf.debugging.assert_near(output[:, 1:4, 1:4]					,  __UpperCamelCase					,  atol=1e-4			)








from manim import *




class    _a  (  UpperCamelCase__):

      """simple docstring"""


      def 	UpperCAmelCase_   (						self:	str			):
          '''simple docstring'''


          UpperCamelCase__:					Optional[Any]												=      Rectangle(height=0.5					,  width=0.5			)
          UpperCamelCase__:					Optional[int]												=      Rectangle(height=0.46					,  width=0.46			).set_stroke(width=0			)
          UpperCamelCase__:					List[Any]												=      Rectangle(height=0.25					,  width=0.25			)

          UpperCamelCase__:					Tuple												=      [mem.copy() for i in range(6			)]
          UpperCamelCase__:					Dict												=      [mem.copy() for i in range(6			)]
          UpperCamelCase__:					Dict												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					List[Any]												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					List[Any]												=      VGroup(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					List[str]												=      Text("CPU"					,  font_size=24			)
          UpperCamelCase__:					Optional[int]												=      Group(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0.5					,  aligned_edge=__lowerCamelCase			)
          cpu.move_to([-2.5, -0.5, 0]			)
          self.add(__lowerCamelCase			)

          UpperCamelCase__:					Dict												=      [mem.copy() for i in range(4			)]
          UpperCamelCase__:					Any												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					str												=      Text("GPU"					,  font_size=24			)
          UpperCamelCase__:					List[Any]												=      Group(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0.5					,  aligned_edge=__lowerCamelCase			)
          gpu.move_to([-1, -1, 0]			)
          self.add(__lowerCamelCase			)

          UpperCamelCase__:					str												=      [mem.copy() for i in range(6			)]
          UpperCamelCase__:					Tuple												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)

          UpperCamelCase__:					Union[str, Any]												=      Text("Model"					,  font_size=24			)
          UpperCamelCase__:					Tuple												=      Group(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0.5					,  aligned_edge=__lowerCamelCase			)
          model.move_to([3, -1.0, 0]			)
          self.add(__lowerCamelCase			)

          UpperCamelCase__:					int												=      []
          UpperCamelCase__:					Optional[Any]												=      []

          for i, rect in enumerate(__lowerCamelCase			):
              rect.set_stroke(__lowerCamelCase			)

              UpperCamelCase__:					Tuple												=      Rectangle(height=0.46 / 4					,  width=0.46 / 3			).set_stroke(width=0.0			).set_fill(__lowerCamelCase					,  opacity=0.7			)

              if i == 0:
                  cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT			)					,  buff=0.02					,  direction=__lowerCamelCase			)
                  cpu_target.set_x(cpu_target.get_x() + 0.1			)
              elif i == 3:
                  cpu_target.next_to(model_cpu_arr[0]					,  direction=__lowerCamelCase					,  buff=0.0			)
              else:
                  cpu_target.next_to(model_cpu_arr[i - 1]					,  direction=__lowerCamelCase					,  buff=0.0			)
              self.add(__lowerCamelCase			)
              model_cpu_arr.append(__lowerCamelCase			)

          self.add(*__lowerCamelCase					,  *__lowerCamelCase			)

          UpperCamelCase__:					int												=      [meta_mem.copy() for i in range(6			)]
          UpperCamelCase__:					Dict												=      [meta_mem.copy() for i in range(6			)]
          UpperCamelCase__:					Union[str, Any]												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					Tuple												=      VGroup(*__lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					int												=      VGroup(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0			)
          UpperCamelCase__:					List[Any]												=      Text("Disk"					,  font_size=24			)
          UpperCamelCase__:					Union[str, Any]												=      Group(__lowerCamelCase					,  __lowerCamelCase			).arrange(__lowerCamelCase					,  buff=0.5					,  aligned_edge=__lowerCamelCase			)
          disk.move_to([-4.0, -1.25, 0]			)
          self.add(__lowerCamelCase					,  __lowerCamelCase			)

          UpperCamelCase__:					Optional[int]												=      []

          for i in range(6			):
              UpperCamelCase__:					List[str]												=      fill.copy().set_fill(__lowerCamelCase					,  opacity=0.8			)
              target.move_to(disk_left_col_base[i]			).scale(0.5			)
              cpu_disk_arr.append(__lowerCamelCase			)

          self.add(*__lowerCamelCase			)

          UpperCamelCase__:					Tuple												=      Square(side_length=2.2			)
          key.move_to([-5, 2, 0]			)

          UpperCamelCase__:					str												=      MarkupText(
              F"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"					,  font_size=18					,  )

          key_text.move_to([-5, 2.4, 0]			)

          self.add(__lowerCamelCase					,  __lowerCamelCase			)

          UpperCamelCase__:					str												=      MarkupText(
              F"<span fgcolor='{BLUE}'>●</span> Checkpoint"					,  font_size=18					,  )

          blue_text.next_to(__lowerCamelCase					,  DOWN * 2.4					,  aligned_edge=key_text.get_left()			)
          self.add(__lowerCamelCase			)

          UpperCamelCase__:					Dict												=      MarkupText(F"The offloaded weights are all sent to the CPU."					,  font_size=24			)
          step_a.move_to([2, 2, 0]			)

          self.play(Write(__lowerCamelCase					,  run_time=3			)			)

          for i in range(6			):
              UpperCamelCase__:					List[Any]												=      cpu_disk_arr[i]
              UpperCamelCase__:					Optional[int]												=      rect.copy().set_fill(__lowerCamelCase					,  opacity=0.8			).scale(2.0			)
              cpa.generate_target()
              cpa.target.move_to(model_base[i]			)

              if i == 0:
                  rect.set_fill(__lowerCamelCase					,  opacity=0.8			)
                  rect.generate_target()
                  rect.target.move_to(cpu_left_col_base[0]			).scale(2.0			)

                  self.remove(
                      *__lowerCamelCase					,  *__lowerCamelCase					,  )

              else:
                  rect.generate_target()
                  rect.target.move_to(cpu_left_col_base[i]			).scale(2.0			)
              self.play(MoveToTarget(__lowerCamelCase			)					,  MoveToTarget(__lowerCamelCase			)					,  model_base[i].animate.set_stroke(__lowerCamelCase			)			)
          self.play(FadeOut(__lowerCamelCase			)			)

          UpperCamelCase__:					Dict												=      MarkupText(
              F"Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed."					,  font_size=24					,  )
          step_a.move_to([2, 2, 0]			)

          self.play(Write(__lowerCamelCase					,  run_time=3			)			)

          UpperCamelCase__:					List[str]												=      []
          UpperCamelCase__:					Optional[Any]												=      []
          for i in range(6			):
              UpperCamelCase__:					List[str]												=      Arrow(start=__lowerCamelCase					,  end=__lowerCamelCase					,  color=__lowerCamelCase					,  buff=0.5			)
              a.next_to(model_base[i].get_left()					,  __lowerCamelCase					,  buff=0.2			)
              arrows.append(__lowerCamelCase			)
              animations.append(Write(__lowerCamelCase			)			)
          self.play(*__lowerCamelCase			)
          self.wait()

