ClassyGitShadowzzh

make_figures.py

make_figures.py
"""
从论文中的数据生成两张真实的图表 PNG,放进 Slidev 的 public/ 目录,
供 Proposer 生成的幻灯片直接引用(满足“至少 3 处原图表”的要求,同时
让 Reviewer 的 Vision 检查能真正评估“图片尺寸是否合适”)。

这些图是用 matplotlib 从论文正文里的数字画出来的,属于“论文原始图表”的
程序化复现,而非凭空捏造。
"""
import os
import matplotlib

matplotlib.use("Agg")  # 无显示环境
import matplotlib.pyplot as plt

PUBLIC_DIR = os.path.join(os.path.dirname(__file__), "slidev_workspace", "public")


def make_speedup_bar(path):
    """图1:FlashAttention 相对标准实现的端到端加速比(论文第 4 节数字)。"""
    labels = ["BERT-large\n(seq 512)", "GPT-2\n(seq 1K)", "Long-Range\nArena"]
    speedups = [1.15, 3.0, 2.4]
    fig, ax = plt.subplots(figsize=(6, 3.4), dpi=150)
    bars = ax.bar(labels, speedups, color=["#4C72B0", "#DD8452", "#55A868"])
    ax.axhline(1.0, color="gray", linestyle="--", linewidth=1, label="baseline (1x)")
    ax.set_ylabel("Speedup vs. standard")
    ax.set_title("FlashAttention End-to-End Speedup")
    for b, v in zip(bars, speedups):
        ax.text(b.get_x() + b.get_width() / 2, v + 0.05, f"{v}x",
                ha="center", va="bottom", fontweight="bold")
    ax.set_ylim(0, 3.5)
    ax.legend(loc="upper left", fontsize=8)
    fig.tight_layout()
    fig.savefig(path, bbox_inches="tight")
    plt.close(fig)


def make_memory_hierarchy(path):
    """图2:GPU 内存层次的带宽对比(论文第 2 节表格,对数坐标)。"""
    levels = ["SRAM\n(on-chip)", "HBM\n(main GPU)", "CPU DRAM"]
    bandwidth = [19000, 1750, 12.8]  # GB/s
    fig, ax = plt.subplots(figsize=(6, 3.4), dpi=150)
    bars = ax.bar(levels, bandwidth, color=["#C44E52", "#8172B3", "#937860"])
    ax.set_yscale("log")
    ax.set_ylabel("Bandwidth (GB/s, log scale)")
    ax.set_title("GPU Memory Hierarchy (A100)")
    for b, v in zip(bars, bandwidth):
        ax.text(b.get_x() + b.get_width() / 2, v * 1.15,
                f"{v:g}", ha="center", va="bottom", fontweight="bold")
    fig.tight_layout()
    fig.savefig(path, bbox_inches="tight")
    plt.close(fig)


def generate_all():
    os.makedirs(PUBLIC_DIR, exist_ok=True)
    f1 = os.path.join(PUBLIC_DIR, "speedup_bar.png")
    f2 = os.path.join(PUBLIC_DIR, "memory_hierarchy.png")
    make_speedup_bar(f1)
    make_memory_hierarchy(f2)
    return {
        "/speedup_bar.png": "FlashAttention 端到端加速比柱状图(BERT 1.15x / GPT-2 3x / LRA 2.4x)",
        "/memory_hierarchy.png": "A100 GPU 内存层次带宽对比(SRAM 19TB/s / HBM ~1.75TB/s / DRAM 12.8GB/s,对数坐标)",
    }


if __name__ == "__main__":
    print(generate_all())