





# 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_  )     ->		Dict:
							return (torch.arange(state.num_processes  ) + 1.0 + (state.num_processes * state.process_index)).to(state.device  )




def  lowerCAmelCase_ (							UpperCamelCase_  )     ->		Tuple:
							UpperCamelCase_          =  create_tensor(UpperCamelCase_  )
							UpperCamelCase_          =  gather(UpperCamelCase_  )
							assert gathered_tensor.tolist() == list(range(1					,		state.num_processes**2 + 1  )  )




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




def  lowerCAmelCase_ (							UpperCamelCase_  )     ->		Union[str, Any]:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCamelCase_          =  create_tensor(UpperCamelCase_  )
							UpperCamelCase_          =  reduce(UpperCamelCase_					,		"mean"  )
							UpperCamelCase_          =  torch.tensor([2.0, 3]  ).to(state.device  )
							assert torch.allclose(UpperCamelCase_					,		UpperCamelCase_  ), F'''{reduced_tensor} != {truth_tensor}'''




def  lowerCAmelCase_ (							UpperCamelCase_  )     ->		int:
							# For xla_spawn (TPUs)
							main()




def  lowerCAmelCase_ (							)     ->		Optional[int]:
							UpperCamelCase_          =  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()








import re
import string

import numpy as np

import datasets


_UpperCAmelCase        =				'\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

_UpperCAmelCase        =				'\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

_UpperCAmelCase        =				'\n'



@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION  ,						_KWARGS_DESCRIPTION     )
class 							_UpperCamelCase					(	datasets.Metric     ):

       def lowercase (		self: Any		)       -> List[str]:




              """simple docstring"""


              return datasets.MetricInfo(
                  description=_DESCRIPTION					,   citation=_CITATION					,   inputs_description=_KWARGS_DESCRIPTION					,   features=datasets.Features(
                      {
                          "predictions": datasets.Value("string"					,   id="sequence"		),
                          "references": datasets.Value("string"					,   id="sequence"		),
                      }		)					,   reference_urls=[]					,   )



       def lowercase (		self: Any					,   _SCREAMING_SNAKE_CASE: Any					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: List[Any]=None					,   _SCREAMING_SNAKE_CASE: Tuple=False					,   _SCREAMING_SNAKE_CASE: Optional[int]=False					,   _SCREAMING_SNAKE_CASE: Any=False					,   )       -> Optional[Any]:




              """simple docstring"""


              if regexes_to_ignore is not None:
                     for s in regexes_to_ignore:
                            UpperCamelCase_          =  np.array([re.sub(_SCREAMING_SNAKE_CASE					,   ""					,   _SCREAMING_SNAKE_CASE		) for x in predictions]		)
                            UpperCamelCase_          =  np.array([re.sub(_SCREAMING_SNAKE_CASE					,   ""					,   _SCREAMING_SNAKE_CASE		) for x in references]		)
              else:
                     UpperCamelCase_          =  np.asarray(_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  np.asarray(_SCREAMING_SNAKE_CASE		)

              if ignore_case:
                     UpperCamelCase_          =  np.char.lower(_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  np.char.lower(_SCREAMING_SNAKE_CASE		)

              if ignore_punctuation:
                     UpperCamelCase_          =  string.punctuation.maketrans(""					,   ""					,   string.punctuation		)
                     UpperCamelCase_          =  np.char.translate(_SCREAMING_SNAKE_CASE					,   table=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  np.char.translate(_SCREAMING_SNAKE_CASE					,   table=_SCREAMING_SNAKE_CASE		)

              if ignore_numbers:
                     UpperCamelCase_          =  string.digits.maketrans(""					,   ""					,   string.digits		)
                     UpperCamelCase_          =  np.char.translate(_SCREAMING_SNAKE_CASE					,   table=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  np.char.translate(_SCREAMING_SNAKE_CASE					,   table=_SCREAMING_SNAKE_CASE		)

              UpperCamelCase_          =  predictions == references

              return {"exact_match": np.mean(_SCREAMING_SNAKE_CASE		) * 100}

