



'''simple docstring'''





from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 (
       TFConvBertForMaskedLM,
       TFConvBertForMultipleChoice,
       TFConvBertForQuestionAnswering,
       TFConvBertForSequenceClassification,
       TFConvBertForTokenClassification,
       TFConvBertModel,
   )






class 					lowerCamelCase_    :
  """simple docstring"""




  def __init__( self      :	Optional[Any]			,	_a      :	Dict			,	_a      :	str=13			,	_a      :	Any=7			,	_a      :	str=True			,	_a      :	Any=True			,	_a      :	Tuple=True			,	_a      :	Any=True			,	_a      :	str=99			,	_a      :	Optional[int]=32			,	_a      :	Dict=2			,	_a      :	Optional[Any]=4			,	_a      :	List[Any]=37			,	_a      :	Optional[int]="gelu"			,	_a      :	List[str]=0.1			,	_a      :	Union[str, Any]=0.1			,	_a      :	Optional[int]=512			,	_a      :	List[Any]=16			,	_a      :	Optional[int]=2			,	_a      :	Optional[int]=0.02			,	_a      :	List[str]=3			,	_a      :	Optional[Any]=4			,	_a      :	Any=None			,	)					->					Optional[int]:
    __lowerCamelCase       :       List[str]           = parent
    __lowerCamelCase       :       Dict           = 13
    __lowerCamelCase       :       str           = 7
    __lowerCamelCase       :       List[str]           = True
    __lowerCamelCase       :       List[Any]           = True
    __lowerCamelCase       :       List[str]           = True
    __lowerCamelCase       :       Any           = True
    __lowerCamelCase       :       Any           = 99
    __lowerCamelCase       :       List[Any]           = 384
    __lowerCamelCase       :       Optional[int]           = 2
    __lowerCamelCase       :       Tuple           = 4
    __lowerCamelCase       :       Dict           = 37
    __lowerCamelCase       :       Any           = 'gelu'
    __lowerCamelCase       :       Optional[Any]           = 0.1
    __lowerCamelCase       :       str           = 0.1
    __lowerCamelCase       :       Any           = 512
    __lowerCamelCase       :       Any           = 16
    __lowerCamelCase       :       str           = 2
    __lowerCamelCase       :       List[Any]           = 0.02
    __lowerCamelCase       :       List[Any]           = 3
    __lowerCamelCase       :       Optional[Any]           = 4
    __lowerCamelCase       :       List[Any]           = 128
    __lowerCamelCase       :       Union[str, Any]           = 2
    __lowerCamelCase       :       Optional[int]           = 9
    __lowerCamelCase       :       Any           = 1
    __lowerCamelCase       :       Any           = None




  def 						_lowercase     ( self      :	Union[str, Any]   )					->					Dict:
    __lowerCamelCase       :       Dict           = ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size   )

    __lowerCamelCase       :       Union[str, Any]           = None
    if self.use_input_mask:
      __lowerCamelCase       :       Optional[int]           = random_attention_mask([self.batch_size, self.seq_length]   )

    __lowerCamelCase       :       Union[str, Any]           = None
    if self.use_token_type_ids:
      __lowerCamelCase       :       Dict           = ids_tensor([self.batch_size, self.seq_length]			,	self.type_vocab_size   )

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

    __lowerCamelCase       :       Tuple           = ConvBertConfig(
        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			,	initializer_range=self.initializer_range			,	return_dict=_a			,	)

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




  def 						_lowercase     ( self      :	int			,	_a      :	Tuple			,	_a      :	Optional[Any]			,	_a      :	int			,	_a      :	List[Any]			,	_a      :	int			,	_a      :	Any			,	_a      :	int   )					->					Any:
    __lowerCamelCase       :       Union[str, Any]           = TFConvBertModel(config=_a   )
    __lowerCamelCase       :       List[str]           = {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

    __lowerCamelCase       :       Dict           = [input_ids, input_mask]
    __lowerCamelCase       :       Tuple           = model(_a   )

    __lowerCamelCase       :       Union[str, Any]           = model(_a   )

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




  def 						_lowercase     ( self      :	Union[str, Any]			,	_a      :	Optional[Any]			,	_a      :	str			,	_a      :	Tuple			,	_a      :	str			,	_a      :	List[Any]			,	_a      :	List[str]			,	_a      :	int   )					->					Optional[Any]:
    __lowerCamelCase       :       List[str]           = TFConvBertForMaskedLM(config=_a   )
    __lowerCamelCase       :       List[Any]           = {
        'input_ids': input_ids,
        'attention_mask': input_mask,
        'token_type_ids': token_type_ids,
    }
    __lowerCamelCase       :       List[str]           = model(_a   )
    self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.seq_length, self.vocab_size)   )




  def 						_lowercase     ( self      :	Optional[Any]			,	_a      :	List[Any]			,	_a      :	str			,	_a      :	List[Any]			,	_a      :	Dict			,	_a      :	Any			,	_a      :	Optional[int]			,	_a      :	Tuple   )					->					str:
    __lowerCamelCase       :       Tuple           = self.num_labels
    __lowerCamelCase       :       List[Any]           = TFConvBertForSequenceClassification(config=_a   )
    __lowerCamelCase       :       List[str]           = {
        'input_ids': input_ids,
        'attention_mask': input_mask,
        'token_type_ids': token_type_ids,
    }

    __lowerCamelCase       :       Tuple           = model(_a   )
    self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.num_labels)   )




  def 						_lowercase     ( self      :	Any			,	_a      :	Tuple			,	_a      :	str			,	_a      :	str			,	_a      :	List[Any]			,	_a      :	Optional[Any]			,	_a      :	Dict			,	_a      :	List[Any]   )					->					str:
    __lowerCamelCase       :       Optional[Any]           = self.num_choices
    __lowerCamelCase       :       Optional[int]           = TFConvBertForMultipleChoice(config=_a   )
    __lowerCamelCase       :       List[Any]           = tf.tile(tf.expand_dims(_a			,	1   )			,	(1, self.num_choices, 1)   )
    __lowerCamelCase       :       Optional[int]           = tf.tile(tf.expand_dims(_a			,	1   )			,	(1, self.num_choices, 1)   )
    __lowerCamelCase       :       List[str]           = tf.tile(tf.expand_dims(_a			,	1   )			,	(1, self.num_choices, 1)   )
    __lowerCamelCase       :       List[str]           = {
        'input_ids': multiple_choice_inputs_ids,
        'attention_mask': multiple_choice_input_mask,
        'token_type_ids': multiple_choice_token_type_ids,
    }
    __lowerCamelCase       :       List[str]           = model(_a   )
    self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.num_choices)   )




  def 						_lowercase     ( self      :	int			,	_a      :	str			,	_a      :	Union[str, Any]			,	_a      :	Optional[int]			,	_a      :	List[str]			,	_a      :	Dict			,	_a      :	Tuple			,	_a      :	Dict   )					->					List[Any]:
    __lowerCamelCase       :       Optional[Any]           = self.num_labels
    __lowerCamelCase       :       Tuple           = TFConvBertForTokenClassification(config=_a   )
    __lowerCamelCase       :       Any           = {
        'input_ids': input_ids,
        'attention_mask': input_mask,
        'token_type_ids': token_type_ids,
    }
    __lowerCamelCase       :       Optional[int]           = model(_a   )
    self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.seq_length, self.num_labels)   )




  def 						_lowercase     ( self      :	Optional[Any]			,	_a      :	Optional[Any]			,	_a      :	Tuple			,	_a      :	str			,	_a      :	Optional[Any]			,	_a      :	Tuple			,	_a      :	str			,	_a      :	Tuple   )					->					List[Any]:
    __lowerCamelCase       :       str           = TFConvBertForQuestionAnswering(config=_a   )
    __lowerCamelCase       :       List[Any]           = {
        'input_ids': input_ids,
        'attention_mask': input_mask,
        'token_type_ids': token_type_ids,
    }

    __lowerCamelCase       :       List[str]           = model(_a   )
    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 						_lowercase     ( self      :	List[str]   )					->					int:
    __lowerCamelCase       :       Dict           = self.prepare_config_and_inputs()
    (
        (
        __lowerCamelCase
    )       ,(
        __lowerCamelCase
    )       ,(
        __lowerCamelCase
    )       ,(
        __lowerCamelCase
    )       ,(
        __lowerCamelCase
    )       ,(
        __lowerCamelCase
    )       ,(
        __lowerCamelCase
    )       ,
    )       :       Any           = config_and_inputs
    __lowerCamelCase       :       Dict           = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
    return config, inputs_dict






@require_tf
class 					lowerCamelCase_    (				SCREAMING_SNAKE_CASE_   ,   SCREAMING_SNAKE_CASE_   ,   unittest.TestCase     ):
  """simple docstring"""




  a_												=(
      (
          TFConvBertModel,
          TFConvBertForMaskedLM,
          TFConvBertForQuestionAnswering,
          TFConvBertForSequenceClassification,
          TFConvBertForTokenClassification,
          TFConvBertForMultipleChoice,
      )
      if is_tf_available()
      else ()
  )
  a_												=(
      {
          """feature-extraction""": TFConvBertModel,
          """fill-mask""": TFConvBertForMaskedLM,
          """question-answering""": TFConvBertForQuestionAnswering,
          """text-classification""": TFConvBertForSequenceClassification,
          """token-classification""": TFConvBertForTokenClassification,
          """zero-shot""": TFConvBertForSequenceClassification,
      }
      if is_tf_available()
      else {}
  )
  a_												=False
  a_												=False
  a_												=False




  def 						_lowercase     ( self      :	Union[str, Any]   )					->					List[str]:
    __lowerCamelCase       :       Optional[Any]           = TFConvBertModelTester(self   )
    __lowerCamelCase       :       List[Any]           = ConfigTester(self			,	config_class=_a			,	hidden_size=37   )




  def 						_lowercase     ( self      :	List[str]   )					->					List[str]:
    self.config_tester.run_common_tests()




  def 						_lowercase     ( self      :	Optional[Any]   )					->					Tuple:
    __lowerCamelCase       :       List[str]           = self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_a   )




  def 						_lowercase     ( self      :	Union[str, Any]   )					->					int:
    __lowerCamelCase       :       str           = self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_masked_lm(*_a   )




  def 						_lowercase     ( self      :	Optional[int]   )					->					Tuple:
    __lowerCamelCase       :       List[Any]           = self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_multiple_choice(*_a   )




  def 						_lowercase     ( self      :	Dict   )					->					str:
    __lowerCamelCase       :       Optional[int]           = self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*_a   )




  def 						_lowercase     ( self      :	List[str]   )					->					List[Any]:
    __lowerCamelCase       :       int           = self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*_a   )




  def 						_lowercase     ( self      :	int   )					->					int:
    __lowerCamelCase       :       Dict           = self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*_a   )




  @slow
  def 						_lowercase     ( self      :	Tuple   )					->					List[str]:
    __lowerCamelCase       ,__lowerCamelCase       :       List[Any]           = self.model_tester.prepare_config_and_inputs_for_common()
    __lowerCamelCase       :       List[str]           = True
    __lowerCamelCase       :       List[str]           = True

    if hasattr(_a			,	'use_cache'   ):
      __lowerCamelCase       :       List[Any]           = True

    __lowerCamelCase       :       str           = getattr(self.model_tester			,	'encoder_seq_length'			,	self.model_tester.seq_length   )
    __lowerCamelCase       :       Optional[Any]           = getattr(self.model_tester			,	'key_length'			,	_a   )

    for model_class in self.all_model_classes:
      __lowerCamelCase       :       Optional[Any]           = self._prepare_for_class(_a			,	_a   )
      __lowerCamelCase       :       Union[str, Any]           = model_class(_a   )
      __lowerCamelCase       :       Optional[int]           = len(model(_a   )   )

      with tempfile.TemporaryDirectory() as tmpdirname:
        model.save_pretrained(_a			,	saved_model=_a   )
        __lowerCamelCase       :       Dict           = os.path.join(_a			,	'saved_model'			,	'1'   )
        __lowerCamelCase       :       int           = tf.keras.models.load_model(_a   )
        __lowerCamelCase       :       List[str]           = model(_a   )

        if self.is_encoder_decoder:
          __lowerCamelCase       :       Union[str, Any]           = outputs['encoder_hidden_states']
          __lowerCamelCase       :       Dict           = outputs['encoder_attentions']
        else:
          __lowerCamelCase       :       Tuple           = outputs['hidden_states']
          __lowerCamelCase       :       int           = outputs['attentions']

        self.assertEqual(len(_a   )			,	_a   )

        __lowerCamelCase       :       List[Any]           = getattr(
            self.model_tester			,	'expected_num_hidden_layers'			,	self.model_tester.num_hidden_layers + 1   )

        self.assertEqual(len(_a   )			,	_a   )
        self.assertListEqual(
            list(output_hidden_states[0].shape[-2:]   )			,	[self.model_tester.seq_length, self.model_tester.hidden_size]			,	)

        self.assertEqual(len(_a   )			,	self.model_tester.num_hidden_layers   )
        self.assertListEqual(
            list(output_attentions[0].shape[-3:]   )			,	[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]			,	)




  @slow
  def 						_lowercase     ( self      :	Optional[int]   )					->					Any:
    __lowerCamelCase       :       List[str]           = TFConvBertModel.from_pretrained('YituTech/conv-bert-base'   )
    self.assertIsNotNone(_a   )




  def 						_lowercase     ( self      :	int   )					->					Optional[int]:
    __lowerCamelCase       ,__lowerCamelCase       :       List[str]           = self.model_tester.prepare_config_and_inputs_for_common()
    __lowerCamelCase       :       Dict           = True
    __lowerCamelCase       :       str           = getattr(self.model_tester			,	'decoder_seq_length'			,	self.model_tester.seq_length   )
    __lowerCamelCase       :       List[str]           = getattr(self.model_tester			,	'encoder_seq_length'			,	self.model_tester.seq_length   )
    __lowerCamelCase       :       int           = getattr(self.model_tester			,	'key_length'			,	_a   )
    __lowerCamelCase       :       str           = getattr(self.model_tester			,	'key_length'			,	_a   )

    def check_decoder_attentions_output(_a      :	Optional[int]   ):
      __lowerCamelCase       :       int           = len(_a   )
      self.assertEqual(out_len % 2			,	0   )
      __lowerCamelCase       :       Optional[Any]           = outputs.decoder_attentions
      self.assertEqual(len(_a   )			,	self.model_tester.num_hidden_layers   )
      self.assertListEqual(
          list(decoder_attentions[0].shape[-3:]   )			,	[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]			,	)

    def check_encoder_attentions_output(_a      :	int   ):
      __lowerCamelCase       :       Any           = [
          t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
      ]
      self.assertEqual(len(_a   )			,	self.model_tester.num_hidden_layers   )
      self.assertListEqual(
          list(attentions[0].shape[-3:]   )			,	[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]			,	)

    for model_class in self.all_model_classes:
      __lowerCamelCase       :       Tuple           = True
      __lowerCamelCase       :       Optional[Any]           = False
      __lowerCamelCase       :       Optional[int]           = model_class(_a   )
      __lowerCamelCase       :       Optional[int]           = model(self._prepare_for_class(_a			,	_a   )   )
      __lowerCamelCase       :       Union[str, Any]           = len(_a   )
      self.assertEqual(config.output_hidden_states			,	_a   )
      check_encoder_attentions_output(_a   )

      if self.is_encoder_decoder:
        __lowerCamelCase       :       List[str]           = model_class(_a   )
        __lowerCamelCase       :       List[Any]           = model(self._prepare_for_class(_a			,	_a   )   )
        self.assertEqual(config.output_hidden_states			,	_a   )
        check_decoder_attentions_output(_a   )

      # Check that output attentions can also be changed via the config
      del inputs_dict["output_attentions"]
      __lowerCamelCase       :       List[str]           = True
      __lowerCamelCase       :       Tuple           = model_class(_a   )
      __lowerCamelCase       :       int           = model(self._prepare_for_class(_a			,	_a   )   )
      self.assertEqual(config.output_hidden_states			,	_a   )
      check_encoder_attentions_output(_a   )

      # Check attention is always last and order is fine
      __lowerCamelCase       :       Tuple           = True
      __lowerCamelCase       :       int           = True
      __lowerCamelCase       :       int           = model_class(_a   )
      __lowerCamelCase       :       int           = model(self._prepare_for_class(_a			,	_a   )   )

      self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)			,	len(_a   )   )
      self.assertEqual(model.config.output_hidden_states			,	_a   )
      check_encoder_attentions_output(_a   )






@require_tf
class 					lowerCamelCase_    (				unittest.TestCase     ):
  """simple docstring"""




  @slow
  def 						_lowercase     ( self      :	Dict   )					->					Any:
    __lowerCamelCase       :       List[Any]           = TFConvBertModel.from_pretrained('YituTech/conv-bert-base'   )
    __lowerCamelCase       :       Optional[Any]           = tf.constant([[0, 1, 2, 3, 4, 5]]   )
    __lowerCamelCase       :       int           = model(_a   )[0]

    __lowerCamelCase       :       Dict           = [1, 6, 768]
    self.assertEqual(output.shape			,	_a   )

    __lowerCamelCase       :       str           = tf.constant(
        [
            [
                [-0.03475493, -0.4686034, -0.30638832],
                [0.22637248, -0.26988646, -0.7423424],
                [0.10324868, -0.45013508, -0.58280784],
            ]
        ]   )
    tf.debugging.assert_near(output[:, :3, :3]			,	_a			,	atol=1e-4   )











'''simple docstring'''





from collections.abc import Sequence

def        a_     (							_lowerCAmelCase       ,_lowerCAmelCase       )      ->      float:
  return sum(c * (x**i) for i, c in enumerate(_lowerCAmelCase       )       )

def        a_     (							_lowerCAmelCase       ,_lowerCAmelCase       )      ->      float:
  __lowerCamelCase       :       Any           = 0.0
  for coeff in reversed(_lowerCAmelCase       ):
    __lowerCamelCase       :       Tuple           = result * x + coeff
  return result


if __name__ == "__main__":
   _UpperCamelCase											=						(0.0, 0.0, 5.0, 9.3, 7.0)
   _UpperCamelCase											=						1_0.0
   print(evaluate_poly(poly, x))
   print(horner(poly, x))






