




'''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     _a						:
       def __init__(			self		:			int     ,       lowercase		:			str     ,       lowercase		:			Union[str, Any]=13     ,       lowercase		:			Union[str, Any]=7     ,       lowercase		:			str=True     ,       lowercase		:			Tuple=True     ,       lowercase		:			int=True     ,       lowercase		:			str=True     ,       lowercase		:			Union[str, Any]=99     ,       lowercase		:			Optional[Any]=32     ,       lowercase		:			Dict=2     ,       lowercase		:			int=4     ,       lowercase		:			List[Any]=37     ,       lowercase		:			List[str]="gelu"     ,       lowercase		:			Union[str, Any]=0.1     ,       lowercase		:			List[Any]=0.1     ,       lowercase		:			List[Any]=512     ,       lowercase		:			Optional[Any]=16     ,       lowercase		:			Tuple=2     ,       lowercase		:			Optional[int]=0.02     ,       lowercase		:			int=3     ,       lowercase		:			Optional[Any]=4     ,       lowercase		:			List[str]=None     ,       ):



           '''simple docstring'''

           UpperCAmelCase     =	parent
           UpperCAmelCase     =	13
           UpperCAmelCase     =	7
           UpperCAmelCase     =	True
           UpperCAmelCase     =	True
           UpperCAmelCase     =	True
           UpperCAmelCase     =	True
           UpperCAmelCase     =	99
           UpperCAmelCase     =	384
           UpperCAmelCase     =	2
           UpperCAmelCase     =	4
           UpperCAmelCase     =	37
           UpperCAmelCase     =	'''gelu'''
           UpperCAmelCase     =	0.1
           UpperCAmelCase     =	0.1
           UpperCAmelCase     =	512
           UpperCAmelCase     =	16
           UpperCAmelCase     =	2
           UpperCAmelCase     =	0.02
           UpperCAmelCase     =	3
           UpperCAmelCase     =	4
           UpperCAmelCase     =	128
           UpperCAmelCase     =	2
           UpperCAmelCase     =	9
           UpperCAmelCase     =	1
           UpperCAmelCase     =	None
       def 					A   (			self		:			int       ):



           '''simple docstring'''

           UpperCAmelCase     =	ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size       )

           UpperCAmelCase     =	None
           if self.use_input_mask:
               UpperCAmelCase     =	random_attention_mask([self.batch_size, self.seq_length]       )

           UpperCAmelCase     =	None
           if self.use_token_type_ids:
               UpperCAmelCase     =	ids_tensor([self.batch_size, self.seq_length]     ,       self.type_vocab_size       )

           UpperCAmelCase     =	None
           UpperCAmelCase     =	None
           UpperCAmelCase     =	None
           if self.use_labels:
               UpperCAmelCase     =	ids_tensor([self.batch_size]     ,       self.type_sequence_label_size       )
               UpperCAmelCase     =	ids_tensor([self.batch_size, self.seq_length]     ,       self.num_labels       )
               UpperCAmelCase     =	ids_tensor([self.batch_size]     ,       self.num_choices       )

           UpperCAmelCase     =	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=lowercase     ,       )

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
       def 					A   (			self		:			Tuple     ,       lowercase		:			Optional[int]     ,       lowercase		:			Optional[int]     ,       lowercase		:			int     ,       lowercase		:			Any     ,       lowercase		:			str     ,       lowercase		:			str     ,       lowercase		:			Union[str, Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	TFConvBertModel(config=lowercase       )
           UpperCAmelCase     =	{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

           UpperCAmelCase     =	[input_ids, input_mask]
           UpperCAmelCase     =	model(lowercase       )

           UpperCAmelCase     =	model(lowercase       )

           self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)       )
       def 					A   (			self		:			Tuple     ,       lowercase		:			Dict     ,       lowercase		:			Optional[Any]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			Tuple     ,       lowercase		:			List[str]     ,       lowercase		:			int     ,       lowercase		:			Tuple       ):



           '''simple docstring'''

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



           '''simple docstring'''

           UpperCAmelCase     =	self.num_labels
           UpperCAmelCase     =	TFConvBertForSequenceClassification(config=lowercase       )
           UpperCAmelCase     =	{
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }

           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)       )
       def 					A   (			self		:			int     ,       lowercase		:			List[str]     ,       lowercase		:			List[str]     ,       lowercase		:			List[str]     ,       lowercase		:			Optional[int]     ,       lowercase		:			int     ,       lowercase		:			List[Any]     ,       lowercase		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.num_choices
           UpperCAmelCase     =	TFConvBertForMultipleChoice(config=lowercase       )
           UpperCAmelCase     =	tf.tile(tf.expand_dims(lowercase     ,       1       )     ,       (1, self.num_choices, 1)       )
           UpperCAmelCase     =	tf.tile(tf.expand_dims(lowercase     ,       1       )     ,       (1, self.num_choices, 1)       )
           UpperCAmelCase     =	tf.tile(tf.expand_dims(lowercase     ,       1       )     ,       (1, self.num_choices, 1)       )
           UpperCAmelCase     =	{
               '''input_ids''': multiple_choice_inputs_ids,
               '''attention_mask''': multiple_choice_input_mask,
               '''token_type_ids''': multiple_choice_token_type_ids,
           }
           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_choices)       )
       def 					A   (			self		:			int     ,       lowercase		:			Optional[int]     ,       lowercase		:			List[str]     ,       lowercase		:			int     ,       lowercase		:			Optional[Any]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			Optional[int]     ,       lowercase		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.num_labels
           UpperCAmelCase     =	TFConvBertForTokenClassification(config=lowercase       )
           UpperCAmelCase     =	{
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }
           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length, self.num_labels)       )
       def 					A   (			self		:			Tuple     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			int     ,       lowercase		:			Any     ,       lowercase		:			Optional[Any]     ,       lowercase		:			Optional[int]     ,       lowercase		:			Optional[Any]     ,       lowercase		:			Optional[int]       ):



           '''simple docstring'''

           UpperCAmelCase     =	TFConvBertForQuestionAnswering(config=lowercase       )
           UpperCAmelCase     =	{
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }

           UpperCAmelCase     =	model(lowercase       )
           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 					A   (			self		:			int       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.prepare_config_and_inputs()
           (
               (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  (
               UpperCAmelCase
           ) ,  
           )     =	config_and_inputs
           UpperCAmelCase     =	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict






@require_tf
class     _a						(			__a							,							__a							,							unittest.TestCase				):
       __a       :  str            =      (
           (
               TFConvBertModel,
               TFConvBertForMaskedLM,
               TFConvBertForQuestionAnswering,
               TFConvBertForSequenceClassification,
               TFConvBertForTokenClassification,
               TFConvBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       __a       :  Optional[int]            =      (
           {
               """feature-extraction""": TFConvBertModel,
               """fill-mask""": TFConvBertForMaskedLM,
               """question-answering""": TFConvBertForQuestionAnswering,
               """text-classification""": TFConvBertForSequenceClassification,
               """token-classification""": TFConvBertForTokenClassification,
               """zero-shot""": TFConvBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __a       :  int            =      False
       __a       :  Any            =      False
       __a       :  List[Any]            =      False
       def 					A   (			self		:			List[str]       ):



           '''simple docstring'''

           UpperCAmelCase     =	TFConvBertModelTester(self       )
           UpperCAmelCase     =	ConfigTester(self     ,       config_class=lowercase     ,       hidden_size=37       )
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase       )
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*lowercase       )
       def 					A   (			self		:			int       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*lowercase       )
       def 					A   (			self		:			int       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*lowercase       )
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*lowercase       )
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*lowercase       )
       @slow
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs_for_common()
           UpperCAmelCase     =	True
           UpperCAmelCase     =	True

           if hasattr(lowercase     ,       '''use_cache'''       ):
               UpperCAmelCase     =	True

           UpperCAmelCase     =	getattr(self.model_tester     ,       '''encoder_seq_length'''     ,       self.model_tester.seq_length       )
           UpperCAmelCase     =	getattr(self.model_tester     ,       '''key_length'''     ,       lowercase       )

           for model_class in self.all_model_classes:
               UpperCAmelCase     =	self._prepare_for_class(lowercase     ,       lowercase       )
               UpperCAmelCase     =	model_class(lowercase       )
               UpperCAmelCase     =	len(model(lowercase       )       )

               with tempfile.TemporaryDirectory() as tmpdirname:
                   model.save_pretrained(lowercase     ,       saved_model=lowercase       )
                   UpperCAmelCase     =	os.path.join(lowercase     ,       '''saved_model'''     ,       '''1'''       )
                   UpperCAmelCase     =	tf.keras.models.load_model(lowercase       )
                   UpperCAmelCase     =	model(lowercase       )

                   if self.is_encoder_decoder:
                       UpperCAmelCase     =	outputs['''encoder_hidden_states''']
                       UpperCAmelCase     =	outputs['''encoder_attentions''']
                   else:
                       UpperCAmelCase     =	outputs['''hidden_states''']
                       UpperCAmelCase     =	outputs['''attentions''']

                   self.assertEqual(len(lowercase       )     ,       lowercase       )

                   UpperCAmelCase     =	getattr(
                       self.model_tester     ,       '''expected_num_hidden_layers'''     ,       self.model_tester.num_hidden_layers + 1       )

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

                   self.assertEqual(len(lowercase       )     ,       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 					A   (			self		:			List[str]       ):



           '''simple docstring'''

           UpperCAmelCase     =	TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
           self.assertIsNotNone(lowercase       )







       def 					A   (			self		:			List[str]       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs_for_common()
           UpperCAmelCase     =	True
           UpperCAmelCase     =	getattr(self.model_tester     ,       '''decoder_seq_length'''     ,       self.model_tester.seq_length       )
           UpperCAmelCase     =	getattr(self.model_tester     ,       '''encoder_seq_length'''     ,       self.model_tester.seq_length       )
           UpperCAmelCase     =	getattr(self.model_tester     ,       '''key_length'''     ,       lowercase       )
           UpperCAmelCase     =	getattr(self.model_tester     ,       '''key_length'''     ,       lowercase       )

           def check_decoder_attentions_output(lowercase		:			int       ):
               UpperCAmelCase     =	len(lowercase       )
               self.assertEqual(out_len % 2     ,       0       )
               UpperCAmelCase     =	outputs.decoder_attentions
               self.assertEqual(len(lowercase       )     ,       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(lowercase		:			List[str]       ):
               UpperCAmelCase     =	[
                   t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
               ]
               self.assertEqual(len(lowercase       )     ,       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:
               UpperCAmelCase     =	True
               UpperCAmelCase     =	False
               UpperCAmelCase     =	model_class(lowercase       )
               UpperCAmelCase     =	model(self._prepare_for_class(lowercase     ,       lowercase       )       )
               UpperCAmelCase     =	len(lowercase       )
               self.assertEqual(config.output_hidden_states     ,       lowercase       )
               check_encoder_attentions_output(lowercase       )

               if self.is_encoder_decoder:
                   UpperCAmelCase     =	model_class(lowercase       )
                   UpperCAmelCase     =	model(self._prepare_for_class(lowercase     ,       lowercase       )       )
                   self.assertEqual(config.output_hidden_states     ,       lowercase       )
                   check_decoder_attentions_output(lowercase       )

               # Check that output attentions can also be changed via the config
               del inputs_dict["output_attentions"]
               UpperCAmelCase     =	True
               UpperCAmelCase     =	model_class(lowercase       )
               UpperCAmelCase     =	model(self._prepare_for_class(lowercase     ,       lowercase       )       )
               self.assertEqual(config.output_hidden_states     ,       lowercase       )
               check_encoder_attentions_output(lowercase       )

               # Check attention is always last and order is fine
               UpperCAmelCase     =	True
               UpperCAmelCase     =	True
               UpperCAmelCase     =	model_class(lowercase       )
               UpperCAmelCase     =	model(self._prepare_for_class(lowercase     ,       lowercase       )       )

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






@require_tf
class     _a						(			unittest.TestCase				):
       @slow
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
           UpperCAmelCase     =	tf.constant([[0, 1, 2, 3, 4, 5]]       )
           UpperCAmelCase     =	model(lowercase       )[0]

           UpperCAmelCase     =	[1, 6, 768]
           self.assertEqual(output.shape     ,       lowercase       )

           UpperCAmelCase     =	tf.constant(
               [
                   [
                       [-0.0347_5493, -0.468_6034, -0.3063_8832],
                       [0.2263_7248, -0.2698_8646, -0.742_3424],
                       [0.1032_4868, -0.4501_3508, -0.5828_0784],
                   ]
               ]       )
           tf.debugging.assert_near(output[:, :3, :3]     ,       lowercase     ,       atol=1E-4       )










'''simple docstring'''







import math
from typing import Optional

import numpy as np

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A									=logging.get_logger(__name__)

A									={
    'facebook/encodec_24khz': 'https://huggingface.co/facebook/encodec_24khz/resolve/main/config.json',
    'facebook/encodec_48khz': 'https://huggingface.co/facebook/encodec_48khz/resolve/main/config.json',
}


class     _a						(			__a				):
       __a       :  Union[str, Any]            =      """encodec"""
       def __init__(			self		:			Optional[int]     ,       lowercase		:			Union[str, Any]=[1.5, 3.0, 6.0, 12.0, 24.0]     ,       lowercase		:			str=24_000     ,       lowercase		:			int=1     ,       lowercase		:			int=False     ,       lowercase		:			List[str]=None     ,       lowercase		:			Tuple=None     ,       lowercase		:			int=128     ,       lowercase		:			List[Any]=32     ,       lowercase		:			Optional[int]=1     ,       lowercase		:			Optional[int]=[8, 5, 4, 2]     ,       lowercase		:			Union[str, Any]="weight_norm"     ,       lowercase		:			Optional[int]=7     ,       lowercase		:			Tuple=7     ,       lowercase		:			Tuple=3     ,       lowercase		:			Union[str, Any]=2     ,       lowercase		:			Optional[Any]=True     ,       lowercase		:			Tuple="reflect"     ,       lowercase		:			Any=2     ,       lowercase		:			List[str]=2     ,       lowercase		:			str=1.0     ,       lowercase		:			List[str]=1_024     ,       lowercase		:			Optional[Any]=None     ,       lowercase		:			Any=True     ,       **lowercase		:			Optional[Any]     ,       ):



           '''simple docstring'''

           UpperCAmelCase     =	target_bandwidths
           UpperCAmelCase     =	sampling_rate
           UpperCAmelCase     =	audio_channels
           UpperCAmelCase     =	normalize
           UpperCAmelCase     =	chunk_length_s
           UpperCAmelCase     =	overlap
           UpperCAmelCase     =	hidden_size
           UpperCAmelCase     =	num_filters
           UpperCAmelCase     =	num_residual_layers
           UpperCAmelCase     =	upsampling_ratios
           UpperCAmelCase     =	norm_type
           UpperCAmelCase     =	kernel_size
           UpperCAmelCase     =	last_kernel_size
           UpperCAmelCase     =	residual_kernel_size
           UpperCAmelCase     =	dilation_growth_rate
           UpperCAmelCase     =	use_causal_conv
           UpperCAmelCase     =	pad_mode
           UpperCAmelCase     =	compress
           UpperCAmelCase     =	num_lstm_layers
           UpperCAmelCase     =	trim_right_ratio
           UpperCAmelCase     =	codebook_size
           UpperCAmelCase     =	codebook_dim if codebook_dim is not None else hidden_size
           UpperCAmelCase     =	use_conv_shortcut

           if self.norm_type not in ["weight_norm", "time_group_norm"]:
               raise ValueError(
                   f"self.norm_type must be one of `\"weight_norm\"`, `\"time_group_norm\"`), got {self.norm_type}"       )

           super().__init__(**lowercase       )
       @property
       def 					A   (			self		:			List[str]       ):



           '''simple docstring'''

           if self.chunk_length_s is None:
               return None
           else:
               return int(self.chunk_length_s * self.sampling_rate       )
       @property
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           if self.chunk_length_s is None or self.overlap is None:
               return None
           else:
               return max(1     ,       int((1.0 - self.overlap) * self.chunk_length       )       )
       @property
       def 					A   (			self		:			Union[str, Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	np.prod(self.upsampling_ratios       )
           return math.ceil(self.sampling_rate / hop_length       )







       @property
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           return int(1_000 * self.target_bandwidths[-1] // (self.frame_rate * 10)       )




