File size: 1,833 Bytes
aa7bfed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ed3b70
 
aa7bfed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
title: RoPE Embedding Visualization
emoji: ๐Ÿ“š
colorFrom: gray
colorTo: green
sdk: gradio
sdk_version: 6.26.0
python_version: 3.11
app_file: app.py
pinned: false
short_description: Visualize how RoPE rotates query and key vectors
---

# RoPE Explorer

Interactive Gradio app for **Rotary Position Embedding**. The Hugging Face Space
runs `app.py` (`sdk: gradio`). Local Docker (`Dockerfile`, `compose.yaml`) is for
running `python app.py` on port **7860**.

Production math lives in [`src/`](src/) (`rope.py`, `absolute_pe.py`, `extract.py`,
`plots.py`). Scratch scripts under `rope_implementation/`,
`absolute_sinusoidal_position_embedding/`, and `relative_pos_embedding/` are
learning notes only and are **not** imported by the app.

## Modes

1. **Random matrix** โ€” sample even-width Q (and K) tensors, apply numpy RoPE,
   inspect heatmaps, pairwise 2D rotation, `QK^T`, and additive sinusoidal PE.
2. **Real model** โ€” lazy-load an ungated Llama-like checkpoint (default
   `HuggingFaceTB/SmolLM2-135M`; `HuggingFaceM4/tiny-random-LlamaForCausalLM`
   is included for a very small test model), take `embed_tokens`, first-layer `q_proj` /
   `k_proj` (GQA-aware), and compare educational numpy RoPE (`llama` pairing)
   to the model's `rotary_emb`. No Hugging Face token is required. Gated models
   are not used.

First load of a model downloads weights into the cache; later runs reuse the
last loaded model in memory.

CPU is enough for SmolLM2 and Qwen2.5-0.5B. TinyLlama is included for a larger
example and may be slow on CPU. This Space does **not** require ZeroGPU
(`@spaces.GPU` is unused).

## Local run

```bash
pip install -r requirements.txt
python app.py
```

Or `docker compose up`. Hugging Face Cloud uses the README YAML (`sdk: gradio`),
not the Docker image, unless the Space SDK is switched to Docker.