




import json
import os
from collections.abc import Mapping
from typing import Dict, List, Optional, Union

import numpy as np
import torch

from .imports import is_safetensors_available




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_    ,   lowercase_    ,   lowercase_=None					)				->		List[Any]:

    """simple docstring"""


    __UpperCamelCase     						=  None
    # Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
    if str(weight.dtype					) == "torch.bfloat16":
        # Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
        __UpperCamelCase     						=  weight.view(torch.intaa					)
        __UpperCamelCase     						=  '''bfloat16'''
    __UpperCamelCase     						=  weight.cpu().numpy()
    __UpperCamelCase     						=  os.path.join(lowercase_    ,   F"{weight_name}.dat"					)
    if index is not None:
        if dtype is None:
            __UpperCamelCase     						=  str(array.dtype					)
        __UpperCamelCase     						=  {'''dtype''': dtype, '''shape''': list(array.shape					)}
    if array.ndim == 0:
        __UpperCamelCase     						=  array[None]
    __UpperCamelCase     						=  np.memmap(lowercase_    ,   dtype=array.dtype    ,   mode='''w+'''    ,   shape=array.shape					)
    __UpperCamelCase     						=  array[:]
    file_array.flush()
    return index




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		List[Any]:

    """simple docstring"""


    __UpperCamelCase     						=  tuple(weight_info['''shape''']					)
    if shape == ():
        # NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
        __UpperCamelCase     						=  (1,)

    __UpperCamelCase     						=  weight_info['''dtype''']
    if dtype == "bfloat16":
        # NumPy does not support bfloat16 so this was saved as a int16
        __UpperCamelCase     						=  '''int16'''

    __UpperCamelCase     						=  np.memmap(lowercase_    ,   dtype=lowercase_    ,   shape=lowercase_    ,   mode='''r'''					)

    if len(weight_info['''shape''']					) == 0:
        __UpperCamelCase     						=  weight[0]
    __UpperCamelCase     						=  torch.tensor(lowercase_					)
    if weight_info["dtype"] == "bfloat16":
        __UpperCamelCase     						=  weight.view(torch.bfloataa					)

    return weight




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		Dict:

    """simple docstring"""


    if index is None or len(lowercase_					) == 0:
        # Nothing to save
        return

    __UpperCamelCase     						=  os.path.join(lowercase_    ,   '''index.json'''					)
    if os.path.isfile(lowercase_					):
        with open(lowercase_    ,   '''r'''    ,   encoding='''utf-8'''					) as f:
            __UpperCamelCase     						=  json.load(lowercase_					)
    else:
        __UpperCamelCase     						=  {}
    current_index.update(lowercase_					)

    with open(lowercase_    ,   '''w'''    ,   encoding='''utf-8'''					) as f:
        json.dump(lowercase_    ,   lowercase_    ,   indent=2					)




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		Tuple:

    """simple docstring"""


    os.makedirs(lowercase_    ,   exist_ok=lowercase_					)
    __UpperCamelCase     						=  {}
    for name, parameter in state_dict.items():
        __UpperCamelCase     						=  offload_weight(lowercase_    ,   lowercase_    ,   lowercase_    ,   index=lowercase_					)

    # Update index
    save_offload_index(lowercase_    ,   lowercase_					)

class        _lowerCamelCase  (	_SCREAMING_SNAKE_CASE      ):

      """simple docstring"""

      def __init__(   self    :       int       ,				snake_case    :       Mapping       ,				snake_case    :       str ):
          __UpperCamelCase     						=  dataset
          __UpperCamelCase     						=  prefix







      def __getitem__(   self    :       Any       ,				snake_case    :       Optional[Any] ):
          return self.dataset[F"{self.prefix}{key}"]







      def __iter__(   self    :       Tuple ):
          return iter([key for key in self.dataset if key.startswith(self.prefix )] )







      def __len__(   self    :       List[Any] ):
          return len(self.dataset )







class        _lowerCamelCase  (	_SCREAMING_SNAKE_CASE      ):

      """simple docstring"""

      def __init__(   self    :       Optional[int]       ,				snake_case    :       Dict[str, torch.Tensor] = None       ,				snake_case    :       Optional[Union[str, os.PathLike]] = None       ,				snake_case    :       Mapping = None       ,				snake_case    :       Union[str, Any]=None       ,				):
          if state_dict is None and save_folder is None:
              raise ValueError('''Need either a `state_dict` or a `save_folder` containing offloaded weights.''' )

          __UpperCamelCase     						=  {} if state_dict is None else state_dict
          __UpperCamelCase     						=  save_folder
          if index is None and save_folder is not None:
              with open(os.path.join(snake_case       ,				'''index.json''' ) ) as f:
                  __UpperCamelCase     						=  json.load(snake_case )
          __UpperCamelCase     						=  {} if index is None else index
          __UpperCamelCase     						=  list(self.state_dict.keys() )
          self.all_keys.extend([key for key in self.index if key not in self.all_keys] )
          __UpperCamelCase     						=  device







      def __getitem__(   self    :       List[Any]       ,				snake_case    :       str ):
          # State dict gets priority
          if key in self.state_dict:
              return self.state_dict[key]
          __UpperCamelCase     						=  self.index[key]
          if weight_info.get('''safetensors_file''' ) is not None:
              if not is_safetensors_available():
                  raise ImportError('''These offloaded weights require the use of safetensors: `pip install safetensors`.''' )

              from safetensors import safe_open

              __UpperCamelCase     						=  '''cpu''' if self.device is None else self.device
              with safe_open(weight_info['''safetensors_file''']       ,				framework='''pt'''       ,				device=snake_case ) as f:
                  __UpperCamelCase     						=  f.get_tensor(weight_info.get('''weight_name'''       ,				snake_case ) )

              if "dtype" in weight_info:
                  return tensor.to(getattr(snake_case       ,				weight_info['''dtype'''] ) )
              else:
                  return tensor

          __UpperCamelCase     						=  os.path.join(self.save_folder       ,				F"{key}.dat" )
          return load_offloaded_weight(snake_case       ,				snake_case )







      def __iter__(   self    :       Optional[Any] ):
          return iter(self.all_keys )







      def __len__(   self    :       List[Any] ):
          return len(self.all_keys )







def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		List[Any]:

    """simple docstring"""


    __UpperCamelCase     						=  {}
    for module_name in submodule_names:
        # We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
        # submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
        result.update(
            {
                key: param
                for key, param in state_dict.items()
                if key == module_name or key.startswith(module_name + '''.'''					)
            }					)
    return result












from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
 import PIL


a_      						=   logging.get_logger(__name__)

class        _lowerCamelCase  (	_SCREAMING_SNAKE_CASE      ):

      """simple docstring"""

      lowerCAmelCase__	:       Union[str, Any]			=    ["pixel_values"]







      def __init__(   self    :       Union[str, Any]       ,				snake_case    :       bool = True       ,				snake_case    :       Dict[str, int] = None       ,				snake_case    :       bool = True       ,				snake_case    :       Dict[str, int] = None       ,				snake_case    :       PILImageResampling = PILImageResampling.BICUBIC       ,				snake_case    :       bool = True       ,				snake_case    :       Union[int, float] = 1 / 255       ,				snake_case    :       bool = True       ,				snake_case    :       Optional[Union[float, List[float]]] = None       ,				snake_case    :       Optional[Union[float, List[float]]] = None       ,				**snake_case    :       Union[str, Any]       ,				):
          super().__init__(**snake_case )
          __UpperCamelCase     						=  crop_size if crop_size is not None else {'''height''': 256, '''width''': 256}
          __UpperCamelCase     						=  get_size_dict(snake_case       ,				param_name='''crop_size''' )
          __UpperCamelCase     						=  size if size is not None else {'''height''': 224, '''width''': 224}
          __UpperCamelCase     						=  get_size_dict(snake_case )

          __UpperCamelCase     						=  do_center_crop
          __UpperCamelCase     						=  crop_size
          __UpperCamelCase     						=  do_resize
          __UpperCamelCase     						=  size
          __UpperCamelCase     						=  resample
          __UpperCamelCase     						=  do_rescale
          __UpperCamelCase     						=  rescale_factor
          __UpperCamelCase     						=  do_normalize
          __UpperCamelCase     						=  image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
          __UpperCamelCase     						=  image_std if image_std is not None else IMAGENET_DEFAULT_STD







      def 					snake_case		(   self    :       Optional[Any]       ,				snake_case    :       np.ndarray       ,				snake_case    :       Dict[str, int]       ,				snake_case    :       Optional[int] = None       ,				snake_case    :       Optional[Union[str, ChannelDimension]] = None       ,				**snake_case    :       List[Any]       ,				):
          __UpperCamelCase     						=  self.size if size is None else size
          __UpperCamelCase     						=  get_size_dict(snake_case )
          __UpperCamelCase     						=  get_size_dict(snake_case       ,				param_name='''crop_size''' )

          __UpperCamelCase						,					__UpperCamelCase     						=  get_image_size(snake_case )
          __UpperCamelCase     						=  min(snake_case       ,				snake_case )
          __UpperCamelCase     						=  (size['''height'''] / crop_size['''height''']) * min_dim
          __UpperCamelCase     						=  (size['''width'''] / crop_size['''width''']) * min_dim
          return center_crop(snake_case       ,				size=(cropped_height, cropped_width)       ,				data_format=snake_case       ,				**snake_case )







      def 					snake_case		(   self    :       Union[str, Any]       ,				snake_case    :       np.ndarray       ,				snake_case    :       Dict[str, int]       ,				snake_case    :       PILImageResampling = PIL.Image.BICUBIC       ,				snake_case    :       Optional[Union[str, ChannelDimension]] = None       ,				**snake_case    :       List[str]       ,				):
          __UpperCamelCase     						=  get_size_dict(snake_case )
          if "height" not in size or "width" not in size:
              raise ValueError(F"The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}" )
          return resize(
              snake_case       ,				size=(size['''height'''], size['''width'''])       ,				resample=snake_case       ,				data_format=snake_case       ,				**snake_case )







      def 					snake_case		(   self    :       Union[str, Any]       ,				snake_case    :       np.ndarray       ,				snake_case    :       Union[int, float]       ,				snake_case    :       Optional[Union[str, ChannelDimension]] = None       ,				**snake_case    :       Optional[int]       ,				):
          return rescale(snake_case       ,				scale=snake_case       ,				data_format=snake_case       ,				**snake_case )







      def 					snake_case		(   self    :       Optional[int]       ,				snake_case    :       np.ndarray       ,				snake_case    :       Union[float, List[float]]       ,				snake_case    :       Union[float, List[float]]       ,				snake_case    :       Optional[Union[str, ChannelDimension]] = None       ,				**snake_case    :       Any       ,				):
          return normalize(snake_case       ,				mean=snake_case       ,				std=snake_case       ,				data_format=snake_case       ,				**snake_case )







      def 					snake_case		(   self    :       List[Any]       ,				snake_case    :       ImageInput       ,				snake_case    :       Optional[bool] = None       ,				snake_case    :       Optional[Dict[str, int]] = None       ,				snake_case    :       Optional[bool] = None       ,				snake_case    :       Optional[Dict[str, int]] = None       ,				snake_case    :       PILImageResampling = None       ,				snake_case    :       Optional[bool] = None       ,				snake_case    :       Optional[float] = None       ,				snake_case    :       Optional[bool] = None       ,				snake_case    :       Optional[Union[float, List[float]]] = None       ,				snake_case    :       Optional[Union[float, List[float]]] = None       ,				snake_case    :       Optional[Union[str, TensorType]] = None       ,				snake_case    :       ChannelDimension = ChannelDimension.FIRST       ,				**snake_case    :       Tuple       ,				):
          __UpperCamelCase     						=  do_center_crop if do_center_crop is not None else self.do_center_crop
          __UpperCamelCase     						=  crop_size if crop_size is not None else self.crop_size
          __UpperCamelCase     						=  get_size_dict(snake_case       ,				param_name='''crop_size''' )
          __UpperCamelCase     						=  do_resize if do_resize is not None else self.do_resize
          __UpperCamelCase     						=  size if size is not None else self.size
          __UpperCamelCase     						=  get_size_dict(snake_case )
          __UpperCamelCase     						=  resample if resample is not None else self.resample
          __UpperCamelCase     						=  do_rescale if do_rescale is not None else self.do_rescale
          __UpperCamelCase     						=  rescale_factor if rescale_factor is not None else self.rescale_factor
          __UpperCamelCase     						=  do_normalize if do_normalize is not None else self.do_normalize
          __UpperCamelCase     						=  image_mean if image_mean is not None else self.image_mean
          __UpperCamelCase     						=  image_std if image_std is not None else self.image_std

          __UpperCamelCase     						=  make_list_of_images(snake_case )

          if not valid_images(snake_case ):
              raise ValueError(
                  '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
                  '''torch.Tensor, tf.Tensor or jax.ndarray.''' )

          if do_center_crop and crop_size is None:
              raise ValueError('''If `do_center_crop` is set to `True`, `crop_size` must be provided.''' )

          if do_resize and size is None:
              raise ValueError('''Size must be specified if do_resize is True.''' )

          if do_rescale and rescale_factor is None:
              raise ValueError('''Rescale factor must be specified if do_rescale is True.''' )

          if do_normalize and (image_mean is None or image_std is None):
              raise ValueError('''Image mean and image standard deviation must be specified if do_normalize is True.''' )

          # All transformations expect numpy arrays.
          __UpperCamelCase     						=  [to_numpy_array(snake_case ) for image in images]

          if do_center_crop:
              __UpperCamelCase     						=  [self.center_crop(snake_case       ,				snake_case       ,				size=snake_case ) for image in images]

          if do_resize:
              __UpperCamelCase     						=  [self.resize(image=snake_case       ,				size=snake_case       ,				resample=snake_case ) for image in images]

          if do_rescale:
              __UpperCamelCase     						=  [self.rescale(image=snake_case       ,				scale=snake_case ) for image in images]

          if do_normalize:
              __UpperCamelCase     						=  [self.normalize(image=snake_case       ,				mean=snake_case       ,				std=snake_case ) for image in images]

          __UpperCamelCase     						=  [to_channel_dimension_format(snake_case       ,				snake_case ) for image in images]

          __UpperCamelCase     						=  {'''pixel_values''': images}
          return BatchFeature(data=snake_case       ,				tensor_type=snake_case )






