






'''simple docstring'''

import os

import jsonlines
import numpy as np
from tqdm import tqdm


UpperCamelCase							=2048
UpperCamelCase							=4096
UpperCamelCase							=42
UpperCamelCase							=os.environ.pop("PROCESS_TRAIN", "false")
UpperCamelCase							={"null": 0, "short": 1, "long": 2, "yes": 3, "no": 4}



def 							snake_case					(     a_							: Any	)       ->				List[str]:





      """simple docstring"""

      def choose_first(a_							: List[Any]    ,   a_							: List[str]=False	):
            assert isinstance(a_    ,   a_	)
            if len(a_	) == 1:
                  UpperCamelCase_				:   Union[str, Any]					    =							answer[0]
                  return {k: [answer[k]] for k in answer} if is_long_answer else answer
            for a in answer:
                  if is_long_answer:
                        UpperCamelCase_				:   Dict					    =							{k: [a[k]] for k in a}
                  if len(a["""start_token"""]	) > 0:
                        break
            return a

      UpperCamelCase_				:   List[Any]					    =							{"""id""": example["""id"""]}
      UpperCamelCase_				:   Union[str, Any]					    =							example["""annotations"""]
      UpperCamelCase_				:   List[Any]					    =							annotation["""yes_no_answer"""]
      if 0 in yes_no_answer or 1 in yes_no_answer:
            UpperCamelCase_				:   int					    =							["""yes"""] if 1 in yes_no_answer else ["""no"""]
            UpperCamelCase_				:   Optional[int]					    =							[]
            UpperCamelCase_				:   Tuple					    =							[]
            UpperCamelCase_				:   Dict					    =							["""<cls>"""]
      else:
            UpperCamelCase_				:   List[Any]					    =							["""short"""]
            UpperCamelCase_				:   Union[str, Any]					    =							choose_first(annotation["""short_answers"""]	)
            if len(out["""start_token"""]	) == 0:
                  # answer will be long if short is not available
                  UpperCamelCase_				:   int					    =							["""long"""]
                  UpperCamelCase_				:   Union[str, Any]					    =							choose_first(annotation["""long_answer"""]    ,   is_long_answer=a_	)
                  UpperCamelCase_				:   Optional[Any]					    =							[]
            answer.update(a_	)

      # disregard some samples
      if len(answer["""start_token"""]	) > 1 or answer["start_token"] == answer["end_token"]:
            UpperCamelCase_				:   List[Any]					    =							True
      else:
            UpperCamelCase_				:   Optional[Any]					    =							False

      UpperCamelCase_				:   Optional[int]					    =							["""start_token""", """end_token""", """start_byte""", """end_byte""", """text"""]
      if not all(isinstance(answer[k]    ,   a_	) for k in cols	):
            raise ValueError("""Issue in ID"""    ,   example["""id"""]	)

      return answer



def 							snake_case					(     a_							: List[Any]    ,   a_							: str=False	)       ->				Any:





      """simple docstring"""

      UpperCamelCase_				:   List[str]					    =							_get_single_answer(a_	)
      # bytes are of no use
      del answer["start_byte"]
      del answer["end_byte"]

      # handle yes_no answers explicitly
      if answer["category"][0] in ["yes", "no"]:  # category is list with one element
            UpperCamelCase_				:   List[str]					    =							example["""document"""]["""tokens"""]
            UpperCamelCase_				:   str					    =							[]
            for i in range(len(doc["""token"""]	)	):
                  if not doc["is_html"][i]:
                        context.append(doc["""token"""][i]	)
            return {
                "context": " ".join(a_	),
                "answer": {
                    "start_token": -100,  # ignore index in cross-entropy
                    "end_token": -100,  # ignore index in cross-entropy
                    "category": answer["category"],
                    "span": answer["category"],  # extra
                },
            }

      # later, help in removing all no answers
      if answer["start_token"] == [-1]:
            return {
                "context": "None",
                "answer": {
                    "start_token": -1,
                    "end_token": -1,
                    "category": "null",
                    "span": "None",  # extra
                },
            }

      # handling normal samples

      UpperCamelCase_				:   Dict					    =							["""start_token""", """end_token"""]
      answer.update({k: answer[k][0] if len(answer[k]	) > 0 else answer[k] for k in cols}	)  # e.g. [10] == 10

      UpperCamelCase_				:   List[Any]					    =							example["""document"""]["""tokens"""]
      UpperCamelCase_				:   List[Any]					    =							answer["""start_token"""]
      UpperCamelCase_				:   Tuple					    =							answer["""end_token"""]

      UpperCamelCase_				:   str					    =							[]
      for i in range(len(doc["""token"""]	)	):
            if not doc["is_html"][i]:
                  context.append(doc["""token"""][i]	)
            else:
                  if answer["start_token"] > i:
                        start_token -= 1
                  if answer["end_token"] > i:
                        end_token -= 1
      UpperCamelCase_				:   List[str]					    =							""" """.join(context[start_token:end_token]	)

      # checking above code
      if assertion:
            UpperCamelCase_				:   List[str]					    =							doc["""is_html"""][answer["""start_token"""] : answer["""end_token"""]]
            UpperCamelCase_				:   int					    =							doc["""token"""][answer["""start_token"""] : answer["""end_token"""]]
            UpperCamelCase_				:   Optional[int]					    =							""" """.join([old[i] for i in range(len(a_	)	) if not is_html[i]]	)
            if new != old:
                  print("""ID:"""    ,   example["""id"""]	)
                  print("""New:"""    ,   a_    ,   end="""\n"""	)
                  print("""Old:"""    ,   a_    ,   end="""\n\n"""	)

      return {
          "context": " ".join(a_	),
          "answer": {
              "start_token": start_token,
              "end_token": end_token - 1,  # this makes it inclusive
              "category": answer["category"],  # either long or short
              "span": new,  # extra
          },
      }



def 							snake_case					(     a_							: Dict    ,   a_							: Optional[int]    ,   a_							: List[Any]=2_048    ,   a_							: str=4_096    ,   a_							: Any=True	)       ->				Tuple:





      """simple docstring"""

      UpperCamelCase_				:   List[Any]					    =							get_context_and_ans(a_    ,   assertion=a_	)
      UpperCamelCase_				:   Any					    =							out["""answer"""]

      # later, removing these samples
      if answer["start_token"] == -1:
            return {
                "example_id": example["id"],
                "input_ids": [[-1]],
                "labels": {
                    "start_token": [-1],
                    "end_token": [-1],
                    "category": ["null"],
                },
            }

      UpperCamelCase_				:   Any					    =							tokenizer(example["""question"""]["""text"""]    ,   out["""context"""]	).input_ids
      UpperCamelCase_				:   str					    =							input_ids.index(tokenizer.sep_token_id	) + 1

      # return yes/no
      if answer["category"][0] in ["yes", "no"]:  # category is list with one element
            UpperCamelCase_				:   Any					    =							[]
            UpperCamelCase_				:   Union[str, Any]					    =							[]
            UpperCamelCase_				:   List[Any]					    =							input_ids[:q_len]
            UpperCamelCase_				:   List[Any]					    =							range(a_    ,   len(a_	)    ,   max_length - doc_stride	)
            for i in doc_start_indices:
                  UpperCamelCase_				:   Any					    =							i + max_length - q_len
                  UpperCamelCase_				:   List[Any]					    =							input_ids[i:end_index]
                  inputs.append(q_indices + slice	)
                  category.append(answer["""category"""][0]	)
                  if slice[-1] == tokenizer.sep_token_id:
                        break

            return {
                "example_id": example["id"],
                "input_ids": inputs,
                "labels": {
                    "start_token": [-100] * len(a_	),
                    "end_token": [-100] * len(a_	),
                    "category": category,
                },
            }

      UpperCamelCase_				:   str					    =							out["""context"""].split()
      UpperCamelCase_				:   Dict					    =							splitted_context[answer["""end_token"""]]
      UpperCamelCase_				:   List[Any]					    =							len(
          tokenizer(
              """ """.join(splitted_context[: answer["""start_token"""]]	)    ,   add_special_tokens=a_    ,   ).input_ids	)
      UpperCamelCase_				:   Optional[int]					    =							len(
          tokenizer(""" """.join(splitted_context[: answer["""end_token"""]]	)    ,   add_special_tokens=a_	).input_ids	)

      answer["start_token"] += q_len
      answer["end_token"] += q_len

      # fixing end token
      UpperCamelCase_				:   str					    =							len(tokenizer(a_    ,   add_special_tokens=a_	).input_ids	)
      if num_sub_tokens > 1:
            answer["end_token"] += num_sub_tokens - 1

      UpperCamelCase_				:   Optional[Any]					    =							input_ids[answer["""start_token"""] : answer["""end_token"""] + 1]  # right & left are inclusive
      UpperCamelCase_				:   Dict					    =							answer["""start_token"""]
      UpperCamelCase_				:   Any					    =							answer["""end_token"""]

      if assertion:
            UpperCamelCase_				:   Any					    =							tokenizer.decode(a_	)
            if answer["span"] != new:
                  print("""ISSUE IN TOKENIZATION"""	)
                  print("""OLD:"""    ,   answer["""span"""]	)
                  print("""NEW:"""    ,   a_    ,   end="""\n\n"""	)

      if len(a_	) <= max_length:
            return {
                "example_id": example["id"],
                "input_ids": [input_ids],
                "labels": {
                    "start_token": [answer["start_token"]],
                    "end_token": [answer["end_token"]],
                    "category": answer["category"],
                },
            }

      UpperCamelCase_				:   Optional[int]					    =							input_ids[:q_len]
      UpperCamelCase_				:   Optional[int]					    =							range(a_    ,   len(a_	)    ,   max_length - doc_stride	)

      UpperCamelCase_				:   Union[str, Any]					    =							[]
      UpperCamelCase_				:   int					    =							[]
      UpperCamelCase_				:   int					    =							[]
      UpperCamelCase_				:   str					    =							[]  # null, yes, no, long, short
      for i in doc_start_indices:
            UpperCamelCase_				:   Union[str, Any]					    =							i + max_length - q_len
            UpperCamelCase_				:   int					    =							input_ids[i:end_index]
            inputs.append(q_indices + slice	)
            assert len(inputs[-1]	) <= max_length, "Issue in truncating length"

            if start_token >= i and end_token <= end_index - 1:
                  UpperCamelCase_				:   Optional[Any]					    =							start_token - i + q_len
                  UpperCamelCase_				:   Dict					    =							end_token - i + q_len
                  answers_category.append(answer["""category"""][0]	)  # ["short"] -> "short"
            else:
                  UpperCamelCase_				:   Union[str, Any]					    =							-100
                  UpperCamelCase_				:   List[Any]					    =							-100
                  answers_category.append("""null"""	)
            UpperCamelCase_				:   int					    =							inputs[-1][start_token : end_token + 1]

            answers_start_token.append(a_	)
            answers_end_token.append(a_	)
            if assertion:
                  if new != old and new != [tokenizer.cls_token_id]:
                        print("""ISSUE in strided for ID:"""    ,   example["""id"""]	)
                        print("""New:"""    ,   tokenizer.decode(a_	)	)
                        print("""Old:"""    ,   tokenizer.decode(a_	)    ,   end="""\n\n"""	)
            if slice[-1] == tokenizer.sep_token_id:
                  break

      return {
          "example_id": example["id"],
          "input_ids": inputs,
          "labels": {
              "start_token": answers_start_token,
              "end_token": answers_end_token,
              "category": answers_category,
          },
      }



def 							snake_case					(     a_							: Dict    ,   a_							: List[Any]    ,   a_							: List[str]=2_048    ,   a_							: Optional[Any]=4_096    ,   a_							: Any=False	)       ->				Union[str, Any]:





      """simple docstring"""

      UpperCamelCase_				:   List[str]					    =							get_strided_contexts_and_ans(
          a_    ,   a_    ,   doc_stride=a_    ,   max_length=a_    ,   assertion=a_    ,   )

      return example



def 							snake_case					(     a_							: Any    ,   a_							: Optional[Any]	)       ->				Dict:





      """simple docstring"""

      with jsonlines.open(a_    ,   """a"""	) as writer:
            for example in tqdm(a_    ,   total=len(a_	)    ,   desc="""Saving samples ... """	):
                  UpperCamelCase_				:   Union[str, Any]					    =							example["""labels"""]
                  for ids, start, end, cat in zip(
                      example["""input_ids"""]    ,   labels["""start_token"""]    ,   labels["""end_token"""]    ,   labels["""category"""]    ,   ):
                        if start == -1 and end == -1:
                              continue  # leave waste samples with no answer
                        if cat == "null" and np.random.rand() < 0.6:
                              continue  # removing 50 % samples
                        writer.write(
                            {
                                """input_ids""": ids,
                                """start_token""": start,
                                """end_token""": end,
                                """category""": CATEGORY_MAPPING[cat],
                            }	)


if __name__ == "__main__":
      from datasets import load_dataset

      from transformers import BigBirdTokenizer

      UpperCamelCase							=load_dataset("natural_questions")
      UpperCamelCase							=BigBirdTokenizer.from_pretrained("google/bigbird-roberta-base")

      UpperCamelCase							=data["train" if PROCESS_TRAIN == "true" else "validation"]

      UpperCamelCase							={
          "tokenizer": tokenizer,
          "doc_stride": DOC_STRIDE,
          "max_length": MAX_LENGTH,
          "assertion": False,
      }
      UpperCamelCase							=data.map(prepare_inputs, fn_kwargs=fn_kwargs)
      UpperCamelCase							=data.remove_columns(["annotations", "document", "id", "question"])
      print(data)

      np.random.seed(SEED)
      UpperCamelCase							="nq-training.jsonl" if PROCESS_TRAIN == "true" else "nq-validation.jsonl"
      save_to_disk(data, file_name=cache_file_name)














'''simple docstring'''

import inspect
import os
import unittest
from dataclasses import dataclass

import torch

from accelerate import Accelerator, DistributedDataParallelKwargs, GradScalerKwargs
from accelerate.state import AcceleratorState
from accelerate.test_utils import execute_subprocess_async, require_cuda, require_multi_gpu
from accelerate.utils import KwargsHandler




@dataclass
class     A    (      SCREAMING_SNAKE_CASE__						):






 """simple docstring"""





 __a			:						int      =						0
 __a			:						bool      =						False
 __a			:						float      =						3.0






class     A    (      unittest.TestCase						):






 """simple docstring"""





 def   _UpperCAmelCase       (							self ):
       # If no defaults are changed, `to_kwargs` returns an empty dict.
       self.assertDictEqual(MockClass().to_kwargs()			,     {} )
       self.assertDictEqual(MockClass(a=2 ).to_kwargs()			,     {"""a""": 2} )
       self.assertDictEqual(MockClass(a=2			,     b=__lowerCAmelCase ).to_kwargs()			,     {"""a""": 2, """b""": True} )
       self.assertDictEqual(MockClass(a=2			,     c=2.25 ).to_kwargs()			,     {"""a""": 2, """c""": 2.25} )
 @require_cuda
 def   _UpperCAmelCase       (							self ):
       # If no defaults are changed, `to_kwargs` returns an empty dict.
       UpperCamelCase_				:   int					    =							GradScalerKwargs(init_scale=10_24			,     growth_factor=2 )
       AcceleratorState._reset_state()
       UpperCamelCase_				:   Dict					    =							Accelerator(mixed_precision="""fp16"""			,     kwargs_handlers=[scaler_handler] )
       print(accelerator.use_fpaa )
       UpperCamelCase_				:   Any					    =							accelerator.scaler

       # Check the kwargs have been applied
       self.assertEqual(scaler._init_scale			,     10_24.0 )
       self.assertEqual(scaler._growth_factor			,     2.0 )

       # Check the other values are at the default
       self.assertEqual(scaler._backoff_factor			,     0.5 )
       self.assertEqual(scaler._growth_interval			,     20_00 )
       self.assertEqual(scaler._enabled			,     __lowerCAmelCase )





 @require_multi_gpu
 def   _UpperCAmelCase       (							self ):
       UpperCamelCase_				:   Optional[int]					    =							["""torchrun""", F"--nproc_per_node={torch.cuda.device_count()}", inspect.getfile(self.__class__ )]
       execute_subprocess_async(__lowerCAmelCase			,     env=os.environ.copy() )


if __name__ == "__main__":
      UpperCamelCase							=DistributedDataParallelKwargs(bucket_cap_mb=15, find_unused_parameters=True)
      UpperCamelCase							=Accelerator(kwargs_handlers=[ddp_scaler])
      UpperCamelCase							=torch.nn.Linear(100, 200)
      UpperCamelCase							=accelerator.prepare(model)

      # Check the values changed in kwargs
      UpperCamelCase							=""
      UpperCamelCase							=model.bucket_bytes_cap // (1024 * 1024)
      if observed_bucket_cap_map != 15:
            error_msg += f"Kwargs badly passed, should have `15` but found {observed_bucket_cap_map}.\n"
      if model.find_unused_parameters is not True:
            error_msg += f"Kwargs badly passed, should have `True` but found {model.find_unused_parameters}.\n"

      # Check the values of the defaults
      if model.dim != 0:
            error_msg += f"Default value not respected, should have `0` but found {model.dim}.\n"
      if model.broadcast_buffers is not True:
            error_msg += f"Default value not respected, should have `True` but found {model.broadcast_buffers}.\n"
      if model.gradient_as_bucket_view is not False:
            error_msg += f"Default value not respected, should have `False` but found {model.gradient_as_bucket_view}.\n"

      # Raise error at the end to make sure we don't stop at the first failure.
      if len(error_msg) > 0:
            raise ValueError(error_msg)






