Language models, retrieval, prompting, and language adaptation · Long-context and retrieval-augmented language · LNG045 · Architecture
Transformer-XL
Extends language-model context through segment-level recurrence, reusing hidden states from previous segments. Relative positional representations permit cached context to remain interpretable as segment boundaries move.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Extends language-model context through segment-level recurrence, reusing hidden states from previous segments. Relative positional representations permit cached context to remain interpretable as segment boundaries move.
Question / objective
Can recurrent memory extend usable language context across fixed-length training segments?
Architecture / computational structure
Per-layer cached hidden states are concatenated with current states and detached for backpropagation, with relative positions. Learned attention/recurrent-segment processing weights are parameters; memory and segment lengths are hyperparameters.
Assumptions
Cached states can become stale as parameters change, and incorrect cache reuse can leak across independent documents or users.
Formula / computational definition
For current hidden sequence h_t, keys/values incorporate concat(stopgrad(h_(t-1)),h_t). The previous segment's states supply memory but are detached from the current backward pass, limiting gradient propagation across segments.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Segment length, memory length, relative-position scheme, layer depth, cache reset policy, and batch/document alignment.
Fitting, fine-tuning, and adaptation
Fit next-token loss with document-aware batching and cache resets. Adapt via full or supported parameter-efficient updates only after verifying architecture support; freezing caches is not equivalent to freezing model weights. Validate memory-length curves, boundary leakage, and general-domain retention after domain training.
How to use and calibrate
Plot perplexity against memory length, inspect distant-context tasks, and report memory/throughput tradeoffs with document boundaries enforced. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Model long technical documents whose dependencies span ordinary segment boundaries.
Interpretation / pitfalls
Watch out
Accessible memory does not imply that all long-range dependencies are learned. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: Use the authors' implementation or supported Transformer-XL integrations; API support can differ across library versions. R bridges Python or implements the recurrence in R torch. The native concatenation demo highlights what is cached, while stop-gradient requires an autodiff framework.
Example scope: Transformer-XL memory concatenation and truncation; autodiff stop-gradient is not shown.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Transformer-XL memory concatenation and truncation; autodiff stop-gradient is not shown.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
previous = [[1,0],[0,1],[0.5,0.5]]; current = [[0.2,0.8],[0.6,0.4]]
memory_length = 2
attention_memory = previous[-memory_length:] + current
print(attention_memory)
Component demo, not a trained model — Transformer-XL memory concatenation and truncation; autodiff stop-gradient is not shown.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
previous <- rbind(c(1,0),c(0,1),c(0.5,0.5)); current <- rbind(c(0.2,0.8),c(0.6,0.4))
memory_length <- 2L
attention_memory <- rbind(tail(previous,memory_length),current)
print(attention_memory)
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG045.