{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:31:33.325671Z",
     "start_time": "2024-04-25T04:31:31.954721Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_331409/4201459797.py:1: DeprecationWarning: \n",
      "Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),\n",
      "(to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)\n",
      "but was not found to be installed on your system.\n",
      "If this would cause problems for you,\n",
      "please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466\n",
      "        \n",
      "  import pandas as pd\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "from sklearn.model_selection import train_test_split\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "import sys\n",
    "from collections import defaultdict\n",
    "from suno_utils.utils.text import (\n",
    "    write_jsonl,\n",
    "    read_jsonl,\n",
    "    write_json,\n",
    "    read_json,\n",
    ")\n",
    "import shutil\n",
    "import ast\n",
    "from preference_helper import *\n",
    "\n",
    "sys.path.insert(0, \"/home/tony/Work/glockenspiel/sunoGPT/scripts/\")\n",
    "\n",
    "from data_preparation_7b import *\n",
    "import numpy as np\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": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:31:33.366658Z",
     "start_time": "2024-04-25T04:31:33.327115Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/dpo/7v_r1_v16/\"\n",
    "# OUT_DATA_DIR = \"/home/tony/Data/test/7v_v6_full/\"\n",
    "os.makedirs(OUT_DATA_DIR, exist_ok=True)\n",
    "shutil.copyfile(\"/app/suno/data/dpo/7v_v1_full/tokenizer_60k.json\", os.path.join(OUT_DATA_DIR, \"tokenizer_60k.json\"))\n",
    "NPZ_DIR = \"/app/suno/data/dpo/v3_npz\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:31:33.390755Z",
     "start_time": "2024-04-25T04:31:33.367720Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_origin = pd.read_csv(\"/home/tony/Data/Preference/7b_v1/interesting_clips.csv\")\n",
    "# df_origin[df_origin[\"id\"] == \"75f69d46-1d81-4327-9253-a8a4886777d1\"]\n",
    "# df_origin[df_origin[\"request_id\"] == \"9414f356-88d6-40ba-b95e-9f4c5004a4aa\"]\n",
    "# df_origin[df_origin[\"request_id\"] == \"b2a150c1-f058-4bff-a013-1961e574631d\"]\n",
    "# df_origin[df_origin[\"id\"] == \"574ba431-18a1-4e28-ac7c-d3e401dba6fe\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:31:38.107843Z",
     "start_time": "2024-04-25T04:31:33.392226Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(122254, 43)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"/home/tony/Data/Preference/7b_v2/interesting_clips_20240424.csv\")\n",
    "df.shape\n",
    "# v0: (68746, 31)\n",
    "# v5: (938008, 37)\n",
    "# v6: (1097024, 38)\n",
    "# v21: (1822704, 41)\n",
    "# r1 \n",
    "# v2: 2620268 (pre fixes...lots of imperfections...)\n",
    "# v3: 1552512"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:31:38.139332Z",
     "start_time": "2024-04-25T04:31:38.109015Z"
    }
   },
   "outputs": [],
   "source": [
    "# download all clips; 883k\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "\n",
    "s3_ids = df[\"s3_id\"].values\n",
    "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": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T04:31:47.677387Z",
     "start_time": "2024-04-25T04:31:38.141746Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 107712 107712\n"
     ]
    }
   ],
   "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": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:09:45.461115Z",
     "start_time": "2024-04-25T04:31:47.678585Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 108/108 [1:37:55<00:00, 54.40s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=64)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:15:51.776033Z",
     "start_time": "2024-04-25T06:09:45.462579Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 14 14\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:03<00:00,  3.41s/it]\n"
     ]
    }
   ],
   "source": [
    "# # deleted files are at: deleted; try to get them as well\n",
    "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))\n",
    "unfinished_deleted_s3_paths = [\n",
    "    path.replace(\"/uploads/\", \"/deleted/\") for path in unfinished_s3_paths\n",
    "]\n",
    "_ = download_s3_files(unfinished_deleted_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:15:51.780128Z",
     "start_time": "2024-04-25T06:15:51.777944Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finish downloads\n"
     ]
    }
   ],
   "source": [
    "print(\"Finish downloads\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:37.445993Z",
     "start_time": "2024-04-25T06:15:51.782824Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11256105\n"
     ]
    }
   ],
   "source": [
    "converted_paths = os.listdir(NPZ_DIR)\n",
    "print(len(converted_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:37.450034Z",
     "start_time": "2024-04-25T06:21:37.447265Z"
    }
   },
   "outputs": [],
   "source": [
    "# # don't run this unless you kill some job accidentally\n",
    "# for s3_processed_file in tqdm.tqdm(converted_paths):\n",
    "#     processed_file = os.path.basename(s3_processed_file)\n",
    "#     processed_file_path = os.path.join(\"/app/suno/data/dpo/v3_npz\", processed_file)\n",
    "#     # could have been removed already\n",
    "#     if os.path.exists(processed_file_path):\n",
    "#         file_size = os.stat(processed_file_path).st_size\n",
    "#         # print(processed_file_path, file_size)\n",
    "#         # break\n",
    "#         if file_size < 2000:\n",
    "#             print(processed_file_path, file_size)\n",
    "#             os.remove(processed_file_path)\n",
    "# converted_paths = os.listdir(NPZ_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:42.079918Z",
     "start_time": "2024-04-25T06:21:37.451122Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11256105\n"
     ]
    }
   ],
   "source": [
    "converted_paths = set([f.replace(\".npz\", \"\") for f in converted_paths])\n",
    "print(len(converted_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:54.215286Z",
     "start_time": "2024-04-25T06:21:42.081900Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pre-downloaded df (122254, 43)\n",
      "downloaded df (122254, 43)\n"
     ]
    }
   ],
   "source": [
    "print(\"pre-downloaded df\", df.shape)\n",
    "df[df[\"s3_id\"].isin(converted_paths)].shape\n",
    "df = df[df[\"s3_id\"].isin(converted_paths)].copy()\n",
    "print(\"downloaded df\", df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:54.246764Z",
     "start_time": "2024-04-25T06:21:54.217242Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "is_7b\n",
       "True    122254\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"is_7b\"] = df[\"model_name\"].str.contains(\"v3\")\n",
    "df[\"is_7b\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.120955Z",
     "start_time": "2024-04-25T06:21:54.248283Z"
    }
   },
   "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[15], 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": "markdown",
   "metadata": {},
   "source": [
    "# LET's do the data prep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.123921Z",
     "start_time": "2024-04-25T06:21:56.123911Z"
    }
   },
   "outputs": [],
   "source": [
    "date_cut = '2024-03-22 04:30:00' # v3 launch test time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.124816Z",
     "start_time": "2024-04-25T06:21:56.124808Z"
    }
   },
   "outputs": [],
   "source": [
    "# # let's also kick out the ... ipo and ipo-dpoed model for now?\n",
    "print(df.groupby([\"preference\"])[\"model_name\"].value_counts())\n",
    "print(df.shape)\n",
    "# df = df[df[\"model_name\"].isin([\"chirp-v3-engine-d\", \"chirp-v3-engine-v0\", \"chirp-v3-engine-i\"])]\n",
    "# df = df[df[\"model_name\"].isin([\"chirp-v3-engine-d\", \"chirp-v3-engine-v0\"])]\n",
    "# only IPO\n",
    "# df = df[(df[\"model_name\"].isin([\"chirp-v3-engine-i\"]) ) & (df[\"created_at\"] >= date_cut)]\n",
    "# for some reason...we can't train dpo on the ipo data...it just doesn't follow lyrics...X.x\n",
    "# df = df[\n",
    "#     (\n",
    "#         (df[\"model_name\"].isin([\"chirp-v3-engine-d\", \"chirp-v3-engine-v0\"]))\n",
    "#         & (df[\"preference\"] == True)\n",
    "#     )\n",
    "#     | (df[\"preference\"] == False)\n",
    "# ]\n",
    "print(df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.125652Z",
     "start_time": "2024-04-25T06:21:56.125644Z"
    }
   },
   "outputs": [],
   "source": [
    "print(df.shape)\n",
    "df = df[\n",
    "    df[\"request_id\"].isin(\n",
    "        df[\"request_id\"].value_counts().index[df[\"request_id\"].value_counts() == 2]\n",
    "    )\n",
    "]\n",
    "print(df.shape)\n",
    "print(df.groupby([\"preference\"])[\"model_name\"].value_counts())\n",
    "assert df.shape[0] == df[\"request_id\"].nunique() * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.126651Z",
     "start_time": "2024-04-25T06:21:56.126643Z"
    }
   },
   "outputs": [],
   "source": [
    "# expand the metadata columns -- this takes forever...~ 6 mins\n",
    "test_slice = df[\"metadata\"].apply(lambda x: ast.literal_eval(x))\n",
    "test_slice_series = test_slice.apply(pd.Series)\n",
    "df = pd.concat([df, test_slice_series], axis=1, join=\"inner\")\n",
    "print(\"unique_requests\", df[\"request_id\"].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.127391Z",
     "start_time": "2024-04-25T06:21:56.127384Z"
    }
   },
   "outputs": [],
   "source": [
    "# cut on v3 time\n",
    "df[\"has_bad_gpt_prompt\"] = (~df[\"gpt_description_prompt\"].isna()) & (df[\"created_at\"] <= \"2024-03-22 04:30:00\")\n",
    "df[\"has_bad_gpt_prompt\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.128297Z",
     "start_time": "2024-04-25T06:21:56.128289Z"
    }
   },
   "outputs": [],
   "source": [
    "# double check we removed the gpt prompted ones for now \n",
    "# TODO: add these data back...\n",
    "df = df[df[\"has_bad_gpt_prompt\"] == False]\n",
    "print(\"unique_requests\", df[\"request_id\"].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.128992Z",
     "start_time": "2024-04-25T06:21:56.128984Z"
    }
   },
   "outputs": [],
   "source": [
    "# get the original duration of the clips, if they are concacted\n",
    "df[\"original_duration_s\"] = df[\"total_start_s\"] + df[\"duration\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.129697Z",
     "start_time": "2024-04-25T06:21:56.129689Z"
    }
   },
   "outputs": [],
   "source": [
    "# classify the continue at behavoirs by the duration choice\n",
    "audio_prompt_id_to_continue_at = {}\n",
    "for _, row in df[~df[\"audio_prompt_id\"].isna()].iterrows():\n",
    "    audio_prompt_id = row[\"audio_prompt_id\"]\n",
    "    if audio_prompt_id not in audio_prompt_id_to_continue_at:\n",
    "        audio_prompt_id_to_continue_at[audio_prompt_id] = row[\"continue_at\"]\n",
    "    else:\n",
    "        # pick the max\n",
    "        audio_prompt_id = max(\n",
    "            audio_prompt_id_to_continue_at[audio_prompt_id], row[\"continue_at\"]\n",
    "        )\n",
    "print(len(audio_prompt_id_to_continue_at))\n",
    "df[\"has_continue_and_start_continue_at\"] = df[\"id\"].apply(\n",
    "    lambda x: audio_prompt_id_to_continue_at.get(x)\n",
    ")\n",
    "# we want continue at to be at least half of the clip...\n",
    "df[\"good_continue_at\"] = (\n",
    "    (df[\"has_continue_and_start_continue_at\"] / df[\"duration\"]) > 0.9\n",
    ") | df[\"has_continue_and_start_continue_at\"].isna()\n",
    "print(df[\"good_continue_at\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.130639Z",
     "start_time": "2024-04-25T06:21:56.130631Z"
    }
   },
   "outputs": [],
   "source": [
    "print(\n",
    "    df[\"preference\"].value_counts(),\n",
    "    df[\"is_7b\"].value_counts(),\n",
    "    df[\"model_name\"].value_counts(),\n",
    "    df.groupby([\"preference\"])[\"model_name\"].value_counts()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.131407Z",
     "start_time": "2024-04-25T06:21:56.131399Z"
    }
   },
   "outputs": [],
   "source": [
    "df = df.sort_values(by=[\"request_id\", \"preference\"])\n",
    "df[\"duration_rel_diff\"] = df['duration'].diff() \n",
    "df[\"play_rel_diff\"] = df['reaction_play_count'].diff() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.132339Z",
     "start_time": "2024-04-25T06:21:56.132332Z"
    }
   },
   "outputs": [],
   "source": [
    "df[\n",
    "    [\n",
    "        \"request_id\",\n",
    "        \"preference\",\n",
    "        \"duration\",\n",
    "        \"duration_rel_diff\",\n",
    "        \"reaction_play_count\",\n",
    "        \"play_rel_diff\",\n",
    "    ]\n",
    "].tail(n=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.133305Z",
     "start_time": "2024-04-25T06:21:56.133298Z"
    }
   },
   "outputs": [],
   "source": [
    "df[df[\"preference\"] == True].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.134003Z",
     "start_time": "2024-04-25T06:21:56.133995Z"
    }
   },
   "outputs": [],
   "source": [
    "df[df[\"preference\"] == False].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.135202Z",
     "start_time": "2024-04-25T06:21:56.135194Z"
    }
   },
   "outputs": [],
   "source": [
    "normal_pos_play_count = 3\n",
    "# this is lower, cause a concat is probably already ensuring that it is good\n",
    "concat_pos_play_count = 1\n",
    "# this is a filter on the concated clip\n",
    "concat_total_play_count = 3\n",
    "\n",
    "neg_filter_selection_mask = (\n",
    "    (df[\"preference\"] == False)  # get basics aligned\n",
    "    & (df[\"reaction_play_count\"] >= 1)  # has to be played once -- just villian?\n",
    "    & (\n",
    "        df[\"reaction_play_count\"] <= 3\n",
    "    )  # if it is actually bad, shouldn't be listened often\n",
    "    & (df[\"duration\"] >= 10)  # can't be too short, otherwise it is obvious\n",
    "    & (df[\"duration\"] <= 120)  # can't be badly long\n",
    "    & (df[\"has_continue_and_start_continue_at\"].isna())  # won't have any continues\n",
    "    & ((df[\"dislike_count\"] >= 1) | (df[\"flag_count\"] >= 1))  # this is kinda strict\n",
    "    #     & (\n",
    "    #         (df_slice[\"is_in_playlist\"] == False)\n",
    "    #         & (df_slice[\"concat_in_playlist\"] == False)\n",
    "    #     )  # can't be part of a playlist -- otherwise there are some like signal in it?\n",
    ")\n",
    "pos_filter_selectin_mask = (\n",
    "    (df[\"preference\"] == True)  # get basics aligned\n",
    "    & (\n",
    "        df[\"good_continue_at\"] == True\n",
    "    )  # if continue, needs to continue off a certain percentage\n",
    "    & (df[\"continue_at\"].isna())\n",
    "    & (\n",
    "        (\n",
    "            (df[\"part_of_concat\"] == True)\n",
    "            & (df[\"reaction_play_count\"] >= concat_pos_play_count)\n",
    "            & (df[\"concat_play_counts\"] >= concat_total_play_count)\n",
    "        )\n",
    "        | (\n",
    "            (df[\"part_of_concat\"] == False)\n",
    "            & (df[\"reaction_play_count\"] >= normal_pos_play_count)\n",
    "        )\n",
    "    )\n",
    "    & (df[\"play_rel_diff\"] >= 0)  # this is more like quality assurance\n",
    "    & (df[\"duration\"] >= 10)  # can't be too short, otherwise it is obvious\n",
    "    & (df[\"duration\"] <= 120)  # can't be badly long\n",
    "    & (df[\"dislike_count\"] == 0)  # can't have dislikes\n",
    "    & (df[\"flag_count\"] == 0)  # can't have issues\n",
    "    & (df[\"user_n_clips\"] >= 10)  # user needs to have genereated at least 40\n",
    "    # & (df[\"duration_rel_diff\"] < 10) # positive isn't just longer\n",
    "    # & (df[\"upvote_count\"] >= 1)\n",
    ")\n",
    "print(\n",
    "    \"negative\",\n",
    "    sum(neg_filter_selection_mask),\n",
    "    \"positive\",\n",
    "    sum(pos_filter_selectin_mask),\n",
    ")\n",
    "\n",
    "neg_filter_requests = df[neg_filter_selection_mask][\"request_id\"].unique()\n",
    "pos_filter_requests = df[pos_filter_selectin_mask][\"request_id\"].unique()\n",
    "# looking for very strong signal here:\n",
    "# listen to the positive/negative more than once\n",
    "# disliked one of the clips\n",
    "unique_requests = set(pos_filter_requests).intersection(neg_filter_requests)\n",
    "print(\n",
    "    \"total pair requests\",\n",
    "    df[\"request_id\"].nunique(),\n",
    "    \"selected pair requests\",\n",
    "    len(unique_requests),\n",
    "    f\"frac {len(unique_requests) / df['request_id'].nunique():.3f}\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.136048Z",
     "start_time": "2024-04-25T06:21:56.136041Z"
    }
   },
   "outputs": [],
   "source": [
    "df_slice = df[df[\"request_id\"].isin(set(unique_requests))].copy()\n",
    "print(\n",
    "    \"requests\",\n",
    "    df_slice[\"request_id\"].nunique(),\n",
    "    \"clips\",\n",
    "    df_slice.shape[0],\n",
    "    f\"total khrs {sum(df_slice['duration'] / 3600 / 1000):.3f};\",\n",
    "    f\"N gpus for 1250 iters {df_slice.shape[0] / 8 / 4 / 1250:.3f};\",\n",
    "    f\"n unique users {df_slice['user_id'].nunique()}\",\n",
    ")\n",
    "# 76171 152342 total khrs 2.880 n gpus for 1250 iters 3.809"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.137135Z",
     "start_time": "2024-04-25T06:21:56.137127Z"
    }
   },
   "outputs": [],
   "source": [
    "last_continue_requests = df[\n",
    "    ((df[\"has_continue_and_start_continue_at\"] / df[\"duration\"]) > 0.99)\n",
    "][\"request_id\"].unique()\n",
    "early_continue_requests = df[\n",
    "    ((df[\"has_continue_and_start_continue_at\"] / df[\"duration\"]) < 0.99)\n",
    "    & ((df[\"has_continue_and_start_continue_at\"] / df[\"duration\"]) > 0.01)\n",
    "][\"request_id\"].unique()\n",
    "not_continue_requests = df[df[\"has_continue_and_start_continue_at\"].isna()][\n",
    "    \"request_id\"\n",
    "].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.137847Z",
     "start_time": "2024-04-25T06:21:56.137839Z"
    }
   },
   "outputs": [],
   "source": [
    "print(df_slice.groupby([\"preference\"])[\"model_name\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.138543Z",
     "start_time": "2024-04-25T06:21:56.138536Z"
    }
   },
   "outputs": [],
   "source": [
    "get_preferfence_counts(df_slice)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.139257Z",
     "start_time": "2024-04-25T06:21:56.139249Z"
    }
   },
   "outputs": [],
   "source": [
    "# get_preferfence_counts(df_slice[df_slice[\"request_id\"].isin(not_continue_requests)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.139996Z",
     "start_time": "2024-04-25T06:21:56.139988Z"
    }
   },
   "outputs": [],
   "source": [
    "# get_preferfence_counts(df_slice[df_slice[\"request_id\"].isin(last_continue_requests)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.140825Z",
     "start_time": "2024-04-25T06:21:56.140818Z"
    }
   },
   "outputs": [],
   "source": [
    "# get_preferfence_counts(df_slice[df_slice[\"request_id\"].isin(early_continue_requests)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.141705Z",
     "start_time": "2024-04-25T06:21:56.141698Z"
    }
   },
   "outputs": [],
   "source": [
    "df_slice[df_slice[\"preference\"] == True].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.142271Z",
     "start_time": "2024-04-25T06:21:56.142264Z"
    }
   },
   "outputs": [],
   "source": [
    "df_slice[df_slice[\"preference\"] == False].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.143647Z",
     "start_time": "2024-04-25T06:21:56.143638Z"
    }
   },
   "outputs": [],
   "source": [
    "test_mask = (df_slice[\"preference\"] == True) & (\n",
    "    (df_slice[\"is_in_playlist\"] == True) | (df_slice[\"concat_in_playlist\"] == True)\n",
    ")\n",
    "print(\"positive in playlist\", df_slice[test_mask].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.144363Z",
     "start_time": "2024-04-25T06:21:56.144356Z"
    }
   },
   "outputs": [],
   "source": [
    "interesting_clips_must_be_positive_mask = (\n",
    "    (df_slice[\"upvoted\"] == True)\n",
    "    | (df_slice[\"has_action\"] == True)\n",
    "    | (df_slice[\"part_of_concat\"] == True)\n",
    ")\n",
    "interesting_clips_must_be_not_negative_mask = df_slice[\"downvoted\"] == False\n",
    "interesting_clips_mask = interesting_clips_must_be_positive_mask & interesting_clips_must_be_not_negative_mask\n",
    "assert interesting_clips_mask.eq(df_slice[\"preference\"]).all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.145149Z",
     "start_time": "2024-04-25T06:21:56.145142Z"
    }
   },
   "outputs": [],
   "source": [
    "BREAK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Need to kick out the ones has gpt prompt -- these are pairs with different text inputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.145714Z",
     "start_time": "2024-04-25T06:21:56.145707Z"
    }
   },
   "outputs": [],
   "source": [
    "# don't have continue at\n",
    "df_slice[df_slice[\"continue_at\"].isna()][\"request_id\"].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.146380Z",
     "start_time": "2024-04-25T06:21:56.146372Z"
    }
   },
   "outputs": [],
   "source": [
    "final_filtered_requests = df_slice[\"request_id\"].unique()\n",
    "print(len(final_filtered_requests))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.147108Z",
     "start_time": "2024-04-25T06:21:56.147100Z"
    }
   },
   "outputs": [],
   "source": [
    "train_requests, val_requests = train_test_split(\n",
    "    sorted(list(final_filtered_requests)), test_size=0.01, random_state=42\n",
    ")\n",
    "print(len(train_requests), len(val_requests))\n",
    "\n",
    "train_df = df_slice[df_slice[\"request_id\"].isin(set(train_requests))].copy()\n",
    "val_df = df_slice[df_slice[\"request_id\"].isin(set(val_requests))].copy()\n",
    "train_df = train_df.sort_values(by=[\"request_id\", \"preference\"])\n",
    "train_df = train_df.reset_index()\n",
    "val_df = val_df.sort_values(by=[\"request_id\", \"preference\"])\n",
    "val_df = val_df.reset_index()\n",
    "\n",
    "print(train_df.shape, val_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.148104Z",
     "start_time": "2024-04-25T06:21:56.148096Z"
    }
   },
   "outputs": [],
   "source": [
    "def reshift(arr):\n",
    "    sem_start_idx = 0\n",
    "    sem_end_idx = len(arr) - 1\n",
    "    semantic_arr = arr[:, :SEMANTIC_N_CODEBOOKS]\n",
    "    coarse_arr = arr[:, SEMANTIC_N_CODEBOOKS:]\n",
    "\n",
    "    coarse_start_idx = int(round(sem_start_idx * COARSE_RATE_HZ / SEMANTIC_RATE_HZ))\n",
    "    coarse_end_idx = int(round(sem_end_idx * COARSE_RATE_HZ / SEMANTIC_RATE_HZ))\n",
    "    assert sem_end_idx >= 0 and coarse_start_idx >= 0\n",
    "    assert not (sem_end_idx > len(semantic_arr) or coarse_end_idx > len(coarse_arr))\n",
    "\n",
    "    # get array segments\n",
    "    arr_s = semantic_arr[sem_start_idx:sem_end_idx, :].copy()\n",
    "    arr_c = coarse_arr[coarse_start_idx:coarse_end_idx, :].copy()\n",
    "    assert arr_s.max() <= SEMANTIC_PAD_TOKEN\n",
    "    assert arr_c.max() <= COARSE_PAD_TOKEN\n",
    "    assert len(arr_s) == len(arr_c)\n",
    "    # concat and stack\n",
    "    if len(arr_c) < N_TOKENS_AUDIO:\n",
    "        arr_c = np.pad(\n",
    "            arr_c,\n",
    "            ((0, N_TOKENS_AUDIO - len(arr_c)), (0, 0)),\n",
    "            constant_values=COARSE_PAD_TOKEN,\n",
    "            mode=\"constant\",\n",
    "        )\n",
    "        arr_s = np.pad(\n",
    "            arr_s,\n",
    "            ((0, N_TOKENS_AUDIO - len(arr_s)), (0, 0)),\n",
    "            constant_values=SEMANTIC_PAD_TOKEN,\n",
    "            mode=\"constant\",\n",
    "        )\n",
    "    arr = np.concatenate([arr_s, arr_c], axis=-1)\n",
    "    arr = arr.astype(np.uint16)\n",
    "    assert arr.shape == (N_TOKENS_AUDIO, SEMANTIC_N_CODEBOOKS + COARSE_N_CODEBOOKS)\n",
    "    return arr\n",
    "\n",
    "\n",
    "def make_dataset(input_df, is_val=False):\n",
    "    dset_type = \"val\" if is_val else \"tr\"\n",
    "    out_mmap_path = os.path.join(OUT_DATA_DIR, f\"data_{dset_type}.bin\")\n",
    "    out_metas_path = os.path.join(OUT_DATA_DIR, f\"meta_{dset_type}.jsonl\")\n",
    "    out_info_filepath = os.path.join(OUT_DATA_DIR, f\"info_{dset_type}.json\")\n",
    "\n",
    "    # gather the data\n",
    "    _ = np.memmap(out_mmap_path, dtype=np.uint16, mode=\"w+\", shape=(1,))\n",
    "    n_offs = 0\n",
    "    tot_duration_dict = defaultdict(int)\n",
    "    datasets_info = defaultdict(dict)\n",
    "    n = 0\n",
    "    for i, row in tqdm.tqdm(input_df.iterrows(), total=len(input_df)):\n",
    "        # we need to alternate between preference: neg, pos\n",
    "        # print(i, row)\n",
    "        assert row[\"preference\"] == (i % 2 == 1)\n",
    "        # make mmap -- two different paths\n",
    "        local_path = (\n",
    "            f\"{NPZ_DIR}/{row['s3_id']}.npz\"\n",
    "            if row[\"is_7b\"]\n",
    "            else f\"/app/suno/data/dpo/7b_npz/{row['s3_id']}.npz\"\n",
    "        )\n",
    "        if not os.path.exists(local_path):\n",
    "            # print(row, local_path)\n",
    "            raise ValueError()\n",
    "        # print(local_path)\n",
    "        # print( np.load(local_path))\n",
    "        try:\n",
    "            arr = (\n",
    "                np.load(local_path)[\"v3.0_raw\"]\n",
    "                if row[\"is_7b\"]\n",
    "                else np.load(local_path)[\"v2_raw\"]\n",
    "            )\n",
    "        except Exception as e:\n",
    "            print(local_path)\n",
    "            raise e\n",
    "        assert arr.shape[0] <= 3000\n",
    "        assert arr.shape[1] == 13\n",
    "        arr_duration = arr.shape[0] / 25\n",
    "        # print(arr.shape)\n",
    "        arr = reshift(arr)\n",
    "        # print(\"after shift and pad\", arr.shape)\n",
    "        arr = arr.reshape(\n",
    "            -1,\n",
    "        )\n",
    "        # print(arr.shape)\n",
    "        out_mm = np.memmap(\n",
    "            out_mmap_path,\n",
    "            dtype=np.uint16,\n",
    "            mode=\"r+\",\n",
    "            shape=(n_offs + arr.size,),\n",
    "        )\n",
    "        out_mm[n_offs : n_offs + arr.size] = arr\n",
    "        # print(f\"offset is: {n_offs}\")\n",
    "        # break\n",
    "        # write it once\n",
    "        out_mm.flush()\n",
    "        del out_mm\n",
    "\n",
    "        add_metas = []\n",
    "        add_meta = {\n",
    "            \"dataset\": f\"perference_{int(row['preference'])}\",\n",
    "            \"id\": row[\"s3_id\"],  # this is the row s3_id\n",
    "            \"start_s\": row[\"total_start_s\"] if row[\"total_start_s\"] >= 0 else None,\n",
    "            \"end_s\": (\n",
    "                row[\"total_clip_s\"] if row[\"total_clip_s\"] >= 0 else None\n",
    "            ),  # for full clips we do know it has an edding, other wise, we don't know\n",
    "            \"original_duration_s\": (\n",
    "                row[\"original_duration_s\"]\n",
    "                if row[\"original_duration_s\"] >= 0\n",
    "                else arr_duration\n",
    "            ),  # this nees to be... a bit more complicated, only works with concat!\n",
    "            \"vocal_start_s\": None,  # these are unfortunately missing for now\n",
    "            \"vocal_end_s\": None,  # these are unfortunately missing for now\n",
    "            \"tags\": [\n",
    "                row[\"tags\"] if row[\"tags\"] else \"\"\n",
    "            ],  # tags is a list, do you know :)\n",
    "            \"text\": row[\"prompt\"] if row[\"prompt\"] else \"\",\n",
    "        }\n",
    "        add_metas.append(add_meta)\n",
    "        tot_duration_dict[row[\"preference\"]] += arr_duration\n",
    "        write_jsonl(\n",
    "            add_metas,\n",
    "            os.path.join(out_metas_path),\n",
    "            do_append=bool(n_offs != 0),\n",
    "        )\n",
    "        if \"idx_list\" not in datasets_info[add_meta[\"dataset\"]]:\n",
    "            datasets_info[add_meta[\"dataset\"]][\"idx_list\"] = [n]\n",
    "        else:\n",
    "            datasets_info[add_meta[\"dataset\"]][\"idx_list\"].append(n)\n",
    "        n += 1\n",
    "        n_offs += arr.size\n",
    "\n",
    "    write_json(datasets_info, out_info_filepath)\n",
    "    print(f\"Total {n} clips\")\n",
    "    for k, v in tot_duration_dict.items():\n",
    "        print(f\"{round(v / 60 / 60):,} hours of {k}\")\n",
    "    print(f\"Done\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Actually make"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.148638Z",
     "start_time": "2024-04-25T06:21:56.148631Z"
    }
   },
   "outputs": [],
   "source": [
    "# val_df[[\"request_id\", \"metadata\", \"updated_at\", \"user_id\", \"preference\"]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.149612Z",
     "start_time": "2024-04-25T06:21:56.149605Z"
    }
   },
   "outputs": [],
   "source": [
    "total_duration = 0\n",
    "for i, row in tqdm.tqdm(train_df.iterrows(), total=len(train_df)):\n",
    "    # we need to alternate between preference: neg, pos\n",
    "    # print(i, row)\n",
    "    try:\n",
    "        assert row[\"preference\"] == (i % 2 == 1)\n",
    "    except:\n",
    "        print(i, row)\n",
    "    total_duration += row[\"duration\"]\n",
    "print(f\"{round(total_duration / 60 / 60):,} hours of {train_df.shape[0]} clips, {train_df.shape[0] / 8 / 4 / 1000} nodes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.150399Z",
     "start_time": "2024-04-25T06:21:56.150391Z"
    }
   },
   "outputs": [],
   "source": [
    "make_dataset(val_df, is_val=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.151261Z",
     "start_time": "2024-04-25T06:21:56.151254Z"
    }
   },
   "outputs": [],
   "source": [
    "make_dataset(train_df, is_val=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-29T19:46:47.549860Z",
     "start_time": "2024-01-29T19:46:47.548015Z"
    }
   },
   "source": [
    "# Validation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.152000Z",
     "start_time": "2024-04-25T06:21:56.151992Z"
    }
   },
   "outputs": [],
   "source": [
    "# verify\n",
    "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\"meta_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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.152708Z",
     "start_time": "2024-04-25T06:21:56.152701Z"
    }
   },
   "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_stream_to_full_audio as codec_decode,\n",
    "    EMBEDDING_RATE as CODEC_EMBEDDING_RATE,\n",
    "    decode as decode\n",
    ")\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"3\"\n",
    "_ = preload_codec_models(\"/app/suno/tony/v3/dac_2c_25x12.pt\", device=\"cuda\")\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,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.153616Z",
     "start_time": "2024-04-25T06:21:56.153607Z"
    }
   },
   "outputs": [],
   "source": [
    "idx = random.choice(test_info[\"perference_0\"][\"idx_list\"])\n",
    "assert \"original_duration_s\" in test_metas[idx]\n",
    "# positive index should be shifted by 1\n",
    "pos_idx = idx + 1\n",
    "print(\n",
    "    \"tags:\",\n",
    "    test_metas[idx].get(\"tags\") == test_metas[pos_idx].get(\"tags\"),\n",
    "    test_metas[idx].get(\"tags\"),\n",
    ")\n",
    "arr = mm[idx, 1:].copy().astype(np.int16)[:, 1:]\n",
    "pos_arr = mm[pos_idx, 1:].copy().astype(np.int16)[:, 1:]\n",
    "pad_idx_arr = np.where(arr == COARSE_PAD_TOKEN)[0]\n",
    "if len(pad_idx_arr) > 0:\n",
    "    arr = arr[: pad_idx_arr[0], :]\n",
    "pos_pad_idx_arr = np.where(pos_arr == COARSE_PAD_TOKEN)[0]\n",
    "if len(pos_pad_idx_arr) > 0:\n",
    "    pos_arr = pos_arr[: pos_pad_idx_arr[0], :]\n",
    "a = decode(arr)\n",
    "print(\"negative example\", test_metas[idx])\n",
    "a.play(compress=False)\n",
    "pos_a = decode(pos_arr)\n",
    "print(\"positive example\", test_metas[pos_idx])\n",
    "pos_a.play(compress=False)\n",
    "print(\n",
    "    \"text:\",\n",
    "    test_metas[idx].get(\"text\") == test_metas[pos_idx].get(\"text\"),\n",
    "    test_metas[idx].get(\"text\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.154538Z",
     "start_time": "2024-04-25T06:21:56.154530Z"
    }
   },
   "outputs": [],
   "source": [
    "# val_df[val_df[\"tags\"] == 'a vibrant blend of experimental jazz fusion, drum-and-bass and swagger fuzzed-out guitars']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.155210Z",
     "start_time": "2024-04-25T06:21:56.155203Z"
    }
   },
   "outputs": [],
   "source": [
    "# from collections import Counter\n",
    "# c = Counter()\n",
    "# for _, row in df_slice.iterrows():\n",
    "#     # print(row[\"metadata\"])\n",
    "#     for k in ast.literal_eval(row[\"metadata\"]).keys():\n",
    "#         c[k] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.155970Z",
     "start_time": "2024-04-25T06:21:56.155962Z"
    }
   },
   "outputs": [],
   "source": [
    "# original_npz_path = f\"/app/suno/data/dpo/7b_npz/{test_metas[idx]['id']}.npz\"\n",
    "# original_npz_path = \"/app/suno/data/dpo/7b_npz/729c3011-f672-4ccd-8d82-1cbf2b52ff69.npz\"\n",
    "# original_arr = np.load(original_npz_path)[\"v2_raw\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.156596Z",
     "start_time": "2024-04-25T06:21:56.156589Z"
    }
   },
   "outputs": [],
   "source": [
    "def validation_on_metas(input_metas):\n",
    "\n",
    "    total_bad = 0\n",
    "    total_good = 0\n",
    "    for idx in range(len(input_metas)):\n",
    "        if idx % 2 == 0:\n",
    "            pos_idx = idx + 1\n",
    "            if input_metas[idx].get(\"tags\") != input_metas[pos_idx].get(\"tags\"):\n",
    "                # print(test_metas[idx].get(\"text\") == test_metas[pos_idx].get(\"text\"), test_metas[idx].get(\"tags\"), test_metas[pos_idx].get(\"tags\"))\n",
    "                total_bad += 1\n",
    "            else:\n",
    "                total_good += 1\n",
    "    print(total_good, total_bad)\n",
    "    return\n",
    "\n",
    "\n",
    "validation_on_metas(test_metas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.157293Z",
     "start_time": "2024-04-25T06:21:56.157285Z"
    }
   },
   "outputs": [],
   "source": [
    "train_info = read_json(os.path.join(OUT_DATA_DIR, f\"info_tr.json\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.157976Z",
     "start_time": "2024-04-25T06:21:56.157969Z"
    }
   },
   "outputs": [],
   "source": [
    "n_neg_tr = train_info[\"perference_0\"][\"idx_list\"]\n",
    "n_pos_tr = train_info[\"perference_1\"][\"idx_list\"]\n",
    "assert len(n_pos_tr) == len(n_neg_tr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.158986Z",
     "start_time": "2024-04-25T06:21:56.158978Z"
    }
   },
   "outputs": [],
   "source": [
    "total_iters = (len(n_neg_tr) + len(n_pos_tr))\n",
    "print(\"total samples\", total_iters, train_df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.159486Z",
     "start_time": "2024-04-25T06:21:56.159478Z"
    }
   },
   "outputs": [],
   "source": [
    "print(\"1 epoch per batch 4, total\", total_iters / 8 / 4 / 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-25T06:21:56.160332Z",
     "start_time": "2024-04-25T06:21:56.160325Z"
    }
   },
   "outputs": [],
   "source": [
    "!cd /home/tony/Work/tony/slurm && sbatch sbatch_dpo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 2
}
