Skip to content

Language models, retrieval, prompting, and language adaptation · Autoregressive and open-weight model families · LNG036 · Model

Mistral 7B

An efficient decoder-only model illustrating grouped-query attention and sliding-window attention in a language-model system.

Explained here Component demo, not a trained model CodeSplash's own run

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

An efficient decoder-only model illustrating grouped-query attention and sliding-window attention in a language-model system. Its technical choices are documented as a specific design rather than assumed to apply unchanged to every later Mistral-branded model.

Question / objective

How can sliding attention and shared key/value heads improve a decoder's efficiency?

Architecture / computational structure

A causal decoder combines local attention windows with grouped-query key/value sharing in the documented 7B design. Learned projections and feed-forward weights are parameters; window, head grouping, and cache strategy are hyperparameters/configuration.

Assumptions

Local receptive fields can restrict direct access to distant evidence; information propagation across layers is not equivalent to perfect long-range recall.

Formula / computational definition

Mij={0,max⁡(0,i−w+1)≤j≤i,−∞,otherwise.M_{ij}=\begin{cases}0,&\max(0,i-w+1)\le j\le i,\\-\infty,&\text{otherwise.}\end{cases}

A causal local-attention mask permits a query at position t to attend to a bounded recent window. Stacking layers expands information propagation, while grouped-query attention shares key/value heads among multiple query heads.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Attention window, query versus key/value head counts, layer depth, context handling, cache implementation, tokenizer, and generation controls.

Fitting, fine-tuning, and adaptation

Apply supported LoRA/adapters to selected projections or fine-tune full weights with response-masked supervised loss. Verify the actual attention backend and checkpoint configuration. Tune learning rate and context data on validation tasks, including distant-evidence questions and general-capability retention.

How to use and calibrate

Measure task quality, long-distance retrieval, cache memory, throughput, and behavior across input lengths. 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

Run a compact language assistant with constrained key/value-cache memory.

Interpretation / pitfalls

Watch out

Backend differences can affect whether a particular checkpoint actually uses the intended window behavior. 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 a supported Transformers checkpoint and documented attention backend; R bridges it with reticulate. The native example constructs a causal sliding-window mask to expose the architectural constraint, not full model weights.

Example scope: Causal sliding-window attention mask illustrating Mistral's local-attention idea.

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.

Python

Component demo, not a trained model — Causal sliding-window attention mask illustrating Mistral's local-attention idea.

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)
n, window = 6, 3
mask = [[int(max(0,i-window+1) <= j <= i) for j in range(n)] for i in range(n)]
print(mask)
R

Component demo, not a trained model — Causal sliding-window attention mask illustrating Mistral's local-attention idea.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
n <- 6L; window <- 3L
mask <- outer(0:(n-1), 0:(n-1), function(i,j) as.integer(j <= i & j >= pmax(0,i-window+1)))
print(mask)

Sources

From the CodeSplash reference, entry LNG036.