๐ง Lost in the Middle - ๊ธด ๋ฌธ๋งฅ์์ ์ธ์ด๋ชจ๋ธ์ด ์ง์ง ์ ๋ณด๋ฅผ ๊ธฐ์ตํ ๊น?
๐ง Reading the Paper Lost in the Middle
๐ LLMs struggle to remember information located in the middle of long documents!
Paper: Lost in the Middle: How Language Models Use Long Contexts
Venue: TACL 2023 (Liu, Nelson F., et al.)
โ Core Question from the Paper
โCan language models utilize information equally regardless of its position in a long context?โ
Short answer: No.
- Most LLMs are least effective at recalling information located in the middle of long documents.
- Even with large context windows, position bias still persists.
As shown below, the performance follows a
U-shape
curve:
Models perform best when the answer is at the beginning (primacy) or end (recency),
but significantly worse when it is in the middle.
๐งช Experiment: Needle-in-a-Haystack
Setup:
- Insert a single key fact (โneedleโ) into a long passage
- Place it at the beginning / middle / end of the input
- Ask the model to extract that specific information
1
2
3
4
5
Example:
Document length = 8,000 tokens
[... lengthy text ...]
โ Insert target sentence in the middle
โ Ask: "What was the number mentioned in the document above?"
๐ The target sentence is hidden in the middle of the input like this:
๐ Summary of Results (Figure 5)
When the answer is in the middle of the document, model accuracy drops significantly.
โ Most models, including GPT-3.5, show a U-shaped performance curve.
Position | Recall (GPT-3.5) | Recall (Claude-1.3) |
---|---|---|
Beginning | High | High |
Middle โ ๏ธ | Lowest | Slightly lower |
End | High | High |
๐ GPT-4 also shows similar patterns in a subset of experiments,
but was excluded from full-scale experiments due to high cost (see Appendix D).
๐ Why does this happen? (Section ยง2.3, ยง3.2)
- ๐ Limitations of absolute positional encoding
- ๐ Self-attentionโs inherent position bias
โ Stronger focus on early (primacy) and late (recency) positions
โ Middle positions receive less attention - ๐ The longer the input, the more the performance degrades,
with over 20% drop in 30-document settings (GPT-3.5)
๐ง Why does this matter?
Most real-world tasks like RAG, multi-document QA, and summarization rely on long input contexts.
But what if the model ignores the middle?
- ๐ The position of retrieved documents directly impacts answer accuracy
- ๐ Effective chunking and ordering of key information is critical
- โ Simply increasing the context window size is not enough
๐ก Takeaways: Position Bias Matters
โLLMs can remember contextโbut mainly the beginning and the end.โ
Strategies to mitigate position bias:
- โ
Query-aware contextualization
โ Place the query before the documents for decoder-only models - โ
Chunk ordering optimization
โ Put more relevant content earlier in the input - โ
Improved attention architectures
โ Encoder-decoder models (e.g., T5, BART) perform better with long input - โ
Position-free architectures
โ Hyena, RWKV, and other models aim to remove positional dependence
๐ Retrieval-Based QA Setup (Section ยง2.2)
- Task: Multi-document question answering
- Retriever: Contriever (fine-tuned on MS-MARCO)
- Reader input: Top-k retrieved documents + query
- Number of docs (k): 10, 20, 30
- Document type: Only paragraphs (no tables or lists)
๐ Impact of Increasing Retrieved Docs (Figure 5)
- โ k = 10 or 20 โ improved accuracy
- โ ๏ธ k = 30 โ performance plateaus or drops
- When the relevant document appears in the middle, accuracy drops
- Some models even perform worse than closed-book setting
โ Retrieval Alone Is Not Enough
- Even if retrieval includes the correct document,
models may fail to use it effectively, especially if itโs in the middle.
Retrieval โ success
โ Prompt design must account for position bias
Practical strategies:
- โ Move relevant docs closer to the top
- โ Use query-aware formatting
- โ Minimize irrelevant context
โ TL;DR
โLLMs remember long contexts โ but often forget whatโs in the middle.โ
๐ง (ํ๊ตญ์ด) Lost in the Middle
๋
ผ๋ฌธ ์ฝ๊ธฐ
๐ LLM์ ๊ธด ๋ฌธ์ ์ค๊ฐ์ ์๋ ์ ๋ณด๋ ์ ๊ธฐ์ตํ์ง ๋ชปํจ!!
๋ ผ๋ฌธ: Lost in the Middle: How Language Models Use Long Contexts
๋ฐํ: TACL 2023 (Liu, Nelson F., et al.)
โ ๋ ผ๋ฌธ์ด ๋์ง ํต์ฌ ์ง๋ฌธ
โ๊ธด context ์์์, ๋ชจ๋ธ์ **๋ชจ๋ ์์น์ ์ ๋ณด๋ฅผ ๊ท ๋ฑํ๊ฒ ํ์ฉํ ์ ์์๊น?โ
๊ฒฐ๋ก : No.
- ๋๋ถ๋ถ์ LLM์ ๊ธด ๋ฌธ์์์ ์ค๊ฐ ์ ๋ณด๋ฅผ ๊ฐ์ฅ ์ ๋์นฉ๋๋ค.
- context window๊ฐ ์๋ฌด๋ฆฌ ๊ธธ์ด๋ ์์น ํธํฅ(position bias)์ด ์กด์ฌํฉ๋๋ค.
์๋ ์ด๋ฏธ์ง์ฒ๋ผ
U-shape
์ฑ๋ฅ ๊ณก์ ์ด ๋ํ๋๋ฉฐ,
์(primacy)๊ณผ ๋ค(recency) ์ ๋ณด๋ ์ ๊ธฐ์ตํ์ง๋ง,
์ค๊ฐ ์ ๋ณด๋ ๊ธฐ์ต๋ ฅ์ด ๊ธ๋ฝํฉ๋๋ค.
๐งช ์คํ: Needle-in-a-Haystack
์คํ ๊ตฌ์ฑ:
- ๊ธด ๋ฌธ์์ ๋จ ํ๋์ ํต์ฌ ์ ๋ณด (โneedleโ)๋ฅผ ์ฝ์
- ์ ๋ณด๋ฅผ ๋ฌธ์์ ์ / ์ค๊ฐ / ๋์ ์์น์ํค๊ณ ๋น๊ต
- ๋ชจ๋ธ์๊ฒ ํด๋น ์ ๋ณด๋ฅผ ์ ํํ ์ถ์ถํ๋๋ก ์ง๋ฌธ
1
2
3
4
5
์์:
๋ฌธ์ ๊ธธ์ด = 8,000 tokens
[... ๊ธด ํ
์คํธ ...]
โ ์ค๊ฐ์ "์ ๋ต ๋ฌธ์ฅ" ์ฝ์
โ ๋ชจ๋ธ์๊ฒ: "์ ๋ฌธ์์ ๋์จ ์ซ์๋ ๋ช์ด์์ง?" ์ง๋ฌธ
๐ ์๋์ ๊ฐ์ด ์ค๊ฐ ์์น์ ํต์ฌ ์ ๋ณด๊ฐ ์๋ ํ๋กฌํํธ๋ฅผ ํ์ฉํ์ฌ ์ฑ๋ฅ์ ์ธก์ ํฉ๋๋ค:
๐ ์คํ ๊ฒฐ๊ณผ ์์ฝ
๋ฌธ์ ์ค๊ฐ์ ์ ๋ณด๊ฐ ์์ ๊ฒฝ์ฐ, ๋ชจ๋ธ ์ ํ๋๊ฐ ๊ธ๋ฝ
โ GPT-3.5 ๋ฐ ๋๋ถ๋ถ์ ๋ชจ๋ธ์์ U์ํ ์ฑ๋ฅ ๊ณก์ ์ด ๋ํ๋จ
์์น | ํ์๋ฅ (GPT-3.5) | ํ์๋ฅ (Claude-1.3) |
---|---|---|
์๋ถ๋ถ | ๋์ | ๋์ |
์ค๊ฐ โ ๏ธ | ์ต์ ์ฑ๋ฅ | ์ํญ ์ ํ |
๋๋ถ๋ถ | ๋์ | ๋์ |
๐ GPT-4๋ ์ผ๋ถ ์คํ์์ ์ ์ฌํ ์ฑ๋ฅ ํจํด์ ๋ณด์์ผ๋,
์ ์ฒด ์คํ์๋ ํฌํจ๋์ง ์์์ผ๋ฉฐ Appendix D์ ์ ํ์ ์ผ๋ก ๋ณด๊ณ ๋จ.
๐ ์ด ํ์์ ์์ธ (๋ ผ๋ฌธ ยง2.3, ยง3.2)
- ๐ Absolute positional encoding์ ๊ตฌ์กฐ์ ํ๊ณ
- ๐ Self-attention์ ์์น ํธํฅ(position bias)
โ ์(primacy)๊ณผ ๋ค(recency)์ ์ฃผ์๋ฅผ ์ง์ค, ์ค๊ฐ์ ํฌ์ - ๐ ๋ฌธ์ ๊ธธ์ด๊ฐ ๊ธธ์๋ก ์ฑ๋ฅ ํ๋ฝ ํญ์ด ๋ ์ปค์ง
โ GPT-3.5 ๊ธฐ์ค 30-document ์ค์ ์์ 20% ์ด์ ์ฑ๋ฅ ํ๋ฝ
๐ง ์ ์ค์ํ๊ฐ?
๋๋ถ๋ถ์ RAG (Retrieval-Augmented Generation), multi-document QA, long-context summarization ์์คํ ์
๊ธด ๋ฌธ๋งฅ์ ํ์ฉํฉ๋๋ค. ๊ทธ๋ฐ๋ฐ ์ค๊ฐ ์ ๋ณด๋ฅผ ๋ชจ๋ธ์ด ๋ฌด์ํ๋ค๋ฉด?
- ๐ ๊ฒ์ ๊ฒฐ๊ณผ์ ์์น๊ฐ QA ์ฑ๋ฅ์ ์ง์ ์ํฅ
- ๐ ์ค์ ๋ฌธ์๋ ํต์ฌ ์ ๋ณด๋ ์์ชฝ์ ๋ฐฐ์นํด์ผ ํจ๊ณผ์
- โ ๋จ์ํ context window๋ฅผ ๋๋ฆฌ๋ ๊ฒ๋ง์ผ๋ก๋ ๋ฌธ์ ํด๊ฒฐ โ
๐ก ์์ฌ์ : ์์น ํธํฅ์ ๊ณ ๋ คํ ํ์ฉ ์ ๋ต
โLLM์ context๋ฅผ ๊ธฐ์ตํ๋ค. ํ์ง๋ง, ๊ทธ๊ฑด ์์๊ณผ ๋์ผ ๋ฟโฆโ
์์น ํธํฅ์ ์ค์ด๊ธฐ ์ํ ์ ๋ต:
- โ
Query-aware contextualization
โ ๋์ฝ๋-์จ๋ฆฌ ๋ชจ๋ธ์์๋ ์ง๋ฌธ์ ๋ฌธ์ ์์ ๋จผ์ ์ ์ - โ
Chunk ordering optimization
โ ์ค์ํ ์ ๋ณด๋ฅผ ์์ชฝ์, ๋ ์ค์ํ ๊ฑด ๋ค๋ก ์ฌ๋ฐฐ์น - โ
Attention ๊ตฌ์กฐ ๊ฐ์
โ ์๋ฐฉํฅ ์ธ์ฝ๋๊ฐ ์๋ ๋ชจ๋ธ (T5, BART ๋ฑ)์ด ๋ ์ ๋ฆฌ - โ
Position-free architectures
โ Hyena, RWKV ๋ฑ ์๋ก์ด ๊ตฌ์กฐ๋ ์์น ๋ ๋ฆฝ์ฑ์ ์ถ๊ตฌํจ
๐ Retrieval ๊ธฐ๋ฐ ์คํ ๊ตฌ์ฑ (๋ ผ๋ฌธ ยง2.2)
- Task: Multi-document QA
- Retriever: Contriever (MS-MARCO fine-tuned)
- Reader ์ ๋ ฅ: ๊ฒ์๋ k๊ฐ์ ๋ฌธ์ + ์ง๋ฌธ
- ๋ฌธ์ ์(k): 10, 20, 30๊ฐ
- ๋ฌธ์ ํํ: paragraph ๊ธฐ๋ฐ (ํ, ๋ชฉ๋ก์ ์ ์ธ)
๐ Retrieval ์ ์ฆ๊ฐ vs ์ฑ๋ฅ ๋ณํ
- โ k=10, 20 โ ์ฑ๋ฅ ํฅ์
- โ ๏ธ k=30 โ ์ฑ๋ฅ ํ๋ฝ ๋๋ ํฌํ
- ์ ๋ต ๋ฌธ์๊ฐ ์ค๊ฐ์ ์์นํ ๊ฒฝ์ฐ ์ ํ๋ ๊ธ๋ฝ
- ์ผ๋ถ ๋ชจ๋ธ์ closed-book ์ฑ๋ฅ๋ณด๋ค๋ ๋ฎ์์ง
โ Retrieval์ ์ ๋์ด๋, ํ์ฉ์ ์ด๋ ค์
- LLM์ ์ ๋ต ๋ฌธ์๋ฅผ ๋ฐ์๋,
๊ทธ ์ ๋ณด๊ฐ ์ค๊ฐ์ ์์ผ๋ฉด ์ ์ฌ์ฉํ์ง ๋ชปํจ
Retrieval๋ง ์ํด๋ ์ฑ๋ฅ์ด ๋ณด์ฅ๋์ง ์์!
โ LLM์ ์์น ํธํฅ์ ๊ณ ๋ คํ prompt ๊ตฌ์กฐ ์ค๊ณ ํ์
ํด๊ฒฐ ์ ๋ต ์์:
- โ ์ ๋ต ๋ฌธ์๋ฅผ ํ๋กฌํํธ ์์ ๋ฐฐ์น
- โ Query-aware ๊ตฌ์กฐ ์ฌ์ฉ
- โ Noise ๋ฌธ์ ์๋ฅผ ์ค์ด๊ธฐ (๋ฌธ์ ์ ํ ์์ถ)
โ ํ ์ค ์์ฝ
โLLM์ ๊ธด context๋ฅผ ๊ธฐ์ตํ์ง๋ง context ๋ด์ ์ค๊ฐ๋ถ๋ถ์ ์ ๋ง๊ฐํ๋ค!!!