{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "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": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 129396 characters]",
      "text/plain": [
       "<Figure size 4200x1400 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(\n",
    "    figsize=(14, 14 / 3), ncols=2, nrows=2, constrained_layout=True, dpi=300\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_name_a = [\n",
    "    \"ModelA\\n8DP\",\n",
    "    \"ModelA\\n8TP\",\n",
    "    \"ModelB\\n8TP\",\n",
    "    \"ModelC\\n8DP\",\n",
    "    \"ModelC\\n8TP\",\n",
    "    \"ModelD\\n8TP\",\n",
    "]\n",
    "labels_name_b = [\n",
    "    \"ModelA\\n16DP\",\n",
    "    \"ModelA\\n16TP\",\n",
    "    \"ModelB\\n16TP\",\n",
    "    \"ModelC\\n16DP\",\n",
    "    \"ModelC\\n16TP\",\n",
    "    \"ModelD\\n16TP\",\n",
    "]\n",
    "labels_name_c = [\n",
    "    \"ModelA\\n2DP$\\\\times$4TP\",\n",
    "    \"ModelA\\n4DP$\\\\times$2TP\",\n",
    "    \"ModelB\\n4DP$\\\\times$2TP\",\n",
    "    \"ModelC\\n2DP$\\\\times$4TP\",\n",
    "    \"ModelC\\n4DP$\\\\times$2TP\",\n",
    "    \"ModelD\\n2DP$\\\\times$4TP\",\n",
    "]\n",
    "labels_name_d = [\n",
    "    \"ModelA\\n15DP\",\n",
    "    \"ModelA\\n15TP\",\n",
    "    \"ModelB\\n15TP\",\n",
    "    \"ModelC\\n15DP\",\n",
    "    \"ModelC\\n15TP\",\n",
    "    \"ModelD\\n15TP\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# overlapp\n",
    "data_a = {\n",
    "    labels_name_a[0]: [36.1, 0, 38.3],  # 94.2,??,81\n",
    "    labels_name_a[1]: [16.9, 0, 46.7],  # 22.64\n",
    "    labels_name_a[2]: [18.7, 0, 0],  # 37\n",
    "    labels_name_a[3]: [0, 0, 0],  # 37\n",
    "    labels_name_a[4]: [0, 0, 0],  # 22.64\n",
    "    labels_name_a[5]: [0, 0, 0],  # 22.64\n",
    "}\n",
    "data_a_diff = {\n",
    "    labels_name_a[0]: 38.4,  # 22.64\n",
    "    labels_name_a[1]: 57.6,  # 22.64\n",
    "    labels_name_a[2]: 52.2,  # 37\n",
    "    labels_name_a[3]: 70,  # 37\n",
    "    labels_name_a[4]: 70,  # 22.64\n",
    "    labels_name_a[5]: 70,  # 22.64\n",
    "}\n",
    "\n",
    "data_b = {\n",
    "    labels_name_b[0]: [0, 0, 0],  # 37\n",
    "    labels_name_b[1]: [0, 0, 0],  # 22.64\n",
    "    labels_name_b[2]: [0, 0, 0],  # 22.64\n",
    "    labels_name_b[3]: [0, 0, 0],  # 37\n",
    "    labels_name_b[4]: [0, 0, 0],  # 22.64\n",
    "    labels_name_b[5]: [0, 0, 0],  # 22.64\n",
    "}\n",
    "data_b_diff = {\n",
    "    labels_name_b[0]: 70,  # 37\n",
    "    labels_name_b[1]: 70,  # 22.64\n",
    "    labels_name_b[2]: 70,  # 22.64\n",
    "    labels_name_b[3]: 70,  # 37\n",
    "    labels_name_b[4]: 70,  # 22.64\n",
    "    labels_name_b[5]: 70,  # 22.64\n",
    "}\n",
    "\n",
    "data_c = {\n",
    "    labels_name_c[0]: [20.1, 0, 0],  # 2DP 4TP\n",
    "    labels_name_c[1]: [17.4, 0, 0],  # 4DP 2TP\n",
    "    labels_name_c[2]: [-1, 0, 25.3],  #\n",
    "    labels_name_c[3]: [0, 0, 0],  # 37\n",
    "    labels_name_c[4]: [0, 0, 0],  # 22.64\n",
    "    labels_name_c[5]: [0, 0, 0],  # 22.64\n",
    "}\n",
    "data_c_diff = {\n",
    "    labels_name_c[0]: 46.8,  # 2DP 4TP\n",
    "    labels_name_c[1]: 43.4,  # 4DP 2TP\n",
    "    labels_name_c[2]: 53.9,  #\n",
    "    labels_name_c[3]: 70,  # 37\n",
    "    labels_name_c[4]: 70,  # 22.64\n",
    "    labels_name_c[5]: 70,  # 22.64\n",
    "}\n",
    "\n",
    "data_d = {\n",
    "    labels_name_d[0]: [0, 0, 0],  # 37\n",
    "    labels_name_d[1]: [0, 0, 0],  # 22.64\n",
    "    labels_name_d[2]: [0, 0, 0],  # 22.64\n",
    "    labels_name_d[3]: [0, 0, 0],  # 37\n",
    "    labels_name_d[4]: [0, 0, 0],  # 22.64\n",
    "    labels_name_d[5]: [0, 0, 0],  # 22.64\n",
    "}\n",
    "data_d_diff = {\n",
    "    labels_name_d[0]: 70,  # 37\n",
    "    labels_name_d[1]: 70,  # 22.64\n",
    "    labels_name_d[2]: 70,  # 22.64\n",
    "    labels_name_d[3]: 70,  # 37\n",
    "    labels_name_d[4]: 70,  # 22.64\n",
    "    labels_name_d[5]: 70,  # 22.64\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Megatron-LM\", \"XLA\", \"DLRover-Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "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": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(a) Single-node, single parallel')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "label_set = set()\n",
    "\n",
    "for idx, (x_label, y_data) in enumerate(data_a.items()):\n",
    "    diff_value = data_a_diff[x_label]\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",
    "\n",
    "        ax[0][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",
    "        ax[0][0].bar(\n",
    "            pos,\n",
    "            diff_value - value,\n",
    "            width=bar_width,\n",
    "            color=\"#E3E3E3\",\n",
    "            label=None,\n",
    "            edgecolor=edgecolor,\n",
    "            bottom=value\n",
    "        )\n",
    "\n",
    "ax[0][0].set_xticks(list(group_centers.values()))\n",
    "ax[0][0].set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax[0][0].set_ylim(0, 100)\n",
    "ax[0][0].set_yticks([0, 50, 100])\n",
    "ax[0][0].set_yticklabels([\"0\", \"50\", \"100\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[0][0].tick_params(axis=\"x\", bottom=False, labelsize=g_label_fontsize, pad=1)\n",
    "ax[0][0].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[0][0].set_ylabel(\"Comm-Time Ratio (%)\", fontsize=g_label_fontsize)\n",
    "ax[0][0].set_title(\"(a) Single-node, single parallel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(b) Two-node, single parallel')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total_label = \"Non-Overlap Communication\"\n",
    "\n",
    "for idx, (x_label, y_data) in enumerate(data_b.items()):\n",
    "    diff_value = data_b_diff[x_label]\n",
    "    positions = group_positions[idx]\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[0][1].bar(\n",
    "            pos,\n",
    "            value,\n",
    "            width=bar_width,\n",
    "            color=color,\n",
    "            hatch=hatch,\n",
    "            edgecolor=edgecolor,\n",
    "        )\n",
    "        ax[0][1].bar(\n",
    "            pos,\n",
    "            diff_value - value,\n",
    "            width=bar_width,\n",
    "            color=\"#E3E3E3\",\n",
    "            label=total_label,\n",
    "            edgecolor=edgecolor,\n",
    "            bottom=value\n",
    "        )\n",
    "\n",
    "        if total_label is not None:\n",
    "            total_label = None\n",
    "\n",
    "ax[0][1].set_xticks(list(group_centers.values()))\n",
    "ax[0][1].set_xticklabels(list(data_b.keys()))\n",
    "\n",
    "ax[0][1].set_ylim(0, 100)\n",
    "ax[0][1].set_yticks([0, 50, 100])\n",
    "ax[0][1].set_yticklabels([\"0\", \"50\", \"100\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[0][1].tick_params(axis=\"x\", bottom=False, labelsize=g_label_fontsize, pad=1)\n",
    "ax[0][1].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[0][1].set_title(\"(b) Two-node, single parallel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(c) Single-node, hybrid parallel')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for idx, (x_label, y_data) in enumerate(data_c.items()):\n",
    "    diff_value = data_c_diff[x_label]\n",
    "    positions = group_positions[idx]\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",
    "        if value == -1:\n",
    "            ax[1][0].text(\n",
    "                pos,\n",
    "                1,\n",
    "                \"OOM$\\\\times$\",\n",
    "                rotation=90,\n",
    "                ha=\"center\",\n",
    "                va=\"bottom\",\n",
    "                color=\"red\",\n",
    "            )\n",
    "            continue\n",
    "        ax[1][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",
    "        ax[1][0].bar(\n",
    "            pos,\n",
    "            diff_value - value,\n",
    "            width=bar_width,\n",
    "            color=\"#E3E3E3\",\n",
    "            label=None,\n",
    "            edgecolor=edgecolor,\n",
    "            bottom=value,\n",
    "        )\n",
    "\n",
    "ax[1][0].set_xticks(list(group_centers.values()))\n",
    "ax[1][0].set_xticklabels(list(data_c.keys()))\n",
    "\n",
    "ax[1][0].set_ylim(0, 100)\n",
    "ax[1][0].set_yticks([0, 50, 100])\n",
    "ax[1][0].set_yticklabels([\"0\", \"50\", \"100\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[1][0].tick_params(axis=\"x\", bottom=False, labelsize=g_label_fontsize, pad=1)\n",
    "ax[1][0].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[1][0].set_ylabel(\"Comm-Time Ratio (%)\", fontsize=g_label_fontsize)\n",
    "ax[1][0].set_title(\"(c) Single-node, hybrid parallel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 1.0, '(d) Two-node, hybrid parallel')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for idx, (x_label, y_data) in enumerate(data_d.items()):\n",
    "    diff_value = data_d_diff[x_label]\n",
    "    positions = group_positions[idx]\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][1].bar(\n",
    "            pos,\n",
    "            value,\n",
    "            width=bar_width,\n",
    "            color=color,\n",
    "            hatch=hatch,\n",
    "            edgecolor=edgecolor,\n",
    "        )\n",
    "        ax[1][1].bar(\n",
    "            pos,\n",
    "            diff_value - value,\n",
    "            width=bar_width,\n",
    "            color=\"#E3E3E3\",\n",
    "            label=None,\n",
    "            edgecolor=edgecolor,\n",
    "            bottom=value,\n",
    "        )\n",
    "\n",
    "ax[1][1].set_xticks(list(group_centers.values()))\n",
    "ax[1][1].set_xticklabels(list(data_d.keys()))\n",
    "\n",
    "ax[1][1].set_ylim(0, 100)\n",
    "ax[1][1].set_yticks([0, 50, 100])\n",
    "ax[1][1].set_yticklabels([\"0\", \"50\", \"100\"], rotation=90, ha=\"center\", va=\"center\")\n",
    "\n",
    "ax[1][1].tick_params(axis=\"x\", bottom=False, labelsize=g_label_fontsize, pad=1)\n",
    "ax[1][1].tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax[1][1].set_title(\"(d) Two-node, hybrid parallel\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f13b82bd1f0>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.legend(\n",
    "    ncol=4,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    bbox_to_anchor=(0.5, 1.1),\n",
    "    fontsize=g_label_fontsize,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family ['Times New Roman'] not found. Falling back to DejaVu Sans.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n",
      "findfont: Font family 'Times New Roman' not found.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 371044 characters]",
      "text/plain": [
       "<Figure size 4200x1400 with 4 Axes>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.savefig(\"end_to_end_overlap.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
}
