


"""simple docstring"""





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import ConvNextImageProcessor

class  lowercase_  ( unittest.TestCase						):







      def __init__(      self :							Union[str, Any]		,  _lowercase :							Optional[Any]		,  _lowercase :							str=7		,  _lowercase :							str=3		,  _lowercase :							Union[str, Any]=1_8		,  _lowercase :							Any=3_0		,  _lowercase :							Union[str, Any]=4_0_0		,  _lowercase :							str=True		,  _lowercase :							str=None		,  _lowercase :							Any=0.875		,  _lowercase :							Optional[Any]=True		,  _lowercase :							Union[str, Any]=[0.5, 0.5, 0.5]		,  _lowercase :							Any=[0.5, 0.5, 0.5]		,  ):
        lowerCAmelCase__    :    Any								=					size if size is not None else {"shortest_edge": 2_0}
        lowerCAmelCase__    :    int								=					parent
        lowerCAmelCase__    :    int								=					batch_size
        lowerCAmelCase__    :    str								=					num_channels
        lowerCAmelCase__    :    Optional[int]								=					image_size
        lowerCAmelCase__    :    Optional[Any]								=					min_resolution
        lowerCAmelCase__    :    List[Any]								=					max_resolution
        lowerCAmelCase__    :    Dict								=					do_resize
        lowerCAmelCase__    :    List[Any]								=					size
        lowerCAmelCase__    :    Union[str, Any]								=					crop_pct
        lowerCAmelCase__    :    Any								=					do_normalize
        lowerCAmelCase__    :    Optional[int]								=					image_mean
        lowerCAmelCase__    :    Tuple								=					image_std







      def 				_lowerCAmelCase       (      self :							Optional[Any]    ):
        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "crop_pct": self.crop_pct,
        }

@require_torch
@require_vision
class  lowercase_  ( UpperCAmelCase__							,							unittest.TestCase						):
      __magic_name__     :							List[str]          =				ConvNextImageProcessor if is_vision_available() else None







      def 				_lowerCAmelCase       (      self :							Dict    ):
        lowerCAmelCase__    :    List[Any]								=					ConvNextImageProcessingTester(self    )







      @property
      def 				_lowerCAmelCase       (      self :							int    ):
        return self.image_processor_tester.prepare_image_processor_dict()







      def 				_lowerCAmelCase       (      self :							str    ):
        lowerCAmelCase__    :    Dict								=					self.image_processing_class(**self.image_processor_dict    )
        self.assertTrue(hasattr(lowerCamelCase__		,  "do_resize"    )    )
        self.assertTrue(hasattr(lowerCamelCase__		,  "size"    )    )
        self.assertTrue(hasattr(lowerCamelCase__		,  "crop_pct"    )    )
        self.assertTrue(hasattr(lowerCamelCase__		,  "do_normalize"    )    )
        self.assertTrue(hasattr(lowerCamelCase__		,  "image_mean"    )    )
        self.assertTrue(hasattr(lowerCamelCase__		,  "image_std"    )    )







      def 				_lowerCAmelCase       (      self :							Union[str, Any]    ):
        lowerCAmelCase__    :    Dict								=					self.image_processing_class.from_dict(self.image_processor_dict    )
        self.assertEqual(image_processor.size		,  {"shortest_edge": 2_0}    )

        lowerCAmelCase__    :    List[Any]								=					self.image_processing_class.from_dict(self.image_processor_dict		,  size=4_2    )
        self.assertEqual(image_processor.size		,  {"shortest_edge": 4_2}    )







      def 				_lowerCAmelCase       (      self :							Optional[int]    ):
        pass







      def 				_lowerCAmelCase       (      self :							List[str]    ):
        # Initialize image_processing
        lowerCAmelCase__    :    Tuple								=					self.image_processing_class(**self.image_processor_dict    )
        # create random PIL images
        lowerCAmelCase__    :    Union[str, Any]								=					prepare_image_inputs(self.image_processor_tester		,  equal_resolution=lowerCamelCase__    )
        for image in image_inputs:
          self.assertIsInstance(lowerCamelCase__		,  Image.Image    )

        # Test not batched input
        lowerCAmelCase__    :    List[str]								=					image_processing(image_inputs[0]		,  return_tensors="pt"    ).pixel_values
        self.assertEqual(
            encoded_images.shape		,  (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["shortest_edge"],
                self.image_processor_tester.size["shortest_edge"],
            )		,  )

        # Test batched
        lowerCAmelCase__    :    Optional[Any]								=					image_processing(lowerCamelCase__		,  return_tensors="pt"    ).pixel_values
        self.assertEqual(
            encoded_images.shape		,  (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["shortest_edge"],
                self.image_processor_tester.size["shortest_edge"],
            )		,  )







      def 				_lowerCAmelCase       (      self :							Union[str, Any]    ):
        # Initialize image_processing
        lowerCAmelCase__    :    int								=					self.image_processing_class(**self.image_processor_dict    )
        # create random numpy tensors
        lowerCAmelCase__    :    List[str]								=					prepare_image_inputs(self.image_processor_tester		,  equal_resolution=lowerCamelCase__		,  numpify=lowerCamelCase__    )
        for image in image_inputs:
          self.assertIsInstance(lowerCamelCase__		,  np.ndarray    )

        # Test not batched input
        lowerCAmelCase__    :    Optional[Any]								=					image_processing(image_inputs[0]		,  return_tensors="pt"    ).pixel_values
        self.assertEqual(
            encoded_images.shape		,  (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["shortest_edge"],
                self.image_processor_tester.size["shortest_edge"],
            )		,  )

        # Test batched
        lowerCAmelCase__    :    List[Any]								=					image_processing(lowerCamelCase__		,  return_tensors="pt"    ).pixel_values
        self.assertEqual(
            encoded_images.shape		,  (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["shortest_edge"],
                self.image_processor_tester.size["shortest_edge"],
            )		,  )







      def 				_lowerCAmelCase       (      self :							str    ):
        # Initialize image_processing
        lowerCAmelCase__    :    List[str]								=					self.image_processing_class(**self.image_processor_dict    )
        # create random PyTorch tensors
        lowerCAmelCase__    :    Tuple								=					prepare_image_inputs(self.image_processor_tester		,  equal_resolution=lowerCamelCase__		,  torchify=lowerCamelCase__    )
        for image in image_inputs:
          self.assertIsInstance(lowerCamelCase__		,  torch.Tensor    )

        # Test not batched input
        lowerCAmelCase__    :    List[Any]								=					image_processing(image_inputs[0]		,  return_tensors="pt"    ).pixel_values
        self.assertEqual(
            encoded_images.shape		,  (
                1,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["shortest_edge"],
                self.image_processor_tester.size["shortest_edge"],
            )		,  )

        # Test batched
        lowerCAmelCase__    :    int								=					image_processing(lowerCamelCase__		,  return_tensors="pt"    ).pixel_values
        self.assertEqual(
            encoded_images.shape		,  (
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["shortest_edge"],
                self.image_processor_tester.size["shortest_edge"],
            )		,  )




"""simple docstring"""





import functools
import operator

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


__UpperCAmelCase    				=      logging.get_logger(__name__)

__UpperCAmelCase    				=      {
    "facebook/hubert-base-ls960": "https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}

class  lowercase_  ( a_						):
      __magic_name__     :							List[Any]          =				"""hubert"""







      def __init__(      self :							List[Any]		,  _lowercase :							Optional[int]=3_2		,  _lowercase :							Tuple=7_6_8		,  _lowercase :							List[Any]=1_2		,  _lowercase :							Dict=1_2		,  _lowercase :							Tuple=3_0_7_2		,  _lowercase :							str="gelu"		,  _lowercase :							Optional[int]=0.1		,  _lowercase :							int=0.1		,  _lowercase :							Tuple=0.1		,  _lowercase :							str=True		,  _lowercase :							Optional[Any]=0.0		,  _lowercase :							List[Any]=0.1		,  _lowercase :							Union[str, Any]=0.1		,  _lowercase :							Union[str, Any]=0.02		,  _lowercase :							List[Any]=1e-5		,  _lowercase :							List[Any]="group"		,  _lowercase :							Tuple="gelu"		,  _lowercase :							List[str]=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)		,  _lowercase :							List[Any]=(5, 2, 2, 2, 2, 2, 2)		,  _lowercase :							Tuple=(1_0, 3, 3, 3, 3, 2, 2)		,  _lowercase :							Any=False		,  _lowercase :							str=1_2_8		,  _lowercase :							int=1_6		,  _lowercase :							str=False		,  _lowercase :							Union[str, Any]=True		,  _lowercase :							Optional[Any]=0.05		,  _lowercase :							List[Any]=1_0		,  _lowercase :							Dict=2		,  _lowercase :							Optional[int]=0.0		,  _lowercase :							List[str]=1_0		,  _lowercase :							List[str]=0		,  _lowercase :							Optional[Any]="sum"		,  _lowercase :							int=False		,  _lowercase :							Any=False		,  _lowercase :							Optional[Any]=2_5_6		,  _lowercase :							int=0		,  _lowercase :							Optional[int]=1		,  _lowercase :							List[Any]=2		,  **_lowercase :							Tuple		,  ):
        super().__init__(**_lowercase		,  pad_token_id=_lowercase		,  bos_token_id=_lowercase		,  eos_token_id=_lowercase    )
        lowerCAmelCase__    :    Optional[Any]								=					hidden_size
        lowerCAmelCase__    :    int								=					feat_extract_norm
        lowerCAmelCase__    :    Optional[Any]								=					feat_extract_activation
        lowerCAmelCase__    :    Optional[Any]								=					list(_lowercase    )
        lowerCAmelCase__    :    Union[str, Any]								=					list(_lowercase    )
        lowerCAmelCase__    :    Tuple								=					list(_lowercase    )
        lowerCAmelCase__    :    str								=					conv_bias
        lowerCAmelCase__    :    Optional[Any]								=					num_conv_pos_embeddings
        lowerCAmelCase__    :    List[str]								=					num_conv_pos_embedding_groups
        lowerCAmelCase__    :    List[Any]								=					len(self.conv_dim    )
        lowerCAmelCase__    :    Union[str, Any]								=					num_hidden_layers
        lowerCAmelCase__    :    str								=					intermediate_size
        lowerCAmelCase__    :    List[Any]								=					hidden_act
        lowerCAmelCase__    :    Optional[int]								=					num_attention_heads
        lowerCAmelCase__    :    Optional[int]								=					hidden_dropout
        lowerCAmelCase__    :    Dict								=					attention_dropout
        lowerCAmelCase__    :    Dict								=					activation_dropout
        lowerCAmelCase__    :    Optional[Any]								=					feat_proj_layer_norm
        lowerCAmelCase__    :    Union[str, Any]								=					feat_proj_dropout
        lowerCAmelCase__    :    Optional[Any]								=					final_dropout
        lowerCAmelCase__    :    Dict								=					layerdrop
        lowerCAmelCase__    :    Optional[int]								=					layer_norm_eps
        lowerCAmelCase__    :    str								=					initializer_range
        lowerCAmelCase__    :    int								=					vocab_size
        lowerCAmelCase__    :    int								=					do_stable_layer_norm
        lowerCAmelCase__    :    Union[str, Any]								=					use_weighted_layer_sum
        lowerCAmelCase__    :    Tuple								=					classifier_proj_size

        if (
            (len(self.conv_stride    ) != self.num_feat_extract_layers)
            or (len(self.conv_kernel    ) != self.num_feat_extract_layers)
            or (len(self.conv_dim    ) != self.num_feat_extract_layers)
        ):
          raise ValueError(
              "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
              " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
              f" {len(self.conv_dim    )}`, `len(config.conv_stride) = {len(self.conv_stride    )}`,"
              f" `len(config.conv_kernel) = {len(self.conv_kernel    )}`."    )

        # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
        lowerCAmelCase__    :    int								=					apply_spec_augment
        lowerCAmelCase__    :    Tuple								=					mask_time_prob
        lowerCAmelCase__    :    int								=					mask_time_length
        lowerCAmelCase__    :    Dict								=					mask_time_min_masks
        lowerCAmelCase__    :    Union[str, Any]								=					mask_feature_prob
        lowerCAmelCase__    :    Dict								=					mask_feature_length
        lowerCAmelCase__    :    Tuple								=					mask_feature_min_masks

        # ctc loss
        lowerCAmelCase__    :    Tuple								=					ctc_loss_reduction
        lowerCAmelCase__    :    List[str]								=					ctc_zero_infinity







      @property
      def 				_lowerCAmelCase       (      self :							Dict    ):
        return functools.reduce(operator.mul		,  self.conv_stride		,  1    )
