{
 "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-08-02T05:05:38.205441Z",
     "iopub.status.busy": "2024-08-02T05:05:38.205008Z",
     "iopub.status.idle": "2024-08-02T05:05:40.188955Z",
     "shell.execute_reply": "2024-08-02T05:05:40.188337Z",
     "shell.execute_reply.started": "2024-08-02T05:05:38.205412Z"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "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_extend 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-08-02T05:05:40.268439Z",
     "iopub.status.busy": "2024-08-02T05:05:40.268210Z",
     "iopub.status.idle": "2024-08-02T05:05:40.329591Z",
     "shell.execute_reply": "2024-08-02T05:05:40.329028Z",
     "shell.execute_reply.started": "2024-08-02T05:05:40.268421Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/dpo/13b_feedback_v7/\"\n",
    "os.makedirs(OUT_DATA_DIR, exist_ok=True)\n",
    "shutil.copyfile(\n",
    "    \"/app/suno/data/chirp_v4/multi/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-08-02T05:05:45.500474Z",
     "iopub.status.busy": "2024-08-02T05:05:45.500113Z",
     "iopub.status.idle": "2024-08-02T05:05:46.558123Z",
     "shell.execute_reply": "2024-08-02T05:05:46.557389Z",
     "shell.execute_reply.started": "2024-08-02T05:05:45.500454Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preference data shape (30994, 47)\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\n",
    "    \"/home/tony/Data/Preference/13b_v0/interesting_clips_20240802_feedback.csv\"\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-08-02T05:05:46.559485Z",
     "iopub.status.busy": "2024-08-02T05:05:46.559302Z",
     "iopub.status.idle": "2024-08-02T05:07:51.747145Z",
     "shell.execute_reply": "2024-08-02T05:07:51.746542Z",
     "shell.execute_reply.started": "2024-08-02T05:05:46.559466Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5379938\n",
      "5379938\n",
      "pre-downloaded df (30994, 47)\n",
      "downloaded df (30039, 47)\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",
    "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-08-02T05:07:51.748301Z",
     "iopub.status.busy": "2024-08-02T05:07:51.748129Z",
     "iopub.status.idle": "2024-08-02T05:07:52.356820Z",
     "shell.execute_reply": "2024-08-02T05:07:52.356340Z",
     "shell.execute_reply.started": "2024-08-02T05:07:51.748283Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "is_13b\n",
       "True     30026\n",
       "False       13\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": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:07:52.357773Z",
     "iopub.status.busy": "2024-08-02T05:07:52.357620Z",
     "iopub.status.idle": "2024-08-02T05:07:52.395407Z",
     "shell.execute_reply": "2024-08-02T05:07:52.394937Z",
     "shell.execute_reply.started": "2024-08-02T05:07:52.357757Z"
    }
   },
   "outputs": [],
   "source": [
    "# this is a big hack but for feedback preference we need to make it compatible with before\n",
    "df[\"preference\"] = df[\"feedback_preference\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.592883Z",
     "start_time": "2024-05-16T13:58:56.470781Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:07:52.396596Z",
     "iopub.status.busy": "2024-08-02T05:07:52.396323Z",
     "iopub.status.idle": "2024-08-02T05:07:52.435630Z",
     "shell.execute_reply": "2024-08-02T05:07:52.435163Z",
     "shell.execute_reply.started": "2024-08-02T05:07:52.396579Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "preference  model_name                    \n",
      "False       chirp-v3p5-engine-s-8             11849\n",
      "            chirp-v3p5-engine-upload-4         1487\n",
      "            chirp-v3p5-engine-t-1               568\n",
      "            chirp-v3p5-engine-ft                443\n",
      "            chirp-v3p5-engine-ft-1              252\n",
      "            chirp-v3p5-engine-upload            182\n",
      "            chirp-v3p5-engine-s-14               62\n",
      "            chirp-v3p5-engine-ft-5               59\n",
      "            chirp-v3p5-engine-s-8-no-top-p       18\n",
      "            chirp-v3p5-engine-t-1-7              15\n",
      "            chirp-v3p5-engine-s-20               13\n",
      "            chirp-v3p5-engine-ft-4                9\n",
      "            chirp-v3p5-engine-s-8-paged           8\n",
      "            chirp-v3p5-engine-ft-6                7\n",
      "            chirp-v3p5-engine-t-1-5               7\n",
      "            chirp-v3p5-engine-t-1-fast-sem        7\n",
      "            chirp-v3p5-engine-ft-3                6\n",
      "            chirp-v3p5-engine-ft-2                5\n",
      "            chirp-v3p5-engine-t-1-fast            5\n",
      "            chirp-v3p5-engine-s-19                4\n",
      "            chirp-v3p5-engine-t                   4\n",
      "            chirp-v3-engine-i                     3\n",
      "            chirp-v3p5-engine-s-18                2\n",
      "            chirp-v3p5-engine-b                   1\n",
      "True        chirp-v3p5-engine-s-8             11904\n",
      "            chirp-v3p5-engine-upload-4         1488\n",
      "            chirp-v3p5-engine-t-1               566\n",
      "            chirp-v3p5-engine-ft                439\n",
      "            chirp-v3p5-engine-ft-1              226\n",
      "            chirp-v3p5-engine-upload            181\n",
      "            chirp-v3p5-engine-ft-5               68\n",
      "            chirp-v3p5-engine-s-14               38\n",
      "            chirp-v3p5-engine-s-20               21\n",
      "            chirp-v3p5-engine-ft-4               16\n",
      "            chirp-v3p5-engine-s-8-no-top-p       14\n",
      "            chirp-v3p5-engine-ft-3                9\n",
      "            chirp-v3p5-engine-t-1-7               9\n",
      "            chirp-v3-engine-i                     8\n",
      "            chirp-v3p5-engine-s-8-paged           8\n",
      "            chirp-v3p5-engine-t-1-5               5\n",
      "            chirp-v3p5-engine-t-1-fast-sem        5\n",
      "            chirp-v3p5-engine-ft-6                4\n",
      "            chirp-v3p5-engine-s-19                4\n",
      "            chirp-v3p5-engine-t-1-fast            4\n",
      "            chirp-v2-xxl-alpha                    2\n",
      "            chirp-v3p5-engine-b                   1\n",
      "            chirp-v3p5-engine-ft-2                1\n",
      "            chirp-v3p5-engine-s-18                1\n",
      "            chirp-v3p5-engine-t                   1\n",
      "Name: count, dtype: int64\n",
      "(30039, 48)\n"
     ]
    }
   ],
   "source": [
    "## for 13b this is easy for now\n",
    "print(df.groupby([\"preference\"])[\"model_name\"].value_counts())\n",
    "print(df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:21.936897Z",
     "iopub.status.busy": "2024-08-02T05:08:21.936544Z",
     "iopub.status.idle": "2024-08-02T05:08:21.966956Z",
     "shell.execute_reply": "2024-08-02T05:08:21.966446Z",
     "shell.execute_reply.started": "2024-08-02T05:08:21.936878Z"
    }
   },
   "outputs": [],
   "source": [
    "df = df[df[\"model_name\"] == \"chirp-v3p5-engine-s-8\"].copy()\n",
    "# df = df[~df[\"model_name\"].str.contains(\"engine-t\")].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.909539Z",
     "start_time": "2024-05-16T13:58:56.595736Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:23.475846Z",
     "iopub.status.busy": "2024-08-02T05:08:23.475558Z",
     "iopub.status.idle": "2024-08-02T05:08:23.512249Z",
     "shell.execute_reply": "2024-08-02T05:08:23.511741Z",
     "shell.execute_reply.started": "2024-08-02T05:08:23.475827Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(23753, 48)\n",
      "(23484, 48)\n",
      "preference  model_name           \n",
      "False       chirp-v3p5-engine-s-8    11742\n",
      "True        chirp-v3p5-engine-s-8    11742\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": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:36.043975Z",
     "start_time": "2024-05-16T13:58:56.910958Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:26.796792Z",
     "iopub.status.busy": "2024-08-02T05:08:26.796471Z",
     "iopub.status.idle": "2024-08-02T05:08:33.459877Z",
     "shell.execute_reply": "2024-08-02T05:08:33.459288Z",
     "shell.execute_reply.started": "2024-08-02T05:08:26.796773Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique_requests 11742\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(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": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:34.056300Z",
     "iopub.status.busy": "2024-08-02T05:08:34.056034Z",
     "iopub.status.idle": "2024-08-02T05:08:36.721280Z",
     "shell.execute_reply": "2024-08-02T05:08:36.720688Z",
     "shell.execute_reply.started": "2024-08-02T05:08:34.056282Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pre-downloaded audio_prompt df (23484, 79)\n",
      "(3680, 79)\n",
      "downloaded df (23484, 79)\n"
     ]
    }
   ],
   "source": [
    "print(\"pre-downloaded audio_prompt df\", df.shape)\n",
    "print(df[df[\"audio_prompt_id\"].isin(converted_paths)].shape)\n",
    "# df = df[df[\"audio_prompt_id\"].isin(converted_paths)].copy()\n",
    "print(\"downloaded df\", df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:36.393047Z",
     "start_time": "2024-05-16T13:59:36.048831Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:36.722312Z",
     "iopub.status.busy": "2024-08-02T05:08:36.722149Z",
     "iopub.status.idle": "2024-08-02T05:08:36.741631Z",
     "shell.execute_reply": "2024-08-02T05:08:36.741152Z",
     "shell.execute_reply.started": "2024-08-02T05:08:36.722295Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique_requests 11742\n"
     ]
    }
   ],
   "source": [
    "# GPT requests are also fine for now\n",
    "print(\"unique_requests\", df[\"request_id\"].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:37.316022Z",
     "iopub.status.busy": "2024-08-02T05:08:37.315861Z",
     "iopub.status.idle": "2024-08-02T05:08:37.365082Z",
     "shell.execute_reply": "2024-08-02T05:08:37.364591Z",
     "shell.execute_reply.started": "2024-08-02T05:08:37.316005Z"
    }
   },
   "outputs": [],
   "source": [
    "df = df.loc[:,~df.columns.duplicated()].copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:38.540440Z",
     "iopub.status.busy": "2024-08-02T05:08:38.540030Z",
     "iopub.status.idle": "2024-08-02T05:08:38.556203Z",
     "shell.execute_reply": "2024-08-02T05:08:38.555743Z",
     "shell.execute_reply.started": "2024-08-02T05:08:38.540421Z"
    }
   },
   "outputs": [],
   "source": [
    "df[\"total_start_s\"] = 0\n",
    "df[\"total_clip_s\"] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:40.799375Z",
     "start_time": "2024-05-16T13:59:36.394236Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:41.620640Z",
     "iopub.status.busy": "2024-08-02T05:08:41.620394Z",
     "iopub.status.idle": "2024-08-02T05:08:41.776946Z",
     "shell.execute_reply": "2024-08-02T05:08:41.776450Z",
     "shell.execute_reply.started": "2024-08-02T05:08:41.620622Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1110\n",
      "good_continue_at\n",
      "True     23335\n",
      "False      149\n",
      "Name: count, dtype: int64\n",
      "\n",
      " Check some basics... \n",
      " preference\n",
      "False    11742\n",
      "True     11742\n",
      "Name: count, dtype: int64 is_13b\n",
      "True    23484\n",
      "Name: count, dtype: int64 model_name\n",
      "chirp-v3p5-engine-s-8    23484\n",
      "Name: count, dtype: int64 preference  model_name           \n",
      "False       chirp-v3p5-engine-s-8    11742\n",
      "True        chirp-v3p5-engine-s-8    11742\n",
      "Name: count, dtype: int64\n",
      "(23484, 81)\n"
     ]
    }
   ],
   "source": [
    "# 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[\"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 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",
    "print(df.shape)\n",
    "# df[\"play_rel_diff\"] = df['reaction_play_count'].diff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.035167Z",
     "start_time": "2024-05-16T13:59:40.801098Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:44.085773Z",
     "iopub.status.busy": "2024-08-02T05:08:44.085286Z",
     "iopub.status.idle": "2024-08-02T05:08:44.129751Z",
     "shell.execute_reply": "2024-08-02T05:08:44.129233Z",
     "shell.execute_reply.started": "2024-08-02T05:08:44.085752Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "negative 11631 positive 10372\n",
      "total pair requests 11742 selected pair requests 10273 frac 0.875\n"
     ]
    }
   ],
   "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\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\"] <= 240)  # can't be badly long\n",
    "    # & (df[\"has_continue_and_start_continue_at\"].isna())  # won't have any continues\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\"] <= 240)  # 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[\"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[\"play_count\"] >= normal_pos_play_count)\n",
    "    #     )\n",
    "    # )\n",
    "    # & (df[\"user_n_clips\"] >= 20)  # 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",
    ")\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": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:49.261780Z",
     "iopub.status.busy": "2024-08-02T05:08:49.261464Z",
     "iopub.status.idle": "2024-08-02T05:08:49.277850Z",
     "shell.execute_reply": "2024-08-02T05:08:49.277375Z",
     "shell.execute_reply.started": "2024-08-02T05:08:49.261761Z"
    }
   },
   "outputs": [],
   "source": [
    "# df[(df[\"neg_feedback\"] == True) & (df[\"upvote_count\"] != 0)][\"user_id\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:49.760214Z",
     "iopub.status.busy": "2024-08-02T05:08:49.759908Z",
     "iopub.status.idle": "2024-08-02T05:08:49.774326Z",
     "shell.execute_reply": "2024-08-02T05:08:49.773892Z",
     "shell.execute_reply.started": "2024-08-02T05:08:49.760195Z"
    }
   },
   "outputs": [],
   "source": [
    "# df[(df[\"pos_feedback\"] == True) & (df[\"dislike_count\"] != 0)][\"user_id\"].value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Overwrite with only quality"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:51.368475Z",
     "iopub.status.busy": "2024-08-02T05:08:51.368016Z",
     "iopub.status.idle": "2024-08-02T05:08:51.382476Z",
     "shell.execute_reply": "2024-08-02T05:08:51.382026Z",
     "shell.execute_reply.started": "2024-08-02T05:08:51.368456Z"
    }
   },
   "outputs": [],
   "source": [
    "# import json\n",
    "\n",
    "# with open(\n",
    "#     \"/home/tony/Data/Preference/13b_v0/interesting_clips_20240627_feedback_bad_audio_quality_ids.json\",\n",
    "#     \"r\",\n",
    "# ) as fp:\n",
    "#     bad_audio_quality_ids = json.load(fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:52.012047Z",
     "iopub.status.busy": "2024-08-02T05:08:52.011767Z",
     "iopub.status.idle": "2024-08-02T05:08:52.293149Z",
     "shell.execute_reply": "2024-08-02T05:08:52.292710Z",
     "shell.execute_reply.started": "2024-08-02T05:08:52.012028Z"
    }
   },
   "outputs": [],
   "source": [
    "# unique_requests = df[df[\"id\"].isin(bad_audio_quality_ids)][\"request_id\"].unique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.250737Z",
     "start_time": "2024-05-16T13:59:41.036434Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:54.088368Z",
     "iopub.status.busy": "2024-08-02T05:08:54.088136Z",
     "iopub.status.idle": "2024-08-02T05:08:54.127436Z",
     "shell.execute_reply": "2024-08-02T05:08:54.126957Z",
     "shell.execute_reply.started": "2024-08-02T05:08:54.088351Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "requests 10273 clips 20546 total khrs 0.957; N gpus for 1000 iters 1.284; n unique users 684\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": 22,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:57.552455Z",
     "iopub.status.busy": "2024-08-02T05:08:57.552146Z",
     "iopub.status.idle": "2024-08-02T05:08:57.568577Z",
     "shell.execute_reply": "2024-08-02T05:08:57.568118Z",
     "shell.execute_reply.started": "2024-08-02T05:08:57.552436Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice[df_slice[\"id\"].isin(bad_audio_quality_ids)][\"preference\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.277006Z",
     "start_time": "2024-05-16T13:59:41.252105Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:58.076360Z",
     "iopub.status.busy": "2024-08-02T05:08:58.075996Z",
     "iopub.status.idle": "2024-08-02T05:08:58.090399Z",
     "shell.execute_reply": "2024-08-02T05:08:58.089972Z",
     "shell.execute_reply.started": "2024-08-02T05:08:58.076342Z"
    }
   },
   "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": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.323409Z",
     "start_time": "2024-05-16T13:59:41.278278Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:58.200551Z",
     "iopub.status.busy": "2024-08-02T05:08:58.200266Z",
     "iopub.status.idle": "2024-08-02T05:08:58.213423Z",
     "shell.execute_reply": "2024-08-02T05:08:58.212977Z",
     "shell.execute_reply.started": "2024-08-02T05:08:58.200532Z"
    }
   },
   "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": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.392244Z",
     "start_time": "2024-05-16T13:59:41.324472Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:59.133031Z",
     "iopub.status.busy": "2024-08-02T05:08:59.132678Z",
     "iopub.status.idle": "2024-08-02T05:08:59.144668Z",
     "shell.execute_reply": "2024-08-02T05:08:59.144248Z",
     "shell.execute_reply.started": "2024-08-02T05:08:59.133013Z"
    }
   },
   "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": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T14:00:20.866354Z",
     "start_time": "2024-05-16T14:00:12.443344Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:08:59.413767Z",
     "iopub.status.busy": "2024-08-02T05:08:59.413621Z",
     "iopub.status.idle": "2024-08-02T05:08:59.425313Z",
     "shell.execute_reply": "2024-08-02T05:08:59.424896Z",
     "shell.execute_reply.started": "2024-08-02T05:08:59.413752Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/7b_v0/interesting_clips_v3_processed.csv\")"
   ]
  },
  {
   "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": 27,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:00.340204Z",
     "iopub.status.busy": "2024-08-02T05:09:00.339953Z",
     "iopub.status.idle": "2024-08-02T05:09:00.356119Z",
     "shell.execute_reply": "2024-08-02T05:09:00.355706Z",
     "shell.execute_reply.started": "2024-08-02T05:09:00.340187Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       NaN\n",
       "1       NaN\n",
       "2       NaN\n",
       "3       NaN\n",
       "4       NaN\n",
       "         ..\n",
       "30983   NaN\n",
       "30990   NaN\n",
       "30991   NaN\n",
       "30992   NaN\n",
       "30993   NaN\n",
       "Name: continue_at, Length: 20546, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_slice[\"continue_at\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932296Z",
     "start_time": "2024-05-16T13:59:41.932287Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:00.808116Z",
     "iopub.status.busy": "2024-08-02T05:09:00.807791Z",
     "iopub.status.idle": "2024-08-02T05:09:00.833784Z",
     "shell.execute_reply": "2024-08-02T05:09:00.833368Z",
     "shell.execute_reply.started": "2024-08-02T05:09:00.808099Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8754"
      ]
     },
     "execution_count": 28,
     "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": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932966Z",
     "start_time": "2024-05-16T13:59:41.932957Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:01.564141Z",
     "iopub.status.busy": "2024-08-02T05:09:01.563804Z",
     "iopub.status.idle": "2024-08-02T05:09:01.578182Z",
     "shell.execute_reply": "2024-08-02T05:09:01.577720Z",
     "shell.execute_reply.started": "2024-08-02T05:09:01.564122Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10273\n"
     ]
    }
   ],
   "source": [
    "final_filtered_requests = df_slice[\"request_id\"].unique()\n",
    "print(len(final_filtered_requests))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.933558Z",
     "start_time": "2024-05-16T13:59:41.933550Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:02.333549Z",
     "iopub.status.busy": "2024-08-02T05:09:02.333197Z",
     "iopub.status.idle": "2024-08-02T05:09:02.345351Z",
     "shell.execute_reply": "2024-08-02T05:09:02.344925Z",
     "shell.execute_reply.started": "2024-08-02T05:09:02.333532Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/7b_v2/7b_before_recode_20240412\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934277Z",
     "start_time": "2024-05-16T13:59:41.934268Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:02.812678Z",
     "iopub.status.busy": "2024-08-02T05:09:02.812276Z",
     "iopub.status.idle": "2024-08-02T05:09:02.864825Z",
     "shell.execute_reply": "2024-08-02T05:09:02.864357Z",
     "shell.execute_reply.started": "2024-08-02T05:09:02.812660Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10170 103\n",
      "(20340, 82) (206, 82)\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": "markdown",
   "metadata": {},
   "source": [
    "# Actually make"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934954Z",
     "start_time": "2024-05-16T13:59:41.934946Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:09.084716Z",
     "iopub.status.busy": "2024-08-02T05:09:09.084404Z",
     "iopub.status.idle": "2024-08-02T05:09:09.100754Z",
     "shell.execute_reply": "2024-08-02T05:09:09.100292Z",
     "shell.execute_reply.started": "2024-08-02T05:09:09.084698Z"
    }
   },
   "outputs": [],
   "source": [
    "# val_df[[\"request_id\", \"metadata\", \"updated_at\", \"user_id\", \"preference\"]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.935620Z",
     "start_time": "2024-05-16T13:59:41.935613Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:09.637313Z",
     "iopub.status.busy": "2024-08-02T05:09:09.637016Z",
     "iopub.status.idle": "2024-08-02T05:09:10.249256Z",
     "shell.execute_reply": "2024-08-02T05:09:10.248684Z",
     "shell.execute_reply.started": "2024-08-02T05:09:09.637295Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 20340/20340 [00:00<00:00, 34208.13it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "947 hours of 20340 clips, 1.8160714285714286 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 / 700} nodes\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936268Z",
     "start_time": "2024-05-16T13:59:41.936260Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:18.340514Z",
     "iopub.status.busy": "2024-08-02T05:09:18.340199Z",
     "iopub.status.idle": "2024-08-02T05:09:21.768680Z",
     "shell.execute_reply": "2024-08-02T05:09:21.767979Z",
     "shell.execute_reply.started": "2024-08-02T05:09:18.340495Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████| 206/206 [00:03<00:00, 60.56it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 206 clips, 0 different prompts\n",
      "5 hours of False\n",
      "5 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": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936964Z",
     "start_time": "2024-05-16T13:59:41.936957Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:09:22.380356Z",
     "iopub.status.busy": "2024-08-02T05:09:22.380158Z",
     "iopub.status.idle": "2024-08-02T05:15:01.536875Z",
     "shell.execute_reply": "2024-08-02T05:15:01.536211Z",
     "shell.execute_reply.started": "2024-08-02T05:09:22.380338Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 20340/20340 [05:39<00:00, 59.98it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 20340 clips, 40 different prompts\n",
      "473 hours of False\n",
      "474 hours of True\n",
      "Done\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\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": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.937879Z",
     "start_time": "2024-05-16T13:59:41.937870Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:01.037164Z",
     "iopub.status.busy": "2024-08-02T05:29:01.036780Z",
     "iopub.status.idle": "2024-08-02T05:29:01.345824Z",
     "shell.execute_reply": "2024-08-02T05:29:01.345225Z",
     "shell.execute_reply.started": "2024-08-02T05:29:01.037144Z"
    }
   },
   "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": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.938629Z",
     "start_time": "2024-05-16T13:59:41.938621Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:01.520691Z",
     "iopub.status.busy": "2024-08-02T05:29:01.520347Z",
     "iopub.status.idle": "2024-08-02T05:29:01.535118Z",
     "shell.execute_reply": "2024-08-02T05:29:01.534583Z",
     "shell.execute_reply.started": "2024-08-02T05:29:01.520672Z"
    }
   },
   "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": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939205Z",
     "start_time": "2024-05-16T13:59:41.939198Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:01.848755Z",
     "iopub.status.busy": "2024-08-02T05:29:01.848340Z",
     "iopub.status.idle": "2024-08-02T05:29:01.862210Z",
     "shell.execute_reply": "2024-08-02T05:29:01.861695Z",
     "shell.execute_reply.started": "2024-08-02T05:29:01.848736Z"
    }
   },
   "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": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939977Z",
     "start_time": "2024-05-16T13:59:41.939969Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:01.972314Z",
     "iopub.status.busy": "2024-08-02T05:29:01.971909Z",
     "iopub.status.idle": "2024-08-02T05:29:01.984089Z",
     "shell.execute_reply": "2024-08-02T05:29:01.983570Z",
     "shell.execute_reply.started": "2024-08-02T05:29:01.972295Z"
    }
   },
   "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": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.940610Z",
     "start_time": "2024-05-16T13:59:41.940603Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:02.256278Z",
     "iopub.status.busy": "2024-08-02T05:29:02.255988Z",
     "iopub.status.idle": "2024-08-02T05:29:02.268035Z",
     "shell.execute_reply": "2024-08-02T05:29:02.267525Z",
     "shell.execute_reply.started": "2024-08-02T05:29:02.256259Z"
    }
   },
   "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": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941167Z",
     "start_time": "2024-05-16T13:59:41.941159Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:02.896414Z",
     "iopub.status.busy": "2024-08-02T05:29:02.896134Z",
     "iopub.status.idle": "2024-08-02T05:29:02.908282Z",
     "shell.execute_reply": "2024-08-02T05:29:02.907765Z",
     "shell.execute_reply.started": "2024-08-02T05:29:02.896396Z"
    }
   },
   "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": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941801Z",
     "start_time": "2024-05-16T13:59:41.941793Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:03.132657Z",
     "iopub.status.busy": "2024-08-02T05:29:03.132257Z",
     "iopub.status.idle": "2024-08-02T05:29:03.145924Z",
     "shell.execute_reply": "2024-08-02T05:29:03.145360Z",
     "shell.execute_reply.started": "2024-08-02T05:29:03.132638Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "103 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": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.942520Z",
     "start_time": "2024-05-16T13:59:41.942511Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:03.696372Z",
     "iopub.status.busy": "2024-08-02T05:29:03.695950Z",
     "iopub.status.idle": "2024-08-02T05:29:03.710262Z",
     "shell.execute_reply": "2024-08-02T05:29:03.709733Z",
     "shell.execute_reply.started": "2024-08-02T05:29:03.696352Z"
    }
   },
   "outputs": [],
   "source": [
    "train_info = read_json(os.path.join(OUT_DATA_DIR, f\"info_tr.json\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.943072Z",
     "start_time": "2024-05-16T13:59:41.943065Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:04.468438Z",
     "iopub.status.busy": "2024-08-02T05:29:04.468133Z",
     "iopub.status.idle": "2024-08-02T05:29:04.484906Z",
     "shell.execute_reply": "2024-08-02T05:29:04.484423Z",
     "shell.execute_reply.started": "2024-08-02T05:29:04.468419Z"
    }
   },
   "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": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.944246Z",
     "start_time": "2024-05-16T13:59:41.944237Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:04.808797Z",
     "iopub.status.busy": "2024-08-02T05:29:04.808255Z",
     "iopub.status.idle": "2024-08-02T05:29:04.824204Z",
     "shell.execute_reply": "2024-08-02T05:29:04.823717Z",
     "shell.execute_reply.started": "2024-08-02T05:29:04.808773Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total samples 20340 (20340, 82)\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": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945249Z",
     "start_time": "2024-05-16T13:59:41.945241Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:05.536076Z",
     "iopub.status.busy": "2024-08-02T05:29:05.535912Z",
     "iopub.status.idle": "2024-08-02T05:29:05.549317Z",
     "shell.execute_reply": "2024-08-02T05:29:05.548862Z",
     "shell.execute_reply.started": "2024-08-02T05:29:05.536060Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 epoch per batch 4, total 635.625\n"
     ]
    }
   ],
   "source": [
    "print(\"1 epoch per batch 4, total\", total_iters / 8 / 2 / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945972Z",
     "start_time": "2024-05-16T13:59:41.945964Z"
    },
    "execution": {
     "iopub.execute_input": "2024-08-02T05:29:07.260723Z",
     "iopub.status.busy": "2024-08-02T05:29:07.260185Z",
     "iopub.status.idle": "2024-08-02T05:29:07.474356Z",
     "shell.execute_reply": "2024-08-02T05:29:07.473798Z",
     "shell.execute_reply.started": "2024-08-02T05:29:07.260702Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitted batch job 1303\n"
     ]
    }
   ],
   "source": [
    "!cd /home/tony/Work/tony/slurm/feedback/ && sbatch sbatch_ipo_13b_feedback"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# some gymathtics loading prev data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.946562Z",
     "start_time": "2024-05-16T13:59:41.946555Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-19T05:01:11.985249Z",
     "iopub.status.busy": "2024-07-19T05:01:11.985061Z",
     "iopub.status.idle": "2024-07-19T05:01:12.006061Z",
     "shell.execute_reply": "2024-07-19T05:01:12.005532Z",
     "shell.execute_reply.started": "2024-07-19T05:01:11.985228Z"
    }
   },
   "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)"
   ]
  },
  {
   "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.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
}
