dslee2601 commited on
Commit
fb2212a
·
1 Parent(s): 5808c9b

cpu and gpu support; readme update;

Browse files
.fig/artifact-dac-decoding without overlap.png ADDED
.gitignore CHANGED
@@ -1,2 +1,4 @@
1
  token.txt
2
  __pycache__/
 
 
 
1
  token.txt
2
  __pycache__/
3
+ tokens.pt
4
+ out.wav
README.md CHANGED
@@ -1,3 +1,67 @@
1
  ---
2
  license: mit
 
 
 
 
3
  ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  license: mit
3
+ tags:
4
+ - DAC
5
+ - Descript Audio Codec
6
+ - PyTorch
7
  ---
8
+
9
+ # Descript Audio Codec (DAC)
10
+ DAC is the state-of-the-art audio tokenizer with improvement upon the previous tokenizers like SoundStream and EnCodec.
11
+
12
+ This model card provides an easy-to-use API for a *pretrained DAC* [1] whose backbone and pretrained weights are from [its original reposotiry](https://github.com/descriptinc/descript-audio-codec). With this API, you can encode and decode by a single line of code either using CPU or GPU. Furhtermore, it supports chunk-based processing for memory-efficient processing, especially important for GPU processing.
13
+
14
+
15
+ # Usage
16
+
17
+
18
+
19
+ <!--
20
+ - different models for different khz
21
+ -->
22
+
23
+
24
+ # Runtime
25
+
26
+ To give you a brief idea, the following table reports average runtime on CPU and GPU to encode and decode 10s audio. The runtime is measured in second. The used CPU is Intel Core i9 11900K and GPU is RTX3060.
27
+ ```
28
+ | Task | CPU | GPU |
29
+ |:---------------:|:-------:|:-------:|
30
+ | Encoding | 6.71 | 0.19 |
31
+ | Decoding | 15.4 | 0.31 |
32
+ ```
33
+ The decoding process takes a longer simply because the decoder is larger than the encoder.
34
+
35
+
36
+
37
+ # Technical Discussion
38
+
39
+ ### Chunk-based Processing
40
+ It's introduced for memory-efficient processing for both encoding and decoding.
41
+ For encoding, we simply chunk an audio into N chunks and process them iteratively.
42
+ Similarly, for decoding, we chunk a token set into M chunks of token subsets and process them iteratively.
43
+ However, the decoding process with naive chunking causes an artifact in the decoded audio.
44
+ That is because the decoder reconstructs audio given multiple neighboring tokens (i.e., multiple neighboring tokens for a segment of audio) rather than a single token for a segment of audio.
45
+
46
+ To tackle the problem, we introduce overlap between the chunks in the decoding, parameterized by `decoding_overlap_rate` in the model. By default, we introduce 10% of overlap between the chunks. Then, two subsequent chunks reuslt in two segments of audio with 10% overlap, and the overlap is averaged out for smoothing.
47
+
48
+ The following figure compares reconstructed audio with and without the overlapping.
49
+ <p align="center">
50
+ <img src=".fig/artifact-dac-decoding without overlap.png" alt="" width=80%>
51
+ </p>
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+ # References
60
+ [1] Kumar, Rithesh, et al. "High-fidelity audio compression with improved rvqgan." Advances in Neural Information Processing Systems 36 (2024).
61
+
62
+
63
+
64
+ <!-- contributions
65
+ - chunk processing
66
+ - add device parameter in the test notebook
67
+ -->
model.py CHANGED
@@ -124,7 +124,7 @@ class DAC(PreTrainedModel):
124
  end = start + chunk_size
125
  chunk = x[:, :, start:end]
126
  chunk = self.dac.preprocess(chunk, sr)
127
- zq, s, _, _, _ = self.dac.encode(chunk)
128
  zq = zq.cpu()
129
  s = s.cpu()
130
  """
@@ -175,7 +175,7 @@ class DAC(PreTrainedModel):
175
  for start in range(0, length, chunk_size-overlap_size):
176
  end = start + chunk_size
177
  chunk = zq[:,:, start:end] # (b, d, chunk_size)
178
- waveform = self.dac.decode(chunk) # (b, 1, chunk_size*self.downsampling_rate)
179
  waveform = waveform.cpu()
180
 
181
  if isinstance(waveform_concat, type(None)):
@@ -198,11 +198,12 @@ class DAC(PreTrainedModel):
198
  """
199
  s: (b, n_rvq, length)
200
  """
201
- zq, _, _ = self.dac.quantizer.from_codes(s) # zq: (b, d, length)
 
202
  return zq
203
 
204
  def save_tensor(self, tensor:torch.Tensor, fname:str) -> None:
205
- torch.save(tensor, fname)
206
 
207
  def load_tensor(self, fname:str):
208
  return torch.load(fname)
 
124
  end = start + chunk_size
125
  chunk = x[:, :, start:end]
126
  chunk = self.dac.preprocess(chunk, sr)
127
+ zq, s, _, _, _ = self.dac.encode(chunk.to(self.device))
128
  zq = zq.cpu()
129
  s = s.cpu()
130
  """
 
175
  for start in range(0, length, chunk_size-overlap_size):
176
  end = start + chunk_size
177
  chunk = zq[:,:, start:end] # (b, d, chunk_size)
178
+ waveform = self.dac.decode(chunk.to(self.device)) # (b, 1, chunk_size*self.downsampling_rate)
179
  waveform = waveform.cpu()
180
 
181
  if isinstance(waveform_concat, type(None)):
 
198
  """
199
  s: (b, n_rvq, length)
200
  """
201
+ zq, _, _ = self.dac.quantizer.from_codes(s.to(self.device)) # zq: (b, d, length)
202
+ zq = zq.cpu()
203
  return zq
204
 
205
  def save_tensor(self, tensor:torch.Tensor, fname:str) -> None:
206
+ torch.save(tensor.cpu(), fname)
207
 
208
  def load_tensor(self, fname:str):
209
  return torch.load(fname)
save_model.ipynb CHANGED
@@ -101,7 +101,7 @@
101
  },
102
  {
103
  "cell_type": "code",
104
- "execution_count": 5,
105
  "metadata": {},
106
  "outputs": [
107
  {
@@ -124,6 +124,26 @@
124
  "model = AutoModel.from_pretrained('hance-ai/descript-audio-codec', trust_remote_code=True)"
125
  ]
126
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  {
128
  "cell_type": "code",
129
  "execution_count": 6,
 
101
  },
102
  {
103
  "cell_type": "code",
104
+ "execution_count": 8,
105
  "metadata": {},
106
  "outputs": [
107
  {
 
124
  "model = AutoModel.from_pretrained('hance-ai/descript-audio-codec', trust_remote_code=True)"
125
  ]
126
  },
127
+ {
128
+ "cell_type": "code",
129
+ "execution_count": 9,
130
+ "metadata": {},
131
+ "outputs": [
132
+ {
133
+ "data": {
134
+ "text/plain": [
135
+ "device(type='cpu')"
136
+ ]
137
+ },
138
+ "execution_count": 9,
139
+ "metadata": {},
140
+ "output_type": "execute_result"
141
+ }
142
+ ],
143
+ "source": [
144
+ "model.device"
145
+ ]
146
+ },
147
  {
148
  "cell_type": "code",
149
  "execution_count": 6,
test_DAC.ipynb CHANGED
@@ -11,20 +11,40 @@
11
  "cell_type": "code",
12
  "execution_count": 1,
13
  "metadata": {},
14
- "outputs": [],
 
 
 
 
 
 
 
 
 
15
  "source": [
16
  "import os\n",
17
  "from pathlib import Path\n",
18
  "\n",
19
  "import torch\n",
20
  "\n",
21
- "from model import DAC"
22
  ]
23
  },
24
  {
25
  "cell_type": "code",
26
  "execution_count": 2,
27
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
28
  "outputs": [
29
  {
30
  "name": "stderr",
@@ -39,22 +59,13 @@
39
  ],
40
  "source": [
41
  "# load the model\n",
42
- "dac = DAC('44khz')"
43
- ]
44
- },
45
- {
46
- "cell_type": "code",
47
- "execution_count": 3,
48
- "metadata": {},
49
- "outputs": [],
50
- "source": [
51
- "# settings\n",
52
- "fname = str(Path(os.getcwd()).joinpath('.sample_sound', 'jazz_swing.wav'))"
53
  ]
54
  },
55
  {
56
  "cell_type": "code",
57
- "execution_count": 4,
58
  "metadata": {},
59
  "outputs": [
60
  {
@@ -75,14 +86,22 @@
75
  },
76
  {
77
  "cell_type": "code",
78
- "execution_count": 5,
79
  "metadata": {},
80
  "outputs": [
81
  {
82
  "name": "stdout",
83
  "output_type": "stream",
84
  "text": [
85
- "waveform.shape: torch.Size([1, 1, 441344])\n"
 
 
 
 
 
 
 
 
86
  ]
87
  }
88
  ],
@@ -94,7 +113,7 @@
94
  },
95
  {
96
  "cell_type": "code",
97
- "execution_count": 10,
98
  "metadata": {},
99
  "outputs": [
100
  {
@@ -113,7 +132,7 @@
113
  },
114
  {
115
  "cell_type": "code",
116
- "execution_count": 11,
117
  "metadata": {},
118
  "outputs": [],
119
  "source": [
@@ -123,14 +142,14 @@
123
  },
124
  {
125
  "cell_type": "code",
126
- "execution_count": 14,
127
  "metadata": {},
128
  "outputs": [
129
  {
130
  "name": "stderr",
131
  "output_type": "stream",
132
  "text": [
133
- "d:\\projects\\sound_effect_variation_generation\\dac_model.py:181: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
134
  " return torch.load(fname)\n"
135
  ]
136
  }
@@ -140,6 +159,13 @@
140
  "dac.save_tensor(s, 'tokens.pt')\n",
141
  "loaded_s = dac.load_tensor('tokens.pt') # s == loaded_s"
142
  ]
 
 
 
 
 
 
 
143
  }
144
  ],
145
  "metadata": {
 
11
  "cell_type": "code",
12
  "execution_count": 1,
13
  "metadata": {},
14
+ "outputs": [
15
+ {
16
+ "name": "stderr",
17
+ "output_type": "stream",
18
+ "text": [
19
+ "C:\\Users\\dslee\\AppData\\Roaming\\Python\\Python38\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
20
+ " from .autonotebook import tqdm as notebook_tqdm\n"
21
+ ]
22
+ }
23
+ ],
24
  "source": [
25
  "import os\n",
26
  "from pathlib import Path\n",
27
  "\n",
28
  "import torch\n",
29
  "\n",
30
+ "from model import DAC, DACConfig"
31
  ]
32
  },
33
  {
34
  "cell_type": "code",
35
  "execution_count": 2,
36
  "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "# settings\n",
40
+ "fname = str(Path(os.getcwd()).joinpath('.sample_sound', 'jazz_swing.wav'))\n",
41
+ "device = 'cpu'"
42
+ ]
43
+ },
44
+ {
45
+ "cell_type": "code",
46
+ "execution_count": 3,
47
+ "metadata": {},
48
  "outputs": [
49
  {
50
  "name": "stderr",
 
59
  ],
60
  "source": [
61
  "# load the model\n",
62
+ "config = DACConfig()\n",
63
+ "dac = DAC(config).to(device)"
 
 
 
 
 
 
 
 
 
64
  ]
65
  },
66
  {
67
  "cell_type": "code",
68
+ "execution_count": 7,
69
  "metadata": {},
70
  "outputs": [
71
  {
 
86
  },
87
  {
88
  "cell_type": "code",
89
+ "execution_count": 8,
90
  "metadata": {},
91
  "outputs": [
92
  {
93
  "name": "stdout",
94
  "output_type": "stream",
95
  "text": [
96
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
97
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
98
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
99
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
100
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
101
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
102
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
103
+ "waveform.shape: torch.Size([1, 1, 441344])\n",
104
+ "15.4 s ± 142 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
105
  ]
106
  }
107
  ],
 
113
  },
114
  {
115
  "cell_type": "code",
116
+ "execution_count": 9,
117
  "metadata": {},
118
  "outputs": [
119
  {
 
132
  },
133
  {
134
  "cell_type": "code",
135
+ "execution_count": null,
136
  "metadata": {},
137
  "outputs": [],
138
  "source": [
 
142
  },
143
  {
144
  "cell_type": "code",
145
+ "execution_count": null,
146
  "metadata": {},
147
  "outputs": [
148
  {
149
  "name": "stderr",
150
  "output_type": "stream",
151
  "text": [
152
+ "d:\\projects\\descript-audio-codec\\model.py:209: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.\n",
153
  " return torch.load(fname)\n"
154
  ]
155
  }
 
159
  "dac.save_tensor(s, 'tokens.pt')\n",
160
  "loaded_s = dac.load_tensor('tokens.pt') # s == loaded_s"
161
  ]
162
+ },
163
+ {
164
+ "cell_type": "code",
165
+ "execution_count": null,
166
+ "metadata": {},
167
+ "outputs": [],
168
+ "source": []
169
  }
170
  ],
171
  "metadata": {