Prompt
You are asked to write Python code using the Manim Community (v0.19.0) visualization library to produce a high-quality, intuitive, and mathematically precise visualization of the computation process of a single-head attention module (as used in transformers). The figure should reflect a clean, compact, and professional style.
Visualization Requirements (Detailed and Explicit)
1. Canvas:
- Set the background color to white.
2. Structure: You must clearly visualize the computation involving 4 token positions z1,z2,z3,z4z_1, z_2, z_3, z_4.
- Use rectangles for vectors, clearly labeled with large font size.
- Explicitly visualize three matrices: Q,K,VQ, K, V.
- Place these matrices clearly and vertically, right next to input tokens.
3. Input Tokens ziz_i:
- Align vertically, at the leftmost side, with labels z1,z2,z3,z4z_1, z_2, z_3, z_4.
- Use a professional, soft blue color for these rectangles (e.g., HEX:
#6C8EBF).
4. Queries, Keys, and Values:
- Generate keys vectors (kjk_j), horizontally at the top.
- Generate values vectors (vjv_j), horizontally at the bottom.
- Generate queries vectors (qiq_i), horizontally aligned next to each corresponding input token ziz_i.
- Clearly use consistent and distinct colors for each set:
- Queries (qiq_i): soft orange (e.g., HEX:
#FFB347) - Keys (kjk_j): soft green (e.g., HEX:
#77DD77) - Values (vjv_j): soft purple (e.g., HEX:
#A67FBF)
- Queries (qiq_i): soft orange (e.g., HEX:
- Explicitly write equations clearly next to each group:
- qi=Qziq_i = Q z_i, in a dark orange/brown color (
#D2691E). - kj=Kzjk_j = K z_j, in a dark green color (
#1E5631). - vj=Vzjv_j = V z_j, in a dark purple color (
#5D3FD3).
- qi=Qziq_i = Q z_i, in a dark orange/brown color (
5. Inner Product Matrix:
- Create a large, clearly labeled 4×44\times4 inner product matrix.
- Each cell is explicitly labeled with the inner product qi⊺kjq_i^\intercal k_j clearly using LaTeX.
- Place this matrix clearly to the right of the queries, centrally aligned vertically.
6. Softmax Visualization (Important):
- Clearly visualize the “softmax” operation row by row.
- When visualizing each row:
- Highlight the row with a distinct, subtle color (e.g., peach:
#FFDAB9). - Clearly write in dark red (
#B22222) large LaTeX text centered exactly inside each row: softmax⇒pi(⋅)\text{softmax} \Rightarrow p_i(\cdot)
- Highlight the row with a distinct, subtle color (e.g., peach:
- After this step, clearly visualize aggregation explicitly: zˉi=∑j=14pi(j)vj\bar{z}i = \sum{j=1}^4 p_i(j)v_j
- Place these resulting vectors (zˉi\bar z_i) clearly to the right side of each corresponding row of the matrix.
- Make sure they have the same soft blue color as the input tokens.
7. Aesthetics & Fonts:
- Clearly ensure each text element (vectors, matrices, equations, labels) is large (font size approximately 50 for main vectors/elements, 55 for “softmax” text, and 30-50 for equations) and clearly readable.
- Use professional, pleasant colors as specified explicitly above. Avoid overly bright colors.
- Keep the layout compact and neat. Avoid unnecessary whitespace.
8. Animation Flow (Explicit):
- Clearly animate in this order explicitly:
- Input vectors ziz_i appear first.
- Matrices Q,K,VQ, K, V appear simultaneously afterward.
- All keys (kjk_j) appear simultaneously with the equation kj=Kzjk_j = K z_j.
- All values (vjv_j) appear simultaneously with the equation vj=Vzjv_j = V z_j.
- Queries (qiq_i) appear simultaneously next to input tokens with the equation qi=Qziq_i = Q z_i.
- Then clearly animate the inner product matrix.
- Sequentially animate each row of the inner product matrix, clearly visualizing the “softmax ⇒ pi(⋅)p_i(\cdot)” inside the row, then show each aggregated vector (zˉi\bar z_i) clearly.
- Finally, fade out or leave the final visualization clearly displayed.
Technical Instructions for GPT-4:
- You must produce a complete, runnable Manim code snippet directly usable in a Jupyter notebook cell (with the magic command
%%manim -qm SingleHeadAttention). - Your final code must run without errors in Manim Community v0.19.0.
- Explicitly use the LaTeX transpose symbol
\intercalinstead of\top(due to Manim limitations). - Ensure proper LaTeX syntax with correctly escaped braces for Python f-strings (e.g., use
{{and}}for braces within f-strings). - Explicitly set all text color to black (
color=BLACK) against the white background unless specifically instructed otherwise. - The final visualization must look clean, professional, and educational.