File size: 11,618 Bytes
9d0d223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d179a842",
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b561bd5-2495-47ab-9cdf-786662610840",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from audiocraft.models.loaders import load_compression_model, load_dit_model_melodyflow\n",
    "import audiocraft.models.builders as builders\n",
    "from omegaconf import OmegaConf, DictConfig"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ef93dcd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# T18 32k mono\n",
    "BOX_CHECKPOINT_PATH = \"/Users/glelan/projects/melodyflow/checkpoints/923877216v250.pt\"\n",
    "CODEC_CHECKPOINT_PATH = \"/Users/glelan/projects/melodyflow/checkpoints/epoch_200_train_step_401200_eval_step_24875.pt\"\n",
    "DEMO_PATH = \"/Users/glelan/projects/melodyflow/checkpoints/t18_32k_25hz_mono_10s\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "95af0ec5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# T24 48k stereo 25Hz 30s\n",
    "BOX_CHECKPOINT_PATH = \"/Users/glelan/projects/melodyflow/checkpoints/924369475v250.pt\"\n",
    "CODEC_CHECKPOINT_PATH = \"/Users/glelan/projects/melodyflow/checkpoints/vae/epoch_200_train_step_401200_eval_step_24875.pt\"\n",
    "DEMO_PATH = \"/Users/glelan/projects/melodyflow/checkpoints/t24_48k_25hz_stereo_30s\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f0027692",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(CODEC_CHECKPOINT_PATH, \"rb\") as fileobj:\n",
    "    codec_checkpoint = torch.load(fileobj, map_location=torch.device('cpu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "570506c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "del codec_checkpoint['hyper_parameters']['cfg']['disable_cuda_benchmarks']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['enable_profiler']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['manifold_bucket']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['manifold_path']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['ckpt_path']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['auto_retry_from_last_checkpoint']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['checkpoint_ttl']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['async_checkpoint']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['pg_timeout_in_minutes']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['check_preemption_every_n_train_steps']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['model_entity_id']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['max_eval_steps_per_epoch']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['prefetch_factor']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['max_audio_size']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['conditioners']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['data']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['trainer']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['lfq']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['residual_lfq']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['seed']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['monitor']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['generate_every']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['epochs']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['steps_per_epoch']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['batch_size_per_gpu']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['num_workers_per_gpu']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['save_every_n_epochs']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['unit']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['lr']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['optimizer']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['optim']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['msstftd']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['msd']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['mpd']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['l1']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['l2']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['mrstft']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['sdstft']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['mel']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['sisnr']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['sisdr']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['msspec']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['losses']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['balancer']\n",
    "del codec_checkpoint['hyper_parameters']['cfg']['adversarial']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "96d0ad5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(BOX_CHECKPOINT_PATH, \"rb\") as fileobj:\n",
    "    dit_checkpoint = torch.load(fileobj, map_location=torch.device('cpu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "057a5f1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "del dit_checkpoint['hyper_parameters']['cfg']['disable_cuda_benchmarks']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['enable_profiler']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['manifold_bucket']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['manifold_path']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['ckpt_path']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['auto_retry_from_last_checkpoint']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['checkpoint_ttl']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['async_checkpoint']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['pg_timeout_in_minutes']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['check_preemption_every_n_train_steps']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['model_entity_id']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['max_eval_steps_per_epoch']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['prefetch_factor']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['max_audio_size']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['sample_rate_video']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['video_crop']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['compression_model_checkpoint']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['public_compression']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['finetune_cp_path']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['clip_grad_norm']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['generate']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['logging']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['schedule']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['data']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['trainer']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['seed']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['monitor']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['generate_every']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['epochs']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['steps_per_epoch']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['batch_size_per_gpu']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['num_workers_per_gpu']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['save_every_n_epochs']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['unit']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['lr']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['optim']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "df52ded5",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['spectral_norm_attn_iters']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['spectral_norm_ff_iters']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['residual_balancer_attn']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['residual_balancer_ff']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['mask_cross_attention']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['qk_norm']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['n_q']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['q_modeling']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['emb_lr']\n",
    "del dit_checkpoint['hyper_parameters']['cfg']['transformer_lm']['card']\n",
    "cfg = OmegaConf.create(dit_checkpoint['hyper_parameters']['cfg'])\n",
    "cfg.fuser.input_interpolate = []\n",
    "cfg.dataset = {\"segment_duration\" : 30.0}\n",
    "cleaned_dit_checkpoint = {'xp.cfg': cfg, \"best_state\": {}}\n",
    "for key, value in dit_checkpoint[\"state_dict\"].items():\n",
    "    if key.startswith(\"ema.module.\"):\n",
    "        cleaned_dit_checkpoint[\"best_state\"][key[11:]] = value\n",
    "cleaned_dit_checkpoint[\"best_state\"][\"latent_mean\"] = codec_checkpoint[\"ema_model\"][\"bottleneck_norm.running_mean\"]\n",
    "cleaned_dit_checkpoint[\"best_state\"][\"latent_std\"] = codec_checkpoint[\"ema_model\"][\"bottleneck_norm.running_std\"]\n",
    "with open(DEMO_PATH + \"/state_dict.bin\", \"wb\") as fileobj:\n",
    "    torch.save(cleaned_dit_checkpoint, fileobj)\n",
    "flow_model = load_dit_model_melodyflow(DEMO_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "793188cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "del codec_checkpoint[\"ema_model\"][\"bottleneck_norm.running_mean\"]\n",
    "del codec_checkpoint[\"ema_model\"][\"bottleneck_norm.running_std\"]\n",
    "codec_checkpoint['hyper_parameters']['cfg']['encodec']['quantizer'] = \"no_quant\"\n",
    "cleand_codec_checkpoint = {'xp.cfg': codec_checkpoint['hyper_parameters']['cfg'], 'best_state': codec_checkpoint[\"ema_model\"]}\n",
    "with open(DEMO_PATH + \"/compression_state_dict.bin\", \"wb\") as fileobj:\n",
    "    torch.save(cleand_codec_checkpoint, fileobj)\n",
    "compression_model = load_compression_model(DEMO_PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d6d2305",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1915c5e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "melodyflow",
   "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.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}