{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.rcParams[\"font.family\"] = \"Times New Roman\"\n",
    "plt.rcParams[\"font.size\"] = 16\n",
    "\n",
    "g_label_fontsize = 16\n",
    "\n",
    "colors = [\n",
    "    \"#5D7599\",\n",
    "    \"#233142\",\n",
    "    \"#F95959\",\n",
    "]\n",
    "\n",
    "edgecolors = [\n",
    "    \"#FFFFFF\",\n",
    "    \"#FFFFFF\",\n",
    "    \"#FFFFFF\",\n",
    "]\n",
    "\n",
    "hatches = [\"\\\\\\\\\", \"\", \"\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 49480 characters]",
      "text/plain": [
       "<Figure size 2100x700 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(\n",
    "    figsize=(7, 14 / 6), ncols=1, nrows=1, constrained_layout=True, dpi=300\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_name_a = [\n",
    "    \"(A,8,-,-)\",\n",
    "    \"(A,-,8,-)\",\n",
    "    \"(B,-,8,-)\",\n",
    "    \"(A,2,4,-)\",\n",
    "    \"(A,2,8,-)\",\n",
    "    \"(A,4,2,-)\",\n",
    "    \"(B,4,2,-)\",\n",
    "    \"(C,1,8,8)\",\n",
    "    \"(C,2,8,8)\",\n",
    "    \"(D,1,8,8)\",\n",
    "    \"(D,2,8,8)\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# peak memory\n",
    "data_a = {\n",
    "    labels_name_a[0]: [79, 40.95, 50.12],  # modela 8DP\n",
    "    labels_name_a[1]: [47, 44.24, 41.74],  # modela 8TP\n",
    "    labels_name_a[2]: [65.66, 59.47, 69.75],  # modelb 8TP\n",
    "    labels_name_a[3]: [50, 40.94, 54.22],  # modela 2DP 4TP\n",
    "    labels_name_a[4]: [76, 64.92, 71.72],  # modela 2DP 8TP\n",
    "    labels_name_a[5]: [60, 40.97, 55.75],  # modela 4DP 2TP\n",
    "    labels_name_a[6]: [-1, 59.32, 64.60],  # modelb 4DP 2TP\n",
    "    labels_name_a[7]: [70, 1, 1],  # modelc 1DP 8TP 8EP\n",
    "    labels_name_a[8]: [70, 41.57, 42.32],  # modelc 2DP 8TP 8EP\n",
    "    labels_name_a[9]: [80, 74.85, 76.00],  # modeld 1DP 8TP 8EP\n",
    "    labels_name_a[10]: [80, 73.24, 73.96],  # modeld 2DP 8TP 8EP\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Megatron-LM\", \"XLA\", \"Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar_width = 0.2\n",
    "group_spaing = 0.15\n",
    "\n",
    "group_positions = {}\n",
    "current_pos = 0\n",
    "\n",
    "x_label_cnt = len(data_a.keys())\n",
    "x_label_group_cnt = 3\n",
    "\n",
    "for idx in range(x_label_cnt):\n",
    "    group_positions[idx] = []\n",
    "    for _ in range(x_label_group_cnt):\n",
    "        group_positions[idx].append(current_pos)\n",
    "        current_pos += bar_width\n",
    "    current_pos += group_spaing\n",
    "\n",
    "group_centers = {}\n",
    "for idx, positions in group_positions.items():\n",
    "    group_centers[idx] = sum(positions) / len(positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 13.333333333333332, '(b) # (model, DP, TP, EP)')"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set = set()\n",
    "for idx, (x_label, y_data) in enumerate(data_a.items()):\n",
    "    positions = group_positions[idx]\n",
    "    for i, (pos, value, color, edgecolor, hatch, label) in enumerate(\n",
    "        zip(\n",
    "            positions,\n",
    "            y_data,\n",
    "            colors,\n",
    "            edgecolors,\n",
    "            hatches,\n",
    "            legend_labels,\n",
    "        )\n",
    "    ):\n",
    "        if label in label_set:\n",
    "            local_label = None\n",
    "        else:\n",
    "            local_label = label\n",
    "            label_set.add(local_label)\n",
    "        if value == -1:\n",
    "            ax.text(\n",
    "                pos,\n",
    "                1,\n",
    "                \"$\\\\times$OOM\",\n",
    "                rotation=90,\n",
    "                ha=\"center\",\n",
    "                va=\"bottom\",\n",
    "                color=\"red\",\n",
    "            )\n",
    "        ax.bar(\n",
    "            pos,\n",
    "            value,\n",
    "            width=bar_width,\n",
    "            color=color,\n",
    "            label=local_label,\n",
    "            hatch=hatch,\n",
    "            edgecolor=edgecolor,\n",
    "        )\n",
    "\n",
    "ax.set_xticks(list(group_centers.values()))\n",
    "ax.set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax.set_ylim(0, 95)\n",
    "ax.set_yticks([0, 40, 80])\n",
    "ax.set_yticklabels([\"0\", \"40\", \"80\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax.tick_params(\n",
    "    axis=\"x\", bottom=False, labelsize=g_label_fontsize - 2, pad=1, rotation=-20\n",
    ")\n",
    "ax.tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax.set_ylabel(\"Peak Memory (GB)\", fontsize=g_label_fontsize)\n",
    "ax.set_xlabel(\"(b) # (model, DP, TP, EP)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7fbd0a4172c0>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.legend(\n",
    "    ncol=3,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    fontsize=g_label_fontsize - 2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 181748 characters]",
      "text/plain": [
       "<Figure size 2100x700 with 1 Axes>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.savefig(\"end_to_end_mem.pdf\", bbox_inches=\"tight\", dpi=1000)\n",
    "fig"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
