NLP/📚 paper

논문 리뷰 💬 [Contrastive Decoding: Open-ended Text Generation as Optimization]

MINAIR 2024. 9. 11. 16:36

📚 Paper

https://arxiv.org/pdf/2210.15097

 

🌟요약
amateur LM은 expert LM에 비해 간단한 내용 (e.g., grammatical structure, common sense)를 학습하기 때문에 repetitive, incoherence와 같은 failure가 많다. Contrastive Decoding은 expert LM의 log prob에서 amateur LM의 log prob을 빼줌으로써 expert LM에서 undesirable output을 야기하는 특징들을 penalize (제거) 하고 expert LM에서 desirable (good) feature를 emphasize한다.

이때, amateur LM은 expert LM이 실수할 수 있는 부분을 빼줘야 하므로 어느 정도의 성능을 보이는 모델이어야 한다. 예를 들어 n-gram LM과 같은 low-capacity를 갖는 LM은 적합하지 않다. 

또한, 항상 amateur LM이 undesirable feature만을 학습하는 것은 아니기 때문에 적절한 constraint가 필요하다. 만약 constraint 없이 무조건 amateur LM를 penalize하기만 한다면 CD의 결과로 정답이 오답으로 되는 False Negative와 오답이 정답으로 되는 False Positive 문제가 발생할 수 있다. 따라서 plausible constraint가 필요하다. 

Abstract


LM의 decoding, 즉 토큰을 생성해내는 방법으로 maximum probability는 short & repetitive한 generation을 한다는 점에서 한계가 있다. 또한 sampling은 정해진 범위 내에서 무작위로 토큰을 선택함으로써 incoherent text generation & drift from topic의 문제가 있다. 

 

본 논문은 contrastive decoding (CD), plausibility constraint를 만족하는 contrastive objective를 최적화하는 믿을 만한 decoding을 제안한다. contrastive objective는 large LM (e.g. OPT-13B)의 likelihood에서 small LM (e.g. OPT-125M)의 likelihood을 뺀 값을 최대화하는 토큰을 선택하며, plausible constraint가 CD의 output을 plausible하도록 보장한다. 

 

CD는 small LM이 large LM보다 repetition, incoherence와 같은 에러가 더 잦다는 점에 기반한다. CD는 이미 훈련된 expert LM과 amateur LM을 사용하기 때문에 추가적인 훈련이 필요 없고, expert LM 하나만으로 decoding할 때보다 high quality의 output을 내놓는다. 또한, wikipedia, news, story domain (open-ended generation) 에서 기존의 decoding 방법들 (e.g., nucleus, top-k sampling)을 능가한다.

 

Introduction


  • canonical approach (전통적인 접근법)
    • generated text from sampling is prone to incoherence and topic drift as unlucky sampling choises compound over lone sequences. 
    • generated text from search-based maximum likelihood results in short, repetitive, and tedious text. 

Constrastive Decoding은 새로운 search-based approach로, coherence, diversity, fluency 모두 높은 텍스트를 generate하는 decoding 방법이다. Figure 1에서 expert LM으로 OPT-13B, amateur LM으로 OPT-125M가 사용됐다. CD는 difference between expert log-probabilities and amateur log-probabilities를 최대화하는 text (token) 를 search하는데, 이때 expert LM이 충분히 높은 확률을 준 text (token) 의 space에 한한다는 (즉, 말이 되는 token들만 고려하겠다는 뜻) plausibility constraints를 만족시켜야 한다. 

 

CD가 작동하는 이유는 shoft, repetitive, irrelevent or uninteresting strings generation과 같은 LM의 failure가 expert LM보다 amateur LM에서 훨씬 더 흔하기 때문이다. CD의 difference를 통해 amateur LM의 failure ouput은 deemphasized되고 반대로 expert LM이 desire하는 output (즉, amateur LM은 배우지 못한 knowledge) 은 emphasized된다. 즉, 논문에서는 CD가 expert LM의 best를 emphasize하고 expert LM의 amateur tendency는 제거한다고 한다. 

또한, CD는 추가적인 훈련이 필요 없고, two frozen LM of different sizes만 있으면 된다. 이때 expert LM과 amateur LM의 모델 크기 차이가 커질수록 CD의 성능이 좋아진다. 결국 amateur LM은 cheap하기 때문에 비용 절약이 가능하다. 

 

combination of expert LM and amateur LM: GPT2-XL vs. GPT2-small, OPT-13B vs. OPT-125M

 

Problem Statement


논문은 open-ended language generation에서 decoding하는 방법을 제안하고 있다. 우선 n 길이의 short prompt x_pre = x1, x2...xn이 주어지면 decoder는 m 길이의 continuation x_cont = xn+1, xn+2...xn+m을 generate한다. decoder가 continuation의 i번째 토큰을 generate한 것을 product하여 prompt가 주어졌을 때 continuation이 생성될 확률 (좌변) 을 정의할 수 있다. 

 

canonical decoding approach는 next token distribution을 truncate하여 sampling하는 방식이었다. 예를 들어 nucleus sampling (top-p sampling) 은 가장 확률이 큰 토큰부터 그 확률을 누적했을 때 합이 p%가 될 때까지의 토큰들 중에서 무작위로 sampling하는 방식이다. top-k sampling은 가장 확률이 큰 k개의 토큰들 중 무작위로 sampling한다. search-based의 greedy decoding, beam search 등도 있다. 

 

Contrastive Decoding


Contrastive Objective

small LM이 expert LM보다 repetition, topic drift, and self contradiction과 같은 문제를 가지는 이유는, small LM과 large LM 모두 repetitive token에 큰 확률을 부여하지만 large LM은 그것에 대해 low confidence를 가지며 다른 토큰들에게도 상당한 확률을 부여하기 때문이다. 이렇게 하면 desirable output을 출력하면서도 non-repetitive continuation을 할 수 있다. 

 

CD의 objective function은 다음과 같고, 이를 최대화하는 text를 찾는 것이 목적이다.

위 식에 따르면 expert LM이 high probability를 부여한 토큰은 favor되고 amateur LM이 high probability를 부여한 토큰은 penalize된다. 그러나, 항상 amateur LM이 mistaken output만을 generate하는 것은 아니다. amateur LM은 simple grammatical structure와 common sense를 capture할 수 있기 때문이다. 따라서 무차별적으로 amateur LM을 penalize하는 것은 옳은 것을 틀린 것이라고 하는 False Negative 문제와, 그렇다면 반대로 틀린 것을 옳은 것이라고 하는 False Positive 문제가 발생할 수 있다. 

V_head: Adaptive Plausibility Constraint

Eq. (1)

alpha는 [0, 1] 사이의 hyperparameter로, expert LM의 output 중 일부를 truncate하는 것을 조절한다. 만약 alpha가 크다면 그만큼 truncate되는 output은 많아질 것이다. 

 

Eq. (1)을 해석하자면, i번째 토큰을 생성할 때 candidate pool에 여러 후보 토큰이 있을 것이다. 거기에서 최대 확률을 가지는 max token에 alpha를 곱한 값보다 큰 확률을 가지는 후보 토큰만 candidate vocabulary V_head에 남을 수 있다. 이 vocabulary V_head가 constraint로 작용하고, V_head 내에서 contrastive decoding을 통해 하나의 토큰을 뽑는다. 논문에서는 alpha = 0.1로 설정하고 있다. 

 

V_head라는 constraint는 false negative와 false positive 문제를 해결할 수 있다.

  • False Positive: 예를 들어 'message'라는 토큰이 exp LM은 3e-9, ama LM은 8e-14라는 낮은 확률을 가진다고 해보자. 그런데 contrastive decoding을 하면 'message'의 확률이 10.6으로 매우 커진다. 이때 V_head는 애초에 exp LM에서 low probability를 가지는 토큰을 filtering out함으로써 candidate pool에 어느 정도 그럴 듯한 (exp LM에서 높은 확률을 가지는) 토큰만 남겨두게 된다. 
  • False Negative: easy decision에서 expert LM과 amateur LM 모두 correct token을 잘 맞힐 것이다. 그러나 contrastive decoding을 하면 정답 토큰의 확률이 낮아진다. 예를 들어 "unic"과 "#orn"이라는 토큰이 있다고 하자. 두 개의 LM은 모두 "unic" 뒤에 "#orn"이 올 확률을 0.99로 예측하는데 CD를 거치면 6e-4의 낮은 확률을 갖게 된다. 만약 expert LM이 어떤 토큰에 대해 highly confident하다면 애초에 max prob이 매우 클 것이므로, V_head에는 하나 또는 몇 개의 토큰밖에 남지 않을 것이다. 이렇게 되면 어차피 고려 대상에 하나의 토큰밖에 없으므로 contrastive decoding으로 인한 false negative 문제를 상쇄할 수 있다. 

Full Method

Eq. (3)이 논문의 full method이다. 토큰 xi가 V_head에 속한다면 CD를 계산해 score를 계산하고 속하지 않는다면 그 확률을 -inf로 보내버린다. 이후 beam search를 이용해 CD-score를 최적화 (최대화) 하는 text generation을 찾는다. (search-based)

(Eq. (3)

Choice of Amateur

적절한 amateur LM을 고르는 것도 중요하다. amateur LM은 expert LM에서 failure tendency (reptitive, incoherence...)를 제대로 제거해야 한다. 예를 들어 n-gram LM은 너무 low capacity를 갖기 때문에 expert LM와 similar failure tendency를 갖지 않아 expert LM으로부터 제대로 그런 경향을 제거하지 못한다. -> 즉, amateur LM은 어느 정도 expert LM과 failure tendency를 공유하면서도 incoherence와 repetition tendency를 가져서 expert LM으로부터 이를 제거해야 한다...

  • Scale: OPT-13B vs. OPT-125M / GPT-2 XL vs. GPT-2 small
  • Temperature: amateur LM의 temperate 타우를 조절한다. 높은 temperature는 amateur LM이 flatter distribution에서 토큰들을 고르게 하고 낮은 temperature는 amateur LM이 sharper disrtibution에서 토큰들을 고르게 한다. 실험 결과, temperature는 0.5 ~ 1.0 사이가 가장 generalization이 잘된다고 한다. 
  • Context Window: amateur LM이 볼 수 있는 prompt는 x_prompt의 마지막 토큰으로 정하여 context window를 제한한다. (expert LM은 prompt 전체를 볼 수 있다.)

 

Experimental Setup


Datasets and Metrics

dataset으로는 news, Wikipedia, story domain을 사용했다. (prompt가 주어지면 그에 맞는 continuation을 generate하는 테스크) prompt로는 first 32 words를, continuation으로는 256 token을 generate하도록 했다. 

 

평가 방법 (metric) 으로 다음과 같은 요소를 사용했다.

  • diversity: unique n-grams는 x_cont에서 n-gram이 중복 없이 나온 횟수, total n-grams는 x_cont에서 전체 n-gram 횟수를 의미한다. 높을수록 repetition이 적은 것이다. 

  • MAUVE score: generated text와 golden reference의 distribution similarity를 측정한 것으로, 높을수록 좋은 것이다. 
  • coherence: x_prompt와 x_cont의 embedding의 cosine similarity로 coherence를 측정했다. 이때 embedding은 pre-trained SimCSE sentence embedding을 사용했다. 

  • Human Eval: 사람이 generated text를 평가할 때는 fluency와 coherence 두 가지 측면에서 평가했다. fluency는 correct grammatical English & natural flow를 가지는지를, coherence는 continuation이 prompt의 주제로부터 벗어나지 않는지를 평가했다. Amazon Mechanical Turkers로 하여금 same prompt에 대해 A와 B 두 가지 continuation 중 더 fluent & coherent한 것을 고르거나 아니면 두 개가 비슷한지를 결정하도록 했다. 

Baselines

  • sampling method: nucleus sampling (with p = 0.95), top-k sampling (k = 50), typical decoding (극단적인 확률값 truncate, 중간 확률값을 가지는 토큰 사용)
  • search-based method: greedy decoding, contrastive search

-> 즉, CD vs. baselines 중 어떤 decoding이 더 잘 generation하는지 metrics를 통해 평가한다. 

Models and Hyperparameters

  • expert LM: GPT-2 XL (1.5B) < OPT (6.7B) < OPT (13B)
  • amateur LM: GPT-2  small (100M) < OPT (125M)

OPT experiments에서는 amateur temperature = 1.0으로, GPT experiment에서는 temperature = 0.5로 설정했다. Contrastive Decoding에서 beam search에 사용되는 beam size = 5로 설정하여 difference가 가장 큰 토큰들을 찾아가며 continuation을 generate했다.

 

Main Results


Automatic Evaluation

CD가 다른 baseline에 비해 MAUVE score와 coherence score가 news, wikipedia, story 모두에서 가장 높았다. diversity 측면에서는 low probability token도 고를 수 있는 sampling method와 비슷하거나 조금 떨어졌다.

 

위의 model 설명으로 봐서, CD를 할 때 OPT-13B & OPT-125M, GPT-2 XL (1.5B) & GPT-2 small (100M)을 사용한 것 같다.

 

Table 1에서 max prob의 coherence가 CD 다음으로 높은데, 이는 단순히 repetition이 많기 때문이라고 해석된다. CD의 coherence가 significant margin으로 outperformance를 보이는 것을 다음과 같이 해석한다. 

  • CD는 CD-score를 최적화할 때 topic drift가 일어나지 않도록 한다. -> high coherent
  • large LM이 small LM보다 coherent하기 때문에 difference 한 것이 large LM의 coherence에 implicit reward를 준 것과 같다. -> coherency에 reward를 줌. 
  • amateur LM에게 제공하는 prompt를 제한해 CD로 하여금 prompt와 continuation이 이어질 때 reward를 주도록 학습됐다.

Human Evaluation

Ablation Studies


Size of Amateur and Expert LMs

Figure 2의 diagonal entries를 보면 low diversity를 가진 것을 알 수 있는데, 이는 같은 LM으로부터는 contrastive decoding의 효과를 볼 수 없기 때문이다. upper triangular entries는 large LM을 amateur로, small LM을 teacher로 사용했는데 이 역시 inferior text quality를 확인할 수 있다. 반면, lower triangular entries는 large LM을 expert로, small LM을 amateur로 사용했기 때문에 CD를 했을 때 expert LM으로부터 amateur tendecy를 제거하고 good knowledge만을 살려 high diversity, high MAUVE score를 가짐을 알 수 있다. 가장 최적의 scale design은 XL LM을 expert로, small LM을 amateur로 쓰는 것이다. 

The Impact of Amateur Temperature

large temperature in amateur는 flatter distribution을 만들기 때문에 CD에서 별 효과를 보이지 못한다. (CD에서 penalize 효과가 없다.) small temperature는 amateur LM을 더욱 spiky하게 만들고 amateur LM의 undesirable amateur behavior를 강조하기 때문에 CD의 효과를 강하게 해준다. [0.5, 1.5] 사이의 temperature가 good and robust performance in coherence and fluency를 보여준다. 

Sampling vs. Search

A는 CD를 beam search로 최적화한 search-based이고 B는 CD objective를 sampling한 것이다. A와 B의 퀄리티를 coherence와 fluency 관점에서 human evaluation으로 비교한 결과, A (search) 를 더욱 선호했다. 그러나 CD with sampling도 꽤나 좋은 성능을 보였다. (same은 A와 B의 퀄리티를 같다고 평가한 것이다.)