finetune.py 1.97 KB
Newer Older
Mohammad's avatar
Mohammad committed
1
# coding=utf-8
Mohammad's avatar
Mohammad committed
2
# Copyright (c) 2020, NVIDIA CORPORATION.  All rights reserved.
Mohammad's avatar
Mohammad committed
3
4
5
6
7
8
9
10
11
12
13
14
15
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16
17
"""Race."""

18
from megatron import get_args, print_rank_0
Mohammad's avatar
Mohammad committed
19
20
from megatron import get_tokenizer
from megatron.model.multiple_choice import MultipleChoice
21
22
23
24
25
from tasks.eval_utils import accuracy_func_provider
from tasks.finetune_utils import finetune
from tasks.race.data import RaceDataset


Mohammad's avatar
Mohammad committed
26
def train_valid_datasets_provider():
27
    """Provide train and validation datasets."""
Mohammad's avatar
Mohammad committed
28
29
    args = get_args()
    tokenizer = get_tokenizer()
30
31

    train_dataset = RaceDataset('training', args.train_data,
Mohammad's avatar
Mohammad committed
32
                                tokenizer, args.seq_length)
33
    valid_dataset = RaceDataset('validation', args.valid_data,
Mohammad's avatar
Mohammad committed
34
                                tokenizer, args.seq_length)
35
36
37
38

    return train_dataset, valid_dataset


Mohammad's avatar
Mohammad committed
39
def model_provider():
40
41
42
43
    """Build the model."""

    print_rank_0('building multichoice model for RACE ...')

Mohammad's avatar
Mohammad committed
44
    return MultipleChoice(num_tokentypes=2)
45
46


Mohammad's avatar
Mohammad committed
47
def metrics_func_provider():
48
    """Privde metrics callback function."""
Mohammad's avatar
Mohammad committed
49
50
    args = get_args()
    tokenizer = get_tokenizer()
51

Mohammad's avatar
Mohammad committed
52
    def single_dataset_provider(datapath):
53
        name = datapath.split('RACE')[-1].strip('/').replace('/', '-')
Mohammad's avatar
Mohammad committed
54
        return RaceDataset(name, [datapath], tokenizer, args.seq_length)
55

Mohammad's avatar
Mohammad committed
56
    return accuracy_func_provider(single_dataset_provider)
57
58


Mohammad's avatar
Mohammad committed
59
def main():
60

Mohammad's avatar
Mohammad committed
61
    finetune(train_valid_datasets_provider, model_provider,
62
             end_of_epoch_callback_provider=metrics_func_provider)