{
 "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": "2025-02-25T01:29:31.729492Z",
     "iopub.status.busy": "2025-02-25T01:29:31.729317Z",
     "iopub.status.idle": "2025-02-25T01:29:33.800756Z",
     "shell.execute_reply": "2025-02-25T01:29:33.800288Z",
     "shell.execute_reply.started": "2025-02-25T01:29:31.729478Z"
    }
   },
   "outputs": [],
   "source": [
    "# setup autoload\n",
    "%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_30b_task 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",
    "import matplotlib.pyplot as plt\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": "2025-02-25T01:29:33.801399Z",
     "iopub.status.busy": "2025-02-25T01:29:33.801208Z",
     "iopub.status.idle": "2025-02-25T01:29:33.837683Z",
     "shell.execute_reply": "2025-02-25T01:29:33.837283Z",
     "shell.execute_reply.started": "2025-02-25T01:29:33.801387Z"
    }
   },
   "outputs": [],
   "source": [
    "OUT_DATA_DIR = \"/app/suno/data/dpo/13b_s32_v22/\"\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/concat_cycle_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": "2025-02-25T01:29:33.838343Z",
     "iopub.status.busy": "2025-02-25T01:29:33.838163Z",
     "iopub.status.idle": "2025-02-25T01:29:36.899982Z",
     "shell.execute_reply": "2025-02-25T01:29:36.899471Z",
     "shell.execute_reply.started": "2025-02-25T01:29:33.838326Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Preference data shape (216002, 152)\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/13b_v32/interesting_clips_v4_h_s_32_20250222_full_long_final.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": "2025-02-25T01:29:36.901275Z",
     "iopub.status.busy": "2025-02-25T01:29:36.901038Z",
     "iopub.status.idle": "2025-02-25T01:29:38.269202Z",
     "shell.execute_reply": "2025-02-25T01:29:38.268708Z",
     "shell.execute_reply.started": "2025-02-25T01:29:36.901262Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "457458\n",
      "457458\n",
      "pre-downloaded df (216002, 152)\n",
      "downloaded df (214457, 152)\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": "2025-02-25T01:29:38.269918Z",
     "iopub.status.busy": "2025-02-25T01:29:38.269709Z",
     "iopub.status.idle": "2025-02-25T01:29:38.318780Z",
     "shell.execute_reply": "2025-02-25T01:29:38.318403Z",
     "shell.execute_reply.started": "2025-02-25T01:29:38.269905Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "is_13b\n",
       "True    214457\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"is_13b\"] = df[\"model_name\"].str.contains(\"-s-\")\n",
    "df[\"is_13b\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:29:38.319409Z",
     "iopub.status.busy": "2025-02-25T01:29:38.319232Z",
     "iopub.status.idle": "2025-02-25T01:29:38.336028Z",
     "shell.execute_reply": "2025-02-25T01:29:38.335665Z",
     "shell.execute_reply.started": "2025-02-25T01:29:38.319396Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "is_public\n",
      "False    186370\n",
      "True      28087\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(df[\"is_public\"].value_counts())\n",
    "# remove public for now cause fucking users\n",
    "# df = df[~df[\"is_public\"]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LET's do the data prep"
   ]
  },
  {
   "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": "2025-02-25T01:29:38.336737Z",
     "iopub.status.busy": "2025-02-25T01:29:38.336461Z",
     "iopub.status.idle": "2025-02-25T01:29:38.529272Z",
     "shell.execute_reply": "2025-02-25T01:29:38.528773Z",
     "shell.execute_reply.started": "2025-02-25T01:29:38.336724Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "preference  model_name     \n",
      "False       chirp-v4-h-s-32    106617\n",
      "True        chirp-v4-h-s-32    107840\n",
      "Name: count, dtype: int64\n",
      "(214457, 152)\n",
      "(214457, 152)\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-v4-h-s-32\"])]\n",
    "print(df.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:58:56.909539Z",
     "start_time": "2024-05-16T13:58:56.595736Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:29:38.529942Z",
     "iopub.status.busy": "2025-02-25T01:29:38.529804Z",
     "iopub.status.idle": "2025-02-25T01:29:38.830217Z",
     "shell.execute_reply": "2025-02-25T01:29:38.829717Z",
     "shell.execute_reply.started": "2025-02-25T01:29:38.529929Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(214457, 152)\n",
      "(213062, 152)\n",
      "preference  model_name     \n",
      "False       chirp-v4-h-s-32    106531\n",
      "True        chirp-v4-h-s-32    106531\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": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:29:38.830943Z",
     "iopub.status.busy": "2025-02-25T01:29:38.830736Z",
     "iopub.status.idle": "2025-02-25T01:29:38.844060Z",
     "shell.execute_reply": "2025-02-25T01:29:38.843702Z",
     "shell.execute_reply.started": "2025-02-25T01:29:38.830930Z"
    }
   },
   "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": "2025-02-25T01:29:38.844597Z",
     "iopub.status.busy": "2025-02-25T01:29:38.844488Z",
     "iopub.status.idle": "2025-02-25T01:30:04.197305Z",
     "shell.execute_reply": "2025-02-25T01:30:04.196781Z",
     "shell.execute_reply.started": "2025-02-25T01:29:38.844586Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique_requests 106531\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": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:04.198199Z",
     "iopub.status.busy": "2025-02-25T01:30:04.197874Z",
     "iopub.status.idle": "2025-02-25T01:30:04.230172Z",
     "shell.execute_reply": "2025-02-25T01:30:04.229763Z",
     "shell.execute_reply.started": "2025-02-25T01:30:04.198185Z"
    }
   },
   "outputs": [],
   "source": [
    "# def modify_model_name(model_name, metadata):\n",
    "#     if (\n",
    "#         model_name.startswith(\"chirp-v3p5-engine-t\")\n",
    "#         or model_name.startswith(\"chirp-v3p5-engine-s\")\n",
    "#         or model_name.startswith(\"chirp-v4\")\n",
    "#         or model_name.startswith(\"chirp-v3p5-h-s-31\")\n",
    "#     ):\n",
    "#         if \"param_experiment\" in metadata:\n",
    "#             exp = metadata.get(\"param_experiment\", \"\")\n",
    "#             if exp:\n",
    "#                 return f\"{model_name}_{exp}\"\n",
    "#     return model_name\n",
    "\n",
    "\n",
    "# df[\"param_model_name\"] = df.apply(\n",
    "#     lambda row: modify_model_name(row[\"model_name\"], row[\"metadata\"]), axis=1\n",
    "# )\n",
    "# diff_experiments = (df[\"param_model_name\"].str.contains(\"text_\"))| (df[\"param_model_name\"].str.contains(\"step_\")) |  (df[\"param_model_name\"].str.contains(\"tk_\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:04.230818Z",
     "iopub.status.busy": "2025-02-25T01:30:04.230633Z",
     "iopub.status.idle": "2025-02-25T01:30:04.271194Z",
     "shell.execute_reply": "2025-02-25T01:30:04.270788Z",
     "shell.execute_reply.started": "2025-02-25T01:30:04.230805Z"
    }
   },
   "outputs": [],
   "source": [
    "# # try taking out diffusion experiment -- cause they could literally be noise\n",
    "# print(\"unique_requests\", df[\"request_id\"].nunique())\n",
    "# df = df[~diff_experiments].copy()\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(\"unique_requests\", df[\"request_id\"].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:36.393047Z",
     "start_time": "2024-05-16T13:59:36.048831Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:04.273274Z",
     "iopub.status.busy": "2025-02-25T01:30:04.272896Z",
     "iopub.status.idle": "2025-02-25T01:30:04.331662Z",
     "shell.execute_reply": "2025-02-25T01:30:04.331170Z",
     "shell.execute_reply.started": "2025-02-25T01:30:04.273260Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "unique_requests 106531\n"
     ]
    }
   ],
   "source": [
    "# GPT requests are also fine for now\n",
    "print(\"unique_requests\", df[\"request_id\"].nunique())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:40.799375Z",
     "start_time": "2024-05-16T13:59:36.394236Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:04.332456Z",
     "iopub.status.busy": "2025-02-25T01:30:04.332261Z",
     "iopub.status.idle": "2025-02-25T01:30:05.493950Z",
     "shell.execute_reply": "2025-02-25T01:30:05.493473Z",
     "shell.execute_reply.started": "2025-02-25T01:30:04.332442Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6646\n",
      "good_continue_at\n",
      "True    213062\n",
      "Name: count, dtype: int64\n",
      "\n",
      " Check some basics... \n",
      " preference\n",
      "False    106531\n",
      "True     106531\n",
      "Name: count, dtype: int64 is_13b\n",
      "True    213062\n",
      "Name: count, dtype: int64 model_name\n",
      "chirp-v4-h-s-32    213062\n",
      "Name: count, dtype: int64 preference  model_name     \n",
      "False       chirp-v4-h-s-32    106531\n",
      "True        chirp-v4-h-s-32    106531\n",
      "Name: count, dtype: int64\n",
      "task\n",
      "                 198468\n",
      "extend            10518\n",
      "upload_extend      4074\n",
      "cover                 2\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()\n",
    "print(df[\"task\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:05.494670Z",
     "iopub.status.busy": "2025-02-25T01:30:05.494459Z",
     "iopub.status.idle": "2025-02-25T01:30:05.778980Z",
     "shell.execute_reply": "2025-02-25T01:30:05.778563Z",
     "shell.execute_reply.started": "2025-02-25T01:30:05.494657Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pos_diff_preference\n",
       "2.0    106531\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.sort_values(by=[\"request_id\", \"preference\", \"diff_preference\"])\n",
    "df[\"pos_diff_preference\"] = df[\"diff_preference\"].diff()\n",
    "# df[\"cer_diff_preference\"] = df[\"cer\"].diff()\n",
    "df[df[\"preference\"]][\"pos_diff_preference\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:05.779605Z",
     "iopub.status.busy": "2025-02-25T01:30:05.779475Z",
     "iopub.status.idle": "2025-02-25T01:30:05.792404Z",
     "shell.execute_reply": "2025-02-25T01:30:05.792055Z",
     "shell.execute_reply.started": "2025-02-25T01:30:05.779592Z"
    }
   },
   "outputs": [],
   "source": [
    "# df[df[\"preference\"]][\"cer_diff_preference\"].hist(bins=50)\n",
    "# print(df[df[\"preference\"]][\"cer_diff_preference\"].quantile(0.95))\n",
    "# plt.show()\n",
    "# print(df[df[\"preference\"]][\"cer\"].hist(bins=50))\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:05.792952Z",
     "iopub.status.busy": "2025-02-25T01:30:05.792844Z",
     "iopub.status.idle": "2025-02-25T01:30:05.902154Z",
     "shell.execute_reply": "2025-02-25T01:30:05.901645Z",
     "shell.execute_reply.started": "2025-02-25T01:30:05.792942Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "positive with likes (68145, 152)\n"
     ]
    }
   ],
   "source": [
    "test_mask = (df[\"preference\"] == True) & (\n",
    "    (df[\"upvote_count\"] >= 1)\n",
    ")\n",
    "print(\"positive with likes\", df[test_mask].shape)\n",
    "# positive with likes (285955, 149) -- 0215 data\n",
    "# positive with likes (517621, 151) -- 0217 data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.035167Z",
     "start_time": "2024-05-16T13:59:40.801098Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:05.902959Z",
     "iopub.status.busy": "2025-02-25T01:30:05.902743Z",
     "iopub.status.idle": "2025-02-25T01:30:06.202845Z",
     "shell.execute_reply": "2025-02-25T01:30:06.202350Z",
     "shell.execute_reply.started": "2025-02-25T01:30:05.902946Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "negative 106531 positive 106530\n",
      "total pair requests 106531 selected pair requests 106530 frac 1.000\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[\"norm_play_frac\"] >= df[\"reaction_play_count\"] / 3) # play duration is not low on average\n",
    "    & (df[\"pos_diff_preference\"] == 2)\n",
    "    & (df[\"task\"].isin([\"upload_extend\", \"extend\", \"\"]))\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": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.250737Z",
     "start_time": "2024-05-16T13:59:41.036434Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:06.203509Z",
     "iopub.status.busy": "2025-02-25T01:30:06.203387Z",
     "iopub.status.idle": "2025-02-25T01:30:06.724810Z",
     "shell.execute_reply": "2025-02-25T01:30:06.724306Z",
     "shell.execute_reply.started": "2025-02-25T01:30:06.203497Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " requests 106530 clips 213060 total khrs 11.323; N gpus for 1000 iters 13.316; 8 gpus for x iters 832.266; n unique users 25050 n pro users 22553\n"
     ]
    }
   ],
   "source": [
    "df_slice = df[df[\"request_id\"].isin(set(unique_requests))].copy()\n",
    "print(\n",
    "    f\"{os.path.basename(OUT_DATA_DIR)} 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\"8 gpus for x iters {df_slice.shape[0] / 8 / 8 / 4:.3f};\",\n",
    "    f\"n unique users {df_slice['user_id'].nunique()}\",\n",
    "    f\"n pro users {df_slice[df_slice['is_pro_user']]['user_id'].nunique()}\",\n",
    ")\n",
    "# 76171 152342 total khrs 2.880 n gpus for 1250 iters 3.809\n",
    "# 8 v9 requests 106299 clips 212598 total khrs 9.776; N gpus for 1000 iters 13.287; n unique users 27775\n",
    "# 29 v2 requests 22572 clips 45144 total khrs 2.252; N gpus for 1000 iters 2.821; 4 gpus for x iters 705.375; n unique users 18149 n pro users 10155\n",
    "# 29 v4 requests 32479 clips 64958 total khrs 3.248; N gpus for 1000 iters 4.060; 4 gpus for x iters 1014.969; n unique users 25192 n pro users 13547\n",
    "# samve for v5\n",
    "# 31 v9  requests 63922 clips 127844 total khrs 6.631; N gpus for 1000 iters 7.990; 4 gpus for x iters 1997.562; n unique users 25461 n pro users 24396\n",
    "# 32 v1  requests 42336 clips 84672 total khrs 4.311; N gpus for 1000 iters 5.292; 4 gpus for x iters 1323.000; n unique users 20440 n pro users 14949\n",
    "# 32 v2  requests 83218 clips 166436 total khrs 8.672; N gpus for 1000 iters 10.402; 4 gpus for x iters 2600.562; n unique users 36912 n pro users 24545\n",
    "# 32 v3  requests 71020 clips 142040 total khrs 7.562; N gpus for 1000 iters 8.877; 4 gpus for x iters 2219.375; n unique users 25143 n pro users 22341\n",
    "# 32 v5  requests 83743 clips 167486 total khrs 8.923; N gpus for 1000 iters 10.468; 4 gpus for x iters 2616.969; n unique users 28348 n pro users 24894\n",
    "# 32 v6  requests 63924 clips 127848 total khrs 6.725; N gpus for 1000 iters 7.990; 4 gpus for x iters 1997.625; n unique users 24675 n pro users 22071\n",
    "# 32 v7  requests 106565 clips 213130 total khrs 11.335; N gpus for 1000 iters 13.321; 4 gpus for x iters 3330.156; n unique users 34779 n pro users 29833\n",
    "# 32 v8  requests 135955 clips 271910 total khrs 14.459; N gpus for 1000 iters 16.994; 4 gpus for x iters 4248.594; n unique users 42469 n pro users 35114\n",
    "# 32 v9  requests 60046 clips 120092 total khrs 6.444; N gpus for 1000 iters 7.506; 4 gpus for x iters 1876.438; n unique users 15885 n pro users 14566\n",
    "# 32 v10  requests 79415 clips 158830 total khrs 8.497; N gpus for 1000 iters 9.927; 4 gpus for x iters 2481.719; n unique users 19892 n pro users 17860\n",
    "# 32 v13  requests 173814 clips 347628 total khrs 18.463; N gpus for 1000 iters 21.727; 4 gpus for x iters 5431.688; n unique users 43848 n pro users 39116\n",
    "# 32 v14  requests 87118 clips 174236 total khrs 8.689; N gpus for 1000 iters 10.890; 4 gpus for x iters 2722.438; n unique users 22826 n pro users 20899\n",
    "# 32 v15  requests 68523 clips 137046 total khrs 7.169; N gpus for 1000 iters 8.565; 4 gpus for x iters 1070.672; n unique users 26101 n pro users 23037\n",
    "# 32 v16  requests 88908 clips 177816 total khrs 8.876; N gpus for 1000 iters 11.114; 4 gpus for x iters 1389.188; n unique users 23225 n pro users 21243\n",
    "# 32 v17  requests 43677 clips 87354 total khrs 4.468; N gpus for 1000 iters 5.460; 4 gpus for x iters 682.453; n unique users 17554 n pro users 16917\n",
    "# 32 v18  requests 89035 clips 178070 total khrs 9.312; N gpus for 1000 iters 11.129; 4 gpus for x iters 1391.172; n unique users 26572 n pro users 23755\n",
    "# 32 v19  requests 117202 clips 234404 total khrs 12.520; N gpus for 1000 iters 14.650; 8 gpus for x iters 915.641; n unique users 31847 n pro users 28249\n",
    "# 32 v20  requests 52910 clips 105820 total khrs 5.572; N gpus for 1000 iters 6.614; 4 gpus for x iters 826.719; n unique users 16708 n pro users 15184\n",
    "# 32 v21 requests 108001 clips 216002 total khrs 11.480; N gpus for 1000 iters 13.500; 8 gpus for x iters 843.758; n unique users 25205 n pro users 22700\n",
    "# 32 v22  requests 106530 clips 213060 total khrs 11.323; N gpus for 1000 iters 13.316; 8 gpus for x iters 832.266; n unique users 25050 n pro users 22553"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.277006Z",
     "start_time": "2024-05-16T13:59:41.252105Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:06.725627Z",
     "iopub.status.busy": "2025-02-25T01:30:06.725341Z",
     "iopub.status.idle": "2025-02-25T01:30:06.799577Z",
     "shell.execute_reply": "2025-02-25T01:30:06.799074Z",
     "shell.execute_reply.started": "2025-02-25T01:30:06.725613Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "positive in playlist (43307, 152)\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": 21,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:06.800300Z",
     "iopub.status.busy": "2025-02-25T01:30:06.800096Z",
     "iopub.status.idle": "2025-02-25T01:30:07.155125Z",
     "shell.execute_reply": "2025-02-25T01:30:07.154624Z",
     "shell.execute_reply.started": "2025-02-25T01:30:06.800286Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "positive with likes (68144, 152)\n"
     ]
    }
   ],
   "source": [
    "test_mask = (df_slice[\"preference\"] == True) & (\n",
    "    (df_slice[\"upvote_count\"] >= 1)\n",
    ")\n",
    "print(\"positive with likes\", df_slice[test_mask].shape)\n",
    "# positive with likes (60349, 149)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:07.155967Z",
     "iopub.status.busy": "2025-02-25T01:30:07.155639Z",
     "iopub.status.idle": "2025-02-25T01:30:07.168820Z",
     "shell.execute_reply": "2025-02-25T01:30:07.168451Z",
     "shell.execute_reply.started": "2025-02-25T01:30:07.155953Z"
    }
   },
   "outputs": [],
   "source": [
    "# BREAK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.323409Z",
     "start_time": "2024-05-16T13:59:41.278278Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:07.169471Z",
     "iopub.status.busy": "2025-02-25T01:30:07.169358Z",
     "iopub.status.idle": "2025-02-25T01:30:07.213228Z",
     "shell.execute_reply": "2025-02-25T01:30:07.212815Z",
     "shell.execute_reply.started": "2025-02-25T01:30:07.169460Z"
    }
   },
   "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": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.392244Z",
     "start_time": "2024-05-16T13:59:41.324472Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:07.213937Z",
     "iopub.status.busy": "2025-02-25T01:30:07.213685Z",
     "iopub.status.idle": "2025-02-25T01:30:07.270793Z",
     "shell.execute_reply": "2025-02-25T01:30:07.270356Z",
     "shell.execute_reply.started": "2025-02-25T01:30:07.213923Z"
    }
   },
   "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": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T14:00:20.866354Z",
     "start_time": "2024-05-16T14:00:12.443344Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:30:07.271390Z",
     "iopub.status.busy": "2025-02-25T01:30:07.271277Z",
     "iopub.status.idle": "2025-02-25T01:30:07.499789Z",
     "shell.execute_reply": "2025-02-25T01:30:07.498932Z",
     "shell.execute_reply.started": "2025-02-25T01:30:07.271379Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(213060, 152)\n",
      "task\n",
      "                 198468\n",
      "extend            10518\n",
      "upload_extend      4074\n",
      "Name: count, dtype: int64\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'BREAK' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[25], line 5\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28mprint\u001b[39m(df_slice\u001b[38;5;241m.\u001b[39mshape)\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(df_slice[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtask\u001b[39m\u001b[38;5;124m\"\u001b[39m]\u001b[38;5;241m.\u001b[39mvalue_counts())\n\u001b[0;32m----> 5\u001b[0m \u001b[43mBREAK\u001b[49m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'BREAK' is not defined"
     ]
    }
   ],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/13b_v32/interesting_clips_v4_h_s_32_20250222_full_long_final.csv\")\n",
    "# df_slice.to_pickle(\"/home/tony/Data/Preference/13b_v32/interesting_clips_v4_h_s_32_20250222_full_long_final.pkl\")\n",
    "print(df_slice.shape)\n",
    "print(df_slice[\"task\"].value_counts())\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": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932296Z",
     "start_time": "2024-05-16T13:59:41.932287Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:03.173204Z",
     "iopub.status.busy": "2025-02-25T01:32:03.172870Z",
     "iopub.status.idle": "2025-02-25T01:32:03.940712Z",
     "shell.execute_reply": "2025-02-25T01:32:03.940220Z",
     "shell.execute_reply.started": "2025-02-25T01:32:03.173188Z"
    }
   },
   "outputs": [],
   "source": [
    "# don't have continue at\n",
    "# df_slice[df_slice[\"continue_at\"].isna()][\"request_id\"].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.932966Z",
     "start_time": "2024-05-16T13:59:41.932957Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:03.941716Z",
     "iopub.status.busy": "2025-02-25T01:32:03.941508Z",
     "iopub.status.idle": "2025-02-25T01:32:03.998226Z",
     "shell.execute_reply": "2025-02-25T01:32:03.997764Z",
     "shell.execute_reply.started": "2025-02-25T01:32:03.941703Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "106530\n"
     ]
    }
   ],
   "source": [
    "final_filtered_requests = df_slice[\"request_id\"].unique()\n",
    "print(len(final_filtered_requests))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.933558Z",
     "start_time": "2024-05-16T13:59:41.933550Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:03.998896Z",
     "iopub.status.busy": "2025-02-25T01:32:03.998710Z",
     "iopub.status.idle": "2025-02-25T01:32:04.016931Z",
     "shell.execute_reply": "2025-02-25T01:32:04.016554Z",
     "shell.execute_reply.started": "2025-02-25T01:32:03.998883Z"
    }
   },
   "outputs": [],
   "source": [
    "# df_slice.to_csv(\"/home/tony/Data/Preference/7b_v2/7b_before_recode_20240412\", index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934277Z",
     "start_time": "2024-05-16T13:59:41.934268Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:04.017463Z",
     "iopub.status.busy": "2025-02-25T01:32:04.017351Z",
     "iopub.status.idle": "2025-02-25T01:32:04.556122Z",
     "shell.execute_reply": "2025-02-25T01:32:04.555633Z",
     "shell.execute_reply.started": "2025-02-25T01:32:04.017452Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "105464 1066\n",
      "(210928, 152) (2132, 152)\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": 30,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:04.557376Z",
     "iopub.status.busy": "2025-02-25T01:32:04.557155Z",
     "iopub.status.idle": "2025-02-25T01:32:04.570115Z",
     "shell.execute_reply": "2025-02-25T01:32:04.569765Z",
     "shell.execute_reply.started": "2025-02-25T01:32:04.557362Z"
    }
   },
   "outputs": [],
   "source": [
    "# BREAK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Actually make"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.934954Z",
     "start_time": "2024-05-16T13:59:41.934946Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:04.570828Z",
     "iopub.status.busy": "2025-02-25T01:32:04.570534Z",
     "iopub.status.idle": "2025-02-25T01:32:04.608088Z",
     "shell.execute_reply": "2025-02-25T01:32:04.607691Z",
     "shell.execute_reply.started": "2025-02-25T01:32:04.570816Z"
    }
   },
   "outputs": [],
   "source": [
    "# val_df[[\"request_id\", \"metadata\", \"updated_at\", \"user_id\", \"preference\"]].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.935620Z",
     "start_time": "2024-05-16T13:59:41.935613Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:04.608624Z",
     "iopub.status.busy": "2025-02-25T01:32:04.608515Z",
     "iopub.status.idle": "2025-02-25T01:32:09.035004Z",
     "shell.execute_reply": "2025-02-25T01:32:09.034498Z",
     "shell.execute_reply.started": "2025-02-25T01:32:04.608613Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████| 210928/210928 [00:04<00:00, 48116.31it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11,209 hours of 210928 clips, 6.5915 nodes, 1098.5833333333333 steps\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 / 4 / 1000} nodes, {train_df.shape[0] / 8 / 6 / 4} steps\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936268Z",
     "start_time": "2024-05-16T13:59:41.936260Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:09.035749Z",
     "iopub.status.busy": "2025-02-25T01:32:09.035531Z",
     "iopub.status.idle": "2025-02-25T01:32:32.367099Z",
     "shell.execute_reply": "2025-02-25T01:32:32.366626Z",
     "shell.execute_reply.started": "2025-02-25T01:32:09.035736Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t_data_memmap is set to: 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2132/2132 [00:23<00:00, 92.48it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 1990 clips, 1 different prompts\n",
      "55 hours of False\n",
      "54 hours of True\n",
      "gen: 108.9 hours\n",
      "Error extend: 100\n",
      "Error upload_extend: 42\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": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.936964Z",
     "start_time": "2024-05-16T13:59:41.936957Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T01:32:32.367809Z",
     "iopub.status.busy": "2025-02-25T01:32:32.367570Z",
     "iopub.status.idle": "2025-02-25T02:18:13.438015Z",
     "shell.execute_reply": "2025-02-25T02:18:13.437521Z",
     "shell.execute_reply.started": "2025-02-25T01:32:32.367796Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t_data_memmap is set to: 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  7%|███████▋                                                                                                 | 15532/210928 [02:47<37:29, 86.85it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 162838, 178489e6-79a1-45a6-91a7-24fecf3ed3f3_gen_cycle: 10747 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "  8%|███████▉                                                                                                 | 15989/210928 [02:53<43:53, 74.02it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 167400, 09b9b0e2-4aa8-44e5-89a0-9d56b798c772_gen_cycle: 7247 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 26%|███████████████████████████▏                                                                             | 54628/210928 [11:33<34:55, 74.58it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 576316, 3b1aebc5-22e9-4617-922d-cda151fcf18f_gen_cycle: 6352 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 36%|█████████████████████████████████████▌                                                                   | 75334/210928 [16:06<30:00, 75.29it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 796724, 0dec69f5-8d21-4800-8195-971119ed0f81_gen_cycle: 10970 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 50%|████████████████████████████████████████████████████▏                                                   | 105809/210928 [23:05<23:06, 75.81it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 1120376, 062479a0-f1f6-4784-9b59-196fe40849cb_gen_cycle: 6959 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 55%|█████████████████████████████████████████████████████████▌                                              | 116753/210928 [25:29<21:19, 73.62it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 1234950, 0a07fc08-a351-4981-9919-98165e1577c2_gen_cycle: 8840 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 63%|█████████████████████████████████████████████████████████████████                                       | 132056/210928 [28:46<17:11, 76.44it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 1396734, 0b65a603-cdcd-4c75-b74c-2ad8cc740cdb_gen_cycle: 8222 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 63%|██████████████████████████████████████████████████████████████████                                      | 133860/210928 [29:10<16:08, 79.59it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 1414643, 5fad386c-3007-4c1d-8600-7ad6580dbcdb_gen_cycle: 6477 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 68%|███████████████████████████████████████████████████████████████████████▏                                | 144341/210928 [31:26<14:50, 74.80it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 1524970, 4b8b56d4-ddec-4ee6-8e8d-4c9761392842_gen_cycle: 8480 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " 95%|██████████████████████████████████████████████████████████████████████████████████████████████████▉     | 200556/210928 [43:26<02:19, 74.27it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overflow at 2119082, 57311cb7-32dd-4ba9-96b6-38941a83fba4_gen_cycle: 6455 > 6016\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████| 210928/210928 [45:39<00:00, 77.00it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total 196478 clips, 34 different prompts\n",
      "5,427 hours of False\n",
      "5,321 hours of True\n",
      "gen: 10748.0 hours\n",
      "Error upload_extend: 4032\n",
      "Error extend: 10418\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": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.937879Z",
     "start_time": "2024-05-16T13:59:41.937870Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.438982Z",
     "iopub.status.busy": "2025-02-25T02:18:13.438520Z",
     "iopub.status.idle": "2025-02-25T02:18:13.495324Z",
     "shell.execute_reply": "2025-02-25T02:18:13.494892Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.438966Z"
    }
   },
   "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": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.938629Z",
     "start_time": "2024-05-16T13:59:41.938621Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.495992Z",
     "iopub.status.busy": "2025-02-25T02:18:13.495799Z",
     "iopub.status.idle": "2025-02-25T02:18:13.510826Z",
     "shell.execute_reply": "2025-02-25T02:18:13.510473Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.495979Z"
    }
   },
   "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": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939205Z",
     "start_time": "2024-05-16T13:59:41.939198Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.511557Z",
     "iopub.status.busy": "2025-02-25T02:18:13.511260Z",
     "iopub.status.idle": "2025-02-25T02:18:13.548437Z",
     "shell.execute_reply": "2025-02-25T02:18:13.548048Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.511544Z"
    }
   },
   "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": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.939977Z",
     "start_time": "2024-05-16T13:59:41.939969Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.549077Z",
     "iopub.status.busy": "2025-02-25T02:18:13.548963Z",
     "iopub.status.idle": "2025-02-25T02:18:13.594242Z",
     "shell.execute_reply": "2025-02-25T02:18:13.593841Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.549066Z"
    }
   },
   "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": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.940610Z",
     "start_time": "2024-05-16T13:59:41.940603Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.595762Z",
     "iopub.status.busy": "2025-02-25T02:18:13.595574Z",
     "iopub.status.idle": "2025-02-25T02:18:13.646438Z",
     "shell.execute_reply": "2025-02-25T02:18:13.646023Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.595750Z"
    }
   },
   "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": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941167Z",
     "start_time": "2024-05-16T13:59:41.941159Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.646973Z",
     "iopub.status.busy": "2025-02-25T02:18:13.646860Z",
     "iopub.status.idle": "2025-02-25T02:18:13.687758Z",
     "shell.execute_reply": "2025-02-25T02:18:13.687349Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.646962Z"
    }
   },
   "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": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.941801Z",
     "start_time": "2024-05-16T13:59:41.941793Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.688480Z",
     "iopub.status.busy": "2025-02-25T02:18:13.688321Z",
     "iopub.status.idle": "2025-02-25T02:18:13.734814Z",
     "shell.execute_reply": "2025-02-25T02:18:13.734408Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.688468Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "995 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": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.942520Z",
     "start_time": "2024-05-16T13:59:41.942511Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.735420Z",
     "iopub.status.busy": "2025-02-25T02:18:13.735248Z",
     "iopub.status.idle": "2025-02-25T02:18:13.848416Z",
     "shell.execute_reply": "2025-02-25T02:18:13.847940Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.735408Z"
    }
   },
   "outputs": [],
   "source": [
    "train_info = read_json(os.path.join(OUT_DATA_DIR, f\"info_tr.json\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.943072Z",
     "start_time": "2024-05-16T13:59:41.943065Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.849201Z",
     "iopub.status.busy": "2025-02-25T02:18:13.848902Z",
     "iopub.status.idle": "2025-02-25T02:18:13.870036Z",
     "shell.execute_reply": "2025-02-25T02:18:13.869680Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.849187Z"
    }
   },
   "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": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.944246Z",
     "start_time": "2024-05-16T13:59:41.944237Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.870716Z",
     "iopub.status.busy": "2025-02-25T02:18:13.870452Z",
     "iopub.status.idle": "2025-02-25T02:18:13.907036Z",
     "shell.execute_reply": "2025-02-25T02:18:13.906650Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.870703Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total samples 196478 (210928, 152)\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": 45,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945249Z",
     "start_time": "2024-05-16T13:59:41.945241Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.907632Z",
     "iopub.status.busy": "2025-02-25T02:18:13.907466Z",
     "iopub.status.idle": "2025-02-25T02:18:13.946093Z",
     "shell.execute_reply": "2025-02-25T02:18:13.945701Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.907620Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 epoch per batch 4, total 3069.96875\n"
     ]
    }
   ],
   "source": [
    "print(\"1 epoch per batch 4, total\", total_iters / 8 / 4 / 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.945972Z",
     "start_time": "2024-05-16T13:59:41.945964Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:13.946674Z",
     "iopub.status.busy": "2025-02-25T02:18:13.946506Z",
     "iopub.status.idle": "2025-02-25T02:18:14.192736Z",
     "shell.execute_reply": "2025-02-25T02:18:14.192214Z",
     "shell.execute_reply.started": "2025-02-25T02:18:13.946662Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Submitted batch job 3598\n"
     ]
    }
   ],
   "source": [
    "!cd /home/tony/Work/tony/slurm/13b_dpo && sbatch sbatch_ipo_13b_s32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:14.193619Z",
     "iopub.status.busy": "2025-02-25T02:18:14.193377Z",
     "iopub.status.idle": "2025-02-25T02:18:14.216946Z",
     "shell.execute_reply": "2025-02-25T02:18:14.216582Z",
     "shell.execute_reply.started": "2025-02-25T02:18:14.193605Z"
    }
   },
   "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_s32_cycle.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": 48,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-16T13:59:41.946562Z",
     "start_time": "2024-05-16T13:59:41.946555Z"
    },
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:14.217575Z",
     "iopub.status.busy": "2025-02-25T02:18:14.217409Z",
     "iopub.status.idle": "2025-02-25T02:18:14.249433Z",
     "shell.execute_reply": "2025-02-25T02:18:14.249073Z",
     "shell.execute_reply.started": "2025-02-25T02:18:14.217562Z"
    }
   },
   "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": 49,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-02-25T02:18:14.250034Z",
     "iopub.status.busy": "2025-02-25T02:18:14.249869Z",
     "iopub.status.idle": "2025-02-25T02:18:14.285192Z",
     "shell.execute_reply": "2025-02-25T02:18:14.284819Z",
     "shell.execute_reply.started": "2025-02-25T02:18:14.250022Z"
    }
   },
   "outputs": [],
   "source": [
    "# train_df[\"lang\"].value_counts()"
   ]
  },
  {
   "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.15"
  },
  "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
}
