{ "cells": [ { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "from safetensors.torch import load_file, save_file\n", "import torch" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "file_path = \"/Qwen2.5-1.5B-Instruct/model.safetensors\"\n", "tensors = load_file(file_path)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "model.embed_tokens.weight torch.Size([151936, 1536])\n" ] } ], "source": [ "# 152064\n", "# 151642\n", "for name in tensors.keys():\n", " if 151936 in tensors[name].shape:\n", " print(name, tensors[name].shape)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "embed = \"model.embed_tokens.weight\"\n", "new_vocab = 152064\n", "source_shape = tensors[embed].shape\n", "target_shape = torch.Size((new_vocab, source_shape[-1]))\n", "\n", "source = tensors[embed]\n", "\n", "target = torch.zeros(target_shape, dtype=source.dtype, device=source.device)\n", "target[:source_shape[0], :] = source" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "updated_tensors = {**tensors, embed: target}\n", "save_file(updated_tensors, file_path)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "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.11.10" } }, "nbformat": 4, "nbformat_minor": 2 }