

'''simple docstring'''







import math
from typing import Optional

import numpy as np

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


__lowerCamelCase						    =      logging.get_logger(__name__)

__lowerCamelCase						    =      {
    '''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__ ( _snake_case     ):
    lowercase		   =						"encodec"



    def __init__(   self						,			UpperCamelCase__=[1.5, 3.0, 6.0, 12.0, 24.0]						,			UpperCamelCase__=24000						,			UpperCamelCase__=1						,			UpperCamelCase__=False						,			UpperCamelCase__=None						,			UpperCamelCase__=None						,			UpperCamelCase__=128						,			UpperCamelCase__=32						,			UpperCamelCase__=1						,			UpperCamelCase__=[8, 5, 4, 2]						,			UpperCamelCase__="weight_norm"						,			UpperCamelCase__=7						,			UpperCamelCase__=7						,			UpperCamelCase__=3						,			UpperCamelCase__=2						,			UpperCamelCase__=True						,			UpperCamelCase__="reflect"						,			UpperCamelCase__=2						,			UpperCamelCase__=2						,			UpperCamelCase__=1.0						,			UpperCamelCase__=1024						,			UpperCamelCase__=None						,			UpperCamelCase__=True						,			**UpperCamelCase__						,			)  -> str:





           '''simple docstring'''
           A_		   = target_bandwidths
           A_		   = sampling_rate
           A_		   = audio_channels
           A_		   = normalize
           A_		   = chunk_length_s
           A_		   = overlap
           A_		   = hidden_size
           A_		   = num_filters
           A_		   = num_residual_layers
           A_		   = upsampling_ratios
           A_		   = norm_type
           A_		   = kernel_size
           A_		   = last_kernel_size
           A_		   = residual_kernel_size
           A_		   = dilation_growth_rate
           A_		   = use_causal_conv
           A_		   = pad_mode
           A_		   = compress
           A_		   = num_lstm_layers
           A_		   = trim_right_ratio
           A_		   = codebook_size
           A_		   = codebook_dim if codebook_dim is not None else hidden_size
           A_		   = 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__(**UpperCamelCase__    )



    @property
    def    snake_case_     (   self    )  -> Optional[int]:





           '''simple docstring'''
           if self.chunk_length_s is None:
                  return None
           else:
                  return int(self.chunk_length_s * self.sampling_rate    )



    @property
    def    snake_case_     (   self    )  -> Optional[int]:





           '''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    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           A_		   = np.prod(self.upsampling_ratios    )
           return math.ceil(self.sampling_rate / hop_length    )







    @property
    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           return int(1000 * self.target_bandwidths[-1] // (self.frame_rate * 10)    )



'''simple docstring'''







from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class    A__ ( _snake_case     ):



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = SMALL_MODEL_IDENTIFIER
           A_		   = """pt"""
           A_		   = """tf"""



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> Tuple:





           '''simple docstring'''
           A_		   = AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> List[str]:





           '''simple docstring'''
           A_		   = TFAutoModel.from_pretrained(self.test_model						,			from_pt=UpperCamelCase__    )
           model_tf.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = """mock_framework"""

           # Framework provided - return whatever the user provides
           A_		   = FeaturesManager.determine_framework(self.test_model						,			UpperCamelCase__    )
           self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

           # Local checkpoint and framework provided - return provided framework
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                  self._setup_tf_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )



    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                  self._setup_tf_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_tf    )

           # Invalid local checkpoint
           with TemporaryDirectory() as local_invalid_ckpt:
                  with self.assertRaises(UpperCamelCase__    ):
                         A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )







    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # PyTorch not in environment -> use TensorFlow
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_tf    )

           # Both in environment -> use PyTorch
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ), patch(
               """transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # Both not in environment -> raise error
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ), patch(
               """transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  with self.assertRaises(UpperCamelCase__    ):
                         A_		   = FeaturesManager.determine_framework(self.test_model    )
