finetune.py 2.53 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
22
from megatron import mpu
from megatron.model.multiple_choice import MultipleChoice, MultipleChoiceFirstStage, MultipleChoiceIntermediateStage, MultipleChoiceLastStage
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


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

    print_rank_0('building multichoice model for RACE ...')
45
46
47
48
49
50
51
52
53
54
55
56
    if mpu.get_pipeline_model_parallel_world_size() > 1:
        # Determine model based on position of stage in pipeline.
        if mpu.is_pipeline_first_stage():
            model = MultipleChoiceFirstStage(num_tokentypes=2)
        elif mpu.is_pipeline_last_stage():
            model = MultipleChoiceLastStage(num_tokentypes=2)
        else:
            model = MultipleChoiceIntermediateStage(num_tokentypes=2)
    else:
        model = MultipleChoice(num_tokentypes=2)

    return model
57
58


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

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

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


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

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