(args, tokenizer, evaluate=False, output_examples=False)
| 161 | |
| 162 | |
| 163 | def load_and_cache_examples(args, tokenizer, evaluate=False, output_examples=False): |
| 164 | if args.local_rank not in [-1, 0] and not evaluate: |
| 165 | # Make sure only the first process in distributed training process the dataset, and the others will use the cache |
| 166 | torch.distributed.barrier() |
| 167 | |
| 168 | # Load data features from cache or dataset file |
| 169 | input_dir = args.data_dir if args.data_dir else "." |
| 170 | cached_features_file = os.path.join( |
| 171 | input_dir, |
| 172 | "cached_{}_{}_{}".format( |
| 173 | "dev" if evaluate else "train", |
| 174 | list(filter(None, args.model_name_or_path.split("/"))).pop(), |
| 175 | str(args.max_seq_length), |
| 176 | ), |
| 177 | ) |
| 178 | |
| 179 | # Init features and dataset from cache if it exists |
| 180 | if os.path.exists(cached_features_file) and not args.overwrite_cache: |
| 181 | logger.info("Loading features from cached file %s", cached_features_file) |
| 182 | features_and_dataset = torch.load(cached_features_file) |
| 183 | features, dataset, examples = ( |
| 184 | features_and_dataset["features"], |
| 185 | features_and_dataset["dataset"], |
| 186 | features_and_dataset["examples"], |
| 187 | ) |
| 188 | else: |
| 189 | logger.info("Creating features from dataset file at %s", input_dir) |
| 190 | |
| 191 | if not args.data_dir and ((evaluate and not args.predict_file) or (not evaluate and not args.train_file)): |
| 192 | try: |
| 193 | import tensorflow_datasets as tfds |
| 194 | except ImportError: |
| 195 | raise ImportError("If not data_dir is specified, tensorflow_datasets needs to be installed.") |
| 196 | |
| 197 | if args.version_2_with_negative: |
| 198 | logger.warn("tensorflow_datasets does not handle version 2 of SQuAD.") |
| 199 | |
| 200 | tfds_examples = tfds.load("squad") |
| 201 | examples = SquadV1Processor().get_examples_from_dataset(tfds_examples, evaluate=evaluate) |
| 202 | else: |
| 203 | processor = SquadV2Processor() if args.version_2_with_negative else SquadV1Processor() |
| 204 | if evaluate: |
| 205 | examples = processor.get_dev_examples(args.data_dir, filename=args.predict_file) |
| 206 | else: |
| 207 | examples = processor.get_train_examples(args.data_dir, filename=args.train_file) |
| 208 | |
| 209 | features, dataset = squad_convert_examples_to_features( |
| 210 | examples=examples, |
| 211 | tokenizer=tokenizer, |
| 212 | max_seq_length=args.max_seq_length, |
| 213 | doc_stride=args.doc_stride, |
| 214 | max_query_length=args.max_query_length, |
| 215 | is_training=not evaluate, |
| 216 | return_dataset="pt", |
| 217 | threads=args.threads, |
| 218 | ) |
| 219 | |
| 220 | if args.local_rank in [-1, 0]: |
no test coverage detected