finetune.py 2.17 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."""

Neel Kant's avatar
Neel Kant committed
18
19
from megatron import get_args
from megatron import print_rank_0
Mohammad's avatar
Mohammad committed
20
from megatron import get_tokenizer
21
from megatron import mpu
Jared Casper's avatar
Jared Casper committed
22
from megatron.model.multiple_choice import MultipleChoice
23
24
25
26
27
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
28
def train_valid_datasets_provider():
29
    """Provide train and validation datasets."""
Mohammad's avatar
Mohammad committed
30
31
    args = get_args()
    tokenizer = get_tokenizer()
32
33

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

    return train_dataset, valid_dataset


Jared Casper's avatar
Jared Casper committed
41
def model_provider(pre_process=True, post_process=True):
42
43
44
    """Build the model."""

    print_rank_0('building multichoice model for RACE ...')
Jared Casper's avatar
Jared Casper committed
45
46
47
    model = MultipleChoice(num_tokentypes=2,
                           pre_process=pre_process,
                           post_process=post_process)
48
49

    return model
50
51


Mohammad's avatar
Mohammad committed
52
def metrics_func_provider():
53
    """Privde metrics callback function."""
Mohammad's avatar
Mohammad committed
54
55
    args = get_args()
    tokenizer = get_tokenizer()
56

Mohammad's avatar
Mohammad committed
57
    def single_dataset_provider(datapath):
58
        name = datapath.split('RACE')[-1].strip('/').replace('/', '-')
Mohammad's avatar
Mohammad committed
59
        return RaceDataset(name, [datapath], tokenizer, args.seq_length)
60

Mohammad's avatar
Mohammad committed
61
    return accuracy_func_provider(single_dataset_provider)
62
63


Mohammad's avatar
Mohammad committed
64
def main():
65

Mohammad's avatar
Mohammad committed
66
    finetune(train_valid_datasets_provider, model_provider,
67
             end_of_epoch_callback_provider=metrics_func_provider)