






'''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 	__magic_name__( _A					):
						'''simple docstring'''





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







def 	__magic_name__( _A					):
						'''simple docstring'''





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







def 	__magic_name__( _A					):
						'''simple docstring'''





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







def 	__magic_name__( _A					):
						'''simple docstring'''





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







def 	__magic_name__( _A					):
						'''simple docstring'''





						if state.is_main_process:
												UpperCamelCase__         = torch.arange(state.num_processes + 1					).to(state.device					)
						else:
												UpperCamelCase__         = torch.arange(state.num_processes					).to(state.device					)
						UpperCamelCase__         = pad_across_processes(_A					)
						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 	__magic_name__( _A					):
						'''simple docstring'''





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







def 	__magic_name__( _A					):
						'''simple docstring'''





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







def 	__magic_name__( _A					):
						'''simple docstring'''





						main()







def 	__magic_name__( ):
						'''simple docstring'''





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


if __name__ == "__main__":
					main()








'''simple docstring'''


import numpy as np

from transformers import Pipeline







def 	__magic_name__( _A					):
						'''simple docstring'''





						UpperCamelCase__         = np.max(_A						,      axis=-1						,      keepdims=_A					)
						UpperCamelCase__         = np.exp(outputs - maxes					)
						return shifted_exp / shifted_exp.sum(axis=-1						,      keepdims=_A					)







class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			def 				A      (   self							:						int ,      **lowercase							:						List[Any]			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = {}
									if "second_text" in kwargs:
															UpperCamelCase__         = kwargs["""second_text"""]
									return preprocess_kwargs, {}, {}






			def 				A      (   self							:						Optional[int] ,      lowercase							:						Union[str, Any] ,      lowercase							:						Any=None			)							->    Optional[int]:

									'''simple docstring'''







									return self.tokenizer(lowercase ,      text_pair=lowercase ,      return_tensors=self.framework			)






			def 				A      (   self							:						Union[str, Any] ,      lowercase							:						Optional[int]			)							->    Any:

									'''simple docstring'''







									return self.model(**lowercase			)







			def 				A      (   self							:						List[str] ,      lowercase							:						Any			)							->    Optional[int]:

									'''simple docstring'''







									UpperCamelCase__         = model_outputs.logits[0].numpy()
									UpperCamelCase__         = softmax(lowercase			)

									UpperCamelCase__         = np.argmax(lowercase			)
									UpperCamelCase__         = self.model.config.idalabel[best_class]
									UpperCamelCase__         = probabilities[best_class].item()
									UpperCamelCase__         = logits.tolist()
									return {"label": label, "score": score, "logits": logits}
