






"""simple docstring"""







import itertools
import random
import unittest

import numpy as np

from transformers import EncodecFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
  import torch


_UpperCamelCase     			=  random.Random()

def _a (		_snake_case      ,       _snake_case=1.0      ,       _snake_case=None      ,       _snake_case=None							):







      """simple docstring"""






      if rng is None:
            UpperCAmelCase							=       global_rng

      UpperCAmelCase							=       []
      for batch_idx in range(shape[0]							):
            values.append([]							)
            for _ in range(shape[1]							):
                  values[-1].append(rng.random() * scale							)

      return values







@require_torch
class 					lowerCamelCase__					(       unittest.TestCase ):






    def __init__( self	,A	,A=7	,A=400	,A=2_000	,A=1	,A=0.0	,A=24_000	,A=True	,):
          UpperCAmelCase							=       parent
          UpperCAmelCase							=       batch_size
          UpperCAmelCase							=       min_seq_length
          UpperCAmelCase							=       max_seq_length
          UpperCAmelCase							=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
          UpperCAmelCase							=       feature_size
          UpperCAmelCase							=       padding_value
          UpperCAmelCase							=       sampling_rate
          UpperCAmelCase							=       return_attention_mask






    def 				_UpperCamelCase							( self					):
          return {
              "feature_size": self.feature_size,
              "padding_value": self.padding_value,
              "sampling_rate": self.sampling_rate,
              "return_attention_mask": self.return_attention_mask,
          }






    def 				_UpperCamelCase							( self	,A=False	,A=False					):
          def _flatten(A					):
                return list(itertools.chain(*A					)					)

          if equal_length:
                UpperCAmelCase							=       floats_list((self.batch_size, self.max_seq_length)					)
          else:
                # make sure that inputs increase in size
                UpperCAmelCase							=       [
                    _flatten(floats_list((x, self.feature_size)					)					)
                    for x in range(self.min_seq_length	,self.max_seq_length	,self.seq_length_diff					)
                ]

          if numpify:
                UpperCAmelCase							=       [np.asarray(A					) for x in audio_inputs]

          return audio_inputs







@require_torch
class 					lowerCamelCase__					(       snake_case    ,     unittest.TestCase ):
    SCREAMING_SNAKE_CASE		=			EncodecFeatureExtractor






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       EnCodecFeatureExtractionTester(self					)






    def 				_UpperCamelCase							( self					):
          # Tests that all call wrap to encode_plus and batch_encode_plus
          UpperCAmelCase							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          # create three inputs of length 800, 1000, and 1200
          UpperCAmelCase							=       [floats_list((1, x)					)[0] for x in range(800	,1_400	,200					)]
          UpperCAmelCase							=       [np.asarray(A					) for audio_input in audio_inputs]

          # Test not batched input
          UpperCAmelCase							=       feat_extract(audio_inputs[0]	,return_tensors="""np"""					).input_values
          UpperCAmelCase							=       feat_extract(np_audio_inputs[0]	,return_tensors="""np"""					).input_values
          self.assertTrue(np.allclose(A	,A	,atol=1e-3					)					)

          # Test batched
          UpperCAmelCase							=       feat_extract(A	,padding=A	,return_tensors="""np"""					).input_values
          UpperCAmelCase							=       feat_extract(A	,padding=A	,return_tensors="""np"""					).input_values
          for enc_seq_a, enc_seq_a in zip(A	,A					):
                self.assertTrue(np.allclose(A	,A	,atol=1e-3					)					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
          UpperCAmelCase							=       np.random.rand(100					).astype(np.floataa					)
          UpperCAmelCase							=       np_audio_inputs.tolist()

          for inputs in [py_audio_inputs, np_audio_inputs]:
                UpperCAmelCase							=       feature_extractor.pad([{"""input_values""": inputs}]	,return_tensors="""np"""					)
                self.assertTrue(np_processed.input_values.dtype == np.floataa					)
                UpperCAmelCase							=       feature_extractor.pad([{"""input_values""": inputs}]	,return_tensors="""pt"""					)
                self.assertTrue(pt_processed.input_values.dtype == torch.floataa					)






    def 				_UpperCamelCase							( self	,A					):
          from datasets import load_dataset

          UpperCAmelCase							=       load_dataset("""hf-internal-testing/librispeech_asr_dummy"""	,"""clean"""	,split="""validation"""					)
          # automatic decoding with librispeech
          UpperCAmelCase							=       ds.sort("""id"""					).select(range(A					)					)[:num_samples]["""audio"""]

          return [x["array"] for x in audio_samples]






    def 				_UpperCamelCase							( self					):
          # fmt: off
          UpperCAmelCase							=       torch.tensor(
              [2.3_8_0_4e-0_3, 2.0_7_5_2e-0_3, 1.9_8_3_6e-0_3, 2.1_0_5_7e-0_3, 1.6_1_7_4e-0_3,
               3.0_5_1_8e-0_4, 9.1_5_5_3e-0_5, 3.3_5_6_9e-0_4, 9.7_6_5_6e-0_4, 1.8_3_1_1e-0_3,
               2.0_1_4_2e-0_3, 2.1_0_5_7e-0_3, 1.7_3_9_5e-0_3, 4.5_7_7_6e-0_4, -3.9_6_7_3e-0_4,
               4.5_7_7_6e-0_4, 1.0_0_7_1e-0_3, 9.1_5_5_3e-0_5, 4.8_8_2_8e-0_4, 1.1_5_9_7e-0_3,
               7.3_2_4_2e-0_4, 9.4_6_0_4e-0_4, 1.8_0_0_5e-0_3, 1.8_3_1_1e-0_3, 8.8_5_0_1e-0_4,
               4.2_7_2_5e-0_4, 4.8_8_2_8e-0_4, 7.3_2_4_2e-0_4, 1.0_9_8_6e-0_3, 2.1_0_5_7e-0_3]					)
          # fmt: on
          UpperCAmelCase							=       self._load_datasamples(1					)
          UpperCAmelCase							=       EncodecFeatureExtractor()
          UpperCAmelCase							=       feature_extractor(A	,return_tensors="""pt"""					).input_values
          self.assertEquals(input_values.shape	,(1, 1, 93_680)					)
          self.assertTrue(torch.allclose(input_values[0, 0, :30]	,A	,atol=1e-6					)					)






    def 				_UpperCamelCase							( self					):
          # fmt: off
          UpperCAmelCase							=       torch.tensor(
              [2.3_8_0_4e-0_3, 2.0_7_5_2e-0_3, 1.9_8_3_6e-0_3, 2.1_0_5_7e-0_3, 1.6_1_7_4e-0_3,
               3.0_5_1_8e-0_4, 9.1_5_5_3e-0_5, 3.3_5_6_9e-0_4, 9.7_6_5_6e-0_4, 1.8_3_1_1e-0_3,
               2.0_1_4_2e-0_3, 2.1_0_5_7e-0_3, 1.7_3_9_5e-0_3, 4.5_7_7_6e-0_4, -3.9_6_7_3e-0_4,
               4.5_7_7_6e-0_4, 1.0_0_7_1e-0_3, 9.1_5_5_3e-0_5, 4.8_8_2_8e-0_4, 1.1_5_9_7e-0_3,
               7.3_2_4_2e-0_4, 9.4_6_0_4e-0_4, 1.8_0_0_5e-0_3, 1.8_3_1_1e-0_3, 8.8_5_0_1e-0_4,
               4.2_7_2_5e-0_4, 4.8_8_2_8e-0_4, 7.3_2_4_2e-0_4, 1.0_9_8_6e-0_3, 2.1_0_5_7e-0_3]					)
          # fmt: on
          UpperCAmelCase							=       self._load_datasamples(1					)
          UpperCAmelCase							=       [np.tile(input_audio[0][None]	,reps=(2, 1)					)]
          input_audio[0][1] *= 0.5
          UpperCAmelCase							=       EncodecFeatureExtractor(feature_size=2					)
          UpperCAmelCase							=       feature_extractor(A	,return_tensors="""pt"""					).input_values
          self.assertEquals(input_values.shape	,(1, 2, 93_680)					)
          self.assertTrue(torch.allclose(input_values[0, 0, :30]	,A	,atol=1e-6					)					)
          self.assertTrue(torch.allclose(input_values[0, 1, :30]	,EXPECTED_INPUT_VALUES * 0.5	,atol=1e-6					)					)






    def 				_UpperCamelCase							( self					):
          UpperCAmelCase							=       self._load_datasamples(2					)
          # would be easier if the stride was like
          UpperCAmelCase							=       EncodecFeatureExtractor(feature_size=1	,chunk_length_s=1	,overlap=0.01					)

          # pad and trunc raise an error ?
          with self.assertRaisesRegex(
              A	,"""^Both padding and truncation were set. Make sure you only set one.$"""	,):
                UpperCAmelCase							=       feature_extractor(
                    A	,padding="""max_length"""	,truncation=A	,return_tensors="""pt"""					).input_values

          # truncate to chunk
          UpperCAmelCase							=       feature_extractor(A	,truncation=A	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(2, 1, 71_520)					)  # 2 chunks

          # force truncate to max_length
          UpperCAmelCase							=       feature_extractor(
              A	,truncation=A	,max_length=48_000	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(2, 1, 48_000)					)

          # pad to chunk
          UpperCAmelCase							=       feature_extractor(A	,padding=A	,return_tensors="""pt"""					).input_values
          self.assertEquals(padded_outputs.shape	,(2, 1, 95_280)					)

          # pad to chunk
          UpperCAmelCase							=       feature_extractor(A	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(2, 1, 95_280)					)

          # force pad to max length
          UpperCAmelCase							=       feature_extractor(
              A	,padding="""max_length"""	,max_length=100_000	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(2, 1, 100_000)					)

          # force no pad
          with self.assertRaisesRegex(
              A	,"""^Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.$"""	,):
                UpperCAmelCase							=       feature_extractor(A	,padding=A	,return_tensors="""pt"""					).input_values

          UpperCAmelCase							=       feature_extractor(input_audio[0]	,padding=A	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(1, 1, 93_680)					)

          # no pad if no chunk_length_s
          UpperCAmelCase							=       None
          with self.assertRaisesRegex(
              A	,"""^Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.$"""	,):
                UpperCAmelCase							=       feature_extractor(A	,padding=A	,return_tensors="""pt"""					).input_values

          UpperCAmelCase							=       feature_extractor(input_audio[0]	,padding=A	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(1, 1, 93_680)					)

          # no pad if no overlap
          UpperCAmelCase							=       2
          UpperCAmelCase							=       None
          with self.assertRaisesRegex(
              A	,"""^Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.$"""	,):
                UpperCAmelCase							=       feature_extractor(A	,padding=A	,return_tensors="""pt"""					).input_values

          UpperCAmelCase							=       feature_extractor(input_audio[0]	,padding=A	,return_tensors="""pt"""					).input_values
          self.assertEquals(truncated_outputs.shape	,(1, 1, 93_680)					)











"""simple docstring"""







from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


_UpperCamelCase     			=  logging.get_logger(__name__)

_UpperCamelCase     			=  {
    """facebook/mbart-large-cc25""": """https://huggingface.co/facebook/mbart-large-cc25/resolve/main/config.json""",
    # See all MBART models at https://huggingface.co/models?filter=mbart
}







class 					lowerCamelCase__					(       snake_case ):
    SCREAMING_SNAKE_CASE		=			'''mbart'''
    SCREAMING_SNAKE_CASE		=			['''past_key_values''']
    SCREAMING_SNAKE_CASE		=			{'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






    def __init__( self	,A=50_265	,A=1_024	,A=12	,A=4_096	,A=16	,A=12	,A=4_096	,A=16	,A=0.0	,A=0.0	,A=True	,A=True	,A="gelu"	,A=1_024	,A=0.1	,A=0.0	,A=0.0	,A=0.02	,A=0.0	,A=False	,A=1	,A=0	,A=2	,A=2	,**A	,):
          UpperCAmelCase							=       vocab_size
          UpperCAmelCase							=       max_position_embeddings
          UpperCAmelCase							=       d_model
          UpperCAmelCase							=       encoder_ffn_dim
          UpperCAmelCase							=       encoder_layers
          UpperCAmelCase							=       encoder_attention_heads
          UpperCAmelCase							=       decoder_ffn_dim
          UpperCAmelCase							=       decoder_layers
          UpperCAmelCase							=       decoder_attention_heads
          UpperCAmelCase							=       dropout
          UpperCAmelCase							=       attention_dropout
          UpperCAmelCase							=       activation_dropout
          UpperCAmelCase							=       activation_function
          UpperCAmelCase							=       init_std
          UpperCAmelCase							=       encoder_layerdrop
          UpperCAmelCase							=       decoder_layerdrop
          UpperCAmelCase							=       classifier_dropout
          UpperCAmelCase							=       use_cache
          UpperCAmelCase							=       encoder_layers
          UpperCAmelCase							=       scale_embedding  # scale factor will be sqrt(d_model) if True
          super().__init__(
              pad_token_id=A	,bos_token_id=A	,eos_token_id=A	,is_encoder_decoder=A	,forced_eos_token_id=A	,**A	,)







class 					lowerCamelCase__					(       snake_case ):






    @property
    def 				_UpperCamelCase							( self					):
          if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase							=       OrderedDict(
                    [
                        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                    ]					)

                if self.use_past:
                      UpperCAmelCase							=       {0: """batch"""}
                      UpperCAmelCase							=       {0: """batch""", 1: """past_decoder_sequence + sequence"""}
                else:
                      UpperCAmelCase							=       {0: """batch""", 1: """decoder_sequence"""}
                      UpperCAmelCase							=       {0: """batch""", 1: """decoder_sequence"""}

                if self.use_past:
                      self.fill_with_past_key_values_(A	,direction="""inputs"""					)
          elif self.task == "causal-lm":
                # TODO: figure this case out.
                UpperCAmelCase							=       OrderedDict(
                    [
                        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                    ]					)
                if self.use_past:
                      UpperCAmelCase			,					UpperCAmelCase							=       self.num_layers
                      for i in range(A					):
                            UpperCAmelCase							=       {0: """batch""", 2: """past_sequence + sequence"""}
                            UpperCAmelCase							=       {0: """batch""", 2: """past_sequence + sequence"""}
          else:
                UpperCAmelCase							=       OrderedDict(
                    [
                        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
                        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
                        ("""decoder_input_ids""", {0: """batch""", 1: """decoder_sequence"""}),
                        ("""decoder_attention_mask""", {0: """batch""", 1: """decoder_sequence"""}),
                    ]					)

          return common_inputs






    @property
    def 				_UpperCamelCase							( self					):
          if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase							=       super().outputs
          else:
                UpperCAmelCase							=       super(A	,self					).outputs
                if self.use_past:
                      UpperCAmelCase			,					UpperCAmelCase							=       self.num_layers
                      for i in range(A					):
                            UpperCAmelCase							=       {0: """batch""", 2: """past_sequence + sequence"""}
                            UpperCAmelCase							=       {0: """batch""", 2: """past_sequence + sequence"""}
          return common_outputs






    def 				_UpperCamelCase							( self	,A	,A = -1	,A = -1	,A = False	,A = None	,):
          UpperCAmelCase							=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              A	,A	,A	,A	,A					)

          # Generate decoder inputs
          UpperCAmelCase							=       seq_length if not self.use_past else 1
          UpperCAmelCase							=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              A	,A	,A	,A	,A					)
          UpperCAmelCase							=       {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
          UpperCAmelCase							=       dict(**A	,**A					)

          if self.use_past:
                if not is_torch_available():
                      raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""					)
                else:
                      import torch
                UpperCAmelCase			,					UpperCAmelCase							=       common_inputs["""input_ids"""].shape
                UpperCAmelCase							=       common_inputs["""decoder_input_ids"""].shape[1]
                UpperCAmelCase			,					UpperCAmelCase							=       self.num_attention_heads
                UpperCAmelCase							=       (
                    batch,
                    num_encoder_attention_heads,
                    encoder_seq_length,
                    self._config.hidden_size // num_encoder_attention_heads,
                )
                UpperCAmelCase							=       decoder_seq_length + 3
                UpperCAmelCase							=       (
                    batch,
                    num_decoder_attention_heads,
                    decoder_past_length,
                    self._config.hidden_size // num_decoder_attention_heads,
                )

                UpperCAmelCase							=       torch.cat(
                    [common_inputs["""decoder_attention_mask"""], torch.ones(A	,A					)]	,dim=1					)

                UpperCAmelCase							=       []
                # If the number of encoder and decoder layers are present in the model configuration, both are considered
                UpperCAmelCase			,					UpperCAmelCase							=       self.num_layers
                UpperCAmelCase							=       min(A	,A					)
                UpperCAmelCase							=       max(A	,A					) - min_num_layers
                UpperCAmelCase							=       """encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

                for _ in range(A					):
                      common_inputs["past_key_values"].append(
                          (
                              torch.zeros(A					),
                              torch.zeros(A					),
                              torch.zeros(A					),
                              torch.zeros(A					),
                          )					)
                # TODO: test this.
                UpperCAmelCase							=       encoder_shape if remaining_side_name == """encoder""" else decoder_shape
                for _ in range(A	,A					):
                      common_inputs["past_key_values"].append((torch.zeros(A					), torch.zeros(A					))					)
          return common_inputs






    def 				_UpperCamelCase							( self	,A	,A = -1	,A = -1	,A = False	,A = None	,):
          UpperCAmelCase							=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
              A	,A	,A	,A	,A					)

          if self.use_past:
                if not is_torch_available():
                      raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""					)
                else:
                      import torch
                UpperCAmelCase			,					UpperCAmelCase							=       common_inputs["""input_ids"""].shape
                # Not using the same length for past_key_values
                UpperCAmelCase							=       seqlen + 2
                UpperCAmelCase			,					UpperCAmelCase							=       self.num_layers
                UpperCAmelCase			,					UpperCAmelCase							=       self.num_attention_heads
                UpperCAmelCase							=       (
                    batch,
                    num_encoder_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // num_encoder_attention_heads,
                )

                UpperCAmelCase							=       common_inputs["""attention_mask"""].dtype
                UpperCAmelCase							=       torch.cat(
                    [common_inputs["""attention_mask"""], torch.ones(A	,A	,dtype=A					)]	,dim=1					)
                UpperCAmelCase							=       [
                    (torch.zeros(A					), torch.zeros(A					)) for _ in range(A					)
                ]
          return common_inputs






    def 				_UpperCamelCase							( self	,A	,A = -1	,A = -1	,A = False	,A = None	,):
          # Copied from OnnxConfig.generate_dummy_inputs
          # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
          # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
          UpperCAmelCase							=       compute_effective_axis_dimension(
              A	,fixed_dimension=OnnxConfig.default_fixed_batch	,num_token_to_add=0					)

          # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
          UpperCAmelCase							=       tokenizer.num_special_tokens_to_add(A					)
          UpperCAmelCase							=       compute_effective_axis_dimension(
              A	,fixed_dimension=OnnxConfig.default_fixed_sequence	,num_token_to_add=A					)

          # Generate dummy inputs according to compute batch and sequence
          UpperCAmelCase							=       [""" """.join([tokenizer.unk_token]					) * seq_length] * batch_size
          UpperCAmelCase							=       dict(tokenizer(A	,return_tensors=A					)					)
          return common_inputs






    def 				_UpperCamelCase							( self	,A	,A = -1	,A = -1	,A = False	,A = None	,):
          if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase							=       self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                    A	,batch_size=A	,seq_length=A	,is_pair=A	,framework=A					)

          elif self.task == "causal-lm":
                UpperCAmelCase							=       self._generate_dummy_inputs_for_causal_lm(
                    A	,batch_size=A	,seq_length=A	,is_pair=A	,framework=A					)
          else:
                UpperCAmelCase							=       self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                    A	,batch_size=A	,seq_length=A	,is_pair=A	,framework=A					)

          return common_inputs






    def 				_UpperCamelCase							( self	,A	,A	,A	,A					):
          if self.task in ["default", "seq2seq-lm"]:
                UpperCAmelCase							=       super()._flatten_past_key_values_(A	,A	,A	,A					)
          else:
                UpperCAmelCase							=       super(A	,self					)._flatten_past_key_values_(
                    A	,A	,A	,A					)



