


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




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


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			__lowerCAmelCase							       =     [state.process_index]
			__lowerCAmelCase							       =     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'''




			__lowerCAmelCase							       =     create_tensor(_UpperCamelCase )
			__lowerCAmelCase							       =     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'''




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




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


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




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


def _lowerCamelCase      (						_UpperCamelCase ):

			'''simple docstring'''




			main()


def _lowerCamelCase      (						):

			'''simple docstring'''




			__lowerCAmelCase							       =     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 copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import ClassLabel, Features, Image
from .base import TaskTemplate
@dataclass(frozen=lowerCAmelCase__						)
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       str							 =field(default="""image-classification"""			,metadata={"""include_in_asdict_even_if_is_default""": True}						)
							__UpperCAmelCase		:       ClassVar[Features]							 =Features({"""image""": Image()}						)
							__UpperCAmelCase		:       ClassVar[Features]							 =Features({"""labels""": ClassLabel}						)
							__UpperCAmelCase		:       str							 ="image"
							__UpperCAmelCase		:       str							 ="labels"

							def      snake_case							(	self       ,  __a      ):
										if self.label_column not in features:
													raise ValueError(f"Column {self.label_column} is not present in features."      )
										if not isinstance(features[self.label_column]       ,  __a      ):
													raise ValueError(f"Column {self.label_column} is not a ClassLabel."      )
										__lowerCAmelCase							       =     copy.deepcopy(self      )
										__lowerCAmelCase							       =     self.label_schema.copy()
										__lowerCAmelCase							       =     features[self.label_column]
										__lowerCAmelCase							       =     label_schema
										return task_template



							@property
							def      snake_case							(	self      ):
										return {
										    self.image_column: "image",
										    self.label_column: "labels",
										}




