{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from transformers import AutoModelForCausalLM, AutoTokenizer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from safetensors.torch import save_file, load_file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "CACHE_DIR = \"/huggingface/cache\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NEKOMATA_MODEL = \"rinna/nekomata-14b\"\n", "QARASU_MODEL = \"lightblue/qarasu-14B-chat-plus-unleashed\"\n", "QWEN_14B_MODEL = \"Qwen/Qwen-14B\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekomata = AutoModelForCausalLM.from_pretrained(\n", " NEKOMATA_MODEL,\n", " cache_dir=CACHE_DIR,\n", " torch_dtype=torch.bfloat16,\n", " device_map=\"cpu\",\n", " offload_folder=\"nekomata\",\n", " offload_state_dict=True,\n", " trust_remote_code=True,\n", ")\n", "nekomata.eval()\n", "nekomata.hf_device_map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekomata_state_dict = nekomata.state_dict().copy()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key in nekomata_state_dict.keys():\n", " nekomata_value = nekomata_state_dict[key].clone().to(\"cpu\")\n", " print(key, nekomata_value.dtype, nekomata_value.shape, nekomata_value)\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "save_file(nekomata_state_dict, \"./nekomata_state.safetensors\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Restart Runtime**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qarasu = AutoModelForCausalLM.from_pretrained(\n", " QARASU_MODEL,\n", " cache_dir=CACHE_DIR,\n", " torch_dtype=torch.bfloat16,\n", " device_map=\"cpu\",\n", " offload_folder=\"qarasu\",\n", " offload_state_dict=True,\n", " trust_remote_code=True,\n", ")\n", "qarasu.eval()\n", "qarasu.hf_device_map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Restart Runtime**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qarasu_state_dict = qarasu.state_dict().copy()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for key in qarasu_state_dict.keys():\n", " qarasu_value = qarasu_state_dict[key].clone().to(\"cpu\")\n", " print(key, qarasu_value.dtype, qarasu_value.shape, qarasu_value)\n", " break" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "save_file(qarasu_state_dict, \"./qarasu_state.safetensors\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Restart Runtime**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qwen14b = AutoModelForCausalLM.from_pretrained(\n", " QWEN_14B_MODEL,\n", " cache_dir=CACHE_DIR,\n", " torch_dtype=torch.bfloat16,\n", " device_map=\"cpu\",\n", " offload_folder=\"qwen\",\n", " offload_state_dict=True,\n", " trust_remote_code=True,\n", ")\n", "qwen14b.eval()\n", "qwen14b.hf_device_map" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qwen14b_state_dict = qwen14b.state_dict().copy()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "save_file(qwen14b_state_dict, \"./qwen14b_state.safetensors\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Restart Runtime**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from safetensors.torch import save_file, load_file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekomata_state_dict = load_file(\"./nekomata_state.safetensors\", device=\"cpu\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qarasu_state_dict = load_file(\"./qarasu_state.safetensors\", device=\"cpu\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_state_dict = nekomata_state_dict\n", "\n", "with torch.no_grad():\n", " for key in nekomata_state_dict.keys():\n", " print(key)\n", "\n", " new_state_dict[key] = (\n", " new_state_dict[key].to(\"cuda\") + qarasu_state_dict[key].to(\"cuda\")\n", " ).to(\"cpu\")\n", "\n", "new_state_dict" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del nekomata_state_dict, qarasu_state_dict" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "torch.cuda.empty_cache()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "save_file(new_state_dict, \"./nekomata+qarasu_state.safetensors\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Restart Runtime**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from safetensors.torch import load_file, save_file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekomata_qarasu_state_dict = load_file(\n", " \"./nekomata+qarasu_state.safetensors\", device=\"cpu\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qwen14b_state_dict = load_file(\"./qwen14b_state.safetensors\", device=\"cpu\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# キー名が同じことを確認\n", "for neko_key, qwen14b_key in zip(\n", " nekomata_qarasu_state_dict.keys(), qwen14b_state_dict.keys()\n", "):\n", " assert neko_key == qwen14b_key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "new_state_dict = nekomata_qarasu_state_dict\n", "\n", "with torch.no_grad():\n", " for key in new_state_dict.keys():\n", " print(key)\n", "\n", " new_state_dict[key] = (\n", " new_state_dict[key].to(\"cuda\") - qwen14b_state_dict[key].to(\"cuda\")\n", " ).to(\"cpu\")\n", "\n", "new_state_dict" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "save_file(new_state_dict, \"./nekoqarasu_state.safetensors\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Restart Runtime**\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import torch\n", "from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "CACHE_DIR = \"/huggingface/cache\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "QWEN_14B_CHAT_MODEL = \"Qwen/Qwen-14B-Chat\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "qwen_chat_config = AutoConfig.from_pretrained(\n", " QWEN_14B_CHAT_MODEL, trust_remote_code=True, cache_dir=CACHE_DIR\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekoqarasu = AutoModelForCausalLM.from_config(\n", " qwen_chat_config,\n", " trust_remote_code=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from safetensors.torch import load_file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "state_dict = load_file(\"./nekoqarasu_state.safetensors\", device=\"cpu\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekoqarasu.load_state_dict(\n", " state_dict,\n", " strict=False,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "nekoqarasu.push_to_hub(\"nekoqarasu-14b-chat\", private=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tokenizer = AutoTokenizer.from_pretrained(\n", " QWEN_14B_CHAT_MODEL, cache_dir=CACHE_DIR, trust_remote_code=True\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tokenizer.push_to_hub(\"nekoqarasu-14b-chat\", private=True)" ] } ], "metadata": { "kernelspec": { "display_name": "py310", "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 }