realm_model.py 8.71 KB
Newer Older
xingjinliang's avatar
xingjinliang committed
1
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
2
import os
3
4
import torch

xingjinliang's avatar
xingjinliang committed
5
6
7
from megatron.training import get_args, print_rank_0
from megatron.training.checkpointing import get_checkpoint_tracker_filename, get_checkpoint_name
from megatron.legacy.model import BertModel
8
from .module import MegatronModule
9
from megatron.core import mpu
xingjinliang's avatar
xingjinliang committed
10
11
12
13
14
15
from megatron.legacy.model.enums import AttnMaskType
from megatron.legacy.model.utils import get_linear_layer
from megatron.legacy.model.utils import init_method_normal
from megatron.legacy.model.language_model import get_language_model
from megatron.legacy.model.utils import scaled_init_method_normal
from megatron.legacy.model.bert_model import bert_extended_attention_mask, bert_position_ids
16
17


Neel Kant's avatar
Neel Kant committed
18
19
20
21
22
def general_ict_model_provider(only_query_model=False, only_block_model=False):
    """Build the model."""
    args = get_args()
    assert args.ict_head_size is not None, \
        "Need to specify --ict-head-size to provide an ICTBertModel"
23
    assert mpu.get_tensor_model_parallel_world_size() == 1 and mpu.get_pipeline_model_parallel_world_size() == 1, \
Neel Kant's avatar
Neel Kant committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        "Model parallel size > 1 not supported for ICT"

    print_rank_0('building ICTBertModel...')

    # simpler to just keep using 2 tokentypes since the LM we initialize with has 2 tokentypes
    model = ICTBertModel(
        ict_head_size=args.ict_head_size,
        num_tokentypes=2,
        parallel_output=True,
        only_query_model=only_query_model,
        only_block_model=only_block_model)

    return model


39
40
41
42
43
44
45
46
47
class ICTBertModel(MegatronModule):
    """Bert-based module for Inverse Cloze task."""
    def __init__(self,
                 ict_head_size,
                 num_tokentypes=1,
                 parallel_output=True,
                 only_query_model=False,
                 only_block_model=False):
        super(ICTBertModel, self).__init__()
48
        bert_kwargs = dict(
49
            ict_head_size=ict_head_size,
50
            num_tokentypes=num_tokentypes,
51
52
53
54
55
56
57
58
            parallel_output=parallel_output
        )
        assert not (only_block_model and only_query_model)
        self.use_block_model = not only_query_model
        self.use_query_model = not only_block_model

        if self.use_query_model:
            # this model embeds (pseudo-)queries - Embed_input in the paper
59
            self.query_model = IREncoderBertModel(**bert_kwargs)
60
61
62
63
            self._query_key = 'question_model'

        if self.use_block_model:
            # this model embeds evidence blocks - Embed_doc in the paper
64
            self.block_model = IREncoderBertModel(**bert_kwargs)
65
66
            self._block_key = 'context_model'

Neel Kant's avatar
Neel Kant committed
67
68
    def forward(self, query_tokens, query_attention_mask, block_tokens, block_attention_mask):
        """Run a forward pass for each of the models and return the respective embeddings."""
69
70
        query_logits = self.embed_query(query_tokens, query_attention_mask)
        block_logits = self.embed_block(block_tokens, block_attention_mask)
Neel Kant's avatar
Neel Kant committed
71
        return query_logits, block_logits
72
73
74
75

    def embed_query(self, query_tokens, query_attention_mask):
        """Embed a batch of tokens using the query model"""
        if self.use_query_model:
76
            query_types = torch.cuda.LongTensor(*query_tokens.shape).fill_(0)
77
78
79
80
81
82
83
84
            query_ict_logits, _ = self.query_model.forward(query_tokens, query_attention_mask, query_types)
            return query_ict_logits
        else:
            raise ValueError("Cannot embed query without query model.")

    def embed_block(self, block_tokens, block_attention_mask):
        """Embed a batch of tokens using the block model"""
        if self.use_block_model:
85
            block_types = torch.cuda.LongTensor(*block_tokens.shape).fill_(0)
86
87
88
89
90
            block_ict_logits, _ = self.block_model.forward(block_tokens, block_attention_mask, block_types)
            return block_ict_logits
        else:
            raise ValueError("Cannot embed block without block model.")

91
    def state_dict_for_save_checkpoint(self, prefix='', keep_vars=False):
92
93
94
95
96
        """Save dict with state dicts of each of the models."""
        state_dict_ = {}
        if self.use_query_model:
            state_dict_[self._query_key] \
                = self.query_model.state_dict_for_save_checkpoint(
97
                    prefix=prefix, keep_vars=keep_vars)
98
99
100
101

        if self.use_block_model:
            state_dict_[self._block_key] \
                = self.block_model.state_dict_for_save_checkpoint(
102
                    prefix=prefix, keep_vars=keep_vars)
103
104
105
106
107
108
109
110
111
112
113
114
115
116

        return state_dict_

    def load_state_dict(self, state_dict, strict=True):
        """Load the state dicts of each of the models"""
        if self.use_query_model:
            print("Loading ICT query model", flush=True)
            self.query_model.load_state_dict(
                state_dict[self._query_key], strict=strict)

        if self.use_block_model:
            print("Loading ICT block model", flush=True)
            self.block_model.load_state_dict(
                state_dict[self._block_key], strict=strict)
Neel Kant's avatar
Neel Kant committed
117
118

    def init_state_dict_from_bert(self):
119
        """Initialize the state from a pretrained BERT model on iteration zero of ICT pretraining"""
Neel Kant's avatar
Neel Kant committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        args = get_args()
        tracker_filename = get_checkpoint_tracker_filename(args.bert_load)
        if not os.path.isfile(tracker_filename):
            raise FileNotFoundError("Could not find BERT load for ICT")
        with open(tracker_filename, 'r') as f:
            iteration = int(f.read().strip())
            assert iteration > 0

        checkpoint_name = get_checkpoint_name(args.bert_load, iteration, False)
        if mpu.get_data_parallel_rank() == 0:
            print('global rank {} is loading checkpoint {}'.format(
                torch.distributed.get_rank(), checkpoint_name))

        try:
            state_dict = torch.load(checkpoint_name, map_location='cpu')
xingjinliang's avatar
xingjinliang committed
135
        except Exception:
Neel Kant's avatar
Neel Kant committed
136
137
            raise ValueError("Could not load checkpoint")

138
        # load the LM state dict into each model
Neel Kant's avatar
Neel Kant committed
139
140
141
        model_dict = state_dict['model']['language_model']
        self.query_model.language_model.load_state_dict(model_dict)
        self.block_model.language_model.load_state_dict(model_dict)
142
143

        # give each model the same ict_head to begin with as well
Neel Kant's avatar
Neel Kant committed
144
145
        query_ict_head_state_dict = self.state_dict_for_save_checkpoint()[self._query_key]['ict_head']
        self.block_model.ict_head.load_state_dict(query_ict_head_state_dict)
146
147
148


class IREncoderBertModel(MegatronModule):
149
    """BERT-based encoder for queries or blocks used for learned information retrieval."""
150
151
152
153
154
155
156
157
158
159
160
161
162
    def __init__(self, ict_head_size, num_tokentypes=2, parallel_output=True):
        super(IREncoderBertModel, self).__init__()
        args = get_args()

        self.ict_head_size = ict_head_size
        self.parallel_output = parallel_output
        init_method = init_method_normal(args.init_method_std)
        scaled_init_method = scaled_init_method_normal(args.init_method_std,
                                                       args.num_layers)

        self.language_model, self._language_model_key = get_language_model(
            num_tokentypes=num_tokentypes,
            add_pooler=True,
163
            encoder_attn_mask_type=AttnMaskType.padding,
164
165
166
167
168
169
170
171
172
173
174
175
            init_method=init_method,
            scaled_init_method=scaled_init_method)

        self.ict_head = get_linear_layer(args.hidden_size, ict_head_size, init_method)
        self._ict_head_key = 'ict_head'

    def forward(self, input_ids, attention_mask, tokentype_ids=None):
        extended_attention_mask = bert_extended_attention_mask(
            attention_mask, next(self.language_model.parameters()).dtype)
        position_ids = bert_position_ids(input_ids)

        lm_output, pooled_output = self.language_model(
176
177
            input_ids,
            position_ids,
178
179
180
181
            extended_attention_mask,
            tokentype_ids=tokentype_ids)

        # Output.
182
183
        ict_logits = self.ict_head(pooled_output)
        return ict_logits, None
184

185
    def state_dict_for_save_checkpoint(self, prefix='', keep_vars=False):
186
187
188
189
190
        """For easy load when model is combined with other heads,
        add an extra key."""

        state_dict_ = {}
        state_dict_[self._language_model_key] \
191
192
            = self.language_model.state_dict_for_save_checkpoint(prefix=prefix,
                                                                 keep_vars=keep_vars)
193
        state_dict_[self._ict_head_key] \
194
195
            = self.ict_head.state_dict(prefix=prefix,
                                       keep_vars=keep_vars)
196
197
198
199
200
201
202
203
204
205
        return state_dict_

    def load_state_dict(self, state_dict, strict=True):
        """Customized load."""
        self.language_model.load_state_dict(
            state_dict[self._language_model_key], strict=strict)
        self.ict_head.load_state_dict(
            state_dict[self._ict_head_key], strict=strict)