





'''simple docstring'''

#
# This a `torch.distributed` diagnostics script that checks that all GPUs in the cluster (one or
# many nodes) can talk to each other via nccl and allocate gpu memory.
#
# To run first adjust the number of processes and nodes:
#
# python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
#
# You may need to add --master_addr $MASTER_ADDR --master_port $MASTER_PORT if using a custom addr:port
#
# You can also use the rdzv API: --rdzv_endpoint $MASTER_ADDR:$MASTER_PORT --rdzv_backend c10d
#
# use torch.distributed.launch instead of torch.distributed.run for torch < 1.9
#
# If you get a hanging in `barrier` calls you have some network issues, you may try to debug this with:
#
# NCCL_DEBUG=INFO python -m torch.distributed.run --nproc_per_node 2 --nnodes 1 torch-distributed-gpu-test.py
#
# which should tell you what's going on behind the scenes.
#
#
# This script can be run via `srun` in the SLURM environment as well. Here is a SLURM script that
# runs on 2 nodes of 4 gpus per node:
#
# #SBATCH --job-name=test-nodes        # name
# #SBATCH --nodes=2                    # nodes
# #SBATCH --ntasks-per-node=1          # crucial - only 1 task per dist per node!
# #SBATCH --cpus-per-task=10           # number of cores per tasks
# #SBATCH --gres=gpu:4                 # number of gpus
# #SBATCH --time 0:05:00               # maximum execution time (HH:MM:SS)
# #SBATCH --output=%x-%j.out           # output file name
#
# GPUS_PER_NODE=4
# MASTER_ADDR=$(scontrol show hostnames $SLURM_JOB_NODELIST | head -n 1)
# MASTER_PORT=6000
#
# srun --jobid $SLURM_JOBID bash -c 'python -m torch.distributed.run \
# --nproc_per_node $GPUS_PER_NODE --nnodes $SLURM_NNODES --node_rank $SLURM_PROCID \
# --master_addr $MASTER_ADDR --master_port $MASTER_PORT \
# torch-distributed-gpu-test.py'
#

import fcntl
import os
import socket

import torch
import torch.distributed as dist







def 							UpperCAmelCase_    (	*lowerCAmelCase_							):

				"""simple docstring"""






				with open(lowerCAmelCase_		,							"r"							) as fh:
								fcntl.flock(lowerCAmelCase_		,							fcntl.LOCK_EX							)
								try:
												print(*lowerCAmelCase_							)
								finally:
												fcntl.flock(lowerCAmelCase_		,							fcntl.LOCK_UN							)


__lowerCamelCase : Optional[int]											=			int(os.environ["LOCAL_RANK"])
torch.cuda.set_device(local_rank)
__lowerCamelCase : Any											=			torch.device("cuda", local_rank)
__lowerCamelCase : str											=			socket.gethostname()

__lowerCamelCase : List[Any]											=			f"[{hostname}-{local_rank}]"

try:
			# test distributed
			dist.init_process_group("nccl")
			dist.all_reduce(torch.ones(1).to(device), op=dist.ReduceOp.SUM)
			dist.barrier()

			# test cuda is available and can allocate memory
			torch.cuda.is_available()
			torch.ones(1).cuda(local_rank)

			# global rank
			__lowerCamelCase : Optional[Any]											=			dist.get_rank()
			__lowerCamelCase : Dict											=			dist.get_world_size()

			printflock(f"{gpu} is OK (global rank: {rank}/{world_size})")

			dist.barrier()
			if rank == 0:
						printflock(f"pt={torch.__version__}, cuda={torch.version.cuda}, nccl={torch.cuda.nccl.version()}")

except Exception:
			printflock(f"{gpu} is broken")
			raise











'''simple docstring'''

import datasets

from .evaluate import evaluate


__lowerCamelCase : Optional[Any]											=			"\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

__lowerCamelCase : Any											=			"\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

__lowerCamelCase : Union[str, Any]											=			"\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"

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




		def 				UpperCAmelCase__   (self      :	Union[str, Any]				)					-> Union[str, Any]:
						return datasets.MetricInfo(
						    description=_DESCRIPTION							,			citation=_CITATION							,			inputs_description=_KWARGS_DESCRIPTION							,			features=datasets.Features(
						        {
						            "predictions": {"id": datasets.Value("string"				), "prediction_text": datasets.Value("string"				)},
						            "references": {
						                "id": datasets.Value("string"				),
						                "answers": datasets.features.Sequence(
						                    {
						                        "text": datasets.Value("string"				),
						                        "answer_start": datasets.Value("int32"				),
						                    }				),
						            },
						        }				)							,			codebase_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]							,			reference_urls=["https://rajpurkar.github.io/SQuAD-explorer/"]							,			)





		def 				UpperCAmelCase__   (self      :	List[str]							,			A__      :	List[Any]							,			A__      :	List[str]				)					-> str:
						lowercase         = {prediction["id"]: prediction["prediction_text"] for prediction in predictions}
						lowercase         = [
						    {
						        "paragraphs": [
						            {
						                "qas": [
						                    {
						                        "answers": [{"text": answer_text} for answer_text in ref["answers"]["text"]],
						                        "id": ref["id"],
						                    }
						                    for ref in references
						                ]
						            }
						        ]
						    }
						]
						lowercase         = evaluate(dataset=A__							,			predictions=A__				)
						return score




