{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "import torch\n", "\n", "# Load the checkpoint\n", "input_state_dict = torch.load(\"vodka_v5_4_768-ep60-gs146640.ckpt\")\n", "\n", "root_is_state_dict = False\n", "if \"state_dict\" in input_state_dict:\n", " root_is_state_dict = True\n", " state_dict = input_state_dict[\"state_dict\"]\n", "else:\n", " state_dict = input_state_dict\n", "\n", "mappings = {\n", " \"attn_1.to_out.0\": \"attn_1.proj_out\",\n", " \"attn_1.to_k\": \"attn_1.k\",\n", " \"attn_1.to_q\": \"attn_1.q\",\n", " \"attn_1.to_v\": \"attn_1.v\"\n", "}\n", "\n", "def replace_all(key):\n", " for mapping in mappings:\n", " key = key.replace(mapping, mappings[mapping])\n", " return key\n", "\n", "# First, create a new state_dict with renamed keys\n", "renamed_state_dict = dict()\n", "for key in state_dict:\n", " renamed_state_dict[replace_all(key)] = state_dict[key]\n", "\n", "# Then, reshape the tensors in the renamed state_dict\n", "def reshape_tensors(sd):\n", " for key in sd:\n", " if \"attn_1\" in key and sd[key].shape == torch.Size([512, 512]):\n", " sd[key] = sd[key].unsqueeze(2).unsqueeze(3)\n", " return sd\n", "\n", "output_state_dict = reshape_tensors(renamed_state_dict)\n", "\n", "# Finally, save the updated state_dict\n", "if root_is_state_dict:\n", " input_state_dict[\"state_dict\"] = output_state_dict\n", " torch.save(input_state_dict, \"6_vodka_v5_768_adamw8bit_ep60.ckpt\")\n", "else:\n", " torch.save(output_state_dict, \"6_vodka_v5_768_adamw8bit_ep60.ckpt\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "fastai", "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.10" } }, "nbformat": 4, "nbformat_minor": 2 }