{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'plt' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m fig, ax \u001b[38;5;241m=\u001b[39m \u001b[43mplt\u001b[49m\u001b[38;5;241m.\u001b[39msubplots(\n\u001b[1;32m      2\u001b[0m     figsize\u001b[38;5;241m=\u001b[39m(\u001b[38;5;241m7\u001b[39m, \u001b[38;5;241m14\u001b[39m \u001b[38;5;241m/\u001b[39m \u001b[38;5;241m5\u001b[39m), ncols\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m, nrows\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m, constrained_layout\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, dpi\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m300\u001b[39m\n\u001b[1;32m      3\u001b[0m )\n",
      "\u001b[0;31mNameError\u001b[0m: name 'plt' is not defined"
     ]
    }
   ],
   "source": [
    "fig, ax = plt.subplots(\n",
    "    figsize=(7, 14 / 5), ncols=1, nrows=1, constrained_layout=True, dpi=300\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_name = [\n",
    "    \"ModelA\\n\" + r\"2DP$\\times $4TP\",\n",
    "    \"ModelA\\n\" + r\"2DP$\\times $8TP\",\n",
    "    \"ModelB\\n\" + r\"2DP$\\times $4TP\",\n",
    "    \"ModelB\\n\" + r\"2DP$\\times $8TP\",\n",
    "    \"ModelC\\n\" + r\"2DP$\\times $4TP\",\n",
    "    \"ModelC\\n\" + r\"2DP$\\times $8TP\",\n",
    "    \"ModelD\\n\" + r\"2DP$\\times $4TP\",\n",
    "    \"ModelD\\n\" + r\"2DP$\\times $8TP\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# memory cost\n",
    "data_a = {\n",
    "    labels_name[0]: [0, 53, 1200],  # 22.64\n",
    "    labels_name[1]: [0, 53, 1200],  # 22.64\n",
    "    labels_name[2]: [0, 35, 1200],  # 37\n",
    "    labels_name[3]: [0, 35, 1200],  # 37\n",
    "    labels_name[4]: [0, 53, 1200],  # 22.64\n",
    "    labels_name[5]: [0, 53, 1200],  # 22.64\n",
    "    labels_name[6]: [0, 53, 1200],  # 22.64\n",
    "    labels_name[7]: [0, 53, 1200],  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Megatron-LM\", \"XLA\", \"DLRover-Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bar_width = 0.2\n",
    "group_spaing = 0.15\n",
    "\n",
    "group_positions = {}\n",
    "current_pos = 0\n",
    "\n",
    "for x_label, y_data in data_a.items():\n",
    "    group_positions[x_label] = []\n",
    "    for i in range(len(y_data)):\n",
    "        group_positions[x_label].append(current_pos)\n",
    "        current_pos += bar_width\n",
    "    current_pos += group_spaing\n",
    "\n",
    "group_centers = {}\n",
    "for x_label, positions in group_positions.items():\n",
    "    group_centers[x_label] = sum(positions) / len(positions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(b)')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set = set()\n",
    "\n",
    "for x_label, y_data in data_a.items():\n",
    "    positions = group_positions[x_label]\n",
    "    for i, (pos, value, color, edgecolor, label, hatch) in enumerate(\n",
    "        zip(\n",
    "            positions,\n",
    "            y_data,\n",
    "            colors,\n",
    "            edgecolors,\n",
    "            legend_labels,\n",
    "            hatches,\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",
    "\n",
    "        ax.bar(\n",
    "            pos,\n",
    "            value,\n",
    "            width=bar_width,\n",
    "            color=color,\n",
    "            edgecolor=edgecolor,\n",
    "            hatch=hatch,\n",
    "            label=local_label,\n",
    "        )\n",
    "\n",
    "\n",
    "ax.set_xticks(list(group_centers.values()))\n",
    "ax.set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax.set_ylim(0, 1500)\n",
    "ax.set_yticks([0, 500, 1000, 1500])\n",
    "ax.set_yticklabels([\"0\", \"500\", \"1000\", \"1500\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax.tick_params(axis=\"x\", bottom=False, labelsize=11, pad=1)\n",
    "ax.tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax.set_ylabel(\"Time (s)\", fontsize=g_label_fontsize)\n",
    "ax.set_title(\"(b)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f63985869c0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax.legend(\n",
    "    ncol=3,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    fontsize=g_label_fontsize,\n",
    "    bbox_to_anchor=(0.5, 1)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 114000 characters]",
      "text/plain": [
       "<Figure size 2100x840 with 1 Axes>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.savefig(\"compile_time_hybrid.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
}
