





"""simple docstring"""



import os
import sys
import warnings
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen
from ..table import array_cast
from ..utils.file_utils import is_local_path
from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
       import PIL.Image

       from .features import FeatureType


a_			=      None
a_			=      """<""" if sys.byteorder == """little""" else """>"""
# Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image
a_			=      [
    np.dtype("""|b1"""),
    np.dtype("""|u1"""),
    np.dtype("""<u2"""),
    np.dtype(""">u2"""),
    np.dtype("""<i2"""),
    np.dtype(""">i2"""),
    np.dtype("""<u4"""),
    np.dtype(""">u4"""),
    np.dtype("""<i4"""),
    np.dtype(""">i4"""),
    np.dtype("""<f4"""),
    np.dtype(""">f4"""),
    np.dtype("""<f8"""),
    np.dtype(""">f8"""),
]






@dataclass
class 		__snake_case :





 """simple docstring"""


 _lowerCamelCase				=					True
 _lowerCamelCase				=					None
 # Automatically constructed
 _lowerCamelCase				=					"PIL.Image.Image"
 _lowerCamelCase				=					pa.struct({"""bytes""": pa.binary(), """path""": pa.string()}					)
 _lowerCamelCase				=					field(default="""Image"""							,							init=SCREAMING_SNAKE_CASE__							,							repr=SCREAMING_SNAKE_CASE__					)

 def __call__(   self  ):



     '''simple docstring'''




     return self.pa_type

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     if config.PIL_AVAILABLE:
         import PIL.Image
     else:
         raise ImportError('''To support encoding images, please install \'Pillow\'.'''  )

     if isinstance(__lowerCamelCase ,						__lowerCamelCase  ):
         __A   :						List[str]									=					np.array(__lowerCamelCase  )

     if isinstance(__lowerCamelCase ,						__lowerCamelCase  ):
         return {"path": value, "bytes": None}
     elif isinstance(__lowerCamelCase ,						__lowerCamelCase  ):
         return {"path": None, "bytes": value}
     elif isinstance(__lowerCamelCase ,						np.ndarray  ):
         # convert the image array to PNG/TIFF bytes
         return encode_np_array(__lowerCamelCase  )
     elif isinstance(__lowerCamelCase ,						PIL.Image.Image  ):
         # convert the PIL image to bytes (default format is PNG/TIFF)
         return encode_pil_image(__lowerCamelCase  )
     elif value.get('''path'''  ) is not None and os.path.isfile(value['''path''']  ):
         # we set "bytes": None to not duplicate the data if they're already available locally
         return {"bytes": None, "path": value.get('''path'''  )}
     elif value.get('''bytes'''  ) is not None or value.get('''path'''  ) is not None:
         # store the image bytes, and path is used to infer the image format using the file extension
         return {"bytes": value.get('''bytes'''  ), "path": value.get('''path'''  )}
     else:
         raise ValueError(
             F"""An image sample should have one of 'path' or 'bytes' but they are missing or None in {value}."""  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=None  ):



     '''simple docstring'''




     if not self.decode:
         raise RuntimeError('''Decoding is disabled for this feature. Please use Image(decode=True) instead.'''  )

     if config.PIL_AVAILABLE:
         import PIL.Image
     else:
         raise ImportError('''To support decoding images, please install \'Pillow\'.'''  )

     if token_per_repo_id is None:
         __A   :						Dict									=					{}

     __A			,	__A   :						Tuple									=					value['''path'''], value['''bytes''']
     if bytes_ is None:
         if path is None:
             raise ValueError(F"""An image should have one of 'path' or 'bytes' but both are None in {value}."""  )
         else:
             if is_local_path(__lowerCamelCase  ):
                 __A   :						Union[str, Any]									=					PIL.Image.open(__lowerCamelCase  )
             else:
                 __A   :						List[Any]									=					path.split('''::'''  )[-1]
                 try:
                     __A   :						Tuple									=					string_to_dict(__lowerCamelCase ,						config.HUB_DATASETS_URL  )['''repo_id''']
                     __A   :						str									=					token_per_repo_id.get(__lowerCamelCase  )
                 except ValueError:
                     __A   :						Optional[Any]									=					None
                 with xopen(__lowerCamelCase ,						'''rb''' ,						use_auth_token=__lowerCamelCase  ) as f:
                     __A   :						Optional[Any]									=					BytesIO(f.read()  )
                 __A   :						List[str]									=					PIL.Image.open(bytes_  )
     else:
         __A   :						Optional[int]									=					PIL.Image.open(BytesIO(bytes_  )  )
     image.load()  # to avoid "Too many open files" errors
     return image

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     from .features import Value

     return (
         self
         if self.decode
         else {
             "bytes": Value('''binary'''  ),
             "path": Value('''string'''  ),
         }
     )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     if pa.types.is_string(storage.type  ):
         __A   :						Optional[Any]									=					pa.array([None] * len(__lowerCamelCase  ) ,						type=pa.binary()  )
         __A   :						str									=					pa.StructArray.from_arrays([bytes_array, storage] ,						['''bytes''', '''path'''] ,						mask=storage.is_null()  )
     elif pa.types.is_binary(storage.type  ):
         __A   :						str									=					pa.array([None] * len(__lowerCamelCase  ) ,						type=pa.string()  )
         __A   :						Optional[Any]									=					pa.StructArray.from_arrays([storage, path_array] ,						['''bytes''', '''path'''] ,						mask=storage.is_null()  )
     elif pa.types.is_struct(storage.type  ):
         if storage.type.get_field_index('''bytes'''  ) >= 0:
             __A   :						Optional[Any]									=					storage.field('''bytes'''  )
         else:
             __A   :						str									=					pa.array([None] * len(__lowerCamelCase  ) ,						type=pa.binary()  )
         if storage.type.get_field_index('''path'''  ) >= 0:
             __A   :						Optional[Any]									=					storage.field('''path'''  )
         else:
             __A   :						Tuple									=					pa.array([None] * len(__lowerCamelCase  ) ,						type=pa.string()  )
         __A   :						Tuple									=					pa.StructArray.from_arrays([bytes_array, path_array] ,						['''bytes''', '''path'''] ,						mask=storage.is_null()  )
     elif pa.types.is_list(storage.type  ):
         __A   :						List[Any]									=					pa.array(
             [encode_np_array(np.array(__lowerCamelCase  )  )['''bytes'''] if arr is not None else None for arr in storage.to_pylist()] ,						type=pa.binary() ,						)
         __A   :						List[str]									=					pa.array([None] * len(__lowerCamelCase  ) ,						type=pa.string()  )
         __A   :						Tuple									=					pa.StructArray.from_arrays(
             [bytes_array, path_array] ,						['''bytes''', '''path'''] ,						mask=bytes_array.is_null()  )
     return array_cast(__lowerCamelCase ,						self.pa_type  )







 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     @no_op_if_value_is_null
     def path_to_bytes(__lowerCamelCase  ):
         with xopen(__lowerCamelCase ,						'''rb'''  ) as f:
             __A   :						Tuple									=					f.read()
         return bytes_

     __A   :						List[Any]									=					pa.array(
         [
             (path_to_bytes(x['''path''']  ) if x['''bytes'''] is None else x['''bytes''']) if x is not None else None
             for x in storage.to_pylist()
         ] ,						type=pa.binary() ,						)
     __A   :						List[Any]									=					pa.array(
         [os.path.basename(__lowerCamelCase  ) if path is not None else None for path in storage.field('''path'''  ).to_pylist()] ,						type=pa.string() ,						)
     __A   :						List[Any]									=					pa.StructArray.from_arrays([bytes_array, path_array] ,						['''bytes''', '''path'''] ,						mask=bytes_array.is_null()  )
     return array_cast(__lowerCamelCase ,						self.pa_type  )







def 		__lowercase					(       )       ->List[str]:



    '''simple docstring'''







    if config.PIL_AVAILABLE:
        import PIL.Image
    else:
        raise ImportError('''To support encoding images, please install \'Pillow\'.'''						)

    global _IMAGE_COMPRESSION_FORMATS
    if _IMAGE_COMPRESSION_FORMATS is None:
        PIL.Image.init()
        __A   :						str									=					list(set(PIL.Image.OPEN.keys()						) & set(PIL.Image.SAVE.keys()						)						)
    return _IMAGE_COMPRESSION_FORMATS







def 		__lowercase					(       snake_case_  :  "PIL.Image.Image"						)       ->bytes:



    '''simple docstring'''







    __A   :						Any									=					BytesIO()
    if image.format in list_image_compression_formats():
        __A   :						List[str]									=					image.format
    else:
        __A   :						Optional[Any]									=					'''PNG''' if image.mode in ['''1''', '''L''', '''LA''', '''RGB''', '''RGBA'''] else '''TIFF'''
    image.save(snake_case_    ,format=snake_case_						)
    return buffer.getvalue()







def 		__lowercase					(       snake_case_  :  "PIL.Image.Image"						)       ->dict:



    '''simple docstring'''







    if hasattr(snake_case_    ,'''filename'''						) and image.filename != "":
        return {"path": image.filename, "bytes": None}
    else:
        return {"path": None, "bytes": image_to_bytes(snake_case_						)}







def 		__lowercase					(       snake_case_  :  np.ndarray						)       ->dict:



    '''simple docstring'''







    if config.PIL_AVAILABLE:
        import PIL.Image
    else:
        raise ImportError('''To support encoding images, please install \'Pillow\'.'''						)

    __A   :						List[Any]									=					array.dtype
    __A   :						Optional[Any]									=					dtype.byteorder if dtype.byteorder != '''=''' else _NATIVE_BYTEORDER
    __A   :						Union[str, Any]									=					dtype.kind
    __A   :						List[Any]									=					dtype.itemsize

    __A   :						Tuple									=					None

    # Multi-channel array case (only np.dtype("|u1") is allowed)
    if array.shape[2:]:
        __A   :						List[Any]									=					np.dtype('''|u1'''						)
        if dtype_kind not in ["u", "i"]:
            raise TypeError(
                F"""Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays."""						)
        if dtype is not dest_dtype:
            warnings.warn(F"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'"""						)
    # Exact match
    elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
        __A   :						Optional[Any]									=					dtype
    else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
        while dtype_itemsize >= 1:
            __A   :						int									=					dtype_byteorder + dtype_kind + str(snake_case_						)
            __A   :						Optional[int]									=					np.dtype(snake_case_						)
            if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES:
                warnings.warn(F"""Downcasting array dtype {dtype} to {dest_dtype} to be compatible with 'Pillow'"""						)
                break
            else:
                dtype_itemsize //= 2

    if dest_dtype is None:
        raise TypeError(
            F"""Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}"""						)
    __A   :						Optional[Any]									=					PIL.Image.fromarray(array.astype(snake_case_						)						)
    return {"path": None, "bytes": image_to_bytes(snake_case_						)}







def 		__lowercase					(       snake_case_  :  Union[List[str], List[dict], List[np.ndarray], List["PIL.Image.Image"]]						)       ->List[dict]:



    '''simple docstring'''







    if config.PIL_AVAILABLE:
        import PIL.Image
    else:
        raise ImportError('''To support encoding images, please install \'Pillow\'.'''						)

    if objs:
        __A			,	__A   :						Dict									=					first_non_null_value(snake_case_						)
        if isinstance(snake_case_    ,snake_case_						):
            return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs]
        if isinstance(snake_case_    ,np.ndarray						):
            __A   :						List[str]									=					no_op_if_value_is_null(snake_case_						)
            return [obj_to_image_dict_func(snake_case_						) for obj in objs]
        elif isinstance(snake_case_    ,PIL.Image.Image						):
            __A   :						List[Any]									=					no_op_if_value_is_null(snake_case_						)
            return [obj_to_image_dict_func(snake_case_						) for obj in objs]
        else:
            return objs
    else:
        return objs











"""simple docstring"""



import functools
import operator

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


a_			=      logging.get_logger(__name__)

a_			=      {
    """asapp/sew-d-tiny-100k""": """https://huggingface.co/asapp/sew-d-tiny-100k/resolve/main/config.json""",
    # See all SEW-D models at https://huggingface.co/models?filter=sew-d
}






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					"""sew-d"""

 def __init__(   self ,						__lowerCamelCase=32 ,						__lowerCamelCase=768 ,						__lowerCamelCase=12 ,						__lowerCamelCase=12 ,						__lowerCamelCase=3072 ,						__lowerCamelCase=2 ,						__lowerCamelCase=512 ,						__lowerCamelCase=256 ,						__lowerCamelCase=True ,						__lowerCamelCase=True ,						__lowerCamelCase=("p2c", "c2p") ,						__lowerCamelCase="layer_norm" ,						__lowerCamelCase="gelu_python" ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=0.1 ,						__lowerCamelCase=0.0_2 ,						__lowerCamelCase=1e-7 ,						__lowerCamelCase=1e-5 ,						__lowerCamelCase="group" ,						__lowerCamelCase="gelu" ,						__lowerCamelCase=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512) ,						__lowerCamelCase=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1) ,						__lowerCamelCase=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1) ,						__lowerCamelCase=False ,						__lowerCamelCase=128 ,						__lowerCamelCase=16 ,						__lowerCamelCase=True ,						__lowerCamelCase=0.0_5 ,						__lowerCamelCase=10 ,						__lowerCamelCase=2 ,						__lowerCamelCase=0.0 ,						__lowerCamelCase=10 ,						__lowerCamelCase=0 ,						__lowerCamelCase="mean" ,						__lowerCamelCase=False ,						__lowerCamelCase=False ,						__lowerCamelCase=256 ,						__lowerCamelCase=0 ,						__lowerCamelCase=1 ,						__lowerCamelCase=2 ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase ,						pad_token_id=__lowerCamelCase ,						bos_token_id=__lowerCamelCase ,						eos_token_id=__lowerCamelCase  )
     __A   :						str									=					hidden_size
     __A   :						List[Any]									=					feat_extract_norm
     __A   :						Tuple									=					feat_extract_activation
     __A   :						Dict									=					list(__lowerCamelCase  )
     __A   :						int									=					list(__lowerCamelCase  )
     __A   :						List[Any]									=					list(__lowerCamelCase  )
     __A   :						Any									=					conv_bias
     __A   :						List[Any]									=					num_conv_pos_embeddings
     __A   :						Any									=					num_conv_pos_embedding_groups
     __A   :						Optional[Any]									=					len(self.conv_dim  )
     __A   :						int									=					num_hidden_layers
     __A   :						Union[str, Any]									=					intermediate_size
     __A   :						Union[str, Any]									=					squeeze_factor
     __A   :						int									=					max_position_embeddings
     __A   :						Tuple									=					position_buckets
     __A   :						Tuple									=					share_att_key
     __A   :						List[str]									=					relative_attention
     __A   :						Optional[Any]									=					norm_rel_ebd
     __A   :						Dict									=					list(__lowerCamelCase  )
     __A   :						str									=					hidden_act
     __A   :						List[str]									=					num_attention_heads
     __A   :						Union[str, Any]									=					hidden_dropout
     __A   :						Optional[int]									=					attention_dropout
     __A   :						Optional[Any]									=					activation_dropout
     __A   :						List[str]									=					feat_proj_dropout
     __A   :						str									=					final_dropout
     __A   :						Tuple									=					layer_norm_eps
     __A   :						int									=					feature_layer_norm_eps
     __A   :						Optional[int]									=					initializer_range
     __A   :						Dict									=					vocab_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)`,'''
             F"""but is `len(config.conv_dim) = {len(self.conv_dim  )}`, `len(config.conv_stride)"""
             F"""= {len(self.conv_stride  )}`, `len(config.conv_kernel) = {len(self.conv_kernel  )}`."""  )

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

     # ctc loss
     __A   :						Tuple									=					ctc_loss_reduction
     __A   :						Union[str, Any]									=					ctc_zero_infinity

     # sequence classification
     __A   :						Tuple									=					use_weighted_layer_sum
     __A   :						List[str]									=					classifier_proj_size







 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return functools.reduce(operator.mul ,						self.conv_stride ,						1  )




