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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from collections import OrderedDict
|
| 3 |
+
from transformers import BertConfig, PretrainedConfig
|
| 4 |
+
from transformers import XLMRobertaForMaskedLM
|
| 5 |
+
|
| 6 |
+
from flash_attn.models.bert import BertModel
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
import click
|
| 10 |
+
|
| 11 |
+
## inspired by https://github.com/Dao-AILab/flash-attention/blob/85881f547fd1053a7b4a2c3faad6690cca969279/flash_attn/models/bert.py
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def remap_state_dict(state_dict, config: PretrainedConfig):
|
| 15 |
+
"""
|
| 16 |
+
Map the state_dict of a Huggingface BERT model to be flash_attn compatible.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
# Replace Roberta with Bert
|
| 20 |
+
def key_mapping_roberta(key):
|
| 21 |
+
return re.sub(r"^roberta.", "bert.", key)
|
| 22 |
+
|
| 23 |
+
state_dict = OrderedDict((key_mapping_roberta(k), v) for k, v in state_dict.items())
|
| 24 |
+
|
| 25 |
+
# LayerNorm
|
| 26 |
+
def key_mapping_ln_gamma_beta(key):
|
| 27 |
+
key = re.sub(r"LayerNorm.gamma$", "LayerNorm.weight", key)
|
| 28 |
+
key = re.sub(r"LayerNorm.beta$", "LayerNorm.bias", key)
|
| 29 |
+
return key
|
| 30 |
+
|
| 31 |
+
state_dict = OrderedDict(
|
| 32 |
+
(key_mapping_ln_gamma_beta(k), v) for k, v in state_dict.items()
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Layers
|
| 36 |
+
def key_mapping_layers(key):
|
| 37 |
+
return re.sub(r"^bert.encoder.layer.", "bert.encoder.layers.", key)
|
| 38 |
+
|
| 39 |
+
state_dict = OrderedDict((key_mapping_layers(k), v) for k, v in state_dict.items())
|
| 40 |
+
|
| 41 |
+
# LayerNorm
|
| 42 |
+
def key_mapping_ln(key):
|
| 43 |
+
key = re.sub(r"^bert.embeddings.LayerNorm.", "bert.emb_ln.", key)
|
| 44 |
+
key = re.sub(
|
| 45 |
+
r"^bert.encoder.layers.(\d+).attention.output.LayerNorm.(weight|bias)",
|
| 46 |
+
r"bert.encoder.layers.\1.norm1.\2",
|
| 47 |
+
key,
|
| 48 |
+
)
|
| 49 |
+
key = re.sub(
|
| 50 |
+
r"^bert.encoder.layers.(\d+).output.LayerNorm.(weight|bias)",
|
| 51 |
+
r"bert.encoder.layers.\1.norm2.\2",
|
| 52 |
+
key,
|
| 53 |
+
)
|
| 54 |
+
key = re.sub(
|
| 55 |
+
r"^cls.predictions.transform.LayerNorm.(weight|bias)",
|
| 56 |
+
r"cls.predictions.transform.layer_norm.\1",
|
| 57 |
+
key,
|
| 58 |
+
)
|
| 59 |
+
return key
|
| 60 |
+
|
| 61 |
+
state_dict = OrderedDict((key_mapping_ln(k), v) for k, v in state_dict.items())
|
| 62 |
+
|
| 63 |
+
# MLP
|
| 64 |
+
def key_mapping_mlp(key):
|
| 65 |
+
key = re.sub(
|
| 66 |
+
r"^bert.encoder.layers.(\d+).intermediate.dense.(weight|bias)",
|
| 67 |
+
r"bert.encoder.layers.\1.mlp.fc1.\2",
|
| 68 |
+
key,
|
| 69 |
+
)
|
| 70 |
+
key = re.sub(
|
| 71 |
+
r"^bert.encoder.layers.(\d+).output.dense.(weight|bias)",
|
| 72 |
+
r"bert.encoder.layers.\1.mlp.fc2.\2",
|
| 73 |
+
key,
|
| 74 |
+
)
|
| 75 |
+
return key
|
| 76 |
+
|
| 77 |
+
state_dict = OrderedDict((key_mapping_mlp(k), v) for k, v in state_dict.items())
|
| 78 |
+
|
| 79 |
+
# Attention
|
| 80 |
+
last_layer_subset = getattr(config, "last_layer_subset", False)
|
| 81 |
+
for d in range(config.num_hidden_layers):
|
| 82 |
+
Wq = state_dict.pop(f"bert.encoder.layers.{d}.attention.self.query.weight")
|
| 83 |
+
Wk = state_dict.pop(f"bert.encoder.layers.{d}.attention.self.key.weight")
|
| 84 |
+
Wv = state_dict.pop(f"bert.encoder.layers.{d}.attention.self.value.weight")
|
| 85 |
+
bq = state_dict.pop(f"bert.encoder.layers.{d}.attention.self.query.bias")
|
| 86 |
+
bk = state_dict.pop(f"bert.encoder.layers.{d}.attention.self.key.bias")
|
| 87 |
+
bv = state_dict.pop(f"bert.encoder.layers.{d}.attention.self.value.bias")
|
| 88 |
+
if not (last_layer_subset and d == config.num_hidden_layers - 1):
|
| 89 |
+
state_dict[f"bert.encoder.layers.{d}.mixer.Wqkv.weight"] = torch.cat(
|
| 90 |
+
[Wq, Wk, Wv], dim=0
|
| 91 |
+
)
|
| 92 |
+
state_dict[f"bert.encoder.layers.{d}.mixer.Wqkv.bias"] = torch.cat(
|
| 93 |
+
[bq, bk, bv], dim=0
|
| 94 |
+
)
|
| 95 |
+
else:
|
| 96 |
+
state_dict[f"bert.encoder.layers.{d}.mixer.Wq.weight"] = Wq
|
| 97 |
+
state_dict[f"bert.encoder.layers.{d}.mixer.Wkv.weight"] = torch.cat(
|
| 98 |
+
[Wk, Wv], dim=0
|
| 99 |
+
)
|
| 100 |
+
state_dict[f"bert.encoder.layers.{d}.mixer.Wq.bias"] = bq
|
| 101 |
+
state_dict[f"bert.encoder.layers.{d}.mixer.Wkv.bias"] = torch.cat(
|
| 102 |
+
[bk, bv], dim=0
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
def key_mapping_attn(key):
|
| 106 |
+
return re.sub(
|
| 107 |
+
r"^bert.encoder.layers.(\d+).attention.output.dense.(weight|bias)",
|
| 108 |
+
r"bert.encoder.layers.\1.mixer.out_proj.\2",
|
| 109 |
+
key,
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
state_dict = OrderedDict((key_mapping_attn(k), v) for k, v in state_dict.items())
|
| 113 |
+
|
| 114 |
+
def key_mapping_decoder_bias(key):
|
| 115 |
+
return re.sub(r"^cls.predictions.bias", "cls.predictions.decoder.bias", key)
|
| 116 |
+
|
| 117 |
+
state_dict = OrderedDict(
|
| 118 |
+
(key_mapping_decoder_bias(k), v) for k, v in state_dict.items()
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
# Word embedding
|
| 122 |
+
pad_vocab_size_multiple = getattr(config, "pad_vocab_size_multiple", 1)
|
| 123 |
+
if pad_vocab_size_multiple > 1:
|
| 124 |
+
word_embeddings = state_dict["bert.embeddings.word_embeddings.weight"]
|
| 125 |
+
state_dict["bert.embeddings.word_embeddings.weight"] = F.pad(
|
| 126 |
+
word_embeddings, (0, 0, 0, config.vocab_size - word_embeddings.shape[0])
|
| 127 |
+
)
|
| 128 |
+
decoder_weight = state_dict["cls.predictions.decoder.weight"]
|
| 129 |
+
state_dict["cls.predictions.decoder.weight"] = F.pad(
|
| 130 |
+
decoder_weight, (0, 0, 0, config.vocab_size - decoder_weight.shape[0])
|
| 131 |
+
)
|
| 132 |
+
# If the vocab was padded, we want to set the decoder bias for those padded indices to be
|
| 133 |
+
# strongly negative (i.e. the decoder shouldn't predict those indices).
|
| 134 |
+
# TD [2022-05-09]: I don't think it affects the MLPerf training.
|
| 135 |
+
decoder_bias = state_dict["cls.predictions.decoder.bias"]
|
| 136 |
+
state_dict["cls.predictions.decoder.bias"] = F.pad(
|
| 137 |
+
decoder_bias, (0, config.vocab_size - decoder_bias.shape[0]), value=-100.0
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Embeddings
|
| 141 |
+
def key_remove_bert(key):
|
| 142 |
+
return re.sub(r"^bert.", "", key)
|
| 143 |
+
|
| 144 |
+
state_dict = OrderedDict(
|
| 145 |
+
(key_remove_bert(k), v)
|
| 146 |
+
for k, v in state_dict.items()
|
| 147 |
+
if not k.startswith('lm_head')
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
return state_dict
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
@click.command()
|
| 154 |
+
@click.option('--model_name', default='FacebookAI/xlm-roberta-base', help='model name')
|
| 155 |
+
@click.option('--output', default='converted_roberta_weights.bin', help='model name')
|
| 156 |
+
def main(model_name, output):
|
| 157 |
+
roberta_model = XLMRobertaForMaskedLM.from_pretrained(model_name)
|
| 158 |
+
config = BertConfig.from_dict(roberta_model.config.to_dict())
|
| 159 |
+
state_dict = roberta_model.state_dict()
|
| 160 |
+
new_state_dict = remap_state_dict(state_dict, config)
|
| 161 |
+
|
| 162 |
+
flash_model = BertModel(config)
|
| 163 |
+
|
| 164 |
+
for k, v in flash_model.state_dict().items():
|
| 165 |
+
if k not in new_state_dict:
|
| 166 |
+
print(f'Use old weights from {k}')
|
| 167 |
+
new_state_dict[k] = v
|
| 168 |
+
|
| 169 |
+
flash_model.load_state_dict(new_state_dict)
|
| 170 |
+
|
| 171 |
+
torch.save(new_state_dict, output)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
if __name__ == '__main__':
|
| 175 |
+
main()
|