{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:21.040680Z",
     "start_time": "2024-05-16T13:58:19.777010Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:25:40.433868Z",
     "iopub.status.busy": "2024-09-25T21:25:40.433719Z",
     "iopub.status.idle": "2024-09-25T21:25:44.539950Z",
     "shell.execute_reply": "2024-09-25T21:25:44.539433Z",
     "shell.execute_reply.started": "2024-09-25T21:25:40.433849Z"
    }
   },
   "outputs": [],
   "source": [
    "import ast\n",
    "import os\n",
    "import shutil\n",
    "import sys\n",
    "from collections import defaultdict\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from preference_data_preparation_4min_13b import *\n",
    "from preference_helper import *\n",
    "from sklearn.model_selection import train_test_split\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "from suno_utils.utils.text import read_json, read_jsonl, write_json, write_jsonl\n",
    "from tqdm import tqdm\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-05-16T13:58:21.082172Z",
     "start_time": "2024-05-16T13:58:21.041926Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:25:44.540853Z",
     "iopub.status.busy": "2024-09-25T21:25:44.540644Z",
     "iopub.status.idle": "2024-09-25T21:25:44.581792Z",
     "shell.execute_reply": "2024-09-25T21:25:44.581337Z",
     "shell.execute_reply.started": "2024-09-25T21:25:44.540838Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/dpo/13b_s8_v9/\"\n",
    "os.makedirs(OUT_DATA_DIR, exist_ok=True)\n",
    "shutil.copyfile(\n",
    "    \"/app/suno/data/dpo/7v_v20_full/tokenizer_60k.json\",\n",
    "    os.path.join(OUT_DATA_DIR, \"tokenizer_60k.json\"),\n",
    ")\n",
    "NPZ_DIR = \"/app/suno/data/dpo/13b_npz\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:53.962528Z",
     "start_time": "2024-05-16T13:58:21.105919Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:25:44.582532Z",
     "iopub.status.busy": "2024-09-25T21:25:44.582390Z",
     "iopub.status.idle": "2024-09-25T21:25:54.502027Z",
     "shell.execute_reply": "2024-09-25T21:25:54.501451Z",
     "shell.execute_reply.started": "2024-09-25T21:25:44.582518Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preference data shape (477514, 71)\n"
     ]
    }
   ],
   "source": [
    "# df = pd.read_csv(\n",
    "#     \"/home/tony/Data/Preference/13b_v0/interesting_clips_v3p5_s_8_20240813.csv\"\n",
    "# )  # , engine='python')\n",
    "df = pd.read_pickle(\n",
    "    \"/home/tony/Data/Preference/30b_v2/interesting_clips_13b_s8_20240925_full_l10.pkl\"\n",
    ")  # , engine='python')\n",
    "print(\"Preference data shape\", df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.199480Z",
     "start_time": "2024-05-16T13:58:53.963687Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:25:54.503724Z",
     "iopub.status.busy": "2024-09-25T21:25:54.503330Z",
     "iopub.status.idle": "2024-09-25T21:29:14.196517Z",
     "shell.execute_reply": "2024-09-25T21:29:14.195938Z",
     "shell.execute_reply.started": "2024-09-25T21:25:54.503707Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6855989\n",
      "6855989\n",
      "pre-downloaded df (477514, 71)\n",
      "downloaded df (477514, 71)\n"
     ]
    }
   ],
   "source": [
    "converted_paths = os.listdir(NPZ_DIR)\n",
    "print(len(converted_paths))\n",
    "\n",
    "converted_paths = set([f.replace(\".npz\", \"\") for f in converted_paths])\n",
    "print(len(converted_paths))\n",
    "\n",
    "if \"cycle\" in NPZ_DIR:\n",
    "    # hack in the cycle label\n",
    "    df[\"s3_id\"] += \"_gen_cycle\"\n",
    "\n",
    "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": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.467253Z",
     "start_time": "2024-05-16T13:58:56.207647Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:29:14.197358Z",
     "iopub.status.busy": "2024-09-25T21:29:14.197200Z",
     "iopub.status.idle": "2024-09-25T21:29:14.547448Z",
     "shell.execute_reply": "2024-09-25T21:29:14.546964Z",
     "shell.execute_reply.started": "2024-09-25T21:29:14.197342Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "is_13b\n",
       "True    477514\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"is_13b\"] = df[\"model_name\"].str.contains(\"v3p5\")\n",
    "df[\"is_13b\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LET's do the data prep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.592883Z",
     "start_time": "2024-05-16T13:58:56.470781Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:29:14.548276Z",
     "iopub.status.busy": "2024-09-25T21:29:14.548123Z",
     "iopub.status.idle": "2024-09-25T21:29:14.827137Z",
     "shell.execute_reply": "2024-09-25T21:29:14.826610Z",
     "shell.execute_reply.started": "2024-09-25T21:29:14.548260Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "preference  model_name           \n",
      "False       chirp-v3p5-engine-s-8    238757\n",
      "True        chirp-v3p5-engine-s-8    238757\n",
      "Name: count, dtype: int64\n",
      "(477514, 72)\n",
      "(477514, 72)\n"
     ]
    }
   ],
   "source": [
    "## for 13b this is easy for now\n",
    "print(df.groupby([\"preference\"])[\"model_name\"].value_counts())\n",
    "print(df.shape)\n",
    "df = df[df[\"model_name\"].isin([\"chirp-v3p5-engine-s-8\"])]\n",
    "print(df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.909539Z",
     "start_time": "2024-05-16T13:58:56.595736Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:29:14.827946Z",
     "iopub.status.busy": "2024-09-25T21:29:14.827792Z",
     "iopub.status.idle": "2024-09-25T21:29:15.683783Z",
     "shell.execute_reply": "2024-09-25T21:29:15.683194Z",
     "shell.execute_reply.started": "2024-09-25T21:29:14.827932Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(477514, 72)\n",
      "(477514, 72)\n",
      "preference  model_name           \n",
      "False       chirp-v3p5-engine-s-8    238757\n",
      "True        chirp-v3p5-engine-s-8    238757\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "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": {
    "execution": {
     "iopub.execute_input": "2024-09-25T21:29:15.684629Z",
     "iopub.status.busy": "2024-09-25T21:29:15.684478Z",
     "iopub.status.idle": "2024-09-25T21:29:15.686900Z",
     "shell.execute_reply": "2024-09-25T21:29:15.686491Z",
     "shell.execute_reply.started": "2024-09-25T21:29:15.684614Z"
    }
   },
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "def custom_parse(x):\n",
    "    try:\n",
    "        return json.loads(x)\n",
    "    except:\n",
    "        return {}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:36.043975Z",
     "start_time": "2024-05-16T13:58:56.910958Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:29:15.726905Z",
     "iopub.status.busy": "2024-09-25T21:29:15.726769Z",
     "iopub.status.idle": "2024-09-25T21:30:31.169493Z",
     "shell.execute_reply": "2024-09-25T21:30:31.168916Z",
     "shell.execute_reply.started": "2024-09-25T21:29:15.726892Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique_requests 238757\n"
     ]
    }
   ],
   "source": [
    "# Let's use the old selection for now -- for quality assurance\n",
    "# expand the metadata columns -- this takes forever...~ 6 mins\n",
    "# test_slice = df[\"metadata\"].apply(lambda x: ast.literal_eval(str(x)))\n",
    "test_slice = df[\"metadata\"].apply(lambda x: custom_parse(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": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:36.393047Z",
     "start_time": "2024-05-16T13:59:36.048831Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:31.170365Z",
     "iopub.status.busy": "2024-09-25T21:30:31.170214Z",
     "iopub.status.idle": "2024-09-25T21:30:31.219059Z",
     "shell.execute_reply": "2024-09-25T21:30:31.218571Z",
     "shell.execute_reply.started": "2024-09-25T21:30:31.170350Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique_requests 238757\n"
     ]
    }
   ],
   "source": [
    "# GPT requests are also fine for now\n",
    "print(\"unique_requests\", df[\"request_id\"].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:40.799375Z",
     "start_time": "2024-05-16T13:59:36.394236Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:31.219859Z",
     "iopub.status.busy": "2024-09-25T21:30:31.219717Z",
     "iopub.status.idle": "2024-09-25T21:30:35.749239Z",
     "shell.execute_reply": "2024-09-25T21:30:35.748660Z",
     "shell.execute_reply.started": "2024-09-25T21:30:31.219845Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "21928\n",
      "good_continue_at\n",
      "True     474002\n",
      "False      3512\n",
      "Name: count, dtype: int64\n",
      "\n",
      " Check some basics... \n",
      " preference\n",
      "False    238757\n",
      "True     238757\n",
      "Name: count, dtype: int64 is_13b\n",
      "True    477514\n",
      "Name: count, dtype: int64 model_name\n",
      "chirp-v3p5-engine-s-8    477514\n",
      "Name: count, dtype: int64 preference  model_name           \n",
      "False       chirp-v3p5-engine-s-8    238757\n",
      "True        chirp-v3p5-engine-s-8    238757\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df = df.loc[:, ~df.columns.duplicated()].copy()\n",
    "# get the original duration of the clips, if they are concacted\n",
    "df[\"original_duration_s\"] = df[\"total_start_s\"] + df[\"duration\"]\n",
    "# classify the continue at behavoirs by the duration choice\n",
    "audio_prompt_id_to_continue_at = {}\n",
    "for _, row in df[~df[\"continued_parent\"].isna()].iterrows():\n",
    "    audio_prompt_id = row[\"continued_parent\"]\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[\"s3_id\"].apply(\n",
    "    lambda x: audio_prompt_id_to_continue_at.get(x)\n",
    ")\n",
    "# we want continue at to be at most 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())\n",
    "\n",
    "\n",
    "print(\n",
    "    \"\\n Check some basics... \\n\",\n",
    "    df[\"preference\"].value_counts(),\n",
    "    df[\"is_13b\"].value_counts(),\n",
    "    df[\"model_name\"].value_counts(),\n",
    "    df.groupby([\"preference\"])[\"model_name\"].value_counts(),\n",
    ")\n",
    "\n",
    "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": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:35.751871Z",
     "iopub.status.busy": "2024-09-25T21:30:35.751425Z",
     "iopub.status.idle": "2024-09-25T21:30:35.988685Z",
     "shell.execute_reply": "2024-09-25T21:30:35.988191Z",
     "shell.execute_reply.started": "2024-09-25T21:30:35.751854Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pos_diff_preference\n",
       "2.0    238757\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"pos_diff_preference\"] = df[\"diff_preference\"].diff()\n",
    "df[df[\"preference\"]][\"pos_diff_preference\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.035167Z",
     "start_time": "2024-05-16T13:59:40.801098Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:35.989495Z",
     "iopub.status.busy": "2024-09-25T21:30:35.989347Z",
     "iopub.status.idle": "2024-09-25T21:30:36.558049Z",
     "shell.execute_reply": "2024-09-25T21:30:36.557465Z",
     "shell.execute_reply.started": "2024-09-25T21:30:35.989481Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "negative 233189 positive 108992\n",
      "total pair requests 238757 selected pair requests 106299 frac 0.445\n"
     ]
    }
   ],
   "source": [
    "normal_pos_play_count = 10\n",
    "# this is lower, cause a concat is probably already ensuring that it is good\n",
    "concat_pos_play_count = 2\n",
    "# this is a filter on the concated clip\n",
    "concat_total_play_count = 10\n",
    "\n",
    "neg_filter_selection_mask = (\n",
    "    (df[\"preference\"] == False)  # get basics aligned\n",
    "    & (df[\"reaction_play_count\"] >= 1)  # has to be played once\n",
    "    # & (df[\"play_count\"] <= 3)  # 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\"] <= 60)  # can't be badly long\n",
    "    & (df[\"has_continue_and_start_continue_at\"].isna())  # won't have any continues\n",
    "    & ((df[\"norm_play_frac\"] <= 2.1))\n",
    "    # & (df[\"dislike_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[\"reaction_play_count\"] >= 1)\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\"] <= 60)  # can't be badly long\n",
    "    & (df[\"dislike_count\"] == 0)  # can't have dislikes\n",
    "    & (df[\"flag_count\"] == 0)  # can't have issues\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[\"user_n_clips\"] >= 200)  # user needs to have genereated at least 20\n",
    "    # & (df[\"duration_rel_diff\"] < 10) # positive isn't just longer\n",
    "    # & ((df[\"upvote_count\"] >= 1) )\n",
    "    & ((df[\"norm_play_frac\"] >= 5.1) | (~df[\"continued_parent\"].isna()))\n",
    "    & (df[\"pos_diff_preference\"] == 2)\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": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.250737Z",
     "start_time": "2024-05-16T13:59:41.036434Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:36.558925Z",
     "iopub.status.busy": "2024-09-25T21:30:36.558766Z",
     "iopub.status.idle": "2024-09-25T21:30:37.067653Z",
     "shell.execute_reply": "2024-09-25T21:30:37.067073Z",
     "shell.execute_reply.started": "2024-09-25T21:30:36.558910Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "requests 106299 clips 212598 total khrs 9.776; N gpus for 1000 iters 13.287; n unique users 27775\n"
     ]
    }
   ],
   "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 1000 iters {df_slice.shape[0] / 8 / 2 / 1000:.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": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.277006Z",
     "start_time": "2024-05-16T13:59:41.252105Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.068507Z",
     "iopub.status.busy": "2024-09-25T21:30:37.068353Z",
     "iopub.status.idle": "2024-09-25T21:30:37.131961Z",
     "shell.execute_reply": "2024-09-25T21:30:37.131415Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.068492Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "positive in playlist (47866, 116)\n"
     ]
    }
   ],
   "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": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.132772Z",
     "iopub.status.busy": "2024-09-25T21:30:37.132624Z",
     "iopub.status.idle": "2024-09-25T21:30:37.134891Z",
     "shell.execute_reply": "2024-09-25T21:30:37.134497Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.132757Z"
    }
   },
   "outputs": [],
   "source": [
    "# BREAK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.323409Z",
     "start_time": "2024-05-16T13:59:41.278278Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.135684Z",
     "iopub.status.busy": "2024-09-25T21:30:37.135550Z",
     "iopub.status.idle": "2024-09-25T21:30:37.173856Z",
     "shell.execute_reply": "2024-09-25T21:30:37.173480Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.135671Z"
    }
   },
   "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) # & (df_slice[\"dislike_count\"] < 1)\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": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.392244Z",
     "start_time": "2024-05-16T13:59:41.324472Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.174638Z",
     "iopub.status.busy": "2024-09-25T21:30:37.174391Z",
     "iopub.status.idle": "2024-09-25T21:30:37.209191Z",
     "shell.execute_reply": "2024-09-25T21:30:37.208815Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.174623Z"
    }
   },
   "outputs": [],
   "source": [
    "# save positive ids\n",
    "# positive_preference_ids = df_slice[df_slice[\"preference\"] == False][\"s3_id\"].to_json(orient='values')\n",
    "# with open('/home/tony/Data/Preference/7b_v2/7v_v20_full_recut_id_negative.json', 'w') as file:\n",
    "#     file.write(positive_preference_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T14:00:20.866354Z",
     "start_time": "2024-05-16T14:00:12.443344Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.209850Z",
     "iopub.status.busy": "2024-09-25T21:30:37.209726Z",
     "iopub.status.idle": "2024-09-25T21:30:37.245826Z",
     "shell.execute_reply": "2024-09-25T21:30:37.245448Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.209837Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/13b_v0/interesting_clips_v3p5_s_8_20240828_slice.csv\")\n",
    "# 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": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932296Z",
     "start_time": "2024-05-16T13:59:41.932287Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.246495Z",
     "iopub.status.busy": "2024-09-25T21:30:37.246367Z",
     "iopub.status.idle": "2024-09-25T21:30:37.471296Z",
     "shell.execute_reply": "2024-09-25T21:30:37.470806Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.246482Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "93010"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# don't have continue at\n",
    "df_slice[df_slice[\"continue_at\"].isna()][\"request_id\"].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932966Z",
     "start_time": "2024-05-16T13:59:41.932957Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.472114Z",
     "iopub.status.busy": "2024-09-25T21:30:37.471965Z",
     "iopub.status.idle": "2024-09-25T21:30:37.492510Z",
     "shell.execute_reply": "2024-09-25T21:30:37.492005Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.472100Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "106299\n"
     ]
    }
   ],
   "source": [
    "final_filtered_requests = df_slice[\"request_id\"].unique()\n",
    "print(len(final_filtered_requests))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.933558Z",
     "start_time": "2024-05-16T13:59:41.933550Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:37.493288Z",
     "iopub.status.busy": "2024-09-25T21:30:37.493142Z",
     "iopub.status.idle": "2024-09-25T21:30:37.525104Z",
     "shell.execute_reply": "2024-09-25T21:30:37.524714Z",
     "shell.execute_reply.started": "2024-09-25T21:30:37.493273Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/7b_v2/7b_before_recode_20240412\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934277Z",
     "start_time": "2024-05-16T13:59:41.934268Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:50.154227Z",
     "iopub.status.busy": "2024-09-25T21:30:50.153782Z",
     "iopub.status.idle": "2024-09-25T21:30:51.097980Z",
     "shell.execute_reply": "2024-09-25T21:30:51.097412Z",
     "shell.execute_reply.started": "2024-09-25T21:30:50.154209Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "105236 1063\n",
      "(210472, 116) (2126, 116)\n"
     ]
    }
   ],
   "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": 26,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:54.003389Z",
     "iopub.status.busy": "2024-09-25T21:30:54.002903Z",
     "iopub.status.idle": "2024-09-25T21:30:54.005376Z",
     "shell.execute_reply": "2024-09-25T21:30:54.004956Z",
     "shell.execute_reply.started": "2024-09-25T21:30:54.003372Z"
    }
   },
   "outputs": [],
   "source": [
    "# BREAK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Actually make"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934954Z",
     "start_time": "2024-05-16T13:59:41.934946Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:30:55.574120Z",
     "iopub.status.busy": "2024-09-25T21:30:55.573649Z",
     "iopub.status.idle": "2024-09-25T21:30:55.576069Z",
     "shell.execute_reply": "2024-09-25T21:30:55.575656Z",
     "shell.execute_reply.started": "2024-09-25T21:30:55.574103Z"
    }
   },
   "outputs": [],
   "source": [
    "# val_df[[\"request_id\", \"metadata\", \"updated_at\", \"user_id\", \"preference\"]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.935620Z",
     "start_time": "2024-05-16T13:59:41.935613Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:31:04.731969Z",
     "iopub.status.busy": "2024-09-25T21:31:04.731710Z",
     "iopub.status.idle": "2024-09-25T21:31:11.280629Z",
     "shell.execute_reply": "2024-09-25T21:31:11.280064Z",
     "shell.execute_reply.started": "2024-09-25T21:31:04.731954Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 210472/210472 [00:06<00:00, 32165.28it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9,679 hours of 210472 clips, 13.1545 nodes\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "total_duration = 0\n",
    "for i, row in 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(\n",
    "    f\"{round(total_duration / 60 / 60):,} hours of {train_df.shape[0]} clips, {train_df.shape[0] / 8 / 2 / 1000} nodes\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936268Z",
     "start_time": "2024-05-16T13:59:41.936260Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:31:11.281819Z",
     "iopub.status.busy": "2024-09-25T21:31:11.281658Z",
     "iopub.status.idle": "2024-09-25T21:31:56.671071Z",
     "shell.execute_reply": "2024-09-25T21:31:56.670524Z",
     "shell.execute_reply.started": "2024-09-25T21:31:11.281804Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 2126/2126 [00:45<00:00, 46.86it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 2126 clips, 22 different prompts\n",
      "49 hours of False\n",
      "48 hours of True\n",
      "Done\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "make_dataset(val_df, OUT_DATA_DIR, is_val=True, npz_dir=NPZ_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936964Z",
     "start_time": "2024-05-16T13:59:41.936957Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T21:31:56.671911Z",
     "iopub.status.busy": "2024-09-25T21:31:56.671759Z",
     "iopub.status.idle": "2024-09-25T22:41:51.815384Z",
     "shell.execute_reply": "2024-09-25T22:41:51.814823Z",
     "shell.execute_reply.started": "2024-09-25T21:31:56.671896Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 15%|██████████████▉                                                                                       | 30796/210472 [10:38<1:03:43, 46.99it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weird, /app/suno/data/dpo/13b_npz/737bcbae-2cca-409b-8172-202e62770dd0.npz, with only v4.0\n",
      "{\"tags\": \"rap trap, agressive, rap, hip hop, bass\", \"type\": \"gen\", \"prompt\": \"[Intro]\\nRandom man\\nRandom\\n(yea, random)\\n\\n[Verse 1]\\nCaminhando nessa city\\nTu ta sugando tudo from my díri\\nBebe tudo antes que espíre\\n(tudo antes que espíre)\\n\\n[Verse 2]\\nRemind me that\\n(Who are you?)\\nRandom, fodo tua mãe depois eu fodo tu\\nTu é mais fake que urubu\\n(vai tudo tomar no cu)\\n(haha)\\n\\n[Bridge]\\nWho are you?\\n(Random man)\\nWho are you?\\nRandom\\nWho are you?\\n(Random man)\\nWho are you?\\n\\n\\n[Chorus]\\nFrom the left\\nFrom the front\\nMy beck na estantí\\nBolado num estantí\\n\\nFrom the back\\nGold na minha Neck\\nJust the Wind from my léque\\n\\nta pronto, ta ready, ta tiéck\\ngambly blackjack\\njust puxa, just trága desse beck\\n\\n[Bridge]\\nWho are you?\\nRandom\\nWho are you?\\nRandom man\\n\\n[Verse 3]\\nShow me that\\nFrom the topo\\nPortando aéroporto e heliporto\\nMany riches from the esgôto.\\nFeici scam com o meu rosto.\\n(Feici scam com o meu rosto)\\n\\n[Verse 4]\\nMy bad you are Trash\\ncomigo tu nao mexe\\nde random a gente esquece\\nAbáut âs nou- uan forget\\n\\n[Chorus]\\nFrom the back\\nGold na minha Neck\\nJust the Wind from my léque\\n\\ngambly blackjack\\njust puxa, just trága desse beck\\n\\n[Bridge]\\nWho are you?\\n(Random)\\nWho are you?\\n(Random man)\\n\\n\\nMy bad you are Trash\\nde random a gente esquece\\nAbáut âs nou- uan forget\\n(sheesh)\\n\\n just trága desse beck\\nde random a gente esquece.\\n(sheesh)\\n\\nAbáut âs nou- uan forget\\nWho are you?\\n(Random)\\n[End]\", \"source\": \"web\", \"stream\": true, \"duration\": 174.2, \"priority\": 10, \"experiment\": [\"chirp-v3p5-engine-s-8\", \"chirp-v3p5-engine-s-8\"], \"gpt_prompt\": null, \"refund_credits\": false, \"param_experiment\": \"tag_cfg_text_cfg_11\", \"make_instrumental\": false, \"gpt_description_prompt\": null}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 80%|██████████████████████████████████████████████████████████████████████████████████▏                    | 167952/210472 [56:09<13:33, 52.26it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weird, /app/suno/data/dpo/13b_npz/6a546a9e-3a38-4517-b0a0-dde66baeb649.npz, with only v4.0\n",
      "{\"tags\": \"Male vocal, Chillwavewave,  Ambient Dub Bedroom Pop, Emotional, 808s\", \"type\": \"gen\", \"is_bot\": false, \"prompt\": \"[Catchy Intro Hook]\\n\\n[Catchy Instrumental Jam]\\n\\n[[Bridge]\\nIL, avait que 3ans\\nPourtant, il, s'en souviens\\nIL, a vu son père, devoir quitter la maison, du jour au lendemain\\nA cause d'un beau père,  qui venai de sortir de prison et ça ce n'est pas, rien \\n\\n[Hook}\\n[females vocals]\\nIl n'y a que les fait marquant qui change du quotidien\\nTu peu t'en souvenir longtemps, même si certains ne sont pas rien\\nIl faut battre t'es démon, pour essayer d'en ressortir QUE, DU, BON\\n\\n[Verse1]\\n[male vocal]\\nIl a grandi dans les cris et la guerre\\nA cause de ce faux daron qui se prenait pour un gangster\\nTous les jours il s'en prenait à sa mère\\nla pauvre devait suporter ses coups de nerfs\\n\\n[Catchy Instrumental Jam]\\n\\nMais il ne savait pas qu'elle envoyai son fils voir son père en cachette\\nParce qu'il avait  pas quitter la planète\\n\\nIL, habitai en ville\\nDans un petit studio tranquille\\nEt profitai des moments passer ensemble\\nParce qu'il se voyait peu et ça il fallait le comprendre\\nParce que ça n'a pas durée, il a bien fallu un jour se séparer\\n\\n[Hook}\\n[female voice]\\nIl n'y a que les fait marquant qui change du quotidien\\nTu peu t'en souvenir longtemps, même si certains ne sont pas bien\\nIl faut battre t'es démon, pour essayer d'en ressortir QUE, DU, BON\\n\\n[Verse2]\\nA 4ans le petit garçon\\nUn jour en a eu marre des cries et des pleures\\nQu'il entendant sans cesse dans sa demeure\\nAlors, il est sortie en fureur\\nSans que personne ne le voie courir vers sa mort\\nParce qu'avant de s'enfuir\\nIl a peu t'être eu tord\\nil a attraper un couteau qui trainai\\nIl ne savais pas pourquoi\\nMais il la FAIT\\n\\n[Catchy Instrumental Jam]\\n\\n[Verse3]\\nUne fois parti de la maison,\\nCe pauvre garçon avais trouver  refuge dans un petit bois, juste derrière chez lui\\nIl faisait beau, c'étais silencieux\\nIl a eu du pot, ce lieu était merveilleux\\nIl y avais des oiseaux qui chantaient\\nHeureusement qu'il est tomber sur ce coin de paix\\n\\n[Verse4]\\nIl pleurait et ne savais plus quoi faire\\nIl se voyais vivre tout les jours dans un enfer\\nIl voulais que tout s'arrête\\nIl n'en pouvais plus il voulais que tous cesse\\n\\n[Catchy Instrumental Jam]\\n\\n[Verse]\\nIl était a  deux doigts de passer le pas\\nDans ce petit bois il a failli passer a trépas\\nMais dans sa tête il a entendu une petite voix\\n\\nElle lui a dit\\n\\\"Mais pourquoi tu ferais ça ?\\\"\\n\\\"Oubli les soucis qui te mettent dans cette état\\\"\\n\\\"Lève les yeux en l'air et pense à toi\\\" \\\"Imagine toi au dessus des nuages\\\"\\n\\\"C'est ce que font tous les enfants de ton âge\\\"\\n\\n[Hook}\\n[female voice]\\nIl n'y a que les fait marquant qui change du quotidien\\nTu peu t'en souvenir longtemps, même si certains ne sont pas rien\\nIl faut battre t'es démon, pour essayer d'en ressortir QUE, DU, BON\\n\\n[Verse]\\nAlors depuis ce jour pour oublier ses problèmes\\nDans sa tête tous les jours il imagina des merveilles\\nIl imaginai toujours être au dessus des nuages\\nIl s'inventa des histoires d'un enfant de son âge\\n\\n[Catchy Instrumental Jam]\", \"source\": \"web\", \"stream\": true, \"user_id\": 30969275, \"duration\": 240.0, \"priority\": 0, \"experiment\": [\"chirp-v3p5-engine-s-8\", \"chirp-v3p5-engine-s-8\"], \"gpt_prompt\": null, \"refund_credits\": false, \"make_instrumental\": false, \"gpt_description_prompt\": null}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 98%|██████████████████████████████████████████████████████████████████████████████████████████████████▋  | 205658/210472 [1:08:19<01:34, 51.08it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weird, /app/suno/data/dpo/13b_npz/fc79baa2-ae7a-45aa-aa76-691e9b168750.npz, with only v4.0\n",
      "{\"tags\": \"afrobeat relaxing future sounds\", \"type\": \"gen\", \"is_bot\": false, \"prompt\": \"[Instrumental]\", \"source\": \"web\", \"stream\": true, \"user_id\": 19852368, \"duration\": 240.0, \"priority\": 10, \"experiment\": [\"chirp-v3p5-engine-s-8\", \"chirp-v3p5-engine-s-8\"], \"gpt_prompt\": null, \"refund_credits\": false, \"make_instrumental\": true, \"gpt_description_prompt\": \"binary form,  afrobeat, relaxing, future sounds\"}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 210472/210472 [1:09:54<00:00, 50.17it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 210472 clips, 1660 different prompts\n",
      "4,883 hours of False\n",
      "4,794 hours of True\n",
      "Done\n"
     ]
    }
   ],
   "source": [
    "make_dataset(train_df, OUT_DATA_DIR, is_val=False, npz_dir=NPZ_DIR)"
   ]
  },
  {
   "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": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.937879Z",
     "start_time": "2024-05-16T13:59:41.937870Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:51.816303Z",
     "iopub.status.busy": "2024-09-25T22:41:51.816151Z",
     "iopub.status.idle": "2024-09-25T22:41:51.975417Z",
     "shell.execute_reply": "2024-09-25T22:41:51.974950Z",
     "shell.execute_reply.started": "2024-09-25T22:41:51.816288Z"
    }
   },
   "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, 6016, 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": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.938629Z",
     "start_time": "2024-05-16T13:59:41.938621Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:51.976979Z",
     "iopub.status.busy": "2024-09-25T22:41:51.976622Z",
     "iopub.status.idle": "2024-09-25T22:41:51.978940Z",
     "shell.execute_reply": "2024-09-25T22:41:51.978554Z",
     "shell.execute_reply.started": "2024-09-25T22:41:51.976963Z"
    }
   },
   "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\"] = \"0\"\n",
    "# _ = preload_codec_models(\"/app/suno/data/dpo/models/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": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939205Z",
     "start_time": "2024-05-16T13:59:41.939198Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:51.979616Z",
     "iopub.status.busy": "2024-09-25T22:41:51.979487Z",
     "iopub.status.idle": "2024-09-25T22:41:52.017476Z",
     "shell.execute_reply": "2024-09-25T22:41:52.017086Z",
     "shell.execute_reply.started": "2024-09-25T22:41:51.979604Z"
    }
   },
   "outputs": [],
   "source": [
    "# import random\n",
    "# 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(\"\\n negative example \\n\", test_metas[idx])\n",
    "# a.play(compress=False)\n",
    "# pos_a = decode(pos_arr)\n",
    "# print(\"\\n positive example \\n\", 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": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939977Z",
     "start_time": "2024-05-16T13:59:41.939969Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.018142Z",
     "iopub.status.busy": "2024-09-25T22:41:52.018010Z",
     "iopub.status.idle": "2024-09-25T22:41:52.055324Z",
     "shell.execute_reply": "2024-09-25T22:41:52.054942Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.018129Z"
    }
   },
   "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": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.940610Z",
     "start_time": "2024-05-16T13:59:41.940603Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.056086Z",
     "iopub.status.busy": "2024-09-25T22:41:52.055948Z",
     "iopub.status.idle": "2024-09-25T22:41:52.090728Z",
     "shell.execute_reply": "2024-09-25T22:41:52.090348Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.056073Z"
    }
   },
   "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": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941167Z",
     "start_time": "2024-05-16T13:59:41.941159Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.091380Z",
     "iopub.status.busy": "2024-09-25T22:41:52.091238Z",
     "iopub.status.idle": "2024-09-25T22:41:52.128551Z",
     "shell.execute_reply": "2024-09-25T22:41:52.128172Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.091367Z"
    }
   },
   "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": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941801Z",
     "start_time": "2024-05-16T13:59:41.941793Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.129309Z",
     "iopub.status.busy": "2024-09-25T22:41:52.129181Z",
     "iopub.status.idle": "2024-09-25T22:41:52.168373Z",
     "shell.execute_reply": "2024-09-25T22:41:52.167931Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.129296Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1063 0\n"
     ]
    }
   ],
   "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": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.942520Z",
     "start_time": "2024-05-16T13:59:41.942511Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.169104Z",
     "iopub.status.busy": "2024-09-25T22:41:52.168960Z",
     "iopub.status.idle": "2024-09-25T22:41:52.220284Z",
     "shell.execute_reply": "2024-09-25T22:41:52.219863Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.169090Z"
    }
   },
   "outputs": [],
   "source": [
    "train_info = read_json(os.path.join(OUT_DATA_DIR, f\"info_tr.json\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.943072Z",
     "start_time": "2024-05-16T13:59:41.943065Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.220963Z",
     "iopub.status.busy": "2024-09-25T22:41:52.220827Z",
     "iopub.status.idle": "2024-09-25T22:41:52.247526Z",
     "shell.execute_reply": "2024-09-25T22:41:52.247119Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.220950Z"
    }
   },
   "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": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.944246Z",
     "start_time": "2024-05-16T13:59:41.944237Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.248218Z",
     "iopub.status.busy": "2024-09-25T22:41:52.248083Z",
     "iopub.status.idle": "2024-09-25T22:41:52.290877Z",
     "shell.execute_reply": "2024-09-25T22:41:52.290465Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.248205Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total samples 210472 (210472, 116)\n"
     ]
    }
   ],
   "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": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945249Z",
     "start_time": "2024-05-16T13:59:41.945241Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.292409Z",
     "iopub.status.busy": "2024-09-25T22:41:52.292266Z",
     "iopub.status.idle": "2024-09-25T22:41:52.336852Z",
     "shell.execute_reply": "2024-09-25T22:41:52.336428Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.292395Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 epoch per batch 4, total 3288.625\n"
     ]
    }
   ],
   "source": [
    "print(\"1 epoch per batch 4, total\", total_iters / 8 / 4 / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945972Z",
     "start_time": "2024-05-16T13:59:41.945964Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.337538Z",
     "iopub.status.busy": "2024-09-25T22:41:52.337401Z",
     "iopub.status.idle": "2024-09-25T22:41:52.853861Z",
     "shell.execute_reply": "2024-09-25T22:41:52.853291Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.337524Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitted batch job 2653\n"
     ]
    }
   ],
   "source": [
    "!cd /home/tony/Work/tony/slurm && sbatch sbatch_ipo_13b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.854839Z",
     "iopub.status.busy": "2024-09-25T22:41:52.854680Z",
     "iopub.status.idle": "2024-09-25T22:41:52.867732Z",
     "shell.execute_reply": "2024-09-25T22:41:52.867294Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.854822Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cache kept!\n"
     ]
    }
   ],
   "source": [
    "import shutil\n",
    "\n",
    "# Basic file copy\n",
    "shutil.copy('/home/tony/Work/tony/Preference/make_dataset_13b_v3p5data.ipynb', os.path.join(OUT_DATA_DIR, \"make_dataset.ipynb\"))\n",
    "print(\"Cache kept!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# some gymathtics loading prev data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.946562Z",
     "start_time": "2024-05-16T13:59:41.946555Z"
    },
    "execution": {
     "iopub.execute_input": "2024-09-25T22:41:52.868514Z",
     "iopub.status.busy": "2024-09-25T22:41:52.868366Z",
     "iopub.status.idle": "2024-09-25T22:41:52.895744Z",
     "shell.execute_reply": "2024-09-25T22:41:52.895366Z",
     "shell.execute_reply.started": "2024-09-25T22:41:52.868501Z"
    }
   },
   "outputs": [],
   "source": [
    "# prev_v3_data = \"/app/suno/data/dpo/7v_v20_full/\"\n",
    "\n",
    "# test_val_metas = read_jsonl(os.path.join(prev_v3_data, f\"meta_val.jsonl\"))\n",
    "# test_tr_metas = read_jsonl(os.path.join(prev_v3_data, f\"meta_tr.jsonl\"))\n",
    "\n",
    "# all_ids = set()\n",
    "# for meta in test_val_metas:\n",
    "#     all_ids.add(meta[\"id\"])\n",
    "# for meta in test_tr_metas:\n",
    "#     all_ids.add(meta[\"id\"])\n",
    "# print(len(all_ids), len(test_val_metas) + len(test_tr_metas))\n",
    "\n",
    "# all_ids = list(all_ids)\n",
    "# with open(\"/home/tony/Data/Preference/7b_v2/7v_v20_full_recut_id.json\", \"w\") as fp:\n",
    "#     json.dump(all_ids, fp)"
   ]
  }
 ],
 "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.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": 4
}
