"""Plot variance analysis results for SmolLM (Math), Qwen3 (Math), Maze.""" import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import numpy as np import os SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) OUTPUT_DIR = os.path.join(SCRIPT_DIR, "outputs") os.makedirs(OUTPUT_DIR, exist_ok=True) # ── Data ────────────────────────────────────────────────────────────────────── rollout_nums = [4, 8, 16, 32, 64, 128] smollm_math = { "blTrue": [3.461569e-01, 3.488942e-01, 2.763410e-01, 2.600237e-01, 2.039304e-01, 1.596079e-01], "blFalse": [5.613685e-01, 4.680250e-01, 3.457040e-01, 2.965937e-01, 2.275286e-01, 1.723276e-01], } qwen3_math = { "blTrue": [1.544762e-01, 1.679264e-01, 2.075920e-01, 1.788574e-01, 1.592376e-01, 1.314381e-01], "blFalse": [2.140592e-01, 2.190761e-01, 2.448343e-01, 2.002312e-01, 1.702958e-01, 1.390878e-01], } maze = { "blTrue": [9.343412e-02, 7.531368e-02, 5.179188e-02, 4.027390e-02, 2.930888e-02, 2.354821e-02], "blFalse": [1.171640e-01, 8.123477e-02, 5.614680e-02, 4.236686e-02, 3.015591e-02, 2.521931e-02], } datasets = [ ("SmolLM-360M (GSM8k)", smollm_math), ("Qwen3-1.7B (Polaris-53K)", qwen3_math), ("Qwen2-3M (Maze)", maze), ] # ── Style ───────────────────────────────────────────────────────────────────── plt.rcParams.update({ "font.size": 15, "axes.titlesize": 18, "axes.labelsize": 16, "legend.fontsize": 14, "xtick.labelsize": 14, "ytick.labelsize": 14, "figure.dpi": 150, "savefig.dpi": 300, "font.family": "sans-serif", }) RED = "#D32F2F" AMBER = "#F9A825" # ── Plot ────────────────────────────────────────────────────────────────────── fig, axes = plt.subplots(1, 3, figsize=(21, 6)) for ax, (title, data) in zip(axes, datasets): xs = np.array(rollout_nums) bl_true = np.array([v if v is not None else np.nan for v in data["blTrue"]]) bl_false = np.array([v if v is not None else np.nan for v in data["blFalse"]]) ax.plot(xs, bl_true, color=RED, marker="*", markersize=18, linewidth=4, label="MaxRL", zorder=5) ax.plot(xs, bl_false, color=AMBER, marker="o", markersize=11, linewidth=4, label="MaxRL (w/o baseline)", zorder=4) ax.set_xscale("log", base=2) ax.set_xticks(rollout_nums) ax.set_xticklabels([str(n) for n in rollout_nums]) ax.set_xlabel("Number of Rollouts", fontsize=16) ax.set_ylabel("Gradient Variance", fontsize=16) ax.set_title(title, fontsize=18, fontweight="bold", pad=12) ax.legend(loc="upper right", framealpha=0.9, edgecolor="gray") ax.grid(True, alpha=0.3, linestyle="--") ax.spines["top"].set_visible(False) ax.spines["right"].set_visible(False) plt.tight_layout(w_pad=3) out_path = os.path.join(OUTPUT_DIR, "variance_comparison_all.png") plt.savefig(out_path, bbox_inches="tight") plt.savefig(out_path.replace(".png", ".pdf"), bbox_inches="tight") print(f"Saved to {out_path}")