






"""simple docstring"""




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  return (torch.arange(state.num_processes				) + 1.0 + (state.num_processes * state.process_index)).to(state.device				)

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  _a  :       List[Any]             =	create_tensor(UpperCamelCase__				)
  _a  :       Optional[Any]             =	gather(UpperCamelCase__				)
  assert gathered_tensor.tolist() == list(range(1     ,   state.num_processes**2 + 1				)				)

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  _a  :       Dict             =	[state.process_index]
  _a  :       List[Any]             =	gather_object(UpperCamelCase__				)
  assert len(UpperCamelCase__				) == state.num_processes, F"""{gathered_obj}, {len(UpperCamelCase__				)} != {state.num_processes}"""
  assert gathered_obj == list(range(state.num_processes				)				), F"""{gathered_obj} != {list(range(state.num_processes				)				)}"""

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  _a  :       Any             =	create_tensor(UpperCamelCase__				)
  _a  :       Optional[Any]             =	broadcast(UpperCamelCase__				)
  assert broadcasted_tensor.shape == torch.Size([state.num_processes]				)
  assert broadcasted_tensor.tolist() == list(range(1     ,   state.num_processes + 1				)				)

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  # We need to pad the tensor with one more element if we are the main process
  # to ensure that we can pad
  if state.is_main_process:
    _a  :       Optional[int]             =	torch.arange(state.num_processes + 1				).to(state.device				)
  else:
    _a  :       Dict             =	torch.arange(state.num_processes				).to(state.device				)
  _a  :       Union[str, Any]             =	pad_across_processes(UpperCamelCase__				)
  assert padded_tensor.shape == torch.Size([state.num_processes + 1]				)
  if not state.is_main_process:
    assert padded_tensor.tolist() == list(range(0     ,   state.num_processes				)				) + [0]

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  _a  :       List[str]             =	create_tensor(UpperCamelCase__				)
  _a  :       int             =	reduce(UpperCamelCase__     ,   """sum"""				)
  _a  :       Tuple             =	torch.tensor([4.0, 6]				).to(state.device				)
  assert torch.allclose(UpperCamelCase__     ,   UpperCamelCase__				), F"""{reduced_tensor} != {truth_tensor}"""

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  # For now runs on only two processes
  if state.num_processes != 2:
    return
  _a  :       Optional[int]             =	create_tensor(UpperCamelCase__				)
  _a  :       Union[str, Any]             =	reduce(UpperCamelCase__     ,   """mean"""				)
  _a  :       List[Any]             =	torch.tensor([2.0, 3]				).to(state.device				)
  assert torch.allclose(UpperCamelCase__     ,   UpperCamelCase__				), F"""{reduced_tensor} != {truth_tensor}"""

def 			lowerCAmelCase__     (     UpperCamelCase__				):



  '''simple docstring'''
  # For xla_spawn (TPUs)
  main()

def 			lowerCAmelCase__     (     ):



  '''simple docstring'''
  _a  :       List[Any]             =	PartialState()
  state.print(F"""State: {state}"""				)
  state.print("""testing gather"""				)
  test_gather(UpperCamelCase__				)
  state.print("""testing gather_object"""				)
  test_gather_object(UpperCamelCase__				)
  state.print("""testing broadcast"""				)
  test_broadcast(UpperCamelCase__				)
  state.print("""testing pad_across_processes"""				)
  test_pad_across_processes(UpperCamelCase__				)
  state.print("""testing reduce_sum"""				)
  test_reduce_sum(UpperCamelCase__				)
  state.print("""testing reduce_mean"""				)
  test_reduce_mean(UpperCamelCase__				)


if __name__ == "__main__":
 main()









"""simple docstring"""




import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
 import torch

if is_tf_available():
 import tensorflow as tf






@require_vision
@require_torchvision
class        UpperCamelCase			(	unittest.TestCase      ):


 def _lowercase	(     self		:						List[Any]							)					->       Dict:
   _a  :       Optional[int]             =	tempfile.mkdtemp()
   _a  :       Optional[Any]             =	SamImageProcessor()
   _a  :       int             =	SamProcessor(UpperCAmelCase__							)
   processor.save_pretrained(self.tmpdirname							)


 def _lowercase	(     self		:						Tuple							,						**UpperCAmelCase__		:						Any							)					->       Any:
   return AutoProcessor.from_pretrained(self.tmpdirname							,						**UpperCAmelCase__							).image_processor


 def _lowercase	(     self		:						str							)					->       int:
   shutil.rmtree(self.tmpdirname							)


 def _lowercase	(     self		:						Tuple							)					->       Dict:
   _a  :       List[Any]             =	[np.random.randint(255							,						size=(3, 30, 400)							,						dtype=np.uinta							)]

   _a  :       Optional[int]             =	[Image.fromarray(np.moveaxis(UpperCAmelCase__							,						0							,						-1							)							) for x in image_inputs]

   return image_inputs


 def _lowercase	(     self		:						Dict							)					->       Dict:
   _a  :       List[Any]             =	SamProcessor(image_processor=self.get_image_processor()							)
   processor.save_pretrained(self.tmpdirname							)

   _a  :       Tuple             =	self.get_image_processor(do_normalize=UpperCAmelCase__							,						padding_value=1.0							)

   _a  :       Tuple             =	SamProcessor.from_pretrained(self.tmpdirname							,						do_normalize=UpperCAmelCase__							,						padding_value=1.0							)

   self.assertEqual(processor.image_processor.to_json_string()							,						image_processor_add_kwargs.to_json_string()							)
   self.assertIsInstance(processor.image_processor							,						UpperCAmelCase__							)


 def _lowercase	(     self		:						Union[str, Any]							)					->       Tuple:
   _a  :       Optional[Any]             =	self.get_image_processor()

   _a  :       int             =	SamProcessor(image_processor=UpperCAmelCase__							)

   _a  :       Union[str, Any]             =	self.prepare_image_inputs()

   _a  :       List[str]             =	image_processor(UpperCAmelCase__							,						return_tensors="""np"""							)
   _a  :       List[str]             =	processor(images=UpperCAmelCase__							,						return_tensors="""np"""							)

   input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
   input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop original_sizes as it is popped in the processor

   for key in input_feat_extract.keys():
     self.assertAlmostEqual(input_feat_extract[key].sum()							,						input_processor[key].sum()							,						delta=1E-2							)







 @require_torch
 def _lowercase	(     self		:						Optional[Any]							)					->       Optional[Any]:
   _a  :       Optional[Any]             =	self.get_image_processor()

   _a  :       Dict             =	SamProcessor(image_processor=UpperCAmelCase__							)
   _a  :       Tuple             =	[torch.ones((1, 3, 5, 5)							)]

   _a  :       Tuple             =	[[1764, 2646]]

   _a  :       Optional[int]             =	[[683, 1024]]
   _a  :       List[Any]             =	processor.post_process_masks(UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							)
   self.assertEqual(masks[0].shape							,						(1, 3, 1764, 2646)							)

   _a  :       int             =	processor.post_process_masks(
       UpperCAmelCase__							,						torch.tensor(UpperCAmelCase__							)							,						torch.tensor(UpperCAmelCase__							)							)
   self.assertEqual(masks[0].shape							,						(1, 3, 1764, 2646)							)

   # should also work with np
   _a  :       Optional[Any]             =	[np.ones((1, 3, 5, 5)							)]
   _a  :       Tuple             =	processor.post_process_masks(UpperCAmelCase__							,						np.array(UpperCAmelCase__							)							,						np.array(UpperCAmelCase__							)							)

   self.assertEqual(masks[0].shape							,						(1, 3, 1764, 2646)							)

   _a  :       List[str]             =	[[1, 0], [0, 1]]
   with self.assertRaises(UpperCAmelCase__							):
     _a  :       str             =	processor.post_process_masks(UpperCAmelCase__							,						np.array(UpperCAmelCase__							)							,						np.array(UpperCAmelCase__							)							)







@require_vision
@require_tf
class        UpperCamelCase			(	unittest.TestCase      ):


 def _lowercase	(     self		:						Any							)					->       List[str]:
   _a  :       List[str]             =	tempfile.mkdtemp()
   _a  :       Any             =	SamImageProcessor()
   _a  :       Union[str, Any]             =	SamProcessor(UpperCAmelCase__							)
   processor.save_pretrained(self.tmpdirname							)


 def _lowercase	(     self		:						List[str]							,						**UpperCAmelCase__		:						Any							)					->       List[str]:
   return AutoProcessor.from_pretrained(self.tmpdirname							,						**UpperCAmelCase__							).image_processor


 def _lowercase	(     self		:						Optional[Any]							)					->       Union[str, Any]:
   shutil.rmtree(self.tmpdirname							)


 def _lowercase	(     self		:						Dict							)					->       List[str]:
   _a  :       List[Any]             =	[np.random.randint(255							,						size=(3, 30, 400)							,						dtype=np.uinta							)]

   _a  :       List[Any]             =	[Image.fromarray(np.moveaxis(UpperCAmelCase__							,						0							,						-1							)							) for x in image_inputs]

   return image_inputs


 def _lowercase	(     self		:						Union[str, Any]							)					->       Union[str, Any]:
   _a  :       Optional[int]             =	SamProcessor(image_processor=self.get_image_processor()							)
   processor.save_pretrained(self.tmpdirname							)

   _a  :       str             =	self.get_image_processor(do_normalize=UpperCAmelCase__							,						padding_value=1.0							)

   _a  :       Union[str, Any]             =	SamProcessor.from_pretrained(self.tmpdirname							,						do_normalize=UpperCAmelCase__							,						padding_value=1.0							)

   self.assertEqual(processor.image_processor.to_json_string()							,						image_processor_add_kwargs.to_json_string()							)
   self.assertIsInstance(processor.image_processor							,						UpperCAmelCase__							)


 def _lowercase	(     self		:						List[Any]							)					->       str:
   _a  :       Union[str, Any]             =	self.get_image_processor()

   _a  :       Dict             =	SamProcessor(image_processor=UpperCAmelCase__							)

   _a  :       int             =	self.prepare_image_inputs()

   _a  :       List[str]             =	image_processor(UpperCAmelCase__							,						return_tensors="""np"""							)
   _a  :       List[str]             =	processor(images=UpperCAmelCase__							,						return_tensors="""np"""							)

   input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
   input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop reshaped_input_sizes as it is popped in the processor

   for key in input_feat_extract.keys():
     self.assertAlmostEqual(input_feat_extract[key].sum()							,						input_processor[key].sum()							,						delta=1E-2							)







 @require_tf
 def _lowercase	(     self		:						Optional[Any]							)					->       int:
   _a  :       Optional[Any]             =	self.get_image_processor()

   _a  :       Dict             =	SamProcessor(image_processor=UpperCAmelCase__							)
   _a  :       Any             =	[tf.ones((1, 3, 5, 5)							)]

   _a  :       Tuple             =	[[1764, 2646]]

   _a  :       str             =	[[683, 1024]]
   _a  :       Union[str, Any]             =	processor.post_process_masks(UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						return_tensors="""tf"""							)
   self.assertEqual(masks[0].shape							,						(1, 3, 1764, 2646)							)

   _a  :       Union[str, Any]             =	processor.post_process_masks(
       UpperCAmelCase__							,						tf.convert_to_tensor(UpperCAmelCase__							)							,						tf.convert_to_tensor(UpperCAmelCase__							)							,						return_tensors="""tf"""							,						)
   self.assertEqual(masks[0].shape							,						(1, 3, 1764, 2646)							)

   # should also work with np
   _a  :       List[Any]             =	[np.ones((1, 3, 5, 5)							)]
   _a  :       Optional[int]             =	processor.post_process_masks(
       UpperCAmelCase__							,						np.array(UpperCAmelCase__							)							,						np.array(UpperCAmelCase__							)							,						return_tensors="""tf"""							)

   self.assertEqual(masks[0].shape							,						(1, 3, 1764, 2646)							)

   _a  :       Dict             =	[[1, 0], [0, 1]]
   with self.assertRaises(tf.errors.InvalidArgumentError							):
     _a  :       List[Any]             =	processor.post_process_masks(
         UpperCAmelCase__							,						np.array(UpperCAmelCase__							)							,						np.array(UpperCAmelCase__							)							,						return_tensors="""tf"""							)







@require_vision
@require_torchvision
class        UpperCamelCase			(	unittest.TestCase      ):


 def _lowercase	(     self		:						str							)					->       Optional[Any]:
   _a  :       Optional[Any]             =	tempfile.mkdtemp()
   _a  :       Dict             =	SamImageProcessor()
   _a  :       List[str]             =	SamProcessor(UpperCAmelCase__							)
   processor.save_pretrained(self.tmpdirname							)


 def _lowercase	(     self		:						Any							,						**UpperCAmelCase__		:						Dict							)					->       int:
   return AutoProcessor.from_pretrained(self.tmpdirname							,						**UpperCAmelCase__							).image_processor


 def _lowercase	(     self		:						Tuple							)					->       List[Any]:
   shutil.rmtree(self.tmpdirname							)


 def _lowercase	(     self		:						str							)					->       int:
   _a  :       str             =	[np.random.randint(255							,						size=(3, 30, 400)							,						dtype=np.uinta							)]

   _a  :       int             =	[Image.fromarray(np.moveaxis(UpperCAmelCase__							,						0							,						-1							)							) for x in image_inputs]

   return image_inputs


 @is_pt_tf_cross_test
 def _lowercase	(     self		:						int							)					->       List[Any]:
   _a  :       Optional[Any]             =	self.get_image_processor()

   _a  :       Optional[Any]             =	SamProcessor(image_processor=UpperCAmelCase__							)
   _a  :       Tuple             =	np.random.randint(0							,						2							,						size=(1, 3, 5, 5)							).astype(np.floataa							)
   _a  :       str             =	[tf.convert_to_tensor(UpperCAmelCase__							)]
   _a  :       Optional[int]             =	[torch.tensor(UpperCAmelCase__							)]

   _a  :       Union[str, Any]             =	[[1764, 2646]]

   _a  :       List[str]             =	[[683, 1024]]
   _a  :       Optional[int]             =	processor.post_process_masks(
       UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						return_tensors="""tf"""							)
   _a  :       List[str]             =	processor.post_process_masks(
       UpperCAmelCase__							,						UpperCAmelCase__							,						UpperCAmelCase__							,						return_tensors="""pt"""							)

   self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)







 @is_pt_tf_cross_test
 def _lowercase	(     self		:						str							)					->       Optional[Any]:
   _a  :       List[Any]             =	self.get_image_processor()

   _a  :       Any             =	SamProcessor(image_processor=UpperCAmelCase__							)

   _a  :       Dict             =	self.prepare_image_inputs()

   _a  :       List[str]             =	image_processor(UpperCAmelCase__							,						return_tensors="""pt"""							)["""pixel_values"""].numpy()
   _a  :       str             =	processor(images=UpperCAmelCase__							,						return_tensors="""pt"""							)["""pixel_values"""].numpy()

   _a  :       Optional[Any]             =	image_processor(UpperCAmelCase__							,						return_tensors="""tf"""							)["""pixel_values"""].numpy()
   _a  :       Optional[int]             =	processor(images=UpperCAmelCase__							,						return_tensors="""tf"""							)["""pixel_values"""].numpy()

   self.assertTrue(np.allclose(UpperCAmelCase__							,						UpperCAmelCase__							)							)
   self.assertTrue(np.allclose(UpperCAmelCase__							,						UpperCAmelCase__							)							)
   self.assertTrue(np.allclose(UpperCAmelCase__							,						UpperCAmelCase__							)							)

