mirror of
https://github.com/hanzoai/papers.git
synced 2026-10-29 07:34:31 +00:00
328 lines
13 KiB
TeX
328 lines
13 KiB
TeX
\documentclass[11pt,a4paper]{article}
|
|
\usepackage[utf8]{inputenc}
|
|
\usepackage[T1]{fontenc}
|
|
\usepackage{amsmath,amsfonts,amssymb}
|
|
\usepackage{graphicx}
|
|
\usepackage{hyperref}
|
|
\usepackage{listings}
|
|
\usepackage{color}
|
|
\usepackage{booktabs}
|
|
\usepackage{float}
|
|
\usepackage{algorithm}
|
|
\usepackage{algorithmic}
|
|
\usepackage{geometry}
|
|
\geometry{margin=1in}
|
|
\definecolor{zenblue}{RGB}{41,121,255}
|
|
\hypersetup{colorlinks=true,linkcolor=zenblue,urlcolor=zenblue,citecolor=zenblue}
|
|
|
|
\title{\textbf{Long Context Scaling in Zen Models: 1M Token Extension}\\
|
|
\large Technical Report v2025.07}
|
|
\author{Antje Worring, Zach Kelling, Zen LM Research Team\\
|
|
\texttt{[email protected]}}
|
|
\date{July 2025}
|
|
|
|
\begin{document}
|
|
\maketitle
|
|
|
|
\begin{abstract}
|
|
We describe techniques for extending Zen model context windows from 32K to 1M tokens.
|
|
Our approach combines dynamic NTK-aware RoPE scaling, memory-efficient sliding window
|
|
attention for the bulk of context, ring attention for multi-GPU context distribution,
|
|
and targeted long-context fine-tuning on 50K documents exceeding 100K tokens. On
|
|
the RULER benchmark, Zen achieves 94.1\% at 128K context, 87.3\% at 512K, and
|
|
79.8\% at 1M tokens. The needle-in-haystack retrieval task shows $>95\%$ recall
|
|
up to 512K context. Memory overhead for 1M context on Zen-7B requires 8$\times$A100
|
|
80GB with ring attention, compared to infeasible memory requirements for naive attention.
|
|
\end{abstract}
|
|
|
|
\section{Introduction}
|
|
|
|
The practical utility of language models scales dramatically with context window size.
|
|
Repository-level code completion, book-length summarization, and multi-document
|
|
reasoning all require contexts exceeding the 4K--32K windows of first-generation
|
|
instruction-tuned models.
|
|
|
|
Extending context windows faces two independent challenges:
|
|
\begin{enumerate}
|
|
\item \textbf{Positional encoding generalization}: Models trained with rotary position
|
|
embeddings (RoPE) at 32K tokens degrade on longer sequences because RoPE frequencies
|
|
were not calibrated for larger positions.
|
|
\item \textbf{Memory complexity}: Naive self-attention is $O(n^2)$ in sequence length,
|
|
making 1M-token context require $10^{12}$ attention operations per layer.
|
|
\end{enumerate}
|
|
|
|
We address both challenges: NTK-aware RoPE scaling for positional generalization,
|
|
and ring attention for distributed $O(n)$ per-GPU memory scaling.
|
|
|
|
\section{Background}
|
|
|
|
\subsection{Rotary Position Embeddings}
|
|
|
|
RoPE~\cite{su2021rope} encodes position by rotating query and key vectors in
|
|
$d/2$ 2D planes, with rotation frequency $\theta_i = 10000^{-2i/d}$ for $i \in [0, d/2)$:
|
|
|
|
\begin{equation}
|
|
\text{RoPE}(x, m)_i = x_i \cos(m\theta_i) - x_{i+1}\sin(m\theta_i)
|
|
\end{equation}
|
|
|
|
For position $m > m_\text{train}$, rotation frequencies exceed those seen during
|
|
training, causing catastrophic degradation.
|
|
|
|
\subsection{Sliding Window Attention}
|
|
|
|
Sliding window attention~\cite{beltagy2020longformer} restricts each token to attend
|
|
only to a fixed window $w$ of neighboring tokens:
|
|
\begin{equation}
|
|
\text{attn}(q_i, K, V) = \sum_{j \in [i-w, i+w]} \text{softmax}(q_i k_j^\top/\sqrt{d}) v_j
|
|
\end{equation}
|
|
|
|
This reduces complexity to $O(n \cdot w)$ but loses global context.
|
|
|
|
\subsection{Ring Attention}
|
|
|
|
Ring attention~\cite{liu2023ring} distributes the $n^2$ attention computation across
|
|
$P$ GPUs, each holding $n/P$ tokens. Each GPU computes attention for its slice while
|
|
rotating KV blocks around a ring of GPUs:
|
|
|
|
\begin{equation}
|
|
\text{Memory per GPU} = O\left(\frac{n}{P} \cdot d\right), \quad \text{Compute} = O\left(\frac{n^2}{P} \cdot d\right)
|
|
\end{equation}
|
|
|
|
This enables linear memory scaling with GPU count.
|
|
|
|
\section{NTK-Aware Dynamic RoPE Scaling}
|
|
|
|
\subsection{Standard Linear Interpolation}
|
|
|
|
The naive approach to extending RoPE is linear interpolation: scale all positions
|
|
by $s = m_\text{train}/m_\text{target}$~\cite{chen2023extending}:
|
|
|
|
\begin{equation}
|
|
\theta_i^{\text{scaled}} = \theta_i / s
|
|
\end{equation}
|
|
|
|
This compresses positions into the trained range but reduces positional resolution,
|
|
degrading performance on short contexts.
|
|
|
|
\subsection{NTK-Aware Scaling}
|
|
|
|
Neural Tangent Kernel theory suggests that the base $b$ of RoPE frequencies should
|
|
scale with context length. Dynamic NTK-aware scaling adjusts the base:
|
|
|
|
\begin{equation}
|
|
b_\text{dynamic} = b_\text{base} \cdot \left(\frac{s \cdot d}{d - 2}\right)^{d/(d-2)}
|
|
\end{equation}
|
|
|
|
where $s = n / n_\text{train}$ is the scale factor. This preserves short-context
|
|
fidelity while extending long-context range.
|
|
|
|
\subsection{YaRN: Yet Another RoPE eNtension}
|
|
|
|
We extend NTK-aware scaling with YaRN~\cite{peng2023yarn}, which applies different
|
|
scaling to low-frequency and high-frequency RoPE dimensions:
|
|
|
|
\begin{equation}
|
|
\theta_i^\text{YaRN} = \begin{cases} \theta_i & \text{if } \lambda_i < 1 \\ \theta_i / s & \text{if } \lambda_i > \alpha \\ \text{interpolate} & \text{otherwise} \end{cases}
|
|
\end{equation}
|
|
|
|
where $\lambda_i = 2\pi n_\text{train} / \theta_i$ is the wavelength and $\alpha = 1$
|
|
separates low/high frequency regimes. An attention scaling factor $\sqrt{\ln(n)/\ln(n_\text{train})}$
|
|
is applied to compensate for entropy changes at long context.
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lcccc}
|
|
\toprule
|
|
\textbf{Method} & \textbf{32K PPL} & \textbf{128K PPL} & \textbf{512K PPL} & \textbf{1M PPL} \\
|
|
\midrule
|
|
No extension (RoPE fail) & 6.84 & $\infty$ & $\infty$ & $\infty$ \\
|
|
Linear interpolation & 6.87 & 8.21 & 12.4 & 18.7 \\
|
|
NTK-aware & 6.85 & 7.43 & 9.8 & 14.2 \\
|
|
YaRN (ours) & \textbf{6.84} & \textbf{7.11} & \textbf{8.4} & \textbf{11.3} \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{Perplexity at various context lengths on PG-19 test set (Zen-7B).}
|
|
\end{table}
|
|
|
|
\section{Hybrid Attention Architecture}
|
|
|
|
For contexts beyond 32K tokens, full attention is prohibitively expensive even
|
|
with ring attention. We use a hybrid architecture combining sliding window attention
|
|
for most layers and full global attention for select layers:
|
|
|
|
\begin{equation}
|
|
\text{Layer}(i) = \begin{cases} \text{SlidingWindow}(w=4096) & \text{if } i \notin \mathcal{G} \\ \text{GlobalAttention} & \text{if } i \in \mathcal{G} \end{cases}
|
|
\end{equation}
|
|
|
|
where $\mathcal{G}$ is the set of global attention layers. We set $|\mathcal{G}| = 8$
|
|
(every 4th layer in a 32-layer model). This reduces attention FLOPs by 74\% for
|
|
128K context while preserving global information flow.
|
|
|
|
\subsection{Memory-Efficient Attention Implementation}
|
|
|
|
All attention is implemented with Flash Attention 3~\cite{shah2023flashattention3},
|
|
providing $O(1)$ memory in sequence length (trading memory for recomputation
|
|
in the backward pass). For ring attention, KV shards are streamed between GPUs
|
|
in a pipelined fashion to overlap communication with computation.
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lrrrr}
|
|
\toprule
|
|
\textbf{Context Length} & \textbf{Naive (GB)} & \textbf{Flash Attn (GB)} & \textbf{Ring (8 GPU, GB)} & \textbf{Feasible?} \\
|
|
\midrule
|
|
32K & 8.2 & 0.8 & -- & Yes (1 GPU) \\
|
|
128K & 130 & 3.1 & -- & Flash only \\
|
|
512K & 2048 & 49 & 6.1/GPU & 8$\times$A100 \\
|
|
1M & 8192 & 196 & 24.5/GPU & 8$\times$H100 \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{Attention memory requirements for Zen-7B. Ring attention makes 1M context feasible.}
|
|
\end{table}
|
|
|
|
\section{Long-Context Fine-Tuning}
|
|
|
|
\subsection{Data Construction}
|
|
|
|
We curate a long-context fine-tuning dataset of 50K documents exceeding 100K tokens:
|
|
\begin{itemize}
|
|
\item Legal documents and case law (18K samples, avg. 250K tokens)
|
|
\item Scientific papers with appendices (15K samples, avg. 80K tokens)
|
|
\item Books and long-form fiction (12K samples, avg. 150K tokens)
|
|
\item Code repositories (5K samples, avg. 200K tokens)
|
|
\end{itemize}
|
|
|
|
\subsection{Continuation Fine-Tuning Protocol}
|
|
|
|
Long-context fine-tuning continues from the 32K-context checkpoint for 10K steps:
|
|
\begin{itemize}
|
|
\item YaRN scaling applied to RoPE from step 1
|
|
\item Learning rate $5\times10^{-6}$, linear decay to 0
|
|
\item Batch size: 1 document per GPU (effective batch = 8--16 docs)
|
|
\item Loss computed only on the final 20\% of each document to emphasize long-range dependency
|
|
\end{itemize}
|
|
|
|
The truncated loss objective forces the model to integrate early context for
|
|
late predictions:
|
|
\begin{equation}
|
|
\mathcal{L}_\text{long} = -\sum_{t > 0.8n} \log p_\theta(x_t \mid x_{<t})
|
|
\end{equation}
|
|
|
|
\section{Evaluation}
|
|
|
|
\subsection{RULER Benchmark}
|
|
|
|
RULER~\cite{hsieh2024ruler} provides a suite of tasks at controlled context lengths:
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lcccccc}
|
|
\toprule
|
|
\textbf{Model} & \textbf{32K} & \textbf{64K} & \textbf{128K} & \textbf{256K} & \textbf{512K} & \textbf{1M} \\
|
|
\midrule
|
|
Zen-7B (32K) & 95.8 & 78.2 & 41.3 & -- & -- & -- \\
|
|
Zen-7B (128K) & 95.6 & 93.1 & 94.1 & 81.4 & 52.3 & -- \\
|
|
Zen-7B (1M) & 95.4 & 93.0 & 94.1 & 89.2 & 87.3 & 79.8 \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{RULER accuracy (\%) at various context lengths. 1M model extends all prior limits.}
|
|
\end{table}
|
|
|
|
\subsection{Needle-in-Haystack Retrieval}
|
|
|
|
We insert a single fact (``needle'') at a random position in a long document (``haystack'')
|
|
and ask the model to retrieve it. Recall at various depths and context lengths:
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lcccccc}
|
|
\toprule
|
|
\textbf{Depth} & \textbf{32K} & \textbf{64K} & \textbf{128K} & \textbf{256K} & \textbf{512K} & \textbf{1M} \\
|
|
\midrule
|
|
10\% (near start) & 99.1 & 98.8 & 98.4 & 97.9 & 97.2 & 91.3 \\
|
|
50\% (middle) & 98.4 & 97.2 & 96.8 & 95.3 & 94.1 & 84.2 \\
|
|
90\% (near end) & 99.2 & 99.0 & 98.9 & 98.4 & 97.8 & 93.1 \\
|
|
\midrule
|
|
Average & \textbf{98.9} & \textbf{98.3} & \textbf{98.0} & \textbf{97.2} & \textbf{96.4} & \textbf{89.5} \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{Needle-in-haystack recall (\%) by depth and context length (Zen-7B 1M).}
|
|
\end{table}
|
|
|
|
The ``lost in the middle'' phenomenon (lower middle-depth recall) is minimal at contexts
|
|
up to 512K, appearing at 1M context with a 7-point gap versus near-end recall.
|
|
|
|
\subsection{Long-Context Retrieval: SCROLLS}
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lcccc}
|
|
\toprule
|
|
\textbf{Model} & \textbf{GovReport} & \textbf{QMSum} & \textbf{SummScreen} & \textbf{QASPER} \\
|
|
\midrule
|
|
Zen-7B (32K) & 51.2 & 22.4 & 27.8 & 41.3 \\
|
|
Zen-7B (1M) & \textbf{56.8} & \textbf{26.1} & \textbf{32.4} & \textbf{47.8} \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{SCROLLS benchmark ROUGE-1 scores comparing 32K and 1M context.}
|
|
\end{table}
|
|
|
|
\section{Analysis}
|
|
|
|
\subsection{Position Interpolation vs. YaRN at Short Context}
|
|
|
|
A critical concern is whether long-context extension degrades short-context performance.
|
|
We measure perplexity at 2K, 4K, and 8K contexts:
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lccc}
|
|
\toprule
|
|
\textbf{Method} & \textbf{2K PPL} & \textbf{4K PPL} & \textbf{8K PPL} \\
|
|
\midrule
|
|
Original 32K model & 6.31 & 6.58 & 6.74 \\
|
|
Linear interpolation & 6.44 & 6.68 & 6.82 \\
|
|
YaRN (ours) & \textbf{6.32} & \textbf{6.59} & \textbf{6.75} \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{Short-context perplexity. YaRN preserves short-context quality; linear interpolation degrades it.}
|
|
\end{table}
|
|
|
|
\subsection{Throughput at Scale}
|
|
|
|
\begin{table}[H]
|
|
\centering
|
|
\begin{tabular}{lrrrr}
|
|
\toprule
|
|
\textbf{Context} & \textbf{1 GPU} & \textbf{2 GPU} & \textbf{4 GPU} & \textbf{8 GPU} \\
|
|
\midrule
|
|
32K & 1420 tok/s & 2750 tok/s & 5280 tok/s & 9840 tok/s \\
|
|
128K & OOM & 1180 tok/s & 2350 tok/s & 4610 tok/s \\
|
|
512K & OOM & OOM & OOM & 1230 tok/s \\
|
|
1M & OOM & OOM & OOM & 640 tok/s \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
\caption{Zen-7B prefill throughput at various context lengths and GPU counts (A100 80GB).}
|
|
\end{table}
|
|
|
|
\section{Conclusion}
|
|
|
|
The combination of YaRN dynamic RoPE scaling, hybrid sliding window + global attention,
|
|
ring attention for multi-GPU distribution, and targeted long-context fine-tuning
|
|
extends Zen model context to 1M tokens with 79.8\% RULER accuracy and $>89\%$
|
|
needle-in-haystack recall. The approach preserves short-context quality exactly
|
|
and enables practical long-context use cases at reasonable compute cost.
|
|
|
|
\bibliographystyle{plain}
|
|
\begin{thebibliography}{99}
|
|
\bibitem{su2021rope} Su et al. (2021). RoFormer: Enhanced Transformer with Rotary Position Embedding. \textit{arXiv:2104.09864}.
|
|
\bibitem{beltagy2020longformer} Beltagy et al. (2020). Longformer: The Long-Document Transformer. \textit{arXiv:2004.05150}.
|
|
\bibitem{liu2023ring} Liu et al. (2023). Ring Attention with Blockwise Transformers for Near-Infinite Context. \textit{arXiv:2310.01889}.
|
|
\bibitem{chen2023extending} Chen et al. (2023). Extending Context Window of Large Language Models via Positional Interpolation. \textit{arXiv:2306.15595}.
|
|
\bibitem{peng2023yarn} Peng et al. (2023). YaRN: Efficient Context Window Extension of Large Language Models. \textit{arXiv:2309.00071}.
|
|
\bibitem{shah2023flashattention3} Shah et al. (2024). FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision. \textit{arXiv:2407.08608}.
|
|
\bibitem{hsieh2024ruler} Hsieh et al. (2024). RULER: What's the Real Context Size of Your Long-Context Language Models? \textit{arXiv:2404.06654}.
|
|
\end{thebibliography}
|
|
|
|
\end{document}
|