finetune.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. """Race."""
  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.multiple_choice import MultipleChoice
  21. from tasks.eval_utils import accuracy_func_provider
  22. from tasks.finetune_utils import finetune
  23. from tasks.race.data import RaceDataset
  24. def train_valid_datasets_provider():
  25. """Provide train and validation datasets."""
  26. args = get_args()
  27. tokenizer = get_tokenizer()
  28. train_dataset = RaceDataset('training', args.train_data,
  29. tokenizer, args.seq_length)
  30. valid_dataset = RaceDataset('validation', args.valid_data,
  31. tokenizer, args.seq_length)
  32. return train_dataset, valid_dataset
  33. def model_provider(pre_process=True, post_process=True):
  34. """Build the model."""
  35. print_rank_0('building multichoice model for RACE ...')
  36. model = MultipleChoice(num_tokentypes=2,
  37. pre_process=pre_process,
  38. post_process=post_process)
  39. return model
  40. def metrics_func_provider():
  41. """Privde metrics callback function."""
  42. args = get_args()
  43. tokenizer = get_tokenizer()
  44. def single_dataset_provider(datapath):
  45. name = datapath.split('RACE')[-1].strip('/').replace('/', '-')
  46. return RaceDataset(name, [datapath], tokenizer, args.seq_length)
  47. return accuracy_func_provider(single_dataset_provider)
  48. def main():
  49. finetune(train_valid_datasets_provider, model_provider,
  50. end_of_epoch_callback_provider=metrics_func_provider)