'''simple docstring'''

from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
       import torch

if is_tf_available():
       import tensorflow as tf





class 							lowercase						(						__lowerCamelCase ):
   lowerCamelCase_    					=['image_processor']
   lowerCamelCase_    					='SamImageProcessor'


   def __init__(   self				:	Optional[int]   ,		__lowerCAmelCase				:	str)      ->					List[str]:
         super().__init__(__lowerCAmelCase)
         lowercase_     =      self.image_processor
         lowercase_     =      -10
         lowercase_     =      self.image_processor.size["longest_edge"]


   def __call__(   self				:	int   ,		__lowerCAmelCase				:	Dict=None   ,		__lowerCAmelCase				:	Any=None   ,		__lowerCAmelCase				:	Optional[int]=None   ,		__lowerCAmelCase				:	int=None   ,		__lowerCAmelCase				:	Optional[Union[str, TensorType]] = None   ,		**__lowerCAmelCase				:	List[Any]   ,		)      ->					BatchEncoding:
         lowercase_     =      self.image_processor(
             __lowerCAmelCase   ,		return_tensors=__lowerCAmelCase   ,		**__lowerCAmelCase   ,		)

         # pop arguments that are not used in the foward but used nevertheless
         lowercase_     =      encoding_image_processor["original_sizes"]

         if hasattr(__lowerCAmelCase   ,		"numpy"):  # Checks if Torch or TF tensor
               lowercase_     =      original_sizes.numpy()

         lowercase_ ,							lowercase_ ,							lowercase_     =      self._check_and_preprocess_points(
             input_points=__lowerCAmelCase   ,		input_labels=__lowerCAmelCase   ,		input_boxes=__lowerCAmelCase   ,		)

         lowercase_     =      self._normalize_and_convert(
             __lowerCAmelCase   ,		__lowerCAmelCase   ,		input_points=__lowerCAmelCase   ,		input_labels=__lowerCAmelCase   ,		input_boxes=__lowerCAmelCase   ,		return_tensors=__lowerCAmelCase   ,		)

         return encoding_image_processor


   def __UpperCAmelCase       (   self				:	List[str]   ,		__lowerCAmelCase				:	Union[str, Any]   ,		__lowerCAmelCase				:	Dict   ,		__lowerCAmelCase				:	Optional[Any]=None   ,		__lowerCAmelCase				:	List[str]=None   ,		__lowerCAmelCase				:	Union[str, Any]=None   ,		__lowerCAmelCase				:	List[str]="pt"   ,		)      ->					str:
         if input_points is not None:
               if len(__lowerCAmelCase) != len(__lowerCAmelCase):
                     lowercase_     =      [
                         self._normalize_coordinates(self.target_size   ,		__lowerCAmelCase   ,		original_sizes[0]) for point in input_points
                     ]
               else:
                     lowercase_     =      [
                         self._normalize_coordinates(self.target_size   ,		__lowerCAmelCase   ,		__lowerCAmelCase)
                         for point, original_size in zip(__lowerCAmelCase   ,		__lowerCAmelCase)
                     ]
               # check that all arrays have the same shape
               if not all(point.shape == input_points[0].shape for point in input_points):
                     if input_labels is not None:
                           lowercase_ ,							lowercase_     =      self._pad_points_and_labels(__lowerCAmelCase   ,		__lowerCAmelCase)

               lowercase_     =      np.array(__lowerCAmelCase)

         if input_labels is not None:
               lowercase_     =      np.array(__lowerCAmelCase)

         if input_boxes is not None:
               if len(__lowerCAmelCase) != len(__lowerCAmelCase):
                     lowercase_     =      [
                         self._normalize_coordinates(self.target_size   ,		__lowerCAmelCase   ,		original_sizes[0]   ,		is_bounding_box=__lowerCAmelCase)
                         for box in input_boxes
                     ]
               else:
                     lowercase_     =      [
                         self._normalize_coordinates(self.target_size   ,		__lowerCAmelCase   ,		__lowerCAmelCase   ,		is_bounding_box=__lowerCAmelCase)
                         for box, original_size in zip(__lowerCAmelCase   ,		__lowerCAmelCase)
                     ]
               lowercase_     =      np.array(__lowerCAmelCase)

         if input_boxes is not None:
               if return_tensors == "pt":
                     lowercase_     =      torch.from_numpy(__lowerCAmelCase)
                     # boxes batch size of 1 by default
                     lowercase_     =      input_boxes.unsqueeze(1) if len(input_boxes.shape) != 3 else input_boxes
               elif return_tensors == "tf":
                     lowercase_     =      tf.convert_to_tensor(__lowerCAmelCase)
                     # boxes batch size of 1 by default
                     lowercase_     =      tf.expand_dims(__lowerCAmelCase   ,		1) if len(input_boxes.shape) != 3 else input_boxes
               encoding_image_processor.update({"input_boxes": input_boxes})
         if input_points is not None:
               if return_tensors == "pt":
                     lowercase_     =      torch.from_numpy(__lowerCAmelCase)
                     # point batch size of 1 by default
                     lowercase_     =      input_points.unsqueeze(1) if len(input_points.shape) != 4 else input_points
               elif return_tensors == "tf":
                     lowercase_     =      tf.convert_to_tensor(__lowerCAmelCase)
                     # point batch size of 1 by default
                     lowercase_     =      tf.expand_dims(__lowerCAmelCase   ,		1) if len(input_points.shape) != 4 else input_points
               encoding_image_processor.update({"input_points": input_points})
         if input_labels is not None:
               if return_tensors == "pt":
                     lowercase_     =      torch.from_numpy(__lowerCAmelCase)
                     # point batch size of 1 by default
                     lowercase_     =      input_labels.unsqueeze(1) if len(input_labels.shape) != 3 else input_labels
               elif return_tensors == "tf":
                     lowercase_     =      tf.convert_to_tensor(__lowerCAmelCase)
                     # point batch size of 1 by default
                     lowercase_     =      tf.expand_dims(__lowerCAmelCase   ,		1) if len(input_labels.shape) != 3 else input_labels
               encoding_image_processor.update({"input_labels": input_labels})

         return encoding_image_processor


   def __UpperCAmelCase       (   self				:	str   ,		__lowerCAmelCase				:	Optional[Any]   ,		__lowerCAmelCase				:	Union[str, Any])      ->					Dict:
         lowercase_     =      max([point.shape[0] for point in input_points])
         lowercase_     =      []
         for i, point in enumerate(__lowerCAmelCase):
               if point.shape[0] != expected_nb_points:
                     lowercase_     =      np.concatenate(
                         [point, np.zeros((expected_nb_points - point.shape[0], 2)) + self.point_pad_value]   ,		axis=0)
                     lowercase_     =      np.append(input_labels[i]   ,		[self.point_pad_value])
               processed_input_points.append(__lowerCAmelCase)
         lowercase_     =      processed_input_points
         return input_points, input_labels


   def __UpperCAmelCase       (   self				:	Union[str, Any]   ,		__lowerCAmelCase				:	int   ,		__lowerCAmelCase				:	np.ndarray   ,		__lowerCAmelCase				:	Dict   ,		__lowerCAmelCase				:	Tuple=False)      ->					np.ndarray:
         lowercase_ ,							lowercase_     =      original_size
         lowercase_ ,							lowercase_     =      self.image_processor._get_preprocess_shape(__lowerCAmelCase   ,		longest_edge=__lowerCAmelCase)
         lowercase_     =      deepcopy(__lowerCAmelCase).astype(__lowerCAmelCase)

         if is_bounding_box:
               lowercase_     =      coords.reshape(-1   ,		2   ,		2)

         lowercase_     =      coords[..., 0] * (new_w / old_w)
         lowercase_     =      coords[..., 1] * (new_h / old_h)

         if is_bounding_box:
               lowercase_     =      coords.reshape(-1   ,		4)

         return coords


   def __UpperCAmelCase       (   self				:	Dict   ,		__lowerCAmelCase				:	Optional[int]=None   ,		__lowerCAmelCase				:	Tuple=None   ,		__lowerCAmelCase				:	int=None   ,		)      ->					int:
         if input_points is not None:
               if hasattr(__lowerCAmelCase   ,		"numpy"):  # Checks for TF or Torch tensor
                     lowercase_     =      input_points.numpy().tolist()

               if not isinstance(__lowerCAmelCase   ,		__lowerCAmelCase) or not isinstance(input_points[0]   ,		__lowerCAmelCase):
                     raise ValueError("Input points must be a list of list of floating points.")
               lowercase_     =      [np.array(__lowerCAmelCase) for input_point in input_points]
         else:
               lowercase_     =      None

         if input_labels is not None:
               if hasattr(__lowerCAmelCase   ,		"numpy"):
                     lowercase_     =      input_labels.numpy().tolist()

               if not isinstance(__lowerCAmelCase   ,		__lowerCAmelCase) or not isinstance(input_labels[0]   ,		__lowerCAmelCase):
                     raise ValueError("Input labels must be a list of list integers.")
               lowercase_     =      [np.array(__lowerCAmelCase) for label in input_labels]
         else:
               lowercase_     =      None

         if input_boxes is not None:
               if hasattr(__lowerCAmelCase   ,		"numpy"):
                     lowercase_     =      input_boxes.numpy().tolist()

               if (
                   not isinstance(__lowerCAmelCase   ,		__lowerCAmelCase)
                   or not isinstance(input_boxes[0]   ,		__lowerCAmelCase)
                   or not isinstance(input_boxes[0][0]   ,		__lowerCAmelCase)
               ):
                     raise ValueError("Input boxes must be a list of list of list of floating points.")
               lowercase_     =      [np.array(__lowerCAmelCase).astype(np.floataa) for box in input_boxes]
         else:
               lowercase_     =      None

         return input_points, input_labels, input_boxes


   @property
   def __UpperCAmelCase       (   self				:	Optional[Any])      ->					List[str]:
         lowercase_     =      self.image_processor.model_input_names
         return list(dict.fromkeys(__lowerCAmelCase))


   def __UpperCAmelCase       (   self				:	Any   ,		*__lowerCAmelCase				:	List[Any]   ,		**__lowerCAmelCase				:	int)      ->					Union[str, Any]:
         return self.image_processor.post_process_masks(*__lowerCAmelCase   ,		**__lowerCAmelCase)







'''simple docstring'''

import inspect
import os

import torch
from transformers import AutoModel
from transformers.testing_utils import mockenv_context
from transformers.trainer_utils import set_seed

import accelerate
from accelerate.accelerator import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils.testing import (
    AccelerateTestCase,
    TempDirTestCase,
    execute_subprocess_async,
    require_cuda,
    require_fsdp,
    require_multi_gpu,
    slow,
)
from accelerate.utils.constants import (
    FSDP_AUTO_WRAP_POLICY,
    FSDP_BACKWARD_PREFETCH,
    FSDP_SHARDING_STRATEGY,
    FSDP_STATE_DICT_TYPE,
)
from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin
from accelerate.utils.other import patch_environment


set_seed(42)

lowerCAmelCase_ : Optional[Any]						  =      "bert-base-cased"
lowerCAmelCase_ : Any						  =      "fp16"
lowerCAmelCase_ : Union[str, Any]						  =      "bf16"
lowerCAmelCase_ : List[Any]						  =      [FPaa, BFaa]





@require_fsdp
@require_cuda
class 							lowercase						(						__lowerCamelCase ):


   def __UpperCAmelCase       (   self				:	str)      ->					Union[str, Any]:
         super().setUp()

         lowercase_     =      dict(
             ACCELERATE_USE_FSDP="true"   ,		MASTER_ADDR="localhost"   ,		MASTER_PORT="10999"   ,		RANK="0"   ,		LOCAL_RANK="0"   ,		WORLD_SIZE="1"   ,		)


   def __UpperCAmelCase       (   self				:	Optional[Any])      ->					List[Any]:
         from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy

         for i, strategy in enumerate(__lowerCAmelCase):
               lowercase_     =      self.dist_env.copy()
               lowercase_     =      F'{i + 1}'
               lowercase_     =      strategy
               with mockenv_context(**__lowerCAmelCase):
                     lowercase_     =      FullyShardedDataParallelPlugin()
                     self.assertEqual(fsdp_plugin.sharding_strategy   ,		ShardingStrategy(i + 1))


   def __UpperCAmelCase       (   self				:	Union[str, Any])      ->					List[str]:
         from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch

         for i, prefetch_policy in enumerate(__lowerCAmelCase):
               lowercase_     =      self.dist_env.copy()
               lowercase_     =      prefetch_policy
               with mockenv_context(**__lowerCAmelCase):
                     lowercase_     =      FullyShardedDataParallelPlugin()
                     if prefetch_policy == "NO_PREFETCH":
                           self.assertIsNone(fsdp_plugin.backward_prefetch)
                     else:
                           self.assertEqual(fsdp_plugin.backward_prefetch   ,		BackwardPrefetch(i + 1))


   def __UpperCAmelCase       (   self				:	Dict)      ->					List[str]:
         from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType

         for i, state_dict_type in enumerate(__lowerCAmelCase):
               lowercase_     =      self.dist_env.copy()
               lowercase_     =      state_dict_type
               with mockenv_context(**__lowerCAmelCase):
                     lowercase_     =      FullyShardedDataParallelPlugin()
                     self.assertEqual(fsdp_plugin.state_dict_type   ,		StateDictType(i + 1))
                     if state_dict_type == "FULL_STATE_DICT":
                           self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu)
                           self.assertTrue(fsdp_plugin.state_dict_config.ranka_only)


   def __UpperCAmelCase       (   self				:	Optional[int])      ->					List[Any]:
         lowercase_     =      AutoModel.from_pretrained(__lowerCAmelCase)
         for policy in FSDP_AUTO_WRAP_POLICY:
               lowercase_     =      self.dist_env.copy()
               lowercase_     =      policy
               if policy == "TRANSFORMER_BASED_WRAP":
                     lowercase_     =      "BertLayer"
               elif policy == "SIZE_BASED_WRAP":
                     lowercase_     =      "2000"
               with mockenv_context(**__lowerCAmelCase):
                     lowercase_     =      FullyShardedDataParallelPlugin()
                     fsdp_plugin.set_auto_wrap_policy(__lowerCAmelCase)
                     if policy == "NO_WRAP":
                           self.assertIsNone(fsdp_plugin.auto_wrap_policy)
                     else:
                           self.assertIsNotNone(fsdp_plugin.auto_wrap_policy)

         lowercase_     =      self.dist_env.copy()
         lowercase_     =      "TRANSFORMER_BASED_WRAP"
         lowercase_     =      "T5Layer"
         with mockenv_context(**__lowerCAmelCase):
               lowercase_     =      FullyShardedDataParallelPlugin()
               with self.assertRaises(__lowerCAmelCase) as cm:
                     fsdp_plugin.set_auto_wrap_policy(__lowerCAmelCase)
               self.assertTrue("Could not find the transformer layer class to wrap in the model." in str(cm.exception))

         lowercase_     =      self.dist_env.copy()
         lowercase_     =      "SIZE_BASED_WRAP"
         lowercase_     =      "0"
         with mockenv_context(**__lowerCAmelCase):
               lowercase_     =      FullyShardedDataParallelPlugin()
               fsdp_plugin.set_auto_wrap_policy(__lowerCAmelCase)
               self.assertIsNone(fsdp_plugin.auto_wrap_policy)


   def __UpperCAmelCase       (   self				:	Union[str, Any])      ->					int:
         from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision
         from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler

         for mp_dtype in dtypes:
               lowercase_     =      self.dist_env.copy()
               lowercase_     =      mp_dtype
               with mockenv_context(**__lowerCAmelCase):
                     lowercase_     =      Accelerator()
                     if mp_dtype == "fp16":
                           lowercase_     =      torch.floataa
                     elif mp_dtype == "bf16":
                           lowercase_     =      torch.bfloataa
                     lowercase_     =      MixedPrecision(param_dtype=__lowerCAmelCase   ,		reduce_dtype=__lowerCAmelCase   ,		buffer_dtype=__lowerCAmelCase)
                     self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy   ,		__lowerCAmelCase)
                     if mp_dtype == FPaa:
                           self.assertTrue(isinstance(accelerator.scaler   ,		__lowerCAmelCase))
                     elif mp_dtype == BFaa:
                           self.assertIsNone(accelerator.scaler)
                     AcceleratorState._reset_state(__lowerCAmelCase)


   def __UpperCAmelCase       (   self				:	List[str])      ->					Dict:
         from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload

         for flag in [True, False]:
               lowercase_     =      self.dist_env.copy()
               lowercase_     =      str(__lowerCAmelCase).lower()
               with mockenv_context(**__lowerCAmelCase):
                     lowercase_     =      FullyShardedDataParallelPlugin()
                     self.assertEqual(fsdp_plugin.cpu_offload   ,		CPUOffload(offload_params=__lowerCAmelCase))





@require_fsdp
@require_multi_gpu
@slow
class 							lowercase						(						__lowerCamelCase ):


   def __UpperCAmelCase       (   self				:	Optional[int])      ->					str:
         super().setUp()
         lowercase_     =      0.82
         lowercase_     =      [
             "fsdp_shard_grad_op_transformer_based_wrap",
             "fsdp_full_shard_transformer_based_wrap",
         ]
         lowercase_     =      {
             "multi_gpu_fp16": 3200,
             "fsdp_shard_grad_op_transformer_based_wrap_fp16": 2000,
             "fsdp_full_shard_transformer_based_wrap_fp16": 1900,
             # Disabling below test as it overwhelms the RAM memory usage
             # on CI self-hosted runner leading to tests getting killed.
             # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500,  # fp16 was leading to indefinite hang
         }
         lowercase_     =      160
         lowercase_     =      160

         lowercase_     =      inspect.getfile(accelerate.test_utils)
         lowercase_     =      os.path.sep.join(mod_file.split(os.path.sep)[:-1] + ["scripts", "external_deps"])


   def __UpperCAmelCase       (   self				:	Optional[Any])      ->					Optional[Any]:
         lowercase_     =      os.path.join(self.test_scripts_folder   ,		"test_performance.py")
         lowercase_     =      ["accelerate", "launch", "--num_processes=2", "--num_machines=1", "--machine_rank=0", "--use_fsdp"]
         for config in self.performance_configs:
               lowercase_     =      cmd.copy()
               for i, strategy in enumerate(__lowerCAmelCase):
                     if strategy.lower() in config:
                           cmd_config.append(F'--fsdp_sharding_strategy={i+1}')
                           break

               if "fp32" in config:
                     cmd_config.append("--mixed_precision=no")
               else:
                     cmd_config.append("--mixed_precision=fp16")

               if "cpu_offload" in config:
                     cmd_config.append("--fsdp_offload_params=True")

               for policy in FSDP_AUTO_WRAP_POLICY:
                     if policy.lower() in config:
                           cmd_config.append(F'--fsdp_auto_wrap_policy={policy}')
                           break

               if policy == "TRANSFORMER_BASED_WRAP":
                     cmd_config.append("--fsdp_transformer_layer_cls_to_wrap=BertLayer")
               elif policy == "SIZE_BASED_WRAP":
                     cmd_config.append("--fsdp_min_num_params=2000")

               cmd_config.extend(
                   [
                       self.test_file_path,
                       F'--output_dir={self.tmpdir}',
                       F'--performance_lower_bound={self.performance_lower_bound}',
                   ])
               with patch_environment(omp_num_threads=1):
                     execute_subprocess_async(__lowerCAmelCase   ,		env=os.environ.copy())


   def __UpperCAmelCase       (   self				:	Dict)      ->					Dict:
         lowercase_     =      os.path.join(self.test_scripts_folder   ,		"test_checkpointing.py")
         lowercase_     =      [
             "accelerate",
             "launch",
             "--num_processes=2",
             "--num_machines=1",
             "--machine_rank=0",
             "--use_fsdp",
             "--mixed_precision=fp16",
             "--fsdp_transformer_layer_cls_to_wrap=BertLayer",
         ]

         for i, strategy in enumerate(__lowerCAmelCase):
               lowercase_     =      cmd.copy()
               cmd_config.append(F'--fsdp_sharding_strategy={i+1}')
               if strategy != "FULL_SHARD":
                     continue
               lowercase_     =      len(__lowerCAmelCase)
               for state_dict_type in FSDP_STATE_DICT_TYPE:
                     lowercase_     =      cmd_config[:state_dict_config_index]
                     cmd_config.append(F'--fsdp_state_dict_type={state_dict_type}')
                     cmd_config.extend(
                         [
                             self.test_file_path,
                             F'--output_dir={self.tmpdir}',
                             "--partial_train_epoch=1",
                         ])
                     with patch_environment(omp_num_threads=1):
                           execute_subprocess_async(__lowerCAmelCase   ,		env=os.environ.copy())

                     lowercase_     =      cmd_config[:-1]
                     lowercase_     =      os.path.join(self.tmpdir   ,		"epoch_0")
                     cmd_config.extend(
                         [
                             F'--resume_from_checkpoint={resume_from_checkpoint}',
                         ])
                     with patch_environment(omp_num_threads=1):
                           execute_subprocess_async(__lowerCAmelCase   ,		env=os.environ.copy())


   def __UpperCAmelCase       (   self				:	Optional[int])      ->					int:
         lowercase_     =      os.path.join(self.test_scripts_folder   ,		"test_peak_memory_usage.py")
         lowercase_     =      [
             "accelerate",
             "launch",
             "--num_processes=2",
             "--num_machines=1",
             "--machine_rank=0",
         ]
         for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items():
               lowercase_     =      cmd.copy()
               if "fp16" in spec:
                     cmd_config.extend(["--mixed_precision=fp16"])
               else:
                     cmd_config.extend(["--mixed_precision=no"])

               if "multi_gpu" in spec:
                     continue
               else:
                     cmd_config.extend(["--use_fsdp"])
                     for i, strategy in enumerate(__lowerCAmelCase):
                           if strategy.lower() in spec:
                                 cmd_config.append(F'--fsdp_sharding_strategy={i+1}')
                                 break

                     if "cpu_offload" in spec:
                           cmd_config.append("--fsdp_offload_params=True")

                     for policy in FSDP_AUTO_WRAP_POLICY:
                           if policy.lower() in spec:
                                 cmd_config.append(F'--fsdp_auto_wrap_policy={policy}')
                                 break

                     if policy == "TRANSFORMER_BASED_WRAP":
                           cmd_config.append("--fsdp_transformer_layer_cls_to_wrap=BertLayer")
                     elif policy == "SIZE_BASED_WRAP":
                           cmd_config.append("--fsdp_min_num_params=2000")

               cmd_config.extend(
                   [
                       self.test_file_path,
                       F'--output_dir={self.tmpdir}',
                       F'--peak_memory_upper_bound={peak_mem_upper_bound}',
                       F'--n_train={self.n_train}',
                       F'--n_val={self.n_val}',
                   ])
               with patch_environment(omp_num_threads=1):
                     execute_subprocess_async(__lowerCAmelCase   ,		env=os.environ.copy())






