


"""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    _snake_case  (  _snake_case :	int	):
      return (torch.arange(state.num_processes	) + 1.0 + (state.num_processes * state.process_index)).to(state.device	)


def    _snake_case  (  _snake_case :	Optional[int]	):
      lowerCAmelCase    :		Optional[int]				=      create_tensor(_snake_case	)
      lowerCAmelCase    :		Dict				=      gather(_snake_case	)
      assert gathered_tensor.tolist() == list(range(1       ,      state.num_processes**2 + 1	)	)


def    _snake_case  (  _snake_case :	Dict	):
      lowerCAmelCase    :		Optional[Any]				=      [state.process_index]
      lowerCAmelCase    :		Tuple				=      gather_object(_snake_case	)
      assert len(_snake_case	) == state.num_processes, f'''{gathered_obj}, {len(_snake_case	)} != {state.num_processes}'''
      assert gathered_obj == list(range(state.num_processes	)	), f'''{gathered_obj} != {list(range(state.num_processes	)	)}'''


def    _snake_case  (  _snake_case :	Dict	):
      lowerCAmelCase    :		Dict				=      create_tensor(_snake_case	)
      lowerCAmelCase    :		Optional[Any]				=      broadcast(_snake_case	)
      assert broadcasted_tensor.shape == torch.Size([state.num_processes]	)
      assert broadcasted_tensor.tolist() == list(range(1       ,      state.num_processes + 1	)	)


def    _snake_case  (  _snake_case :	int	):
      # 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:
            lowerCAmelCase    :		str				=      torch.arange(state.num_processes + 1	).to(state.device	)
      else:
            lowerCAmelCase    :		Union[str, Any]				=      torch.arange(state.num_processes	).to(state.device	)
      lowerCAmelCase    :		List[Any]				=      pad_across_processes(_snake_case	)
      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    _snake_case  (  _snake_case :	Any	):
      # For now runs on only two processes
      if state.num_processes != 2:
            return
      lowerCAmelCase    :		Optional[Any]				=      create_tensor(_snake_case	)
      lowerCAmelCase    :		Any				=      reduce(_snake_case       ,      '''sum'''	)
      lowerCAmelCase    :		Optional[int]				=      torch.tensor([4.0, 6]	).to(state.device	)
      assert torch.allclose(_snake_case       ,      _snake_case	), f'''{reduced_tensor} != {truth_tensor}'''


def    _snake_case  (  _snake_case :	List[str]	):
      # For now runs on only two processes
      if state.num_processes != 2:
            return
      lowerCAmelCase    :		Optional[int]				=      create_tensor(_snake_case	)
      lowerCAmelCase    :		Optional[Any]				=      reduce(_snake_case       ,      '''mean'''	)
      lowerCAmelCase    :		Dict				=      torch.tensor([2.0, 3]	).to(state.device	)
      assert torch.allclose(_snake_case       ,      _snake_case	), f'''{reduced_tensor} != {truth_tensor}'''


def    _snake_case  (  _snake_case :	int	):
      # For xla_spawn (TPUs)
      main()


def    _snake_case  (  ):
      lowerCAmelCase    :		Dict				=      PartialState()
      state.print(f'''State: {state}'''	)
      state.print('''testing gather'''	)
      test_gather(_snake_case	)
      state.print('''testing gather_object'''	)
      test_gather_object(_snake_case	)
      state.print('''testing broadcast'''	)
      test_broadcast(_snake_case	)
      state.print('''testing pad_across_processes'''	)
      test_pad_across_processes(_snake_case	)
      state.print('''testing reduce_sum'''	)
      test_reduce_sum(_snake_case	)
      state.print('''testing reduce_mean'''	)
      test_reduce_mean(_snake_case	)


if __name__ == "__main__":
 main()



'''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_video_inputs


if is_torch_available():
  import torch

if is_vision_available():
  from PIL import Image

  from transformers import VivitImageProcessor

class 	lowerCamelCase_	(unittest.TestCase    ):
      '''simple docstring'''

      def __init__(       self						:				int						,					A						:				Dict						,					A						:				Optional[int]=7						,					A						:				Tuple=3						,					A						:				Optional[Any]=10						,					A						:				int=18						,					A						:				Dict=30						,					A						:				List[str]=400						,					A						:				int=True						,					A						:				Optional[Any]=None						,					A						:				Optional[Any]=True						,					A						:				List[Any]=[0.5, 0.5, 0.5]						,					A						:				List[str]=[0.5, 0.5, 0.5]						,					A						:				Optional[int]=None						,					):
             _UpperCAmelCase     :  Dict							      =    size if size is not None else {"shortest_edge": 18}
             _UpperCAmelCase     :  Optional[Any]							      =    crop_size if crop_size is not None else {"height": 18, "width": 18}

             _UpperCAmelCase     :  Tuple							      =    parent
             _UpperCAmelCase     :  Any							      =    batch_size
             _UpperCAmelCase     :  Optional[int]							      =    num_channels
             _UpperCAmelCase     :  Optional[Any]							      =    num_frames
             _UpperCAmelCase     :  Any							      =    image_size
             _UpperCAmelCase     :  Dict							      =    min_resolution
             _UpperCAmelCase     :  Any							      =    max_resolution
             _UpperCAmelCase     :  Optional[int]							      =    do_resize
             _UpperCAmelCase     :  str							      =    size
             _UpperCAmelCase     :  List[Any]							      =    do_normalize
             _UpperCAmelCase     :  Any							      =    image_mean
             _UpperCAmelCase     :  Tuple							      =    image_std
             _UpperCAmelCase     :  Any							      =    crop_size





      def      _A	(       self						:				List[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_size": self.crop_size,
             }

@require_torch
@require_vision
class 	lowerCamelCase_	(snake_case__							, unittest.TestCase    ):
      '''simple docstring'''

      __UpperCamelCase:  Dict						=						VivitImageProcessor if is_vision_available() else None




      def      _A	(       self						:				int       ):
             _UpperCAmelCase     :  Tuple							      =    VivitImageProcessingTester(self       )




      @property
      def      _A	(       self						:				Optional[Any]       ):
             return self.image_processor_tester.prepare_image_processor_dict()




      def      _A	(       self						:				Union[str, Any]       ):
             _UpperCAmelCase     :  str							      =    self.image_processing_class(**self.image_processor_dict       )
             self.assertTrue(hasattr(A						,					"image_mean"       )       )
             self.assertTrue(hasattr(A						,					"image_std"       )       )
             self.assertTrue(hasattr(A						,					"do_normalize"       )       )
             self.assertTrue(hasattr(A						,					"do_resize"       )       )
             self.assertTrue(hasattr(A						,					"do_center_crop"       )       )
             self.assertTrue(hasattr(A						,					"size"       )       )




      def      _A	(       self						:				List[Any]       ):
             _UpperCAmelCase     :  Union[str, Any]							      =    self.image_processing_class.from_dict(self.image_processor_dict       )
             self.assertEqual(image_processor.size						,					{"shortest_edge": 18}       )
             self.assertEqual(image_processor.crop_size						,					{"height": 18, "width": 18}       )

             _UpperCAmelCase     :  Optional[int]							      =    self.image_processing_class.from_dict(self.image_processor_dict						,					size=42						,					crop_size=84       )
             self.assertEqual(image_processor.size						,					{"shortest_edge": 42}       )
             self.assertEqual(image_processor.crop_size						,					{"height": 84, "width": 84}       )




      def      _A	(       self						:				Tuple       ):
             # Initialize image_processing
             _UpperCAmelCase     :  Optional[Any]							      =    self.image_processing_class(**self.image_processor_dict       )
             # create random PIL videos
             _UpperCAmelCase     :  Any							      =    prepare_video_inputs(self.image_processor_tester						,					equal_resolution=A       )
             for video in video_inputs:
                    self.assertIsInstance(A						,					A       )
                    self.assertIsInstance(video[0]						,					Image.Image       )

             # Test not batched input
             _UpperCAmelCase     :  str							      =    image_processing(video_inputs[0]						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_videos.shape						,					(
                     1,
                     self.image_processor_tester.num_frames,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

             # Test batched
             _UpperCAmelCase     :  List[Any]							      =    image_processing(A						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_videos.shape						,					(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_frames,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)




      def      _A	(       self						:				List[Any]       ):
             # Initialize image_processing
             _UpperCAmelCase     :  List[str]							      =    self.image_processing_class(**self.image_processor_dict       )
             # create random numpy tensors
             _UpperCAmelCase     :  List[Any]							      =    prepare_video_inputs(self.image_processor_tester						,					equal_resolution=A						,					numpify=A       )
             for video in video_inputs:
                    self.assertIsInstance(A						,					A       )
                    self.assertIsInstance(video[0]						,					np.ndarray       )

             # Test not batched input
             _UpperCAmelCase     :  Tuple							      =    image_processing(video_inputs[0]						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_videos.shape						,					(
                     1,
                     self.image_processor_tester.num_frames,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

             # Test batched
             _UpperCAmelCase     :  Optional[int]							      =    image_processing(A						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_videos.shape						,					(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_frames,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)





      def      _A	(       self						:				List[Any]       ):
             # Initialize image_processing
             _UpperCAmelCase     :  Optional[Any]							      =    self.image_processing_class(**self.image_processor_dict       )
             # create random PyTorch tensors
             _UpperCAmelCase     :  Optional[int]							      =    prepare_video_inputs(self.image_processor_tester						,					equal_resolution=A						,					torchify=A       )
             for video in video_inputs:
                    self.assertIsInstance(A						,					A       )
                    self.assertIsInstance(video[0]						,					torch.Tensor       )

             # Test not batched input
             _UpperCAmelCase     :  Optional[Any]							      =    image_processing(video_inputs[0]						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_videos.shape						,					(
                     1,
                     self.image_processor_tester.num_frames,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

             # Test batched
             _UpperCAmelCase     :  List[Any]							      =    image_processing(A						,					return_tensors="pt"       ).pixel_values
             self.assertEqual(
                 encoded_videos.shape						,					(
                     self.image_processor_tester.batch_size,
                     self.image_processor_tester.num_frames,
                     self.image_processor_tester.num_channels,
                     self.image_processor_tester.crop_size["height"],
                     self.image_processor_tester.crop_size["width"],
                 )						,					)

