{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 145,
   "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": 146,
   "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": 147,
   "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": 148,
   "metadata": {},
   "outputs": [],
   "source": [
    "# comm\n",
    "data_a = {\n",
    "    labels_name_a[0]: [3.5, 10.4, 0.1],  # modela 8DP\n",
    "    labels_name_a[1]: [49.0, 26.5, 13.6],  # modela 8TP\n",
    "    labels_name_a[2]: [41.3, 25.5, 13.0],  # modelb 8TP\n",
    "    labels_name_a[3]: [33.4, 37.6, 28.2],  # modela 2DP 4TP\n",
    "    labels_name_a[4]: [31.0, 32.3, 25.7],  # modela 2DP 8TP\n",
    "    labels_name_a[5]: [31.6, 36.2, 28.2],  # modela 4DP 2TP\n",
    "    labels_name_a[6]: [-1, 42.3, 37.3],  # modelb 4DP 2TP\n",
    "    labels_name_a[7]: [48.2, 1, 1],  # modelc 1DP 8TP 8EP\n",
    "    labels_name_a[8]: [81.9, 1, 1],  # modelc 2DP 8TP 8EP\n",
    "    labels_name_a[9]: [32.5, 1, 1],  # modeld 1DP 8TP 8EP\n",
    "    labels_name_a[10]: [81.2, 80.0, 79.1],  # modeld 2DP 8TP 8EP\n",
    "}\n",
    "# total = comm/(comm+comp)\n",
    "data_a_diff = {\n",
    "    labels_name_a[0]: 38.4,  # modela 8DP\n",
    "    labels_name_a[1]: 57.6,  # modela 8TP\n",
    "    labels_name_a[2]: 52.2,  # modelb 8TP\n",
    "    labels_name_a[3]: 46.8,  # modela 2DP 4TP\n",
    "    labels_name_a[4]: 42.0,  # modela 2DP 8TP\n",
    "    labels_name_a[5]: 43.4,  # modela 4DP 2TP\n",
    "    labels_name_a[6]: 53.9,  # modelb 4DP 2TP\n",
    "    labels_name_a[7]: 48.3,  # modelc 1DP 8TP 8EP\n",
    "    labels_name_a[8]: 82.2,  # modelc 2DP 8TP 8EP\n",
    "    labels_name_a[9]: 35.8,  # modeld 1DP 8TP 8EP\n",
    "    labels_name_a[10]: 81.5,  # modeld 2DP 8TP 8EP\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Megatron-LM\", \"XLA\", \"Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "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": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(0.5, 13.333333333333332, '(a) # (model, DP, TP, EP)')"
      ]
     },
     "execution_count": 151,
     "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",
    "\n",
    "    up_base = None\n",
    "\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",
    "\n",
    "        if label in label_set:\n",
    "            local_label = None\n",
    "        else:\n",
    "            local_label = label\n",
    "            label_set.add(local_label)\n",
    "\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",
    "                size=g_label_fontsize - 6,\n",
    "            )\n",
    "            continue\n",
    "\n",
    "        if value == 100:\n",
    "            continue\n",
    "\n",
    "        # if up_base == None:\n",
    "        #     ax.text(\n",
    "        #         pos,\n",
    "        #         diff_value + 3,\n",
    "        #         f\"{round(value, 2)}%\",\n",
    "        #         ha=\"center\",\n",
    "        #         fontweight=\"bold\",\n",
    "        #         color=colors[1],\n",
    "        #         size=g_label_fontsize - 8,\n",
    "        #     )\n",
    "        #     up_base = value\n",
    "        # else:\n",
    "        #     ax.text(\n",
    "        #         pos,\n",
    "        #         diff_value + 3,\n",
    "        #         f\"{round((value) - up_base, 2)}%\",\n",
    "        #         ha=\"center\",\n",
    "        #         fontweight=\"bold\",\n",
    "        #         color=colors[1],\n",
    "        #         size=g_label_fontsize - 8,\n",
    "        #     )\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",
    "        total_label = None\n",
    "        if idx == 1 and i == 0:\n",
    "            total_label = \"Total Communication\"\n",
    "\n",
    "        ax.bar(\n",
    "            pos,\n",
    "            diff_value - value,\n",
    "            width=bar_width,\n",
    "            color=\"#E3E3E3\",\n",
    "            alpha=0.6,\n",
    "            label=total_label,\n",
    "            edgecolor=edgecolor,\n",
    "            bottom=value,\n",
    "        )\n",
    "\n",
    "ax.set_xticks(list(group_centers.values()))\n",
    "ax.set_xticklabels(list(data_a.keys()))\n",
    "\n",
    "ax.set_ylim(0, 110)\n",
    "ax.set_yticks([0, 50, 100])\n",
    "ax.set_yticklabels([\"0\", \"50\", \"100\"], 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(\"Comm Ratio (%)\", fontsize=g_label_fontsize)\n",
    "ax.set_xlabel(\"(a) # (model, DP, TP, EP)\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f32240d3bc0>"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.legend(\n",
    "    ncol=2,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    bbox_to_anchor=(0.4, 1.0),\n",
    "    fontsize=g_label_fontsize - 2,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 188080 characters]",
      "text/plain": [
       "<Figure size 2100x700 with 1 Axes>"
      ]
     },
     "execution_count": 153,
     "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
}
