code: def __init__(
      self,
      data_dir,
      tokenizer,
      train_file="train-v2.0.json",
      val_file="dev-v2.0.json",
      test_data=None,
      batch_size_per_gpu=16,
      max_seq_length=512,
      doc_stride=128,
      max_query_length=64,
      version_2_with_negative=True,
      multi_gpu=True,
      model_type="bert",
      logger=None,
      clear_cache=False,
      no_cache=False,
      custom_sampler=None,
  ):
      # just in case someone passes string instead of Path
      if isinstance(data_dir, str):
          data_dir = Path(data_dir)
  
      if isinstance(tokenizer, str):
          tokenizer_class = TOKENIZER_CLASSES[model_type]
          # instantiate the new tokeniser object using the tokeniser name
          self.do_lower_case = "uncased" in tokenizer
          tokenizer = tokenizer_class.from_pretrained(
              tokenizer, do_lower_case=self.do_lower_case
          )
  
      self.tokenizer = tokenizer
      self.data_dir = data_dir
      self.cache_dir = data_dir / "cache"
      self.max_seq_length = max_seq_length
      self.doc_stride = doc_stride
      self.max_query_length = max_query_length
      self.batch_size_per_gpu = batch_size_per_gpu
      self.version_2_with_negative = version_2_with_negative
      self.train_dl = None
      self.val_dl = None
      self.test_dl = None
      self.n_gpu = 1
      self.no_cache = no_cache
      self.model_type = model_type
      self.custom_sampler = custom_sampler
      if logger is None:
          logger = logging.getLogger()
      self.logger = logger
      if multi_gpu:
          self.n_gpu = torch.cuda.device_count()
  
      if clear_cache:
          shutil.rmtree(self.cache_dir, ignore_errors=True)
  
      # Create folder if it doesn't exist
      self.cache_dir.mkdir(exist_ok=True)
  
      if train_file:
          # Train DataLoader
          train_examples = None
          cached_features_file = os.path.join(
              self.cache_dir,
              "cached_{}_{}_{}_{}_{}".format(
                  self.model_type,
                  "train",
                  str(self.max_seq_length),
                  str(self.doc_stride),
                  str(self.max_query_length),
              ),
          )
  
          if os.path.exists(cached_features_file) and no_cache == False:
              self.logger.info(
                  "Loading features from cached file %s",
                  cached_features_file,
              )
              features = torch.load(cached_features_file)
          else:
              training_file = str(self.data_dir / train_file)
              self.logger.info(
                  "Creating features from dataset file at %s",
                  training_file,
              )
              examples = read_examples(
                  input_file=training_file,
                  is_training=True,
                  version_2_with_negative=self.version_2_with_negative,
                  logger=self.logger,
              )
  
              features = convert_examples_to_features(
                  examples=examples,
                  tokenizer=self.tokenizer,
                  logger=self.logger,
                  max_seq_length=self.max_seq_length,
                  doc_stride=self.doc_stride,
                  max_query_length=self.max_query_length,
                  is_training=True,
              )
  
              if self.no_cache == False:
                  self.logger.info(
                      "Saving features into cached file %s",
                      cached_features_file,
                  )
                  torch.save(features, cached_features_file)
  
          # Convert to Tensors and build dataset
          all_input_ids = torch.tensor(
              [f.input_ids for f in features], dtype=torch.long
          )
          all_input_mask = torch.tensor(
              [f.input_mask for f in features], dtype=torch.long
          )
          all_segment_ids = torch.tensor(
              [f.segment_ids for f in features], dtype=torch.long
          )
          all_cls_index = torch.tensor(
              [f.cls_index for f in features], dtype=torch.long
          )
          all_p_mask = torch.tensor(
              [f.p_mask for f in features], dtype=torch.float
          )
          all_start_positions = torch.tensor(
              [f.start_position for f in features], dtype=torch.long
          )
          all_end_positions = torch.tensor(
              [f.end_position for f in features], dtype=torch.long
          )
          dataset = TensorDataset(
              all_input_ids,
              all_input_mask,
              all_segment_ids,
              all_start_positions,
              all_end_positions,
              all_cls_index,
              all_p_mask,
          )
  
          self.train_batch_size = self.batch_size_per_gpu * max(
              1, self.n_gpu
          )
  
          if self.custom_sampler is not None:
              train_sampler = self.custom_sampler
          else:
              train_sampler = RandomSampler(train_dataset)
  
          self.train_dl = DataLoader(
              dataset,
              sampler=train_sampler,
              batch_size=self.train_batch_size,
          )
  
      if val_file:
          # Val DataLoader
          train_examples = None
          cached_features_file = os.path.join(
              self.cache_dir,
              "cached_{}_{}_{}_{}_{}".format(
                  self.model_type,
                  "dev",
                  str(self.max_seq_length),
                  str(self.doc_stride),
                  str(self.max_query_length),
              ),
          )
  
          if os.path.exists(cached_features_file) and no_cache == False:
              self.validation_file = str(self.data_dir / val_file)
              examples = read_examples(
                  input_file=self.validation_file,
                  is_training=False,
                  version_2_with_negative=self.version_2_with_negative,
                  logger=self.logger,
              )
              self.logger.info(
                  "Loading features from cached file %s",
                  cached_features_file,
              )
              features = torch.load(cached_features_file)
          else:
              self.validation_file = str(self.data_dir / val_file)
              self.logger.info(
                  "Creating features from dataset file at %s",
                  self.validation_file,
              )
              examples = read_examples(
                  input_file=self.validation_file,
                  is_training=False,
                  version_2_with_negative=self.version_2_with_negative,
                  logger=self.logger,
              )
  
              features = convert_examples_to_features(
                  examples=examples,
                  tokenizer=self.tokenizer,
                  logger=self.logger,
                  max_seq_length=self.max_seq_length,
                  doc_stride=self.doc_stride,
                  max_query_length=self.max_query_length,
                  is_training=False,
              )
  
              if self.no_cache == False:
                  self.logger.info(
                      "Saving features into cached file %s",
                      cached_features_file,
                  )
                  torch.save(features, cached_features_file)
  
          # Save reference to validation features on the databunch object
          self.val_features = features
          self.val_examples = examples
  
          # Convert to Tensors and build dataset
          all_input_ids = torch.tensor(
              [f.input_ids for f in features], dtype=torch.long
          )
          all_input_mask = torch.tensor(
              [f.input_mask for f in features], dtype=torch.long
          )
          all_segment_ids = torch.tensor(
              [f.segment_ids for f in features], dtype=torch.long
          )
          all_cls_index = torch.tensor(
              [f.cls_index for f in features], dtype=torch.long
          )
          all_p_mask = torch.tensor(
              [f.p_mask for f in features], dtype=torch.float
          )
  
          all_example_index = torch.arange(
              all_input_ids.size(0), dtype=torch.long
          )
          dataset = TensorDataset(
              all_input_ids,
              all_input_mask,
              all_segment_ids,
              all_example_index,
              all_cls_index,
              all_p_mask,
          )
  
          self.val_batch_size = (
              self.batch_size_per_gpu * 2 * max(1, self.n_gpu)
          )
          val_sampler = SequentialSampler(dataset)
          self.val_dl = DataLoader(
              dataset,
              sampler=val_sampler,
              batch_size=self.train_batch_size,
          )
  
imports: import json
  import os
  import torch
  from pathlib import Path
  import pickle
  import logging
  import collections
  from io import open
  import shutil
  from torch.utils.data import TensorDataset, DataLoader, RandomSampler, SequentialSampler
  from torch.utils.data.distributed import DistributedSampler
  from transformers.tokenization_bert import BasicTokenizer, whitespace_tokenize
  from transformers import WEIGHTS_NAME, BertTokenizer, XLNetTokenizer, DistilBertTokenizer
label_desc: Use image node_id 1 to create a new BertQADataBunch object from inherited base classes: object with example: obj = BertQADataBunch(data_dir, tokenizer, train_file, val_file, test_data, batch_size_per_gpu, max_seq_length, doc_stride, max_query_length, version_2_with_negative, multi_gpu, model_type, logger, clear_cache, no_cache, custom_sampler)