1
$\begingroup$

BERT uses separator tokens ([SEP]) to input two sequences for a sequence-pair task. If I understand the BERT architecture correctly, attention is applied to all inputs thus coupling the two sequences right from the start.

Now, consider a sequence-pair task in which one of the sequences is constant and known from the start. E.g. Answering multiple unknown questions about a known context. To me it seems that there could be a computational advantage if one would precompute (part of) the model with the context only. However, if my assumption is correct that the two sequences are coupled from the start, precomputation is infeasible.

Therefore my question is: How to precompute one sequence in a sequence-pair task while still using (pre-trained) BERT? Can we combine BERT with some other type of architecture to achieve this? And does it even make sense to do it in terms of speed and accuracy?

$\endgroup$

1 Answer 1

2
$\begingroup$

Each token position at each of the attention layers of BERT is computed taking into account all tokens of both sequences. This way, there is not a single element that depends on just the first sequence and therefore it is not possible to precompute anything to be reused for different second sequences.

enter image description here

As you can see, the very nature of BERT's network architecture prevents you from factoring out the computations involving the first sequence.

In other similar architectures like ALBERT, there are some parts that could be reused, as the embedding computation (because ALBERT's embeddings are factorized, making the embedding matrix smaller but adding a multiplication at runtime), but I am not sure that reusing this computation would save a lot.

I don't know of any architecture made for sequence pairs that would let you do what you described, as most sequence pair approaches derive from BERT, which itself relies on computing attention between every token pair.

One option would be to use a network that gives you a fixed-size representation (i.e. a vector) of a sentence: you would use it on each of the sentences in a pair, and then you would feed both vectors to a second network (e.g. a multilayer perceptron receiving the concatenation of both vectors) to compute the final output. Depending on the task, this may give you good results and allow you to do the mentioned precomputing. To obtain the sentence representation vector, you may use BERT itself (the output at the [CLS] position) or some other architecture like LASER.

$\endgroup$
2
  • 1
    $\begingroup$ Indeed, that's what I meant with "coupling the two sequences from the start". Since you state that ALBERT would probably not benefit a lot from precomputations, are you aware of any pre-trained models that would benefit from such precomputations? $\endgroup$ Commented Dec 17, 2021 at 17:20
  • 1
    $\begingroup$ I updated the answer with some potential approaches. $\endgroup$
    – noe
    Commented Dec 17, 2021 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.