Skip to main content

Reusable Manim components for creating LLM and Transformer explanation videos.

Project description

LLManim

Reusable Manim components for creating LLM and Transformer explanation videos.

PyPI version Python CI License: MIT

A Python library of pre-built visual components, animations, and utilities for creating educational videos about Large Language Models and Transformer architectures using Manim.

Installation · Quick Start · Components · Examples · Contributing


Installation

pip install llmanim

Prerequisites

Manim requires a few system dependencies before you install:

# macOS
brew install cairo pango ffmpeg
brew install --cask mactex          # LaTeX for MathTex

# Ubuntu / Debian
sudo apt install libcairo2-dev libpango1.0-dev ffmpeg texlive-full

# Windows — install MiKTeX from https://miktex.org, then:
# pip install manim

Full Manim installation guide: docs.manim.community

Install from source

git clone https://github.com/rishabhbhartiya/LLManim.git
cd LLManim
pip install -e ".[dev]"

Quick Start

from manim import *
from llmanim.base.shapes import MatrixBox, VectorBar, TokenBox
from llmanim.base.animations import highlight_sequence, data_flow_arrow
from llmanim.base.utils import apply_dark_theme, make_attention_weights
from llmanim.tokenization.token_box import TokenRow
from llmanim.styles.colors import TOKEN_COLOR, HIGHLIGHT_COLOR

class MyTransformerScene(Scene):
    def construct(self):
        apply_dark_theme(self)

        # 1. Show a token sequence
        row = TokenRow(["the", "cat", "sat"], token_ids=[1, 2, 3])
        self.play(row.appear())

        # 2. Show an embedding matrix
        mat = MatrixBox(rows=4, cols=6, color=TOKEN_COLOR).shift(DOWN * 2)
        self.play(mat.fill_anim())

        # 3. Connect them with an animated arrow
        arrow, anim = data_flow_arrow(row, mat, label="embed")
        self.add(arrow)
        self.play(anim)

        # 4. Highlight components
        self.play(highlight_sequence(row.boxes))
        self.play(mat.highlight_row(2))
        self.wait(1)

Render it:

manim -pql my_scene.py MyTransformerScene   # fast preview
manim -pqh my_scene.py MyTransformerScene   # 1080p final

Example Videos

Five complete video scripts are included in examples/:

# File Scene Topic
1 01_what_is_a_token.py WhatIsAToken Tokenization basics
2 02_token_embeddings.py TokenEmbeddings Embedding lookup
3 03_attention_mechanism.py AttentionMechanism Q, K, V & softmax
4 04_multi_head_attention.py MultiHeadAttention Multiple attention heads
5 05_full_forward_pass.py FullForwardPass Complete forward pass

Video 1 — What is a Token?

What is a Token?

Video 2 — Token Embeddings

Token Embeddings

Video 3 — Attention Mechanism

Attention Mechanism

Video 4 — Multi-Head Attention

Multi-Head Attention

Video 5 — Full Forward Pass

Full Transformer Forward Pass
# Clone to access examples (they're not installed with pip)
git clone https://github.com/rishabhbhartiya/LLManim.git
cd LLManim

manim -pql examples/01_what_is_a_token.py WhatIsAToken     # preview
manim -pqh examples/05_full_forward_pass.py FullForwardPass # 1080p
manim -pqk examples/05_full_forward_pass.py FullForwardPass # 4K

Components

Base Shapes — llmanim.base.shapes

Class Description Key Methods
MatrixBox(rows, cols) Colored grid for weight matrices .highlight_row(r), .highlight_col(c), .fill_anim()
VectorBar(dim, values) Colored strip for embedding vectors .pulse(), .highlight_dim(i), .add_value_labels()
TokenBox(text, token_id) Rounded rectangle for a single token .highlight(), .shake(), .pop_in(), .pop_out()
LabeledBlock(label) Named rectangular block .highlight(), .expand_to(mob)
SoftmaxCurve(logits) Probability bar chart .set_temperature(T), .show_values()
MathLabel(latex) LaTeX equation label .write(), .unwrite()
ConnectionLine(srcs, tgts) Fully connected layer lines .draw(), .activate(i,j), .pulse_all()

Animations — llmanim.base.animations

Function Description
flow_through(mob, path) Animate an object along a path
matrix_multiply_anim(A, B, C, scene) Step-by-step matrix multiplication
highlight_sequence(boxes) Flash boxes one by one
attention_flow(tokens, weights) Weighted attention arrows between tokens
data_flow_arrow(A, B, label) Animated labeled arrow between mobjects
equation_reveal(terms, scene) Build an equation term by term
pulse_glow(mob) Expanding glow effect
zoom_into(mob, scene) Zoom in and back out
forward_pass_pulse(blocks) Light pulse travelling through layers

Utilities — llmanim.base.utils

Function Description
softmax(x, temperature) Numerically stable softmax
scaled_dot_product(Q, K, V, mask) Full attention computation
causal_mask(seq_len) Upper-triangular causal mask
sinusoidal_pe(seq_len, d_model) Sinusoidal positional encoding
make_attention_weights(seq_len, n_heads) Generate demo attention weights
make_token_sequence(words) Create a demo token sequence
value_to_color(v) Map a float to a Manim color
attention_palette(weights) Color palette from attention weights
apply_dark_theme(scene) Set dark background on a Scene

Tokenization — llmanim.tokenization.token_box

Class Description
TokenRow(tokens, token_ids) A horizontal row of token boxes
TokenBox(text, token_id) Individual token visualization
SpecialTokenBox(text) Special tokens like [CLS], [SEP]
BPESequence Byte-pair encoding visualization
VocabTable Vocabulary lookup table
ContextWindowBar Context window capacity indicator

Color System

All colors are in llmanim.styles.colors:

Constant Hex Used for
TOKEN_COLOR #4CAF50 Tokens, raw input
EMBEDDING_COLOR #2196F3 Embeddings, weight matrices
ATTENTION_COLOR #9C27B0 Attention mechanisms
FFN_COLOR #FF9800 Feed-forward networks
NORM_COLOR #00BCD4 Layer normalization
OUTPUT_COLOR #F44336 Output logits
QUERY_COLOR #E91E63 Query (Q) vectors
KEY_COLOR #3F51B5 Key (K) vectors
VALUE_COLOR #009688 Value (V) vectors
HIGHLIGHT_COLOR #FFEB3B Highlights, focus
DIM_COLOR #555555 Dimmed / background elements

Project Structure

LLManim/
├── llmanim/           # Installed package (import as llmanim)
│   ├── attention/               # QKV projection, scores, output, multi-head
│   ├── base/                    # shapes.py, animations.py, utils.py
│   ├── blocks/                  # transformer_block.py
│   ├── embeddings/              # embedding_lookup.py, positional_encoding.py
│   ├── feedforward/             # ffn.py
│   ├── normalization/           # layer_norm.py
│   ├── output/                  # logits_sampling.py
│   ├── styles/                  # colors.py, constants.py, fonts.py
│   ├── tokenization/            # token_box.py
│   └── full_model.py
├── examples/                    # Standalone video scripts (not installed)
├── videos/                      # Output GIFs for all example scenes
└── tests/

Video Production Workflow

  1. Plan — write your script outline and narration
  2. Import — pick components from llmanim
  3. Sketch — plan scene layout on paper
  4. Code — build scenes incrementally, preview with -pql
  5. Render — final export with -pqh or -pqk
  6. Voiceover — record audio to match animation timing
  7. Edit — combine in your video editor of choice

Contributing

Contributions are welcome — new components, bug fixes, example videos, or docs improvements. See CONTRIBUTING.md to get started.


License

MIT © Rishabh Bhartiya


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llmanim-0.1.0.tar.gz (154.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llmanim-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file llmanim-0.1.0.tar.gz.

File metadata

  • Download URL: llmanim-0.1.0.tar.gz
  • Upload date:
  • Size: 154.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for llmanim-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5bdd21f477c5bd7d36b99c5b0cf5e7a9a985b3b354fa4dd87b7faf3830ef96d8
MD5 211935c5a330003d812b151f8adab3af
BLAKE2b-256 cf0a3f5139a37d452ec706d98852946b64019fa4429ef33313dd38788d281c43

See more details on using hashes here.

File details

Details for the file llmanim-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: llmanim-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for llmanim-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c5824492d0e88e3c917323390df68838744ec8fa9e36a825f2e8c1d7256324e
MD5 ef2acf7a33f039dfe973ec5e4fbd6f75
BLAKE2b-256 8657918458411beb1793c340c0248c75868d4a67fd7fc93d4b703ac95f276812

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page