{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "root = \"model_assets/jvnv-F1\"\n",
    "pth_files = [os.path.join(root, f) for f in os.listdir(root) if f.endswith(\".pth\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "model_name = \"G_0.pth\"\n",
    "model = torch.load(f\"pretrained/{model_name}\", map_location=torch.device(\"cpu\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enc_p.emo_proj.bias\n",
      "enc_p.emo_q_proj.weight\n",
      "enc_p.emo_q_proj.bias\n"
     ]
    }
   ],
   "source": [
    "from safetensors.torch import save_file\n",
    "state_dict = model[\"model\"]\n",
    "new_dict = {}\n",
    "for k in state_dict.keys():\n",
    "    if k.startswith(\"enc_p.emo\"):\n",
    "        print(k)\n",
    "    else:\n",
    "        new_dict[k] = state_dict[k]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_file(new_dict, f\"pretrained/{model_name.replace('.pth', '.safetensors')}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "root_dir = \"model_assets\"\n",
    "\n",
    "safetensors_files = glob(f\"{root_dir}/**/*.safetensors\", recursive=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 推論に不要なenc_qを消し忘れていたのを削除\n",
    "\n",
    "from safetensors import safe_open\n",
    "from safetensors.torch import save_file\n",
    "\n",
    "for path in safetensors_files:\n",
    "    print(path)\n",
    "    tensors = {}\n",
    "    with safe_open(path, framework=\"pt\", device=\"cpu\") as f:\n",
    "        for key in f.keys():\n",
    "            if key.startswith(\"enc_q\"):\n",
    "                print(key)\n",
    "                continue\n",
    "            tensors[key] = f.get_tensor(key)\n",
    "    save_file(tensors, path.replace(\".safetensors\", \".new.safetensors\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "enc_p.xvec_proj.weight enc_p.style_proj.weight\n",
      "enc_p.xvec_proj.bias enc_p.style_proj.bias\n"
     ]
    }
   ],
   "source": [
    "# pthファイルを推論用safetensorsに変換\n",
    "from safetensors.torch import save_file\n",
    "from safetensors import safe_open\n",
    "import torch\n",
    "\n",
    "pth_path = \"model_assets/jvnv-F1/release_7000.pth\"\n",
    "pth_weight = torch.load(pth_path, map_location=torch.device(\"cpu\"))\n",
    "new_dict = {}\n",
    "for key in pth_weight[\"model\"]:\n",
    "    if key.startswith(\"enc_p.xvec_proj.\"):  # 前のモデルの名残\n",
    "        print(key, key.replace(\"enc_p.xvec_proj.\", \"enc_p.style_proj.\"))\n",
    "        new_dict[key.replace(\"enc_p.xvec_proj.\", \"enc_p.style_proj.\")] = pth_weight[\"model\"][key].clone().contiguous()  # よく分からないおまじないをしないとエラーになる\n",
    "    elif not key.startswith(\"enc_q\"):\n",
    "        new_dict[key] = pth_weight[\"model\"][key]\n",
    "    else:\n",
    "        continue\n",
    "new_dict[\"iteration\"] = torch.LongTensor([pth_weight[\"iteration\"]])\n",
    "save_file(new_dict, pth_path.replace(\".pth\", \".pth.safetensors\"))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
