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

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

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

    return train_dataset, valid_dataset


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

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

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


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

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

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


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

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