{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "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": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 191996 characters]",
      "text/plain": [
       "<Figure size 2100x3230.77 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(\n",
    "    figsize=(7, 14 / 1.3), ncols=1, nrows=4, constrained_layout=True, dpi=300\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_name = [\n",
    "    \"ModelA\\n8DP\",\n",
    "    \"ModelA\\n8TP\",\n",
    "    \"ModelB\\n8TP\",\n",
    "    \"ModelC\\n8DP\",\n",
    "    \"ModelC\\n8TP\",\n",
    "    \"ModelD\\n8TP\",\n",
    "    \"ModelA\\n16DP\",\n",
    "    \"ModelA\\n16TP\",\n",
    "    \"ModelB\\n16TP\",\n",
    "    \"ModelC\\n16DP\",\n",
    "    \"ModelC\\n16TP\",\n",
    "    \"ModelD\\n16TP\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MFU\n",
    "data_a = {\n",
    "    labels_name[0]: [41, 34, 44],  # 22.64\n",
    "    labels_name[1]: [26, 36, 43],  # 22.64\n",
    "    labels_name[2]: [30, 39, 49],  # 37\n",
    "    labels_name[3]: [0, 0, 0],  # 37\n",
    "    labels_name[4]: [0, 0, 0],  # 22.64\n",
    "    labels_name[5]: [0, 0, 0],  # 22.64\n",
    "    labels_name[6]: [0, 0, 0],  # 37\n",
    "    labels_name[7]: [0, 0, 0],  # 22.64\n",
    "    labels_name[8]: [0, 0, 0],  # 22.64\n",
    "    labels_name[9]: [0, 0, 0],  # 37\n",
    "    labels_name[10]: [0, 0, 0],  # 22.64\n",
    "    labels_name[11]: [0, 0, 0],  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# peak memory\n",
    "data_d = {\n",
    "    labels_name[0]: [79, 41, 48],\n",
    "    labels_name[1]: [47, 44, 42],\n",
    "    labels_name[2]: [66, 60, 69],\n",
    "    labels_name[3]: [0, 0, 0],\n",
    "    labels_name[4]: [0, 0, 0],\n",
    "    labels_name[5]: [0, 0, 0],\n",
    "    labels_name[6]: [0, 0, 0],  # 37\n",
    "    labels_name[7]: [0, 0, 0],  # 22.64\n",
    "    labels_name[8]: [0, 0, 0],  # 22.64\n",
    "    labels_name[9]: [0, 0, 0],  # 37\n",
    "    labels_name[10]: [0, 0, 0],  # 22.64\n",
    "    labels_name[11]: [0, 0, 0],  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# overlapping rate\n",
    "data_b = {\n",
    "    labels_name[0]: [0, 0, 0],\n",
    "    labels_name[1]: [0, 0, 0],\n",
    "    labels_name[2]: [0, 0, 0],\n",
    "    labels_name[3]: [0, 0, 0],\n",
    "    labels_name[4]: [0, 0, 0],\n",
    "    labels_name[5]: [0, 0, 0],\n",
    "    labels_name[6]: [0, 0, 0],  # 37\n",
    "    labels_name[7]: [0, 0, 0],  # 22.64\n",
    "    labels_name[8]: [0, 0, 0],  # 22.64\n",
    "    labels_name[9]: [0, 0, 0],  # 37\n",
    "    labels_name[10]: [0, 0, 0],  # 22.64\n",
    "    labels_name[11]: [0, 0, 0],  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# communication baseline\n",
    "data_c = {\n",
    "    labels_name[0]: 70,\n",
    "    labels_name[1]: 70,\n",
    "    labels_name[2]: 70,\n",
    "    labels_name[3]: 70,\n",
    "    labels_name[4]: 70,\n",
    "    labels_name[5]: 70,\n",
    "    labels_name[6]: [0, 0, 0],  # 37\n",
    "    labels_name[7]: [0, 0, 0],  # 22.64\n",
    "    labels_name[8]: [0, 0, 0],  # 22.64\n",
    "    labels_name[9]: [0, 0, 0],  # 37\n",
    "    labels_name[10]: [0, 0, 0],  # 22.64\n",
    "    labels_name[11]: [0, 0, 0],  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# compilation time\n",
    "data_e = {\n",
    "    labels_name[0]: [1, 10, 147],\n",
    "    labels_name[1]: [0, 10, 133],\n",
    "    labels_name[2]: [0, 0, 0],\n",
    "    labels_name[3]: [0, 0, 0],\n",
    "    labels_name[4]: [0, 0, 0],\n",
    "    labels_name[5]: [0, 0, 0],\n",
    "    labels_name[6]: [0, 0, 0],  # 37\n",
    "    labels_name[7]: [0, 0, 0],  # 22.64\n",
    "    labels_name[8]: [0, 0, 0],  # 22.64\n",
    "    labels_name[9]: [0, 0, 0],  # 37\n",
    "    labels_name[10]: [0, 0, 0],  # 22.64\n",
    "    labels_name[11]: [0, 0, 0],  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Megatron-LM\", \"XLA\", \"DLRover-Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "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": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(a)')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set = set()\n",
    "for x_label, y_data in data_a.items():\n",
    "    positions = group_positions[x_label]\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",
    "        ax[0].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[0].set_xticks(list(group_centers.values()))\n",
    "ax[0].set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax[0].set_ylim(0, 100)\n",
    "ax[0].set_yticks([0, 50, 100])\n",
    "ax[0].set_yticklabels([\"0\", \"50\", \"100\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[0].tick_params(axis=\"x\", bottom=False, labelsize=8, pad=1)\n",
    "ax[0].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[0].set_ylabel(\"MFU (%)\", fontsize=g_label_fontsize)\n",
    "ax[0].set_title(\"(a)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(b)')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for x_label, y_data in data_d.items():\n",
    "    positions = group_positions[x_label]\n",
    "    for i, (pos, value, color, edgecolor, hatch) in enumerate(\n",
    "        zip(\n",
    "            positions,\n",
    "            y_data,\n",
    "            colors,\n",
    "            edgecolors,\n",
    "            hatches,\n",
    "        )\n",
    "    ):\n",
    "        ax[1].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[1].set_xticks(list(group_centers.values()))\n",
    "ax[1].set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax[1].set_ylim(0, 90)\n",
    "ax[1].set_yticks([0, 40, 80])\n",
    "ax[1].set_yticklabels([\"0\", \"40\", \"80\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[1].tick_params(axis=\"x\", bottom=False, labelsize=8, pad=1)\n",
    "ax[1].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[1].set_ylabel(\"Peak Memory (GB)\", fontsize=g_label_fontsize)\n",
    "ax[1].set_title(\"(b)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(c)')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set = set()\n",
    "total_label = \"Non-Overlap Communication\"\n",
    "\n",
    "for x_label, y_data in data_b.items():\n",
    "    positions = group_positions[x_label]\n",
    "    for i, (pos, value, diff_value, color, edgecolor, label, hatch) in enumerate(\n",
    "        zip(\n",
    "            positions,\n",
    "            y_data,\n",
    "            list(data_c.values()),\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[2].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",
    "        ax[2].bar(\n",
    "            pos,\n",
    "            diff_value - value,\n",
    "            bottom=value,\n",
    "            color=\"#E3E3E3\",\n",
    "            edgecolor=edgecolor,\n",
    "            width=bar_width,\n",
    "            label=total_label,\n",
    "        )\n",
    "        if total_label is not None:\n",
    "            total_label = None\n",
    "\n",
    "ax[2].set_xticks(list(group_centers.values()))\n",
    "ax[2].set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax[2].set_ylim(0, 100)\n",
    "ax[2].set_yticks([0, 50, 100])\n",
    "ax[2].set_yticklabels([\"0\", \"50\", \"100\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[2].tick_params(axis=\"x\", bottom=False, labelsize=8, pad=1)\n",
    "ax[2].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[2].set_ylabel(\"Comm-Comp Overlap Ratio (%)\", fontsize=g_label_fontsize)\n",
    "ax[2].set_title(\"(c)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(d)')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for x_label, y_data in data_e.items():\n",
    "    positions = group_positions[x_label]\n",
    "    for i, (pos, value, color, edgecolor, hatch) in enumerate(\n",
    "        zip(\n",
    "            positions,\n",
    "            y_data,\n",
    "            colors,\n",
    "            edgecolors,\n",
    "            hatches,\n",
    "        )\n",
    "    ):\n",
    "        ax[3].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[3].set_xticks(list(group_centers.values()))\n",
    "ax[3].set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax[3].set_ylim(0, 210)\n",
    "ax[3].set_yticks([0, 50, 100, 150, 200])\n",
    "ax[3].set_yticklabels([\"0\", \"50\", \"100\", \"150\", \"200\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[3].tick_params(axis=\"x\", bottom=False, labelsize=8, pad=1)\n",
    "ax[3].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[3].set_ylabel(\"Compile Time (s)\", fontsize=g_label_fontsize)\n",
    "ax[3].set_title(\"(d)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7fa20a998b00>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ax[0].legend(\n",
    "    ncol=3,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    fontsize=g_label_fontsize,\n",
    ")\n",
    "\n",
    "ax[2].legend(\n",
    "    ncol=1,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    fontsize=g_label_fontsize,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 363532 characters]",
      "text/plain": [
       "<Figure size 2100x3230.77 with 4 Axes>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.savefig(\"end_to_end_single.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
}
