{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90641144",
   "metadata": {},
   "source": [
    "# Bark Memory Profiling\n",
    "Bark has two ways to reduce GPU memory: \n",
    " - Small models: a smaller version of the model. This can be set by using the environment variable `SUNO_USE_SMALL_MODELS`\n",
    " - offloading models to CPU: Holding only one model at a time on the GPU, and shuttling the models to the CPU in between generations. \n",
    "\n",
    "# $ \\\\ $\n",
    "## First, we'll use the most memory efficient configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39ea4bed",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
    "os.environ[\"SUNO_USE_SMALL_MODELS\"] = \"1\"\n",
    "os.environ[\"SUNO_OFFLOAD_CPU\"] = \"1\"\n",
    "\n",
    "from bark.generation import (\n",
    "    generate_text_semantic,\n",
    "    preload_models,\n",
    ")\n",
    "from bark import generate_audio, SAMPLE_RATE\n",
    "\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "66b0c006",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████| 100/100 [00:01<00:00, 62.17it/s]\n",
      "100%|████████████████████████████████████████████████████████████████████████| 10/10 [00:03<00:00,  2.74it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max memory usage = 2396MB\n"
     ]
    }
   ],
   "source": [
    "torch.cuda.reset_peak_memory_stats()\n",
    "preload_models()\n",
    "audio_array = generate_audio(\"madam I'm adam\", history_prompt=\"v2/en_speaker_5\")\n",
    "max_utilization = torch.cuda.max_memory_allocated()\n",
    "print(f\"max memory usage = {max_utilization / 1024 / 1024:.0f}MB\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9922dd2d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bdbe578e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "213d1b5b",
   "metadata": {},
   "source": [
    "# Memory Profiling:\n",
    "We can profile the memory consumption of 4 scenarios\n",
    " - Small models, offloading to CPU\n",
    " - Large models, offloading to CPU\n",
    " - Small models, not offloading to CPU\n",
    " - Large models, not offloading to CPU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "417d5e9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from bark.generation import (\n",
    "    generate_text_semantic,\n",
    "    preload_models,\n",
    "    models,\n",
    ")\n",
    "import bark.generation\n",
    "\n",
    "from bark.api import semantic_to_waveform\n",
    "from bark import generate_audio, SAMPLE_RATE\n",
    "\n",
    "import torch\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cd83b45d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Small models True, offloading to CPU: True\n",
      "\tmax memory usage = 967MB, time 4s\n",
      "\n",
      "Small models False, offloading to CPU: True\n",
      "\tmax memory usage = 2407MB, time 8s\n",
      "\n",
      "Small models True, offloading to CPU: False\n",
      "\tmax memory usage = 2970MB, time 3s\n",
      "\n",
      "Small models False, offloading to CPU: False\n",
      "\tmax memory usage = 7824MB, time 6s\n",
      "\n"
     ]
    }
   ],
   "source": [
    "global models\n",
    "\n",
    "for offload_models in (True, False):\n",
    "    # this setattr is needed to do on the fly\n",
    "    # the easier way to do this is with `os.environ[\"SUNO_OFFLOAD_CPU\"] = \"1\"`\n",
    "    setattr(bark.generation, \"OFFLOAD_CPU\", offload_models)\n",
    "    for use_small_models in (True, False):\n",
    "        models = {}\n",
    "        torch.cuda.empty_cache()\n",
    "        torch.cuda.reset_peak_memory_stats()\n",
    "        preload_models(\n",
    "            text_use_small=use_small_models,\n",
    "            coarse_use_small=use_small_models,\n",
    "            fine_use_small=use_small_models,\n",
    "            force_reload=True,\n",
    "        )\n",
    "        t0 = time.time()\n",
    "        audio_array = generate_audio(\"madam I'm adam\", history_prompt=\"v2/en_speaker_5\", silent=True)\n",
    "        dur = time.time() - t0\n",
    "        max_utilization = torch.cuda.max_memory_allocated()\n",
    "        print(f\"Small models {use_small_models}, offloading to CPU: {offload_models}\")\n",
    "        print(f\"\\tmax memory usage = {max_utilization / 1024 / 1024:.0f}MB, time {dur:.0f}s\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bfe5fa06",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
