{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c666bc86",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:43.754629Z",
     "start_time": "2024-01-20T18:32:43.634685Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a10-yul-dev-node-444\r\n"
     ]
    }
   ],
   "source": [
    "!echo $HOSTNAME"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "08218f19",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:43.757727Z",
     "start_time": "2024-01-20T18:32:43.755992Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "76fc0b2c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:43.814199Z",
     "start_time": "2024-01-20T18:32:43.759128Z"
    }
   },
   "outputs": [],
   "source": [
    "# vocab: \n",
    "#   0-60_000 text\n",
    "#   1x0-3999   semantic\n",
    "#   8x0-4095  coarse\n",
    "\n",
    "#   4000 semantic pad token\n",
    "#   4001 semantic infer token\n",
    "#   2048 coarse pad token\n",
    "#   2049 coarse infer token\n",
    "\n",
    "# Memmaps:\n",
    "#   Nx9x3584 for audio tokens\n",
    "# Jsons:\n",
    "#   N*Dict with meta keys \n",
    "#     \"dataset\"\n",
    "#     \"original_id\", \"original_duration_s\",\n",
    "#     \"start_s\", \"end_s\", \n",
    "#     \"text_segments\", \"private_text_segments\",\n",
    "#     \"text\", \"private_text\",\n",
    "#     \"tags\", \"private_tags\",\n",
    "#     \"views\",\n",
    "#   Dict with meta keys {\"dataset\": [\"idx_list\"]}\n",
    "\n",
    "# Bundles (mert_v2_2x1k & dac_2c_25_8):\n",
    "# s3://suno-data/datasets/bundles/\n",
    "#  v1/youtube_music\n",
    "#  v1/genius_hq\n",
    "#  v1/jamendo\n",
    "#  v1/imslp\n",
    "#  v2/pond5\n",
    "#  v2/deezer\n",
    "#  v2/ytm_tagged\n",
    "#  v2/ytm_mb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ce43e8ad",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:44.330605Z",
     "start_time": "2024-01-20T18:32:43.815683Z"
    }
   },
   "outputs": [],
   "source": [
    "from matplotlib import pyplot as plt\n",
    "\n",
    "import sys\n",
    "\n",
    "sys.path.insert(0, \"/home/tony/Work/glockenspiel/sunoGPT/scripts/\")\n",
    "\n",
    "from data_preparation_7b import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "17f66742",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:44.333630Z",
     "start_time": "2024-01-20T18:32:44.331835Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4000 2048\n"
     ]
    }
   ],
   "source": [
    "print(SEMANTIC_CODEBOOK_SIZE, COARSE_CODEBOOK_SIZE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d188ba34",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:44.390272Z",
     "start_time": "2024-01-20T18:32:44.334619Z"
    }
   },
   "outputs": [],
   "source": [
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/genius_hq.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/youtube_music.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/jamendo.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/imslp.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/pond5_music.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/deezer.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/ytm_tagged.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/musescore.jsonl /app/suno/data/chirp_v3/metadata/\n",
    "# !aws s3 cp s3://suno-data/georg/data/chirp_v2_5/filtered_metas/ytm_mb.jsonl /app/suno/data/chirp_v3/metadata/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "40767a44",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:32:44.447945Z",
     "start_time": "2024-01-20T18:32:44.391682Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/chirp_v3_ft_v3\"\n",
    "os.makedirs(OUT_DATA_DIR, exist_ok=True)\n",
    "METAS_DIR = \"/home/tony/Work/tony/FineTuning_chirp_v3/metadata/\"\n",
    "NJOBS = 60\n",
    "CHUNKSIZE = 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b30bfb77",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:34:50.653465Z",
     "start_time": "2024-01-20T18:32:44.449522Z"
    }
   },
   "outputs": [],
   "source": [
    "# load manifests of IDs and text and tags etc\n",
    "meta_info_map = {\n",
    "    \"genius_hq\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"genius_hq_v3.jsonl\"))},\n",
    "#     \"youtube_music\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"youtube_music.jsonl\"))},\n",
    "#     \"jamendo\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"jamendo.jsonl\"))},\n",
    "#     \"imslp\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"imslp.jsonl\"))},\n",
    "    \"pond5_music\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"pond5_music_v1.jsonl\"))},\n",
    "#     \"deezer\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"deezer.jsonl\"))},\n",
    "#     \"ytm_tagged\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"ytm_tagged.jsonl\"))},\n",
    "#     \"musescore\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"musescore.jsonl\"))},\n",
    "    # \"ytm_mb\": {m[\"id\"]: m for m in read_jsonl(os.path.join(METAS_DIR, \"ytm_mb.jsonl\"))},\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4a145268",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:34:52.469698Z",
     "start_time": "2024-01-20T18:34:50.655901Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(\"/app/suno/data/preprocessing/meta_cutoff_freq.json\", \"r\") as fp:\n",
    "    meta_cutoff_freq = json.load(fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2a01e35c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:35:16.533701Z",
     "start_time": "2024-01-20T18:34:52.471396Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start prepare data\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:12<00:00, 12.43s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 hours of genius_hq_lyrics_foreign\n",
      "10 hours of genius_hq_lyrics\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:11<00:00, 11.43s/it]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 hours of pond5_music\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# (start_idx, end_idx), n_archives_semantic, n_archives_coarse\n",
    "datasets = [\n",
    "    # (\"youtube_music\", \"v1\", (0, 1), 1, 1),\n",
    "    (\"genius_hq\", \"v1\", (0, 1), 1, 1),\n",
    "    # (\"imslp\", \"v1\", (0, 1), 1, 1),\n",
    "    # (\"jamendo\", \"v1\", (0, 1), 1, 1),\n",
    "    (\"pond5_music\", \"v2\", (0, 1), 1, 1),\n",
    "    # (\"deezer\", \"v2\", (0, 1), 1, 1),\n",
    "    # (\"ytm_tagged\", \"v2\", (0, 1), 1, 1),\n",
    "    # (\"musescore\", \"v2\", (0, 1), 1, 1),\n",
    "    # (\"ytm_mb\", \"v2\", (0, 1), 1, 1),\n",
    "]\n",
    "prep_data(\n",
    "    datasets,\n",
    "    out_data_dir=OUT_DATA_DIR,\n",
    "    meta_info_map=meta_info_map,\n",
    "    meta_cutoff_freq=meta_cutoff_freq,  # TODO: This arg should be removed in the longer run...\n",
    "    is_val=True,\n",
    "    njobs=NJOBS,\n",
    "    chunksize=CHUNKSIZE,\n",
    ")\n",
    "# 24 hours of youtube_music\n",
    "#  3 hours of youtube_music_lyrics\n",
    "#  2 hours of youtube_music_lyrics_foreign\n",
    "#  4 hours of genius_hq\n",
    "#  9 hours of genius_hq_lyrics\n",
    "#  5 hours of genius_hq_lyrics_foreign\n",
    "# 17 hours of imslp\n",
    "# 30 hours of jamendo\n",
    "# 14 hours of pond5_music\n",
    "# 12 hours of deezer\n",
    "#  7 hours of deezer_lyrics\n",
    "#  8 hours of deezer_lyrics_foreign\n",
    "# 30 hours of ytm_tagged\n",
    "#  3 hours of musescore_lyrics\n",
    "# 30 hours of ytm_mb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "956bd1f3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T18:35:16.536356Z",
     "start_time": "2024-01-20T18:35:16.534932Z"
    }
   },
   "outputs": [],
   "source": [
    "# Stats:\n",
    "# tot expected: 4.5Tb\n",
    "# speed for ytm_mb: 53129/200*(3*60+40)/60/60 == 16 hours"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a2dfa127",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.381518Z",
     "start_time": "2024-01-20T18:35:16.537322Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "start prepare data\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████| 72/72 [54:36<00:00, 45.51s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "43,921 hours of genius_hq_lyrics\n",
      "23,734 hours of genius_hq_lyrics_foreign\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████| 69/69 [23:41<00:00, 20.60s/it]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8,701 hours of pond5_music\n"
     ]
    }
   ],
   "source": [
    "# (start_idx, end_idx), n_archives_semantic, n_archives_coarse\n",
    "datasets = [\n",
    "    # (\"youtube_music\", \"v1\", (1, 4204), 1, 1),\n",
    "    (\"genius_hq\", \"v1\", (1, 4302), 1, 1),\n",
    "    # (\"imslp\", \"v1\", (1, 558), 1, 1),\n",
    "    # (\"jamendo\", \"v1\", (1, 112), 1, 1),\n",
    "    (\"pond5_music\", \"v2\", (1, 4138), 1, 1),\n",
    "    # (\"deezer\", \"v2\", (1, 1538), 1, 1),\n",
    "    # (\"ytm_tagged\", \"v2\", (1, 5545), 1, 1),\n",
    "    # (\"musescore\", \"v2\", (1, 33), 1, 1),\n",
    "#     (\"ytm_mb\", \"v2\", (1, 53129), 1, 1),\n",
    "#     (\"ytm_mb\", \"v2\", (1, 50001), 1, 1),\n",
    "]\n",
    "prep_data(\n",
    "    datasets,\n",
    "    out_data_dir=OUT_DATA_DIR,\n",
    "    meta_info_map=meta_info_map,\n",
    "    meta_cutoff_freq=meta_cutoff_freq,  # TODO: This arg should be removed in the longer run...\n",
    "    is_val=False,\n",
    "    njobs=NJOBS,\n",
    "    chunksize=CHUNKSIZE,\n",
    ")\n",
    "# youtube_music: ~1h runtime\n",
    "#    105,498 hours of youtube_music\n",
    "#     12,168 hours of youtube_music_lyrics\n",
    "#     10,363 hours of youtube_music_lyrics_foreign\n",
    "# genius_hq: ~1h runtime\n",
    "#     17,784 hours of genius_hq\n",
    "#     41,544 hours of genius_hq_lyrics\n",
    "#     17,469 hours of genius_hq_lyrics_foreign\n",
    "# imslp: ~8m runtime\n",
    "#     12,175 hours of imslp\n",
    "# jamendo: ~27m runtime\n",
    "#      3,251 hours of jamendo\n",
    "# pond5_music: ~51m runtime\n",
    "#     58,592 hours of pond5_music\n",
    "# deezer: ~19m runtime\n",
    "#     12,181 hours of deezer\n",
    "#     10,132 hours of deezer_lyrics\n",
    "#      6,770 hours of deezer_lyrics_foreign\n",
    "# ytm_tagged: ~1h runtime\n",
    "#    141,476 hours of ytm_tagged\n",
    "# musescore: ~5m runtime\n",
    "#        103 hours of ytm_tagged\n",
    "# ytm_mb: ~12h runtime\n",
    "#  1,642,164 hours of ytm_mb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d169a93b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.847855Z",
     "start_time": "2024-01-20T19:58:58.383386Z"
    }
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'BREAK' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mBREAK\u001b[49m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'BREAK' is not defined"
     ]
    }
   ],
   "source": [
    "BREAK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "efcc774c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.849179Z",
     "start_time": "2024-01-20T19:58:58.849170Z"
    }
   },
   "outputs": [],
   "source": [
    "# freq info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1c95d44",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.850027Z",
     "start_time": "2024-01-20T19:58:58.850018Z"
    }
   },
   "outputs": [],
   "source": [
    "# with open(\"/app/suno/data/preprocessing/meta_cutoff_freq.json\", \"r\") as fp:\n",
    "#     meta_cutoff_freq = json.load(fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ab57df8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.850886Z",
     "start_time": "2024-01-20T19:58:58.850878Z"
    }
   },
   "outputs": [],
   "source": [
    "# verify\n",
    "mm = np.memmap(os.path.join(OUT_DATA_DIR, \"data_val.bin\"), dtype=np.uint16, mode=\"r\")\n",
    "metas = read_jsonl(os.path.join(OUT_DATA_DIR,\"metas_val.jsonl\"))\n",
    "mm = mm.reshape(-1, 3008, 13)\n",
    "assert(len(mm) == len(metas))\n",
    "assert(mm[:100,:,0].min() >= 0)\n",
    "assert(mm[:100,:,0].max() <= 4000)\n",
    "assert(mm[:100,:,1:].min() >= 0)\n",
    "assert(mm[:100,:,1:].max() <= 4096)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cfaf6050",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.851438Z",
     "start_time": "2024-01-20T19:58:58.851430Z"
    }
   },
   "outputs": [],
   "source": [
    "metas[797]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cd8eb63",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.852219Z",
     "start_time": "2024-01-20T19:58:58.852211Z"
    }
   },
   "outputs": [],
   "source": [
    "# # verify\n",
    "# mm = np.memmap(os.path.join(OUT_DATA_DIR, \"data_tr.bin\"), dtype=np.uint16, mode=\"r\")\n",
    "# metas = read_jsonl(os.path.join(OUT_DATA_DIR,\"metas_tr.jsonl\"))\n",
    "# mm = mm.reshape(-1, 3008, 13)\n",
    "# assert(len(mm) == len(metas))\n",
    "# assert(mm[:100,:,0].min() >= 0)\n",
    "# assert(mm[:100,:,0].max() <= 4000)\n",
    "# assert(mm[:100,:,1:].min() >= 0)\n",
    "# assert(mm[:100,:,1:].max() <= 4096)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "814a34af",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.852936Z",
     "start_time": "2024-01-20T19:58:58.852928Z"
    }
   },
   "outputs": [],
   "source": [
    "# for meta in metas:\n",
    "#     if meta[\"id\"] in meta_cutoff_freq:\n",
    "#         meta[\"cutoff_freq\"] = meta_cutoff_freq[meta[\"id\"]]\n",
    "\n",
    "# write_jsonl(metas, os.path.join(OUT_DATA_DIR,\"metas_tr.jsonl\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5bdf8ac",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.853955Z",
     "start_time": "2024-01-20T19:58:58.853943Z"
    }
   },
   "outputs": [],
   "source": [
    "# !du -hs /mnt/data/georg/data/chirp_v2/data_tr.bin\n",
    "# # 1003G"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cbcbca32",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.855349Z",
     "start_time": "2024-01-20T19:58:58.855341Z"
    }
   },
   "outputs": [],
   "source": [
    "# randomly listen to some stuff\n",
    "from suno_utils.tasks.dac_2c_12cb import preload_models as preload_codec_models\n",
    "from suno_utils.tasks.dac_2c_12cb import (\n",
    "    encode as codec_encode,\n",
    "    decode as codec_decode,\n",
    "    EMBEDDING_RATE as CODEC_EMBEDDING_RATE,\n",
    ")\n",
    "\n",
    "_ = preload_codec_models(\"/app/suno/tony/v3/dac_2c_25x12.pt\")\n",
    "mm = np.memmap(os.path.join(OUT_DATA_DIR, \"data_val.bin\"), dtype=np.uint16, mode=\"r\")\n",
    "mm = mm.reshape(-1, 3008, 13)\n",
    "test_metas = read_jsonl(os.path.join(OUT_DATA_DIR, \"metas_val.jsonl\"))\n",
    "test_info = read_json(os.path.join(OUT_DATA_DIR, \"info_val.json\"))\n",
    "assert len(test_metas) == len(mm)\n",
    "idx_list = list(range(len(test_metas)))\n",
    "# random.shuffle(idx_list)\n",
    "# idx_list = [idx for idx in idx_list if \"text\" in test_metas[idx]]\n",
    "print(len(mm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7008ddc7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.856171Z",
     "start_time": "2024-01-20T19:58:58.856162Z"
    }
   },
   "outputs": [],
   "source": [
    "# show text and audio\n",
    "# idx = random.choice(idx_list)\n",
    "# idx = random.choice(test_info[\"pond5_music\"][\"idx_list\"])\n",
    "idx = random.choice(test_info[\"genius_hq_lyrics\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"musescore_lyrics\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"deezer_lyrics\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"youtube_music_lyrics_foreign\"][\"idx_list\"])\n",
    "# idx_key = random.choice(list(test_info.keys()))\n",
    "# print(idx_key)\n",
    "# idx = random.choice(test_info[idx_key][\"idx_list\"])\n",
    "assert \"original_duration_s\" in test_metas[idx]\n",
    "print(\"tags:\", test_metas[idx].get(\"tags\"))\n",
    "arr = mm[idx, 1:].copy().astype(np.int16)[:, 1:]\n",
    "pad_idx_arr = np.where(arr == 4096)[0]\n",
    "if len(pad_idx_arr) > 0:\n",
    "    arr = arr[: pad_idx_arr[0], :]\n",
    "a = codec_decode(arr)\n",
    "a.play(compress=False)\n",
    "print(\"text:\", test_metas[idx].get(\"text\"))\n",
    "plt.plot(a.array_float[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f818ab33",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.856882Z",
     "start_time": "2024-01-20T19:58:58.856874Z"
    }
   },
   "outputs": [],
   "source": [
    "test_metas[idx]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87b856f6",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "62fb16fc",
   "metadata": {},
   "source": [
    "### try same with train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f987040e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.857807Z",
     "start_time": "2024-01-20T19:58:58.857800Z"
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4471a7fb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.858733Z",
     "start_time": "2024-01-20T19:58:58.858724Z"
    }
   },
   "outputs": [],
   "source": [
    "# randomly listen to some stuff\n",
    "from suno_utils.tasks.dac_2c_12cb import preload_models as preload_codec_models\n",
    "from suno_utils.tasks.dac_2c_12cb import (\n",
    "    encode as codec_encode,\n",
    "    decode as codec_decode, \n",
    "    EMBEDDING_RATE as CODEC_EMBEDDING_RATE,\n",
    ")\n",
    "from suno_utils.utils.text import read_jsonl, read_json\n",
    "_ = preload_codec_models(\"/app/suno/models/dac_2c_25x12.pt\")\n",
    "mm = np.memmap(\"/mnt/localdisk/data/data_tr.bin\", dtype=np.uint16, mode=\"r\")\n",
    "mm = mm.reshape(-1, 3008, 13)\n",
    "test_metas = read_jsonl(\"/mnt/localdisk/data/metas_tr.jsonl\")\n",
    "test_info = read_json(\"/mnt/localdisk/data/info_tr.json\")\n",
    "assert(len(test_metas) == len(mm))\n",
    "idx_list = list(range(len(test_metas)))\n",
    "#random.shuffle(idx_list)\n",
    "#idx_list = [idx for idx in idx_list if \"text\" in test_metas[idx]]\n",
    "print(len(mm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecb73f42",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.859387Z",
     "start_time": "2024-01-20T19:58:58.859380Z"
    }
   },
   "outputs": [],
   "source": [
    "import random\n",
    "# show text and audio\n",
    "# idx = random.choice(idx_list)\n",
    "# idx = random.choice(test_info[\"pond5_music\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"genius_hq_lyrics\"][\"idx_list\"])\n",
    "idx = random.choice(test_info[\"ytm_mb\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"musescore_lyrics\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"deezer_lyrics\"][\"idx_list\"])\n",
    "# idx = random.choice(test_info[\"youtube_music_lyrics_foreign\"][\"idx_list\"])\n",
    "# idx_key = random.choice(list(test_info.keys()))\n",
    "# print(idx_key)\n",
    "# idx = random.choice(test_info[idx_key][\"idx_list\"])\n",
    "assert(\"original_duration_s\" in test_metas[idx])\n",
    "print(\"tags:\", test_metas[idx].get(\"tags\"))\n",
    "arr = mm[idx,1:].copy().astype(np.int16)[:,1:]\n",
    "pad_idx_arr = np.where(arr == 4096)[0]\n",
    "if len(pad_idx_arr) > 0:\n",
    "    arr = arr[:pad_idx_arr[0],:]\n",
    "a = codec_decode(arr)\n",
    "a.play(compress=False)\n",
    "print(\"text:\", test_metas[idx].get(\"text\"))\n",
    "plt.plot(a.array_float[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee88a2eb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1d977073",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2653b41",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "067a561b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73c92bda",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "08665ec0",
   "metadata": {},
   "source": [
    "## Playground"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "747f0363",
   "metadata": {},
   "source": [
    "#### get all tags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73286512",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.859964Z",
     "start_time": "2024-01-20T19:58:58.859956Z"
    }
   },
   "outputs": [],
   "source": [
    "test_metas = read_jsonl(\"/mnt/data/georg/data/chirp_v2/metas_val.jsonl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a3ed7922",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.861012Z",
     "start_time": "2024-01-20T19:58:58.861004Z"
    }
   },
   "outputs": [],
   "source": [
    "tags = []\n",
    "for e in train_metas:\n",
    "    tags.extend(e.get(\"tags\", []))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eed69a8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.862072Z",
     "start_time": "2024-01-20T19:58:58.862065Z"
    }
   },
   "outputs": [],
   "source": [
    "vc_tags = pd.Series(tags).value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf1c83e8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.862932Z",
     "start_time": "2024-01-20T19:58:58.862924Z"
    }
   },
   "outputs": [],
   "source": [
    "vc_tags.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f76f1e83",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.863509Z",
     "start_time": "2024-01-20T19:58:58.863502Z"
    }
   },
   "outputs": [],
   "source": [
    "vc_tags = vc_tags.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6766efb1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.864298Z",
     "start_time": "2024-01-20T19:58:58.864290Z"
    }
   },
   "outputs": [],
   "source": [
    "vc_tags.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fece8109",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.865008Z",
     "start_time": "2024-01-20T19:58:58.865000Z"
    }
   },
   "outputs": [],
   "source": [
    "a = vc_tags[vc_tags[\"count\"]>=3][\"count\"].to_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "52e1a982",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.865685Z",
     "start_time": "2024-01-20T19:58:58.865677Z"
    }
   },
   "outputs": [],
   "source": [
    "test_metas = read_jsonl(\"/mnt/data/georg/data/chirp_v2/metas_val.jsonl\")\n",
    "test_info = read_json(\"/mnt/data/georg/data/chirp_v2/info_val.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6b9f19e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-20T19:58:58.866479Z",
     "start_time": "2024-01-20T19:58:58.866471Z"
    }
   },
   "outputs": [],
   "source": [
    "idx = random.choice(test_info[\"musescore_lyrics\"][\"idx_list\"])\n",
    "print(idx)\n",
    "m = test_metas[idx]\n",
    "re.sub(r\"\\[.*?\\]\", \"\", m[\"text\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5dffab2d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4531aac2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0e1aafb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "143ac0a7",
   "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.10.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
