{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 41,
   "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": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 59876 characters]",
      "text/plain": [
       "<Figure size 4200x840 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(\n",
    "    figsize=(14, 14 / 5), ncols=1, nrows=1, constrained_layout=True, dpi=300\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "labels_name_a = [\n",
    "    \"ModelA\\n8DP\\n-\",\n",
    "    \"ModelA\\n-\\n8TP\",\n",
    "    \"ModelB\\n-\\n8TP\",\n",
    "    \"ModelA\\n2DP\\n$\\\\times$4TP\",\n",
    "    \"ModelA\\n2DP\\n$\\\\times$8TP\",\n",
    "    \"ModelA\\n4DP\\n$\\\\times$2TP\",\n",
    "    \"ModelB\\n4DP\\n$\\\\times$2TP\",\n",
    "    \"ModelC\\n1DP$\\\\times$8EP\\n$\\\\times$8TP\",\n",
    "    \"ModelC\\n2DP$\\\\times$8EP\\n$\\\\times$8TP\",\n",
    "    \"ModelD\\n1DP$\\\\times$8EP\\n$\\\\times$8TP\",\n",
    "    \"ModelD\\n2DP$\\\\times$8EP\\n$\\\\times$8TP\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# MFU\n",
    "data_a = {\n",
    "    labels_name_a[0]: [41.0, 34.2, 44.4],  # modela 8DP\n",
    "    labels_name_a[1]: [26.2, 36.8, 43.6],  # modela 8TP\n",
    "    labels_name_a[2]: [30.1, 39.6, 49.4],  # modelb 8TP\n",
    "    labels_name_a[3]: [37.1, 30.4, 44.6],  # modela 2DP 4TP\n",
    "    labels_name_a[4]: [39.6, 37.2, 47.9],  # modela 2DP 8TP\n",
    "    labels_name_a[5]: [40.4, 30.5, 44.7],  # modela 4DP 2TP\n",
    "    labels_name_a[6]: [-1, 31.7, 38.0],  # modelb 4DP 2TP\n",
    "    labels_name_a[7]: [9.3, 0.001, 0.001],  # modelc 1DP 8TP 8EP\n",
    "    labels_name_a[8]: [4.0, 4.8, 5.3],  # modelc 2DP 8TP 8EP\n",
    "    labels_name_a[9]: [15.1, 15.3, 16.3],  # modeld 1DP 8TP 8EP\n",
    "    labels_name_a[10]: [4.2, 4.8, 5.3],  # modeld 2DP 8TP 8EP\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LM 1.08 XLA 1.3\n",
      "LM 1.66 XLA 1.18\n",
      "LM 1.64 XLA 1.25\n",
      "LM 1.2 XLA 1.47\n",
      "LM 1.21 XLA 1.29\n",
      "LM 1.11 XLA 1.47\n",
      "LM -38.0 XLA 1.2\n",
      "LM 0.0 XLA 1.0\n",
      "LM 1.32 XLA 1.1\n",
      "LM 1.08 XLA 1.07\n",
      "LM 1.26 XLA 1.1\n",
      "-26.44\n",
      "13.43\n"
     ]
    }
   ],
   "source": [
    "a_m = 0\n",
    "a_x = 0\n",
    "for y_data in data_a.values():\n",
    "    print(f\"LM {round(y_data[2]/y_data[0], 2)} XLA {round(y_data[2]/y_data[1], 2)}\")\n",
    "    a_m += round(y_data[2] / y_data[0], 2)\n",
    "    a_x += round(y_data[2] / y_data[1], 2)\n",
    "print(a_m)\n",
    "print(a_x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "legend_labels = [\"Megatron-LM\", \"XLA\", \"Lynx\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "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": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Text(13.333333333333346, 0.5, 'MFU (%)')"
      ]
     },
     "execution_count": 48,
     "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",
    "    up_base =[i for i in y_data if i > 0][0]\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",
    "        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",
    "            continue\n",
    "        up_v = round(value/up_base, 2)\n",
    "        if up_v == 1:\n",
    "            up_v = 1\n",
    "        ax.text(\n",
    "            pos,\n",
    "            value + 2,\n",
    "            f\"{str(up_v)}$\\\\times$\",\n",
    "            ha=\"center\",\n",
    "            fontweight=\"bold\",\n",
    "            color=colors[1],\n",
    "            rotation=90,\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, 100)\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(axis=\"x\", bottom=False, labelsize=g_label_fontsize, pad=1)\n",
    "ax.tick_params(axis=\"y\", left=True, labelsize=g_label_fontsize, pad=5)\n",
    "\n",
    "ax.set_ylabel(\"MFU (%)\", fontsize=g_label_fontsize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7f356d72f8f0>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.legend(\n",
    "    ncol=3,\n",
    "    loc=\"upper center\",\n",
    "    frameon=False,\n",
    "    shadow=False,\n",
    "    bbox_to_anchor=(0.5, 0.95),\n",
    "    fontsize=g_label_fontsize,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 303800 characters]",
      "text/plain": [
       "<Figure size 4200x840 with 1 Axes>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig.savefig(\"end_to_end_mfu.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
}
