File size: 2,128 Bytes
91ef820 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Backbone modules.
"""
from collections import OrderedDict
import torch
import torch.nn.functional as F
from torch import nn
from typing import Dict, List
from utils.misc import NestedTensor, is_main_process
# from .position_encoding import build_position_encoding
# from pytorch_pretrained_bert.modeling import BertModel
# from transformers import BertModel
from transformers import AutoTokenizer, AutoModel
class BERT(nn.Module):
def __init__(self, name: str, train_bert: bool, hidden_dim: int, max_len: int, enc_num):
super().__init__()
# if name == 'bert-base-uncased' :
# self.num_channels = 768
# else:
# self.num_channels = 1024
self.num_channels = 768
self.enc_num = enc_num
self.bert = AutoModel.from_pretrained(name)
if not train_bert:
for parameter in self.bert.parameters():
parameter.requires_grad_(False)
def forward(self, tensor_list: NestedTensor):
if self.enc_num > 0:
# # pytorch_pretrained_bert version
# all_encoder_layers, _ = self.bert(tensor_list.tensors, token_type_ids=None, attention_mask=tensor_list.mask)
# # use the output of the X-th transformer encoder layers
# xs = all_encoder_layers[self.enc_num - 1]
# transformers bert version
bert_output = self.bert(tensor_list.tensors, token_type_ids=None, attention_mask=tensor_list.mask)
xs = bert_output.last_hidden_state
else:
xs = self.bert.embeddings.word_embeddings(tensor_list.tensors)
mask = tensor_list.mask.to(torch.bool)
mask = ~mask
out = NestedTensor(xs, mask)
return out
def build_bert(args):
# position_embedding = build_position_encoding(args)
train_bert = args.lr_bert > 0
bert = BERT(args.bert_model, train_bert, args.hidden_dim, args.max_query_len, args.bert_enc_num)
# model = Joiner(bert, position_embedding)
# model.num_channels = bert.num_channels
return bert
|