



'''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   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



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



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							__lowercase       	=create_tensor(_lowerCAmelCase					)
							__lowercase       	=gather(_lowerCAmelCase					)
							assert gathered_tensor.tolist() == list(range(1  ,   state.num_processes**2 + 1					)					)



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							__lowercase       	=[state.process_index]
							__lowercase       	=gather_object(_lowerCAmelCase					)
							assert len(_lowerCAmelCase					) == state.num_processes, f"""{gathered_obj}, {len(_lowerCAmelCase					)} != {state.num_processes}"""
							assert gathered_obj == list(range(state.num_processes					)					), f"""{gathered_obj} != {list(range(state.num_processes					)					)}"""



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							__lowercase       	=create_tensor(_lowerCAmelCase					)
							__lowercase       	=broadcast(_lowerCAmelCase					)
							assert broadcasted_tensor.shape == torch.Size([state.num_processes]					)
							assert broadcasted_tensor.tolist() == list(range(1  ,   state.num_processes + 1					)					)



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							if state.is_main_process:
														__lowercase       	=torch.arange(state.num_processes + 1					).to(state.device					)
							else:
														__lowercase       	=torch.arange(state.num_processes					).to(state.device					)
							__lowercase       	=pad_across_processes(_lowerCAmelCase					)
							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   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							if state.num_processes != 2:
														return
							__lowercase       	=create_tensor(_lowerCAmelCase					)
							__lowercase       	=reduce(_lowerCAmelCase  ,   'sum'					)
							__lowercase       	=torch.tensor([4.0, 6]					).to(state.device					)
							assert torch.allclose(_lowerCAmelCase  ,   _lowerCAmelCase					), f"""{reduced_tensor} != {truth_tensor}"""



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							if state.num_processes != 2:
														return
							__lowercase       	=create_tensor(_lowerCAmelCase					)
							__lowercase       	=reduce(_lowerCAmelCase  ,   'mean'					)
							__lowercase       	=torch.tensor([2.0, 3]					).to(state.device					)
							assert torch.allclose(_lowerCAmelCase  ,   _lowerCAmelCase					), f"""{reduced_tensor} != {truth_tensor}"""



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							main()



def   _A       (   ):



							"""simple docstring"""



							__lowercase       	=PartialState()
							state.print(f"""State: {state}"""					)
							state.print('testing gather'					)
							test_gather(_lowerCAmelCase					)
							state.print('testing gather_object'					)
							test_gather_object(_lowerCAmelCase					)
							state.print('testing broadcast'					)
							test_broadcast(_lowerCAmelCase					)
							state.print('testing pad_across_processes'					)
							test_pad_across_processes(_lowerCAmelCase					)
							state.print('testing reduce_sum'					)
							test_reduce_sum(_lowerCAmelCase					)
							state.print('testing reduce_mean'					)
							test_reduce_mean(_lowerCAmelCase					)


if __name__ == "__main__":
		main()








'''simple docstring'''



import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType





class _UpperCamelCase		(     A		):





				'''simple docstring'''





				lowerCAmelCase__					    =    ["""image_processor""", """tokenizer"""]
				lowerCAmelCase__					    =    """ViltImageProcessor"""
				lowerCAmelCase__					    =    ("""BertTokenizer""", """BertTokenizerFast""")

				def __init__(							self  :  str  ,       _lowerCAmelCase  :  Optional[Any]=None  ,       _lowerCAmelCase  :  int=None  ,       **_lowerCAmelCase  :  Optional[Any]):







											'''simple docstring'''

											__lowercase       	=None
											if "feature_extractor" in kwargs:
																		warnings.warn(
																		    'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
																		    ' instead.'  ,       _lowerCAmelCase  ,       )
																		__lowercase       	=kwargs.pop('feature_extractor')

											__lowercase       	=image_processor if image_processor is not None else feature_extractor
											if image_processor is None:
																		raise ValueError('You need to specify an `image_processor`.')
											if tokenizer is None:
																		raise ValueError('You need to specify a `tokenizer`.')

											super().__init__(_lowerCAmelCase  ,       _lowerCAmelCase)
											__lowercase       	=self.image_processor

				def __call__(							self  :  List[Any]  ,       _lowerCAmelCase  :  Tuple  ,       _lowerCAmelCase  :  Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None  ,       _lowerCAmelCase  :  bool = True  ,       _lowerCAmelCase  :  Union[bool, str, PaddingStrategy] = False  ,       _lowerCAmelCase  :  Union[bool, str, TruncationStrategy] = None  ,       _lowerCAmelCase  :  Optional[int] = None  ,       _lowerCAmelCase  :  int = 0  ,       _lowerCAmelCase  :  Optional[int] = None  ,       _lowerCAmelCase  :  Optional[bool] = None  ,       _lowerCAmelCase  :  Optional[bool] = None  ,       _lowerCAmelCase  :  bool = False  ,       _lowerCAmelCase  :  bool = False  ,       _lowerCAmelCase  :  bool = False  ,       _lowerCAmelCase  :  bool = False  ,       _lowerCAmelCase  :  bool = True  ,       _lowerCAmelCase  :  Optional[Union[str, TensorType]] = None  ,       **_lowerCAmelCase  :  Any  ,       ):







											'''simple docstring'''

											__lowercase       	=self.tokenizer(
											    text=_lowerCAmelCase  ,       add_special_tokens=_lowerCAmelCase  ,       padding=_lowerCAmelCase  ,       truncation=_lowerCAmelCase  ,       max_length=_lowerCAmelCase  ,       stride=_lowerCAmelCase  ,       pad_to_multiple_of=_lowerCAmelCase  ,       return_token_type_ids=_lowerCAmelCase  ,       return_attention_mask=_lowerCAmelCase  ,       return_overflowing_tokens=_lowerCAmelCase  ,       return_special_tokens_mask=_lowerCAmelCase  ,       return_offsets_mapping=_lowerCAmelCase  ,       return_length=_lowerCAmelCase  ,       verbose=_lowerCAmelCase  ,       return_tensors=_lowerCAmelCase  ,       **_lowerCAmelCase  ,       )
											# add pixel_values + pixel_mask
											__lowercase       	=self.image_processor(_lowerCAmelCase  ,       return_tensors=_lowerCAmelCase)
											encoding.update(_lowerCAmelCase)

											return encoding

				def 				__lowerCamelCase     (							self  :  List[Any]  ,       *_lowerCAmelCase  :  str  ,       **_lowerCAmelCase  :  Union[str, Any]):







											'''simple docstring'''

											return self.tokenizer.batch_decode(*_lowerCAmelCase  ,       **_lowerCAmelCase)

				def 				__lowerCamelCase     (							self  :  Union[str, Any]  ,       *_lowerCAmelCase  :  str  ,       **_lowerCAmelCase  :  Optional[Any]):







											'''simple docstring'''

											return self.tokenizer.decode(*_lowerCAmelCase  ,       **_lowerCAmelCase)

				@property
				def 				__lowerCamelCase     (							self  :  Optional[int]):







											'''simple docstring'''

											__lowercase       	=self.tokenizer.model_input_names
											__lowercase       	=self.image_processor.model_input_names
											return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))

				@property
				def 				__lowerCamelCase     (							self  :  Union[str, Any]):







											'''simple docstring'''

											warnings.warn(
											    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'  ,       _lowerCAmelCase  ,       )
											return self.image_processor_class

				@property
				def 				__lowerCamelCase     (							self  :  Tuple):







											'''simple docstring'''

											warnings.warn(
											    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'  ,       _lowerCAmelCase  ,       )
											return self.image_processor



