{
 "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-07-01T02:17:04.169987Z",
     "iopub.status.busy": "2024-07-01T02:17:04.169843Z",
     "iopub.status.idle": "2024-07-01T02:17:05.271262Z",
     "shell.execute_reply": "2024-07-01T02:17:05.270757Z",
     "shell.execute_reply.started": "2024-07-01T02:17:04.169970Z"
    }
   },
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "from sklearn.model_selection import train_test_split\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "import sys\n",
    "from collections import defaultdict\n",
    "from suno_utils.utils.text import (\n",
    "    write_jsonl,\n",
    "    read_jsonl,\n",
    "    write_json,\n",
    "    read_json,\n",
    ")\n",
    "import shutil\n",
    "import ast\n",
    "from preference_helper import *\n",
    "from preference_data_preparation_4min_13b_extend import *\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "pd.set_option('display.max_rows', 500)\n",
    "pd.set_option('display.max_columns', 500)\n",
    "pd.set_option('display.width', 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:21.082172Z",
     "start_time": "2024-05-16T13:58:21.041926Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:17:05.272150Z",
     "iopub.status.busy": "2024-07-01T02:17:05.271954Z",
     "iopub.status.idle": "2024-07-01T02:17:05.309973Z",
     "shell.execute_reply": "2024-07-01T02:17:05.309519Z",
     "shell.execute_reply.started": "2024-07-01T02:17:05.272132Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/dpo/13b_extend_mix_cycle/\"\n",
    "os.makedirs(OUT_DATA_DIR, exist_ok=True)\n",
    "shutil.copyfile(\"/app/suno/data/dpo/7v_v20_full/tokenizer_60k.json\", os.path.join(OUT_DATA_DIR, \"tokenizer_60k.json\"))\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-07-01T02:17:05.310648Z",
     "iopub.status.busy": "2024-07-01T02:17:05.310511Z",
     "iopub.status.idle": "2024-07-01T02:17:05.974544Z",
     "shell.execute_reply": "2024-07-01T02:17:05.974005Z",
     "shell.execute_reply.started": "2024-07-01T02:17:05.310633Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preference data shape (14129, 82)\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"/home/tony/Data/Preference/13b_v0/interesting_clips_20240624_mix_selected_pos.csv\") # , engine='python')\n",
    "print(\"Preference data shape\", df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-07-01T02:17:05.975315Z",
     "iopub.status.busy": "2024-07-01T02:17:05.975170Z",
     "iopub.status.idle": "2024-07-01T02:17:05.995323Z",
     "shell.execute_reply": "2024-07-01T02:17:05.994893Z",
     "shell.execute_reply.started": "2024-07-01T02:17:05.975300Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "is_13b\n",
       "True    14129\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"is_13b\"] = df[\"model_name\"].str.contains(\"v3p5\")\n",
    "df[\"is_13b\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-07-01T02:17:05.996168Z",
     "iopub.status.busy": "2024-07-01T02:17:05.996031Z",
     "iopub.status.idle": "2024-07-01T02:17:06.042712Z",
     "shell.execute_reply": "2024-07-01T02:17:06.042251Z",
     "shell.execute_reply.started": "2024-07-01T02:17:05.996153Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28258, 82)\n"
     ]
    }
   ],
   "source": [
    "df_negative = df.copy()\n",
    "df_negative[\"preference\"] = False\n",
    "df_negative[\"s3_id\"] += \"_gen_cycle\"\n",
    "df = pd.concat([df, df_negative])\n",
    "print(df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.199480Z",
     "start_time": "2024-05-16T13:58:53.963687Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:17:06.044400Z",
     "iopub.status.busy": "2024-07-01T02:17:06.044250Z",
     "iopub.status.idle": "2024-07-01T02:18:52.035053Z",
     "shell.execute_reply": "2024-07-01T02:18:52.034480Z",
     "shell.execute_reply.started": "2024-07-01T02:17:06.044385Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4829780\n",
      "4829780\n",
      "pre-downloaded df (28258, 82)\n",
      "downloaded df (28216, 82)\n"
     ]
    }
   ],
   "source": [
    "converted_paths = set(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": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.035915Z",
     "iopub.status.busy": "2024-07-01T02:18:52.035753Z",
     "iopub.status.idle": "2024-07-01T02:18:52.341742Z",
     "shell.execute_reply": "2024-07-01T02:18:52.341245Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.035898Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(28216, 82)\n",
      "(28174, 82)\n",
      "preference  model_name              \n",
      "False       chirp-v3p5-engine-s-2       10638\n",
      "            chirp-v3p5-engine-upload     3449\n",
      "True        chirp-v3p5-engine-s-2       10638\n",
      "            chirp-v3p5-engine-upload     3449\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": "markdown",
   "metadata": {},
   "source": [
    "# LET's do the data prep"
   ]
  },
  {
   "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": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.342645Z",
     "iopub.status.busy": "2024-07-01T02:18:52.342502Z",
     "iopub.status.idle": "2024-07-01T02:18:52.364415Z",
     "shell.execute_reply": "2024-07-01T02:18:52.363980Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.342629Z"
    }
   },
   "outputs": [],
   "source": [
    "df_slice = df.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.365116Z",
     "iopub.status.busy": "2024-07-01T02:18:52.364978Z",
     "iopub.status.idle": "2024-07-01T02:18:52.397689Z",
     "shell.execute_reply": "2024-07-01T02:18:52.397272Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.365101Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0          NaN\n",
       "1          NaN\n",
       "2        59.24\n",
       "3          NaN\n",
       "4        16.32\n",
       "         ...  \n",
       "14124      NaN\n",
       "14125      NaN\n",
       "14126      NaN\n",
       "14127     8.00\n",
       "14128      NaN\n",
       "Name: continue_at, Length: 28174, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_slice[\"continue_at\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932296Z",
     "start_time": "2024-05-16T13:59:41.932287Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.398405Z",
     "iopub.status.busy": "2024-07-01T02:18:52.398269Z",
     "iopub.status.idle": "2024-07-01T02:18:52.441065Z",
     "shell.execute_reply": "2024-07-01T02:18:52.440662Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.398390Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9971"
      ]
     },
     "execution_count": 10,
     "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": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.933558Z",
     "start_time": "2024-05-16T13:59:41.933550Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.441772Z",
     "iopub.status.busy": "2024-07-01T02:18:52.441634Z",
     "iopub.status.idle": "2024-07-01T02:18:52.465754Z",
     "shell.execute_reply": "2024-07-01T02:18:52.465337Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.441757Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/13b_v0/interesting_clips_20240624_mix_selected_pos_cycled.csv\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.466436Z",
     "iopub.status.busy": "2024-07-01T02:18:52.466296Z",
     "iopub.status.idle": "2024-07-01T02:18:52.503505Z",
     "shell.execute_reply": "2024-07-01T02:18:52.503068Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.466422Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14087\n"
     ]
    }
   ],
   "source": [
    "final_filtered_requests = df_slice[\"request_id\"].unique()\n",
    "print(len(final_filtered_requests))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934277Z",
     "start_time": "2024-05-16T13:59:41.934268Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.504189Z",
     "iopub.status.busy": "2024-07-01T02:18:52.504056Z",
     "iopub.status.idle": "2024-07-01T02:18:52.594855Z",
     "shell.execute_reply": "2024-07-01T02:18:52.594403Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.504175Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13946 141\n",
      "(27892, 83) (282, 83)\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",
    "df_slice = df_slice.sort_values(by=[\"request_id\", \"preference\"])\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": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934954Z",
     "start_time": "2024-05-16T13:59:41.934946Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.595595Z",
     "iopub.status.busy": "2024-07-01T02:18:52.595455Z",
     "iopub.status.idle": "2024-07-01T02:18:52.607439Z",
     "shell.execute_reply": "2024-07-01T02:18:52.607012Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.595580Z"
    }
   },
   "outputs": [],
   "source": [
    "# val_df[[\"request_id\", \"metadata\", \"updated_at\", \"user_id\", \"preference\"]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.935620Z",
     "start_time": "2024-05-16T13:59:41.935613Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:52.608116Z",
     "iopub.status.busy": "2024-07-01T02:18:52.607979Z",
     "iopub.status.idle": "2024-07-01T02:18:53.487803Z",
     "shell.execute_reply": "2024-07-01T02:18:53.487280Z",
     "shell.execute_reply.started": "2024-07-01T02:18:52.608102Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 27892/27892 [00:00<00:00, 33129.81it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1,364 hours of 27892 clips, 2.490357142857143 nodes\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "total_duration = 0\n",
    "for i, row in tqdm.tqdm(train_df.iterrows(), total=len(train_df)):\n",
    "    # we need to alternate between preference: neg, pos\n",
    "    # print(i, row)\n",
    "    try:\n",
    "        assert row[\"preference\"] == (i % 2 == 1)\n",
    "    except:\n",
    "        print(i, row)\n",
    "    total_duration += row[\"duration\"]\n",
    "print(f\"{round(total_duration / 60 / 60):,} hours of {train_df.shape[0]} clips, {train_df.shape[0] / 8 / 2 / 700} nodes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936268Z",
     "start_time": "2024-05-16T13:59:41.936260Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:53.488581Z",
     "iopub.status.busy": "2024-07-01T02:18:53.488434Z",
     "iopub.status.idle": "2024-07-01T02:18:56.658350Z",
     "shell.execute_reply": "2024-07-01T02:18:56.657815Z",
     "shell.execute_reply.started": "2024-07-01T02:18:53.488564Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 282/282 [00:03<00:00, 91.44it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 282 clips\n",
      "7 hours of False\n",
      "7 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": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936964Z",
     "start_time": "2024-05-16T13:59:41.936957Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:18:56.659168Z",
     "iopub.status.busy": "2024-07-01T02:18:56.659018Z",
     "iopub.status.idle": "2024-07-01T02:25:39.622864Z",
     "shell.execute_reply": "2024-07-01T02:25:39.622314Z",
     "shell.execute_reply.started": "2024-07-01T02:18:56.659152Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████| 27892/27892 [06:42<00:00, 69.23it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 27892 clips\n",
      "711 hours of False\n",
      "711 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": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.937879Z",
     "start_time": "2024-05-16T13:59:41.937870Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:39.623734Z",
     "iopub.status.busy": "2024-07-01T02:25:39.623576Z",
     "iopub.status.idle": "2024-07-01T02:25:39.993789Z",
     "shell.execute_reply": "2024-07-01T02:25:39.993313Z",
     "shell.execute_reply.started": "2024-07-01T02:25:39.623718Z"
    }
   },
   "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": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.938629Z",
     "start_time": "2024-05-16T13:59:41.938621Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:39.994543Z",
     "iopub.status.busy": "2024-07-01T02:25:39.994399Z",
     "iopub.status.idle": "2024-07-01T02:25:40.008485Z",
     "shell.execute_reply": "2024-07-01T02:25:40.008072Z",
     "shell.execute_reply.started": "2024-07-01T02:25:39.994527Z"
    }
   },
   "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": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939205Z",
     "start_time": "2024-05-16T13:59:41.939198Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.009181Z",
     "iopub.status.busy": "2024-07-01T02:25:40.009045Z",
     "iopub.status.idle": "2024-07-01T02:25:40.047200Z",
     "shell.execute_reply": "2024-07-01T02:25:40.046786Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.009167Z"
    }
   },
   "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": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939977Z",
     "start_time": "2024-05-16T13:59:41.939969Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.049283Z",
     "iopub.status.busy": "2024-07-01T02:25:40.049134Z",
     "iopub.status.idle": "2024-07-01T02:25:40.083787Z",
     "shell.execute_reply": "2024-07-01T02:25:40.083369Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.049267Z"
    }
   },
   "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": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.940610Z",
     "start_time": "2024-05-16T13:59:41.940603Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.084468Z",
     "iopub.status.busy": "2024-07-01T02:25:40.084335Z",
     "iopub.status.idle": "2024-07-01T02:25:40.121440Z",
     "shell.execute_reply": "2024-07-01T02:25:40.121017Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.084453Z"
    }
   },
   "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": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941167Z",
     "start_time": "2024-05-16T13:59:41.941159Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.122115Z",
     "iopub.status.busy": "2024-07-01T02:25:40.121980Z",
     "iopub.status.idle": "2024-07-01T02:25:40.160666Z",
     "shell.execute_reply": "2024-07-01T02:25:40.160247Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.122099Z"
    }
   },
   "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": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941801Z",
     "start_time": "2024-05-16T13:59:41.941793Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.161442Z",
     "iopub.status.busy": "2024-07-01T02:25:40.161304Z",
     "iopub.status.idle": "2024-07-01T02:25:40.194876Z",
     "shell.execute_reply": "2024-07-01T02:25:40.194428Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.161427Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "141 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",
    "validation_on_metas(test_metas)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.942520Z",
     "start_time": "2024-05-16T13:59:41.942511Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.195670Z",
     "iopub.status.busy": "2024-07-01T02:25:40.195535Z",
     "iopub.status.idle": "2024-07-01T02:25:40.228981Z",
     "shell.execute_reply": "2024-07-01T02:25:40.228547Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.195655Z"
    }
   },
   "outputs": [],
   "source": [
    "train_info = read_json(os.path.join(OUT_DATA_DIR, f\"info_tr.json\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.943072Z",
     "start_time": "2024-05-16T13:59:41.943065Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.229684Z",
     "iopub.status.busy": "2024-07-01T02:25:40.229541Z",
     "iopub.status.idle": "2024-07-01T02:25:40.260840Z",
     "shell.execute_reply": "2024-07-01T02:25:40.260418Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.229669Z"
    }
   },
   "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": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.944246Z",
     "start_time": "2024-05-16T13:59:41.944237Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.261540Z",
     "iopub.status.busy": "2024-07-01T02:25:40.261402Z",
     "iopub.status.idle": "2024-07-01T02:25:40.297326Z",
     "shell.execute_reply": "2024-07-01T02:25:40.296889Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.261525Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total samples 27892 (27892, 83)\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": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945249Z",
     "start_time": "2024-05-16T13:59:41.945241Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:29:27.098138Z",
     "iopub.status.busy": "2024-07-01T02:29:27.097849Z",
     "iopub.status.idle": "2024-07-01T02:29:27.111866Z",
     "shell.execute_reply": "2024-07-01T02:29:27.111416Z",
     "shell.execute_reply.started": "2024-07-01T02:29:27.098121Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 epoch per batch 4, total 217.90625\n"
     ]
    }
   ],
   "source": [
    "print(\"1 epoch per batch 4, total\", total_iters / 8 / 4 / 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945972Z",
     "start_time": "2024-05-16T13:59:41.945964Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.334016Z",
     "iopub.status.busy": "2024-07-01T02:25:40.333879Z",
     "iopub.status.idle": "2024-07-01T02:25:40.365682Z",
     "shell.execute_reply": "2024-07-01T02:25:40.365264Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.334001Z"
    }
   },
   "outputs": [],
   "source": [
    "# !cd /home/tony/Work/tony/slurm/extend/ && sbatch sbatch_ipo_13b_extend"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# some gymathtics loading prev data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.946562Z",
     "start_time": "2024-05-16T13:59:41.946555Z"
    },
    "execution": {
     "iopub.execute_input": "2024-07-01T02:25:40.366384Z",
     "iopub.status.busy": "2024-07-01T02:25:40.366247Z",
     "iopub.status.idle": "2024-07-01T02:25:40.403935Z",
     "shell.execute_reply": "2024-07-01T02:25:40.403523Z",
     "shell.execute_reply.started": "2024-07-01T02:25:40.366370Z"
    }
   },
   "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
}
