{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/christian/miniconda3/envs/suno_env/lib/python3.10/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",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WILL USE FLASH ATTN: True\n",
      "Failed to import Bio.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import json\n",
    "import random\n",
    "import uuid\n",
    "import torchaudio\n",
    "import numpy as np\n",
    "import torch.nn.functional as F\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.gpt.chirp_v2_5 import (\n",
    "    GenerationConfig,\n",
    "    decode_stream,\n",
    "    preload_models,\n",
    "    prep_gconf,\n",
    "    codec_decode_stream_to_full_audio,\n",
    "    generate,\n",
    ")\n",
    "from suno_utils.gpt.generation_engine import (\n",
    "    align_codes,\n",
    "    make_request,\n",
    ")\n",
    "from suno_utils.gpt.engine import Engine\n",
    "import torch\n",
    "from tqdm import tqdm\n",
    "\n",
    "from stable_audio_tools.inference.generation import (\n",
    "    upsample_diffusion,\n",
    "    upsample_diffusion_from_codes,\n",
    ")\n",
    "from stable_audio_tools.interface.gradio import load_model\n",
    "from stable_audio_tools.models.utils import apply_normalization\n",
    "\n",
    "\n",
    "from suno_utils.tasks.dac_2c_12cb import DAC\n",
    "\n",
    "# MERT\n",
    "from suno_utils.tasks.mert_25 import (\n",
    "    preload_models as preload_semantic_models,\n",
    "    encode as semantic_encode,\n",
    ")\n",
    "\n",
    "# VAE\n",
    "from suno_utils.tasks.dac_vae_peaq import (\n",
    "    preload_models as preload_vae_models,\n",
    "    encode as vae_encode,\n",
    "    decode as vae_decode,\n",
    ")\n",
    "\n",
    "# DAC\n",
    "from suno_utils.tasks.dac_2c_12cb import (\n",
    "    preload_models as preload_codec_models,\n",
    "    encode as codec_encode,\n",
    "    decode as codec_decode,\n",
    ")\n",
    "\n",
    "N_BATCH = 2\n",
    "MAX_STREAMS = N_BATCH * 4\n",
    "\n",
    "\n",
    "def remove_pad_tokens(arr, value: int = 2048):\n",
    "    # Find the last index where the value is not equal to the specified value\n",
    "    non_value_index = np.where(arr != value)[0]\n",
    "    print(non_value_index)\n",
    "\n",
    "    if non_value_index.size == 0:\n",
    "        # If there are no values that are not equal to the specified value, return an empty array\n",
    "        return np.array([], dtype=arr.dtype)\n",
    "\n",
    "    # Get the last index where the value is not equal to the specified value\n",
    "    last_non_value_index = non_value_index[-1]\n",
    "    print(last_non_value_index)\n",
    "\n",
    "    # Slice the array to remove trailing values\n",
    "    return arr[: last_non_value_index]\n",
    "\n",
    "\n",
    "def tensorize(x):\n",
    "    if isinstance(x, np.ndarray):\n",
    "        x = torch.from_numpy(x.astype(np.int64))\n",
    "    assert isinstance(x, torch.Tensor)\n",
    "    if x.ndim == 1:\n",
    "        x = x.unsqueeze(0)\n",
    "\n",
    "    return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model loaded: /app/suno/checkpoints/2024-08-11_18-28-59/last_ckpt_infer.pt\n",
      "model loaded: 32020.4M params\n"
     ]
    },
    {
     "ename": "OutOfMemoryError",
     "evalue": "CUDA out of memory. Tried to allocate 264.00 MiB. GPU ",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mOutOfMemoryError\u001b[0m                          Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[3], line 28\u001b[0m\n\u001b[1;32m     25\u001b[0m gpt_30b_ft_ckpt_path \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m/app/suno/checkpoints/2024-08-11_18-28-59/last_ckpt_infer.pt\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;66;03m# genius filtered\u001b[39;00m\n\u001b[1;32m     26\u001b[0m \u001b[38;5;66;03m#tokenizer_path = \"/app/suno/data/chirp_v4_ft/base_v2/tokenizer_60k.json\" # genius_hq tokenizer (don't use)\u001b[39;00m\n\u001b[0;32m---> 28\u001b[0m \u001b[43mpreload_models\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     29\u001b[0m \u001b[43m    \u001b[49m\u001b[43mgpt_ckpt_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mgpt_30b_ft_ckpt_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     30\u001b[0m \u001b[43m    \u001b[49m\u001b[43mtokenizer_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtokenizer_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     31\u001b[0m \u001b[43m    \u001b[49m\u001b[43mload_gpt\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m     32\u001b[0m \u001b[43m    \u001b[49m\u001b[43mload_semantic\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m     33\u001b[0m \u001b[43m    \u001b[49m\u001b[43mload_codec_device\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mcuda\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m     34\u001b[0m \u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/code/glockenspiel/suno_utils/suno_utils/gpt/chirp_v2_5.py:114\u001b[0m, in \u001b[0;36mpreload_models\u001b[0;34m(gpt_ckpt_path, tokenizer_path, semantic_ckpt_path, semantic_centroids_path, codec_ckpt_path, fasttext_ckpt_path, hoot_ckpt_path, hoot_tokenizer_path, load_gpt, load_semantic, load_codec, load_whisper, local_whisper, fetch_only, cache_dir, use_gpu, load_codec_device, load_hoot, load_hoot_device)\u001b[0m\n\u001b[1;32m    112\u001b[0m     gpt_ckpt_path \u001b[38;5;241m=\u001b[39m _get_model_if_needed(gpt_ckpt_path, cache_dir\u001b[38;5;241m=\u001b[39mcache_dir)\n\u001b[1;32m    113\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m fetch_only:\n\u001b[0;32m--> 114\u001b[0m         \u001b[43mpreload_gpt_models\u001b[49m\u001b[43m(\u001b[49m\u001b[43mgpt_ckpt_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtokenizer_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43muse_gpu\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muse_gpu\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    115\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m load_semantic:\n\u001b[1;32m    116\u001b[0m     semantic_ckpt_path \u001b[38;5;241m=\u001b[39m _get_model_if_needed(semantic_ckpt_path, cache_dir\u001b[38;5;241m=\u001b[39mcache_dir)\n",
      "File \u001b[0;32m~/code/glockenspiel/suno_utils/suno_utils/gpt/generation.py:230\u001b[0m, in \u001b[0;36mpreload_models\u001b[0;34m(ckpt_path, tokenizer_path, use_gpu)\u001b[0m\n\u001b[1;32m    229\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpreload_models\u001b[39m(ckpt_path, tokenizer_path, use_gpu\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m--> 230\u001b[0m     _ \u001b[38;5;241m=\u001b[39m \u001b[43mload_model\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    231\u001b[0m \u001b[43m        \u001b[49m\u001b[43mckpt_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mckpt_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    232\u001b[0m \u001b[43m        \u001b[49m\u001b[43mtokenizer_path\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtokenizer_path\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    233\u001b[0m \u001b[43m        \u001b[49m\u001b[43muse_gpu\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muse_gpu\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    234\u001b[0m \u001b[43m        \u001b[49m\u001b[43mforce_reload\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m    235\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/code/glockenspiel/suno_utils/suno_utils/gpt/generation.py:192\u001b[0m, in \u001b[0;36mload_model\u001b[0;34m(ckpt_path, tokenizer_path, use_gpu, force_reload, use_tp)\u001b[0m\n\u001b[1;32m    190\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel not initialized, need checkpoint path. maybe run `preload_models`?\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    191\u001b[0m clean_models(model_key\u001b[38;5;241m=\u001b[39mmodel_key)\n\u001b[0;32m--> 192\u001b[0m model, tokenizer \u001b[38;5;241m=\u001b[39m \u001b[43m_load_model\u001b[49m\u001b[43m(\u001b[49m\u001b[43mckpt_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtokenizer_path\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43muse_tp\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43muse_tp\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    193\u001b[0m models[model_key] \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m    194\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m: model,\n\u001b[1;32m    195\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtokenizer\u001b[39m\u001b[38;5;124m\"\u001b[39m: tokenizer,\n\u001b[1;32m    196\u001b[0m }\n\u001b[1;32m    197\u001b[0m models[model_key][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mto(device)\n",
      "File \u001b[0;32m~/code/glockenspiel/suno_utils/suno_utils/gpt/generation.py:167\u001b[0m, in \u001b[0;36m_load_model\u001b[0;34m(ckpt_path, tokenizer_path, device, use_tp)\u001b[0m\n\u001b[1;32m    164\u001b[0m model\u001b[38;5;241m.\u001b[39mmodel_args \u001b[38;5;241m=\u001b[39m model_args\n\u001b[1;32m    166\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m device \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcuda\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;129;01mand\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mcuda\u001b[38;5;241m.\u001b[39mis_bf16_supported():\n\u001b[0;32m--> 167\u001b[0m     \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbfloat16\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# this should be fine since it was trained with AMP\u001b[39;00m\n\u001b[1;32m    168\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    169\u001b[0m     model\u001b[38;5;241m.\u001b[39mto(device)\n",
      "File \u001b[0;32m~/miniconda3/envs/suno_env/lib/python3.10/site-packages/torch/nn/modules/module.py:1173\u001b[0m, in \u001b[0;36mModule.to\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1170\u001b[0m         \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m   1171\u001b[0m             \u001b[38;5;28;01mraise\u001b[39;00m\n\u001b[0;32m-> 1173\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconvert\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/miniconda3/envs/suno_env/lib/python3.10/site-packages/torch/nn/modules/module.py:779\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    777\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    778\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 779\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    781\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    782\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    789\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n",
      "File \u001b[0;32m~/miniconda3/envs/suno_env/lib/python3.10/site-packages/torch/nn/modules/module.py:779\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    777\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    778\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 779\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    781\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    782\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    789\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n",
      "    \u001b[0;31m[... skipping similar frames: Module._apply at line 779 (2 times)]\u001b[0m\n",
      "File \u001b[0;32m~/miniconda3/envs/suno_env/lib/python3.10/site-packages/torch/nn/modules/module.py:779\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    777\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    778\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 779\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    781\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    782\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    789\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n",
      "File \u001b[0;32m~/miniconda3/envs/suno_env/lib/python3.10/site-packages/torch/nn/modules/module.py:804\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    800\u001b[0m \u001b[38;5;66;03m# Tensors stored in modules are graph leaves, and we don't want to\u001b[39;00m\n\u001b[1;32m    801\u001b[0m \u001b[38;5;66;03m# track autograd history of `param_applied`, so we have to use\u001b[39;00m\n\u001b[1;32m    802\u001b[0m \u001b[38;5;66;03m# `with torch.no_grad():`\u001b[39;00m\n\u001b[1;32m    803\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mno_grad():\n\u001b[0;32m--> 804\u001b[0m     param_applied \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparam\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    805\u001b[0m p_should_use_set_data \u001b[38;5;241m=\u001b[39m compute_should_use_set_data(param, param_applied)\n\u001b[1;32m    807\u001b[0m \u001b[38;5;66;03m# subclasses may have multiple child tensors so we need to use swap_tensors\u001b[39;00m\n",
      "File \u001b[0;32m~/miniconda3/envs/suno_env/lib/python3.10/site-packages/torch/nn/modules/module.py:1159\u001b[0m, in \u001b[0;36mModule.to.<locals>.convert\u001b[0;34m(t)\u001b[0m\n\u001b[1;32m   1152\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m convert_to_format \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m t\u001b[38;5;241m.\u001b[39mdim() \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m):\n\u001b[1;32m   1153\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m t\u001b[38;5;241m.\u001b[39mto(\n\u001b[1;32m   1154\u001b[0m             device,\n\u001b[1;32m   1155\u001b[0m             dtype \u001b[38;5;28;01mif\u001b[39;00m t\u001b[38;5;241m.\u001b[39mis_floating_point() \u001b[38;5;129;01mor\u001b[39;00m t\u001b[38;5;241m.\u001b[39mis_complex() \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m   1156\u001b[0m             non_blocking,\n\u001b[1;32m   1157\u001b[0m             memory_format\u001b[38;5;241m=\u001b[39mconvert_to_format,\n\u001b[1;32m   1158\u001b[0m         )\n\u001b[0;32m-> 1159\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1160\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1161\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_floating_point\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_complex\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m   1162\u001b[0m \u001b[43m        \u001b[49m\u001b[43mnon_blocking\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1163\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1164\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m   1165\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(e) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot copy out of meta tensor; no data!\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
      "\u001b[0;31mOutOfMemoryError\u001b[0m: CUDA out of memory. Tried to allocate 264.00 MiB. GPU "
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"2\"\n",
    "os.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"expandable_segments:True\"\n",
    "\n",
    "timestamp = \"20240701\"\n",
    "exp_name = f\"model_13b_full_ft\"\n",
    "# exp_name = \"model_13b_full\"\n",
    "os.makedirs(f\"outputs/{timestamp}/{exp_name}\", exist_ok=True)\n",
    "\n",
    "# load VAE\n",
    "_ = preload_vae_models(\"/app/suno/checkpoints/dac_mw/mw_vae_peaq_128_fix.pth\")\n",
    "\n",
    "# load DAC\n",
    "_ = preload_codec_models(\n",
    "    checkpoint_filepath=\"s3://suno-data/georg/models/codec/dac_2c_25x12.pt\",\n",
    ")\n",
    "\n",
    "#gpt_ckpt_path = \"/app/suno/data/dpo/models/model_13b_full.pt\"  # model before fine-tuning\n",
    "gpt_ckpt_path = \"/app/suno/data/dpo/models/model_13b_full.pt\" # start of finetuning\n",
    "\n",
    "#gpt_13b_ft_ckpt_path = \"/app/suno/checkpoints/2024-08-08_19-18-28/last_ckpt_infer.pt\"  # fine tune for audio quality control tags\n",
    "#gpt_13b_ft_ckpt_path = \"/app/suno/checkpoints/2024-08-09_20-41-33/last_ckpt_infer.pt\"  # fine tune for audio quality control tags\n",
    "tokenizer_path = \"/app/suno/data/dpo/models/tokenizer_60k.json\"\n",
    "gpt_13b_ft_ckpt_path = \"/app/suno/checkpoints/2024-08-10_11-19-21/best_ckpt.pt\" # genius filtered\n",
    "gpt_30b_ft_ckpt_path = \"/app/suno/checkpoints/2024-08-11_18-28-59/last_ckpt_infer.pt\" # genius filtered\n",
    "#tokenizer_path = \"/app/suno/data/chirp_v4_ft/base_v2/tokenizer_60k.json\" # genius_hq tokenizer (don't use)\n",
    "\n",
    "preload_models(\n",
    "    gpt_ckpt_path=gpt_30b_ft_ckpt_path,\n",
    "    tokenizer_path=tokenizer_path,\n",
    "    load_gpt=True,\n",
    "    load_semantic=False,\n",
    "    load_codec_device=\"cuda\",\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "text_tags_list = [\n",
    "    \"indie-pop, energetic, rock, psychedelic\",\n",
    "    \"jazz, female vocal, smooth\",\n",
    "    \"r&b, soul, funk\",\n",
    "    \"pop, high energy, female vocal\",\n",
    "    \"rock, high energy, male vocal\",\n",
    "    \"indie rock, female vocal, psychedelic\",\n",
    "    \"bluegrass, female vocal, guitar\",\n",
    "    \"moody, blues, soulful\",\n",
    "    \"classical, cinematic, female vocal\",\n",
    "    \"metal, hardcore, dark\",\n",
    "    \"male vocal, bluegrass, guitar\",\n",
    "    \"metal, hardcore, dark\",\n",
    "    \"pop, rock\",\n",
    "    \"heavy rock\"\n",
    "    \"female vocal, voice, singing, guitar, folk, jazz, r&b\",\n",
    "    \"Cyberpunk ballad,  kaleidoscopic funk,  dreamwave fusion,  existential groove\",\n",
    "    \"#Ambient glitch-pop, neo-baroque jazz, surreal groove, vaporwave funk\",\n",
    "    \"Indie-electro,  playful pulse,  starry-eyed synths,  upbeat\",\n",
    "    #\"Jazz-hop,  smooth satire,  cocktail lounge cool,  lyrical swing\",\n",
    "    #\"Lo-fi chillwave,  campfire pop,  mellow bounce,  conversational\",\n",
    "]\n",
    "\n",
    "text = \"\"\"\n",
    "[verse]\n",
    "Almost Heaven, West Virginia\n",
    "Blue Ridge Mountains, Shenandoah River\n",
    "Life is old there, older than the trees\n",
    "Younger than the mountains, growing like a breeze\n",
    "\n",
    "Country roads, take me home\n",
    "To the place I belong\n",
    "West Virginia, mountain mama\n",
    "Take me home, country roads\n",
    "\"\"\"\n",
    "\n",
    "text = \"\"\"\n",
    "[verse]\n",
    "oh, my love\n",
    "My friend you know\n",
    "it's been a while\n",
    "Without thinking of you\n",
    "but the thought makes me smile\n",
    "\n",
    "[chorus]\n",
    "I'm so tired of wanting\n",
    "wanting more than this\n",
    "i know it but what am i to do\n",
    "i need some space to breathe,\n",
    "so give me some room\n",
    "\n",
    "[verse]\n",
    "oh, my love\n",
    "you have a heart of stone\n",
    "cause since i've come home\n",
    "i've never felt so alone\n",
    "but the thought makes me smile\n",
    "\n",
    "[chorus]\n",
    "I'm so tired of wanting\n",
    "wanting more than this\n",
    "i know it but what am i to do\n",
    "i need some space to breathe,\n",
    "so give me some room\n",
    "\"\"\"\n",
    "\n",
    "temps_coarse = [0.8, 0.9, 0.95, 1.0, 1.1]\n",
    "\n",
    "# generate uuid\n",
    "uid = uuid.uuid4()\n",
    "\n",
    "temp_coarse = random.choice(temps_coarse)\n",
    "\n",
    "# generate high quality and low quality\n",
    "\n",
    "for n in range(5):\n",
    "    random_seed = np.random.randint(0, 1000000)\n",
    "    text_tags = random.choice(text_tags_list)\n",
    "    print(random_seed)\n",
    "\n",
    "    for quality in [\"standard\", \"high\"]:\n",
    "        print(f\"Tags: {text_tags}  Quality: {quality}\")\n",
    "        #text_start_control_tags = \"{start;start:0;vocals:start;audio_quality:\" + f\"{quality}\" + \"}\"\n",
    "        text_start_control_tags = \"{start;audio_quality:\" + f\"{quality}\" + \"}\"\n",
    "        #text_start_control_tags = \"{start:\" + \"}\"\n",
    "        print(text_start_control_tags)\n",
    "\n",
    "        general_config = dict(\n",
    "            #cfg_coef=3.0,  # no text cfg for dpo stream\n",
    "            #min_eos_p=0.1,\n",
    "            #eos_pad_duration_s=0,\n",
    "            #cfg_coef=2.0,\n",
    "            #cfg_coef_tags=2,\n",
    "            #n_repeat_tags=3,\n",
    "            # cfg_coef_tags_max_steps=None,  # collect the data for now\n",
    "            #cfg_coef_tags=0,\n",
    "            #cfg_coef_neg_tags=0,\n",
    "            #text_neg_tags=\"repetitive, loop, noise, noisy\",\n",
    "            use_whisper=False,\n",
    "            text_start_control_tags=text_start_control_tags,\n",
    "            #text_end_control_tags=\"{end}\",\n",
    "            random_seed=random_seed,\n",
    "            n_batch=1,\n",
    "        )\n",
    "\n",
    "        #temp_coarse_tensor = torch.tensor(\n",
    "        #    [0.9, 1.0, 1.25, 1.5, 0.6, 0.5, 0.4, 0.3, 0.3, 0.3, 0.3, 0.3]\n",
    "        #).view(1, 12, 1)\n",
    "\n",
    "        gconfig = GenerationConfig(\n",
    "            text=text,\n",
    "            text_tags=text_tags,\n",
    "            max_gen_duration_s=240,\n",
    "            **general_config,\n",
    "            return_semantic=True,\n",
    "            temp_coarse=1.0,\n",
    "            #top_k_coarse=None,\n",
    "            #top_p_coarse=None,\n",
    "        )\n",
    "\n",
    "        semantic_codes, codec_codes = generate(gconfig)\n",
    "        semantic_codes = torch.from_numpy(semantic_codes[0]).squeeze()\n",
    "        #codec_codes = torch.from_numpy(codec_codes[0])\n",
    "        codec_codes = codec_codes[0]\n",
    "        # convert dac tokens back to audio\n",
    "        audio = codec_decode(codec_codes)\n",
    "        audio.play()\n",
    "        #audio = torch.from_numpy(audio.array_float)\n",
    "        #audio /= audio.abs().max()\n",
    "        #audio = apply_normalization(audio, 48000, target_loudness_lufs_db=-16.0)\n",
    "        #gpt_out = os.path.join(\"outputs\", timestamp, exp_name, f\"{uid}-output.wav\")\n",
    "        #torchaudio.save(gpt_out, audio, 48000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env",
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
