finetune.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # coding=utf-8
  2. # Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """GLUE finetuning/evaluation."""
  16. from megatron import get_args
  17. from megatron import print_rank_0
  18. from megatron import get_tokenizer
  19. from megatron import mpu
  20. from megatron.model.classification import Classification
  21. from tasks.eval_utils import accuracy_func_provider
  22. from tasks.finetune_utils import finetune
  23. def glue_classification(num_classes, Dataset,
  24. name_from_datapath_func):
  25. def train_valid_datasets_provider():
  26. """Build train and validation dataset."""
  27. args = get_args()
  28. tokenizer = get_tokenizer()
  29. train_dataset = Dataset('training', args.train_data,
  30. tokenizer, args.seq_length)
  31. valid_dataset = Dataset('validation', args.valid_data,
  32. tokenizer, args.seq_length)
  33. return train_dataset, valid_dataset
  34. def model_provider(pre_process=True, post_process=True):
  35. """Build the model."""
  36. args = get_args()
  37. print_rank_0('building classification model for {} ...'.format(
  38. args.task))
  39. model = Classification(num_classes=num_classes, num_tokentypes=2,
  40. pre_process=pre_process, post_process=post_process)
  41. return model
  42. def metrics_func_provider():
  43. """Privde metrics callback function."""
  44. def single_dataset_provider(datapath):
  45. args = get_args()
  46. tokenizer = get_tokenizer()
  47. name = name_from_datapath_func(datapath)
  48. return Dataset(name, [datapath], tokenizer, args.seq_length)
  49. return accuracy_func_provider(single_dataset_provider)
  50. """Finetune/evaluate."""
  51. finetune(train_valid_datasets_provider, model_provider,
  52. end_of_epoch_callback_provider=metrics_func_provider)
  53. def main():
  54. args = get_args()
  55. if args.task == 'MNLI':
  56. num_classes = 3
  57. from tasks.glue.mnli import MNLIDataset as Dataset
  58. def name_from_datapath(datapath):
  59. return datapath.split('MNLI')[-1].strip(
  60. '.tsv').strip('/').replace('_', '-')
  61. elif args.task == 'QQP':
  62. num_classes = 2
  63. from tasks.glue.qqp import QQPDataset as Dataset
  64. def name_from_datapath(datapath):
  65. return datapath.split('QQP')[-1].strip(
  66. '.tsv').strip('/').replace('_', '-')
  67. else:
  68. raise NotImplementedError('GLUE task {} is not implemented.'.format(
  69. args.task))
  70. glue_classification(num_classes, Dataset, name_from_datapath)