Spaces:
qatiba
/
Runtime error

File size: 5,506 Bytes
c6919c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{
 "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",
    "## NOTE: this requires a GPU to run\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 preload_models\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",
    "from bark.generation import preload_models\n",
    "from bark import generate_audio, SAMPLE_RATE\n",
    "import torch\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4d19d60",
   "metadata": {},
   "source": []
  },
  {
   "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 = 2949MB, time 3s\n",
      "\n",
      "Small models False, offloading to CPU: True\n",
      "\tmax memory usage = 7826MB, time 4s\n",
      "\n",
      "Small models True, offloading to CPU: False\n",
      "\tmax memory usage = 5504MB, time 2s\n",
      "\n",
      "Small models False, offloading to CPU: False\n",
      "\tmax memory usage = 7825MB, time 5s\n",
      "\n"
     ]
    }
   ],
   "source": [
    "offload_models = True\n",
    "use_small_models = True\n",
    "\n",
    "for offload_models in (True, False):\n",
    "    for use_small_models in (True, False):\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
}