finetune.py 2.43 KB
Newer Older
Mohammad's avatar
Mohammad committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# coding=utf-8
# Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
#
# 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
    """Build the model."""
Mohammad's avatar
Mohammad committed
42
    args = get_args()
43
44
45
46
47

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

    return MultipleChoice(
        num_layers=args.num_layers,
Mohammad's avatar
Mohammad committed
48
        vocab_size=args.padded_vocab_size,
49
50
51
52
53
54
55
56
57
        hidden_size=args.hidden_size,
        num_attention_heads=args.num_attention_heads,
        embedding_dropout_prob=args.hidden_dropout,
        attention_dropout_prob=args.attention_dropout,
        output_dropout_prob=args.hidden_dropout,
        max_sequence_length=args.max_position_embeddings,
        checkpoint_activations=args.checkpoint_activations)


Mohammad's avatar
Mohammad committed
58
def metrics_func_provider():
59
    """Privde metrics callback function."""
Mohammad's avatar
Mohammad committed
60
61
    args = get_args()
    tokenizer = get_tokenizer()
62

Mohammad's avatar
Mohammad committed
63
    def single_dataset_provider(datapath):
64
        name = datapath.split('RACE')[-1].strip('/').replace('/', '-')
Mohammad's avatar
Mohammad committed
65
        return RaceDataset(name, [datapath], tokenizer, args.seq_length)
66

Mohammad's avatar
Mohammad committed
67
    return accuracy_func_provider(single_dataset_provider)
68
69


Mohammad's avatar
Mohammad committed
70
def main():
71

Mohammad's avatar
Mohammad committed
72
    finetune(train_valid_datasets_provider, model_provider,
73
             end_of_epoch_callback_provider=metrics_func_provider)