{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b8da5d39",
   "metadata": {},
   "source": [
    "# Use modal inference workers to generate predictions :)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1a9f6366",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T03:53:17.570097Z",
     "start_time": "2024-04-25T03:53:17.568631Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-24T13:53:52.016677Z",
     "iopub.status.busy": "2025-02-24T13:53:52.016386Z",
     "iopub.status.idle": "2025-02-24T13:53:52.020836Z",
     "shell.execute_reply": "2025-02-24T13:53:52.020494Z",
     "shell.execute_reply.started": "2025-02-24T13:53:52.016660Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3f84f1f3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T03:53:20.332452Z",
     "start_time": "2024-04-25T03:53:17.926936Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-24T13:53:52.206920Z",
     "iopub.status.busy": "2025-02-24T13:53:52.206637Z",
     "iopub.status.idle": "2025-02-24T13:53:58.649023Z",
     "shell.execute_reply": "2025-02-24T13:53:58.648374Z",
     "shell.execute_reply.started": "2025-02-24T13:53:52.206905Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tony/anaconda3/envs/suno_env_dev/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "import tqdm\n",
    "import math\n",
    "import torch\n",
    "import random\n",
    "import funcy\n",
    "import copy\n",
    "import gc\n",
    "import re\n",
    "import json\n",
    "import tempfile\n",
    "import collections\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import fasttext\n",
    "from joblib import Parallel, delayed\n",
    "import modal\n",
    "from uuid import uuid4\n",
    "import random\n",
    "\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.utils.display import capture_output\n",
    "from suno_utils.utils.text import (\n",
    "    write_jsonl,\n",
    "    read_jsonl,\n",
    "    write_json,\n",
    "    read_json,\n",
    "    normalize_whitespace,\n",
    ")\n",
    "from suno_utils.utils.s3 import (\n",
    "    read_from_s3,\n",
    "    check_s3_file_exists,\n",
    "    open_from_s3,\n",
    "    download_s3_files,\n",
    ")\n",
    "from suno_utils.utils.tokenizers import tokenize\n",
    "from suno_utils.harvest.youtube.constants.text_lang import BASE_TO_FASTTEXT_REMAP\n",
    "\n",
    "\n",
    "pd.set_option(\"display.max_rows\", 500)\n",
    "pd.set_option(\"display.max_columns\", 500)\n",
    "pd.set_option(\"display.width\", 1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54c17952-41f9-4237-b06a-aee786c810c1",
   "metadata": {},
   "source": [
    "# Regenerate audio from prompt using workers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33afd417",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T03:54:46.541696Z",
     "start_time": "2024-04-25T03:54:46.539855Z"
    }
   },
   "outputs": [],
   "source": [
    "# OUT_DATA_DIR = \"/app/suno/data/dpo/top_mix\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d406229",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:18:01.615588Z",
     "start_time": "2024-04-25T04:18:01.612274Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_gen_from_meta(meta_dict, index=0):\n",
    "    uid = str(uuid4())\n",
    "    new_id = uid + \"_gen_\" + str(index)\n",
    "    tags = meta_dict.get(\"tags\", [])\n",
    "    random.shuffle(tags)\n",
    "    return dict(\n",
    "        id=new_id,\n",
    "        prompt_text=meta_dict.get(\"text\", \"\"),\n",
    "        metadata={\"tags\": \", \".join(tags)},\n",
    "        model_name=\"chirp-v3-engine-v0\",\n",
    "        title=new_id,\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c970dcf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:17:26.740286Z",
     "start_time": "2024-04-25T04:17:26.737753Z"
    }
   },
   "outputs": [],
   "source": [
    "mm = np.memmap(os.path.join(OUT_DATA_DIR, f\"data_val.bin\"), dtype=np.uint16, mode=\"r\")\n",
    "test_metas = read_jsonl(os.path.join(OUT_DATA_DIR, f\"metas_val.jsonl\"))\n",
    "test_info = read_json(os.path.join(OUT_DATA_DIR, f\"info_val.json\"))\n",
    "mm = mm.reshape(-1, 3008, 13)\n",
    "assert len(mm) == len(test_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() <= 2048\n",
    "len(test_metas)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9ece8a7",
   "metadata": {},
   "source": [
    "# Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a700ab4f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:06:33.043946Z",
     "start_time": "2024-04-25T04:06:33.041365Z"
    }
   },
   "outputs": [],
   "source": [
    "test_input = get_gen_from_meta(test_metas[0])\n",
    "f = modal.Function.lookup(\n",
    "    \"engine-chirpv2_engine_7b_dev_120s_novid\", \"ChirpV2Stub.generate\"\n",
    ")\n",
    "test_input"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "41528872",
   "metadata": {},
   "source": [
    "# Bulk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cf6e9d5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:18:11.482514Z",
     "start_time": "2024-04-25T04:18:11.480329Z"
    }
   },
   "outputs": [],
   "source": [
    "print(len(test_metas))\n",
    "val_inputs = []\n",
    "for i, test_meta in enumerate(test_metas):\n",
    "    val_inputs.append(get_gen_from_meta(test_meta, i))\n",
    "print(val_inputs[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3911e7e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:19:10.287746Z",
     "start_time": "2024-04-25T04:19:10.272848Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(os.path.join(OUT_DATA_DIR, \"gen_val.json\"), \"w\") as fp:\n",
    "    json.dump(val_inputs, fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3d0e8adc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:20:20.419321Z",
     "start_time": "2024-04-25T04:20:05.130227Z"
    }
   },
   "outputs": [],
   "source": [
    "for val_input in tqdm.tqdm(val_inputs):\n",
    "    fn_call = f.spawn(json.dumps(val_input))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a35b17a1-1a1c-46cc-8415-bcdb83326d01",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:20:51.076455Z",
     "start_time": "2024-04-25T04:20:47.005512Z"
    }
   },
   "outputs": [],
   "source": [
    "train_metas = read_jsonl(os.path.join(OUT_DATA_DIR, f\"metas_tr.jsonl\"))\n",
    "\n",
    "train_inputs = []\n",
    "for i, test_meta in enumerate(train_metas):\n",
    "    train_inputs.append(get_gen_from_meta(test_meta, i))\n",
    "print(train_inputs[-1])\n",
    "print(len(train_inputs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad3f30ae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:21:21.914769Z",
     "start_time": "2024-04-25T04:21:20.065862Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(os.path.join(OUT_DATA_DIR, \"gen_tr.json\"), \"w\") as fp:\n",
    "    json.dump(train_inputs, fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "829080ce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T08:23:16.935812Z",
     "start_time": "2024-04-25T04:22:31.050478Z"
    }
   },
   "outputs": [],
   "source": [
    "for tr_input in tqdm.tqdm(train_inputs):\n",
    "    fn_call = f.spawn(json.dumps(tr_input))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d563c63-7cab-4ca9-b826-8ac08a660b3a",
   "metadata": {},
   "source": [
    "## Get the npzs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33833099",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T12:39:12.854799Z",
     "start_time": "2024-04-25T12:39:12.851631Z"
    }
   },
   "outputs": [],
   "source": [
    "NPZ_DIR = \"/app/suno/data/dpo/gen_npz\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "597a686f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T12:40:39.389958Z",
     "start_time": "2024-04-25T12:40:39.375459Z"
    }
   },
   "outputs": [],
   "source": [
    "s3_ids = [test_input[\"id\"] for test_input in train_inputs + val_inputs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f8d14826",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T12:40:40.091648Z",
     "start_time": "2024-04-25T12:40:40.046511Z"
    }
   },
   "outputs": [],
   "source": [
    "s3_paths = [f\"s3://suno-data-uploads/studio/uploads/{s3_id}.npz\" for s3_id in s3_ids]\n",
    "local_paths = [f\"{NPZ_DIR}/{s3_id}.npz\" for s3_id in s3_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc1b535c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T12:40:42.602672Z",
     "start_time": "2024-04-25T12:40:42.436523Z"
    }
   },
   "outputs": [],
   "source": [
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0fee83f5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T14:16:07.886228Z",
     "start_time": "2024-04-25T12:41:21.754521Z"
    }
   },
   "outputs": [],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=48)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4597ea67",
   "metadata": {},
   "source": [
    "# Recode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "357f4dc4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:27:11.687332Z",
     "start_time": "2024-04-25T20:27:11.684067Z"
    }
   },
   "outputs": [],
   "source": [
    "val_inputs[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b168e45",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:28:34.318231Z",
     "start_time": "2024-04-25T20:28:34.315899Z"
    },
    "execution": {
     "iopub.execute_input": "2024-06-30T22:27:03.990235Z",
     "iopub.status.busy": "2024-06-30T22:27:03.989674Z",
     "iopub.status.idle": "2024-06-30T22:27:03.992724Z",
     "shell.execute_reply": "2024-06-30T22:27:03.992298Z",
     "shell.execute_reply.started": "2024-06-30T22:27:03.990215Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_cycle_from_meta(meta_dict):\n",
    "    curr_id = meta_dict[\"id\"]\n",
    "    return {\n",
    "        \"id\": curr_id.replace(\"_gen_\", \"_gen_cycle_\"),\n",
    "        \"prompt_audio\": curr_id,\n",
    "        \"prompt_text\": \"\",\n",
    "        \"metadata\": {},\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2921c9e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:28:42.538947Z",
     "start_time": "2024-04-25T20:28:42.536792Z"
    }
   },
   "outputs": [],
   "source": [
    "val_cycle_inputs = []\n",
    "for i, test_meta in enumerate(val_inputs):\n",
    "    val_cycle_inputs.append(get_cycle_from_meta(test_meta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d113da7c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:30:12.624668Z",
     "start_time": "2024-04-25T20:30:12.471851Z"
    }
   },
   "outputs": [],
   "source": [
    "f_cycle = modal.Function.lookup(\"cycle-dev\", \"CycleStub.cycle_code\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "227807c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:30:53.790020Z",
     "start_time": "2024-04-25T20:30:32.911650Z"
    },
    "execution": {
     "iopub.execute_input": "2024-06-30T22:52:18.968587Z",
     "iopub.status.busy": "2024-06-30T22:52:18.968250Z",
     "iopub.status.idle": "2024-06-30T22:52:18.983233Z",
     "shell.execute_reply": "2024-06-30T22:52:18.982657Z",
     "shell.execute_reply.started": "2024-06-30T22:52:18.968569Z"
    }
   },
   "outputs": [],
   "source": [
    "for val_input in tqdm.tqdm(val_cycle_inputs):\n",
    "    fn_call = f_cycle.spawn(json.dumps(val_input), True, True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86b649de",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:39:31.578324Z",
     "start_time": "2024-04-25T20:39:31.467655Z"
    }
   },
   "outputs": [],
   "source": [
    "tr_cycle_inputs = []\n",
    "for i, test_meta in enumerate(train_inputs):\n",
    "    tr_cycle_inputs.append(get_cycle_from_meta(test_meta))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6aae9c6b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T01:19:54.249631Z",
     "start_time": "2024-04-25T20:40:57.558287Z"
    }
   },
   "outputs": [],
   "source": [
    "for val_input in tqdm.tqdm(tr_cycle_inputs):\n",
    "    fn_call = f_cycle.spawn(json.dumps(val_input))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9249e465",
   "metadata": {},
   "outputs": [],
   "source": [
    "tr_cycle_json_inputs = [json.dumps(test_input) for test_input in tr_cycle_inputs]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e61d11f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:36:22.119164Z",
     "start_time": "2024-04-25T20:36:22.116309Z"
    }
   },
   "outputs": [],
   "source": [
    "len(tr_cycle_json_inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5943fcfe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T20:37:47.259814Z",
     "start_time": "2024-04-25T20:37:47.256874Z"
    }
   },
   "outputs": [],
   "source": [
    "f_cycle.map(tr_cycle_json_inputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f1a5138",
   "metadata": {},
   "outputs": [],
   "source": [
    "# for val_input in tqdm.tqdm(tr_cycle_inputs):\n",
    "#     fn_call = f_cycle.map(json.dumps(val_input))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6c3d39a",
   "metadata": {},
   "source": [
    "# CODEC/MERT Recode a slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ef1aa37",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-26T01:19:54.252995Z",
     "start_time": "2024-04-26T01:19:54.251090Z"
    },
    "execution": {
     "iopub.execute_input": "2025-03-02T13:45:36.684101Z",
     "iopub.status.busy": "2025-03-02T13:45:36.683706Z",
     "iopub.status.idle": "2025-03-02T13:45:55.412639Z",
     "shell.execute_reply": "2025-03-02T13:45:55.411932Z",
     "shell.execute_reply.started": "2025-03-02T13:45:36.684085Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice = pd.read_csv(\"/home/tony/Data/Preference/30b_v2/interesting_clips_v4_t_3_20240902_slice.csv\")\n",
    "# df_slice = pd.read_csv(\"/home/tony/Data/Preference/13b_v0/interesting_clips_20240528_slice.csv\")\n",
    "df_slice = pd.read_pickle(\n",
    "    \"/home/tony/Data/Preference/13b_v32/interesting_clips_v4_h_s_32_20250226_full_long_final.pkl\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23230b83-6b9d-454a-8277-3fccdba049a2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:45:55.417885Z",
     "iopub.status.busy": "2025-03-02T13:45:55.417584Z",
     "iopub.status.idle": "2025-03-02T13:45:55.490720Z",
     "shell.execute_reply": "2025-03-02T13:45:55.490163Z",
     "shell.execute_reply.started": "2025-03-02T13:45:55.417871Z"
    }
   },
   "outputs": [],
   "source": [
    "# # find only the positive, and the popular positive\n",
    "# positive_selection_mask = ((df_slice[\"preference\"] == True) &\n",
    "# (df_slice[\"reaction_play_count\"] >= 10) &\n",
    "# (df_slice[\"upvote_count\"] >= 1) &\n",
    "# (df_slice[\"duration\"] < 235)&\n",
    "# (df_slice[\"duration\"] > 60))\n",
    "# df_slice[positive_selection_mask].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "721fc70e-171c-418d-b098-aaab6bd46595",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:45:55.491765Z",
     "iopub.status.busy": "2025-03-02T13:45:55.491352Z",
     "iopub.status.idle": "2025-03-02T13:45:55.493798Z",
     "shell.execute_reply": "2025-03-02T13:45:55.493400Z",
     "shell.execute_reply.started": "2025-03-02T13:45:55.491751Z"
    }
   },
   "outputs": [],
   "source": [
    "GEN_POST_STR = \"_gen_cycle\"\n",
    "NPZ_DIR = \"/app/suno/data/dpo/concat_cycle_npz\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "42c0499b-a26b-48d4-ac57-0ba07a227450",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:45:55.495204Z",
     "iopub.status.busy": "2025-03-02T13:45:55.494879Z",
     "iopub.status.idle": "2025-03-02T13:45:55.612722Z",
     "shell.execute_reply": "2025-03-02T13:45:55.612105Z",
     "shell.execute_reply.started": "2025-03-02T13:45:55.495190Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "735514\n"
     ]
    }
   ],
   "source": [
    "# use the df_slice's ids\n",
    "input_ids = df_slice[\"s3_id\"].unique()\n",
    "print(len(input_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a969538-683a-4003-86cb-8972df9c84ce",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:45:55.613728Z",
     "iopub.status.busy": "2025-03-02T13:45:55.613475Z",
     "iopub.status.idle": "2025-03-02T13:46:19.620726Z",
     "shell.execute_reply": "2025-03-02T13:46:19.620083Z",
     "shell.execute_reply.started": "2025-03-02T13:45:55.613714Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 2620 2620 2620\n"
     ]
    }
   ],
   "source": [
    "s3_paths = [\n",
    "    f\"s3://suno-data-uploads/studio/uploads/{s3_id}{GEN_POST_STR}.npz\"\n",
    "    for s3_id in input_ids\n",
    "]\n",
    "local_paths = [f\"{NPZ_DIR}/{s3_id}{GEN_POST_STR}.npz\" for s3_id in input_ids]\n",
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "finished_ids_set = set(\n",
    "    [i.replace(f\"{GEN_POST_STR}.npz\", \"\") for i in finished_paths_set]\n",
    ")\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_ids = [i for i in input_ids if i not in finished_ids_set]\n",
    "print(\n",
    "    \"jobs to be done\",\n",
    "    len(unfinished_paths),\n",
    "    len(unfinished_s3_paths),\n",
    "    len(unfinished_ids),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20f4a297",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:46:19.622134Z",
     "iopub.status.busy": "2025-03-02T13:46:19.621839Z",
     "iopub.status.idle": "2025-03-02T13:46:19.865680Z",
     "shell.execute_reply": "2025-03-02T13:46:19.865047Z",
     "shell.execute_reply.started": "2025-03-02T13:46:19.622119Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2620\n"
     ]
    }
   ],
   "source": [
    "input_ids = unfinished_ids\n",
    "cycle_inputs = []\n",
    "for i, test_id in enumerate(input_ids):\n",
    "    cycle_inputs.append(\n",
    "        {\n",
    "            \"id\": str(test_id) + GEN_POST_STR,\n",
    "            \"prompt_audio\": str(test_id),\n",
    "            \"prompt_text\": \"\",\n",
    "            \"metadata\": {},\n",
    "        }\n",
    "    )\n",
    "print(len(cycle_inputs))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "51c8d14c-9368-459d-98f8-ba9ef8caa9b5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:46:19.866574Z",
     "iopub.status.busy": "2025-03-02T13:46:19.866330Z",
     "iopub.status.idle": "2025-03-02T13:46:20.088380Z",
     "shell.execute_reply": "2025-03-02T13:46:20.087834Z",
     "shell.execute_reply.started": "2025-03-02T13:46:19.866559Z"
    }
   },
   "outputs": [],
   "source": [
    "f_cycle = modal.Function.lookup(\"cycle-dev\", \"CodecCycleStub.cycle_code\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "f4b27a2f-a8a4-448f-a039-8037452d80fc",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:46:20.089185Z",
     "iopub.status.busy": "2025-03-02T13:46:20.089061Z",
     "iopub.status.idle": "2025-03-02T13:46:20.091341Z",
     "shell.execute_reply": "2025-03-02T13:46:20.090945Z",
     "shell.execute_reply.started": "2025-03-02T13:46:20.089172Z"
    }
   },
   "outputs": [],
   "source": [
    "# for val_input in tqdm.tqdm(cycle_inputs[:2]):\n",
    "#     # upload_to_s3: bool = True, downgrade_to_v2\n",
    "#     fn_call = f_cycle.spawn(json.dumps(val_input), True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "4521469a-ca26-451a-aa51-8b6d65d0e00b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:46:20.092110Z",
     "iopub.status.busy": "2025-03-02T13:46:20.091844Z",
     "iopub.status.idle": "2025-03-02T13:46:20.093761Z",
     "shell.execute_reply": "2025-03-02T13:46:20.093376Z",
     "shell.execute_reply.started": "2025-03-02T13:46:20.092096Z"
    }
   },
   "outputs": [],
   "source": [
    "# for val_index in tqdm.tqdm(range(0, 5, 5)):\n",
    "#     val_input = cycle_inputs[val_index:val_index+5]\n",
    "#     fn_call = f_cycle.spawn(json.dumps(val_input), True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad009f10-75e1-4731-8bfe-51c3d9fc8659",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:46:56.460064Z",
     "iopub.status.busy": "2025-03-02T13:46:56.459661Z",
     "iopub.status.idle": "2025-03-02T13:47:41.995486Z",
     "shell.execute_reply": "2025-03-02T13:47:41.994890Z",
     "shell.execute_reply.started": "2025-03-02T13:46:56.460048Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 524/524 [00:45<00:00, 11.51it/s]\n"
     ]
    }
   ],
   "source": [
    "# full job\n",
    "import time\n",
    "\n",
    "chunk_size = 5\n",
    "for val_index in tqdm.tqdm(range(0, len(cycle_inputs), chunk_size)):\n",
    "    val_input = cycle_inputs[val_index : val_index + chunk_size]\n",
    "    fn_call = f_cycle.spawn(json.dumps(val_input), True)\n",
    "    time.sleep(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ed94152-4d79-426c-ac01-d387631a2bea",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:47:59.921800Z",
     "iopub.status.busy": "2025-03-02T13:47:59.921382Z",
     "iopub.status.idle": "2025-03-02T13:48:00.779464Z",
     "shell.execute_reply": "2025-03-02T13:48:00.778833Z",
     "shell.execute_reply.started": "2025-03-02T13:47:59.921783Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 2620 2620 2620\n"
     ]
    }
   ],
   "source": [
    "# input_ids = sorted(input_ids)[:30000]\n",
    "s3_paths = [\n",
    "    f\"s3://suno-data-uploads/studio/uploads/{s3_id}{GEN_POST_STR}.npz\"\n",
    "    for s3_id in input_ids\n",
    "]\n",
    "local_paths = [f\"{NPZ_DIR}/{s3_id}{GEN_POST_STR}.npz\" for s3_id in input_ids]\n",
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "finished_ids_set = set(\n",
    "    [i.replace(f\"{GEN_POST_STR}.npz\", \"\") for i in finished_paths_set]\n",
    ")\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_ids = sorted([i for i in input_ids if i not in finished_ids_set])\n",
    "print(\n",
    "    \"jobs to be done\",\n",
    "    len(unfinished_paths),\n",
    "    len(unfinished_s3_paths),\n",
    "    len(unfinished_ids),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "4b07e693-63b6-4fec-89fb-ceeab1b7d5c1",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-03-02T13:52:26.831666Z",
     "iopub.status.busy": "2025-03-02T13:52:26.831243Z",
     "iopub.status.idle": "2025-03-02T13:54:22.475081Z",
     "shell.execute_reply": "2025-03-02T13:54:22.474501Z",
     "shell.execute_reply.started": "2025-03-02T13:52:26.831648Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 3/3 [01:55<00:00, 38.47s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e947b108-f876-43a0-8247-43b29c4f8a1e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ce27bb6-7048-4cbc-b962-cd4af5bd25de",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e27a930-b38f-40f6-8357-8e64f332707c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "361ae5c0-efb0-4211-ae97-35e05026528b",
   "metadata": {},
   "source": [
    "# test decode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "015ffa75-b6e3-438e-83b7-56dec408501c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-03T00:49:26.569022Z",
     "iopub.status.busy": "2024-09-03T00:49:26.568696Z",
     "iopub.status.idle": "2024-09-03T00:49:38.430313Z",
     "shell.execute_reply": "2024-09-03T00:49:38.429765Z",
     "shell.execute_reply.started": "2024-09-03T00:49:26.569003Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/tony/Work/glockenspiel/suno_utils/suno_utils/tasks/dac_2c_12cb.py:75: 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",
      "  sd = load_f(checkpoint_filepath)\n"
     ]
    }
   ],
   "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_stream_to_full_audio as codec_decode,\n",
    "    EMBEDDING_RATE as CODEC_EMBEDDING_RATE,\n",
    "    decode as decode,\n",
    ")\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
    "_ = preload_codec_models(\"/app/suno/data/dpo/models/dac_2c_25x12.pt\", device=\"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "42df7f92-c61e-40fa-86cb-4e46c9fd32d4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-03T00:49:38.431803Z",
     "iopub.status.busy": "2024-09-03T00:49:38.431056Z",
     "iopub.status.idle": "2024-09-03T00:49:38.434562Z",
     "shell.execute_reply": "2024-09-03T00:49:38.434129Z",
     "shell.execute_reply.started": "2024-09-03T00:49:38.431755Z"
    }
   },
   "outputs": [],
   "source": [
    "from tempfile import NamedTemporaryFile\n",
    "from suno_utils.utils.s3 import _download_s3_file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "fee11e48-d70d-4981-bd0e-ebfd4b3e130b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-03T00:49:38.435407Z",
     "iopub.status.busy": "2024-09-03T00:49:38.435250Z",
     "iopub.status.idle": "2024-09-03T00:49:38.479066Z",
     "shell.execute_reply": "2024-09-03T00:49:38.478663Z",
     "shell.execute_reply.started": "2024-09-03T00:49:38.435391Z"
    }
   },
   "outputs": [],
   "source": [
    "test_id = cycle_inputs[1][\"prompt_audio\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "47a85e98-115b-4b27-8e57-76d9ba064503",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-03T00:49:38.479748Z",
     "iopub.status.busy": "2024-09-03T00:49:38.479609Z",
     "iopub.status.idle": "2024-09-03T00:49:38.516278Z",
     "shell.execute_reply": "2024-09-03T00:49:38.515849Z",
     "shell.execute_reply.started": "2024-09-03T00:49:38.479734Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_array_from_id(test_id):\n",
    "    with NamedTemporaryFile(suffix=\".npz\") as f:\n",
    "        _download_s3_file(\n",
    "            f\"s3://suno-data-uploads/studio/uploads/{test_id}.npz\",\n",
    "            f.name,\n",
    "        )\n",
    "        print(f\"keys: {np.load(f.name).files}\")\n",
    "        npf = np.load(f.name)\n",
    "        if \"v3.0_raw\" in npf:\n",
    "            array = npf[\"v3.0_raw\"]\n",
    "        else:\n",
    "            array = npf[\"v1_raw\"]\n",
    "        print(array.shape)\n",
    "    return array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ef91c13c-357f-47ad-850c-39c9af36d4eb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-03T00:49:38.516977Z",
     "iopub.status.busy": "2024-09-03T00:49:38.516840Z",
     "iopub.status.idle": "2024-09-03T00:49:39.092418Z",
     "shell.execute_reply": "2024-09-03T00:49:39.091654Z",
     "shell.execute_reply.started": "2024-09-03T00:49:38.516963Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "keys: ['v4.0_raw', 'inference_runner', 'model_version', 'full_arr']\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'v1_raw is not a file in the archive'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[54], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m array \u001b[38;5;241m=\u001b[39m \u001b[43mget_array_from_id\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtest_id\u001b[49m\u001b[43m)\u001b[49m\n",
      "Cell \u001b[0;32mIn[53], line 12\u001b[0m, in \u001b[0;36mget_array_from_id\u001b[0;34m(test_id)\u001b[0m\n\u001b[1;32m     10\u001b[0m         array \u001b[38;5;241m=\u001b[39m npf[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mv3.0_raw\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m     11\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m---> 12\u001b[0m         array \u001b[38;5;241m=\u001b[39m \u001b[43mnpf\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mv1_raw\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\n\u001b[1;32m     13\u001b[0m     \u001b[38;5;28mprint\u001b[39m(array\u001b[38;5;241m.\u001b[39mshape)\n\u001b[1;32m     14\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m array\n",
      "File \u001b[0;32m~/anaconda3/envs/suno_env/lib/python3.10/site-packages/numpy/lib/npyio.py:263\u001b[0m, in \u001b[0;36mNpzFile.__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m    261\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mzip\u001b[38;5;241m.\u001b[39mread(key)\n\u001b[1;32m    262\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 263\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mkey\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is not a file in the archive\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[0;31mKeyError\u001b[0m: 'v1_raw is not a file in the archive'"
     ]
    }
   ],
   "source": [
    "array = get_array_from_id(test_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f23ba7d-10f0-4d96-b5cd-a9ede42b549c",
   "metadata": {
    "execution": {
     "iopub.status.busy": "2024-09-03T00:49:39.093163Z",
     "iopub.status.idle": "2024-09-03T00:49:39.093362Z",
     "shell.execute_reply": "2024-09-03T00:49:39.093270Z",
     "shell.execute_reply.started": "2024-09-03T00:49:39.093260Z"
    }
   },
   "outputs": [],
   "source": [
    "audio = decode(array[:, 1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb71ed27-01ac-4c3f-96b9-fcb1a37a8b77",
   "metadata": {
    "execution": {
     "iopub.status.busy": "2024-09-03T00:49:39.093953Z",
     "iopub.status.idle": "2024-09-03T00:49:39.094130Z",
     "shell.execute_reply": "2024-09-03T00:49:39.094048Z",
     "shell.execute_reply.started": "2024-09-03T00:49:39.094040Z"
    }
   },
   "outputs": [],
   "source": [
    "audio.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac3dffc8-e66a-4e00-a87d-4e6d97354fc8",
   "metadata": {
    "execution": {
     "iopub.status.busy": "2024-09-03T00:49:39.094662Z",
     "iopub.status.idle": "2024-09-03T00:49:39.094820Z",
     "shell.execute_reply": "2024-09-03T00:49:39.094747Z",
     "shell.execute_reply.started": "2024-09-03T00:49:39.094740Z"
    }
   },
   "outputs": [],
   "source": [
    "cycled_test_id = cycle_inputs[0][\"id\"]\n",
    "cycled_array = get_array_from_id(cycled_test_id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "98f3952b-1ee7-4b58-be53-6c5c5bcb0660",
   "metadata": {
    "execution": {
     "iopub.status.busy": "2024-09-03T00:49:39.095349Z",
     "iopub.status.idle": "2024-09-03T00:49:39.095502Z",
     "shell.execute_reply": "2024-09-03T00:49:39.095432Z",
     "shell.execute_reply.started": "2024-09-03T00:49:39.095424Z"
    }
   },
   "outputs": [],
   "source": [
    "cycled_audio = decode(cycled_array[:, 1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b7c83c34-397b-4000-ac93-ccab64845810",
   "metadata": {
    "execution": {
     "iopub.status.busy": "2024-09-03T00:49:39.096037Z",
     "iopub.status.idle": "2024-09-03T00:49:39.096187Z",
     "shell.execute_reply": "2024-09-03T00:49:39.096118Z",
     "shell.execute_reply.started": "2024-09-03T00:49:39.096111Z"
    }
   },
   "outputs": [],
   "source": [
    "cycled_audio.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a60aa581-708f-4a92-ad38-5e2fa4cd7cd9",
   "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.14"
  },
  "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
}
